Skip to content

Commit 5e534b6

Browse files
author
Kapil Borle
committed
Add corrections for operator violations
1 parent c06bf1f commit 5e534b6

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

Rules/UseWhitespace.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Globalization;
1717
using System.Linq;
1818
using System.Management.Automation.Language;
19+
using System.Text;
1920
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
2021

2122
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
@@ -156,10 +157,7 @@ private IEnumerable<DiagnosticRecord> FindOpenParenViolations(TokenOperations to
156157
private IEnumerable<CorrectionExtent> GetOpenBracketCorrections(Token token)
157158
{
158159
yield return new CorrectionExtent(
159-
token.Extent.StartLineNumber,
160-
token.Extent.EndLineNumber,
161-
token.Extent.StartColumnNumber,
162-
token.Extent.EndColumnNumber,
160+
token.Extent,
163161
whiteSpace + token.Text,
164162
token.Extent.File,
165163
GetError(ErrorKind.Brace));
@@ -173,7 +171,8 @@ private bool IsPreviousTokenApartByWhitespace(LinkedListNode<Token> tokenNode)
173171

174172
private IEnumerable<DiagnosticRecord> FindOperatorViolations(TokenOperations tokenOperations)
175173
{
176-
Func<LinkedListNode<Token>, bool> predicate = tokenNode => {
174+
Func<LinkedListNode<Token>, bool> predicate = tokenNode =>
175+
{
177176
return tokenNode.Previous != null
178177
&& IsPreviousTokenOnSameLine(tokenNode)
179178
&& IsPreviousTokenApartByWhitespace(tokenNode);
@@ -202,11 +201,35 @@ private IEnumerable<DiagnosticRecord> FindOperatorViolations(TokenOperations tok
202201
GetDiagnosticSeverity(),
203202
tokenOperations.Ast.Extent.File,
204203
null,
205-
null);
204+
GetOperatorCorrections(tokenNode.Value, hasWhitespaceBefore, hasWhitespaceAfter).ToList());
206205
}
207206
}
208207
}
209208

209+
private IEnumerable<CorrectionExtent> GetOperatorCorrections(
210+
Token token,
211+
bool hasWhitespaceBefore,
212+
bool hasWhitespaceAfter)
213+
{
214+
var sb = new StringBuilder();
215+
if (!hasWhitespaceBefore)
216+
{
217+
sb.Append(whiteSpace);
218+
}
219+
220+
sb.Append(token.Text);
221+
if (!hasWhitespaceAfter)
222+
{
223+
sb.Append(whiteSpace);
224+
}
225+
226+
yield return new CorrectionExtent(
227+
token.Extent,
228+
sb.ToString(),
229+
token.Extent.File,
230+
GetError(ErrorKind.Operator));
231+
}
232+
210233
private bool IsOperator(Token token)
211234
{
212235
return TokenTraits.HasTrait(token.Kind, TokenFlags.AssignmentOperator)

0 commit comments

Comments
 (0)