Skip to content

Commit d4534c8

Browse files
committed
Rework function name check to be ast based
1 parent ba4f2da commit d4534c8

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/PowerShellEditorServices/Services/TextDocument/RenameService.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,14 @@ internal static bool IsValidFunctionName(string name)
396396
{
397397
// Allows us to supply function:varname or varname and get a proper result
398398
string candidate = "function " + name.TrimStart('$').TrimStart('-') + " {}";
399-
Parser.ParseInput(candidate, out Token[] tokens, out _);
400-
return tokens.Length == 5
401-
&& tokens[0].Kind == TokenKind.Function
402-
&& tokens[1].Kind == TokenKind.Identifier
403-
&& tokens[2].Kind == TokenKind.LCurly
404-
&& tokens[3].Kind == TokenKind.RCurly
405-
&& tokens[4].Kind == TokenKind.EndOfInput;
399+
Ast ast = Parser.ParseInput(candidate, out _, out ParseError[] errors);
400+
if (errors.Length > 0)
401+
{
402+
return false;
403+
}
404+
405+
return (ast.Find(a => a is FunctionDefinitionAst, false) as FunctionDefinitionAst)?
406+
.Name is not null;
406407
}
407408
}
408409

0 commit comments

Comments
 (0)