Skip to content

Commit b2e6149

Browse files
authored
Fix NullReferenceException when analysing partial script with InnerBrace option of UseConsistentWhitespace (#1834)
* Fix NullReferenceException when analysing partial script with InnerBrace option of UseConsistentWhitespace * tweak test name * formatting
1 parent fcead29 commit b2e6149

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Rules/UseConsistentWhitespace.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private IEnumerable<DiagnosticRecord> FindInnerBraceViolations(TokenOperations t
260260
foreach (var lCurly in tokenOperations.GetTokenNodes(TokenKind.LCurly))
261261
{
262262
if (lCurly.Next == null
263-
|| !IsPreviousTokenOnSameLine(lCurly)
263+
|| !(lCurly.Previous == null || IsPreviousTokenOnSameLine(lCurly))
264264
|| lCurly.Next.Value.Kind == TokenKind.NewLine
265265
|| lCurly.Next.Value.Kind == TokenKind.LineContinuation
266266
|| lCurly.Next.Value.Kind == TokenKind.RCurly
@@ -616,7 +616,7 @@ private List<CorrectionExtent> GetCorrections(
616616
}
617617

618618

619-
private bool IsPreviousTokenOnSameLine(LinkedListNode<Token> lparen)
619+
private static bool IsPreviousTokenOnSameLine(LinkedListNode<Token> lparen)
620620
{
621621
return lparen.Previous.Value.Extent.EndLineNumber == lparen.Value.Extent.StartLineNumber;
622622
}

Tests/Rules/UseConsistentWhitespace.tests.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,10 @@ if ($true) { Get-Item `
508508
'@
509509
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
510510
}
511+
512+
It 'Should not throw when analysing a line starting with a scriptblock' {
513+
{ Invoke-ScriptAnalyzer -ScriptDefinition '{ }' -Settings $settings -ErrorAction Stop } | Should -Not -Throw
514+
}
511515
}
512516

513517

0 commit comments

Comments
 (0)