@@ -88,7 +88,7 @@ export function* fixForSorting(
88
88
}
89
89
90
90
/**
91
- * Calculate the range of the target information .
91
+ * Calculate the fix information of the target.
92
92
*/
93
93
function calcTargetInfo (
94
94
sourceCode : SourceCode ,
@@ -97,14 +97,12 @@ function calcTargetInfo(
97
97
insertCode : string ;
98
98
removeRanges : ESLintAST . Range [ ] ;
99
99
} {
100
- if ( ! target . node ) {
101
- return calcTargetInfoFromAround ( sourceCode , target ) ;
102
- }
103
- const node = target . node ;
104
- const nodeLastToken = getLastTokenOfNode ( sourceCode , node ) ;
100
+ const nodeEndIndex = target . node
101
+ ? getLastTokenOfNode ( sourceCode , target . node ) . range [ 1 ]
102
+ : target . after . range [ 0 ] ;
105
103
106
- const endInfo = getElementEndInfo ( sourceCode , node ) ;
107
- const prevInfo = getPrevElementInfo ( sourceCode , { node } ) ;
104
+ const endInfo = getElementEndInfo ( sourceCode , target ) ;
105
+ const prevInfo = getPrevElementInfo ( sourceCode , target ) ;
108
106
109
107
let insertCode : string ;
110
108
@@ -113,17 +111,14 @@ function calcTargetInfo(
113
111
insertCode = `${ sourceCode . text . slice (
114
112
prevInfo . last . range ! [ 1 ] ,
115
113
prevInfo . comma . range [ 0 ] ,
116
- ) } ${ sourceCode . text . slice ( prevInfo . comma . range [ 1 ] , nodeLastToken . range [ 1 ] ) } `;
114
+ ) } ${ sourceCode . text . slice ( prevInfo . comma . range [ 1 ] , nodeEndIndex ) } `;
117
115
removeRanges . push (
118
116
[ prevInfo . last . range ! [ 1 ] , prevInfo . comma . range [ 0 ] ] ,
119
- [ prevInfo . comma . range [ 1 ] , nodeLastToken . range [ 1 ] ] ,
117
+ [ prevInfo . comma . range [ 1 ] , nodeEndIndex ] ,
120
118
) ;
121
119
} else {
122
- insertCode = sourceCode . text . slice (
123
- prevInfo . last . range ! [ 1 ] ,
124
- nodeLastToken . range [ 1 ] ,
125
- ) ;
126
- removeRanges . push ( [ prevInfo . last . range ! [ 1 ] , nodeLastToken . range [ 1 ] ] ) ;
120
+ insertCode = sourceCode . text . slice ( prevInfo . last . range ! [ 1 ] , nodeEndIndex ) ;
121
+ removeRanges . push ( [ prevInfo . last . range ! [ 1 ] , nodeEndIndex ] ) ;
127
122
}
128
123
129
124
const hasTrailingComma =
@@ -134,55 +129,15 @@ function calcTargetInfo(
134
129
removeRanges . push ( prevInfo . comma . range ) ;
135
130
}
136
131
}
137
- insertCode += sourceCode . text . slice (
138
- nodeLastToken . range [ 1 ] ,
139
- endInfo . last . range ! [ 1 ] ,
140
- ) ;
141
- removeRanges . push ( [ nodeLastToken . range [ 1 ] , endInfo . last . range ! [ 1 ] ] ) ;
132
+ insertCode += sourceCode . text . slice ( nodeEndIndex , endInfo . last . range ! [ 1 ] ) ;
133
+ removeRanges . push ( [ nodeEndIndex , endInfo . last . range ! [ 1 ] ] ) ;
142
134
143
135
return {
144
136
insertCode,
145
137
removeRanges,
146
138
} ;
147
139
}
148
140
149
- /**
150
- * Calculate the range of the target information from the around tokens.
151
- */
152
- function calcTargetInfoFromAround (
153
- sourceCode : SourceCode ,
154
- target : AroundTarget ,
155
- ) : {
156
- insertCode : string ;
157
- removeRanges : ESLintAST . Range [ ] ;
158
- } {
159
- const hasTrailingComma = isComma ( target . after ) ;
160
- const codeStart = target . before . range [ 1 ] ; // to include comments
161
- let codeEnd : number ;
162
- if ( hasTrailingComma ) {
163
- // , /**/,
164
- // ^^^^^^
165
- codeEnd = target . after . range [ 1 ] ;
166
- } else {
167
- // , /**/ ]
168
- // ^^^^^^
169
- codeEnd = target . after . range [ 0 ] ;
170
- }
171
- let removeStart = codeStart ;
172
- if ( ! hasTrailingComma ) {
173
- // The target is always the second or subsequent element, so it always has a leading comma.
174
- // , /**/ ]
175
- // ^^^^^^^
176
- removeStart = target . before . range [ 0 ] ;
177
- }
178
-
179
- return {
180
- insertCode :
181
- sourceCode . text . slice ( codeStart , codeEnd ) + ( hasTrailingComma ? "" : "," ) ,
182
- removeRanges : [ [ removeStart , codeEnd ] ] ,
183
- } ;
184
- }
185
-
186
141
/**
187
142
* Get the first token of the node.
188
143
*/
@@ -193,7 +148,7 @@ function getFirstTokenOfNode(
193
148
let token = sourceCode . getFirstToken ( node as never ) ! ;
194
149
let target : ESLintAST . Token | null = token ;
195
150
while (
196
- ( target = sourceCode . getTokenBefore ( target ) ) &&
151
+ ( target = sourceCode . getTokenBefore ( token ) ) &&
197
152
isOpeningParenToken ( target )
198
153
) {
199
154
token = target ;
@@ -211,7 +166,7 @@ function getLastTokenOfNode(
211
166
let token = sourceCode . getLastToken ( node as never ) ! ;
212
167
let target : ESLintAST . Token | null = token ;
213
168
while (
214
- ( target = sourceCode . getTokenAfter ( target ) ) &&
169
+ ( target = sourceCode . getTokenAfter ( token ) ) &&
215
170
isClosingParenToken ( target )
216
171
) {
217
172
token = target ;
@@ -224,7 +179,7 @@ function getLastTokenOfNode(
224
179
*/
225
180
function getElementEndInfo (
226
181
sourceCode : SourceCode ,
227
- node : AST . JSONNode | ESLintAST . Token ,
182
+ target : Target | { node : ESLintAST . Token } ,
228
183
) : {
229
184
// Trailing comma
230
185
comma : ESLintAST . Token | null ;
@@ -233,14 +188,15 @@ function getElementEndInfo(
233
188
// The last token of the target element
234
189
last : ESLintAST . Token | ESTree . Comment ;
235
190
} {
236
- const lastToken = getLastTokenOfNode ( sourceCode , node ) ;
237
- const afterToken = sourceCode . getTokenAfter ( lastToken ) ! ;
191
+ const afterToken = target . node
192
+ ? sourceCode . getTokenAfter ( getLastTokenOfNode ( sourceCode , target . node ) ) !
193
+ : target . after ;
238
194
if ( isNotCommaToken ( afterToken ) ) {
239
195
// If there is no comma, the element is the last element.
240
196
return {
241
197
comma : null ,
242
198
nextElement : null ,
243
- last : getLastTokenWithTrailingComments ( ) ,
199
+ last : getLastTokenWithTrailingComments ( sourceCode , target ) ,
244
200
} ;
245
201
}
246
202
const comma = afterToken ;
@@ -260,11 +216,13 @@ function getElementEndInfo(
260
216
return {
261
217
comma,
262
218
nextElement : null ,
263
- last : getLastTokenWithTrailingComments ( ) ,
219
+ last : getLastTokenWithTrailingComments ( sourceCode , target ) ,
264
220
} ;
265
221
}
266
222
267
- if ( node . loc . end . line === nextElement . loc . start . line ) {
223
+ const node = target . node ;
224
+
225
+ if ( node && node . loc . end . line === nextElement . loc . start . line ) {
268
226
// There is no line break between the target element and the next element.
269
227
return {
270
228
comma,
@@ -274,6 +232,7 @@ function getElementEndInfo(
274
232
}
275
233
// There are line breaks between the target element and the next element.
276
234
if (
235
+ node &&
277
236
node . loc . end . line < comma . loc . start . line &&
278
237
comma . loc . end . line < nextElement . loc . start . line
279
238
) {
@@ -289,29 +248,38 @@ function getElementEndInfo(
289
248
return {
290
249
comma,
291
250
nextElement,
292
- last : getLastTokenWithTrailingComments ( ) ,
251
+ last : getLastTokenWithTrailingComments ( sourceCode , target ) ,
293
252
} ;
253
+ }
294
254
295
- /**
296
- * Get the last token of the target element with trailing comments.
297
- */
298
- function getLastTokenWithTrailingComments ( ) {
299
- if ( lastToken == null ) return afterToken ;
300
- let last : ESLintAST . Token | ESTree . Comment = lastToken ;
301
- let after = sourceCode . getTokenAfter ( lastToken , {
255
+ /**
256
+ * Get the last token of the target element with trailing comments.
257
+ */
258
+ function getLastTokenWithTrailingComments (
259
+ sourceCode : SourceCode ,
260
+ target : Target | { node : ESLintAST . Token } ,
261
+ ) {
262
+ if ( ! target . node ) {
263
+ return sourceCode . getTokenBefore ( target . after , {
302
264
includeComments : true ,
303
265
} ) ! ;
304
- while (
305
- ( isCommentToken ( after ) || isComma ( after ) ) &&
306
- node . loc . end . line === after . loc ! . end . line
307
- ) {
308
- last = after ;
309
- after = sourceCode . getTokenAfter ( after , {
310
- includeComments : true ,
311
- } ) ! ;
312
- }
313
- return last ;
314
266
}
267
+ const node = target . node ;
268
+ let last : ESLintAST . Token | ESTree . Comment = getLastTokenOfNode (
269
+ sourceCode ,
270
+ node ,
271
+ ) ;
272
+ let after : ESLintAST . Token | ESTree . Comment | null ;
273
+ while (
274
+ ( after = sourceCode . getTokenAfter ( last , {
275
+ includeComments : true ,
276
+ } ) ) &&
277
+ ( isCommentToken ( after ) || isComma ( after ) ) &&
278
+ node . loc . end . line === after . loc ! . end . line
279
+ ) {
280
+ last = after ;
281
+ }
282
+ return last ;
315
283
}
316
284
317
285
/**
@@ -352,7 +320,7 @@ function getPrevElementInfo(
352
320
} ;
353
321
}
354
322
355
- const endInfo = getElementEndInfo ( sourceCode , prevElement ) ;
323
+ const endInfo = getElementEndInfo ( sourceCode , { node : prevElement } ) ;
356
324
357
325
return {
358
326
comma : endInfo . comma ,
0 commit comments