Skip to content

Commit 332d3c8

Browse files
committedNov 9, 2021
[Fix] no-import-module-exports: avoid false positives with a shadowed module or exports
Fixes #2297
1 parent add650a commit 332d3c8

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed
 

‎CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
99
### Fixed
1010
- [`extensions`]: ignore unresolveable type-only imports ([#2270], [#2271], [@jablko])
1111
- `importType`: fix `isExternalModule` calculation ([#2282], [@mx-bernhard])
12+
- [`no-import-module-exports`]: avoid false positives with a shadowed `module` or `exports` ([#2297], [@ljharb])
1213

1314
### Changed
1415
- [Docs] [`order`]: add type to the default groups ([#2272], [@charpeni])
@@ -939,6 +940,7 @@ for info on changes for earlier releases.
939940

940941
[`memo-parser`]: ./memo-parser/README.md
941942

943+
[#2297]: https://github.com/import-js/eslint-plugin-import/pull/2297
942944
[#2287]: https://github.com/import-js/eslint-plugin-import/pull/2287
943945
[#2282]: https://github.com/import-js/eslint-plugin-import/pull/2282
944946
[#2279]: https://github.com/import-js/eslint-plugin-import/pull/2279

‎src/rules/no-import-module-exports.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ function getEntryPoint(context) {
1313
}
1414
}
1515

16+
function findScope(context, identifier) {
17+
const scopeManager = context.getSourceCode().scopeManager;
18+
19+
return scopeManager.scopes.slice().reverse().find((scope) => scope.variables.some(variable => variable.identifiers.some((node) => node.name === identifier)));
20+
}
21+
1622
module.exports = {
1723
meta: {
1824
type: 'problem',
@@ -43,10 +49,11 @@ module.exports = {
4349
const isEntryPoint = entryPoint === fileName;
4450
const isIdentifier = node.object.type === 'Identifier';
4551
const hasKeywords = (/^(module|exports)$/).test(node.object.name);
46-
const isException = options.exceptions &&
47-
options.exceptions.some(glob => minimatch(fileName, glob));
52+
const objectScope = hasKeywords && findScope(context, node.object.name);
53+
const hasCJSExportReference = hasKeywords && (!objectScope || objectScope.type === 'module');
54+
const isException = !!options.exceptions && options.exceptions.some(glob => minimatch(fileName, glob));
4855

49-
if (isIdentifier && hasKeywords && !isEntryPoint && !isException) {
56+
if (isIdentifier && hasCJSExportReference && !isEntryPoint && !isException) {
5057
importDeclarations.forEach(importDeclaration => {
5158
context.report({
5259
node: importDeclaration,

‎tests/src/rules/no-import-module-exports.js

+50
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,56 @@ ruleTester.run('no-import-module-exports', rule, {
6464
`,
6565
filename: path.join(process.cwd(), 'tests/files/missing-entrypoint/cli.js'),
6666
}),
67+
test({
68+
code: `
69+
import fs from 'fs/promises';
70+
71+
const subscriptions = new Map();
72+
73+
export default async (client) => {
74+
/**
75+
* loads all modules and their subscriptions
76+
*/
77+
const modules = await fs.readdir('./src/modules');
78+
79+
await Promise.all(
80+
modules.map(async (moduleName) => {
81+
// Loads the module
82+
const module = await import(\`./modules/\${moduleName}/module.js\`);
83+
// skips the module, in case it is disabled.
84+
if (module.enabled) {
85+
// Loads each of it's subscriptions into their according list.
86+
module.subscriptions.forEach((fun, event) => {
87+
if (!subscriptions.has(event)) {
88+
subscriptions.set(event, []);
89+
}
90+
subscriptions.get(event).push(fun);
91+
});
92+
}
93+
})
94+
);
95+
96+
/**
97+
* Setting up all events.
98+
* binds all events inside the subscriptions map to call all functions provided
99+
*/
100+
subscriptions.forEach((funs, event) => {
101+
client.on(event, (...args) => {
102+
funs.forEach(async (fun) => {
103+
try {
104+
await fun(client, ...args);
105+
} catch (e) {
106+
client.emit('error', e);
107+
}
108+
});
109+
});
110+
});
111+
};
112+
`,
113+
parserOptions: {
114+
ecmaVersion: 2020,
115+
},
116+
}),
67117
],
68118
invalid: [
69119
test({

0 commit comments

Comments
 (0)
Please sign in to comment.