fkling / astexplorer

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

jscodeshift + typescript parser do not match or have the same AST #628

Open NullVoxPopuli opened 2 years ago

NullVoxPopuli commented 2 years ago

Describe the bug This came out of trying to force jscodeshift to work with property decorators somehow. The gist is that when selecting typescript for the AST debugger (top half), and setting ts to the parser to jscodeshift, the AST doesn't match.

For example, properties in the top half are called PropertyDeclarations, yet when you .find(j.PropertyDeclaration), nothing is found in jscodeshift.

To Reproduce https://astexplorer.net/#/gist/a8ffcb056b86f73e6b23bd8a8b08e69e/094a53e0abbfb27ae0e4ceae6577e01776584fdb open console to see that properties and methods are not found

Expected behavior I feel like certain combinations of parsers / transformers shouldn't be allowed? maybe/

Browser (please complete the following information): n/a

astexplorer settings:

Additional context Add any other context about the problem here.

fkling commented 2 years ago

Even though jscodeshift lets you choose Typescript as parser, under the hood it still uses recast (or at least it did when I worked on it). Try selecting recast instead and configure it to use Typescript as parser. You will still need to update your script to use the correct node types.

But yes, this is a known limitation: The parser and the transformer are completely independent, which means you can select any parser whether the transformer uses it or not. I had started rewriting astexplorer a while ago and in that version transformers would expose a parsing method themselves so that the AST you see is actually the AST the transformer works with. Not sure whether it's possible to do that for every transformer though, some might not expose just a parsing API. Then there are also those transformers that can accept any external parser (or their AST) as long as it conforms to a specific interface.