Skip to content

Commit 2d439ff

Browse files
authored
Fix AlignAssignment rule to calculate alignment position correctly and avoid crash (#1070)
* Use proper calculation of widest assignment sign by looking at the AST properties. TODO: write regression test * Add regression test * fix test
1 parent f4e77f9 commit 2d439ff

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

Rules/AlignAssignmentStatement.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,7 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
191191
continue;
192192
}
193193

194-
var widestKeyExtent = extentTuples
195-
.Select(t => t.Item1)
196-
.Aggregate((t1, tAggregate) =>
197-
{
198-
return TokenOperations.GetExtentWidth(tAggregate) > TokenOperations.GetExtentWidth(t1)
199-
? tAggregate
200-
: t1;
201-
});
202-
var expectedStartColumnNumber = widestKeyExtent.EndColumnNumber + 1;
194+
var expectedStartColumnNumber = extentTuples.Max(x => x.Item1.EndColumnNumber) + 1;
203195
foreach (var extentTuple in extentTuples)
204196
{
205197
if (extentTuple.Item2.StartColumnNumber != expectedStartColumnNumber)

Tests/Rules/AlignAssignmentStatement.tests.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ $hashtable = @{
5555
Test-CorrectionExtentFromContent $def $violations 1 ' ' ' '
5656
}
5757

58+
It "Should not crash if property name reaches further to the right than the longest property name (regression test for issue 1067)" {
59+
$def = @'
60+
$hashtable = @{ property1 = "value"
61+
anotherProperty = "another value"
62+
}
63+
'@
64+
65+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings -ErrorAction Stop | Get-Count | Should -Be 0
66+
}
67+
5868
It "Should ignore if a hashtable is empty" {
5969
$def = @'
6070
$x = @{ }

0 commit comments

Comments
 (0)