PowerShellPracticeAndStyle
PowerShellPracticeAndStyle copied to clipboard
Auto format
Does something like gofmt
for golang or terraform format
for Terraform exist for PowerShell?
It would be awesome for VS code or PSScriptAnalyzer to auto enforce the standards and even to be able to auto format documents.
It can take a loooot of time to refactor/format code.
You can set up some style rules in the VSCode preferences, either at the user or workspace level, and set it to format on save or paste (or change?). This currently seems limited to things like brace placement, extra whitespace at the end of lines and a few others.
That should be a good place to start though.
From: John McCarthy [email protected] Sent: Thursday, May 17, 2018 3:14:21 AM To: PoshCode/PowerShellPracticeAndStyle Cc: Subscribed Subject: [PoshCode/PowerShellPracticeAndStyle] Auto format (#110)
Does something like gofmt for golang or terraform format for Terraform exist for PowerShell? It would be awesome for VS code or PSScriptAnalyzer to auto enforce the standards and even to be able to auto format documents.
It can take a loooot of time to refactor/format code.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/PoshCode/PowerShellPracticeAndStyle/issues/110, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AQDqFlgLne8cdTVL69qoAjbK94epeamnks5tzNz9gaJpZM4UCTiZ.
Agreed. So far that is the only option I've seen. Other than that, I thought of making PSScriptAnalyzer rules. Something native to VS Code might be better.
But as you alluded to, it is limited. We wouldn't be able to enforce all the settings outlines in the repo.
I believe the current thoughts is that PSScriptAnalyzer within VS Code flags the rule violations up but it's up to you to resolve them. Which I think is a nice compromise between enforcing it (which it could get wrong) and doing nothing.
I'd rather it auto format my code to meet community standards. Like Chris said, it's limited in VS Code, that's why I brought up tools like gofmt or terraform format which auto formats the code to meet the standards.
You can format code with PSScriptAnalyzer from the command line:
6> Invoke-Formatter -ScriptDefinition {
>> if(1-eq1){
>> "yay!" }
>> else
>> {
>> "suck"
>> }
>> }
if (1 -eq 1) {
"yay!"
}
else {
"suck"
}
Note that operating on files is bit clunky:
Invoke-Formatter -ScriptDefinition (Get-Content .\test.ps1 -raw) | Out-File test.ps1 -Encoding utf8
@bergmeister do you have anything to add to this?
Can you be more specific about what you describe as limited? Is it that you would like to customize/tweak one of the 3 built om style rules or are the style rule definitions in PSSA about braces, whitespace, etc. not sufficient? VSCode has 3 styles that map to certain PSSA style rule configurations but using a setting file you can even get warnings when your style is not matching. Ctrl+K+F on a selection auto formats the code. I am planning to write a blog post about pssa and vscode integration and customisation
Maybe I am not aware of all the settings you are pointing out?
user settings -> powershell.codeformatting.*
I still don't think it is enforcing all the rules outlined in this repo. making sure things like function, if, foreach, etc are all lowercase, that the function names are pascal case, auto formatting comment based help, parameters, etc.
Basically like terraform format, gofmt, etc works.
@Midacts There is an open issue (that is up for grabs) in PSSA to do the formatting with the correct casing: https://github.com/PowerShell/PSScriptAnalyzer/issues/767 Sorry, I haven't used terraform format, gofmt before but will look it up
I think this can be closed now that Invoke-Formatter
ships in PSScriptAnalyzer?