Skip to content

Commit

Permalink
[Fix] no-import-module-exports: avoid a false positive for import v…
Browse files Browse the repository at this point in the history
…ariables
  • Loading branch information
BarryThePenguin authored and ljharb committed Nov 30, 2021
1 parent 404b5ce commit cd6648d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -34,6 +34,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- `ExportMap`: add missing param to function ([#2589], thanks [@Fdawgs])
- [`no-unused-modules`]: `checkPkgFieldObject` filters boolean fields from checks ([#2598], thanks [@mpint])
- [`no-cycle`]: accept Flow `typeof` imports, just like `type` ([#2608], thanks [@gnprice])
- [`no-import-module-exports`]: avoid a false positive for import variables ([#2315], thanks [@BarryThePenguin])

### Changed
- [Tests] [`named`]: Run all TypeScript test ([#2427], thanks [@ProdigySim])
Expand Down Expand Up @@ -1074,6 +1075,7 @@ for info on changes for earlier releases.
[#2332]: https://github.com/import-js/eslint-plugin-import/pull/2332
[#2334]: https://github.com/import-js/eslint-plugin-import/pull/2334
[#2330]: https://github.com/import-js/eslint-plugin-import/pull/2330
[#2315]: https://github.com/import-js/eslint-plugin-import/pull/2315
[#2305]: https://github.com/import-js/eslint-plugin-import/pull/2305
[#2299]: https://github.com/import-js/eslint-plugin-import/pull/2299
[#2297]: https://github.com/import-js/eslint-plugin-import/pull/2297
Expand Down Expand Up @@ -1578,6 +1580,7 @@ for info on changes for earlier releases.
[@atos1990]: https://github.com/atos1990
[@azyzz228]: https://github.com/azyzz228
[@barbogast]: https://github.com/barbogast
[@BarryThePenguin]: https://github.com/BarryThePenguin
[@be5invis]: https://github.com/be5invis
[@beatrizrezener]: https://github.com/beatrizrezener
[@benmosher]: https://github.com/benmosher
Expand Down
9 changes: 8 additions & 1 deletion src/rules/no-import-module-exports.js
Expand Up @@ -19,6 +19,11 @@ function findScope(context, identifier) {
return scopeManager && scopeManager.scopes.slice().reverse().find((scope) => scope.variables.some(variable => variable.identifiers.some((node) => node.name === identifier)));
}

function findDefinition(objectScope, identifier) {
const variable = objectScope.variables.find(variable => variable.name === identifier);
return variable.defs.find(def => def.name.name === identifier);
}

module.exports = {
meta: {
type: 'problem',
Expand Down Expand Up @@ -50,10 +55,12 @@ module.exports = {
const isIdentifier = node.object.type === 'Identifier';
const hasKeywords = (/^(module|exports)$/).test(node.object.name);
const objectScope = hasKeywords && findScope(context, node.object.name);
const variableDefinition = objectScope && findDefinition(objectScope, node.object.name);
const isImportBinding = variableDefinition && variableDefinition.type === 'ImportBinding';
const hasCJSExportReference = hasKeywords && (!objectScope || objectScope.type === 'module');
const isException = !!options.exceptions && options.exceptions.some(glob => minimatch(fileName, glob));

if (isIdentifier && hasCJSExportReference && !isEntryPoint && !isException) {
if (isIdentifier && hasCJSExportReference && !isEntryPoint && !isException && !isImportBinding) {
importDeclarations.forEach(importDeclaration => {
context.report({
node: importDeclaration,
Expand Down
6 changes: 6 additions & 0 deletions tests/src/rules/no-import-module-exports.js
Expand Up @@ -40,6 +40,12 @@ ruleTester.run('no-import-module-exports', rule, {
exports.foo = bar
`,
}),
test({
code: `
import { module } from 'qunit'
module.skip('A test', function () {})
`,
}),
test({
code: `
import foo from 'path';
Expand Down

0 comments on commit cd6648d

Please sign in to comment.