rule-fixer.js 4.5 KB

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