1
1
import type { Node } from 'vue-eslint-parser/ast/nodes'
2
2
import type { Token } from 'vue-eslint-parser/ast/tokens'
3
- // import * as fixutils from "./fixUtils";
3
+ /**
4
+ * The following function is adapted from https://github.com/eslint/eslint/blob/master/lib/linter/rule-fixer.js
5
+ * MIT License https://github.com/eslint/eslint/blob/master/LICENSE
6
+ */
7
+
4
8
export type Operation = {
5
9
range : number [ ]
6
10
text : string
7
11
}
8
- // TODO: for simplicity of implementation, we've skipped all `{ ...expr }` cases
9
12
10
13
/**
11
14
* Creates a fix command that inserts text at the specified index in the source text.
12
15
* @param {int } index The 0-based index at which to insert the new text.
13
16
* @param {string } text The text to insert.
14
- * @returns {Object } The fix command.
17
+ * @returns {Operation } The fix command.
15
18
* @private
16
19
*/
17
20
export function insertTextAt ( index : number , text : string ) : Operation {
@@ -26,7 +29,7 @@ export function insertTextAt(index: number, text: string): Operation {
26
29
* The fix is not applied until applyFixes() is called.
27
30
* @param {Node|Token } nodeOrToken The node or token to insert after.
28
31
* @param {string } text The text to insert.
29
- * @returns {Object } The fix command.
32
+ * @returns {Operation } The fix command.
30
33
*/
31
34
export function insertTextAfter (
32
35
nodeOrToken : Node | Token ,
@@ -41,7 +44,7 @@ export function insertTextAfter(
41
44
* @param {int[] } range The range to replace, first item is start of range, second
42
45
* is end of range.
43
46
* @param {string } text The text to insert.
44
- * @returns {Object } The fix command.
47
+ * @returns {Operation } The fix command.
45
48
*/
46
49
export function insertTextAfterRange ( range : number [ ] , text : string ) : Operation {
47
50
return insertTextAt ( range [ 1 ] , text )
@@ -52,7 +55,7 @@ export function insertTextAfterRange(range: number[], text: string): Operation {
52
55
* The fix is not applied until applyFixes() is called.
53
56
* @param {Node|Token } nodeOrToken The node or token to insert before.
54
57
* @param {string } text The text to insert.
55
- * @returns {Object } The fix command.
58
+ * @returns {Operation } The fix command.
56
59
*/
57
60
export function insertTextBefore (
58
61
nodeOrToken : Node | Token ,
@@ -67,7 +70,7 @@ export function insertTextBefore(
67
70
* @param {int[] } range The range to replace, first item is start of range, second
68
71
* is end of range.
69
72
* @param {string } text The text to insert.
70
- * @returns {Object } The fix command.
73
+ * @returns {Operation } The fix command.
71
74
*/
72
75
export function insertTextBeforeRange (
73
76
range : number [ ] ,
@@ -81,7 +84,7 @@ export function insertTextBeforeRange(
81
84
* The fix is not applied until applyFixes() is called.
82
85
* @param {Node|Token } nodeOrToken The node or token to remove.
83
86
* @param {string } text The text to insert.
84
- * @returns {Object } The fix command.
87
+ * @returns {Operation } The fix command.
85
88
*/
86
89
export function replaceText (
87
90
nodeOrToken : Node | Token ,
@@ -96,7 +99,7 @@ export function replaceText(
96
99
* @param {int[] } range The range to replace, first item is start of range, second
97
100
* is end of range.
98
101
* @param {string } text The text to insert.
99
- * @returns {Object } The fix command.
102
+ * @returns {Operation } The fix command.
100
103
*/
101
104
export function replaceTextRange ( range : number [ ] , text : string ) : Operation {
102
105
return {
@@ -109,7 +112,7 @@ export function replaceTextRange(range: number[], text: string): Operation {
109
112
* Creates a fix command that removes the node or token from the source.
110
113
* The fix is not applied until applyFixes() is called.
111
114
* @param {Node|Token } nodeOrToken The node or token to remove.
112
- * @returns {Object } The fix command.
115
+ * @returns {Operation } The fix command.
113
116
*/
114
117
export function remove ( nodeOrToken : Node | Token ) : Operation {
115
118
return removeRange ( nodeOrToken . range )
@@ -120,7 +123,7 @@ export function remove(nodeOrToken: Node | Token): Operation {
120
123
* The fix is not applied until applyFixes() is called.
121
124
* @param {int[] } range The range to remove, first item is start of range, second
122
125
* is end of range.
123
- * @returns {Object } The fix command.
126
+ * @returns {Operation } The fix command.
124
127
*/
125
128
export function removeRange ( range : number [ ] ) : Operation {
126
129
return {
@@ -133,7 +136,7 @@ export function removeRange(range: number[]): Operation {
133
136
* Get text of Node
134
137
* @param {Node } node The node to get text
135
138
* @param {string } source The full text of the source code
136
- * @returns {string } The text of the node
139
+ * @returns {Operation } The text of the node
137
140
*/
138
141
export function getText ( node : Node , source : string ) : string {
139
142
const start = node ?. range [ 0 ]
0 commit comments