File tree Expand file tree Collapse file tree 2 files changed +29
-3
lines changed Expand file tree Collapse file tree 2 files changed +29
-3
lines changed Original file line number Diff line number Diff line change @@ -17,9 +17,14 @@ export abstract class Transformation<T = {}> {
17
17
18
18
public forEach = ( input : ts . Node ) : ts . VisitResult < ts . Node > => {
19
19
const node = this . visit ( input )
20
- return node instanceof Array
21
- ? node . map ( _ => ts . visitEachChild ( _ , this . forEach , this . ctx ) )
22
- : ts . visitEachChild ( node , this . forEach , this . ctx )
20
+
21
+ if ( node === input ) {
22
+ return node instanceof Array
23
+ ? node . map ( _ => ts . visitEachChild ( _ , this . forEach , this . ctx ) )
24
+ : ts . visitEachChild ( node , this . forEach , this . ctx )
25
+ }
26
+
27
+ return node
23
28
}
24
29
25
30
public toNode ( template : string ) : ts . Node {
Original file line number Diff line number Diff line change @@ -42,4 +42,25 @@ describe('replace-node', () => {
42
42
} ) . newContent
43
43
assert . strictEqual ( actual , expected )
44
44
} )
45
+
46
+ it ( 'should not fall into a loop if replaceWith contains a subset of matchWith' , ( ) => {
47
+ const input = normalize ( `
48
+ function doAction (params) {
49
+ console.log(params.inputValue)
50
+ }
51
+ ` )
52
+ const expected = normalize ( `
53
+ function doAction (params) {
54
+ console.log(params && params.inputValue)
55
+ }
56
+ ` )
57
+
58
+ const actual = transform ( {
59
+ transformationCtor : ReplaceNode ,
60
+ content : input ,
61
+ path : './src/file.ts' ,
62
+ params : { matchWith : 'params.inputValue' , replaceWith : 'params && params.inputValue' }
63
+ } ) . newContent
64
+ assert . strictEqual ( actual , expected )
65
+ } )
45
66
} )
You can’t perform that action at this time.
0 commit comments