16
16
using System . Globalization ;
17
17
using System . Linq ;
18
18
using System . Management . Automation . Language ;
19
- using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
20
19
21
20
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . BuiltinRules
22
21
{
@@ -28,6 +27,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
28
27
#endif
29
28
class AlignAssignmentStatement : ConfigurableRule
30
29
{
30
+ private readonly char whitespaceChar = ' ' ;
31
31
32
32
private List < Func < TokenOperations , IEnumerable < DiagnosticRecord > > > violationFinders
33
33
= new List < Func < TokenOperations , IEnumerable < DiagnosticRecord > > > ( ) ;
@@ -91,6 +91,7 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
91
91
92
92
var nodeTuples = GetExtents ( tokenOps , hashtableAst ) ;
93
93
if ( nodeTuples == null
94
+ || nodeTuples . Count == 0
94
95
|| ! nodeTuples . All ( t => t . Item1 . StartLineNumber == t . Item2 . EndLineNumber ) )
95
96
{
96
97
continue ;
@@ -103,10 +104,10 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
103
104
? tAggregate
104
105
: t1 ;
105
106
} ) ;
106
- var expectedStartColumn = widestKeyExtent . EndColumnNumber + 1 ;
107
+ var expectedStartColumnNumber = widestKeyExtent . EndColumnNumber + 1 ;
107
108
foreach ( var extentTuple in nodeTuples )
108
109
{
109
- if ( extentTuple . Item2 . StartColumnNumber != expectedStartColumn )
110
+ if ( extentTuple . Item2 . StartColumnNumber != expectedStartColumnNumber )
110
111
{
111
112
yield return new DiagnosticRecord (
112
113
GetError ( ) ,
@@ -115,12 +116,28 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
115
116
GetDiagnosticSeverity ( ) ,
116
117
extentTuple . Item1 . File ,
117
118
null ,
118
- null ) ;
119
+ GetHashtableCorrections ( extentTuple , expectedStartColumnNumber ) . ToList ( ) ) ;
119
120
}
120
121
}
121
122
}
122
123
}
123
124
125
+ private IEnumerable < CorrectionExtent > GetHashtableCorrections (
126
+ Tuple < IScriptExtent , IScriptExtent > extentTuple ,
127
+ int expectedStartColumnNumber )
128
+ {
129
+ var equalExtent = extentTuple . Item2 ;
130
+ var lhsExtent = extentTuple . Item1 ;
131
+ var columnDiff = expectedStartColumnNumber - equalExtent . StartColumnNumber ;
132
+ yield return new CorrectionExtent (
133
+ lhsExtent . EndLineNumber ,
134
+ equalExtent . StartLineNumber ,
135
+ lhsExtent . EndColumnNumber ,
136
+ equalExtent . StartColumnNumber ,
137
+ new String ( whitespaceChar , Math . Max ( 0 , columnDiff ) + 1 ) ,
138
+ GetError ( ) ) ;
139
+ }
140
+
124
141
private string GetError ( )
125
142
{
126
143
return String . Format ( CultureInfo . CurrentCulture , Strings . AlignAssignmentStatementError ) ;
0 commit comments