Skip to content

Commit 1e89761

Browse files
committed
Clean up AST usage, validate pester syntax better
1 parent 6f5fdc3 commit 1e89761

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/PowerShellEditorServices/Symbols/PesterDocumentSymbolProvider.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ private static bool IsPesterCommand(CommandAst commandAst)
6565
}
6666

6767
// Ensure the first word is a Pester keyword
68-
if (!(commandAst.CommandElements[0] is StringConstantExpressionAst pesterKeywordAst &&
69-
PesterSymbolReference.PesterKeywords.ContainsKey(pesterKeywordAst.Value)))
68+
if (!PesterSymbolReference.PesterKeywords.ContainsKey(commandAst.GetCommandName()))
7069
{
7170
return false;
7271
}
@@ -93,7 +92,9 @@ private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFil
9392
string commandName = pesterCommandAst.GetCommandName();
9493

9594
// Search for a name for the test
95+
// If the test has more than one argument for names, we set it to null
9696
string testName = null;
97+
bool alreadySawName = false;
9798
for (int i = 1; i < pesterCommandAst.CommandElements.Count; i++)
9899
{
99100
CommandElementAst currentCommandElement = pesterCommandAst.CommandElements[i];
@@ -104,17 +105,18 @@ private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFil
104105
i++;
105106
if (parameterAst.ParameterName == "Name" && i < pesterCommandAst.CommandElements.Count)
106107
{
107-
testName = (pesterCommandAst.CommandElements[i] as StringConstantExpressionAst)?.Value;
108-
break;
108+
testName = alreadySawName ? null : (pesterCommandAst.CommandElements[i] as StringConstantExpressionAst)?.Value;
109+
alreadySawName = true;
109110
}
110111
continue;
111112
}
112113

113114
// Otherwise, if an argument is given with no parameter, we assume it's the name
115+
// If we've already seen a name, we set the name to null
114116
if (pesterCommandAst.CommandElements[i] is StringConstantExpressionAst testNameStrAst)
115117
{
116-
testName = testNameStrAst.Value;
117-
break;
118+
testName = alreadySawName ? null : testNameStrAst.Value;
119+
alreadySawName = true;
118120
}
119121
}
120122

0 commit comments

Comments
 (0)