acorn-typescript icon indicating copy to clipboard operation
acorn-typescript copied to clipboard

Unexpected token at directly exported interface declaration

Open jsinterface opened this issue 1 year ago • 5 comments

back with another case study:

parsed file: https://github.com/socketio/socket.io/blob/main/lib/socket.ts#L98

/* The handshake details */
export interface Handshake {
  /* The headers sent as part of the handshake  */
  headers: IncomingHttpHeaders;
         ^
  /* The date of creation (as string)*/
  time: string;

error:

SyntaxError: Unexpected token (98:9)
    at pp$4.raise (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:2472:13)
    at TypeScriptParser.raiseCommonCheck (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:5208:44)
    at TypeScriptParser.raise (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:5214:29)
    at pp$9.unexpected (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:509:8)
    at pp$9.expect (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:506:26)
    at TypeScriptParser.jsx_parseExpressionContainer (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:823:18)
    at TypeScriptParser.jsx_parseElementAt (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:885:48)
    at TypeScriptParser.jsx_parseElement (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:914:25)
    at TypeScriptParser.parseExprAtom (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:3641:33)
    at pp$5.parseExprSubscripts (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:1841:19)

should be a regular TSInterfaceDeclaration, but seems to be mistaken for some reason, only possible one seeming to be the several comments lying around... edit: that might be stupid, the direct export seems more suspect.. although weird if only now was its first occurrence.

jsinterface avatar Feb 08 '24 19:02 jsinterface

a full stack trace:

Failed to patriate typescript "file:///mnt/chromeos/GoogleDrive/MyDrive/blik/rauch_2010_socketio/0/lib/socket.ts" due to SyntaxError: Unexpected token (98:9)
    at pp$4.raise (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:2473:13)
    at TypeScriptParser.raiseCommonCheck (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:5208:44)
    at TypeScriptParser.raise (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:5214:29)
    at pp$9.unexpected (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:509:8)
    at pp$9.expect (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:506:26)
    at TypeScriptParser.jsx_parseExpressionContainer (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:823:18)
    at TypeScriptParser.jsx_parseElementAt (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:885:48)
    at TypeScriptParser.jsx_parseElement (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:914:25)
    at TypeScriptParser.parseExprAtom (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:3641:33)
    at pp$5.parseExprSubscripts (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:1841:19)
    at pp$5.parseMaybeUnary (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:1818:17)
    at pp$5.parseExprOps (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:1765:19)
    at TypeScriptParser.parseMaybeConditional (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:3828:33)
    at TypeScriptParser.parseMaybeAssignOrigin (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:4226:33)
    at file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:4268:52
    at TypeScriptParser.tryParse (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:1198:34)
    at TypeScriptParser.parseMaybeAssign (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:4268:32)
    at TypeScriptParser.parseExprList (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:4718:36)
    at TypeScriptParser.parseNew (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:3322:43)
    at pp$5.parseExprAtom (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:2006:19)
    at TypeScriptParser.parseExprAtom (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:3669:34)
    at pp$5.parseExprSubscripts (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:1841:19)
    at pp$5.parseMaybeUnary (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:1818:17)
    at pp$5.parseExprOps (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:1765:19)
    at TypeScriptParser.parseMaybeConditional (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:3828:33)
    at TypeScriptParser.parseMaybeAssignOrigin (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:4226:33)
    at TypeScriptParser.parseMaybeAssign (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:4288:33)
    at pp$8.parseVar (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:916:24)
    at TypeScriptParser.parseVarStatement (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:3729:23)
    at pp$8.parseStatement (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:617:19)
    at TypeScriptParser.parseStatement (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:3778:30)
    at TypeScriptParser.parseExportDeclaration (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:3880:26)
    at TypeScriptParser.parseExport (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:3556:49)
    at pp$8.parseStatement (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:638:76)
    at TypeScriptParser.parseStatement (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:3778:30)
    at pp$8.parseTopLevel (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:543:21)
    at TypeScriptParser.parse (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/haverbeke_2012_acorn.js:392:17)
    at TypeScriptParser.parse (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/tyrealhu_2023_acorn_typescript.js:5069:31)
    at parse (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/Blik_2023_meta.js:22:19)
    at async Promise.all (index 0)
    at async Promise.all (index 0)
    at async Promise.all (index 0)
    at async Promise.all (index 0)
    at async load (file:///mnt/chromeos/GoogleDrive/MyDrive/blik/Blik_2023_interface.js:491:13)
    at async nextLoad (node:internal/modules/esm/hooks:749:22)
    at async Hooks.load (node:internal/modules/esm/hooks:382:20)
    at async handleMessage (node:internal/modules/esm/worker:199:18) {
  pos: 2566,
  loc: Position { line: 98, column: 9 },
  raisedAt: 2567
}

jsinterface avatar Feb 08 '24 20:02 jsinterface

Could you show me the code that how you use this plugin?

TyrealHu avatar Feb 12 '24 09:02 TyrealHu

in short:

 export async function parse(source, syntax = "javascript", options = {})
{// interpret language syntax.
 let {Parser}=await import("./haverbeke_2012_acorn.js");
 let typescript=syntax==="typescript";
 if(typescript)
 Parser=await import("./tyrealhu_2023_acorn_typescript.js").then(({default:plugin})=>
 Parser.extend(plugin({dts:options.source?.endsWith(".d.ts")})));
 let comments=[];
 let scope=Parser.parse(source,{ecmaVersion:2022,sourceType:"module",onComment:comments,locations:typescript});
  //              ^
  // location 22:19 in meta.js from the stack trace
 comments.map(comment=>
 Object.assign(comment,{type:comment.type+"Comment"})).forEach(comment=>
 route(scope,comment,path));
 return scope;
}

- i use your plugin as a bundle created with rollup cli. just like acorn, except that's generated with programmatic api. the reason this plugin is bundled manually is i use the plugin to parse typescript, but it's written in typescript itself - like every ts parser out there. TSC is a singular bottleneck to the typescript ecosystem. It parses tons of other files well, so the bundle integrity has always seemed fine (no errors or warnings during bundling either).

why, are you unable to reproduce perhaps?

jsinterface avatar Feb 13 '24 08:02 jsinterface

in short:

 export async function parse(source, syntax = "javascript", options = {})
{// interpret language syntax.
 let {Parser}=await import("./haverbeke_2012_acorn.js");
 let typescript=syntax==="typescript";
 if(typescript)
 Parser=await import("./tyrealhu_2023_acorn_typescript.js").then(({default:plugin})=>
 Parser.extend(plugin({dts:options.source?.endsWith(".d.ts")})));
 let comments=[];
 let scope=Parser.parse(source,{ecmaVersion:2022,sourceType:"module",onComment:comments,locations:typescript});
  //              ^
  // location 22:19 in meta.js from the stack trace
 comments.map(comment=>
 Object.assign(comment,{type:comment.type+"Comment"})).forEach(comment=>
 route(scope,comment,path));
 return scope;
}
  • i use your plugin as a bundle created with rollup cli. just like acorn, except that's generated with programmatic api. the reason this plugin is bundled manually is i use the plugin to parse typescript, but it's written in typescript itself - like every ts parser out there. TSC is a singular bottleneck to the typescript ecosystem. It parses tons of other files well, so the bundle integrity has always seemed fine (no errors or warnings during bundling either).

why, are you unable to reproduce perhaps?

I need the code that you want to parse. This is a plugin of acorn. So we just support to parse the code string of one file.

TyrealHu avatar Mar 04 '24 03:03 TyrealHu

oh ok sorry, your question wasn't clear grammatically. I had the exact line in the parsed file that is throwing the error linked in the opening comment:

parsed file: https://github.com/socketio/socket.io/blob/main/lib/socket.ts#L98

jsinterface avatar Mar 04 '24 15:03 jsinterface

found the reason for this, it's actually the const type assertion syntax above, which is in irredeemable conflict with jsx, as acknowledged before here: https://github.com/TyrealHu/acorn-typescript/issues/47 my solution for that is to transform the sources replacing type assertions with type casts. closing this as duplicate.

jsinterface avatar May 31 '24 06:05 jsinterface