|
5 | 5 | using System.Management.Automation.Language;
|
6 | 6 | using Microsoft.PowerShell.EditorServices.Handlers;
|
7 | 7 | using System.Linq;
|
| 8 | +using System; |
8 | 9 |
|
9 | 10 | namespace Microsoft.PowerShell.EditorServices.Refactoring
|
10 | 11 | {
|
@@ -245,6 +246,31 @@ public void ProcessNode(Ast node)
|
245 | 246 |
|
246 | 247 | switch (node)
|
247 | 248 | {
|
| 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; |
248 | 274 | case CommandParameterAst commandParameterAst:
|
249 | 275 |
|
250 | 276 | if (commandParameterAst.ParameterName.ToLower() == OldName.ToLower())
|
@@ -339,6 +365,41 @@ public void ProcessNode(Ast node)
|
339 | 365 | Log.Add($"ShouldRename after proc: {ShouldRename}");
|
340 | 366 | }
|
341 | 367 |
|
| 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 | + |
342 | 403 | internal TextChange NewParameterAliasChange(VariableExpressionAst variableExpressionAst, ParameterAst paramAst)
|
343 | 404 | {
|
344 | 405 | // Check if an Alias AttributeAst already exists and append the new Alias to the existing list
|
|
0 commit comments