@@ -14,9 +14,11 @@ import {
1414 ESLintExtendedProgram ,
1515 ESLintForInStatement ,
1616 ESLintForOfStatement ,
17+ ESLintFunctionExpression ,
1718 ESLintPattern ,
1819 ESLintProgram ,
1920 ESLintVariableDeclaration ,
21+ ESLintUnaryExpression ,
2022 Node ,
2123 ParseError ,
2224 Reference ,
@@ -25,6 +27,7 @@ import {
2527 VElement ,
2628 VForExpression ,
2729 VOnExpression ,
30+ VSlotScopeExpression ,
2831} from "../ast"
2932import { debug } from "../common/debug"
3033import { LocationCalculator } from "../common/location-calculator"
@@ -216,7 +219,12 @@ function parseScriptFragment(
216219 * The result of parsing expressions.
217220 */
218221export interface ExpressionParseResult {
219- expression : ESLintExpression | VForExpression | VOnExpression | null
222+ expression :
223+ | ESLintExpression
224+ | VForExpression
225+ | VOnExpression
226+ | VSlotScopeExpression
227+ | null
220228 tokens : Token [ ]
221229 comments : Token [ ]
222230 references : Reference [ ]
@@ -494,3 +502,64 @@ export function parseVOnExpression(
494502 return throwErrorAsAdjustingOutsideOfCode ( err , code , locationCalculator )
495503 }
496504}
505+
506+ /**
507+ * Parse the source code of `slot-scope` directive.
508+ * @param code The source code of `slot-scope` directive.
509+ * @param locationCalculator The location calculator for the inline script.
510+ * @param parserOptions The parser options.
511+ * @returns The result of parsing.
512+ */
513+ export function parseSlotScopeExpression (
514+ code : string ,
515+ locationCalculator : LocationCalculator ,
516+ parserOptions : any ,
517+ ) : ExpressionParseResult {
518+ debug ( '[script] parse slot-scope expression: "void function(%s) {}"' , code )
519+
520+ if ( code . trim ( ) === "" ) {
521+ throwEmptyError (
522+ locationCalculator ,
523+ "an identifier or an array/object pattern" ,
524+ )
525+ }
526+
527+ try {
528+ const ast = parseScriptFragment (
529+ `void function(${ code } ) {}` ,
530+ locationCalculator . getSubCalculatorAfter ( - 14 ) ,
531+ parserOptions ,
532+ ) . ast
533+ const tokens = ast . tokens || [ ]
534+ const comments = ast . comments || [ ]
535+ const scope = analyzeVariablesAndExternalReferences ( ast , parserOptions )
536+ const references = scope . references
537+ const variables = scope . variables
538+ const statement = ast . body [ 0 ] as ESLintExpressionStatement
539+ const rawExpression = statement . expression as ESLintUnaryExpression
540+ const functionDecl = rawExpression . argument as ESLintFunctionExpression
541+ const id = functionDecl . params [ 0 ]
542+ const expression : VSlotScopeExpression = {
543+ type : "VSlotScopeExpression" ,
544+ range : [ id . range [ 0 ] , id . range [ 1 ] ] ,
545+ loc : { start : id . loc . start , end : id . loc . end } ,
546+ parent : DUMMY_PARENT ,
547+ id,
548+ }
549+
550+ // Modify parent.
551+ id . parent = expression
552+
553+ // Remvoe `void` `function` `(` `)` `{` `}`.
554+ tokens . shift ( )
555+ tokens . shift ( )
556+ tokens . shift ( )
557+ tokens . pop ( )
558+ tokens . pop ( )
559+ tokens . pop ( )
560+
561+ return { expression, tokens, comments, references, variables }
562+ } catch ( err ) {
563+ return throwErrorAsAdjustingOutsideOfCode ( err , code , locationCalculator )
564+ }
565+ }
0 commit comments