2 * @fileoverview Helper class to aid in constructing fix commands.
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const astUtils = require("./ast-utils");
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
18 * A helper class to combine fix options into a fix command. Currently, it
19 * exposes some "retain" methods that extend the range of the text being
20 * replaced so that other fixes won't touch that region in the same pass.
25 * Create a new FixTracker.
26 * @param {ruleFixer} fixer A ruleFixer instance.
27 * @param {SourceCode} sourceCode A SourceCode object for the current code.
29 constructor(fixer, sourceCode) {
31 this.sourceCode = sourceCode;
32 this.retainedRange = null;
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 {int[]} range The range to retain.
39 * @returns {FixTracker} The same RuleFixer, for chained calls.
42 this.retainedRange = range;
47 * Given a node, find the function containing it (or the entire program) and
48 * mark it as retained, meaning that other fixes may not modify it in this
49 * pass. This is useful for avoiding conflicts in fixes that modify control
51 * @param {ASTNode} node The node to use as a starting point.
52 * @returns {FixTracker} The same RuleFixer, for chained calls.
54 retainEnclosingFunction(node) {
55 const functionNode = astUtils.getUpperFunction(node);
57 return this.retainRange(functionNode ? functionNode.range : this.sourceCode.ast.range);
61 * Given a node or token, find the token before and afterward, and mark that
62 * range as retained, meaning that other fixes may not modify it in this
63 * pass. This is useful for avoiding conflicts in fixes that make a small
64 * change to the code where the AST should not be changed.
65 * @param {ASTNode|Token} nodeOrToken The node or token to use as a starting
66 * point. The token to the left and right are use in the range.
67 * @returns {FixTracker} The same RuleFixer, for chained calls.
69 retainSurroundingTokens(nodeOrToken) {
70 const tokenBefore = this.sourceCode.getTokenBefore(nodeOrToken) || nodeOrToken;
71 const tokenAfter = this.sourceCode.getTokenAfter(nodeOrToken) || nodeOrToken;
73 return this.retainRange([tokenBefore.range[0], tokenAfter.range[1]]);
77 * Create a fix command that replaces the given range with the given text,
78 * accounting for any retained ranges.
79 * @param {int[]} range The range to remove in the fix.
80 * @param {string} text The text to insert in place of the range.
81 * @returns {Object} The fix command.
83 replaceTextRange(range, text) {
86 if (this.retainedRange) {
88 Math.min(this.retainedRange[0], range[0]),
89 Math.max(this.retainedRange[1], range[1])
95 return this.fixer.replaceTextRange(
97 this.sourceCode.text.slice(actualRange[0], range[0]) +
99 this.sourceCode.text.slice(range[1], actualRange[1])
104 * Create a fix command that removes the given node or token, accounting for
105 * any retained ranges.
106 * @param {ASTNode|Token} nodeOrToken The node or token to remove.
107 * @returns {Object} The fix command.
109 remove(nodeOrToken) {
110 return this.replaceTextRange(nodeOrToken.range, "");
114 module.exports = FixTracker;