123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- "use strict";
- const NAMED_TYPES = ["ImportSpecifier", "ExportSpecifier"];
- const NAMESPACE_TYPES = [
- "ImportNamespaceSpecifier",
- "ExportNamespaceSpecifier"
- ];
- function isImportExportSpecifier(importExportType, type) {
- const arrayToCheck = type === "named" ? NAMED_TYPES : NAMESPACE_TYPES;
- return arrayToCheck.includes(importExportType);
- }
- function getImportExportType(node) {
- if (node.specifiers && node.specifiers.length > 0) {
- const nodeSpecifiers = node.specifiers;
- const index = nodeSpecifiers.findIndex(
- ({ type }) =>
- isImportExportSpecifier(type, "named") ||
- isImportExportSpecifier(type, "namespace")
- );
- const i = index > -1 ? index : 0;
- return nodeSpecifiers[i].type;
- }
- if (node.type === "ExportAllDeclaration") {
- if (node.exported) {
- return "ExportNamespaceSpecifier";
- }
- return "ExportAll";
- }
- return "SideEffectImport";
- }
- function isImportExportCanBeMerged(node1, node2) {
- const importExportType1 = getImportExportType(node1);
- const importExportType2 = getImportExportType(node2);
- if (
- (importExportType1 === "ExportAll" &&
- importExportType2 !== "ExportAll" &&
- importExportType2 !== "SideEffectImport") ||
- (importExportType1 !== "ExportAll" &&
- importExportType1 !== "SideEffectImport" &&
- importExportType2 === "ExportAll")
- ) {
- return false;
- }
- if (
- (isImportExportSpecifier(importExportType1, "namespace") &&
- isImportExportSpecifier(importExportType2, "named")) ||
- (isImportExportSpecifier(importExportType2, "namespace") &&
- isImportExportSpecifier(importExportType1, "named"))
- ) {
- return false;
- }
- return true;
- }
- function shouldReportImportExport(node, previousNodes) {
- let i = 0;
- while (i < previousNodes.length) {
- if (isImportExportCanBeMerged(node, previousNodes[i])) {
- return true;
- }
- i++;
- }
- return false;
- }
- function getNodesByDeclarationType(nodes, type) {
- return nodes
- .filter(({ declarationType }) => declarationType === type)
- .map(({ node }) => node);
- }
- function getModule(node) {
- if (node && node.source && node.source.value) {
- return node.source.value.trim();
- }
- return "";
- }
- function checkAndReport(
- context,
- node,
- modules,
- declarationType,
- includeExports
- ) {
- const module = getModule(node);
- 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("importAs");
- }
- }
- } else if (declarationType === "export") {
- if (shouldReportImportExport(node, exportNodes)) {
- messagesIds.push("export");
- }
- if (shouldReportImportExport(node, importNodes)) {
- messagesIds.push("exportAs");
- }
- }
- messagesIds.forEach(messageId =>
- context.report({
- node,
- messageId,
- data: {
- module
- }
- }));
- }
- }
- function handleImportsExports(
- context,
- modules,
- declarationType,
- includeExports
- ) {
- return function(node) {
- const module = getModule(node);
- if (module) {
- checkAndReport(
- context,
- node,
- modules,
- declarationType,
- includeExports
- );
- const currentNode = { node, declarationType };
- let nodes = [currentNode];
- if (modules.has(module)) {
- const previousNodes = modules.get(module);
- nodes = [...previousNodes, currentNode];
- }
- modules.set(module, nodes);
- }
- };
- }
- module.exports = {
- meta: {
- type: "problem",
- docs: {
- description: "disallow duplicate module imports",
- category: "ECMAScript 6",
- recommended: false,
- url: "https://eslint.org/docs/rules/no-duplicate-imports"
- },
- schema: [
- {
- type: "object",
- properties: {
- includeExports: {
- type: "boolean",
- default: 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."
- }
- },
- create(context) {
- const includeExports = (context.options[0] || {}).includeExports,
- modules = new Map();
- const handlers = {
- ImportDeclaration: handleImportsExports(
- context,
- modules,
- "import",
- includeExports
- )
- };
- if (includeExports) {
- handlers.ExportNamedDeclaration = handleImportsExports(
- context,
- modules,
- "export",
- includeExports
- );
- handlers.ExportAllDeclaration = handleImportsExports(
- context,
- modules,
- "export",
- includeExports
- );
- }
- return handlers;
- }
- };
|