Skip to content

Commit

Permalink
PSUseConsistentIndentation: Check indentation of lines where first to…
Browse files Browse the repository at this point in the history
…ken is a LParen not followed by comment or new line (#1995)

* Check a line starting with LParen for correct indentation, even if that LParen doesn't impact the indentation level

* Fix typo in test utility for the UseConsistentIndentation tests

* Add tests to ensure formatting is still checked when LParen is the first token on a line, followed by a non-newline, non-comment
  • Loading branch information
liamjpeters authored Feb 25, 2025
1 parent dc4ae4b commit d13809c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ caused by tokens that require a closing RParen (which are LParen, AtParen and Do
break;

case TokenKind.LParen:
AddViolation(token, indentationLevel, diagnosticRecords, ref onNewLine);
// When a line starts with a parenthesis and it is not the last non-comment token of that line,
// then indentation does not need to be increased.
if ((tokenIndex == 0 || tokens[tokenIndex - 1].Kind == TokenKind.NewLine) &&
Expand All @@ -173,7 +174,7 @@ caused by tokens that require a closing RParen (which are LParen, AtParen and Do
break;
}
lParenSkippedIndentation.Push(false);
AddViolation(token, indentationLevel++, diagnosticRecords, ref onNewLine);
indentationLevel++;
break;

case TokenKind.Pipe:
Expand Down
18 changes: 15 additions & 3 deletions Tests/Rules/UseConsistentIndentation.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ Describe "UseConsistentIndentation" {
function Invoke-FormatterAssertion {
param(
[string] $ScriptDefinition,
[string] $ExcpectedScriptDefinition,
[string] $ExpectedScriptDefinition,
[int] $NumberOfExpectedWarnings,
[hashtable] $Settings
)

# Unit test just using this rule only
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $settings
$violations.Count | Should -Be $NumberOfExpectedWarnings -Because $ScriptDefinition
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expected -Because $ScriptDefinition
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $ExpectedScriptDefinition -Because $ScriptDefinition
# Integration test with all default formatting rules
Invoke-Formatter -ScriptDefinition $scriptDefinition | Should -Be $expected -Because $ScriptDefinition
Invoke-Formatter -ScriptDefinition $scriptDefinition | Should -Be $ExpectedScriptDefinition -Because $ScriptDefinition
}
}
BeforeEach {
Expand Down Expand Up @@ -177,6 +177,18 @@ function test {
'@
Invoke-Formatter -ScriptDefinition $IdempotentScriptDefinition | Should -Be $idempotentScriptDefinition
}

It 'Should find violation in script when LParen is first token on a line and is not followed by Newline' {
$ScriptDefinition = @'
(foo)
(bar)
'@
$ExpectedScriptDefinition = @'
(foo)
(bar)
'@
Invoke-FormatterAssertion $ScriptDefinition $ExpectedScriptDefinition 2 $settings
}
}

Context "When a sub-expression is provided" {
Expand Down

0 comments on commit d13809c

Please sign in to comment.