• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * @fileoverview Helper class to aid in constructing fix commands.
3 * @author Alan Pierce
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Requirements
9//------------------------------------------------------------------------------
10
11const astUtils = require("./ast-utils");
12
13//------------------------------------------------------------------------------
14// Public Interface
15//------------------------------------------------------------------------------
16
17/**
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.
21 */
22class FixTracker {
23
24    /**
25     * Create a new FixTracker.
26     * @param {ruleFixer} fixer A ruleFixer instance.
27     * @param {SourceCode} sourceCode A SourceCode object for the current code.
28     */
29    constructor(fixer, sourceCode) {
30        this.fixer = fixer;
31        this.sourceCode = sourceCode;
32        this.retainedRange = null;
33    }
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 {int[]} 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    /**
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
50     * flow.
51     * @param {ASTNode} node The node to use as a starting point.
52     * @returns {FixTracker} The same RuleFixer, for chained calls.
53     */
54    retainEnclosingFunction(node) {
55        const functionNode = astUtils.getUpperFunction(node);
56
57        return this.retainRange(functionNode ? functionNode.range : this.sourceCode.ast.range);
58    }
59
60    /**
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.
68     */
69    retainSurroundingTokens(nodeOrToken) {
70        const tokenBefore = this.sourceCode.getTokenBefore(nodeOrToken) || nodeOrToken;
71        const tokenAfter = this.sourceCode.getTokenAfter(nodeOrToken) || nodeOrToken;
72
73        return this.retainRange([tokenBefore.range[0], tokenAfter.range[1]]);
74    }
75
76    /**
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.
82     */
83    replaceTextRange(range, text) {
84        let actualRange;
85
86        if (this.retainedRange) {
87            actualRange = [
88                Math.min(this.retainedRange[0], range[0]),
89                Math.max(this.retainedRange[1], range[1])
90            ];
91        } else {
92            actualRange = range;
93        }
94
95        return this.fixer.replaceTextRange(
96            actualRange,
97            this.sourceCode.text.slice(actualRange[0], range[0]) +
98                text +
99                this.sourceCode.text.slice(range[1], actualRange[1])
100        );
101    }
102
103    /**
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.
108     */
109    remove(nodeOrToken) {
110        return this.replaceTextRange(nodeOrToken.range, "");
111    }
112}
113
114module.exports = FixTracker;
115