Skip to content

Commit b0e6f7f

Browse files
committed
[Refactor] namespace: try to improve performance
See #2340
1 parent 00a4ede commit b0e6f7f

File tree

2 files changed

+41
-40
lines changed

2 files changed

+41
-40
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2020
- [Tests] `no-nodejs-modules`: add tests for node protocol URL ([#2367], thanks [@sosukesuzuki])
2121
- [Tests] `default`, `no-anonymous-default-export`, `no-mutable-exports`, `no-named-as-default-member`, `no-named-as-default`: add tests for arbitrary module namespace names ([#2358], thanks [@sosukesuzuki])
2222
- [Docs] [`no-unresolved`]: Fix RegExp escaping in readme ([#2332], thanks [@stephtr])
23+
- [Refactor] `namespace`: try to improve performance ([#2340], thanks [@ljharb])
2324

2425
## [2.25.4] - 2022-01-02
2526

@@ -1255,6 +1256,7 @@ for info on changes for earlier releases.
12551256
[#211]: https://github.com/import-js/eslint-plugin-import/pull/211
12561257
[#164]: https://github.com/import-js/eslint-plugin-import/pull/164
12571258
[#157]: https://github.com/import-js/eslint-plugin-import/pull/157
1259+
[#2340]: https://github.com/import-js/eslint-plugin-import/issues/2340
12581260
[#2255]: https://github.com/import-js/eslint-plugin-import/issues/2255
12591261
[#2210]: https://github.com/import-js/eslint-plugin-import/issues/2210
12601262
[#2201]: https://github.com/import-js/eslint-plugin-import/issues/2201

src/rules/namespace.js

+39-40
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,44 @@ import Exports from '../ExportMap';
33
import importDeclaration from '../importDeclaration';
44
import docsUrl from '../docsUrl';
55

6+
function processBodyStatement(context, namespaces, declaration) {
7+
if (declaration.type !== 'ImportDeclaration') return;
8+
9+
if (declaration.specifiers.length === 0) return;
10+
11+
const imports = Exports.get(declaration.source.value, context);
12+
if (imports == null) return null;
13+
14+
if (imports.errors.length > 0) {
15+
imports.reportErrors(context, declaration);
16+
return;
17+
}
18+
19+
declaration.specifiers.forEach((specifier) => {
20+
switch (specifier.type) {
21+
case 'ImportNamespaceSpecifier':
22+
if (!imports.size) {
23+
context.report(
24+
specifier,
25+
`No exported names found in module '${declaration.source.value}'.`,
26+
);
27+
}
28+
namespaces.set(specifier.local.name, imports);
29+
break;
30+
case 'ImportDefaultSpecifier':
31+
case 'ImportSpecifier': {
32+
const meta = imports.get(
33+
// default to 'default' for default https://i.imgur.com/nj6qAWy.jpg
34+
specifier.imported ? (specifier.imported.name || specifier.imported.value) : 'default',
35+
);
36+
if (!meta || !meta.namespace) { break; }
37+
namespaces.set(specifier.local.name, meta.namespace);
38+
break;
39+
}
40+
}
41+
});
42+
}
43+
644
module.exports = {
745
meta: {
846
type: 'problem',
@@ -41,44 +79,7 @@ module.exports = {
4179
return {
4280
// pick up all imports at body entry time, to properly respect hoisting
4381
Program({ body }) {
44-
function processBodyStatement(declaration) {
45-
if (declaration.type !== 'ImportDeclaration') return;
46-
47-
if (declaration.specifiers.length === 0) return;
48-
49-
const imports = Exports.get(declaration.source.value, context);
50-
if (imports == null) return null;
51-
52-
if (imports.errors.length) {
53-
imports.reportErrors(context, declaration);
54-
return;
55-
}
56-
57-
for (const specifier of declaration.specifiers) {
58-
switch (specifier.type) {
59-
case 'ImportNamespaceSpecifier':
60-
if (!imports.size) {
61-
context.report(
62-
specifier,
63-
`No exported names found in module '${declaration.source.value}'.`,
64-
);
65-
}
66-
namespaces.set(specifier.local.name, imports);
67-
break;
68-
case 'ImportDefaultSpecifier':
69-
case 'ImportSpecifier': {
70-
const meta = imports.get(
71-
// default to 'default' for default https://i.imgur.com/nj6qAWy.jpg
72-
specifier.imported ? (specifier.imported.name || specifier.imported.value) : 'default',
73-
);
74-
if (!meta || !meta.namespace) { break; }
75-
namespaces.set(specifier.local.name, meta.namespace);
76-
break;
77-
}
78-
}
79-
}
80-
}
81-
body.forEach(processBodyStatement);
82+
body.forEach(x => processBodyStatement(context, namespaces, x));
8283
},
8384

8485
// same as above, but does not add names to local map
@@ -120,7 +121,6 @@ module.exports = {
120121
const namepath = [dereference.object.name];
121122
// while property is namespace and parent is member expression, keep validating
122123
while (namespace instanceof Exports && dereference.type === 'MemberExpression') {
123-
124124
if (dereference.computed) {
125125
if (!allowComputed) {
126126
context.report(
@@ -147,7 +147,6 @@ module.exports = {
147147
namespace = exported.namespace;
148148
dereference = dereference.parent;
149149
}
150-
151150
},
152151

153152
VariableDeclarator({ id, init }) {

0 commit comments

Comments
 (0)