GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||
GeezerVison is showing github.com — Add AvoidReservedWordsAsFunctionNames Rule by liamjpeters · Pull Request #2128 · PowerShell/PSScriptAnalyzer Conversation
Contributor
PR SummaryAdds a new rule that warns when reserved words are used as function names. function function {} function if { function else {} } function global:try {} function private:catch {} Fixes #2099 I wrote a blog post about putting this together 😀 PR Checklist
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
|
There was an error while loading. Please reload this page.
|
Just as a note... I did attempt to use reflection rather than a hard-coded list of keywords. The keyword list is in the Tokenizer. public static HashSet<string> GetReservedWordsFromTokenizer() { // Get the assembly containing Tokenizer var asm = typeof(Token).Assembly; // Get the internal Tokenizer type var tokenizerType = asm.GetType("System.Management.Automation.Language.Tokenizer"); // Get the private static readonly field s_keywordTokenKind var field = tokenizerType.GetField("s_keywordTokenKind", BindingFlags.NonPublic | BindingFlags.Static); // Get the TokenKind[] value var tokenKinds = field.GetValue(null) as Array; var reservedWords = new HashSet<string>( tokenKinds.Cast<Enum>().Select(tk => tk.ToString()), StringComparer.OrdinalIgnoreCase ); return reservedWords; } With similar on the PowerShell side for the tests. This worked fine in PS7 but I can't seem to get it to work in PS5.1 - I can't access the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
That makes sense to me. We've run into similar issues in PSES because the visibility of some of them changed making them available through reflection in PS7 but still not in PS5.1. Oh well 🫠 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
6672621 to
1e5c1b2
Compare
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Patrick Meinecke (@SeeminglyScience) can you take a look at this too? Seems pretty good to me and reasonable to add as a default.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
…ed word, and reserved word with a letter missing
…st for good measure
Co-authored-by: Andy Jordan <[email protected]>
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'm good with this, just want Patrick Meinecke (@SeeminglyScience) to double check. I'll send it his way.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Definitely a useful rule, ty Liam! Couple of small changes
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
…bility Co-authored-by: Patrick Meinecke <[email protected]>
assembly, base, command, hidden, in, inlinescript, interface, module, namespace, private, public, static
|
We did it, high-fives all round 🫸🫷 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Rule request: Avoid reserved words for functions names
There was an error while loading. Please reload this page.