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 9, 2021
1 parent 6e494a1 commit d30fc24
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 82 deletions.
58 changes: 29 additions & 29 deletions lib/rules/no-duplicate-imports.js
Expand Up @@ -8,12 +8,12 @@
// Rule Definition
//------------------------------------------------------------------------------

const EXPORT_ALL_DECLARATION = 'ExportAllDeclaration'
const IMPORT_NAME_SPACE_SPECIFIER = 'ImportNamespaceSpecifier';
const IMPORT_SPECIFIER = 'ImportSpecifier';
const EXPORT_ALL_DECLARATION = "ExportAllDeclaration";
const IMPORT_NAME_SPACE_SPECIFIER = "ImportNamespaceSpecifier";
const IMPORT_SPECIFIER = "ImportSpecifier";
const CONTRADICTORY_IMPORT_TYPES = [
IMPORT_NAME_SPACE_SPECIFIER,
IMPORT_SPECIFIER,
IMPORT_SPECIFIER
];

/**
Expand All @@ -28,17 +28,17 @@ function getImportType(node) {
node.specifiers[0] &&
node.specifiers[0].type
) {
const index = node.specifiers.findIndex((specifier) =>
const index = node.specifiers.findIndex(specifier =>
CONTRADICTORY_IMPORT_TYPES.includes(
specifier.type
)
);
));

if (index > -1) {
return node.specifiers[index].type;
} else {
return node.specifiers[0].type;
}
} else if (node && node.type) {
return node.specifiers[0].type;
}
if (node && node.type) {
return node.type;
}
return "";
Expand Down Expand Up @@ -67,13 +67,11 @@ function getValue(node) {
function shouldReportContradictoryImportType(importTypes, importType) {
if (importTypes.indexOf(importType) > -1) {
return true;
} else {
return (
importTypes.findIndex((importTypeItem) => {
return CONTRADICTORY_IMPORT_TYPES.includes(importTypeItem);
}) === -1
);
}
return (
importTypes.findIndex(importTypeItem => CONTRADICTORY_IMPORT_TYPES.includes(importTypeItem)) === -1
);

}

/**
Expand All @@ -100,6 +98,7 @@ function checkAndReport(
ExportAllDeclarationsInFile
) {
let isDuplicate = false;

if (type === EXPORT_ALL_DECLARATION) {
if (ExportAllDeclarationsInFile.indexOf(value) !== -1) {
isDuplicate = true;
Expand All @@ -118,8 +117,8 @@ function checkAndReport(
node,
messageId,
data: {
module: value,
},
module: value
}
});
}
}
Expand Down Expand Up @@ -148,7 +147,7 @@ function handleImports(
modulesWithImportTypes,
ExportAllDeclarationsInFile
) {
return function (node) {
return function(node) {
const value = getValue(node);
const type = getImportType(node);

Expand Down Expand Up @@ -205,9 +204,10 @@ function handleExports(
modulesWithImportTypes,
ExportAllDeclarationsInFile
) {
return function (node) {
return function(node) {
const value = getValue(node);
const type = getImportType(node);

if (value) {
checkAndReport(
context,
Expand Down Expand Up @@ -246,7 +246,7 @@ module.exports = {
description: "disallow duplicate module imports",
category: "ECMAScript 6",
recommended: false,
url: "https://eslint.org/docs/rules/no-duplicate-imports",
url: "https://eslint.org/docs/rules/no-duplicate-imports"
},

schema: [
Expand All @@ -255,18 +255,18 @@ module.exports = {
properties: {
includeExports: {
type: "boolean",
default: false,
},
default: false
}
},
additionalProperties: false,
},
additionalProperties: false
}
],
messages: {
import: "'{{module}}' import is duplicated.",
importAs: "'{{module}}' import is duplicated as export.",
export: "'{{module}}' export is duplicated.",
exportAs: "'{{module}}' export is duplicated as import.",
},
exportAs: "'{{module}}' export is duplicated as import."
}
},

create(context) {
Expand All @@ -283,7 +283,7 @@ module.exports = {
exportsInFile,
modulesWithImportTypes,
ExportAllDeclarationsInFile
),
)
};

if (includeExports) {
Expand All @@ -303,5 +303,5 @@ module.exports = {
);
}
return handlers;
},
}
};
107 changes: 54 additions & 53 deletions tests/lib/rules/no-duplicate-imports.js
Expand Up @@ -17,7 +17,7 @@ const rule = require("../../../lib/rules/no-duplicate-imports"),
//------------------------------------------------------------------------------

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

ruleTester.run("no-duplicate-imports", rule, {
Expand All @@ -32,46 +32,47 @@ ruleTester.run("no-duplicate-imports", rule, {
'import foo, * as bar from "mod";\nimport { baz } from "mod";',
{
code: 'import os from "os";\nexport { hello } from "hello";',
options: [{ includeExports: true }],
options: [{ includeExports: true }]
},
{
code: 'import os from "os";\nexport * from "hello";',
options: [{ includeExports: true }],
options: [{ includeExports: true }]
},
{
code: 'import os from "os";\nexport { hello as hi } from "hello";',
options: [{ includeExports: true }],
options: [{ includeExports: true }]
},
{
code: 'import os from "os";\nexport default function(){};',
options: [{ includeExports: true }],
options: [{ includeExports: true }]
},
{
code:
'import { merge } from "lodash-es";\nexport { merge as lodashMerge }',
options: [{ includeExports: true }],
options: [{ includeExports: true }]
},

// ignore `export * from` declarations, they cannot be merged with any other import/export declarations
{
code: "import os from 'os'; export * from 'os';",
options: [{ includeExports: true }],
options: [{ includeExports: true }]
},
{
code: "export * from 'os'; import { a } from 'os';",
options: [{ includeExports: true }],
options: [{ includeExports: true }]
},
{
code: "import * as ns from 'os'; export * from 'os';",
options: [{ includeExports: true }],
options: [{ includeExports: true }]
},
{
code: "export * from 'os'; export { a } from 'os';",
options: [{ includeExports: true }],
options: [{ includeExports: true }]
},
{
code: "export { a as b } from 'os'; export * from 'os';",
options: [{ includeExports: true }],
},
options: [{ includeExports: true }]
}
],
invalid: [
{
Expand All @@ -80,9 +81,9 @@ ruleTester.run("no-duplicate-imports", rule, {
{
messageId: "import",
data: { module: "fs" },
type: "ImportDeclaration",
},
],
type: "ImportDeclaration"
}
]
},
{
code:
Expand All @@ -91,9 +92,9 @@ ruleTester.run("no-duplicate-imports", rule, {
{
messageId: "import",
data: { module: "lodash-es" },
type: "ImportDeclaration",
},
],
type: "ImportDeclaration"
}
]
},
{
code:
Expand All @@ -102,9 +103,9 @@ ruleTester.run("no-duplicate-imports", rule, {
{
messageId: "import",
data: { module: "os" },
type: "ImportDeclaration",
},
],
type: "ImportDeclaration"
}
]
},
{
code:
Expand All @@ -113,9 +114,9 @@ ruleTester.run("no-duplicate-imports", rule, {
{
messageId: "import",
data: { module: "os" },
type: "ImportDeclaration",
},
],
type: "ImportDeclaration"
}
]
},
{
code:
Expand All @@ -124,14 +125,14 @@ ruleTester.run("no-duplicate-imports", rule, {
{
messageId: "import",
data: { module: "os" },
type: "ImportDeclaration",
type: "ImportDeclaration"
},
{
messageId: "import",
data: { module: "os" },
type: "ImportDeclaration",
},
],
type: "ImportDeclaration"
}
]
},
{
code:
Expand All @@ -140,9 +141,9 @@ ruleTester.run("no-duplicate-imports", rule, {
{
messageId: "import",
data: { module: "lodash-es" },
type: "ImportDeclaration",
},
],
type: "ImportDeclaration"
}
]
},
{
code:
Expand All @@ -151,9 +152,9 @@ ruleTester.run("no-duplicate-imports", rule, {
{
messageId: "import",
data: { module: "module" },
type: "ImportDeclaration",
},
],
type: "ImportDeclaration"
}
]
},
{
code:
Expand All @@ -162,9 +163,9 @@ ruleTester.run("no-duplicate-imports", rule, {
{
messageId: "import",
data: { module: "lodash-es" },
type: "ImportDeclaration",
},
],
type: "ImportDeclaration"
}
]
},
{
code: 'export { os } from "os";\nexport { something } from "os";',
Expand All @@ -173,9 +174,9 @@ ruleTester.run("no-duplicate-imports", rule, {
{
messageId: "export",
data: { module: "os" },
type: "ExportNamedDeclaration",
},
],
type: "ExportNamedDeclaration"
}
]
},
{
code:
Expand All @@ -185,19 +186,19 @@ ruleTester.run("no-duplicate-imports", rule, {
{
messageId: "exportAs",
data: { module: "os" },
type: "ExportNamedDeclaration",
type: "ExportNamedDeclaration"
},
{
messageId: "export",
data: { module: "os" },
type: "ExportNamedDeclaration",
type: "ExportNamedDeclaration"
},
{
messageId: "exportAs",
data: { module: "os" },
type: "ExportNamedDeclaration",
},
],
type: "ExportNamedDeclaration"
}
]
},
{
code: 'import os from "os";\nexport { something } from "os";',
Expand All @@ -206,9 +207,9 @@ ruleTester.run("no-duplicate-imports", rule, {
{
messageId: "exportAs",
data: { module: "os" },
type: "ExportNamedDeclaration",
},
],
type: "ExportNamedDeclaration"
}
]
},
{
code: "export * from 'os'; export * from 'os';",
Expand All @@ -217,9 +218,9 @@ ruleTester.run("no-duplicate-imports", rule, {
{
messageId: "export",
data: { module: "os" },
type: "ExportAllDeclaration",
},
],
},
],
type: "ExportAllDeclaration"
}
]
}
]
});

0 comments on commit d30fc24

Please sign in to comment.