Skip to content

Commit 673e2f6

Browse files
committed
feat: implement fix operation utils
1 parent 538c71e commit 673e2f6

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

src/operationUtils.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import type { Node } from 'vue-eslint-parser/ast/nodes'
2+
import type { Token } from 'vue-eslint-parser/ast/tokens'
3+
// import * as fixutils from "./fixUtils";
4+
export type Operation = {
5+
range: number[]
6+
text: string
7+
}
8+
// TODO: for simplicity of implementation, we've skipped all `{ ...expr }` cases
9+
10+
/**
11+
* Creates a fix command that inserts text at the specified index in the source text.
12+
* @param {int} index The 0-based index at which to insert the new text.
13+
* @param {string} text The text to insert.
14+
* @returns {Object} The fix command.
15+
* @private
16+
*/
17+
export function insertTextAt(index: number, text: string): Operation {
18+
return {
19+
range: [index, index],
20+
text,
21+
}
22+
}
23+
24+
/**
25+
* Creates a fix command that inserts text after the given node or token.
26+
* The fix is not applied until applyFixes() is called.
27+
* @param {Node|Token} nodeOrToken The node or token to insert after.
28+
* @param {string} text The text to insert.
29+
* @returns {Object} The fix command.
30+
*/
31+
export function insertTextAfter(
32+
nodeOrToken: Node | Token,
33+
text: string
34+
): Operation {
35+
return insertTextAfterRange(nodeOrToken.range, text)
36+
}
37+
38+
/**
39+
* Creates a fix command that inserts text after the specified range in the source text.
40+
* The fix is not applied until applyFixes() is called.
41+
* @param {int[]} range The range to replace, first item is start of range, second
42+
* is end of range.
43+
* @param {string} text The text to insert.
44+
* @returns {Object} The fix command.
45+
*/
46+
export function insertTextAfterRange(range: number[], text: string): Operation {
47+
return insertTextAt(range[1], text)
48+
}
49+
50+
/**
51+
* Creates a fix command that inserts text before the given node or token.
52+
* The fix is not applied until applyFixes() is called.
53+
* @param {Node|Token} nodeOrToken The node or token to insert before.
54+
* @param {string} text The text to insert.
55+
* @returns {Object} The fix command.
56+
*/
57+
export function insertTextBefore(
58+
nodeOrToken: Node | Token,
59+
text: string
60+
): Operation {
61+
return insertTextBeforeRange(nodeOrToken.range, text)
62+
}
63+
64+
/**
65+
* Creates a fix command that inserts text before the specified range in the source text.
66+
* The fix is not applied until applyFixes() is called.
67+
* @param {int[]} range The range to replace, first item is start of range, second
68+
* is end of range.
69+
* @param {string} text The text to insert.
70+
* @returns {Object} The fix command.
71+
*/
72+
export function insertTextBeforeRange(
73+
range: number[],
74+
text: string
75+
): Operation {
76+
return insertTextAt(range[0], text)
77+
}
78+
79+
/**
80+
* Creates a fix command that replaces text at the node or token.
81+
* The fix is not applied until applyFixes() is called.
82+
* @param {Node|Token} nodeOrToken The node or token to remove.
83+
* @param {string} text The text to insert.
84+
* @returns {Object} The fix command.
85+
*/
86+
export function replaceText(
87+
nodeOrToken: Node | Token,
88+
text: string
89+
): Operation {
90+
return replaceTextRange(nodeOrToken.range, text)
91+
}
92+
93+
/**
94+
* Creates a fix command that replaces text at the specified range in the source text.
95+
* The fix is not applied until applyFixes() is called.
96+
* @param {int[]} range The range to replace, first item is start of range, second
97+
* is end of range.
98+
* @param {string} text The text to insert.
99+
* @returns {Object} The fix command.
100+
*/
101+
export function replaceTextRange(range: number[], text: string): Operation {
102+
return {
103+
range,
104+
text,
105+
}
106+
}
107+
108+
/**
109+
* Creates a fix command that removes the node or token from the source.
110+
* The fix is not applied until applyFixes() is called.
111+
* @param {Node|Token} nodeOrToken The node or token to remove.
112+
* @returns {Object} The fix command.
113+
*/
114+
export function remove(nodeOrToken: Node | Token): Operation {
115+
return removeRange(nodeOrToken.range)
116+
}
117+
118+
/**
119+
* Creates a fix command that removes the specified range of text from the source.
120+
* The fix is not applied until applyFixes() is called.
121+
* @param {int[]} range The range to remove, first item is start of range, second
122+
* is end of range.
123+
* @returns {Object} The fix command.
124+
*/
125+
export function removeRange(range: number[]): Operation {
126+
return {
127+
range,
128+
text: '',
129+
}
130+
}

0 commit comments

Comments
 (0)