forked from vuejs/vue-codemod
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c1b806
commit 08e08ab
Showing
4 changed files
with
4,802 additions
and
4,540 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { defineInlineTest } from 'jscodeshift/src/testUtils' | ||
|
||
const transform = require('../const-app') | ||
|
||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
`Vue.createApp(App).use(button_counter).use(router).use(store).mount("#app"); | ||
//used by new_directive_api | ||
Vue.directive('demo', {}) | ||
`, | ||
`const app = Vue.createApp(App).use(button_counter).use(router).use(store); | ||
//used by new_directive_api | ||
app.directive('demo', {}) | ||
app.mount("#app");`, | ||
|
||
'const app' | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import wrap from '../src/wrapAstTransformation' | ||
import type { ASTTransformation } from '../src/wrapAstTransformation' | ||
import { getCntFunc } from '../src/report' | ||
|
||
export const transformAST: ASTTransformation = ({ root, j }) => { | ||
const cntFunc = getCntFunc('const-app', global.outputReport) | ||
|
||
//判断是否存在.mount() | ||
// @ts-ignore | ||
const createAppParent = root.find(j.ExpressionStatement, { | ||
expression: { | ||
callee: { | ||
property: { | ||
name: 'mount' | ||
} | ||
} | ||
} | ||
}) | ||
if (createAppParent.length != 1) { | ||
return | ||
} | ||
|
||
//处理Vue.component和Vue.directive | ||
root | ||
.find(j.MemberExpression, { | ||
object: { | ||
name: 'Vue' | ||
} | ||
// @ts-ignore | ||
}) | ||
.filter( | ||
node => | ||
// @ts-ignore | ||
node.value.property.name === 'directive' || node.value.property.name === 'component' | ||
) | ||
.forEach(node => { | ||
// @ts-ignore | ||
node.value.object.name = 'app' | ||
}) | ||
|
||
//判断是否已存在app.mount('#app') | ||
const ifConstApp = root.find(j.MemberExpression, { | ||
object: { | ||
name: 'app' | ||
}, | ||
property: { | ||
name: 'mount' | ||
} | ||
}) | ||
if (ifConstApp.length > 0) { | ||
return | ||
} | ||
|
||
var newCreateApp | ||
var mountContext | ||
createAppParent.forEach(node => { | ||
const createApp = node.value.expression | ||
|
||
//获取mount()中的内容 | ||
// @ts-ignore | ||
mountContext = createApp.arguments[0].value | ||
|
||
//去除..mount('#app') | ||
// @ts-ignore | ||
newCreateApp = createApp.callee.object | ||
}) | ||
|
||
createAppParent.replaceWith( | ||
j.variableDeclaration('const', [ | ||
j.variableDeclarator(j.identifier('app'), newCreateApp) | ||
]) | ||
) | ||
|
||
|
||
let end = 0 | ||
let lastNode = createAppParent.get() | ||
root.find(j.VariableDeclaration).forEach(node => { | ||
// @ts-ignore | ||
if (node.value.end > end) { | ||
// @ts-ignore | ||
end = node.value.end | ||
lastNode = node | ||
} | ||
}) | ||
root.find(j.ExpressionStatement).forEach(node => { | ||
// @ts-ignore | ||
if (node.value.end > end) { | ||
// @ts-ignore | ||
end = node.value.end | ||
lastNode = node | ||
} | ||
}) | ||
|
||
j(lastNode).insertAfter( | ||
j.expressionStatement( | ||
j.callExpression( | ||
j.memberExpression(j.identifier('app'), j.identifier('mount')), | ||
// @ts-ignore | ||
[j.stringLiteral(mountContext)] | ||
) | ||
) | ||
) | ||
|
||
cntFunc() | ||
} | ||
|
||
export default wrap(transformAST) | ||
export const parser = 'babylon' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.