From fe5d6eaaf9ea0c8f9b3d26b1e9c2a66d64fb0949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Veyret?= Date: Wed, 15 Jan 2020 17:24:02 +0100 Subject: [PATCH] no-duplicates: allow duplicate if one is namespace and other not It is a syntax error to put both namespace and non namespace import on the same line, so allow it. Fix issue #1538 --- src/rules/no-duplicates.js | 5 ++++- tests/src/rules/no-duplicates.js | 27 +++++++++++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/rules/no-duplicates.js b/src/rules/no-duplicates.js index 1334a12582..69e5a23a02 100644 --- a/src/rules/no-duplicates.js +++ b/src/rules/no-duplicates.js @@ -257,12 +257,14 @@ module.exports = { }) : defaultResolver const imported = new Map() + const nsImported = new Map() const typesImported = new Map() return { 'ImportDeclaration': function (n) { // resolved path will cover aliased duplicates const resolvedPath = resolver(n.source.value) - const importMap = n.importKind === 'type' ? typesImported : imported + const importMap = n.importKind === 'type' ? typesImported : + (hasNamespace(n) ? nsImported : imported) if (importMap.has(resolvedPath)) { importMap.get(resolvedPath).push(n) @@ -273,6 +275,7 @@ module.exports = { 'Program:exit': function () { checkImports(imported, context) + checkImports(nsImported, context) checkImports(typesImported, context) }, } diff --git a/tests/src/rules/no-duplicates.js b/tests/src/rules/no-duplicates.js index a4c41f677a..468c7ab982 100644 --- a/tests/src/rules/no-duplicates.js +++ b/tests/src/rules/no-duplicates.js @@ -31,12 +31,20 @@ ruleTester.run('no-duplicates', rule, { code: "import x from './bar?optionX'; import y from './bar?optionY';", options: [{'considerQueryString': true}], settings: { 'import/resolver': 'webpack' }, - }), + }), test({ code: "import x from './foo'; import y from './bar';", options: [{'considerQueryString': true}], settings: { 'import/resolver': 'webpack' }, - }), + }), + + // #1538: It is impossible to import namespace and other in one line, so allow this. + test({ + code: "import * as ns from './foo'; import {y} from './foo'", + }), + test({ + code: "import {y} from './foo'; import * as ns from './foo'", + }), ], invalid: [ test({ @@ -179,9 +187,16 @@ ruleTester.run('no-duplicates', rule, { }), test({ - code: "import * as ns from './foo'; import {y} from './foo'", - // Autofix bail because first import is a namespace import. - output: "import * as ns from './foo'; import {y} from './foo'", + code: "import * as ns1 from './foo'; import * as ns2 from './foo'", + // Autofix bail because cannot merge namespace imports. + output: "import * as ns1 from './foo'; import * as ns2 from './foo'", + errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'], + }), + + test({ + code: "import * as ns from './foo'; import {x} from './foo'; import {y} from './foo'", + // Autofix could merge some imports, but not the namespace import. + output: "import * as ns from './foo'; import {x,y} from './foo'; ", errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'], }), @@ -189,7 +204,7 @@ ruleTester.run('no-duplicates', rule, { code: "import {x} from './foo'; import * as ns from './foo'; import {y} from './foo'; import './foo'", // Autofix could merge some imports, but not the namespace import. output: "import {x,y} from './foo'; import * as ns from './foo'; ", - errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'], + errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'], }), test({