no-useless-rename.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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", default: false },
  24. ignoreImport: { type: "boolean", default: false },
  25. ignoreExport: { type: "boolean", default: false }
  26. },
  27. additionalProperties: false
  28. }
  29. ]
  30. },
  31. create(context) {
  32. const sourceCode = context.getSourceCode(),
  33. options = context.options[0] || {},
  34. ignoreDestructuring = options.ignoreDestructuring === true,
  35. ignoreImport = options.ignoreImport === true,
  36. ignoreExport = options.ignoreExport === true;
  37. //--------------------------------------------------------------------------
  38. // Helpers
  39. //--------------------------------------------------------------------------
  40. /**
  41. * Reports error for unnecessarily renamed assignments
  42. * @param {ASTNode} node node to report
  43. * @param {ASTNode} initial node with initial name value
  44. * @param {ASTNode} result node with new name value
  45. * @param {string} type the type of the offending node
  46. * @returns {void}
  47. */
  48. function reportError(node, initial, result, type) {
  49. const name = initial.type === "Identifier" ? initial.name : initial.value;
  50. return context.report({
  51. node,
  52. message: "{{type}} {{name}} unnecessarily renamed.",
  53. data: {
  54. name,
  55. type
  56. },
  57. fix(fixer) {
  58. if (sourceCode.commentsExistBetween(initial, result)) {
  59. return null;
  60. }
  61. const replacementText = result.type === "AssignmentPattern"
  62. ? sourceCode.getText(result)
  63. : name;
  64. return fixer.replaceTextRange([
  65. initial.range[0],
  66. result.range[1]
  67. ], replacementText);
  68. }
  69. });
  70. }
  71. /**
  72. * Checks whether a destructured assignment is unnecessarily renamed
  73. * @param {ASTNode} node node to check
  74. * @returns {void}
  75. */
  76. function checkDestructured(node) {
  77. if (ignoreDestructuring) {
  78. return;
  79. }
  80. for (const property of node.properties) {
  81. /*
  82. * TODO: Remove after babel-eslint removes ExperimentalRestProperty
  83. * https://github.com/eslint/eslint/issues/12335
  84. */
  85. if (property.type === "ExperimentalRestProperty") {
  86. continue;
  87. }
  88. /**
  89. * Properties using shorthand syntax and rest elements can not be renamed.
  90. * If the property is computed, we have no idea if a rename is useless or not.
  91. */
  92. if (property.shorthand || property.type === "RestElement" || property.computed) {
  93. continue;
  94. }
  95. const key = (property.key.type === "Identifier" && property.key.name) || (property.key.type === "Literal" && property.key.value);
  96. const renamedKey = property.value.type === "AssignmentPattern" ? property.value.left.name : property.value.name;
  97. if (key === renamedKey) {
  98. reportError(property, property.key, property.value, "Destructuring assignment");
  99. }
  100. }
  101. }
  102. /**
  103. * Checks whether an import is unnecessarily renamed
  104. * @param {ASTNode} node node to check
  105. * @returns {void}
  106. */
  107. function checkImport(node) {
  108. if (ignoreImport) {
  109. return;
  110. }
  111. if (node.imported.name === node.local.name &&
  112. node.imported.range[0] !== node.local.range[0]) {
  113. reportError(node, node.imported, node.local, "Import");
  114. }
  115. }
  116. /**
  117. * Checks whether an export is unnecessarily renamed
  118. * @param {ASTNode} node node to check
  119. * @returns {void}
  120. */
  121. function checkExport(node) {
  122. if (ignoreExport) {
  123. return;
  124. }
  125. if (node.local.name === node.exported.name &&
  126. node.local.range[0] !== node.exported.range[0]) {
  127. reportError(node, node.local, node.exported, "Export");
  128. }
  129. }
  130. //--------------------------------------------------------------------------
  131. // Public
  132. //--------------------------------------------------------------------------
  133. return {
  134. ObjectPattern: checkDestructured,
  135. ImportSpecifier: checkImport,
  136. ExportSpecifier: checkExport
  137. };
  138. }
  139. };