FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add AvoidReservedWordsAsFunctionNames Rule by liamjpeters · Pull Request #2128 · PowerShell/PSScriptAnalyzer · GitHub

Add AvoidReservedWordsAsFunctionNames Rule - #2128

Merged
Andy Jordan (andyleejordan) merged 9 commits into
PowerShell:mainfrom
liamjpeters:#2099PSAvoidReservedWordsAsFunctionNames
Oct 22, 2025
Merged

Add AvoidReservedWordsAsFunctionNames Rule#2128
Andy Jordan (andyleejordan) merged 9 commits into
PowerShell:mainfrom
liamjpeters:#2099PSAvoidReservedWordsAsFunctionNames

Conversation

Copy link
Copy Markdown
Contributor

PR Summary

Adds 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

Liam Peters (liamjpeters) commented Sep 3, 2025
edited
Loading

Copy link
Copy Markdown
Contributor Author

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 s_keywordTokenKind or s_keywordText field.

Copy link
Copy Markdown
Member

This worked fine in PS7 but I can't seem to get it to work in PS5.1 - I can't access the s_keywordTokenKind or s_keywordText field.

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 🫠

Andy Jordan (andyleejordan) force-pushed the #2099PSAvoidReservedWordsAsFunctionNames branch from 6672621 to 1e5c1b2 Compare October 15, 2025 23:23

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Patrick Meinecke (@SeeminglyScience) can you take a look at this too? Seems pretty good to me and reasonable to add as a default.

Comment thread Engine/Helper.cs Outdated
Comment thread Rules/Strings.resx Outdated
…ed word, and reserved word with a letter missing
Co-authored-by: Andy Jordan <2226434+andyleejordan@users.noreply.github.com>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I'm good with this, just want Patrick Meinecke (@SeeminglyScience) to double check. I'll send it his way.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Definitely a useful rule, ty Liam! Couple of small changes

…bility

Co-authored-by: Patrick Meinecke <SeeminglyScience@users.noreply.github.com>
assembly, base, command, hidden, in, inlinescript, interface,
module, namespace, private, public, static

Copy link
Copy Markdown
Contributor Author

We did it, high-fives all round 🫸🫷

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM!

Andy Jordan (andyleejordan) merged commit a9898a6 into PowerShell:main Oct 22, 2025
4 checks passed
Liam Peters (liamjpeters) deleted the #2099PSAvoidReservedWordsAsFunctionNames branch October 23, 2025 22:17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Successfully merging this pull request may close these issues.

Rule request: Avoid reserved words for functions names

4 participants


Back | FazBrowse Home | New Git URL