1 | /**
|
2 | * @fileoverview An object that creates fix commands for rules.
|
3 | * @author Nicholas C. Zakas
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Requirements
|
9 | //------------------------------------------------------------------------------
|
10 |
|
11 | // none!
|
12 |
|
13 | //------------------------------------------------------------------------------
|
14 | // Helpers
|
15 | //------------------------------------------------------------------------------
|
16 |
|
17 | /**
|
18 | * Creates a fix command that inserts text at the specified index in the source text.
|
19 | * @param {int} index The 0-based index at which to insert the new text.
|
20 | * @param {string} text The text to insert.
|
21 | * @returns {Object} The fix command.
|
22 | * @private
|
23 | */
|
24 | function insertTextAt(index, text) {
|
25 | return {
|
26 | range: [index, index],
|
27 | text
|
28 | };
|
29 | }
|
30 |
|
31 | //------------------------------------------------------------------------------
|
32 | // Public Interface
|
33 | //------------------------------------------------------------------------------
|
34 |
|
35 | /**
|
36 | * Creates code fixing commands for rules.
|
37 | * @constructor
|
38 | */
|
39 | function RuleFixer() {
|
40 | Object.freeze(this);
|
41 | }
|
42 |
|
43 | RuleFixer.prototype = {
|
44 | constructor: RuleFixer,
|
45 |
|
46 | /**
|
47 | * Creates a fix command that inserts text after the given node or token.
|
48 | * The fix is not applied until applyFixes() is called.
|
49 | * @param {ASTNode|Token} nodeOrToken The node or token to insert after.
|
50 | * @param {string} text The text to insert.
|
51 | * @returns {Object} The fix command.
|
52 | */
|
53 | insertTextAfter(nodeOrToken, text) {
|
54 | return this.insertTextAfterRange(nodeOrToken.range, text);
|
55 | },
|
56 |
|
57 | /**
|
58 | * Creates a fix command that inserts text after the specified range in the source text.
|
59 | * The fix is not applied until applyFixes() is called.
|
60 | * @param {int[]} range The range to replace, first item is start of range, second
|
61 | * is end of range.
|
62 | * @param {string} text The text to insert.
|
63 | * @returns {Object} The fix command.
|
64 | */
|
65 | insertTextAfterRange(range, text) {
|
66 | return insertTextAt(range[1], text);
|
67 | },
|
68 |
|
69 | /**
|
70 | * Creates a fix command that inserts text before the given node or token.
|
71 | * The fix is not applied until applyFixes() is called.
|
72 | * @param {ASTNode|Token} nodeOrToken The node or token to insert before.
|
73 | * @param {string} text The text to insert.
|
74 | * @returns {Object} The fix command.
|
75 | */
|
76 | insertTextBefore(nodeOrToken, text) {
|
77 | return this.insertTextBeforeRange(nodeOrToken.range, text);
|
78 | },
|
79 |
|
80 | /**
|
81 | * Creates a fix command that inserts text before the specified range in the source text.
|
82 | * The fix is not applied until applyFixes() is called.
|
83 | * @param {int[]} range The range to replace, first item is start of range, second
|
84 | * is end of range.
|
85 | * @param {string} text The text to insert.
|
86 | * @returns {Object} The fix command.
|
87 | */
|
88 | insertTextBeforeRange(range, text) {
|
89 | return insertTextAt(range[0], text);
|
90 | },
|
91 |
|
92 | /**
|
93 | * Creates a fix command that replaces text at the node or token.
|
94 | * The fix is not applied until applyFixes() is called.
|
95 | * @param {ASTNode|Token} nodeOrToken The node or token to remove.
|
96 | * @param {string} text The text to insert.
|
97 | * @returns {Object} The fix command.
|
98 | */
|
99 | replaceText(nodeOrToken, text) {
|
100 | return this.replaceTextRange(nodeOrToken.range, text);
|
101 | },
|
102 |
|
103 | /**
|
104 | * Creates a fix command that replaces text at the specified range in the source text.
|
105 | * The fix is not applied until applyFixes() is called.
|
106 | * @param {int[]} range The range to replace, first item is start of range, second
|
107 | * is end of range.
|
108 | * @param {string} text The text to insert.
|
109 | * @returns {Object} The fix command.
|
110 | */
|
111 | replaceTextRange(range, text) {
|
112 | return {
|
113 | range,
|
114 | text
|
115 | };
|
116 | },
|
117 |
|
118 | /**
|
119 | * Creates a fix command that removes the node or token from the source.
|
120 | * The fix is not applied until applyFixes() is called.
|
121 | * @param {ASTNode|Token} nodeOrToken The node or token to remove.
|
122 | * @returns {Object} The fix command.
|
123 | */
|
124 | remove(nodeOrToken) {
|
125 | return this.removeRange(nodeOrToken.range);
|
126 | },
|
127 |
|
128 | /**
|
129 | * Creates a fix command that removes the specified range of text from the source.
|
130 | * The fix is not applied until applyFixes() is called.
|
131 | * @param {int[]} range The range to remove, first item is start of range, second
|
132 | * is end of range.
|
133 | * @returns {Object} The fix command.
|
134 | */
|
135 | removeRange(range) {
|
136 | return {
|
137 | range,
|
138 | text: ""
|
139 | };
|
140 | }
|
141 |
|
142 | };
|
143 |
|
144 |
|
145 | module.exports = RuleFixer;
|