You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1 lines
13 KiB

{"version":3,"names":["_parseError","require","kExpression","kMaybeArrowParameterDeclaration","kMaybeAsyncArrowParameterDeclaration","kParameterDeclaration","ExpressionScope","constructor","type","canBeArrowParameterDeclaration","isCertainlyParameterDeclaration","ArrowHeadParsingScope","declarationErrors","Map","recordDeclarationError","ParsingErrorClass","at","index","set","clearDeclarationError","delete","iterateErrors","iterator","forEach","ExpressionScopeHandler","parser","stack","enter","scope","push","exit","pop","recordParameterInitializerError","toParseError","node","origin","loc","start","i","length","raise","recordArrowParameterBindingError","error","recordAsyncArrowParametersError","Errors","AwaitBindingIdentifier","validateAsPattern","currentScope","exports","default","newParameterDeclarationScope","newArrowHeadScope","newAsyncArrowScope","newExpressionScope"],"sources":["../../src/util/expression-scope.ts"],"sourcesContent":["import { Errors, type ParseErrorConstructor } from \"../parse-error\";\nimport type { Position } from \"./location\";\nimport type { Node } from \"../types\";\nimport type Tokenizer from \"../tokenizer\";\n\n/**\n * @module util/expression-scope\n\nExpressionScope is used to track declaration errors in these ambiguous patterns:\n\n- CoverParenthesizedExpressionAndArrowParameterList\n e.g. we don't know if `({ x })` is an parenthesized expression or an\n arrow function parameters until we see an `=>` after `)`.\n\n- CoverCallExpressionAndAsyncArrowHead\n e.g. we don't know if `async({ x })` is a call expression or an async arrow\n function parameters until we see an `=>` after `)`\n\nThe following declaration errors (@see parser-errors/standard) will be recorded in\nsome expression scopes and thrown later when we know what the ambiguous pattern is\n\n- AwaitBindingIdentifier\n- AwaitExpressionFormalParameter\n- YieldInParameter\n- InvalidParenthesizedAssignment when parenthesized is an identifier\n\nThere are four different expression scope\n- Expression\n A general scope that represents program / function body / static block. No errors\n will be recorded nor thrown in this scope.\n\n- MaybeArrowParameterDeclaration\n A scope that represents ambiguous arrow head e.g. `(x)`. Errors will be recorded\n alongside parent scopes and thrown when `ExpressionScopeHandler#validateAsPattern`\n is called.\n\n- MaybeAsyncArrowParameterDeclaration\n A scope that represents ambiguous async arrow head e.g. `async(x)`. Errors will\n be recorded alongside parent scopes and thrown when\n `ExpressionScopeHandler#validateAsPattern` is called.\n\n- ParameterDeclaration\n A scope that represents unambiguous function parameters `function(x)`. Errors\n recorded in this scope will be thrown immediately. No errors will be recorded in\n this scope.\n\n// @see {@link https://docs.google.com/document/d/1FAvEp9EUK-G8kHfDIEo_385Hs2SUBCYbJ5H-NnLvq8M|V8 Expression Scope design docs}\n */\n\nconst kExpression = 0,\n kMaybeArrowParameterDeclaration = 1,\n kMaybeAsyncArrowParameterDeclaration = 2,\n kParameterDeclaration = 3;\n\ntype ExpressionScopeType = 0 | 1 | 2 | 3;\n\nclass ExpressionScope {\n type: ExpressionScopeType;\n\n constructor(type: ExpressionScopeType = kExpression) {\n this.type = type;\n }\n\n canBeArrowParameterDeclaration(): this is ArrowHeadParsingScope {\n return (\n this.type === kMaybeAsyncArrowParameterDeclaration ||\n this.type === kMaybeArrowParameterDeclaration\n );\n }\n\n isCertainlyParameterDeclaration() {\n return this.type === kParameterDeclaration;\n }\n}\n\ntype ArrowHeadParsingParameterInitializerError =\n | typeof Errors.AwaitExpressionFormalParameter\n | typeof Errors.YieldInParameter;\ntype ArrowHeadParsingDeclarationError =\n | ArrowHeadParsingParameterInitializerError\n | typeof Errors.InvalidParenthesizedAssignment\n | typeof Errors.AwaitBindingIdentifier;\n\nclass ArrowHeadParsingScope extends ExpressionScope {\n declarationErrors: Map<number, [ParseErrorConstructor<{}>, Position]> =\n new Map();\n constructor(type: 1 | 2) {\n super(type);\n }\n recordDeclarationError(\n ParsingErrorClass: ParseErrorConstructor<{}>,\n {\n at,\n }: {\n at: Position;\n },\n ) {\n const index = at.index;\n\n this.declarationErrors.set(index, [ParsingErrorClass, at]);\n }\n clearDeclarationError(index: number) {\n this.declarationErrors.delete(index);\n }\n iterateErrors(\n iterator: (a: [ArrowHeadParsingDeclarationError, Position]) => void,\n ) {\n this.declarationErrors.forEach(iterator);\n }\n}\n\nexport default class ExpressionScopeHandler {\n parser: Tokenizer;\n stack: Array<ExpressionScope> = [new ExpressionScope()];\n\n constructor(parser: Tokenizer) {\n this.parser = parser;\n }\n enter(scope: ExpressionScope) {\n this.stack.push(scope);\n }\n\n exit() {\n this.stack.pop();\n }\n\n /**\n * Record likely parameter initializer errors\n *\n * When current scope is a ParameterDeclaration, the error will be thrown immediately,\n * otherwise it will be recorded to any ancestry MaybeArrowParameterDeclaration and\n * MaybeAsyncArrowParameterDeclaration scope until an Expression scope is seen.\n */\n recordParameterInitializerError(\n toParseError: ArrowHeadParsingParameterInitializerError,\n {\n at: node,\n }: {\n at: Node;\n },\n ): void {\n const origin = { at: node.loc.start };\n const { stack } = this;\n let i = stack.length - 1;\n let scope: ExpressionScope = stack[i];\n while (!scope.isCertainlyParameterDeclaration()) {\n if (scope.canBeArrowParameterDeclaration()) {\n scope.recordDeclarationError(toParseError, origin);\n } else {\n /*:: invariant(scope.type == kExpression) */\n // Type-Expression is the boundary where initializer error can populate to\n return;\n }\n scope = stack[--i];\n }\n this.parser.raise(toParseError, origin);\n }\n\n /**\n * Record errors that must be thrown if the current pattern ends up being an arrow\n * function parameter. This is used to record parenthesized identifiers, and to record\n * \"a as T\" and \"<T> a\" type assertions when parsing typescript.\n *\n * A parenthesized identifier (or type assertion) in LHS can be ambiguous because the assignment\n * can be transformed to an assignable later, but not vice versa:\n * For example, in `([(a) = []] = []) => {}`, we think `(a) = []` is an LHS in `[(a) = []]`,\n * an LHS within `[(a) = []] = []`. However the LHS chain is then transformed by toAssignable,\n * and we should throw assignment `(a)`, which is only valid in LHS. Hence we record the\n * location of parenthesized `(a)` to current scope if it is one of MaybeArrowParameterDeclaration\n * and MaybeAsyncArrowParameterDeclaration\n *\n * Unlike `recordParameterInitializerError`, we don't record to ancestry scope because we\n * validate arrow head parsing scope before exit, and then the LHS will be unambiguous:\n * For example, in `( x = ( [(a) = []] = [] ) ) => {}`, we should not record `(a)` in `( x = ... ) =>`\n * arrow scope because when we finish parsing `( [(a) = []] = [] )`, it is an unambiguous assignment\n * expression and can not be cast to pattern\n */\n recordArrowParameterBindingError(\n error: ParseErrorConstructor<{}>,\n {\n at: node,\n }: {\n at: Node;\n },\n ): void {\n const { stack } = this;\n const scope: ExpressionScope = stack[stack.length - 1];\n const origin = { at: node.loc.start };\n if (scope.isCertainlyParameterDeclaration()) {\n this.parser.raise(error, origin);\n } else if (scope.canBeArrowParameterDeclaration()) {\n scope.recordDeclarationError(error, origin);\n } else {\n return;\n }\n }\n\n /**\n * Record likely async arrow parameter errors\n *\n * Errors will be recorded to any ancestry MaybeAsyncArrowParameterDeclaration\n * scope until an Expression scope is seen.\n */\n recordAsyncArrowParametersError({ at }: { at: Position }): void {\n const { stack } = this;\n let i = stack.length - 1;\n let scope: ExpressionScope = stack[i];\n while (scope.canBeArrowParameterDeclaration()) {\n if (scope.type === kMaybeAsyncArrowParameterDeclaration) {\n scope.recordDeclarationError(Errors.AwaitBindingIdentifier, { at });\n }\n scope = stack[--i];\n }\n }\n\n validateAsPattern(): void {\n const { stack } = this;\n const currentScope = stack[stack.length - 1];\n if (!currentScope.canBeArrowParameterDeclaration()) return;\n currentScope.iterateErrors(([toParseError, loc]) => {\n this.parser.raise(toParseError, { at: loc });\n // iterate from parent scope\n let i = stack.length - 2;\n let scope = stack[i];\n while (scope.canBeArrowParameterDeclaration()) {\n scope.clearDeclarationError(loc.index);\n scope = stack[--i];\n }\n });\n }\n}\n\nexport function newParameterDeclarationScope() {\n return new ExpressionScope(kParameterDeclaration);\n}\n\nexport function newArrowHeadScope() {\n return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);\n}\n\nexport function newAsyncArrowScope() {\n return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);\n}\n\nexport function newExpressionScope() {\n return new ExpressionScope();\n}\n"],"mappings":";;;;;;;;;;AAAA,IAAAA,WAAA,GAAAC,OAAA;AAiDA,MAAMC,WAAW,GAAG,CAAC;EACnBC,+BAA+B,G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