fix-tracker.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @fileoverview Helper class to aid in constructing fix commands.
  3. * @author Alan Pierce
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Typedefs
  8. //------------------------------------------------------------------------------
  9. /**
  10. * @import { SourceRange } from "@eslint/core";
  11. */
  12. //------------------------------------------------------------------------------
  13. // Requirements
  14. //------------------------------------------------------------------------------
  15. const astUtils = require("./ast-utils");
  16. //------------------------------------------------------------------------------
  17. // Public Interface
  18. //------------------------------------------------------------------------------
  19. /**
  20. * A helper class to combine fix options into a fix command. Currently, it
  21. * exposes some "retain" methods that extend the range of the text being
  22. * replaced so that other fixes won't touch that region in the same pass.
  23. */
  24. class FixTracker {
  25. /**
  26. * Create a new FixTracker.
  27. * @param {ruleFixer} fixer A ruleFixer instance.
  28. * @param {SourceCode} sourceCode A SourceCode object for the current code.
  29. */
  30. constructor(fixer, sourceCode) {
  31. this.fixer = fixer;
  32. this.sourceCode = sourceCode;
  33. this.retainedRange = null;
  34. }
  35. /**
  36. * Mark the given range as "retained", meaning that other fixes may not
  37. * may not modify this region in the same pass.
  38. * @param {SourceRange} range The range to retain.
  39. * @returns {FixTracker} The same RuleFixer, for chained calls.
  40. */
  41. retainRange(range) {
  42. this.retainedRange = range;
  43. return this;
  44. }
  45. /**
  46. * Given a node, find the function containing it (or the entire program) and
  47. * mark it as retained, meaning that other fixes may not modify it in this
  48. * pass. This is useful for avoiding conflicts in fixes that modify control
  49. * flow.
  50. * @param {ASTNode} node The node to use as a starting point.
  51. * @returns {FixTracker} The same RuleFixer, for chained calls.
  52. */
  53. retainEnclosingFunction(node) {
  54. const functionNode = astUtils.getUpperFunction(node);
  55. return this.retainRange(
  56. functionNode ? functionNode.range : this.sourceCode.ast.range,
  57. );
  58. }
  59. /**
  60. * Given a node or token, find the token before and afterward, and mark that
  61. * range as retained, meaning that other fixes may not modify it in this
  62. * pass. This is useful for avoiding conflicts in fixes that make a small
  63. * change to the code where the AST should not be changed.
  64. * @param {ASTNode|Token} nodeOrToken The node or token to use as a starting
  65. * point. The token to the left and right are use in the range.
  66. * @returns {FixTracker} The same RuleFixer, for chained calls.
  67. */
  68. retainSurroundingTokens(nodeOrToken) {
  69. const tokenBefore =
  70. this.sourceCode.getTokenBefore(nodeOrToken) || nodeOrToken;
  71. const tokenAfter =
  72. this.sourceCode.getTokenAfter(nodeOrToken) || nodeOrToken;
  73. return this.retainRange([tokenBefore.range[0], tokenAfter.range[1]]);
  74. }
  75. /**
  76. * Create a fix command that replaces the given range with the given text,
  77. * accounting for any retained ranges.
  78. * @param {SourceRange} range The range to remove in the fix.
  79. * @param {string} text The text to insert in place of the range.
  80. * @returns {Object} The fix command.
  81. */
  82. replaceTextRange(range, text) {
  83. let actualRange;
  84. if (this.retainedRange) {
  85. actualRange = [
  86. Math.min(this.retainedRange[0], range[0]),
  87. Math.max(this.retainedRange[1], range[1]),
  88. ];
  89. } else {
  90. actualRange = range;
  91. }
  92. return this.fixer.replaceTextRange(
  93. actualRange,
  94. this.sourceCode.text.slice(actualRange[0], range[0]) +
  95. text +
  96. this.sourceCode.text.slice(range[1], actualRange[1]),
  97. );
  98. }
  99. /**
  100. * Create a fix command that removes the given node or token, accounting for
  101. * any retained ranges.
  102. * @param {ASTNode|Token} nodeOrToken The node or token to remove.
  103. * @returns {Object} The fix command.
  104. */
  105. remove(nodeOrToken) {
  106. return this.replaceTextRange(nodeOrToken.range, "");
  107. }
  108. }
  109. module.exports = FixTracker;