From c2f003a801f454abe0ff58f3bc5ea0b5360c036a Mon Sep 17 00:00:00 2001 From: Jonathan Haines Date: Tue, 30 Nov 2021 20:50:08 +1100 Subject: [PATCH] [Fix] `no-import-module-exports`: avoid a false positive for import variables --- CHANGELOG.md | 3 +++ src/rules/no-import-module-exports.js | 9 ++++++++- tests/src/rules/no-import-module-exports.js | 8 +++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e8e88924..83b44e64a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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]) @@ -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 @@ -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 diff --git a/src/rules/no-import-module-exports.js b/src/rules/no-import-module-exports.js index d40bae88c..5a91acd07 100644 --- a/src/rules/no-import-module-exports.js +++ b/src/rules/no-import-module-exports.js @@ -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', @@ -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, diff --git a/tests/src/rules/no-import-module-exports.js b/tests/src/rules/no-import-module-exports.js index a40eb7e27..81faceba9 100644 --- a/tests/src/rules/no-import-module-exports.js +++ b/tests/src/rules/no-import-module-exports.js @@ -1,7 +1,7 @@ import path from 'path'; import { RuleTester } from 'eslint'; -import { test, testVersion } from '../utils'; +import { eslintVersionSatisfies, test, testVersion } from '../utils'; const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, sourceType: 'module' }, @@ -40,6 +40,12 @@ ruleTester.run('no-import-module-exports', rule, { exports.foo = bar `, }), + eslintVersionSatisfies('>= 4') ? test({ + code: ` + import { module } from 'qunit' + module.skip('A test', function () {}) + `, + }) : [], test({ code: ` import foo from 'path';