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 c471b68
Showing
4 changed files
with
4,817 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,38 @@ | ||
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"); | ||
Vue.directive('demo', {}) | ||
Vue.component('myComponent',{}) | ||
`, | ||
`const app = Vue.createApp(App).use(button_counter).use(router).use(store); | ||
app.directive('demo', {}) | ||
app.component('myComponent',{}) | ||
app.mount("#app");`, | ||
|
||
'add const app and transform Vue to app in main.js' | ||
) | ||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
`const app = Vue.createApp(App).use(button_counter).use(router).use(store); | ||
Vue.directive('demo', {}) | ||
Vue.component('myComponent',{}) | ||
app.mount("#app");`, | ||
`const app = Vue.createApp(App).use(button_counter).use(router).use(store); | ||
app.directive('demo', {}) | ||
app.component('myComponent',{}) | ||
app.mount("#app");`, | ||
|
||
'do not add const app when app.mount() is already exist ' | ||
) |
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,107 @@ | ||
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) | ||
|
||
// Determine if there is a .mount() | ||
const createAppParent = root.find(j.ExpressionStatement, { | ||
expression: { | ||
callee: { | ||
property: { | ||
name: 'mount' | ||
} | ||
} | ||
} | ||
}) | ||
if (createAppParent.length != 1) { | ||
return | ||
} | ||
|
||
// replace Vue.component and Vue.directive | ||
root | ||
.find(j.MemberExpression, { | ||
object: { | ||
name: 'Vue' | ||
} | ||
}) | ||
.filter( | ||
node => | ||
// @ts-ignore | ||
node.value.property.name === 'directive' || | ||
// @ts-ignore | ||
node.value.property.name === 'component' | ||
) | ||
.forEach(node => { | ||
// @ts-ignore | ||
node.value.object.name = 'app' | ||
}) | ||
|
||
// Determine if there is a app.mount('#app') | ||
const ifConstApp = root.find(j.MemberExpression, { | ||
object: { | ||
name: 'app' | ||
}, | ||
property: { | ||
name: 'mount' | ||
} | ||
}) | ||
if (ifConstApp.length > 0) { | ||
return | ||
} | ||
|
||
let newCreateApp | ||
let mountContext | ||
createAppParent.forEach(node => { | ||
const createApp = node.value.expression | ||
|
||
// get the value in the .mount() | ||
// @ts-ignore | ||
mountContext = createApp.arguments[0].value | ||
|
||
// get the expression without .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.