Skip to content

Commit cf3b9c4

Browse files
committed
feat: implement remove-vue-use
1 parent c2a7609 commit cf3b9c4

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Vue from 'vue'
2+
import VueRouter from 'vue-router'
3+
import Vuex from 'Vuex'
4+
5+
Vue.use(VueRouter)
6+
Vue.use(Vuex)

generator/codemods/global-api/__testfixtures__/vue-use.output.js

Whitespace-only changes.

generator/codemods/global-api/__tests__/global-api-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ defineTest(__dirname, 'index', null, 'custom-root-option')
77
// defineTest(__dirname, 'index', null, 'el')
88
defineTest(__dirname, 'index', null, 'vue-router')
99
defineTest(__dirname, 'index', null, 'vuex')
10+
defineTest(__dirname, 'index', null, 'vue-use')

generator/codemods/global-api/index.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,36 @@ module.exports = function(fileInfo, api) {
99
require('./root-option-to-use')(context, 'router')
1010
require('./remove-trivial-root')(context)
1111
require('./remove-production-tip')(context)
12+
require('./remove-vue-use')(context)
1213
require('./remove-contextual-h')(context)
1314

14-
// remove extraneous Vue import
15-
const localVueUsages = root.find(j.Identifier, { name: 'Vue' })
16-
if (localVueUsages.length === 1) {
17-
localVueUsages.closest(j.ImportDefaultSpecifier).remove()
18-
}
15+
// remove extraneous imports
16+
removeExtraneousImport(context, 'Vue')
17+
removeExtraneousImport(context, 'Vuex')
18+
removeExtraneousImport(context, 'VueRouter')
1919

2020
return root.toSource({ lineTerminator: '\n' })
2121
}
22+
23+
/**
24+
* @param {Object} context
25+
* @param {import('jscodeshift').JSCodeshift} context.j
26+
* @param {ReturnType<import('jscodeshift').Core>} context.root
27+
*/
28+
function removeExtraneousImport({ root, j }, name) {
29+
const localUsages = root.find(j.Identifier, { name })
30+
if (localUsages.length === 1) {
31+
const importDecl = localUsages.closest(j.ImportDeclaration)
32+
33+
if (!importDecl.length) {
34+
return
35+
}
36+
37+
if (importDecl.get(0).node.specifiers.length === 1) {
38+
importDecl.remove()
39+
} else {
40+
localUsages.closest(j.ImportSpecifier).remove()
41+
localUsages.closest(j.ImportDefaultSpecifier).remove()
42+
}
43+
}
44+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Remove `Vue.use()` calls
3+
* Per current design, `Vue.use` is replaced by `app.use`.
4+
* But in library implementations like `vue-router` and `vuex`,
5+
* the new `app.use` does not reuse the same argument passed to `Vue.use()`,
6+
* but expects instantiated instances that are used to pass to the root components instead.
7+
* So we now expect the migration to be done in the `root-option-to-use` transformation,
8+
* and the `Vue.use` statements can be just abandoned.
9+
* @param {Object} context
10+
* @param {import('jscodeshift').JSCodeshift} context.j
11+
* @param {ReturnType<import('jscodeshift').Core>} context.root
12+
*/
13+
module.exports = function removeVueUse({ j, root }) {
14+
const vueUseCalls = root.find(j.CallExpression, {
15+
callee: {
16+
type: 'MemberExpression',
17+
object: {
18+
name: 'Vue'
19+
},
20+
property: {
21+
name: 'use'
22+
}
23+
}
24+
})
25+
vueUseCalls.remove()
26+
}

0 commit comments

Comments
 (0)