项目原始demo,不改动
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
Este repositório está arquivado. Você pode visualizar os arquivos e realizar clone, mas não poderá realizar push nem abrir issues e pull requests.

no-useless-rename.js 5.0 KiB

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