PSScriptAnalyzer icon indicating copy to clipboard operation
PSScriptAnalyzer copied to clipboard

PSUseConsistentIndentation: Check indentation of lines where first token is a LParen not followed by comment or new line

Open liamjpeters opened this issue 10 months ago • 0 comments

PR Summary

Currently if an LParen ( is the first token on a line and the next token is not a comment or new line, then the line's indentation is not checked.

This is due to this if-check:

https://github.com/PowerShell/PSScriptAnalyzer/blob/a754b950467aa9e78a1eba1a3423bbd055ed8772/Rules/UseConsistentIndentation.cs#L165-L177

AddViolation(), which subsequently checks the lines indentation against the expected indentation, is not called if the conditional evaluates to true.

This PR changes the logic to always call AddViolation(), so the indentation is checked, but to only increase the indentation level when the conditional evaluates to false.

Fixes #1994

I've run this rule recursively over some large code-bases, including the PSScriptAnalyzer repo.

  • In the current 1.22.0 we find 2,927 violations.
  • With this PR applied, we find 2,931 violations.
  • I believe the settings I used (below) means the rule defaults to spaces and many of the files were using tabs - hence the large number of violations.
    $Settings = @{
        IncludeRules = @('PSUseConsistentIndentation')
        Rules        = @{
            PSUseConsistentIndentation = @{
                Enable = $true
            }
        }
    }
    
  • All previously found violations were still found.
  • There were 4 newly found violations:
    • \Tests\Engine\ModuleHelp.Tests.ps1\ModuleHelp.Tests.ps1 line 48 https://github.com/PowerShell/PSScriptAnalyzer/blob/a754b950467aa9e78a1eba1a3423bbd055ed8772/Tests/Engine/ModuleHelp.Tests.ps1#L47-L54

      This becomes:

      image

      Which is a little dubious 🤔

      Edit: This is because the previous line has 6 opening LParens, and 3 closing RParens. So the indentation level is 3 greater than the previous line

    • \Tests\Engine\ModuleHelp.Tests.ps1\ModuleHelp.Tests.ps1 line 116 https://github.com/PowerShell/PSScriptAnalyzer/blob/a754b950467aa9e78a1eba1a3423bbd055ed8772/Tests/Engine/ModuleHelp.Tests.ps1#L115-L117

      This line wasn't previously being checked. It's using tabs, and as mentioned above the settings is defaulting to spaces.

    • \Tests\Engine\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1\CommunityAnalyzerRules.psm1 line 518 https://github.com/PowerShell/PSScriptAnalyzer/blob/a754b950467aa9e78a1eba1a3423bbd055ed8772/Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1#L515-L521

      This is changed to:

      image

    • \Tests\Rules\UseToExportFieldsInManifest.tests.ps1\UseToExportFieldsInManifest.tests.ps1 line 73

      This is a line that randomly uses tabs

      image

PR Checklist

liamjpeters avatar Apr 12 '24 12:04 liamjpeters