-
Notifications
You must be signed in to change notification settings - Fork 382
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
PSAvoidAssignmentToAutomaticVariable triggers on property assignment of automatic variable #1012
Comments
@SeeminglyScience This sounds more like a special case that needs human judgement and should therefore be suppressed by the user rather than having to convolute the code with lots of special cases and maintaining it (especially should behaviour change in a different PS version). PS > $ExecutionContext.SessionState.UseFullLanguageModeInDebugger = $true
'UseFullLanguageModeInDebugger' is a ReadOnly property.
At line:1 char:1
+ $ExecutionContext.SessionState.UseFullLanguageModeInDebugger = $true
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException What are your thoughts? |
Well, there's only a couple assignable AST types that would hold a variable in a way this rule would be interested in. Here's a bit of code I wrote for something else with the same goal. internal class VariableOps
{
internal static IEnumerable<VariableExpressionAst> GetVariablesFromAssignment(AssignmentStatementAst assignmentStatementAst)
{
if (assignmentStatementAst.Left is ArrayLiteralAst arrayLiteral)
{
return GetVariablesFromArrayLiteral(arrayLiteral);
}
var singleVariable = GetVariableFromSingleAssignableAst(assignmentStatementAst.Left);
if (singleVariable == null)
{
return Enumerable.Empty<VariableExpressionAst>();
}
return new[] { singleVariable };
}
private static VariableExpressionAst GetVariableFromSingleAssignableAst(ExpressionAst expressionAst)
{
if (expressionAst is VariableExpressionAst variableExpression)
{
return variableExpression;
}
if (expressionAst is ConvertExpressionAst convertExpression)
{
return GetVariableFromSingleAssignableAst(convertExpression.Child);
}
if (expressionAst is AttributedExpressionAst attributedExpression)
{
return GetVariableFromSingleAssignableAst(attributedExpression.Child);
}
return null;
}
private static IEnumerable<VariableExpressionAst> GetVariablesFromArrayLiteral(ArrayLiteralAst arrayLiteralAst)
{
for (var i = 0; i < arrayLiteralAst.Elements.Count; i++)
{
var foundVariable = GetVariableFromSingleAssignableAst(arrayLiteralAst.Elements[i]);
if (foundVariable != null)
{
yield return foundVariable;
}
}
}
} Catches
Of Invoke-ScriptAnalyzer -ScriptDefinition '$Host.PrivateData["ErrorBackgroundColor"] = "Black"' |
Hmm, yes maybe you are right and it is better to just exclude all properties on automatic variables. I will have a think overnight, I fixed your other raised issue as part of PR 1008 by the way. |
… when assigning a .Net property and only look at the LHS (#1008) * Fix NullReferenceException in AvoidAssignmentToAutomaticVariable rule when assigning a net property to a .Net property * when variableExpressionAst is null, then continue loop to reduce nesting * fix indentation for cleaner diff * Fix issue #1013 as well by making sure the rule looks only the LHS * Fix issue #1012 as well * Simplify test that checks that PSSA does not throw to address PR review
Steps to reproduce
Expected behavior
Actual behavior
Environment data
The text was updated successfully, but these errors were encountered: