Skip to content

Commit a034574

Browse files
committed
Revert "Revert "Fix PowerShell#827 Pester TestName w/expandable str returns nothing (PowerShell#851)""
This reverts commit f26755f.
1 parent f26755f commit a034574

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/PowerShellEditorServices/Symbols/PesterDocumentSymbolProvider.cs

+19-6
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ IEnumerable<SymbolReference> IDocumentSymbolProvider.ProvideDocumentSymbols(
3232

3333
return commandAsts.OfType<CommandAst>()
3434
.Where(IsPesterCommand)
35-
.Select(ast => ConvertPesterAstToSymbolReference(scriptFile, ast))
36-
.Where(pesterSymbol => pesterSymbol?.TestName != null);
35+
.Select(ast => ConvertPesterAstToSymbolReference(scriptFile, ast));
3736
}
3837

3938
/// <summary>
@@ -105,20 +104,21 @@ private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFil
105104
// Check for an explicit "-Name" parameter
106105
if (currentCommandElement is CommandParameterAst parameterAst)
107106
{
107+
// Found -Name parameter, move to next element which is the argument for -TestName
108108
i++;
109-
if (parameterAst.ParameterName == "Name" && i < pesterCommandAst.CommandElements.Count)
109+
110+
if (!alreadySawName && TryGetTestNameArgument(pesterCommandAst.CommandElements[i], out testName))
110111
{
111-
testName = alreadySawName ? null : (pesterCommandAst.CommandElements[i] as StringConstantExpressionAst)?.Value;
112112
alreadySawName = true;
113113
}
114+
114115
continue;
115116
}
116117

117118
// Otherwise, if an argument is given with no parameter, we assume it's the name
118119
// If we've already seen a name, we set the name to null
119-
if (pesterCommandAst.CommandElements[i] is StringConstantExpressionAst testNameStrAst)
120+
if (!alreadySawName && TryGetTestNameArgument(pesterCommandAst.CommandElements[i], out testName))
120121
{
121-
testName = alreadySawName ? null : testNameStrAst.Value;
122122
alreadySawName = true;
123123
}
124124
}
@@ -131,6 +131,19 @@ private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFil
131131
pesterCommandAst.Extent
132132
);
133133
}
134+
135+
private static bool TryGetTestNameArgument(CommandElementAst commandElementAst, out string testName)
136+
{
137+
testName = null;
138+
139+
if (commandElementAst is StringConstantExpressionAst testNameStrAst)
140+
{
141+
testName = testNameStrAst.Value;
142+
return true;
143+
}
144+
145+
return (commandElementAst is ExpandableStringExpressionAst);
146+
}
134147
}
135148

136149
/// <summary>

0 commit comments

Comments
 (0)