Skip to content

Commit edd5247

Browse files
rjmholtcursoragent
andcommitted
Port remaining upstream bug fixes for two rules
UseDeclaredVarsMoreThanAssignments (PowerShell#2013): skip scope-qualified variable references ($global:, $script:, drive-qualified) when checking for uses of local variables, preventing false negatives. AvoidPositionalParameters (PowerShell#1850): skip commands that have no parameters in the database (e.g. native CLI tools like az). Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 7f99057 commit edd5247

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

PSpecter/Builtin/Rules/AvoidPositionalParameters.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public override IEnumerable<ScriptDiagnostic> AnalyzeScript(Ast ast, IReadOnlyLi
8282
continue;
8383
}
8484

85+
if (HasNoParameters(commandName))
86+
{
87+
continue;
88+
}
89+
8590
if (HasSplattedVariable(cmdAst))
8691
{
8792
continue;
@@ -114,6 +119,12 @@ private bool IsApplicationCommand(string commandName)
114119
&& string.Equals(metadata!.CommandType, "Application", StringComparison.OrdinalIgnoreCase);
115120
}
116121

122+
private bool HasNoParameters(string commandName)
123+
{
124+
return _commandDb.TryGetCommand(commandName, platforms: null, out CommandMetadata? metadata)
125+
&& metadata!.Parameters.Count == 0;
126+
}
127+
117128
private static bool LooksLikeCmdlet(string commandName)
118129
{
119130
int hyphen = commandName.IndexOf('-');

PSpecter/Builtin/Rules/UseDeclaredVarsMoreThanAssignments.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ private IEnumerable<ScriptDiagnostic> AnalyzeScriptBlockAst(ScriptBlockAst scrip
110110
{
111111
foreach (VariableExpressionAst varAst in varAsts)
112112
{
113+
if (varAst.VariablePath.IsGlobal
114+
|| varAst.VariablePath.IsScript
115+
|| varAst.VariablePath.DriveName != null)
116+
{
117+
continue;
118+
}
119+
113120
varKey = varAst.GetNameWithoutScope();
114121
inAssignment = false;
115122

0 commit comments

Comments
 (0)