vscode-powershell icon indicating copy to clipboard operation
vscode-powershell copied to clipboard

Set `Code Formatting:` settings based on existing PSScriptAnalyzerSettings.psd1 so autoformat stays in sync with ScriptAnalyzer

Open daprahamian opened this issue 1 year ago • 2 comments

Prerequisites

  • [X] I have written a descriptive issue title.
  • [X] I have searched all issues to ensure it has not already been reported.

Summary

I need to have some PSScriptAnalyzer settings defined in a PSScriptAnalyzerSettings.psd1 so I can validate my code as part of CI. I also use VSCode, and keeping vscode settings and PSScriptAnalyzer settings in sync is a bit of a pain. Since PSScriptAnalyzer has a bunch of code formatting options supported, it would be great to be able to tell vscode to "format it the way Invoke-Formatter would.

Proposed Design

No response

daprahamian avatar May 20 '24 18:05 daprahamian

Is there a specific issue with this setting that is not reading your settings file? image

JustinGrote avatar May 21 '24 21:05 JustinGrote

@JustinGrote here is an example:

# PSScriptAnalyzerSettings.psd1
@{
    Rules = @{
        PSAlignAssignmentStatement = @{
            Enable         = $false
            CheckHashtable = $false
        }
    }
}
# test_file.ps1
$dict = @{
    foo = "bar"
    basdasdasd = "zoom"
}

With the following setup, running Invoke-ScriptAnalyzer -Settings ./PSScriptAnalyzerSettings.psd1 -Path ./test_file.ps1 -Fix does not result in any changes to the file. But if I hit Ctrl+S inside of vscode, the file is auto-formatted as follows:

$dict = @{
    foo = "bar"
    basdasdasd = "zoom"
}

despite the fact that I have set

        PSAlignAssignmentStatement = @{
            Enable         = $false
            CheckHashtable = $false
        }

daprahamian avatar May 22 '24 19:05 daprahamian