-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
rename-default-import.js
182 lines (160 loc) · 4.48 KB
/
rename-default-import.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* @fileoverview Rule to enforce aliases for default imports
* @author Michał Kołodziejski
*/
import docsUrl from '../docsUrl'
import has from 'has'
function isDefaultImport(specifier) {
if (specifier.type === 'ImportDefaultSpecifier') {
return true
}
if (specifier.type === 'ImportSpecifier' && specifier.imported.name === 'default') {
return true
}
return false
}
function isCommonJSImport(declaration) {
const variableInit = declaration.init
if (variableInit.type === 'CallExpression') {
return variableInit.callee.name === 'require'
}
return false
}
function handleImport(
context,
node,
specifierOrDeclaration,
packageName,
importAlias,
exportedIdentifiers
) {
const mappings = context.options[0] || {}
if (!has(mappings, packageName) || mappings[packageName] === importAlias) {
return
}
let declaredVariables
if (specifierOrDeclaration.type === 'VariableDeclarator') {
declaredVariables = context.getDeclaredVariables(specifierOrDeclaration.parent)[0]
} else {
declaredVariables = context.getDeclaredVariables(specifierOrDeclaration)[0]
}
const references = declaredVariables ? declaredVariables.references : []
const skipFixing = exportedIdentifiers.indexOf(importAlias) !== -1
context.report({
node: node,
message: `Default import from '${packageName}' should be aliased to `
+ `${mappings[packageName]}, not ${importAlias}`,
fix: skipFixing ? null : fixImportOrRequire(specifierOrDeclaration, mappings[packageName]),
})
for (const variableReference of references) {
if (specifierOrDeclaration.type === 'VariableDeclarator' && variableReference.init) {
continue
}
context.report({
node: variableReference.identifier,
message: `Using incorrect binding name '${variableReference.identifier.name}' `
+ `instead of ${mappings[packageName]} for `
+ `default import from package ${packageName}`,
fix: fixer => {
if (skipFixing) {
return
}
return fixer.replaceText(variableReference.identifier, mappings[packageName])
},
})
}
}
function fixImportOrRequire(node, text) {
return function(fixer) {
let newAlias = text
let nodeOrToken
if (node.type === 'VariableDeclarator') {
nodeOrToken = node.id
newAlias = text
} else {
nodeOrToken = node
if (node.imported && node.imported.name === 'default') {
newAlias = `default as ${text}`
} else {
newAlias = text
}
}
return fixer.replaceText(nodeOrToken, newAlias)
}
}
module.exports = {
meta: {
docs: {
url: docsUrl('rename-default-import'),
recommended: false,
},
schema: [
{
type: 'object',
minProperties: 1,
additionalProperties: {
type: 'string',
},
},
],
fixable: 'code',
},
create: function(context) {
const exportedIdentifiers = []
return {
'Program': function(programNode) {
const {body} = programNode
body.forEach((node) => {
if (node.type === 'ExportNamedDeclaration') {
node.specifiers.forEach((specifier) => {
const {exported: {name}} = specifier
if (exportedIdentifiers.indexOf(name) === -1) {
exportedIdentifiers.push(name)
}
})
}
})
},
'ImportDeclaration:exit': function(node) {
const {source, specifiers} = node
const {options} = context
if (options.length === 0) {
return
}
for (const specifier of specifiers) {
if (!isDefaultImport(specifier)) {
continue
}
handleImport(
context,
source,
specifier,
source.value,
specifier.local.name,
exportedIdentifiers
)
}
},
'VariableDeclaration:exit': function(node) {
const {declarations} = node
const {options} = context
if (options.length === 0) {
return
}
for (const declaration of declarations) {
if (!isCommonJSImport(declaration) || context.getScope(declaration).type !== 'module') {
continue
}
handleImport(
context,
node,
declaration,
declaration.init.arguments[0].value,
declaration.id.name,
exportedIdentifiers
)
}
},
}
},
}