Skip to content

Commit 11a3107

Browse files
committed
Apply review suggestions
1 parent 7507fec commit 11a3107

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/PowerShellEditorServices/Language/AstOperations.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,13 @@ static private bool IsPowerShellDataFileAstNode(dynamic node, Type[] levelAstMap
320320
/// <summary>
321321
/// Finds all files dot sourced in a script
322322
/// </summary>
323-
/// <param name="scriptFile">The script file to use to find dot sourced files.</param>
323+
/// <param name="scriptAst">The abstract syntax tree of the given script</param>
324+
/// <param name="filePath">The path of the given script</param>
324325
/// <returns></returns>
325-
static public string[] FindDotSourcedIncludes(ScriptFile scriptFile)
326+
static public string[] FindDotSourcedIncludes(Ast scriptAst, string filePath)
326327
{
327-
FindDotSourcedVisitor dotSourcedVisitor = new FindDotSourcedVisitor(scriptFile.FilePath);
328-
scriptFile.ScriptAst.Visit(dotSourcedVisitor);
328+
FindDotSourcedVisitor dotSourcedVisitor = new FindDotSourcedVisitor(filePath);
329+
scriptAst.Visit(dotSourcedVisitor);
329330

330331
return dotSourcedVisitor.DotSourcedFiles.ToArray();
331332
}

src/PowerShellEditorServices/Language/FindDotSourcedVisitor.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,23 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
4242
CommandElementAst commandElementAst = commandAst.CommandElements[0];
4343
if (commandAst.InvocationOperator.Equals(TokenKind.Dot))
4444
{
45-
if (commandElementAst is StringConstantExpressionAst stringConstantExpressionAst)
45+
string path;
46+
switch (commandElementAst)
4647
{
47-
// Strip any quote characters off of the string
48-
DotSourcedFiles.Add(PathUtils.NormalizePathSeparators(stringConstantExpressionAst.Value));
49-
}
50-
else if (commandElementAst is ExpandableStringExpressionAst expandableStringExpressionAst)
51-
{
52-
var path = GetPathFromExpandableStringExpression(expandableStringExpressionAst);
53-
if (path != null)
54-
{
55-
DotSourcedFiles.Add(PathUtils.NormalizePathSeparators(path));
56-
}
48+
case StringConstantExpressionAst stringConstantExpressionAst:
49+
path = stringConstantExpressionAst.Value;
50+
break;
51+
52+
case ExpandableStringExpressionAst expandableStringExpressionAst:
53+
path = GetPathFromExpandableStringExpression(expandableStringExpressionAst);
54+
break;
55+
56+
default:
57+
path = null;
58+
break;
5759
}
60+
61+
DotSourcedFiles.Add(PathUtils.NormalizePathSeparators(path));
5862
}
5963

6064
return base.VisitCommand(commandAst);
@@ -65,6 +69,7 @@ private string GetPathFromExpandableStringExpression(ExpandableStringExpressionA
6569
var path = expandableStringExpressionAst.Value;
6670
foreach (var nestedExpression in expandableStringExpressionAst.NestedExpressions)
6771
{
72+
// If the string contains the variable $PSScriptRoot, we replace it with the corresponding value.
6873
if (nestedExpression is VariableExpressionAst variableExpressionAst
6974
&& variableExpressionAst.VariablePath.UserPath.Equals("PSScriptRoot", StringComparison.CurrentCultureIgnoreCase))
7075
{

src/PowerShellEditorServices/Workspace/ScriptFile.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ScriptFile
2020
{
2121
#region Private Fields
2222

23-
private static readonly string[] s_newlines = new []
23+
private static readonly string[] s_newlines = new[]
2424
{
2525
"\r\n",
2626
"\n"
@@ -649,8 +649,7 @@ private void ParseFileContents()
649649
.ToArray();
650650

651651
//Get all dot sourced referenced files and store them
652-
this.ReferencedFiles =
653-
AstOperations.FindDotSourcedIncludes(this);
652+
this.ReferencedFiles = AstOperations.FindDotSourcedIncludes(this.ScriptAst, this.FilePath);
654653
}
655654

656655
#endregion

0 commit comments

Comments
 (0)