Skip to content

Commit

Permalink
Fix AlignAssignment rule to calculate alignment position correctly an…
Browse files Browse the repository at this point in the history
…d 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
  • Loading branch information
bergmeister authored Sep 21, 2018
1 parent f4e77f9 commit 2d439ff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
10 changes: 1 addition & 9 deletions Rules/AlignAssignmentStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,7 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
continue;
}

var widestKeyExtent = extentTuples
.Select(t => t.Item1)
.Aggregate((t1, tAggregate) =>
{
return TokenOperations.GetExtentWidth(tAggregate) > TokenOperations.GetExtentWidth(t1)
? tAggregate
: t1;
});
var expectedStartColumnNumber = widestKeyExtent.EndColumnNumber + 1;
var expectedStartColumnNumber = extentTuples.Max(x => x.Item1.EndColumnNumber) + 1;
foreach (var extentTuple in extentTuples)
{
if (extentTuple.Item2.StartColumnNumber != expectedStartColumnNumber)
Expand Down
10 changes: 10 additions & 0 deletions Tests/Rules/AlignAssignmentStatement.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ $hashtable = @{
Test-CorrectionExtentFromContent $def $violations 1 ' ' ' '
}

It "Should not crash if property name reaches further to the right than the longest property name (regression test for issue 1067)" {
$def = @'
$hashtable = @{ property1 = "value"
anotherProperty = "another value"
}
'@

$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings -ErrorAction Stop | Get-Count | Should -Be 0
}

It "Should ignore if a hashtable is empty" {
$def = @'
$x = @{ }
Expand Down

0 comments on commit 2d439ff

Please sign in to comment.