Skip to content

Commit 9f65c73

Browse files
committed
Apply last suggestion 'Prevent double-counting indentation in comment runs' #2238 (comment)
1 parent 842d46c commit 9f65c73

3 files changed

Lines changed: 57 additions & 5 deletions

File tree

‎Rules/UseConsistentIndentation.cs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,8 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
303303
tokens,
304304
tokenIndex,
305305
out bool commentRunStartsAfterLineContinuation);
306-
if (commentRunStartsAfterLineContinuation)
307-
{
308-
tempIndentationLevel = indentationLevel + 1;
309-
}
306+
tempIndentationLevel = indentationLevel +
307+
(commentRunStartsAfterLineContinuation ? 1 : 0);
310308
}
311309

312310
AddViolation(token, tempIndentationLevel, diagnosticRecords, ref onNewLine, lineHasPipelineBeforeToken, commentIndentationOffset);

‎Tests/Rules/UseConsistentIndentation.tests.ps1‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) Microsoft Corporation. All rights reserved.
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

44
BeforeAll {
@@ -283,6 +283,57 @@ Write-Output `
283283
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
284284
Should -Be $scriptDefinition
285285
}
286+
287+
It 'Should preserve a comment run containing an internal line continuation with <Kind> indentation' -TestCases @(
288+
@{ Kind = 'space'; Indentation = ' ' }
289+
@{ Kind = 'tab'; Indentation = "`t" }
290+
) {
291+
param([string] $Kind, [string] $Indentation)
292+
$scriptDefinition = @(
293+
'<# first #> `'
294+
"$Indentation<# second #>"
295+
"Write-Output 'after'"
296+
) -join "`n"
297+
$settings.Rules.PSUseConsistentIndentation.Kind = $Kind
298+
299+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $settings
300+
$violations.Count | Should -Be 0
301+
$formatted = Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings
302+
$formatted | Should -Be $scriptDefinition
303+
Invoke-Formatter -ScriptDefinition $formatted -Settings $settings |
304+
Should -Be $formatted
305+
}
306+
307+
It 'Should shift a comment run containing an internal line continuation uniformly with <Kind> indentation' -TestCases @(
308+
@{ Kind = 'space'; Indentation = ' ' }
309+
@{ Kind = 'tab'; Indentation = "`t" }
310+
) {
311+
param([string] $Kind, [string] $Indentation)
312+
$scriptDefinition = @(
313+
'if ($a) {'
314+
'<# first #> `'
315+
"$Indentation<# second #>"
316+
"$($Indentation)Write-Output 'after'"
317+
'}'
318+
) -join "`n"
319+
$expectedScriptDefinition = @(
320+
'if ($a) {'
321+
"$Indentation<# first #> ``"
322+
"$Indentation$Indentation<# second #>"
323+
"$($Indentation)Write-Output 'after'"
324+
'}'
325+
) -join "`n"
326+
$settings.Rules.PSUseConsistentIndentation.Kind = $Kind
327+
328+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $settings
329+
$violations.Count | Should -Be 2
330+
$formatted = Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings
331+
$formatted | Should -Be $expectedScriptDefinition
332+
Invoke-Formatter -ScriptDefinition $formatted -Settings $settings |
333+
Should -Be $formatted
334+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $formatted -Settings $settings
335+
$violations.Count | Should -Be 0
336+
}
286337
}
287338

288339
Context "When a multi-line command is given" {

‎docs/Rules/UseConsistentIndentation.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ title: UseConsistentIndentation
1515
This rule detects inconsistent indentation patterns within a source file. Indentation should be
1616
consistent throughout your script.
1717

18+
Consecutive comment lines are shifted together to preserve their relative indentation, including
19+
when a line continuation occurs within the comment run.
20+
1821
## Example
1922

2023
### Noncompliant

0 commit comments

Comments
 (0)