99
1010namespace  Microsoft . PowerShell . EditorServices . Refactoring 
1111{ 
12+ 
13+  public  class  TargetSymbolNotFoundException  :  Exception 
14+  { 
15+  public  TargetSymbolNotFoundException ( ) 
16+  { 
17+  } 
18+ 
19+  public  TargetSymbolNotFoundException ( string  message ) 
20+  :  base ( message ) 
21+  { 
22+  } 
23+ 
24+  public  TargetSymbolNotFoundException ( string  message ,  Exception  inner ) 
25+  :  base ( message ,  inner ) 
26+  { 
27+  } 
28+  } 
29+ 
1230 internal  class  VariableRename  :  ICustomAstVisitor2 
1331 { 
1432 private  readonly  string  OldName ; 
@@ -23,72 +41,41 @@ internal class VariableRename : ICustomAstVisitor2
2341 internal  List < string >  dotSourcedScripts  =  new ( ) ; 
2442 internal  readonly  Ast  ScriptAst ; 
2543
26-  public  VariableRename ( string  OldName ,   string   NewName ,  int  StartLineNumber ,  int  StartColumnNumber ,  Ast  ScriptAst ) 
44+  public  VariableRename ( string  NewName ,  int  StartLineNumber ,  int  StartColumnNumber ,  Ast  ScriptAst ) 
2745 { 
28-  this . OldName  =  OldName . Replace ( "$" ,  "" ) ; 
2946 this . NewName  =  NewName ; 
3047 this . StartLineNumber  =  StartLineNumber ; 
3148 this . StartColumnNumber  =  StartColumnNumber ; 
3249 this . ScriptAst  =  ScriptAst ; 
3350
34-  VariableExpressionAst  Node  =  VariableRename . GetVariableTopAssignment ( this . OldName ,   StartLineNumber ,  StartColumnNumber ,  ScriptAst ) ; 
51+  VariableExpressionAst  Node  =  ( VariableExpressionAst ) VariableRename . GetVariableTopAssignment ( StartLineNumber ,  StartColumnNumber ,  ScriptAst ) ; 
3552 if  ( Node  !=  null ) 
3653 { 
3754
3855 TargetVariableAst  =  Node ; 
56+  OldName  =  TargetVariableAst . VariablePath . UserPath . Replace ( "$" ,  "" ) ; 
3957 this . StartColumnNumber  =  TargetVariableAst . Extent . StartColumnNumber ; 
4058 this . StartLineNumber  =  TargetVariableAst . Extent . StartLineNumber ; 
4159 } 
4260 } 
4361
44-  public  static   VariableExpressionAst  GetAstNodeByLineAndColumn ( string   OldName ,   int  StartLineNumber ,  int  StartColumnNumber ,  Ast  ScriptAst ) 
62+  public  static   Ast  GetAstNodeByLineAndColumn ( int  StartLineNumber ,  int  StartColumnNumber ,  Ast  ScriptAst ) 
4563 { 
46-  VariableExpressionAst  result  =  null ; 
47-  // Looking for a function 
48-  result  =  ( VariableExpressionAst ) ScriptAst . Find ( ast => 
64+  Ast  result  =  null ; 
65+  result  =  ScriptAst . Find ( ast => 
4966 { 
5067 return  ast . Extent . StartLineNumber  ==  StartLineNumber  && 
5168 ast . Extent . StartColumnNumber  ==  StartColumnNumber  && 
52-  ast  is  VariableExpressionAst  VarDef  && 
53-  VarDef . VariablePath . UserPath . ToLower ( )  ==  OldName . ToLower ( ) ; 
69+  ast  is  VariableExpressionAst  or CommandParameterAst ; 
5470 } ,  true ) ; 
71+  if  ( result  ==  null ) 
72+  { 
73+  throw  new  TargetSymbolNotFoundException ( ) ; 
74+  } 
5575 return  result ; 
5676 } 
57-  public  static   VariableExpressionAst  GetVariableTopAssignment ( string   OldName ,   int  StartLineNumber ,  int  StartColumnNumber ,  Ast  ScriptAst ) 
77+  public  static   Ast  GetVariableTopAssignment ( int  StartLineNumber ,  int  StartColumnNumber ,  Ast  ScriptAst ) 
5878 { 
59-  static   Ast  GetAstParentScope ( Ast  node ) 
60-  { 
61-  Ast  parent  =  node . Parent ; 
62-  // Walk backwards up the tree look 
63-  while  ( parent  !=  null ) 
64-  { 
65-  if  ( parent  is  ScriptBlockAst ) 
66-  { 
67-  break ; 
68-  } 
69-  parent  =  parent . Parent ; 
70-  } 
71-  return  parent ; 
72-  } 
73- 
74-  static   bool  WithinTargetsScope ( Ast  Target  , Ast  Child ) { 
75-  bool  r  =  false ; 
76-  Ast  childParent  =  Child . Parent ; 
77-  Ast  TargetScope  =  GetAstParentScope ( Target ) ; 
78-  while  ( childParent  !=  null ) 
79-  { 
80-  if  ( childParent  ==  TargetScope ) 
81-  { 
82-  break ; 
83-  } 
84-  childParent  =  childParent . Parent ; 
85-  } 
86-  if  ( childParent  ==  TargetScope ) 
87-  { 
88-  r  =  true ; 
89-  } 
90-  return  r ; 
91-  } 
9279
9380 // Look up the target object 
9481 VariableExpressionAst  node  =  GetAstNodeByLineAndColumn ( OldName ,  StartLineNumber ,  StartColumnNumber ,  ScriptAst ) ; 
@@ -98,8 +85,8 @@ static bool WithinTargetsScope(Ast Target ,Ast Child){
9885 List < VariableExpressionAst >  VariableAssignments  =  ScriptAst . FindAll ( ast => 
9986 { 
10087 return  ast  is  VariableExpressionAst  VarDef  && 
101-  VarDef . Parent  is  AssignmentStatementAst  && 
102-  VarDef . VariablePath . UserPath . ToLower ( )  ==  OldName . ToLower ( )  && 
88+  VarDef . Parent  is  AssignmentStatementAst  or  ParameterAst   && 
89+  VarDef . VariablePath . UserPath . ToLower ( )  ==  name . ToLower ( )  && 
10390 ( VarDef . Extent . EndLineNumber  <  node . Extent . StartLineNumber  || 
10491 ( VarDef . Extent . EndColumnNumber  <=  node . Extent . StartColumnNumber  && 
10592 VarDef . Extent . EndLineNumber  <=  node . Extent . StartLineNumber ) ) ; 
@@ -109,7 +96,7 @@ VarDef.Parent is AssignmentStatementAst &&
10996 { 
11097 return  node ; 
11198 } 
112-  VariableExpressionAst  CorrectDefinition  =  null ; 
99+  Ast  CorrectDefinition  =  null ; 
113100 for  ( int  i  =  VariableAssignments . Count  -  1 ;  i  >=  0 ;  i -- ) 
114101 { 
115102 VariableExpressionAst  element  =  VariableAssignments [ i ] ; 
@@ -136,15 +123,71 @@ VarDef.Parent is AssignmentStatementAst &&
136123 CorrectDefinition  =  element ; 
137124 break ; 
138125 } 
139-  if  ( WithinTargetsScope ( element , node ) ) 
126+  if  ( parent   is   FunctionDefinitionAst   funcDef   &&   node   is   CommandParameterAst ) 
140127 { 
141-  CorrectDefinition = element ; 
128+  if  ( node . Parent  is  CommandAst  commDef ) 
129+  { 
130+  if  ( funcDef . Name  ==  commDef . GetCommandName ( ) 
131+  &&  funcDef . Parent . Parent  ==  TargetParent ) 
132+  { 
133+  CorrectDefinition  =  element ; 
134+  break ; 
135+  } 
136+  } 
137+  } 
138+  if  ( WithinTargetsScope ( element ,  node ) ) 
139+  { 
140+  CorrectDefinition  =  element ; 
142141 } 
143142 } 
144-  } 
145143
144+ 
145+  } 
146146 return  CorrectDefinition  ??  node ; 
147147 } 
148+ 
149+  internal  static   Ast  GetAstParentScope ( Ast  node ) 
150+  { 
151+  Ast  parent  =  node ; 
152+  // Walk backwards up the tree look 
153+  while  ( parent  !=  null ) 
154+  { 
155+  if  ( parent  is  ScriptBlockAst  or FunctionDefinitionAst ) 
156+  { 
157+  break ; 
158+  } 
159+  parent  =  parent . Parent ; 
160+  } 
161+  if  ( parent  is  ScriptBlockAst  &&  parent . Parent  !=  null ) 
162+  { 
163+  parent  =  GetAstParentScope ( parent . Parent ) ; 
164+  } 
165+  return  parent ; 
166+  } 
167+ 
168+  internal  static   bool  WithinTargetsScope ( Ast  Target ,  Ast  Child ) 
169+  { 
170+  bool  r  =  false ; 
171+  Ast  childParent  =  Child . Parent ; 
172+  Ast  TargetScope  =  GetAstParentScope ( Target ) ; 
173+  while  ( childParent  !=  null ) 
174+  { 
175+  if  ( childParent  is  FunctionDefinitionAst ) 
176+  { 
177+  break ; 
178+  } 
179+  if  ( childParent  ==  TargetScope ) 
180+  { 
181+  break ; 
182+  } 
183+  childParent  =  childParent . Parent ; 
184+  } 
185+  if  ( childParent  ==  TargetScope ) 
186+  { 
187+  r  =  true ; 
188+  } 
189+  return  r ; 
190+  } 
148191 public  object  VisitArrayExpression ( ArrayExpressionAst  arrayExpressionAst )  =>  throw  new  NotImplementedException ( ) ; 
149192 public  object  VisitArrayLiteral ( ArrayLiteralAst  arrayLiteralAst )  =>  throw  new  NotImplementedException ( ) ; 
150193 public  object  VisitAssignmentStatement ( AssignmentStatementAst  assignmentStatementAst ) 
0 commit comments