Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fix-tracker.js 4.1KB

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