-
Notifications
You must be signed in to change notification settings - Fork 382
/
PlaceOpenBrace.cs
365 lines (329 loc) · 13.3 KB
/
PlaceOpenBrace.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
using System.Globalization;
using System.Management.Automation.Language;
using System.Text;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
/// <summary>
/// A formatting rule about whether braces should start on the same line or not.
/// </summary>
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
public class PlaceOpenBrace : ConfigurableRule
{
/// <summary>
/// Indicates if an open brace should be on the same line or on the next line of its preceding keyword.
///
/// Default value is true.
/// </summary>
[ConfigurableRuleProperty(defaultValue: true)]
public bool OnSameLine { get; protected set; }
/// <summary>
/// Indicates if a new line should or should not follow an open brace.
///
/// Default value is true.
/// </summary>
[ConfigurableRuleProperty(defaultValue: true)]
public bool NewLineAfter { get; protected set; }
/// <summary>
/// Indicates if open braces in a one line block should be ignored or not.
/// E.g. $x = if ($true) { "blah" } else { "blah blah" }
/// In the above example, if the property is set to true then the rule will
/// not fire a violation.
///
/// Default value if true.
/// </summary>
[ConfigurableRuleProperty(defaultValue: true)]
public bool IgnoreOneLineBlock { get; protected set; }
private List<Func<Token[], Ast, string, IEnumerable<DiagnosticRecord>>> violationFinders
= new List<Func<Token[], Ast, string, IEnumerable<DiagnosticRecord>>>();
private HashSet<Token> tokensToIgnore;
/// <summary>
/// Sets the configurable properties of this rule.
/// </summary>
/// <param name="paramValueMap">A dictionary that maps parameter name to it value. Must be non-null</param>
public override void ConfigureRule(IDictionary<string, object> paramValueMap)
{
base.ConfigureRule(paramValueMap);
if (OnSameLine)
{
violationFinders.Add(FindViolationsForBraceShouldBeOnSameLine);
}
else
{
violationFinders.Add(FindViolationsForBraceShouldNotBeOnSameLine);
}
if (NewLineAfter)
{
violationFinders.Add(FindViolationsForNoNewLineAfterBrace);
}
}
/// <summary>
/// Analyzes the given ast to find violations.
/// </summary>
/// <param name="ast">AST to be analyzed. This should be non-null</param>
/// <param name="fileName">Name of file that corresponds to the input AST.</param>
/// <returns>A an enumerable type containing the violations</returns>
public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null)
{
throw new ArgumentNullException("ast");
}
// TODO Should have the following option
// * no-empty-lines-after
var diagnosticRecords = new List<DiagnosticRecord>();
if (!Enable)
{
return diagnosticRecords;
}
var tokens = Helper.Instance.Tokens;
// Ignore open braces that are part of arguments to a command
// * E.g. get-process | % { "blah }
// In the above case even if OnSameLine == false, we should not
// flag the open brace as it would move the brace to the next line
// and will invalidate the command
var tokenOps = new TokenOperations(tokens, ast);
tokensToIgnore = new HashSet<Token>(tokenOps.GetOpenBracesInCommandElements());
// Ignore open braces that are part of a one line if-else statement
// E.g. $x = if ($true) { "blah" } else { "blah blah" }
if (IgnoreOneLineBlock)
{
foreach (var pair in tokenOps.GetBracePairsOnSameLine())
{
tokensToIgnore.Add(pair.Item1);
}
}
foreach (var violationFinder in violationFinders)
{
diagnosticRecords.AddRange(violationFinder(tokens, ast, fileName));
}
return diagnosticRecords;
}
/// <summary>
/// Retrieves the common name of this rule.
/// </summary>
public override string GetCommonName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.PlaceOpenBraceCommonName);
}
/// <summary>
/// Retrieves the description of this rule.
/// </summary>
public override string GetDescription()
{
return string.Format(CultureInfo.CurrentCulture, Strings.PlaceOpenBraceDescription);
}
/// <summary>
/// Retrieves the name of this rule.
/// </summary>
public override string GetName()
{
return string.Format(
CultureInfo.CurrentCulture,
Strings.NameSpaceFormat,
GetSourceName(),
Strings.PlaceOpenBraceName);
}
/// <summary>
/// Retrieves the severity of the rule: error, warning or information.
/// </summary>
public override RuleSeverity GetSeverity()
{
return RuleSeverity.Warning;
}
/// <summary>
/// Gets the severity of the returned diagnostic record: error, warning, or information.
/// </summary>
/// <returns></returns>
public DiagnosticSeverity GetDiagnosticSeverity()
{
return DiagnosticSeverity.Warning;
}
/// <summary>
/// Retrieves the name of the module/assembly the rule is from.
/// </summary>
public override string GetSourceName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
}
/// <summary>
/// Retrieves the type of the rule, Builtin, Managed or Module.
/// </summary>
public override SourceType GetSourceType()
{
return SourceType.Builtin;
}
private static string GetError(string errorString)
{
return string.Format(CultureInfo.CurrentCulture, errorString);
}
private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldBeOnSameLine(
Token[] tokens,
Ast ast,
string fileName)
{
for (int k = 2; k < tokens.Length; k++)
{
if (tokens[k].Kind == TokenKind.LCurly
&& tokens[k - 1].Kind == TokenKind.NewLine
&& !tokensToIgnore.Contains(tokens[k]))
{
var precedingExpression = tokens[k - 2];
Token optionalComment = null;
// If a comment is before the open brace, then take the token before the comment
if (precedingExpression.Kind == TokenKind.Comment && k > 2)
{
precedingExpression = tokens[k - 3];
optionalComment = tokens[k - 2];
}
yield return new DiagnosticRecord(
GetError(Strings.PlaceOpenBraceErrorShouldBeOnSameLine),
tokens[k].Extent,
GetName(),
GetDiagnosticSeverity(),
fileName,
null,
GetCorrectionsForBraceShouldBeOnSameLine(precedingExpression, optionalComment, tokens[k], fileName));
}
}
}
private IEnumerable<DiagnosticRecord> FindViolationsForNoNewLineAfterBrace(
Token[] tokens,
Ast ast,
string fileName)
{
for (int k = 0; k < tokens.Length - 1; k++)
{
// typically the last element is of kind endofinput,
// but this checks adds additional safeguard
if (tokens[k].Kind == TokenKind.EndOfInput)
{
break;
}
if (tokens[k].Kind == TokenKind.LCurly
&& tokens[k + 1].Kind != TokenKind.NewLine
&& !tokensToIgnore.Contains(tokens[k]))
{
yield return new DiagnosticRecord(
GetError(Strings.PlaceOpenBraceErrorNoNewLineAfterBrace),
tokens[k].Extent,
GetName(),
GetDiagnosticSeverity(),
fileName,
null,
GetCorrectionsForNoNewLineAfterBrace(tokens, k, fileName));
}
}
}
private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldNotBeOnSameLine(
Token[] tokens,
Ast ast,
string fileName)
{
for (int k = 1; k < tokens.Length; k++)
{
if (tokens[k].Kind == TokenKind.EndOfInput)
{
break;
}
if (tokens[k].Kind == TokenKind.LCurly
&& tokens[k - 1].Kind != TokenKind.NewLine
&& !tokensToIgnore.Contains(tokens[k]))
{
yield return new DiagnosticRecord(
GetError(Strings.PlaceOpenBraceErrorShouldNotBeOnSameLine),
tokens[k].Extent,
GetName(),
GetDiagnosticSeverity(),
fileName,
null,
GetCorrectionsForBraceShouldNotBeOnSameLine(tokens, k - 1, k, fileName));
}
}
}
private List<CorrectionExtent> GetCorrectionsForNoNewLineAfterBrace(
Token[] tokens,
int openBracePos,
string fileName)
{
var corrections = new List<CorrectionExtent>();
var extent = tokens[openBracePos].Extent;
corrections.Add(
new CorrectionExtent(
extent.StartLineNumber,
extent.EndLineNumber,
extent.StartColumnNumber,
extent.EndColumnNumber,
new StringBuilder().Append(extent.Text).Append(Environment.NewLine).ToString(),
fileName));
return corrections;
}
private List<CorrectionExtent> GetCorrectionsForBraceShouldNotBeOnSameLine(
Token[] tokens,
int prevTokenPos,
int closeBraceTokenPos,
string fileName)
{
var corrections = new List<CorrectionExtent>();
var prevToken = tokens[prevTokenPos];
var closeBraceToken = tokens[closeBraceTokenPos];
corrections.Add(
new CorrectionExtent(
prevToken.Extent.StartLineNumber,
closeBraceToken.Extent.EndLineNumber,
prevToken.Extent.StartColumnNumber,
closeBraceToken.Extent.EndColumnNumber,
(new StringBuilder())
.Append(prevToken.Text)
.AppendLine()
.Append(GetIndentation(tokens, closeBraceTokenPos))
.Append(closeBraceToken.Text)
.ToString(),
fileName));
return corrections;
}
private string GetIndentation(Token[] tokens, int refTokenPos)
{
return new String(' ', GetStartColumnNumberOfTokenLine(tokens, refTokenPos) - 1);
}
private int GetStartColumnNumberOfTokenLine(Token[] tokens, int refTokenPos)
{
var refToken = tokens[refTokenPos];
for (int k = refTokenPos - 1; k >= 0; k--)
{
if (tokens[k].Extent.StartLineNumber != refToken.Extent.StartLineNumber)
{
return tokens[k].Extent.StartColumnNumber;
}
}
return refToken.Extent.StartColumnNumber;
}
private List<CorrectionExtent> GetCorrectionsForBraceShouldBeOnSameLine(
Token precedingExpression,
Token optionalCommentOfPrecedingExpression,
Token lCurly,
string fileName)
{
var corrections = new List<CorrectionExtent>();
var optionalComment = optionalCommentOfPrecedingExpression != null ? $" {optionalCommentOfPrecedingExpression}" : string.Empty;
corrections.Add(
new CorrectionExtent(
precedingExpression.Extent.StartLineNumber,
lCurly.Extent.EndLineNumber,
precedingExpression.Extent.StartColumnNumber,
lCurly.Extent.EndColumnNumber,
$"{precedingExpression.Text} {lCurly.Text}{optionalComment}",
fileName));
return corrections;
}
}
}