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-useless-rename.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @fileoverview Disallow renaming import, export, and destructured assignments to the same name.
  3. * @author Kai Cataldo
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "disallow renaming import, export, and destructured assignments to the same name",
  14. category: "ECMAScript 6",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-useless-rename"
  17. },
  18. fixable: "code",
  19. schema: [
  20. {
  21. type: "object",
  22. properties: {
  23. ignoreDestructuring: { type: "boolean" },
  24. ignoreImport: { type: "boolean" },
  25. ignoreExport: { type: "boolean" }
  26. },
  27. additionalProperties: false
  28. }
  29. ]
  30. },
  31. create(context) {
  32. const options = context.options[0] || {},
  33. ignoreDestructuring = options.ignoreDestructuring === true,
  34. ignoreImport = options.ignoreImport === true,
  35. ignoreExport = options.ignoreExport === true;
  36. //--------------------------------------------------------------------------
  37. // Helpers
  38. //--------------------------------------------------------------------------
  39. /**
  40. * Reports error for unnecessarily renamed assignments
  41. * @param {ASTNode} node - node to report
  42. * @param {ASTNode} initial - node with initial name value
  43. * @param {ASTNode} result - node with new name value
  44. * @param {string} type - the type of the offending node
  45. * @returns {void}
  46. */
  47. function reportError(node, initial, result, type) {
  48. const name = initial.type === "Identifier" ? initial.name : initial.value;
  49. return context.report({
  50. node,
  51. message: "{{type}} {{name}} unnecessarily renamed.",
  52. data: {
  53. name,
  54. type
  55. },
  56. fix(fixer) {
  57. return fixer.replaceTextRange([
  58. initial.range[0],
  59. result.range[1]
  60. ], name);
  61. }
  62. });
  63. }
  64. /**
  65. * Checks whether a destructured assignment is unnecessarily renamed
  66. * @param {ASTNode} node - node to check
  67. * @returns {void}
  68. */
  69. function checkDestructured(node) {
  70. if (ignoreDestructuring) {
  71. return;
  72. }
  73. const properties = node.properties;
  74. for (let i = 0; i < properties.length; i++) {
  75. if (properties[i].shorthand) {
  76. continue;
  77. }
  78. /**
  79. * If an ObjectPattern property is computed, we have no idea
  80. * if a rename is useless or not. If an ObjectPattern property
  81. * lacks a key, it is likely an ExperimentalRestProperty and
  82. * so there is no "renaming" occurring here.
  83. */
  84. if (properties[i].computed || !properties[i].key) {
  85. continue;
  86. }
  87. if (properties[i].key.type === "Identifier" && properties[i].key.name === properties[i].value.name ||
  88. properties[i].key.type === "Literal" && properties[i].key.value === properties[i].value.name) {
  89. reportError(properties[i], properties[i].key, properties[i].value, "Destructuring assignment");
  90. }
  91. }
  92. }
  93. /**
  94. * Checks whether an import is unnecessarily renamed
  95. * @param {ASTNode} node - node to check
  96. * @returns {void}
  97. */
  98. function checkImport(node) {
  99. if (ignoreImport) {
  100. return;
  101. }
  102. if (node.imported.name === node.local.name &&
  103. node.imported.range[0] !== node.local.range[0]) {
  104. reportError(node, node.imported, node.local, "Import");
  105. }
  106. }
  107. /**
  108. * Checks whether an export is unnecessarily renamed
  109. * @param {ASTNode} node - node to check
  110. * @returns {void}
  111. */
  112. function checkExport(node) {
  113. if (ignoreExport) {
  114. return;
  115. }
  116. if (node.local.name === node.exported.name &&
  117. node.local.range[0] !== node.exported.range[0]) {
  118. reportError(node, node.local, node.exported, "Export");
  119. }
  120. }
  121. //--------------------------------------------------------------------------
  122. // Public
  123. //--------------------------------------------------------------------------
  124. return {
  125. ObjectPattern: checkDestructured,
  126. ImportSpecifier: checkImport,
  127. ExportSpecifier: checkExport
  128. };
  129. }
  130. };