Skip to content

Commit 29e4022

Browse files
author
Artem Vasilcenko
committed
Fix #9: handle type renames when they are not fully qualified
1 parent 17eabe1 commit 29e4022

File tree

5 files changed

+55
-10
lines changed

5 files changed

+55
-10
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/**/*

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@yworks/migrate-yfiles-app",
3-
"version": "5.0.0",
3+
"version": "5.1.0",
44
"description": "Semi-automatically migrate yFiles for HTML code to the latest version",
55
"main": "src/index.js",
66
"bin": {

test/from-2.3-to-2.4/dest/app.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1+
import {
2+
EdgeRouterData,
3+
TransitiveEdge,
4+
DistanceMetric,
5+
} from 'yfiles'
6+
17
const gc = new GraphComponent()
8+
new EdgeRouterData()
9+
new yfiles.router.EdgeRouterData()
10+
new foo.bar.PolylineEdgeRouterData()
11+
class Foo extends EdgeRouterData {}
212

313
const listener = () => {
414
console.log('hello')

test/from-2.3-to-2.4/src/app.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1+
import {
2+
PolylineEdgeRouterData,
3+
TransitiveClosureEdge,
4+
KMeansClusteringDistanceMetric,
5+
} from 'yfiles'
6+
17
const gc = new GraphComponent()
8+
new PolylineEdgeRouterData()
9+
new yfiles.router.PolylineEdgeRouterData()
10+
new foo.bar.PolylineEdgeRouterData()
11+
class Foo extends PolylineEdgeRouterData {}
212

313
const listener = () => {
414
console.log('hello')

transforms/namespaceChanges.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function doTransform({
66
ast,
77
filePath,
88
mappings,
9-
options
9+
options,
1010
}: {
1111
api: any
1212
ast: any
@@ -20,16 +20,40 @@ export function doTransform({
2020
const { namespaceChanges } = mappings
2121
const j = api.jscodeshift
2222

23-
const lastSegmentSet = new Set()
24-
const nameMap = new Map()
25-
for (const [key, value] of Object.entries(namespaceChanges)) {
26-
nameMap.set(key, value)
27-
lastSegmentSet.add(key.split('.').pop())
23+
const fqnNameMap = new Map()
24+
const nonFqnNameMap = new Map()
25+
for (const [key, value] of Object.entries(namespaceChanges) as [string, string][]) {
26+
fqnNameMap.set(key, value)
27+
const nonFqnKey = key.split('.').pop()
28+
const nonFqnValue = value.split('.').pop()
29+
nonFqnNameMap.set(nonFqnKey, nonFqnValue)
2830
}
2931

32+
ast
33+
.find(j.Identifier, {
34+
name: n => nonFqnNameMap.has(n),
35+
})
36+
.replaceWith(path => {
37+
if (path.parentPath && j.MemberExpression.check(path.parentPath.value)) {
38+
return path.value
39+
}
40+
const oldName = path.value.name
41+
const newName = nonFqnNameMap.get(oldName)
42+
if (incremental) {
43+
logMigrationMessage(
44+
filePath,
45+
path.value,
46+
createLogMessage`The type '${oldName}' has been renamed to '${newName}'.`,
47+
findCommentParent(path)
48+
)
49+
return path.value
50+
}
51+
return j.identifier(newName)
52+
})
53+
3054
ast
3155
.find(j.MemberExpression, {
32-
property: { type: 'Identifier', name: n => lastSegmentSet.has(n) }
56+
property: { type: 'Identifier', name: n => nonFqnNameMap.has(n) },
3357
})
3458
.replaceWith(path => {
3559
const fqnParts = [path.value.property.name]
@@ -40,8 +64,8 @@ export function doTransform({
4064
}
4165
fqnParts.unshift(current.object.name)
4266
const fqn = fqnParts.join('.')
43-
if (nameMap.has(fqn)) {
44-
const newName = nameMap.get(fqn)
67+
if (fqnNameMap.has(fqn)) {
68+
const newName = fqnNameMap.get(fqn)
4569
if (incremental) {
4670
logMigrationMessage(
4771
filePath,

0 commit comments

Comments
 (0)