Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit85e14ce

Browse files
committed
fix: fix remove extraneous import implementation
1 parent1843c63 commit85e14ce

File tree

5 files changed

+65
-49
lines changed

5 files changed

+65
-49
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
importVuefrom'vue'
22
importVueRouterfrom'vue-router'
3-
importVuexfrom'Vuex'
3+
importVuexfrom'vuex'
44

55
Vue.use(VueRouter)
66
Vue.use(Vuex)
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
import'vue';
2-
import'vue-router';
3-
import'Vuex';

‎generator/codemods/global-api/remove-trivial-root.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* It is expected to be run after the `createApp` transformataion
33
* if a root component is trivial, that is, it contains only one simple prop,
44
* like `{ render: h => h(App) }`, then just use the `App` variable
5-
*
5+
*
66
* TODO: implement `remove-trivial-render`,
77
* move all other rootProps to the second argument of `createApp`
88
*@param {Object} context

‎generator/codemods/router/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ module.exports = function(fileInfo, api) {
3939
}elseif(mode==='abstract'){
4040
initializer='createMemoryHistory'
4141
}else{
42-
console.error(`mode must be one of 'hash', 'history', or 'abstract'`)
42+
console.error(
43+
`mode must be one of 'hash', 'history', or 'abstract'`
44+
)
4345
returnp
4446
}
4547

‎generator/codemods/utils/remove-extraneous-import.js

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,78 @@
22
* Note:
33
* here we don't completely remove the import declaration statement
44
* if all import specifiers are removed.
5-
* For example, `importVue from 'vue'`,
6-
* if `Vue` is unused, the statement would become `import 'vue'`.
5+
* For example, `importfoo from 'bar'`,
6+
* if `foo` is unused, the statement would become `import 'bar'`.
77
* It is because we are not sure if the module contains any side effects.
88
*@param {Object} context
99
*@param {import('jscodeshift').JSCodeshift} context.j
1010
*@param {ReturnType<import('jscodeshift').Core>} context.root
1111
*/
1212
module.exports=functionremoveExtraneousImport({ root, j},name){
13-
constisPathEqual=(path1,path2)=>{
14-
return(
15-
path1.node.start===path2.node.start&&path1.node.end===path2.node.end
16-
)
17-
}
18-
/**
19-
*@param {import('jscodeshift').ASTPath} path
20-
*/
21-
functionfilterAndRemoveImports(path){
22-
constusages=j(path)
23-
.closestScope()
24-
.find(j.Identifier,{ name})
25-
// Ignore the specifier
26-
.filter(identifierPath=>{
27-
constparent=identifierPath.parent.node
28-
return(
29-
!j.ImportDefaultSpecifier.check(parent)&&
30-
!j.ImportSpecifier.check(parent)
31-
)
32-
})
33-
// Ignore properties in MemberExpressions
34-
.filter(identifierPath=>{
35-
constparent=identifierPath.parent.node
36-
return!(
37-
j.MemberExpression.check(parent)&&
38-
parent.property===identifierPath.node
39-
)
40-
})
13+
constusages=root.find(j.Identifier,{ name}).filter(identifierPath=>{
14+
constparent=identifierPath.parent.node
4115

42-
if(!usages.length){
43-
j(path).remove()
16+
// Ignore the import specifier
17+
if(
18+
j.ImportDefaultSpecifier.check(parent)||
19+
j.ImportSpecifier.check(parent)
20+
){
21+
returnfalse
4422
}
45-
}
4623

47-
root
48-
.find(j.ImportSpecifier,{
49-
local:{
50-
name
51-
}
52-
})
53-
.filter(filterAndRemoveImports)
24+
// Ignore properties in MemberExpressions
25+
if(
26+
j.MemberExpression.check(parent)&&
27+
parent.property===identifierPath.node
28+
){
29+
returnfalse
30+
}
31+
32+
// Ignore keys in ObjectProperties
33+
if(
34+
j.ObjectProperty.check(parent)&&
35+
parent.key===identifierPath.node&&
36+
parent.value!==identifierPath.node
37+
){
38+
returnfalse
39+
}
40+
41+
returntrue
42+
})
5443

55-
root
56-
.find(j.ImportDefaultSpecifier,{
44+
if(!usages.length){
45+
letspecifier=root.find(j.ImportSpecifier,{
5746
local:{
5847
name
5948
}
6049
})
61-
.filter(filterAndRemoveImports)
50+
if(!specifier.length){
51+
specifier=root.find(j.ImportDefaultSpecifier,{
52+
local:{
53+
name
54+
}
55+
})
56+
}
57+
58+
if(!specifier.length){
59+
return
60+
}
61+
62+
constdecl=specifier.closest(j.ImportDeclaration)
63+
constdeclNode=decl.get(0).node
64+
constpeerSpecifiers=declNode.specifiers
65+
constsource=declNode.source.value
66+
67+
// these modules are known to have no side effects
68+
constsafelyRemovableModules=['vue','vue-router','vuex']
69+
if(
70+
peerSpecifiers.length===1&&
71+
safelyRemovableModules.includes(source)
72+
){
73+
decl.remove()
74+
}else{
75+
// otherwise, only remove the specifier
76+
specifier.remove()
77+
}
78+
}
6279
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp