GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add AvoidReservedWordsAsFunctionNames Rule by liamjpeters · Pull Request #2128 · PowerShell/PSScriptAnalyzer

Conversation

@liamjpeters

@liamjpeters Liam Peters (liamjpeters) commented Sep 1, 2025

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

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Change is not breaking
  • Make sure all .cs, .ps1 and .psm1 files have the correct copyright header
  • Make sure you've added a new test if existing tests do not effectively test the code changed and/or updated documentation
  • This PR is ready to merge and is not Work in Progress.
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iRon7 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@liamjpeters
Liam Peters (liamjpeters) requested review from Andy Jordan (andyleejordan), Christoph Bergmeister (bergmeister), Mikey Lombardi (He/Him) (michaeltlombardi) and Sean Wheeler (sdwheeler) as code owners September 1, 2025 18:29
@liamjpeters

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

Uh oh!

There was an error while loading. Please reload this page.

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andyleejordan

Andy Jordan (andyleejordan) commented Oct 15, 2025

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 🫠

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Liam Peters (liamjpeters) added 4 commits October 15, 2025 16:23
@liamjpeters @andyleejordan
fix: Update helper GetScriptExtentForFunctionName to return correct e…
fbe1489
…xtent when function name is 'function'
@liamjpeters @andyleejordan
Add AvoidReservedWordsAsFunctionNames rule
b4f67c2
@liamjpeters @andyleejordan
fix: Handle functions with scopes
1819eb3
@liamjpeters @andyleejordan
Copyright header
1e5c1b2
@andyleejordan
Andy Jordan (andyleejordan) force-pushed the #2099PSAvoidReservedWordsAsFunctionNames branch from 6672621 to 1e5c1b2 Compare October 15, 2025 23:23
andyleejordan
Andy Jordan (andyleejordan) reviewed Oct 15, 2025
View reviewed changes

@andyleejordan Andy Jordan (andyleejordan) left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Engine/Helper.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/AvoidReservedWordsAsFunctionNames.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/Strings.resx Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Tests/Engine/GetScriptAnalyzerRule.tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Tests/Rules/AvoidReservedWordsAsFunctionNames.tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Liam Peters (liamjpeters) and others added 3 commits October 16, 2025 20:43
@liamjpeters
Extend Substring tests to starts with reserved word, ends with reserv…
3b56a76
…ed word, and reserved word with a letter missing
@liamjpeters
Check if the function name is 'Function' case insensitively. Add a te…
4510ff4
…st for good measure
@liamjpeters @andyleejordan
Fix casing of common name
a9e6182
Co-authored-by: Andy Jordan <[email protected]>
andyleejordan
Andy Jordan (andyleejordan) approved these changes Oct 20, 2025
View reviewed changes

@andyleejordan Andy Jordan (andyleejordan) left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
SeeminglyScience
Patrick Meinecke (SeeminglyScience) requested changes Oct 21, 2025
View reviewed changes

@SeeminglyScience Patrick Meinecke (SeeminglyScience) left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Rules/AvoidReservedWordsAsFunctionNames.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/AvoidReservedWordsAsFunctionNames.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Liam Peters (liamjpeters) and others added 2 commits October 21, 2025 19:07
@liamjpeters @SeeminglyScience
AvoidReservedWordsAsFunctionNames: factor our function name for reada…
80172fe
…bility

Co-authored-by: Patrick Meinecke <[email protected]>
@liamjpeters
Remove reserved words which do not pose an issue:
8f89a1b
assembly, base, command, hidden, in, inlinescript, interface,
module, namespace, private, public, static
@liamjpeters

Liam Peters (liamjpeters) commented Oct 21, 2025

Copy link
Copy Markdown
Contributor Author

We did it, high-fives all round 🫸🫷

SeeminglyScience and andyleejordan reacted with hooray emoji SeeminglyScience reacted with heart emoji
All reactions
  • 🎉 2 reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

SeeminglyScience
Patrick Meinecke (SeeminglyScience) approved these changes Oct 21, 2025
View reviewed changes

@SeeminglyScience Patrick Meinecke (SeeminglyScience) left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@andyleejordan
Andy Jordan (andyleejordan) enabled auto-merge (squash) October 22, 2025 20:22
michaeltlombardi
Mikey Lombardi (He/Him) (michaeltlombardi) approved these changes Oct 22, 2025
View reviewed changes

@michaeltlombardi Mikey Lombardi (He/Him) (michaeltlombardi) left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@andyleejordan
Andy Jordan (andyleejordan) merged commit a9898a6 into PowerShell:main Oct 22, 2025
@liamjpeters
Liam Peters (liamjpeters) deleted the #2099PSAvoidReservedWordsAsFunctionNames branch October 23, 2025 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@michaeltlombardi Mikey Lombardi (He/Him) (michaeltlombardi) michaeltlombardi approved these changes
@SeeminglyScience Patrick Meinecke (SeeminglyScience) SeeminglyScience approved these changes
@sdwheeler Sean Wheeler (sdwheeler) Awaiting requested review from sdwheeler
@bergmeister Christoph Bergmeister (bergmeister) Awaiting requested review from bergmeister bergmeister is a code owner
+1 more reviewer
@andyleejordan Andy Jordan (andyleejordan) andyleejordan approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Rule request: Avoid reserved words for functions names

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@liamjpeters @andyleejordan @michaeltlombardi @SeeminglyScience
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.