Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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("./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. * @param {ruleFixer} fixer A ruleFixer instance.
  22. * @param {SourceCode} sourceCode A SourceCode object for the current code.
  23. */
  24. constructor(fixer, sourceCode) {
  25. this.fixer = fixer;
  26. this.sourceCode = sourceCode;
  27. this.retainedRange = null;
  28. }
  29. /**
  30. * Mark the given range as "retained", meaning that other fixes may not
  31. * may not modify this region in the same pass.
  32. * @param {int[]} range The range to retain.
  33. * @returns {FixTracker} The same RuleFixer, for chained calls.
  34. */
  35. retainRange(range) {
  36. this.retainedRange = range;
  37. return this;
  38. }
  39. /**
  40. * Given a node, find the function containing it (or the entire program) and
  41. * mark it as retained, meaning that other fixes may not modify it in this
  42. * pass. This is useful for avoiding conflicts in fixes that modify control
  43. * flow.
  44. * @param {ASTNode} node The node to use as a starting point.
  45. * @returns {FixTracker} The same RuleFixer, for chained calls.
  46. */
  47. retainEnclosingFunction(node) {
  48. const functionNode = astUtils.getUpperFunction(node);
  49. return this.retainRange(functionNode ? functionNode.range : this.sourceCode.ast.range);
  50. }
  51. /**
  52. * Given a node or token, find the token before and afterward, and mark that
  53. * range as retained, meaning that other fixes may not modify it in this
  54. * pass. This is useful for avoiding conflicts in fixes that make a small
  55. * change to the code where the AST should not be changed.
  56. * @param {ASTNode|Token} nodeOrToken The node or token to use as a starting
  57. * point. The token to the left and right are use in the range.
  58. * @returns {FixTracker} The same RuleFixer, for chained calls.
  59. */
  60. retainSurroundingTokens(nodeOrToken) {
  61. const tokenBefore = this.sourceCode.getTokenBefore(nodeOrToken) || nodeOrToken;
  62. const tokenAfter = this.sourceCode.getTokenAfter(nodeOrToken) || nodeOrToken;
  63. return this.retainRange([tokenBefore.range[0], tokenAfter.range[1]]);
  64. }
  65. /**
  66. * Create a fix command that replaces the given range with the given text,
  67. * accounting for any retained ranges.
  68. * @param {int[]} range The range to remove in the fix.
  69. * @param {string} text The text to insert in place of the range.
  70. * @returns {Object} The fix command.
  71. */
  72. replaceTextRange(range, text) {
  73. let actualRange;
  74. if (this.retainedRange) {
  75. actualRange = [
  76. Math.min(this.retainedRange[0], range[0]),
  77. Math.max(this.retainedRange[1], range[1])
  78. ];
  79. } else {
  80. actualRange = range;
  81. }
  82. return this.fixer.replaceTextRange(
  83. actualRange,
  84. this.sourceCode.text.slice(actualRange[0], range[0]) +
  85. text +
  86. this.sourceCode.text.slice(range[1], actualRange[1])
  87. );
  88. }
  89. /**
  90. * Create a fix command that removes the given node or token, accounting for
  91. * any retained ranges.
  92. * @param {ASTNode|Token} nodeOrToken The node or token to remove.
  93. * @returns {Object} The fix command.
  94. */
  95. remove(nodeOrToken) {
  96. return this.replaceTextRange(nodeOrToken.range, "");
  97. }
  98. }
  99. module.exports = FixTracker;