Skip to content

Auto format #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
midacts opened this issue May 17, 2018 · 9 comments
Closed

Auto format #110

midacts opened this issue May 17, 2018 · 9 comments

Comments

@midacts
Copy link

midacts commented May 17, 2018

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.

@ChrisLGardner
Copy link

ChrisLGardner commented May 17, 2018 via email

@midacts
Copy link
Author

midacts commented May 17, 2018

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.

@pauby
Copy link
Contributor

pauby commented May 17, 2018

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.

@midacts
Copy link
Author

midacts commented May 17, 2018

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.

@rkeithhill
Copy link

rkeithhill commented May 18, 2018

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?

@bergmeister
Copy link

bergmeister commented May 18, 2018

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

@midacts
Copy link
Author

midacts commented May 18, 2018

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.

@bergmeister
Copy link

@midacts There is an open issue (that is up for grabs) in PSSA to do the formatting with the correct casing: PowerShell/PSScriptAnalyzer#767
Sorry, I haven't used terraform format, gofmt before but will look it up

@Jaykul
Copy link
Member

Jaykul commented Nov 17, 2020

I think this can be closed now that Invoke-Formatter ships in PSScriptAnalyzer?

@Jaykul Jaykul closed this as completed Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants