Skip to content

Commit 1f43e97

Browse files
committed
Merge pull request #129 from PowerShell/BugFixes
Take the AST cast error bug fix to dev branch
2 parents 6ea858a + f73ccc7 commit 1f43e97

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

Rules/AvoidUninitializedVariable.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
5050
}
5151
}
5252

53-
IEnumerable<Ast> funcAsts = ast.FindAll(item => item is FunctionDefinitionAst || item is FunctionMemberAst, true);
53+
IEnumerable<Ast> funcAsts = ast.FindAll(item => item is FunctionDefinitionAst, true);
54+
IEnumerable<Ast> funcMemberAsts = ast.FindAll(item => item is FunctionMemberAst, true);
5455

5556
// Checks whether this is a dsc resource file (we don't raise this rule for get, set and test-target resource
5657
bool isDscResourceFile = Helper.Instance.IsDscResourceModule(fileName);
@@ -83,6 +84,23 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
8384
}
8485
}
8586
}
87+
88+
foreach (FunctionMemberAst funcMemAst in funcMemberAsts)
89+
{
90+
// Finds all VariableExpressionAst.
91+
IEnumerable<Ast> varAsts = funcMemAst.FindAll(testAst => testAst is VariableExpressionAst, true);
92+
93+
// Iterates all VariableExpressionAst and check the command name.
94+
foreach (VariableExpressionAst varAst in varAsts)
95+
{
96+
if (Helper.Instance.IsUninitialized(varAst, funcMemAst))
97+
{
98+
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.AvoidUninitializedVariableError, varAst.VariablePath.UserPath),
99+
varAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, varAst.VariablePath.UserPath);
100+
}
101+
}
102+
103+
}
86104
}
87105

88106
/// <summary>

0 commit comments

Comments
 (0)