-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
namespace.js
217 lines (183 loc) · 6.72 KB
/
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import declaredScope from 'eslint-module-utils/declaredScope';
import Exports from '../ExportMap';
import importDeclaration from '../importDeclaration';
import docsUrl from '../docsUrl';
module.exports = {
meta: {
type: 'problem',
docs: {
url: docsUrl('namespace'),
},
schema: [
{
type: 'object',
properties: {
allowComputed: {
description: 'If `false`, will report computed (and thus, un-lintable) references to namespace members.',
type: 'boolean',
default: false,
},
},
additionalProperties: false,
},
],
},
create: function namespaceRule(context) {
// read options
const {
allowComputed = false,
} = context.options[0] || {};
const namespaces = new Map();
function makeMessage(last, namepath) {
return `'${last.name}' not found in ${namepath.length > 1 ? 'deeply ' : ''}imported namespace '${namepath.join('.')}'.`;
}
return {
// pick up all imports at body entry time, to properly respect hoisting
Program({ body }) {
function processBodyStatement(declaration) {
if (declaration.type !== 'ImportDeclaration') return;
if (declaration.specifiers.length === 0) return;
const imports = Exports.get(declaration.source.value, context);
if (imports == null) return null;
if (imports.errors.length) {
imports.reportErrors(context, declaration);
return;
}
for (const specifier of declaration.specifiers) {
switch (specifier.type) {
case 'ImportNamespaceSpecifier':
if (!imports.size) {
context.report(
specifier,
`No exported names found in module '${declaration.source.value}'.`,
);
}
namespaces.set(specifier.local.name, imports);
break;
case 'ImportDefaultSpecifier':
case 'ImportSpecifier': {
const meta = imports.get(
// default to 'default' for default https://i.imgur.com/nj6qAWy.jpg
specifier.imported ? (specifier.imported.name || specifier.imported.value) : 'default',
);
if (!meta || !meta.namespace) { break; }
namespaces.set(specifier.local.name, meta.namespace);
break;
}
}
}
}
body.forEach(processBodyStatement);
},
// same as above, but does not add names to local map
ExportNamespaceSpecifier(namespace) {
const declaration = importDeclaration(context);
const imports = Exports.get(declaration.source.value, context);
if (imports == null) return null;
if (imports.errors.length) {
imports.reportErrors(context, declaration);
return;
}
if (!imports.size) {
context.report(
namespace,
`No exported names found in module '${declaration.source.value}'.`,
);
}
},
// todo: check for possible redefinition
MemberExpression(dereference) {
if (dereference.object.type !== 'Identifier') return;
if (!namespaces.has(dereference.object.name)) return;
if (declaredScope(context, dereference.object.name) !== 'module') return;
if (dereference.parent.type === 'AssignmentExpression' && dereference.parent.left === dereference) {
context.report(
dereference.parent,
`Assignment to member of namespace '${dereference.object.name}'.`,
);
}
// go deep
let namespace = namespaces.get(dereference.object.name);
const namepath = [dereference.object.name];
// while property is namespace and parent is member expression, keep validating
while (namespace instanceof Exports && dereference.type === 'MemberExpression') {
if (dereference.computed) {
if (!allowComputed) {
context.report(
dereference.property,
`Unable to validate computed reference to imported namespace '${dereference.object.name}'.`,
);
}
return;
}
if (!namespace.has(dereference.property.name)) {
context.report(
dereference.property,
makeMessage(dereference.property, namepath),
);
break;
}
const exported = namespace.get(dereference.property.name);
if (exported == null) return;
// stash and pop
namepath.push(dereference.property.name);
namespace = exported.namespace;
dereference = dereference.parent;
}
},
VariableDeclarator({ id, init }) {
if (init == null) return;
if (init.type !== 'Identifier') return;
if (!namespaces.has(init.name)) return;
// check for redefinition in intermediate scopes
if (declaredScope(context, init.name) !== 'module') return;
// DFS traverse child namespaces
function testKey(pattern, namespace, path = [init.name]) {
if (!(namespace instanceof Exports)) return;
if (pattern.type !== 'ObjectPattern') return;
for (const property of pattern.properties) {
if (
property.type === 'ExperimentalRestProperty'
|| property.type === 'RestElement'
|| !property.key
) {
continue;
}
if (property.key.type !== 'Identifier') {
context.report({
node: property,
message: 'Only destructure top-level names.',
});
continue;
}
if (!namespace.has(property.key.name)) {
context.report({
node: property,
message: makeMessage(property.key, path),
});
continue;
}
path.push(property.key.name);
const dependencyExportMap = namespace.get(property.key.name);
// could be null when ignored or ambiguous
if (dependencyExportMap !== null) {
testKey(property.value, dependencyExportMap.namespace, path);
}
path.pop();
}
}
testKey(id, namespaces.get(init.name));
},
JSXMemberExpression({ object, property }) {
if (!namespaces.has(object.name)) return;
const namespace = namespaces.get(object.name);
if (!namespace.has(property.name)) {
context.report({
node: property,
message: makeMessage(property, [object.name]),
});
}
},
};
},
};