5
5
*/
6
6
import first from "lodash/first"
7
7
import last from "lodash/last"
8
+ import sortedIndexBy from "lodash/sortedIndexBy"
8
9
import {
9
10
traverseNodes ,
10
11
ESLintArrayPattern ,
@@ -18,7 +19,6 @@ import {
18
19
ESLintFunctionExpression ,
19
20
ESLintPattern ,
20
21
ESLintProgram ,
21
- ESLintSpreadElement ,
22
22
ESLintVariableDeclaration ,
23
23
ESLintUnaryExpression ,
24
24
Node ,
@@ -150,6 +150,30 @@ function removeByName(references: Reference[], name: string): void {
150
150
}
151
151
}
152
152
153
+ /**
154
+ * Get the comma token before a given node.
155
+ * @param tokens The token list.
156
+ * @param node The node to get the comma before this node.
157
+ * @returns The comma token.
158
+ */
159
+ function getCommaTokenBeforeNode ( tokens : Token [ ] , node : Node ) : Token | null {
160
+ let tokenIndex = sortedIndexBy (
161
+ tokens ,
162
+ { range : node . range } ,
163
+ t => t . range [ 0 ] ,
164
+ )
165
+
166
+ while ( tokenIndex >= 0 ) {
167
+ const token = tokens [ tokenIndex ]
168
+ if ( token . type === "Punctuator" && token . value === "," ) {
169
+ return token
170
+ }
171
+ tokenIndex -= 1
172
+ }
173
+
174
+ return null
175
+ }
176
+
153
177
/**
154
178
* Throw syntax error for empty.
155
179
* @param locationCalculator The location calculator to get line/column.
@@ -172,22 +196,19 @@ function throwEmptyError(
172
196
}
173
197
174
198
/**
175
- * Throw syntax error for empty .
199
+ * Throw syntax error for unexpected token .
176
200
* @param locationCalculator The location calculator to get line/column.
201
+ * @param name The token name.
202
+ * @param token The token object to get that location.
177
203
*/
178
- function throwUnexpectedSpreadElementError (
179
- locationCalculator : LocationCalculator ,
180
- node : ESLintSpreadElement ,
181
- ) : never {
182
- const loc = locationCalculator . getLocation ( node . start || 0 )
204
+ function throwUnexpectedTokenError ( name : string , token : Node | Token ) : never {
183
205
const err = new ParseError (
184
- " Unexpected spread element." ,
206
+ ` Unexpected token ' ${ name } '.` ,
185
207
undefined ,
186
- 0 ,
187
- loc . line ,
188
- loc . column ,
208
+ token . range [ 0 ] ,
209
+ token . loc . start . line ,
210
+ token . loc . start . column ,
189
211
)
190
- locationCalculator . fixErrorLocation ( err )
191
212
192
213
throw err
193
214
}
@@ -364,13 +385,17 @@ export function parseExpression(
364
385
return throwEmptyError ( locationCalculator , "an expression" )
365
386
}
366
387
if ( expression && expression . type === "SpreadElement" ) {
367
- return throwUnexpectedSpreadElementError (
368
- locationCalculator . getSubCalculatorAfter ( - 2 ) ,
369
- expression ,
388
+ return throwUnexpectedTokenError ( "..." , expression )
389
+ }
390
+ if ( callExpression . arguments [ 1 ] ) {
391
+ const node = callExpression . arguments [ 1 ]
392
+ return throwUnexpectedTokenError (
393
+ "," ,
394
+ getCommaTokenBeforeNode ( tokens , node ) || node ,
370
395
)
371
396
}
372
397
373
- // Remvoe parens.
398
+ // Remove parens.
374
399
tokens . shift ( )
375
400
tokens . shift ( )
376
401
tokens . pop ( )
0 commit comments