-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn against assignment to read-only automatic variables #864
Warn against assignment to read-only automatic variables #864
Conversation
|
||
## Description | ||
|
||
`PowerShell` exposes some of its build in variables that are known as automatic variables. Many of them are read-only and PowerShell would throw an error when trying to assign an value on those. The remaining automatic variables should only be assigned to in certain special cases to achieve a certain effect as a special technique. |
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.
built-in rather than "build in"
"the remaining" would probably be better as other
It "Variable '<VariableName>' produces warning of severity error" -TestCases $testCases_ReadOnlyVariables { | ||
param ($VariableName) | ||
|
||
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition "`$$($VariableName) = 'foo'" | Where-Object { $_.RuleName -eq $ruleName } |
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.
the $(...)
isn't needed
"`$${VariableName} = 'foo'"
will work just fine
It "Using Variable '<VariableName>' as parameter name in param block produces warning of severity error" -TestCases $testCases_ReadOnlyVariables { | ||
param ($VariableName) | ||
|
||
[System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo{Param(`$$VariableName)}" | Where-Object {$_.RuleName -eq $ruleName } |
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.
this looks to be the same as line 36
One remark about the texte of the erreur message : |
…nalyzer into WarnAgainstAssignmentToAutomaticVariables # Conflicts: # Rules/Strings.resx
Thanks for the feedback, I addressed your comments. |
This is the first of a series of PRs to address issues #858 and #712
Because the analysis of automatic variables is quite complex and in certain cases assignment to automatic variables can be by design, the list of 'naughty' variable assignments is going to be opinionated.
Therefore I decided to start with the read-only variables first where PowerShell will throw an error if assignment or usage as function parameter is attempted.
Note that I still have to look into the other tests that started failing.