Skip to content

Commit c2f003a

Browse files
BarryThePenguinljharb
authored andcommitted
[Fix] no-import-module-exports: avoid a false positive for import variables
1 parent 404b5ce commit c2f003a

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
3434
- `ExportMap`: add missing param to function ([#2589], thanks [@Fdawgs])
3535
- [`no-unused-modules`]: `checkPkgFieldObject` filters boolean fields from checks ([#2598], thanks [@mpint])
3636
- [`no-cycle`]: accept Flow `typeof` imports, just like `type` ([#2608], thanks [@gnprice])
37+
- [`no-import-module-exports`]: avoid a false positive for import variables ([#2315], thanks [@BarryThePenguin])
3738

3839
### Changed
3940
- [Tests] [`named`]: Run all TypeScript test ([#2427], thanks [@ProdigySim])
@@ -1074,6 +1075,7 @@ for info on changes for earlier releases.
10741075
[#2332]: https://github.com/import-js/eslint-plugin-import/pull/2332
10751076
[#2334]: https://github.com/import-js/eslint-plugin-import/pull/2334
10761077
[#2330]: https://github.com/import-js/eslint-plugin-import/pull/2330
1078+
[#2315]: https://github.com/import-js/eslint-plugin-import/pull/2315
10771079
[#2305]: https://github.com/import-js/eslint-plugin-import/pull/2305
10781080
[#2299]: https://github.com/import-js/eslint-plugin-import/pull/2299
10791081
[#2297]: https://github.com/import-js/eslint-plugin-import/pull/2297
@@ -1578,6 +1580,7 @@ for info on changes for earlier releases.
15781580
[@atos1990]: https://github.com/atos1990
15791581
[@azyzz228]: https://github.com/azyzz228
15801582
[@barbogast]: https://github.com/barbogast
1583+
[@BarryThePenguin]: https://github.com/BarryThePenguin
15811584
[@be5invis]: https://github.com/be5invis
15821585
[@beatrizrezener]: https://github.com/beatrizrezener
15831586
[@benmosher]: https://github.com/benmosher

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ function findScope(context, identifier) {
1919
return scopeManager && scopeManager.scopes.slice().reverse().find((scope) => scope.variables.some(variable => variable.identifiers.some((node) => node.name === identifier)));
2020
}
2121

22+
function findDefinition(objectScope, identifier) {
23+
const variable = objectScope.variables.find(variable => variable.name === identifier);
24+
return variable.defs.find(def => def.name.name === identifier);
25+
}
26+
2227
module.exports = {
2328
meta: {
2429
type: 'problem',
@@ -50,10 +55,12 @@ module.exports = {
5055
const isIdentifier = node.object.type === 'Identifier';
5156
const hasKeywords = (/^(module|exports)$/).test(node.object.name);
5257
const objectScope = hasKeywords && findScope(context, node.object.name);
58+
const variableDefinition = objectScope && findDefinition(objectScope, node.object.name);
59+
const isImportBinding = variableDefinition && variableDefinition.type === 'ImportBinding';
5360
const hasCJSExportReference = hasKeywords && (!objectScope || objectScope.type === 'module');
5461
const isException = !!options.exceptions && options.exceptions.some(glob => minimatch(fileName, glob));
5562

56-
if (isIdentifier && hasCJSExportReference && !isEntryPoint && !isException) {
63+
if (isIdentifier && hasCJSExportReference && !isEntryPoint && !isException && !isImportBinding) {
5764
importDeclarations.forEach(importDeclaration => {
5865
context.report({
5966
node: importDeclaration,

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path';
22
import { RuleTester } from 'eslint';
33

4-
import { test, testVersion } from '../utils';
4+
import { eslintVersionSatisfies, test, testVersion } from '../utils';
55

66
const ruleTester = new RuleTester({
77
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
@@ -40,6 +40,12 @@ ruleTester.run('no-import-module-exports', rule, {
4040
exports.foo = bar
4141
`,
4242
}),
43+
eslintVersionSatisfies('>= 4') ? test({
44+
code: `
45+
import { module } from 'qunit'
46+
module.skip('A test', function () {})
47+
`,
48+
}) : [],
4349
test({
4450
code: `
4551
import foo from 'path';

0 commit comments

Comments
 (0)