1616using System . Globalization ;
1717using System . Linq ;
1818using System . Management . Automation . Language ;
19- using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
2019
2120namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . BuiltinRules
2221{
@@ -28,6 +27,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2827#endif
2928 class AlignAssignmentStatement : ConfigurableRule
3029 {
30+ private readonly char whitespaceChar = ' ' ;
3131
3232 private List < Func < TokenOperations , IEnumerable < DiagnosticRecord > > > violationFinders
3333 = new List < Func < TokenOperations , IEnumerable < DiagnosticRecord > > > ( ) ;
@@ -91,6 +91,7 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
9191
9292 var nodeTuples = GetExtents ( tokenOps , hashtableAst ) ;
9393 if ( nodeTuples == null
94+ || nodeTuples . Count == 0
9495 || ! nodeTuples . All ( t => t . Item1 . StartLineNumber == t . Item2 . EndLineNumber ) )
9596 {
9697 continue ;
@@ -103,10 +104,10 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
103104 ? tAggregate
104105 : t1 ;
105106 } ) ;
106- var expectedStartColumn = widestKeyExtent . EndColumnNumber + 1 ;
107+ var expectedStartColumnNumber = widestKeyExtent . EndColumnNumber + 1 ;
107108 foreach ( var extentTuple in nodeTuples )
108109 {
109- if ( extentTuple . Item2 . StartColumnNumber != expectedStartColumn )
110+ if ( extentTuple . Item2 . StartColumnNumber != expectedStartColumnNumber )
110111 {
111112 yield return new DiagnosticRecord (
112113 GetError ( ) ,
@@ -115,12 +116,28 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
115116 GetDiagnosticSeverity ( ) ,
116117 extentTuple . Item1 . File ,
117118 null ,
118- null ) ;
119+ GetHashtableCorrections ( extentTuple , expectedStartColumnNumber ) . ToList ( ) ) ;
119120 }
120121 }
121122 }
122123 }
123124
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+
124141 private string GetError ( )
125142 {
126143 return String . Format ( CultureInfo . CurrentCulture , Strings . AlignAssignmentStatementError ) ;
0 commit comments