1 | /**
|
---|
2 | * @fileoverview An object that creates fix commands for rules.
|
---|
3 | * @author Nicholas C. Zakas
|
---|
4 | */
|
---|
5 | "use strict";
|
---|
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 | */
|
---|
38 |
|
---|
39 | const ruleFixer = Object.freeze({
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Creates a fix command that inserts text after the given node or token.
|
---|
43 | * The fix is not applied until applyFixes() is called.
|
---|
44 | * @param {ASTNode|Token} nodeOrToken The node or token to insert after.
|
---|
45 | * @param {string} text The text to insert.
|
---|
46 | * @returns {Object} The fix command.
|
---|
47 | */
|
---|
48 | insertTextAfter(nodeOrToken, text) {
|
---|
49 | return this.insertTextAfterRange(nodeOrToken.range, text);
|
---|
50 | },
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Creates a fix command that inserts text after the specified range in the source text.
|
---|
54 | * The fix is not applied until applyFixes() is called.
|
---|
55 | * @param {int[]} range The range to replace, first item is start of range, second
|
---|
56 | * is end of range.
|
---|
57 | * @param {string} text The text to insert.
|
---|
58 | * @returns {Object} The fix command.
|
---|
59 | */
|
---|
60 | insertTextAfterRange(range, text) {
|
---|
61 | return insertTextAt(range[1], text);
|
---|
62 | },
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Creates a fix command that inserts text before the given node or token.
|
---|
66 | * The fix is not applied until applyFixes() is called.
|
---|
67 | * @param {ASTNode|Token} nodeOrToken The node or token to insert before.
|
---|
68 | * @param {string} text The text to insert.
|
---|
69 | * @returns {Object} The fix command.
|
---|
70 | */
|
---|
71 | insertTextBefore(nodeOrToken, text) {
|
---|
72 | return this.insertTextBeforeRange(nodeOrToken.range, text);
|
---|
73 | },
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Creates a fix command that inserts text before the specified range in the source text.
|
---|
77 | * The fix is not applied until applyFixes() is called.
|
---|
78 | * @param {int[]} range The range to replace, first item is start of range, second
|
---|
79 | * is end of range.
|
---|
80 | * @param {string} text The text to insert.
|
---|
81 | * @returns {Object} The fix command.
|
---|
82 | */
|
---|
83 | insertTextBeforeRange(range, text) {
|
---|
84 | return insertTextAt(range[0], text);
|
---|
85 | },
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Creates a fix command that replaces text at the node or token.
|
---|
89 | * The fix is not applied until applyFixes() is called.
|
---|
90 | * @param {ASTNode|Token} nodeOrToken The node or token to remove.
|
---|
91 | * @param {string} text The text to insert.
|
---|
92 | * @returns {Object} The fix command.
|
---|
93 | */
|
---|
94 | replaceText(nodeOrToken, text) {
|
---|
95 | return this.replaceTextRange(nodeOrToken.range, text);
|
---|
96 | },
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Creates a fix command that replaces text at the specified range in the source text.
|
---|
100 | * The fix is not applied until applyFixes() is called.
|
---|
101 | * @param {int[]} range The range to replace, first item is start of range, second
|
---|
102 | * is end of range.
|
---|
103 | * @param {string} text The text to insert.
|
---|
104 | * @returns {Object} The fix command.
|
---|
105 | */
|
---|
106 | replaceTextRange(range, text) {
|
---|
107 | return {
|
---|
108 | range,
|
---|
109 | text
|
---|
110 | };
|
---|
111 | },
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Creates a fix command that removes the node or token from the source.
|
---|
115 | * The fix is not applied until applyFixes() is called.
|
---|
116 | * @param {ASTNode|Token} nodeOrToken The node or token to remove.
|
---|
117 | * @returns {Object} The fix command.
|
---|
118 | */
|
---|
119 | remove(nodeOrToken) {
|
---|
120 | return this.removeRange(nodeOrToken.range);
|
---|
121 | },
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Creates a fix command that removes the specified range of text from the source.
|
---|
125 | * The fix is not applied until applyFixes() is called.
|
---|
126 | * @param {int[]} range The range to remove, first item is start of range, second
|
---|
127 | * is end of range.
|
---|
128 | * @returns {Object} The fix command.
|
---|
129 | */
|
---|
130 | removeRange(range) {
|
---|
131 | return {
|
---|
132 | range,
|
---|
133 | text: ""
|
---|
134 | };
|
---|
135 | }
|
---|
136 |
|
---|
137 | });
|
---|
138 |
|
---|
139 |
|
---|
140 | module.exports = ruleFixer;
|
---|