From be5ce70a31c9d739c1bd6b5f3a07368cef08c11d Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 10 Sep 2018 23:01:22 +0100 Subject: [PATCH 1/3] Use proper calculation of widest assignment sign by looking at the AST properties. TODO: write regression test --- Rules/AlignAssignmentStatement.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Rules/AlignAssignmentStatement.cs b/Rules/AlignAssignmentStatement.cs index 083b6dcb5..d66ac1411 100644 --- a/Rules/AlignAssignmentStatement.cs +++ b/Rules/AlignAssignmentStatement.cs @@ -191,15 +191,7 @@ private IEnumerable 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) From bd97663e8be06a8f4377ae28f39597273abbd265 Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister [MVP]" Date: Tue, 11 Sep 2018 16:46:48 +0100 Subject: [PATCH 2/3] Add regression test --- Tests/Rules/AlignAssignmentStatement.tests.ps1 | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Tests/Rules/AlignAssignmentStatement.tests.ps1 b/Tests/Rules/AlignAssignmentStatement.tests.ps1 index 31bbfa81d..aa0e8f216 100644 --- a/Tests/Rules/AlignAssignmentStatement.tests.ps1 +++ b/Tests/Rules/AlignAssignmentStatement.tests.ps1 @@ -54,6 +54,16 @@ $hashtable = @{ $violations.Count | Should -Be 1 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 = @' From dfdf06f05b2ff6e55d8e92efaee8d65bf6313103 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 11 Sep 2018 21:11:16 +0100 Subject: [PATCH 3/3] fix test --- Tests/Rules/AlignAssignmentStatement.tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Rules/AlignAssignmentStatement.tests.ps1 b/Tests/Rules/AlignAssignmentStatement.tests.ps1 index aa0e8f216..29a39eeee 100644 --- a/Tests/Rules/AlignAssignmentStatement.tests.ps1 +++ b/Tests/Rules/AlignAssignmentStatement.tests.ps1 @@ -54,11 +54,11 @@ $hashtable = @{ $violations.Count | Should -Be 1 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" + anotherProperty = "another value" } '@