Skip to content

Commit 5cd5061

Browse files
committed
Fix review issues
1 parent 11a3107 commit 5cd5061

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

src/PowerShellEditorServices/Language/AstOperations.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,11 @@ static private bool IsPowerShellDataFileAstNode(dynamic node, Type[] levelAstMap
321321
/// Finds all files dot sourced in a script
322322
/// </summary>
323323
/// <param name="scriptAst">The abstract syntax tree of the given script</param>
324-
/// <param name="filePath">The path of the given script</param>
324+
/// <param name="psScriptRoot">Pre-calculated value of $PSScriptRoot</param>
325325
/// <returns></returns>
326-
static public string[] FindDotSourcedIncludes(Ast scriptAst, string filePath)
326+
static public string[] FindDotSourcedIncludes(Ast scriptAst, string psScriptRoot)
327327
{
328-
FindDotSourcedVisitor dotSourcedVisitor = new FindDotSourcedVisitor(filePath);
328+
FindDotSourcedVisitor dotSourcedVisitor = new FindDotSourcedVisitor(psScriptRoot);
329329
scriptAst.Visit(dotSourcedVisitor);
330330

331331
return dotSourcedVisitor.DotSourcedFiles.ToArray();

src/PowerShellEditorServices/Language/FindDotSourcedVisitor.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
using System;
77
using System.Collections.Generic;
8-
using System.IO;
98
using System.Management.Automation.Language;
109
using System.Text.RegularExpressions;
1110
using Microsoft.PowerShell.EditorServices.Utility;
@@ -17,17 +16,21 @@ namespace Microsoft.PowerShell.EditorServices
1716
/// </summary>
1817
internal class FindDotSourcedVisitor : AstVisitor
1918
{
20-
private readonly string _scriptDirectory;
19+
private readonly string _psScriptRoot;
2120

2221
/// <summary>
2322
/// A hash set of the dot sourced files (because we don't want duplicates)
2423
/// </summary>
2524
public HashSet<string> DotSourcedFiles { get; private set; }
2625

27-
public FindDotSourcedVisitor(string scriptPath)
26+
/// <summary>
27+
/// Creates a new instance of the FindDotSourcedVisitor class.
28+
/// </summary>
29+
/// <param name="psScriptRoot">Pre-calculated value of $PSScriptRoot</param>
30+
public FindDotSourcedVisitor(string psScriptRoot)
2831
{
2932
DotSourcedFiles = new HashSet<string>(StringComparer.CurrentCultureIgnoreCase);
30-
_scriptDirectory = Path.GetDirectoryName(scriptPath);
33+
_psScriptRoot = psScriptRoot;
3134
}
3235

3336
/// <summary>
@@ -58,7 +61,10 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
5861
break;
5962
}
6063

61-
DotSourcedFiles.Add(PathUtils.NormalizePathSeparators(path));
64+
if (!string.IsNullOrWhiteSpace(path))
65+
{
66+
DotSourcedFiles.Add(PathUtils.NormalizePathSeparators(path));
67+
}
6268
}
6369

6470
return base.VisitCommand(commandAst);
@@ -71,9 +77,9 @@ private string GetPathFromExpandableStringExpression(ExpandableStringExpressionA
7177
{
7278
// If the string contains the variable $PSScriptRoot, we replace it with the corresponding value.
7379
if (nestedExpression is VariableExpressionAst variableExpressionAst
74-
&& variableExpressionAst.VariablePath.UserPath.Equals("PSScriptRoot", StringComparison.CurrentCultureIgnoreCase))
80+
&& variableExpressionAst.VariablePath.UserPath.Equals("PSScriptRoot", StringComparison.OrdinalIgnoreCase))
7581
{
76-
path = path.Replace(variableExpressionAst.ToString(), _scriptDirectory);
82+
path = path.Replace(variableExpressionAst.ToString(), _psScriptRoot);
7783
}
7884
else
7985
{

src/PowerShellEditorServices/Language/LanguageService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ private static string GetDotSourcedPath(SymbolReference symbol, Workspace worksp
498498
{
499499
string cleanedUpSymbol = PathUtils.NormalizePathSeparators(symbol.SymbolName.Trim('\'', '"'));
500500
return workspace.ResolveRelativeScriptPath(Path.GetDirectoryName(scriptFile.FilePath),
501-
Regex.Replace(cleanedUpSymbol, @"\$PSScriptRoot", Path.GetDirectoryName(scriptFile.FilePath), RegexOptions.IgnoreCase));
501+
Regex.Replace(cleanedUpSymbol, @"\$PSScriptRoot|\${PSScriptRoot}", Path.GetDirectoryName(scriptFile.FilePath), RegexOptions.IgnoreCase));
502502
}
503503

504504
/// <summary>

src/PowerShellEditorServices/Workspace/ScriptFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ private void ParseFileContents()
649649
.ToArray();
650650

651651
//Get all dot sourced referenced files and store them
652-
this.ReferencedFiles = AstOperations.FindDotSourcedIncludes(this.ScriptAst, this.FilePath);
652+
this.ReferencedFiles = AstOperations.FindDotSourcedIncludes(this.ScriptAst, Path.GetDirectoryName(this.FilePath));
653653
}
654654

655655
#endregion

test/PowerShellEditorServices.Test.Shared/References/DotSources.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
. ./ReferenceFileE.ps1
22
. "$PSScriptRoot/ReferenceFileE.ps1"
3+
. "${PSScriptRoot}/ReferenceFileE.ps1"
34
. './ReferenceFileE.ps1'
45
. "./ReferenceFileE.ps1"
56
. .\ReferenceFileE.ps1

0 commit comments

Comments
 (0)