sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
78.54k stars 4.12k forks source link

Consider switching to a more mature parser #13439

Open ottomated opened 2 hours ago

ottomated commented 2 hours ago

Describe the problem

There are many issues that have acorn-typescript as the upstream cause (#13179, #13125, #13409, #13188, more), but acorn-typescript is abandoned. It also has several open issues that would need to be addressed once forked or collaboration access is granted.

Describe the proposed solution

One of the following:

My vote would be for the latter. The downsides (bigger dependencies and changes to svelte's source) are outweighed by the long-term advantage of using a more reliable library.

Importance

nice to have

dummdidumm commented 2 hours ago

We'll fork in the short term but keep an eye out for how the other variants mature over time. The main blocker for adopting other parsers has been that AFAIK no other has a feature like Acorn's "parse expression", which we need for JS-like snippets within the template.

ottomated commented 32 minutes ago

Looking into this. SWC doesn't expose a function like that but their Rust API exposes a parse_epxr() function that can take something like () => foo++}>increment</button> and spit out an AST (below).

This is what you need, right? In order to make this work, it would take a custom rust package that imports swc_ecma_parser and exposes the custom methods you need to call from JS. This could either be wasm (probably easier) or node bindings (probably faster, but requires different binaries for each platform).

{
  "type": "ArrowFunctionExpression",
  "span": {
    "start": 1,
    "end": 12
  },
  "ctxt": 0,
  "params": [],
  "body": {
    "type": "UpdateExpression",
    "span": {
      "start": 7,
      "end": 12
    },
    "operator": "++",
    "prefix": false,
    "argument": {
      "type": "Identifier",
      "span": {
        "start": 7,
        "end": 10
      },
      "ctxt": 0,
      "value": "foo",
      "optional": false
    }
  },
  "async": false,
  "generator": false,
  "typeParameters": null,
  "returnType": null
}