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 new AvoidDynamicallyCreatingVariableNames rule by iRon7 · Pull Request #2178 · PowerShell/PSScriptAnalyzer Conversation
Contributor
PR SummaryNew rule to push for the use of hash tables rather than dynamically creating vriables in the general variable pool. PR Checklist
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
iRon7
requested review from
a team,
Christoph Bergmeister (bergmeister),
Mikey Lombardi (He/Him) (michaeltlombardi) and
Sean Wheeler (sdwheeler)
as code owners
Contributor
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Contributor
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
This was referenced
Co-authored-by: Copilot <[email protected]>
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pull request overviewAdds a new built-in PSScriptAnalyzer rule intended to discourage dynamically created variable names (favoring hashtables/dictionaries), along with documentation and Pester coverage. Changes:
Reviewed changesCopilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments. Show a summary per file
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsUh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page.
|
There was an error while loading. Please reload this page.
It is meant as a best practice guidance for users that are not known with hash tables, as in e.g. https://stackoverflow.com/q/68827910/1701026. There are probably a lot of ways around it but I am not trying to completely close the door on dynamically creating variable names, but only want to prevent beginners from unknowingly choosing the wrong direction and possibly run into a pitfall. I don't think that the audience I am trying to reach with this rule will use something like
Yes, your example of
Implemented
Changed
Added And now Copilot also kicks in with suggestions... will I ever be able to finish this? 😆 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
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.
Thanks iRon7, and thanks Liam Peters (@liamjpeters) for the deep review on this one — I think your back-and-forth genuinely improved the rule (narrowing scope to New-Variable, picking up the alias coverage via Helper.CmdletNameAndAliases, dropping severity to Information). All of Liam's points look addressed.
Two follow-ups before I'm comfortable merging:
Opt-in by default. This rule is still derived from IScriptRule, which means it's enabled-by-default for everyone. For a new rule — especially one at Information severity that is going to flag any New-Variable -Name $name in dynamic-config / metaprogramming-style scripts — our convention is to ship it opt-in via ConfigurableRule with Enable = false. Same recipe as AvoidExclaimOperator. Could you switch the base class and update docs/Rules/README.md accordingly?
Second maintainer read. Now that the scope is narrowed to just New-Variable, I'd like Christoph Bergmeister (@bergmeister) or Mikey Lombardi (He/Him) (@michaeltlombardi) to weigh in on whether the rule still earns its keep at this scope, or whether it would be better recombined with the dynamic Set-Item Variable:\… form. Either answer is fine with me — just want a second pair of eyes on the design question before we land it.
Drafted by Copilot (Claude Opus 4.7)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
This morning, I have also added an additional #2183 PR. Although, I see the value of preferring a Maybe a general solution (e.g. a setting like |
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 a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I am not sure we need a rule for this to warn when using New-Variable. PowerShell's own variable nature is dynamic and it can sometimes be a blessing and sometimes unpredictable and hard for PSSA. But it's up to user, there are far more dynamic things in PowerShell that allows users to shoot into their own foot
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Christoph Bergmeister (@bergmeister),
This https://stackoverflow.com/q/68827910/1701026 StackOverflow question, shows where a novice scripter could be prevented to run into the wrong direction. But, I agree, even experienced PowerShell programmers might choose to shoot in their own foot 🤪. See: PowerShell/PowerShell#27023 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
… accordingly. Increased number of information tests to 20 in Get-ScriptAnalyzerRule tests.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Docs look good.
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.
Thanks iRon7 — the opt-in conversion (ConfigurableRule, Enable = false, README Information / No / Yes) and the bumped Information-rule count in the Get-ScriptAnalyzerRule test are in. Approving.
Weighing in on the design question: I agree this is a useful "best practice" rule — exactly the kind of thing I'd turn on for my own code. PowerShell's variable layer is dynamic by nature and there are other forms we don't catch (Set-Item Variable:\…, ${$name} = …, Get-Variable -Name $... | Set-…), but as an opt-in Information-severity rule narrowly scoped to New-Variable -Name $... the value/cost calculus is fine. We can extend the detection surface in follow-ups.
Heads-up: CI on the latest commit (3e546a48) is in action_required state — I'll approve the workflow run so we have a green run on file before merge.
Drafted by Copilot (Claude Opus 4.7)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…atingVariableNames in documentation.
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.
There was an error while loading. Please reload this page.