Skip to content

Commit 4c45a16

Browse files
author
quoctruong
committed
Fix UseDeclaredVarsMoreThanAssignments being raised for setting properties
1 parent 1b15e84 commit 4c45a16

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

Rules/UseDeclaredVarsMoreThanAssignments.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4949
{
5050
foreach (AssignmentStatementAst assignmentAst in assignmentAsts)
5151
{
52-
assingmentVarAsts = assignmentAst.Left.FindAll(testAst => testAst is VariableExpressionAst, true); ;
52+
// Only checks for the case where lhs is a variable. Ignore things like $foo.property
53+
VariableExpressionAst assignmentVarAst = assignmentAst.Left as VariableExpressionAst;
5354

54-
foreach (VariableExpressionAst assignmentVarAst in assingmentVarAsts)
55+
if (assignmentVarAst != null)
5556
{
56-
//Ignore if variable is global or environment variable
57+
//Ignore if variable is global or environment variable
5758
if (!Helper.Instance.IsVariableGlobalOrEnvironment(assignmentVarAst, ast)
5859
&& !assignmentVarAst.VariablePath.IsScript)
5960
{
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
$declaredVars = "Declared Vars"
22
Write-Ouput $declaredVars
3-
$script:thisshouldnotraiseerrors = "this should not raise errors"
3+
$script:thisshouldnotraiseerrors = "this should not raise errors"
4+
$foo.property = "This also should not raise errors"

0 commit comments

Comments
 (0)