Skip to content

Commit

Permalink
fix(transformation): update crawler algorithm (#7)
Browse files Browse the repository at this point in the history
* fix: fix replace-node loop

* fix: compare nodes in transformation
  • Loading branch information
gmenih authored and tusharmath committed Oct 13, 2019
1 parent 5d44b6f commit 2a31928
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Transformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ export abstract class Transformation<T = {}> {

public forEach = (input: ts.Node): ts.VisitResult<ts.Node> => {
const node = this.visit(input)
return node instanceof Array
? node.map(_ => ts.visitEachChild(_, this.forEach, this.ctx))
: ts.visitEachChild(node, this.forEach, this.ctx)

if (node === input) {
return node instanceof Array
? node.map(_ => ts.visitEachChild(_, this.forEach, this.ctx))
: ts.visitEachChild(node, this.forEach, this.ctx)
}

return node
}

public toNode(template: string): ts.Node {
Expand Down
21 changes: 21 additions & 0 deletions test/replace-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,25 @@ describe('replace-node', () => {
}).newContent
assert.strictEqual(actual, expected)
})

it('should not fall into a loop if replaceWith contains a subset of matchWith', () => {
const input = normalize(`
function doAction (params) {
console.log(params.inputValue)
}
`)
const expected = normalize(`
function doAction (params) {
console.log(params && params.inputValue)
}
`)

const actual = transform({
transformationCtor: ReplaceNode,
content: input,
path: './src/file.ts',
params: {matchWith: 'params.inputValue', replaceWith: 'params && params.inputValue'}
}).newContent
assert.strictEqual(actual, expected)
})
})

0 comments on commit 2a31928

Please sign in to comment.