Skip to content

Commit

Permalink
Fix: ignore unmergable imports when checking no-duplicate-imports (fixes
Browse files Browse the repository at this point in the history
 eslint#13180) & Fix: Ignore re-export all in no-duplicate-imports (fixes eslint#12760)
  • Loading branch information
boutahlilsoufiane committed May 26, 2021
1 parent 7bb7a50 commit 9d3fd4d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 171 deletions.
21 changes: 13 additions & 8 deletions lib/rules/no-duplicate-imports.js
Expand Up @@ -4,6 +4,16 @@
*/
"use strict";

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

const NAMED_TYPES = ["ImportSpecifier", "ExportSpecifier"];
const NAMESPACE_TYPES = [
"ImportNamespaceSpecifier",
"ExportNamespaceSpecifier"
];

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -15,12 +25,7 @@
* @returns {boolean} True if (import|export) type is belong to (ImportSpecifier|ExportSpecifier) or (ImportNamespaceSpecifier|ExportNamespaceSpecifier) and false if it doesn't.
*/
function isImportExportSpecifier(importExportType, type) {
const namedTypes = ["ImportSpecifier", "ExportSpecifier"];
const namespacesTypes = [
"ImportNamespaceSpecifier",
"ExportNamespaceSpecifier"
];
const arrayToCheck = type === "named" ? namedTypes : namespacesTypes;
const arrayToCheck = type === "named" ? NAMED_TYPES : NAMESPACE_TYPES;

return arrayToCheck.includes(importExportType);
}
Expand Down Expand Up @@ -157,15 +162,15 @@ function checkAndReport(
}
if (includeExports) {
if (shouldReportImportExport(node, exportNodes)) {
messagesIds.push("exportAs");
messagesIds.push("importAs");
}
}
} else if (declarationType === "export") {
if (shouldReportImportExport(node, exportNodes)) {
messagesIds.push("export");
}
if (shouldReportImportExport(node, importNodes)) {
messagesIds.push("importAs");
messagesIds.push("exportAs");
}
}
messagesIds.forEach(messageId =>
Expand Down
203 changes: 40 additions & 163 deletions tests/lib/rules/no-duplicate-imports.js
Expand Up @@ -16,222 +16,99 @@ const rule = require("../../../lib/rules/no-duplicate-imports"),
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester({
parserOptions: { ecmaVersion: 12, sourceType: "module" }
});
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, sourceType: "module" } });

ruleTester.run("no-duplicate-imports", rule, {
valid: [
'import os from "os";\nimport fs from "fs";',
'import { merge } from "lodash-es";',
'import _, { merge } from "lodash-es";',
'import * as Foobar from "async";',
'import "foo"',
'import os from "os";\nexport { something } from "os";',
'import { something } from "os";\nimport * as foobar from "os";',
'import foo, * as bar from "mod";\nimport { baz } from "mod";',
{
code: 'import os from "os";\nexport { hello } from "hello";',
"import os from \"os\";\nimport fs from \"fs\";",
"import { merge } from \"lodash-es\";",
"import _, { merge } from \"lodash-es\";",
"import * as Foobar from \"async\";",
"import \"foo\"",
"import os from \"os\";\nexport { something } from \"os\";",
"import * as bar from \"os\";\nimport { baz } from \"os\";",
"import foo, * as bar from \"os\";\nimport { baz } from \"os\";",
{
code: "import os from \"os\";\nexport { hello } from \"hello\";",
options: [{ includeExports: true }]
},
{
code: 'import os from "os";\nexport { hello as hi } from "hello";',
code: "import os from \"os\";\nexport * from \"hello\";",
options: [{ includeExports: true }]
},
{
code: 'import os from "os";\nexport default function(){};',
code: "import os from \"os\";\nexport { hello as hi } from \"hello\";",
options: [{ includeExports: true }]
},
{
code:
'import { merge } from "lodash-es";\nexport { merge as lodashMerge }',
code: "import os from \"os\";\nexport default function(){};",
options: [{ includeExports: true }]
},
{
code: "import * as foo from 'os';\nexport {too} from 'os';",
code: "import { merge } from \"lodash-es\";\nexport { merge as lodashMerge }",
options: [{ includeExports: true }]
},
{
code: "import os from 'os';\n export * from 'os';",
code: "import os from \"os\";\nexport * from \"os\";",
options: [{ includeExports: true }]
},
{
code: "import * as ns from 'os';\nexport * from 'os';",
options: [{ includeExports: true }]
},
{
code: "export * from 'os';\nexport { a } from 'os';",
options: [{ includeExports: true }]
},
{
code: "export { a as b } from 'os';\nexport * from 'os';",
code: "export { something } from \"os\";\nexport * from \"os\";",
options: [{ includeExports: true }]
}
],
invalid: [
{
code: 'import "fs";\nimport "fs"',
errors: [
{
messageId: "import",
data: { module: "fs" },
type: "ImportDeclaration"
}
]
},
{
code:
'import { merge } from "lodash-es";\nimport { find } from "lodash-es";',
errors: [
{
messageId: "import",
data: { module: "lodash-es" },
type: "ImportDeclaration"
}
]
},
{
code:
'import os from "os";\nimport { something } from "os";\nimport * as foobar from "os";',
errors: [
{
messageId: "import",
data: { module: "os" },
type: "ImportDeclaration"
},
{
messageId: "import",
data: { module: "os" },
type: "ImportDeclaration"
}
]
},
{
code:
'import os from "os";\nimport * as foobar1 from "os";\nimport * as foobar2 from "os";',
errors: [
{
messageId: "import",
data: { module: "os" },
type: "ImportDeclaration"
},
{
messageId: "import",
data: { module: "os" },
type: "ImportDeclaration"
}
]
code: "import \"fs\";\nimport \"fs\"",
errors: [{ messageId: "import", data: { module: "fs" }, type: "ImportDeclaration" }]
},
{
code:
'import { merge } from "lodash-es";\nimport _ from "lodash-es";',
errors: [
{
messageId: "import",
data: { module: "lodash-es" },
type: "ImportDeclaration"
}
]
code: "import { merge } from \"lodash-es\";\nimport { find } from \"lodash-es\";",
errors: [{ messageId: "import", data: { module: "lodash-es" }, type: "ImportDeclaration" }]
},
{
code:
'import foo, { merge } from "module";\nimport { baz } from "module";',
errors: [
{
messageId: "import",
data: { module: "module" },
type: "ImportDeclaration"
}
]
code: "import { merge } from \"lodash-es\";\nimport _ from \"lodash-es\";",
errors: [{ messageId: "import", data: { module: "lodash-es" }, type: "ImportDeclaration" }]
},
{
code:
'import * as modns from "lodash-es";\nimport { merge } from "lodash-es";\nimport { baz } from "lodash-es";',
code: "import os from \"os\";\nimport { something } from \"os\";\nimport * as foobar from \"os\";",
errors: [
{
messageId: "import",
data: { module: "lodash-es" },
type: "ImportDeclaration"
}
{ messageId: "import", data: { module: "os" }, type: "ImportDeclaration" },
{ messageId: "import", data: { module: "os" }, type: "ImportDeclaration" }
]
},
{
code: 'export { os } from "os";\nexport { something } from "os";',
options: [{ includeExports: true }],
errors: [
{
messageId: "export",
data: { module: "os" },
type: "ExportNamedDeclaration"
}
]
code: "import * as modns from \"lodash-es\";\nimport { merge } from \"lodash-es\";\nimport { baz } from \"lodash-es\";",
errors: [{ messageId: "import", data: { module: "lodash-es" }, type: "ImportDeclaration" }]
},
{
code:
'import os from "os"; export { os as foobar } from "os";\nexport { something } from "os";',
code: "export { os } from \"os\";\nexport { something } from \"os\";",
options: [{ includeExports: true }],
errors: [
{
messageId: "importAs",
data: { module: "os" },
type: "ExportNamedDeclaration"
},
{
messageId: "export",
data: { module: "os" },
type: "ExportNamedDeclaration"
},
{
messageId: "importAs",
data: { module: "os" },
type: "ExportNamedDeclaration"
}
]
errors: [{ messageId: "export", data: { module: "os" }, type: "ExportNamedDeclaration" }]
},
{
code: 'import os from "os";\nexport { something } from "os";',
code: "import os from \"os\";\nexport { os as foobar } from \"os\";\nexport { something } from \"os\";",
options: [{ includeExports: true }],
errors: [
{
messageId: "importAs",
data: { module: "os" },
type: "ExportNamedDeclaration"
}
{ messageId: "exportAs", data: { module: "os" }, type: "ExportNamedDeclaration" },
{ messageId: "export", data: { module: "os" }, type: "ExportNamedDeclaration" },
{ messageId: "exportAs", data: { module: "os" }, type: "ExportNamedDeclaration" }
]
},
{
code: "export * from 'os';\nexport * from 'os';",
code: "import os from \"os\";\nexport { something } from \"os\";",
options: [{ includeExports: true }],
errors: [
{
messageId: "export",
data: { module: "os" },
type: "ExportAllDeclaration"
}
]
errors: [{ messageId: "exportAs", data: { module: "os" }, type: "ExportNamedDeclaration" }]
},
{
code: "import 'os';\nexport * from 'os';",
code: "export * from \"os\";\nexport * from \"os\";",
options: [{ includeExports: true }],
errors: [
{
messageId: "importAs",
data: { module: "os" },
type: "ExportAllDeclaration"
}
]
errors: [{ messageId: "export", data: { module: "os" }, type: "ExportAllDeclaration" }]
},
{
code:
"import * as modns from 'mod';\nexport * as modns from 'mod';",
code: "import \"os\";\nexport * from \"os\";",
options: [{ includeExports: true }],
errors: [
{
messageId: "importAs",
data: { module: "mod" },
type: "ExportAllDeclaration"
}
]
errors: [{ messageId: "exportAs", data: { module: "os" }, type: "ExportAllDeclaration" }]
}
]
});

0 comments on commit 9d3fd4d

Please sign in to comment.