1 | /**
|
2 | * @fileoverview Helper class to aid in constructing fix commands.
|
3 | * @author Alan Pierce
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Requirements
|
9 | //------------------------------------------------------------------------------
|
10 |
|
11 | const 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 | */
|
22 | class FixTracker {
|
23 |
|
24 | /**
|
25 | * Create a new FixTracker.
|
26 | *
|
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 | /**
|
37 | * Mark the given range as "retained", meaning that other fixes may not
|
38 | * may not modify this region in the same pass.
|
39 | *
|
40 | * @param {int[]} range The range to retain.
|
41 | * @returns {FixTracker} The same RuleFixer, for chained calls.
|
42 | */
|
43 | retainRange(range) {
|
44 | this.retainedRange = range;
|
45 | return this;
|
46 | }
|
47 |
|
48 | /**
|
49 | * Given a node, find the function containing it (or the entire program) and
|
50 | * mark it as retained, meaning that other fixes may not modify it in this
|
51 | * pass. This is useful for avoiding conflicts in fixes that modify control
|
52 | * flow.
|
53 | *
|
54 | * @param {ASTNode} node The node to use as a starting point.
|
55 | * @returns {FixTracker} The same RuleFixer, for chained calls.
|
56 | */
|
57 | retainEnclosingFunction(node) {
|
58 | const functionNode = astUtils.getUpperFunction(node);
|
59 |
|
60 | return this.retainRange(functionNode ? functionNode.range : this.sourceCode.ast.range);
|
61 | }
|
62 |
|
63 | /**
|
64 | * Given a node or token, find the token before and afterward, and mark that
|
65 | * range as retained, meaning that other fixes may not modify it in this
|
66 | * pass. This is useful for avoiding conflicts in fixes that make a small
|
67 | * change to the code where the AST should not be changed.
|
68 | *
|
69 | * @param {ASTNode|Token} nodeOrToken The node or token to use as a starting
|
70 | * point. The token to the left and right are use in the range.
|
71 | * @returns {FixTracker} The same RuleFixer, for chained calls.
|
72 | */
|
73 | retainSurroundingTokens(nodeOrToken) {
|
74 | const tokenBefore = this.sourceCode.getTokenBefore(nodeOrToken) || nodeOrToken;
|
75 | const tokenAfter = this.sourceCode.getTokenAfter(nodeOrToken) || nodeOrToken;
|
76 |
|
77 | return this.retainRange([tokenBefore.range[0], tokenAfter.range[1]]);
|
78 | }
|
79 |
|
80 | /**
|
81 | * Create a fix command that replaces the given range with the given text,
|
82 | * accounting for any retained ranges.
|
83 | *
|
84 | * @param {int[]} range The range to remove in the fix.
|
85 | * @param {string} text The text to insert in place of the range.
|
86 | * @returns {Object} The fix command.
|
87 | */
|
88 | replaceTextRange(range, text) {
|
89 | let actualRange;
|
90 |
|
91 | if (this.retainedRange) {
|
92 | actualRange = [
|
93 | Math.min(this.retainedRange[0], range[0]),
|
94 | Math.max(this.retainedRange[1], range[1])
|
95 | ];
|
96 | } else {
|
97 | actualRange = range;
|
98 | }
|
99 |
|
100 | return this.fixer.replaceTextRange(
|
101 | actualRange,
|
102 | this.sourceCode.text.slice(actualRange[0], range[0]) +
|
103 | text +
|
104 | this.sourceCode.text.slice(range[1], actualRange[1])
|
105 | );
|
106 | }
|
107 |
|
108 | /**
|
109 | * Create a fix command that removes the given node or token, accounting for
|
110 | * any retained ranges.
|
111 | *
|
112 | * @param {ASTNode|Token} nodeOrToken The node or token to remove.
|
113 | * @returns {Object} The fix command.
|
114 | */
|
115 | remove(nodeOrToken) {
|
116 | return this.replaceTextRange(nodeOrToken.range, "");
|
117 | }
|
118 | }
|
119 |
|
120 | module.exports = FixTracker;
|