rule-fixer.js 5.2 KB

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