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