Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] no-import-module-exports: avoid a false positive for import variables #2315

Merged
merged 1 commit into from Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
8 changes: 7 additions & 1 deletion 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' },
Expand Down Expand Up @@ -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';
Expand Down