Skip to content

Commit b2beb22

Browse files
committed
first stage of supporting symbol renaming for splatted command Ast calls
1 parent 66b56b9 commit b2beb22

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/PowerShellEditorServices/Services/PowerShell/Refactoring/IterativeVariableVisitor.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Management.Automation.Language;
66
using Microsoft.PowerShell.EditorServices.Handlers;
77
using System.Linq;
8+
using System;
89

910
namespace Microsoft.PowerShell.EditorServices.Refactoring
1011
{
@@ -245,6 +246,31 @@ public void ProcessNode(Ast node)
245246

246247
switch (node)
247248
{
249+
case CommandAst commandAst:
250+
// Is the Target Variable a Parameter and is this commandAst the target function
251+
if (isParam && commandAst.GetCommandName()?.ToLower() == TargetFunction?.Name.ToLower())
252+
{
253+
// Check to see if this is a splatted call to the target function.
254+
Ast Splatted = null;
255+
foreach (Ast element in commandAst.CommandElements)
256+
{
257+
if (element is VariableExpressionAst varAst && varAst.Splatted)
258+
{
259+
Splatted = varAst;
260+
break;
261+
}
262+
}
263+
if (Splatted != null)
264+
{
265+
NewSplattedModification(Splatted);
266+
}
267+
else
268+
{
269+
// The Target Variable is a Parameter and the commandAst is the Target Function
270+
ShouldRename = true;
271+
}
272+
}
273+
break;
248274
case CommandParameterAst commandParameterAst:
249275

250276
if (commandParameterAst.ParameterName.ToLower() == OldName.ToLower())
@@ -339,6 +365,41 @@ public void ProcessNode(Ast node)
339365
Log.Add($"ShouldRename after proc: {ShouldRename}");
340366
}
341367

368+
internal void NewSplattedModification(Ast Splatted)
369+
{
370+
// Find the Splats Top Assignment / Definition
371+
Ast SplatAssignment = GetVariableTopAssignment(
372+
Splatted.Extent.StartLineNumber,
373+
Splatted.Extent.StartColumnNumber,
374+
ScriptAst);
375+
// Look for the Parameter within the Splats HashTable
376+
if (SplatAssignment.Parent is AssignmentStatementAst assignmentStatementAst &&
377+
assignmentStatementAst.Right is CommandExpressionAst commExpAst &&
378+
commExpAst.Expression is HashtableAst hashTableAst)
379+
{
380+
foreach (Tuple<ExpressionAst, StatementAst> element in hashTableAst.KeyValuePairs)
381+
{
382+
if (element.Item1 is StringConstantExpressionAst strConstAst &&
383+
strConstAst.Value.ToLower() == OldName.ToLower())
384+
{
385+
TextChange Change = new()
386+
{
387+
NewText = NewName,
388+
StartLine = strConstAst.Extent.StartLineNumber - 1,
389+
StartColumn = strConstAst.Extent.StartColumnNumber - 1,
390+
EndLine = strConstAst.Extent.StartLineNumber - 1,
391+
EndColumn = strConstAst.Extent.EndColumnNumber - 1,
392+
};
393+
394+
Modifications.Add(Change);
395+
break;
396+
}
397+
398+
}
399+
400+
}
401+
}
402+
342403
internal TextChange NewParameterAliasChange(VariableExpressionAst variableExpressionAst, ParameterAst paramAst)
343404
{
344405
// Check if an Alias AttributeAst already exists and append the new Alias to the existing list

0 commit comments

Comments
 (0)