Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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