Skip to content

Support module-qualified calls for Pester keywords #1998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 22, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Management.Automation.Language;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility;

namespace Microsoft.PowerShell.EditorServices.Services.Symbols
{
Expand Down Expand Up @@ -42,8 +43,7 @@ IEnumerable<SymbolReference> IDocumentSymbolProvider.ProvideDocumentSymbols(
private static bool IsNamedCommandWithArguments(Ast ast)
{
return ast is CommandAst commandAst &&
commandAst.InvocationOperator != TokenKind.Dot &&
PesterSymbolReference.GetCommandType(commandAst.GetCommandName()).HasValue &&
commandAst.InvocationOperator is not (TokenKind.Dot or TokenKind.Ampersand) &&
commandAst.CommandElements.Count >= 2;
}

Expand All @@ -59,8 +59,10 @@ private static bool IsPesterCommand(CommandAst commandAst)
return false;
}

// Ensure the first word is a Pester keyword
if (!PesterSymbolReference.PesterKeywords.ContainsKey(commandAst.GetCommandName()))
// Ensure the first word is a Pester keyword and in Pester-module if using module-qualified call
string commandName = CommandHelpers.StripModuleQualification(commandAst.GetCommandName(), out ReadOnlyMemory<char> module);
if (!PesterSymbolReference.PesterKeywords.ContainsKey(commandName) ||
!module.Span.Equals("pester".AsSpan(), StringComparison.OrdinalIgnoreCase))
{
return false;
}
Expand Down Expand Up @@ -90,14 +92,16 @@ private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFil
.TrimStart()
.TrimEnd(DefinitionTrimChars);

PesterCommandType? commandName = PesterSymbolReference.GetCommandType(pesterCommandAst.GetCommandName());
if (commandName == null)
string commandName = CommandHelpers.StripModuleQualification(pesterCommandAst.GetCommandName(), out _);
PesterCommandType? commandType = PesterSymbolReference.GetCommandType(commandName);
if (commandType == null)
{
return null;
}

string testName = null;
if (PesterSymbolReference.IsPesterTestCommand(commandName.Value)) {
if (PesterSymbolReference.IsPesterTestCommand(commandType.Value))
{
// Search for a name for the test
// If the test has more than one argument for names, we set it to null
bool alreadySawName = false;
Expand Down Expand Up @@ -130,7 +134,7 @@ private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFil

return new PesterSymbolReference(
scriptFile,
commandName.Value,
commandType.Value,
symbolName,
testName,
pesterCommandAst.Extent
Expand Down