no-duplicate-imports.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @fileoverview Restrict usage of duplicate imports.
  3. * @author Simen Bekkhus
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /**
  10. * Returns the name of the module imported or re-exported.
  11. *
  12. * @param {ASTNode} node - A node to get.
  13. * @returns {string} the name of the module, or empty string if no name.
  14. */
  15. function getValue(node) {
  16. if (node && node.source && node.source.value) {
  17. return node.source.value.trim();
  18. }
  19. return "";
  20. }
  21. /**
  22. * Checks if the name of the import or export exists in the given array, and reports if so.
  23. *
  24. * @param {RuleContext} context - The ESLint rule context object.
  25. * @param {ASTNode} node - A node to get.
  26. * @param {string} value - The name of the imported or exported module.
  27. * @param {string[]} array - The array containing other imports or exports in the file.
  28. * @param {string} messageId - A messageId to be reported after the name of the module
  29. *
  30. * @returns {void} No return value
  31. */
  32. function checkAndReport(context, node, value, array, messageId) {
  33. if (array.indexOf(value) !== -1) {
  34. context.report({
  35. node,
  36. messageId,
  37. data: {
  38. module: value
  39. }
  40. });
  41. }
  42. }
  43. /**
  44. * @callback nodeCallback
  45. * @param {ASTNode} node - A node to handle.
  46. */
  47. /**
  48. * Returns a function handling the imports of a given file
  49. *
  50. * @param {RuleContext} context - The ESLint rule context object.
  51. * @param {boolean} includeExports - Whether or not to check for exports in addition to imports.
  52. * @param {string[]} importsInFile - The array containing other imports in the file.
  53. * @param {string[]} exportsInFile - The array containing other exports in the file.
  54. *
  55. * @returns {nodeCallback} A function passed to ESLint to handle the statement.
  56. */
  57. function handleImports(context, includeExports, importsInFile, exportsInFile) {
  58. return function(node) {
  59. const value = getValue(node);
  60. if (value) {
  61. checkAndReport(context, node, value, importsInFile, "import");
  62. if (includeExports) {
  63. checkAndReport(context, node, value, exportsInFile, "importAs");
  64. }
  65. importsInFile.push(value);
  66. }
  67. };
  68. }
  69. /**
  70. * Returns a function handling the exports of a given file
  71. *
  72. * @param {RuleContext} context - The ESLint rule context object.
  73. * @param {string[]} importsInFile - The array containing other imports in the file.
  74. * @param {string[]} exportsInFile - The array containing other exports in the file.
  75. *
  76. * @returns {nodeCallback} A function passed to ESLint to handle the statement.
  77. */
  78. function handleExports(context, importsInFile, exportsInFile) {
  79. return function(node) {
  80. const value = getValue(node);
  81. if (value) {
  82. checkAndReport(context, node, value, exportsInFile, "export");
  83. checkAndReport(context, node, value, importsInFile, "exportAs");
  84. exportsInFile.push(value);
  85. }
  86. };
  87. }
  88. module.exports = {
  89. meta: {
  90. type: "problem",
  91. docs: {
  92. description: "disallow duplicate module imports",
  93. category: "ECMAScript 6",
  94. recommended: false,
  95. url: "https://eslint.org/docs/rules/no-duplicate-imports"
  96. },
  97. schema: [{
  98. type: "object",
  99. properties: {
  100. includeExports: {
  101. type: "boolean",
  102. default: false
  103. }
  104. },
  105. additionalProperties: false
  106. }],
  107. messages: {
  108. import: "'{{module}}' import is duplicated.",
  109. importAs: "'{{module}}' import is duplicated as export.",
  110. export: "'{{module}}' export is duplicated.",
  111. exportAs: "'{{module}}' export is duplicated as import."
  112. }
  113. },
  114. create(context) {
  115. const includeExports = (context.options[0] || {}).includeExports,
  116. importsInFile = [],
  117. exportsInFile = [];
  118. const handlers = {
  119. ImportDeclaration: handleImports(context, includeExports, importsInFile, exportsInFile)
  120. };
  121. if (includeExports) {
  122. handlers.ExportNamedDeclaration = handleExports(context, importsInFile, exportsInFile);
  123. handlers.ExportAllDeclaration = handleExports(context, importsInFile, exportsInFile);
  124. }
  125. return handlers;
  126. }
  127. };