@@ -212,9 +212,34 @@ export function parseScriptFragment(
212212 code : string ,
213213 locationCalculator : LocationCalculator ,
214214 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+ } ,
215239) : ESLintExtendedProgram {
216240 try {
217241 const result = parseScript ( code , parserOptions )
242+ processOptions ?. preFixLocationProcess ?.( result )
218243 fixLocations ( result , locationCalculator )
219244 return result
220245 } catch ( err ) {
@@ -1259,19 +1284,38 @@ export function parseGenericExpression(
12591284 throwEmptyError ( locationCalculator , "a type parameter" )
12601285 }
12611286
1262- try {
1263- const result = parseScriptFragment (
1264- `void function<${ code } >(){}` ,
1265- locationCalculator . getSubCalculatorShift ( - 14 ) ,
1266- { ...parserOptions , project : undefined } ,
1267- )
1287+ function getParams ( result : ESLintExtendedProgram ) {
12681288 const { ast } = result
12691289 const statement = ast . body [ 0 ] as ESLintExpressionStatement
12701290 const rawExpression = statement . expression as ESLintUnaryExpression
12711291 const classDecl = rawExpression . argument as ESLintClassExpression
12721292 const typeParameters = ( classDecl as TSESTree . ClassExpression )
12731293 . 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 )
12751319
12761320 if ( ! params || params . length === 0 ) {
12771321 return {
@@ -1300,12 +1344,7 @@ export function parseGenericExpression(
13001344 loc : { start : firstParam . loc . start , end : lastParam . loc . end } ,
13011345 parent : DUMMY_PARENT ,
13021346 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,
13091348 }
13101349
13111350 // Modify parent.
0 commit comments