2 * @fileoverview An object that creates fix commands for rules.
3 * @author Nicholas C. Zakas
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
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.
24 function insertTextAt(index, text) {
26 range: [index, index],
31 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
36 * Creates code fixing commands for rules.
39 const ruleFixer = Object.freeze({
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.
48 insertTextAfter(nodeOrToken, text) {
49 return this.insertTextAfterRange(nodeOrToken.range, text);
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
57 * @param {string} text The text to insert.
58 * @returns {Object} The fix command.
60 insertTextAfterRange(range, text) {
61 return insertTextAt(range[1], text);
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.
71 insertTextBefore(nodeOrToken, text) {
72 return this.insertTextBeforeRange(nodeOrToken.range, text);
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
80 * @param {string} text The text to insert.
81 * @returns {Object} The fix command.
83 insertTextBeforeRange(range, text) {
84 return insertTextAt(range[0], text);
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.
94 replaceText(nodeOrToken, text) {
95 return this.replaceTextRange(nodeOrToken.range, text);
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
103 * @param {string} text The text to insert.
104 * @returns {Object} The fix command.
106 replaceTextRange(range, text) {
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.
119 remove(nodeOrToken) {
120 return this.removeRange(nodeOrToken.range);
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
128 * @returns {Object} The fix command.
140 module.exports = ruleFixer;