diff --git a/docs/rules/no-duplicate-imports.md b/docs/rules/no-duplicate-imports.md index 683f31584a1..627057fb929 100644 --- a/docs/rules/no-duplicate-imports.md +++ b/docs/rules/no-duplicate-imports.md @@ -12,7 +12,9 @@ import { find } from 'module'; ## Rule Details -This rule requires that all imports from a single module exists in a single `import` statement. +an import that can be merged with another is a duplicate of that other + +This rule requires that all imports from a single module that can be merged exists in a single `import` statement. Example of **incorrect** code for this rule: @@ -33,6 +35,16 @@ import { merge, find } from 'module'; import something from 'another-module'; ``` +Example of **correct** code for this rule (namespace imports can't be merged): + +```js +/*eslint no-duplicate-imports: "error"*/ + +import * as Namespace from 'module'; +import { merge } from 'module'; +import something from 'another-module'; +``` + ## Options This rule takes one optional argument, an object with a single key, `includeExports` which is a `boolean`. It defaults to `false`. diff --git a/lib/rules/no-duplicate-imports.js b/lib/rules/no-duplicate-imports.js index 7218dc64add..7ef3b37b978 100644 --- a/lib/rules/no-duplicate-imports.js +++ b/lib/rules/no-duplicate-imports.js @@ -21,6 +21,20 @@ function getValue(node) { return ""; } +/** + * Checks if the import's type is a namespace import. + * @param {ASTNode} node A node to get. + * + * @returns {boolean} Whether or not the import's type is "ImportNamespaceSpecifier". + */ +function isNamespaceImport(node) { + return ( + node.specifiers && + node.specifiers[0] && + node.specifiers[0].type === "ImportNamespaceSpecifier" + ); +} + /** * Checks if the name of the import or export exists in the given array, and reports if so. * @param {RuleContext} context The ESLint rule context object. @@ -61,7 +75,7 @@ function handleImports(context, includeExports, importsInFile, exportsInFile) { return function(node) { const value = getValue(node); - if (value) { + if (value && !isNamespaceImport(node)) { checkAndReport(context, node, value, importsInFile, "import"); if (includeExports) { diff --git a/tests/lib/rules/no-duplicate-imports.js b/tests/lib/rules/no-duplicate-imports.js index 42d35c85751..8b41c2f556c 100644 --- a/tests/lib/rules/no-duplicate-imports.js +++ b/tests/lib/rules/no-duplicate-imports.js @@ -20,6 +20,7 @@ const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, sourceType: ruleTester.run("no-duplicate-imports", rule, { valid: [ + "import * as Lodash from \"lodash-es\";import { merge } from \"lodash-es\";;", "import os from \"os\";\nimport fs from \"fs\";", "import { merge } from \"lodash-es\";", "import _, { merge } from \"lodash-es\";",