fkling / astexplorer

A web tool to explore the ASTs generated by various parsers.
https://astexplorer.net/
MIT License
6.21k stars 739 forks source link

[BUG] SWC spans are broken with multi-byte offsets and accumulating offsets #708

Open Yash-Singh1 opened 10 months ago

Yash-Singh1 commented 10 months ago

Describe the bug A clear and concise description of what the bug is.

Multi-byte offsets

Currently, if you try to insert a multi-byte character in your code on AST Explorer, this eats up more space in the span.

Screenshot 2024-01-15 at 3 03 56 PM

This is related to the fact that the span values in SWC refer to byte positions instead of string indices: https://github.com/swc-project/swc/issues/1366#issuecomment-1576294504

Code:

let a = {
  '❤️': 'hi'
}

console.log(a)

Reproducing:

Accumalating Offsets

https://github.com/fkling/astexplorer/assets/53054099/1baefef0-a5cd-401e-8fc5-4229d2b82def

The above issue is related to another bug in SWC that results in spans being accumalated. Internally this is done to get their sourcemap functionality working, but to workaround this, you will need to either:

Browser (please complete the following information):

astexplorer settings:

Additional context Add any other context about the problem here.

Related to https://github.com/swc-project/swc/issues/1366

BTW thank you so much for making AST Explorer a thing, it helps me out a lot in my daily workflow :)

I can open up a PR to fix this if you want, bcuz I got these problems working in another project I am building that uses swc.