Skip to content

Commit d13809c

Browse files
authored
PSUseConsistentIndentation: Check indentation of lines where first token 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
1 parent dc4ae4b commit d13809c

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Rules/UseConsistentIndentation.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ caused by tokens that require a closing RParen (which are LParen, AtParen and Do
163163
break;
164164

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

179180
case TokenKind.Pipe:

Tests/Rules/UseConsistentIndentation.tests.ps1

+15-3
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ Describe "UseConsistentIndentation" {
1111
function Invoke-FormatterAssertion {
1212
param(
1313
[string] $ScriptDefinition,
14-
[string] $ExcpectedScriptDefinition,
14+
[string] $ExpectedScriptDefinition,
1515
[int] $NumberOfExpectedWarnings,
1616
[hashtable] $Settings
1717
)
1818

1919
# Unit test just using this rule only
2020
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $settings
2121
$violations.Count | Should -Be $NumberOfExpectedWarnings -Because $ScriptDefinition
22-
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expected -Because $ScriptDefinition
22+
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $ExpectedScriptDefinition -Because $ScriptDefinition
2323
# Integration test with all default formatting rules
24-
Invoke-Formatter -ScriptDefinition $scriptDefinition | Should -Be $expected -Because $ScriptDefinition
24+
Invoke-Formatter -ScriptDefinition $scriptDefinition | Should -Be $ExpectedScriptDefinition -Because $ScriptDefinition
2525
}
2626
}
2727
BeforeEach {
@@ -177,6 +177,18 @@ function test {
177177
'@
178178
Invoke-Formatter -ScriptDefinition $IdempotentScriptDefinition | Should -Be $idempotentScriptDefinition
179179
}
180+
181+
It 'Should find violation in script when LParen is first token on a line and is not followed by Newline' {
182+
$ScriptDefinition = @'
183+
(foo)
184+
(bar)
185+
'@
186+
$ExpectedScriptDefinition = @'
187+
(foo)
188+
(bar)
189+
'@
190+
Invoke-FormatterAssertion $ScriptDefinition $ExpectedScriptDefinition 2 $settings
191+
}
180192
}
181193

182194
Context "When a sub-expression is provided" {

0 commit comments

Comments
 (0)