Skip to content

Commit a75e2fb

Browse files
committed
fix(replace-node): compare children only when the node.kind is not of type Identifier
1 parent 3f9d4c8 commit a75e2fb

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

src/eq-node.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
import * as ts from 'typescript'
2-
2+
const debug = require('debug')('ts-codemod:eq')
33
export function eqNode(a: ts.Node, b: ts.Node): boolean {
4-
return (
5-
strictlyEq(a, b) || eqIdentifier(a, b) || (eqKind(a, b) && eqChildren(a, b))
6-
)
4+
return strictlyEq(a, b) || eqIdentifier(a, b) || eqChildren(a, b)
75
}
86

97
function eqChildren(a: ts.Node, b: ts.Node): boolean {
10-
return a.getChildren().every((aChild, id) => eqNode(b.getChildAt(id), aChild))
8+
/**
9+
* Children comparison should happen only for nodes that are
10+
* not of type Identifier
11+
*/
12+
if (eqKind(a, b) && !ts.isIdentifier(a)) {
13+
const result = a
14+
.getChildren()
15+
.every((aChild, id) => eqNode(b.getChildAt(id), aChild))
16+
if (result)
17+
debug('children:', `'${a.getFullText()}'`, `'${b.getFullText()}'`)
18+
return result
19+
}
20+
return false
1121
}
1222

1323
function eqKind(a: ts.Node, b: ts.Node) {
14-
return a.kind === b.kind
24+
const result = a.kind === b.kind
25+
if (result) debug('kind:', `'${a.getFullText()}'`, `'${b.getFullText()}'`)
26+
if (result) return result
1527
}
1628

1729
function eqIdentifier(a: ts.Node, b: ts.Node): boolean {
18-
return ts.isIdentifier(a) && ts.isIdentifier(b) && a.text === b.text
30+
const result = ts.isIdentifier(a) && ts.isIdentifier(b) && a.text === b.text
31+
if (result)
32+
debug('identifier:', `'${a.getFullText()}'`, `'${b.getFullText()}'`)
33+
return result
1934
}
2035

2136
function strictlyEq(a: ts.Node, b: ts.Node) {

test/replace-node.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,23 @@ describe('replace-node', () => {
2323
}).newContent
2424
assert.strictEqual(actual, expected)
2525
})
26+
27+
it('should convert identifiers', () => {
28+
const input = normalize(`
29+
import {Hoe} from '@action-land/core'
30+
export const view = (e: Hoe, m: {}, p: {}) => 'Hello'
31+
`)
32+
const expected = normalize(`
33+
import {Smitten} from '@action-land/core'
34+
export const view = (e: Smitten, m: {}, p: {}) => 'Hello'
35+
`)
36+
37+
const actual = transform({
38+
transformationCtor: ReplaceNode,
39+
content: input,
40+
path: './src/file.ts',
41+
params: {matchWith: 'Hoe', replaceWith: 'Smitten'}
42+
}).newContent
43+
assert.strictEqual(actual, expected)
44+
})
2645
})

transformations/replace-node.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {Transformation} from '..'
22
import * as ts from 'typescript'
3+
const debug = require('debug')('ts-codemod:replace-node')
4+
5+
const cName = (obj: any) => obj.constructor.name.replace('Object', '')
36

47
export default class ReplaceNode extends Transformation<{
58
matchWith: string
@@ -9,11 +12,24 @@ export default class ReplaceNode extends Transformation<{
912
private replaceWith: ts.Node = ts.createEmptyStatement()
1013

1114
init() {
12-
this.matchWith = this.toNode(this.params.matchWith)
13-
this.replaceWith = this.toNode(this.params.replaceWith)
15+
const {matchWith, replaceWith} = this.params
16+
this.matchWith = this.toNode(matchWith)
17+
this.replaceWith = this.toNode(replaceWith)
18+
debug('matchWith:', cName(this.matchWith), `'${matchWith}'`)
19+
debug('replaceWith:', cName(this.replaceWith), `'${replaceWith}'`)
1420
}
1521

1622
visit(node: ts.Node): ts.VisitResult<ts.Node> {
17-
return this.equal(node, this.matchWith) ? this.replaceWith : node
23+
debug('node:', cName(node), `'${node.getFullText()}'`)
24+
if (this.equal(node, this.matchWith)) {
25+
debug(
26+
'replacing',
27+
node.getFullText(),
28+
'with',
29+
this.replaceWith.getFullText()
30+
)
31+
return this.replaceWith
32+
}
33+
return node
1834
}
1935
}

0 commit comments

Comments
 (0)