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
 #13180) & Fix: Ignore re-export all in no-duplicate-imports (fixes #12760)
  • Loading branch information
boutahlilsoufiane committed May 23, 2021
1 parent 1932d2c commit 7bb7a50
Showing 1 changed file with 63 additions and 74 deletions.
137 changes: 63 additions & 74 deletions lib/rules/no-duplicate-imports.js
Expand Up @@ -15,13 +15,12 @@
* @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 specifiersTypes = ["ImportSpecifier", "ExportSpecifier"];
const namedTypes = ["ImportSpecifier", "ExportSpecifier"];
const namespacesTypes = [
"ImportNamespaceSpecifier",
"ExportNamespaceSpecifier"
];
const arrayToCheck =
type === "specifier" ? specifiersTypes : namespacesTypes;
const arrayToCheck = type === "named" ? namedTypes : namespacesTypes;

return arrayToCheck.includes(importExportType);
}
Expand All @@ -32,12 +31,12 @@ function isImportExportSpecifier(importExportType, type) {
* @returns {string} The type of the (import|export).
*/
function getImportExportType(node) {
if (node && node.specifiers && node.specifiers.length > 0) {
if (node.specifiers && node.specifiers.length > 0) {
const nodeSpecifiers = node.specifiers;
const index = nodeSpecifiers.findIndex(
({ type }) =>
isImportExportSpecifier(type, "specifier") ||
isImportExportSpecifier(type, "namespace")
isImportExportSpecifier(type, "named") ||
isImportExportSpecifier(type, "namespace")
);
const i = index > -1 ? index : 0;

Expand All @@ -49,10 +48,7 @@ function getImportExportType(node) {
}
return "ExportAll";
}
if (node.type === "ImportDeclaration") {
return "SideEffectImport";
}
return node.type;
return "SideEffectImport";
}

/**
Expand All @@ -67,19 +63,19 @@ function isImportExportCanBeMerged(node1, node2) {

if (
(importExportType1 === "ExportAll" &&
importExportType2 !== "ExportAll" &&
importExportType2 !== "SideEffectImport") ||
(importExportType1 !== "ExportAll" &&
importExportType1 !== "SideEffectImport" &&
importExportType2 === "ExportAll")
importExportType2 !== "ExportAll" &&
importExportType2 !== "SideEffectImport") ||
(importExportType1 !== "ExportAll" &&
importExportType1 !== "SideEffectImport" &&
importExportType2 === "ExportAll")
) {
return false;
}
if (
(isImportExportSpecifier(importExportType1, "namespace") &&
isImportExportSpecifier(importExportType2, "specifier")) ||
(isImportExportSpecifier(importExportType2, "namespace") &&
isImportExportSpecifier(importExportType1, "specifier"))
isImportExportSpecifier(importExportType2, "named")) ||
(isImportExportSpecifier(importExportType2, "namespace") &&
isImportExportSpecifier(importExportType1, "named"))
) {
return false;
}
Expand All @@ -93,15 +89,13 @@ function isImportExportCanBeMerged(node1, node2) {
* @returns {boolean} True if the (import/export) should be reported.
*/
function shouldReportImportExport(node, previousNodes) {
if (previousNodes) {
let i = 0;
let i = 0;

while (i < previousNodes.length) {
if (isImportExportCanBeMerged(node, previousNodes[i])) {
return true;
}
i++;
while (i < previousNodes.length) {
if (isImportExportCanBeMerged(node, previousNodes[i])) {
return true;
}
i++;
}
return false;
}
Expand All @@ -112,13 +106,10 @@ function shouldReportImportExport(node, previousNodes) {
* @param {string} type Declaration type.
* @returns {[ASTNode]} An array contains only nodes with declarations types equal to type, if the nodes are null we return [].
*/
function getNodesByDeclarationTypeAndFormat(nodes, type) {
if (nodes) {
return nodes
.filter(({ declarationType }) => declarationType === type)
.map(({ node }) => node);
}
return [];
function getNodesByDeclarationType(nodes, type) {
return nodes
.filter(({ declarationType }) => declarationType === type)
.map(({ node }) => node);
}

/**
Expand All @@ -137,7 +128,7 @@ function getModule(node) {
* Checks if the (import|export) can be merged with at least one import and one export, and reports if so.
* @param {RuleContext} context The ESLint rule context object.
* @param {ASTNode} node A node to get.
* @param {{string: [{node: ASTNode, declarationType: string}]}} modules An object contains as a key a module name and as value an array contains objects, each object contains a node and a declaration type.
* @param {Map} modules A Map object contains as a key a module name and as value an array contains objects, each object contains a node and a declaration type.
* @param {string} declarationType A declaration type can be an import or export.
* @param {boolean} includeExports Whether or not to check for exports in addition to imports.
* @returns {void} No return value.
Expand All @@ -150,46 +141,42 @@ function checkAndReport(
includeExports
) {
const module = getModule(node);
const previousNodes = modules[module];
const messagesIds = [];
const importNodes = getNodesByDeclarationTypeAndFormat(
previousNodes,
"import"
);
let exportNodes;

if (includeExports) {
exportNodes = getNodesByDeclarationTypeAndFormat(
previousNodes,
"export"
);
}
if (declarationType === "import") {
if (shouldReportImportExport(node, importNodes)) {
messagesIds.push("import");
}
if (modules.has(module)) {
const previousNodes = modules.get(module);
const messagesIds = [];
const importNodes = getNodesByDeclarationType(previousNodes, "import");
let exportNodes;

if (includeExports) {
exportNodes = getNodesByDeclarationType(previousNodes, "export");
}
if (declarationType === "import") {
if (shouldReportImportExport(node, importNodes)) {
messagesIds.push("import");
}
if (includeExports) {
if (shouldReportImportExport(node, exportNodes)) {
messagesIds.push("exportAs");
}
}
} else if (declarationType === "export") {
if (shouldReportImportExport(node, exportNodes)) {
messagesIds.push("exportAs");
messagesIds.push("export");
}
if (shouldReportImportExport(node, importNodes)) {
messagesIds.push("importAs");
}
}
} else if (declarationType === "export") {
if (shouldReportImportExport(node, exportNodes)) {
messagesIds.push("export");
}
if (shouldReportImportExport(node, importNodes)) {
messagesIds.push("importAs");
}
messagesIds.forEach(messageId =>
context.report({
node,
messageId,
data: {
module
}
}));
}

messagesIds.forEach(messageId =>
context.report({
node,
messageId,
data: {
module
}
}));
}

/**
Expand All @@ -200,7 +187,7 @@ function checkAndReport(
/**
* Returns a function handling the (imports|exports) of a given file
* @param {RuleContext} context The ESLint rule context object.
* @param {{string: [{node: ASTNode, declarationType: string}]}} modules An object contains as a key a module name and as value an array contains objects, each object contains a node and a declaration type.
* @param {Map} modules A Map object contains as a key a module name and as value an array contains objects, each object contains a node and a declaration type.
* @param {string} declarationType A declaration type can be an import or export.
* @param {boolean} includeExports Whether or not to check for exports in addition to imports.
* @returns {nodeCallback} A function passed to ESLint to handle the statement.
Expand All @@ -222,14 +209,15 @@ function handleImportsExports(
declarationType,
includeExports
);
const previousNodes = modules[module];
const currentNode = { node, declarationType };
let nodes = [currentNode];

if (previousNodes) {
modules[module] = [...previousNodes, currentNode];
} else {
modules[module] = [currentNode];
if (modules.has(module)) {
const previousNodes = modules.get(module);

nodes = [...previousNodes, currentNode];
}
modules.set(module, nodes);
}
};
}
Expand Down Expand Up @@ -257,6 +245,7 @@ module.exports = {
additionalProperties: false
}
],

messages: {
import: "'{{module}}' import is duplicated.",
importAs: "'{{module}}' import is duplicated as export.",
Expand All @@ -267,7 +256,7 @@ module.exports = {

create(context) {
const includeExports = (context.options[0] || {}).includeExports,
modules = {};
modules = new Map();
const handlers = {
ImportDeclaration: handleImportsExports(
context,
Expand Down

0 comments on commit 7bb7a50

Please sign in to comment.