Skip to content

Commit eaf1ec0

Browse files
committed
feat: extract remove-trivial-root
1 parent 0ea3e59 commit eaf1ec0

8 files changed

+76
-32
lines changed

generator/codemods/global-api/create-app-mount.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @param {Object} context
3+
* @param {import('jscodeshift').JSCodeshift} context.j
4+
* @param {ReturnType<import('jscodeshift').Core>} context.root
5+
*/
16
module.exports = function createAppMount(context) {
27
const { j, root } = context
38

@@ -15,28 +20,6 @@ module.exports = function createAppMount(context) {
1520
let options = node.callee.object.arguments[0]
1621
const el = node.arguments[0]
1722

18-
// if it's a simple option, like `{ render: h => h(App) }`,
19-
// then just use the App variable
20-
if (
21-
options.properties.length === 1 &&
22-
options.properties[0].key.name === 'render' &&
23-
options.properties[0].value.type === 'ArrowFunctionExpression' &&
24-
options.properties[0].value.body.type === 'CallExpression'
25-
) {
26-
options = options.properties[0].value.body.arguments[0]
27-
} else {
28-
// replace `render: h => h(App)` with `render: () => h(App)
29-
// and add an `h` import
30-
const renderFn = options.properties.find(p => p.key.name === 'render' && p.value.type === 'ArrowFunctionExpression')
31-
if (renderFn) {
32-
const addImport = require('./utilities/add-import')
33-
addImport(context, { imported: 'h' }, 'vue')
34-
35-
// remove the `h` parameter
36-
renderFn.value.params.shift()
37-
}
38-
}
39-
4023
return j.callExpression(
4124
j.memberExpression(
4225
j.callExpression(j.identifier('createApp'), [options]),

generator/codemods/global-api/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @type {import('jscodeshift').Transform} */
12
module.exports = function(fileInfo, api) {
23
const j = api.jscodeshift
34
const root = j(fileInfo.source)
@@ -7,10 +8,11 @@ module.exports = function(fileInfo, api) {
78
addImport(context, { imported: 'createApp' }, 'vue')
89

910
require('./create-app-mount')(context)
11+
require('./vuex')(context)
12+
require('./router')(context)
13+
require('./remove-trivial-root')(context)
1014
require('./remove-production-tip')(context)
1115
require('./remove-contextual-h')(context)
12-
require('./remove-trivial-render')(context)
13-
require('./vuex')(context)
1416

1517
// remove extraneous Vue import
1618
const localVueUsages = root.find(j.Identifier, { name: 'Vue' })

generator/codemods/global-api/remove-contextual-h.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const addImport = require('./utilities/add-import')
22

3-
// replace `render: h => h(App)` with `render: () => h(App)
3+
/**
4+
* replace `render: h => h(App)` with `render: () => h(App)
5+
* @param {Object} context
6+
* @param {import('jscodeshift').JSCodeshift} context.j
7+
* @param {ReturnType<import('jscodeshift').Core>} context.root
8+
*/
49
module.exports = function removeContextualH (context) {
510
const { j, root } = context
611

generator/codemods/global-api/remove-production-tip.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
/**
2+
* @param {Object} context
3+
* @param {import('jscodeshift').JSCodeshift} context.j
4+
* @param {ReturnType<import('jscodeshift').Core>} context.root
5+
*/
16
module.exports = function removeProductionTip({ j, root }) {
27
const productionTipAssignment = root.find(
38
j.AssignmentExpression,
49
n =>
5-
n.left.type === 'MemberExpression' &&
10+
j.MemberExpression.check(n.left) &&
611
n.left.property.name === 'productionTip' &&
712
n.left.object.property.name === 'config' &&
813
n.left.object.object.name === 'Vue'

generator/codemods/global-api/remove-trivial-render.js

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* It is expected to be run after the `createApp` transformataion
3+
* if a root component is trivial, that is, it contains only one simple option,
4+
* like `{ render: h => h(App) }`, then just use the `App` variable
5+
* @param {Object} context
6+
* @param {import('jscodeshift').JSCodeshift} context.j
7+
* @param {ReturnType<import('jscodeshift').Core>} context.root
8+
*/
9+
module.exports = function removeTrivialRoot({ j, root }) {
10+
const appRoots = root.find(j.CallExpression, {
11+
callee: { name: 'createApp' },
12+
arguments: args => args.length === 1 && args[0].type === 'ObjectExpression'
13+
})
14+
appRoots.forEach(({ node: createAppCall }) => {
15+
const { properties } = createAppCall.arguments[0]
16+
if (
17+
properties.length === 1 &&
18+
properties[0].key.name === 'render' &&
19+
j.ArrowFunctionExpression.check(properties[0].value)
20+
) {
21+
const renderFnBody = properties[0].value.body
22+
const isTrivial =
23+
j.CallExpression.check(renderFnBody) &&
24+
renderFnBody.arguments.length === 1
25+
if (isTrivial) {
26+
createAppCall.arguments[0] = renderFnBody.arguments[0]
27+
}
28+
}
29+
})
30+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"vue": "^3.0.0-alpha.4"
2121
},
2222
"devDependencies": {
23+
"@types/jscodeshift": "^0.7.0",
2324
"jest": "^25.1.0",
2425
"jscodeshift": "^0.7.0",
2526
"prettier": "^1.19.1"

yarn.lock

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,14 @@
10541054
"@types/istanbul-lib-coverage" "*"
10551055
"@types/istanbul-lib-report" "*"
10561056

1057+
"@types/jscodeshift@^0.7.0":
1058+
version "0.7.0"
1059+
resolved "https://registry.yarnpkg.com/@types/jscodeshift/-/jscodeshift-0.7.0.tgz#796bf05a420c5d3e95b196b1fa01cc75f10c41b2"
1060+
integrity sha512-W59Y/fnztfJ/5gP4m2ZSWHvb4JPl9SXXn5ne3/6kYP5twRek9DTyKB6xbPIeg0RXbcNTHPBXHNLcKw3AlCrjTw==
1061+
dependencies:
1062+
ast-types "0.12.1"
1063+
recast "0.17.2"
1064+
10571065
"@types/stack-utils@^1.0.1":
10581066
version "1.0.1"
10591067
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
@@ -1206,6 +1214,11 @@ assign-symbols@^1.0.0:
12061214
resolved "https://registry.yarnpkg.com/assign-symbols/-/assign-symbols-1.0.0.tgz#59667f41fadd4f20ccbc2bb96b8d4f7f78ec0367"
12071215
integrity sha1-WWZ/QfrdTyDMvCu5a41Pf3jsA2c=
12081216

1217+
1218+
version "0.12.1"
1219+
resolved "https://registry.yarnpkg.com/ast-types/-/ast-types-0.12.1.tgz#55d3737a8a68e1ccde131067005ce7ee3dd42b99"
1220+
integrity sha512-H2izJAyT2xwew4TxShpmxe6f9R5hHgJQy1QloLiUC2yrJMtyraBWNJL7903rpeCY9keNUipORR/zIUC2XcYKng==
1221+
12091222
12101223
version "0.13.2"
12111224
resolved "https://registry.yarnpkg.com/ast-types/-/ast-types-0.13.2.tgz#df39b677a911a83f3a049644fb74fdded23cea48"
@@ -3451,7 +3464,7 @@ pretty-format@^25.1.0:
34513464
ansi-styles "^4.0.0"
34523465
react-is "^16.12.0"
34533466

3454-
private@^0.1.8:
3467+
private@^0.1.8, private@~0.1.5:
34553468
version "0.1.8"
34563469
resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff"
34573470
integrity sha512-VvivMrbvd2nKkiG38qjULzlc+4Vx4wm/whI9pQD35YrARNnhxeiRktSOhSukRLFNlzg6Br/cJPet5J/u19r/mg==
@@ -3499,6 +3512,16 @@ realpath-native@^1.1.0:
34993512
dependencies:
35003513
util.promisify "^1.0.0"
35013514

3515+
3516+
version "0.17.2"
3517+
resolved "https://registry.yarnpkg.com/recast/-/recast-0.17.2.tgz#f18f18cf20bf3fad4522621a7f9c2ada37276814"
3518+
integrity sha512-YHFvn4rBXl8eIjALjUiOV/AP3xFpyGNGNHDw9mAncAWuIdgnBKjbZQ9+P3VlsKcNaNapRVFlTEX1dvDRlYwyxg==
3519+
dependencies:
3520+
ast-types "0.12.1"
3521+
esprima "~4.0.0"
3522+
private "~0.1.5"
3523+
source-map "~0.6.1"
3524+
35023525
recast@^0.18.1:
35033526
version "0.18.7"
35043527
resolved "https://registry.yarnpkg.com/recast/-/recast-0.18.7.tgz#56338a6d803c8c3b9113344440dc70d13c8a1ef7"

0 commit comments

Comments
 (0)