PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.84k stars 371 forks source link

Add check for capitalization for reserved powershell words #767

Open dchristian3188 opened 7 years ago

dchristian3188 commented 7 years ago

One thing i would like to see is rules that check case of reserved powershell words like function, if, switch, class, etc.

A setting should be available to enforce all lower, upper and proper case. Ideally this would drive a correction that could be used by VSCode to auto correct.

krissmilne commented 7 years ago

As Powershell isn't a case sensitive language, it would be good to provide the case formatting options for the developers preference.

Something along the lines of:

//Changes the case for powershell reserved words (if, else, elseif, function, switch, param etc...) to either UPPER, lower or First (capitalises the first letter) "powershell.codeFormatting.reservedWordCase": "lower", "upper", "first"

//Changes the case for powershell variables declared in the global or script scope to either UPPER, lower or FirstUpper (PascalCase) or firstLower (camelCase) "powershell.codeFormatting.globalVariableCase": "lower", "upper", "FirstUpper", "firstLower"

//Changes the case for powershell variables declared in the local or private scope (within functions) to either UPPER, lower or FirstUpper (PascalCase) or firstLower (camelCase) "powershell.codeFormatting.privateVariableCase": "lower", "upper", "FirstUpper", "firstLower"

//Autocorrects the case for recognised cmdlets and functions "powershell.codeFormatting.cmdletCase": true, false

//Autocorrects the case for recognised parameters of cmdlets and functions "powershell.codeFormatting.parameterCase": true, false

Jaykul commented 3 years ago

My 2c: I think "lower" should be the default, and "pascal" (or "proper") should be the only other option for keywords (and only because of the C# programmers who write the documentation). PSScriptAnalyzer is the defacto standard-bearer for PowerShell best practices, and I am reasonably sure the community wants to discourage the use of ALL CAPS or camelCase keywords in PowerShell. Honestly, I'm not even sure about PascalCase; newbies will discover some annoyed veterans if they start capitalizing keywords such as ForEach...

I also really like the idea of fixing the case on variables, but perhaps that should be a separate issue? I think you'd need to separate not just "global" but "constant" and "parameter" variables -- and you don't have a good way of doing that. I think a good first step would be to have the current capitalization fixer fix the variables based on "follow first use" -- make the case of each variable consistent throughout the file, without worrying about what kind of variable it is.

If we fix variable case, should we consider also enforcing the use of ALL_CAPS for environment variables, for compatibility reasons?