Skip to content

Commit 2a31928

Browse files
gmenihtusharmath
authored andcommitted
fix(transformation): update crawler algorithm (#7)
* fix: fix replace-node loop * fix: compare nodes in transformation
1 parent 5d44b6f commit 2a31928

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Transformation.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ export abstract class Transformation<T = {}> {
1717

1818
public forEach = (input: ts.Node): ts.VisitResult<ts.Node> => {
1919
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
2328
}
2429

2530
public toNode(template: string): ts.Node {

test/replace-node.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,25 @@ describe('replace-node', () => {
4242
}).newContent
4343
assert.strictEqual(actual, expected)
4444
})
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+
})
4566
})

0 commit comments

Comments
 (0)