rule-fixer.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @fileoverview An object that creates fix commands for rules.
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Typedefs
  8. //------------------------------------------------------------------------------
  9. /**
  10. * @import { SourceRange } from "@eslint/core";
  11. */
  12. /* eslint class-methods-use-this: off -- Methods desired on instance */
  13. //------------------------------------------------------------------------------
  14. // Requirements
  15. //------------------------------------------------------------------------------
  16. // none!
  17. //------------------------------------------------------------------------------
  18. // Helpers
  19. //------------------------------------------------------------------------------
  20. /**
  21. * Creates a fix command that inserts text at the specified index in the source text.
  22. * @param {number} index The 0-based index at which to insert the new text.
  23. * @param {string} text The text to insert.
  24. * @returns {Object} The fix command.
  25. * @private
  26. */
  27. function insertTextAt(index, text) {
  28. return {
  29. range: [index, index],
  30. text,
  31. };
  32. }
  33. //------------------------------------------------------------------------------
  34. // Public Interface
  35. //------------------------------------------------------------------------------
  36. /**
  37. * Creates code fixing commands for rules.
  38. */
  39. class RuleFixer {
  40. /**
  41. * The source code object representing the text to be fixed.
  42. * @type {SourceCode}
  43. */
  44. #sourceCode;
  45. /**
  46. * Creates a new instance.
  47. * @param {Object} options The options for the fixer.
  48. * @param {SourceCode} options.sourceCode The source code object representing the text to be fixed.
  49. */
  50. constructor({ sourceCode }) {
  51. this.#sourceCode = sourceCode;
  52. }
  53. /**
  54. * Creates a fix command that inserts text after the given node or token.
  55. * The fix is not applied until applyFixes() is called.
  56. * @param {ASTNode|Token} nodeOrToken The node or token to insert after.
  57. * @param {string} text The text to insert.
  58. * @returns {Object} The fix command.
  59. */
  60. insertTextAfter(nodeOrToken, text) {
  61. const range = this.#sourceCode.getRange(nodeOrToken);
  62. return this.insertTextAfterRange(range, text);
  63. }
  64. /**
  65. * Creates a fix command that inserts text after the specified range in the source text.
  66. * The fix is not applied until applyFixes() is called.
  67. * @param {SourceRange} range The range to replace, first item is start of range, second
  68. * is end of range.
  69. * @param {string} text The text to insert.
  70. * @returns {Object} The fix command.
  71. */
  72. insertTextAfterRange(range, text) {
  73. return insertTextAt(range[1], text);
  74. }
  75. /**
  76. * Creates a fix command that inserts text before the given node or token.
  77. * The fix is not applied until applyFixes() is called.
  78. * @param {ASTNode|Token} nodeOrToken The node or token to insert before.
  79. * @param {string} text The text to insert.
  80. * @returns {Object} The fix command.
  81. */
  82. insertTextBefore(nodeOrToken, text) {
  83. const range = this.#sourceCode.getRange(nodeOrToken);
  84. return this.insertTextBeforeRange(range, text);
  85. }
  86. /**
  87. * Creates a fix command that inserts text before the specified range in the source text.
  88. * The fix is not applied until applyFixes() is called.
  89. * @param {SourceRange} range The range to replace, first item is start of range, second
  90. * is end of range.
  91. * @param {string} text The text to insert.
  92. * @returns {Object} The fix command.
  93. */
  94. insertTextBeforeRange(range, text) {
  95. return insertTextAt(range[0], text);
  96. }
  97. /**
  98. * Creates a fix command that replaces text at the node or token.
  99. * The fix is not applied until applyFixes() is called.
  100. * @param {ASTNode|Token} nodeOrToken The node or token to remove.
  101. * @param {string} text The text to insert.
  102. * @returns {Object} The fix command.
  103. */
  104. replaceText(nodeOrToken, text) {
  105. const range = this.#sourceCode.getRange(nodeOrToken);
  106. return this.replaceTextRange(range, text);
  107. }
  108. /**
  109. * Creates a fix command that replaces text at the specified range in the source text.
  110. * The fix is not applied until applyFixes() is called.
  111. * @param {SourceRange} range The range to replace, first item is start of range, second
  112. * is end of range.
  113. * @param {string} text The text to insert.
  114. * @returns {Object} The fix command.
  115. */
  116. replaceTextRange(range, text) {
  117. return {
  118. range,
  119. text,
  120. };
  121. }
  122. /**
  123. * Creates a fix command that removes the node or token from the source.
  124. * The fix is not applied until applyFixes() is called.
  125. * @param {ASTNode|Token} nodeOrToken The node or token to remove.
  126. * @returns {Object} The fix command.
  127. */
  128. remove(nodeOrToken) {
  129. const range = this.#sourceCode.getRange(nodeOrToken);
  130. return this.removeRange(range);
  131. }
  132. /**
  133. * Creates a fix command that removes the specified range of text from the source.
  134. * The fix is not applied until applyFixes() is called.
  135. * @param {SourceRange} range The range to remove, first item is start of range, second
  136. * is end of range.
  137. * @returns {Object} The fix command.
  138. */
  139. removeRange(range) {
  140. return {
  141. range,
  142. text: "",
  143. };
  144. }
  145. }
  146. module.exports = { RuleFixer };