Skip to content

Commit 5aa04ca

Browse files
committed
refactor: extract remove-extraneous-import
1 parent cf3b9c4 commit 5aa04ca

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

generator/codemods/global-api/index.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,11 @@ module.exports = function(fileInfo, api) {
1313
require('./remove-contextual-h')(context)
1414

1515
// remove extraneous imports
16+
const removeExtraneousImport = require('../utils/remove-extraneous-import')
1617
removeExtraneousImport(context, 'Vue')
1718
removeExtraneousImport(context, 'Vuex')
1819
removeExtraneousImport(context, 'VueRouter')
1920

2021
return root.toSource({ lineTerminator: '\n' })
2122
}
2223

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {Object} context
3+
* @param {import('jscodeshift').JSCodeshift} context.j
4+
* @param {ReturnType<import('jscodeshift').Core>} context.root
5+
*/
6+
function removeExtraneousImport({ root, j }, name) {
7+
const localUsages = root.find(j.Identifier, { name })
8+
if (localUsages.length === 1) {
9+
const importDecl = localUsages.closest(j.ImportDeclaration)
10+
11+
if (!importDecl.length) {
12+
return
13+
}
14+
15+
if (importDecl.get(0).node.specifiers.length === 1) {
16+
importDecl.remove()
17+
} else {
18+
localUsages.closest(j.ImportSpecifier).remove()
19+
localUsages.closest(j.ImportDefaultSpecifier).remove()
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)