Fix NullReferenceException in AvoidAssignmentToAutomaticVariable rule when assigning a .Net property and only look at the LHS#1008
Conversation
… when assigning a net property to a .Net property
| var variableExpressionAst = assignmentStatementAst.Find(testAst => testAst is VariableExpressionAst, searchNestedScriptBlocks: false) as VariableExpressionAst; | ||
| var variableName = variableExpressionAst.VariablePath.UserPath; | ||
| if (_readOnlyAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase)) | ||
| if (variableExpressionAst != null) |
There was a problem hiding this comment.
only this null check was added, all the code diff below is only the added indentation
There was a problem hiding this comment.
from a flow perspective, this could also be
if ( variableExpressionAst == null )
continue
and the indenting is preserved :)
JamesWTruher
left a comment
There was a problem hiding this comment.
neither of my comments are blockers
|
|
||
| It "Does not throw a NullReferenceException when using assigning a .Net property to a .Net property (Bug in 1.17.0 - issue 1007)" { | ||
| $exceptionThrown = $false | ||
| try |
There was a problem hiding this comment.
shouldn't this just be:
{ Invoke-ScriptAnalyzer -ScriptDefinition '[foo]::bar = [baz]::quz' -ErrorAction Stop } | Should Not Throw
It will make the log easier to understand if it fails.
There was a problem hiding this comment.
Pester only works with terminating errors. PSSA throws non-terminating errors, see pester/Pester#366
There was a problem hiding this comment.
i would have thought that -ErrorAction Stop would do the trick (after all, it's in a try/catch, so you're doing the same thing) so somebody is halting the pipeline
| var variableExpressionAst = assignmentStatementAst.Find(testAst => testAst is VariableExpressionAst, searchNestedScriptBlocks: false) as VariableExpressionAst; | ||
| var variableName = variableExpressionAst.VariablePath.UserPath; | ||
| if (_readOnlyAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase)) | ||
| if (variableExpressionAst != null) |
There was a problem hiding this comment.
from a flow perspective, this could also be
if ( variableExpressionAst == null )
continue
and the indenting is preserved :)
|
|
||
| It "Does not throw a NullReferenceException when using assigning a .Net property to a .Net property (Bug in 1.17.0 - issue 1007)" { | ||
| $exceptionThrown = $false | ||
| try |
There was a problem hiding this comment.
i would have thought that -ErrorAction Stop would do the trick (after all, it's in a try/catch, so you're doing the same thing) so somebody is halting the pipeline
PR Summary
variableExpressionAstThe issues are slightly related to each other, therefore they are both addressed in this PR to keep things simple.
PR Checklist
Note: Tick the boxes below that apply to this pull request by putting an
xbetween the square brackets. Please mark anything not applicable to this PRNA.WIP:to the beginning of the title and remove the prefix when the PR is ready