/
no-namespace.js
159 lines (143 loc) · 5.18 KB
/
no-namespace.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
/**
* @fileoverview Rule to disallow namespace import
* @author Radek Benkel
*/
import docsUrl from '../docsUrl'
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
url: docsUrl('no-namespace'),
},
fixable: 'code',
schema: [],
},
create: function (context) {
return {
'ImportNamespaceSpecifier': function (node) {
const scopeVariables = context.getScope().variables
const namespaceVariable = scopeVariables.find((variable) =>
variable.defs[0].node === node
)
const namespaceReferences = namespaceVariable.references
const namespaceIdentifiers = namespaceReferences.map(reference => reference.identifier)
const canFix = namespaceIdentifiers.length > 0 && !usesNamespaceAsObject(namespaceIdentifiers)
context.report({
node,
message: `Unexpected namespace import.`,
fix: canFix && (fixer => {
const scopeManager = context.getSourceCode().scopeManager
const fixes = []
// Pass 1: Collect variable names that are already in scope for each reference we want
// to transform, so that we can be sure that we choose non-conflicting import names
const importNameConflicts = {}
namespaceIdentifiers.forEach((identifier) => {
const parent = identifier.parent
if (parent && parent.type === 'MemberExpression') {
const importName = getMemberPropertyName(parent)
const localConflicts = getVariableNamesInScope(scopeManager, parent)
if (!importNameConflicts[importName]) {
importNameConflicts[importName] = localConflicts
} else {
localConflicts.forEach((c) => importNameConflicts[importName].add(c))
}
}
})
// Choose new names for each import
const importNames = Object.keys(importNameConflicts)
const importLocalNames = generateLocalNames(
importNames,
importNameConflicts,
namespaceVariable.name
)
// Replace the ImportNamespaceSpecifier with a list of ImportSpecifiers
const namedImportSpecifiers = importNames.map((importName) =>
importName === importLocalNames[importName]
? importName
: `${importName} as ${importLocalNames[importName]}`
)
fixes.push(fixer.replaceText(node, `{ ${namedImportSpecifiers.join(', ')} }`))
// Pass 2: Replace references to the namespace with references to the named imports
namespaceIdentifiers.forEach((identifier) => {
const parent = identifier.parent
if (parent && parent.type === 'MemberExpression') {
const importName = getMemberPropertyName(parent)
fixes.push(fixer.replaceText(parent, importLocalNames[importName]))
}
})
return fixes
}),
})
},
}
},
}
/**
* @param {Identifier[]} namespaceIdentifiers
* @returns {boolean} `true` if the namespace variable is more than just a glorified constant
*/
function usesNamespaceAsObject(namespaceIdentifiers) {
return !namespaceIdentifiers.every((identifier) => {
const parent = identifier.parent
// `namespace.x` or `namespace['x']`
return (
parent && parent.type === 'MemberExpression' &&
(parent.property.type === 'Identifier' || parent.property.type === 'Literal')
)
})
}
/**
* @param {MemberExpression} memberExpression
* @returns {string} the name of the member in the object expression, e.g. the `x` in `namespace.x`
*/
function getMemberPropertyName(memberExpression) {
return memberExpression.property.type === 'Identifier'
? memberExpression.property.name
: memberExpression.property.value
}
/**
* @param {ScopeManager} scopeManager
* @param {ASTNode} node
* @return {Set<string>}
*/
function getVariableNamesInScope(scopeManager, node) {
let currentNode = node
let scope = scopeManager.acquire(currentNode)
while (scope == null) {
currentNode = currentNode.parent
scope = scopeManager.acquire(currentNode, true)
}
return new Set([
...scope.variables.map(variable => variable.name),
...scope.upper.variables.map(variable => variable.name),
])
}
/**
*
* @param {*} names
* @param {*} nameConflicts
* @param {*} namespaceName
*/
function generateLocalNames(names, nameConflicts, namespaceName) {
const localNames = {}
names.forEach((name) => {
let localName
if (!nameConflicts[name].has(name)) {
localName = name
} else if (!nameConflicts[name].has(`${namespaceName}_${name}`)) {
localName = `${namespaceName}_${name}`
} else {
for (let i = 1; i < Infinity; i++) {
if (!nameConflicts[name].has(`${namespaceName}_${name}_${i}`)) {
localName = `${namespaceName}_${name}_${i}`
break
}
}
}
localNames[name] = localName
})
return localNames
}