@@ -212,9 +212,34 @@ export function parseScriptFragment(
212
212
code : string ,
213
213
locationCalculator : LocationCalculator ,
214
214
parserOptions : ParserOptions ,
215
+ ) : ESLintExtendedProgram {
216
+ return parseScriptFragmentWithOption (
217
+ code ,
218
+ locationCalculator ,
219
+ parserOptions ,
220
+ )
221
+ }
222
+
223
+ /**
224
+ * Parse the given source code.
225
+ *
226
+ * @param code The source code to parse.
227
+ * @param locationCalculator The location calculator for fixLocations.
228
+ * @param parserOptions The parser options.
229
+ * @param processOptions The process options.
230
+ * @returns The result of parsing.
231
+ */
232
+ function parseScriptFragmentWithOption (
233
+ code : string ,
234
+ locationCalculator : LocationCalculator ,
235
+ parserOptions : ParserOptions ,
236
+ processOptions ?: {
237
+ preFixLocationProcess ?: ( result : ESLintExtendedProgram ) => void
238
+ } ,
215
239
) : ESLintExtendedProgram {
216
240
try {
217
241
const result = parseScript ( code , parserOptions )
242
+ processOptions ?. preFixLocationProcess ?.( result )
218
243
fixLocations ( result , locationCalculator )
219
244
return result
220
245
} catch ( err ) {
@@ -1259,19 +1284,38 @@ export function parseGenericExpression(
1259
1284
throwEmptyError ( locationCalculator , "a type parameter" )
1260
1285
}
1261
1286
1262
- try {
1263
- const result = parseScriptFragment (
1264
- `void function<${ code } >(){}` ,
1265
- locationCalculator . getSubCalculatorShift ( - 14 ) ,
1266
- { ...parserOptions , project : undefined } ,
1267
- )
1287
+ function getParams ( result : ESLintExtendedProgram ) {
1268
1288
const { ast } = result
1269
1289
const statement = ast . body [ 0 ] as ESLintExpressionStatement
1270
1290
const rawExpression = statement . expression as ESLintUnaryExpression
1271
1291
const classDecl = rawExpression . argument as ESLintClassExpression
1272
1292
const typeParameters = ( classDecl as TSESTree . ClassExpression )
1273
1293
. typeParameters
1274
- const params = typeParameters ?. params
1294
+ return typeParameters ?. params
1295
+ }
1296
+
1297
+ try {
1298
+ const rawParams : string [ ] = [ ]
1299
+ const scriptLet = `void function<${ code } >(){}`
1300
+ const result = parseScriptFragmentWithOption (
1301
+ scriptLet ,
1302
+ locationCalculator . getSubCalculatorShift ( - 14 ) ,
1303
+ { ...parserOptions , project : undefined } ,
1304
+ {
1305
+ preFixLocationProcess ( preResult ) {
1306
+ const params = getParams ( preResult )
1307
+ if ( params ) {
1308
+ for ( const param of params ) {
1309
+ rawParams . push (
1310
+ scriptLet . slice ( param . range [ 0 ] , param . range [ 1 ] ) ,
1311
+ )
1312
+ }
1313
+ }
1314
+ } ,
1315
+ } ,
1316
+ )
1317
+ const { ast } = result
1318
+ const params = getParams ( result )
1275
1319
1276
1320
if ( ! params || params . length === 0 ) {
1277
1321
return {
@@ -1300,12 +1344,7 @@ export function parseGenericExpression(
1300
1344
loc : { start : firstParam . loc . start , end : lastParam . loc . end } ,
1301
1345
parent : DUMMY_PARENT ,
1302
1346
params,
1303
- rawParams : params . map ( ( param ) =>
1304
- code . slice (
1305
- param . range [ 0 ] - typeParameters . range [ 0 ] - 1 ,
1306
- param . range [ 1 ] - typeParameters . range [ 0 ] - 1 ,
1307
- ) ,
1308
- ) ,
1347
+ rawParams,
1309
1348
}
1310
1349
1311
1350
// Modify parent.
0 commit comments