TyrealHu / acorn-typescript

Alternative, TypeScript parser
https://www.npmjs.com/package/acorn-typescript?activeTab=readme
MIT License
145 stars 18 forks source link

SyntaxError: Unexpected type cast in parameter position at left side of an assignment #44

Closed bpstrngr closed 1 year ago

bpstrngr commented 1 year ago

Parsed file: https://github.com/rollup/rollup/blob/master/src/ast/nodes/CatchClause.ts#L26

export default class CatchClause extends NodeBase {
        declare body: BlockStatement;
        declare param: PatternNode | null;
        declare preventChildBlockScope: true;
        declare scope: CatchScope;
        declare type: NodeType.tCatchClause;
        createScope(parentScope: Scope): void {
                this.scope = new CatchScope(parentScope, this.context);
        }
        parseNode(esTreeNode: GenericEsTreeNode): void {
                const { param } = esTreeNode;
                if (param) {
                        (this.param as GenericEsTreeNode) = new (this.context.getNodeConstructor(param.type))(
//                      ^
                                param, this,this.scope
                        );
                        this.param!.declare('parameter', UNKNOWN_EXPRESSION);
                }
                super.parseNode(esTreeNode);
        }
}

Result:

SyntaxError: Unexpected type cast in parameter position. (26:4)
    at pp.raise (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn/0/acorn/src/location.js:15:13)
    at TypeScriptParser.raiseCommonCheck (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:5008:44)
    at TypeScriptParser.raise (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:5014:29)
    at TypeScriptParser.toAssignable (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:4294:34)
    at TypeScriptParser.parseMaybeAssignOrigin (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:4037:37)
    at TypeScriptParser.parseMaybeAssign (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:4092:33)
    at pp.parseExpression (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn/0/acorn/src/expression.js:98:19)
    at pp.parseStatement (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn/0/acorn/src/statement.js:151:45)
    at TypeScriptParser.parseStatement (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:3582:30)
    at pp.parseBlock (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn/0/acorn/src/statement.js:436:21) {
  pos: 977,
  loc: Position { line: 26, column: 4 },
  raisedAt: 1011
}
bpstrngr commented 1 year ago

By the way, if you didn't mind, would you perhaps have a little insight to offer about how you figure where to intercept the parser inheritence always so quickly? I suppose it's mostly about being familiar with the control flow (such as perhaps calling some type cast parse method in parseMaybeAssignOrigin here, either in error handling or heuristically), but if therein might be some tricks that help determining the TS/vanilla bifurcations it could help taking a stab at PR-s.

TyrealHu commented 1 year ago

fix in version acorn-typescript@1.4.6