Skip to content

Commit be83037

Browse files
committed
Do not trigger PSProvideDefaultParameterValue when parameters are used inside function
1 parent 67dbc2c commit be83037

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

Rules/ProvideDefaultParameterValue.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class ProvideDefaultParameterValue : IScriptRule
3131
/// </summary>
3232
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3333
{
34-
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
34+
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
3535

3636
// Finds all functionAst
3737
IEnumerable<Ast> functionAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true);
@@ -40,28 +40,32 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4040
{
4141
// Finds all ParamAsts.
4242
IEnumerable<Ast> varAsts = funcAst.FindAll(testAst => testAst is VariableExpressionAst, true);
43-
43+
4444
// Iterrates all ParamAsts and check if their names are on the list.
4545

4646
HashSet<string> paramVariables = new HashSet<string>();
4747
// only raise the rules for variables in the param block.
4848
if (funcAst.Body != null && funcAst.Body.ParamBlock != null && funcAst.Body.ParamBlock.Parameters != null)
4949
{
50-
paramVariables.UnionWith(funcAst.Body.ParamBlock.Parameters.Select(paramAst => paramAst.Name.VariablePath.UserPath));
50+
foreach (var paramAst in funcAst.Body.ParamBlock.Parameters)
51+
{
52+
if (Helper.Instance.IsUninitialized(paramAst.Name, funcAst))
53+
{
54+
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ProvideDefaultParameterValueError, paramAst.Name.VariablePath.UserPath),
55+
paramAst.Name.Extent, GetName(), DiagnosticSeverity.Warning, fileName, paramAst.Name.VariablePath.UserPath);
56+
}
57+
}
5158
}
5259

5360
if (funcAst.Parameters != null)
5461
{
55-
paramVariables.UnionWith(funcAst.Parameters.Select(paramAst => paramAst.Name.VariablePath.UserPath));
56-
}
57-
58-
// Iterates all VariableExpressionAst and check the command name.
59-
foreach (VariableExpressionAst varAst in varAsts)
60-
{
61-
if (Helper.Instance.IsUninitialized(varAst, funcAst) && paramVariables.Contains(varAst.VariablePath.UserPath))
62+
foreach (var paramAst in funcAst.Parameters)
6263
{
63-
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ProvideDefaultParameterValueError, varAst.VariablePath.UserPath),
64-
varAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, varAst.VariablePath.UserPath);
64+
if (Helper.Instance.IsUninitialized(paramAst.Name, funcAst))
65+
{
66+
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ProvideDefaultParameterValueError, paramAst.Name.VariablePath.UserPath),
67+
paramAst.Name.Extent, GetName(), DiagnosticSeverity.Warning, fileName, paramAst.Name.VariablePath.UserPath);
68+
}
6569
}
6670
}
6771
}

0 commit comments

Comments
 (0)