Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-duplicate-imports.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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} message - A message to be reported after the name of the module
  29. *
  30. * @returns {void} No return value
  31. */
  32. function checkAndReport(context, node, value, array, message) {
  33. if (array.indexOf(value) !== -1) {
  34. context.report({
  35. node,
  36. message: "'{{module}}' {{message}}",
  37. data: {
  38. module: value,
  39. message
  40. }
  41. });
  42. }
  43. }
  44. /**
  45. * @callback nodeCallback
  46. * @param {ASTNode} node - A node to handle.
  47. */
  48. /**
  49. * Returns a function handling the imports of a given file
  50. *
  51. * @param {RuleContext} context - The ESLint rule context object.
  52. * @param {boolean} includeExports - Whether or not to check for exports in addition to imports.
  53. * @param {string[]} importsInFile - The array containing other imports in the file.
  54. * @param {string[]} exportsInFile - The array containing other exports in the file.
  55. *
  56. * @returns {nodeCallback} A function passed to ESLint to handle the statement.
  57. */
  58. function handleImports(context, includeExports, importsInFile, exportsInFile) {
  59. return function(node) {
  60. const value = getValue(node);
  61. if (value) {
  62. checkAndReport(context, node, value, importsInFile, "import is duplicated.");
  63. if (includeExports) {
  64. checkAndReport(context, node, value, exportsInFile, "import is duplicated as export.");
  65. }
  66. importsInFile.push(value);
  67. }
  68. };
  69. }
  70. /**
  71. * Returns a function handling the exports of a given file
  72. *
  73. * @param {RuleContext} context - The ESLint rule context object.
  74. * @param {string[]} importsInFile - The array containing other imports in the file.
  75. * @param {string[]} exportsInFile - The array containing other exports in the file.
  76. *
  77. * @returns {nodeCallback} A function passed to ESLint to handle the statement.
  78. */
  79. function handleExports(context, importsInFile, exportsInFile) {
  80. return function(node) {
  81. const value = getValue(node);
  82. if (value) {
  83. checkAndReport(context, node, value, exportsInFile, "export is duplicated.");
  84. checkAndReport(context, node, value, importsInFile, "export is duplicated as import.");
  85. exportsInFile.push(value);
  86. }
  87. };
  88. }
  89. module.exports = {
  90. meta: {
  91. type: "problem",
  92. docs: {
  93. description: "disallow duplicate module imports",
  94. category: "ECMAScript 6",
  95. recommended: false,
  96. url: "https://eslint.org/docs/rules/no-duplicate-imports"
  97. },
  98. schema: [{
  99. type: "object",
  100. properties: {
  101. includeExports: {
  102. type: "boolean"
  103. }
  104. },
  105. additionalProperties: false
  106. }]
  107. },
  108. create(context) {
  109. const includeExports = (context.options[0] || {}).includeExports,
  110. importsInFile = [],
  111. exportsInFile = [];
  112. const handlers = {
  113. ImportDeclaration: handleImports(context, includeExports, importsInFile, exportsInFile)
  114. };
  115. if (includeExports) {
  116. handlers.ExportNamedDeclaration = handleExports(context, importsInFile, exportsInFile);
  117. handlers.ExportAllDeclaration = handleExports(context, importsInFile, exportsInFile);
  118. }
  119. return handlers;
  120. }
  121. };