Skip to content

Commit 5ced27c

Browse files
fix: use manaully migration warn instead of throwing error for import mode (#83)
1 parent 8d96271 commit 5ced27c

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

bin/vue-codemod.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ function processTransformation(
165165
transformationName: string,
166166
transformationModule: TransformationModule
167167
) {
168-
console.time(`\x1B[0mProcessing use \x1B[1m${transformationName} transformation\x1B[0m\x1B[33m`)
168+
console.time(
169+
`\x1B[0mProcessing use \x1B[1m${transformationName} transformation\x1B[0m\x1B[33m`
170+
)
169171
if (formatter === 'log')
170172
logger.time(`Processing use ${transformationName} transformation`)
171173
let ruleProcessFile: string[] = []
@@ -208,7 +210,9 @@ function processTransformation(
208210
}
209211
}
210212
if (ruleProcessFile.length) {
211-
console.timeEnd(`\x1B[0mProcessing use \x1B[1m${transformationName} transformation\x1B[0m\x1B[33m`)
213+
console.timeEnd(
214+
`\x1B[0mProcessing use \x1B[1m${transformationName} transformation\x1B[0m\x1B[33m`
215+
)
212216
if (formatter === 'log')
213217
logger.timeEnd(`Processing use ${transformationName} transformation`)
214218
if (

transformations/vue-router-v4.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import { transformAST as removeExtraneousImport } from './remove-extraneous-impo
66

77
import type { ObjectExpression } from 'jscodeshift'
88
import { getCntFunc } from '../src/report'
9+
import { pushManualList } from '../src/report'
910

1011
// new Router() -> createRouter()
1112
export const transformAST: ASTTransformation = context => {
12-
const { root, j } = context
13+
const { root, j, filename } = context
1314
const routerImportDecls = root.find(j.ImportDeclaration, {
1415
source: {
1516
value: 'vue-router'
@@ -51,6 +52,50 @@ export const transformAST: ASTTransformation = context => {
5152
source: 'vue-router'
5253
})
5354
}
55+
56+
// filter the node to the manual list
57+
let ifEnd = false
58+
newVueRouter.forEach(({ node }) => {
59+
if (
60+
node.arguments.length > 0 &&
61+
!j.ObjectExpression.check(node.arguments[0])
62+
) {
63+
const path = filename
64+
const name = 'vue-router new VueRouter'
65+
const suggest = `Currently, only object expressions passed to \`new VueRouter\` can be transformed.`
66+
const website =
67+
'https://next.router.vuejs.org/guide/migration/index.html#new-router-becomes-createrouter'
68+
pushManualList(path, node, name, suggest, website)
69+
ifEnd = true
70+
return
71+
}
72+
if (node.arguments.length > 0) {
73+
// @ts-ignore
74+
const routerConfig: ObjectExpression = node.arguments[0]
75+
routerConfig.properties.forEach(p => {
76+
if (
77+
(j.ObjectProperty.check(p) || j.Property.check(p)) &&
78+
(p.key as any).name === 'mode'
79+
) {
80+
const mode = (p.value as any).value
81+
if (mode !== 'hash' && mode !== 'history' && mode !== 'abstract') {
82+
const path = filename
83+
const name = 'vue-router mode'
84+
const suggest = `mode must be one of 'hash', 'history', or 'abstract'`
85+
const website =
86+
'https://next.router.vuejs.org/guide/migration/index.html#new-history-option-to-replace-mode'
87+
pushManualList(path, p, name, suggest, website)
88+
ifEnd = true
89+
return
90+
}
91+
}
92+
})
93+
}
94+
})
95+
if (ifEnd) {
96+
return
97+
}
98+
5499
newVueRouter.replaceWith(({ node }) => {
55100
// mode: 'history' -> history: createWebHistory(), etc
56101
let historyMode = 'createWebHashHistory'
@@ -67,7 +112,6 @@ export const transformAST: ASTTransformation = context => {
67112
if (!j.ObjectProperty.check(p) && !j.Property.check(p)) {
68113
return true
69114
}
70-
71115
if ((p.key as any).name === 'mode') {
72116
const mode = (p.value as any).value
73117
if (mode === 'hash') {
@@ -109,7 +153,6 @@ export const transformAST: ASTTransformation = context => {
109153
)
110154
)
111155
)
112-
113156
return j.callExpression(j.identifier(localCreateRouter), node.arguments)
114157
})
115158

@@ -130,12 +173,10 @@ export const transformAST: ASTTransformation = context => {
130173
specifier: { type: 'named', imported: 'START_LOCATION' },
131174
source: 'vue-router'
132175
})
133-
134176
startLocationMember.replaceWith(({ node }) => {
135177
return node.property
136178
})
137179
}
138-
139180
removeExtraneousImport(context, {
140181
localBinding: localVueRouter
141182
})

0 commit comments

Comments
 (0)