mck89 / peast

JavaScript parser written in PHP that generates AST from your code according to ECMAScript specification
BSD 3-Clause "New" or "Revised" License
175 stars 21 forks source link

Typescript Support #28

Closed Eric-P7 closed 4 years ago

Eric-P7 commented 4 years ago

Would it be feasible to add TypeScript support?

Then it'd be easy for me convert typescript to javascript by walking the AST and omitting the typescript only features.

This could be used with a .htaccess rewrite to convert typescript to javascript on the fly, allowing for:

The http request to main.ts would return the transpiled javascript.

That script could cache the generated .js files. On a new request they would only be generated if the .ts file was newer than the generated .js file.

I think this could become a very popular and easy way to serve JavaScript from TypeScript on any web server with php, allowing developers to skip complicated node.js build configurations.

mck89 commented 4 years ago

Actually i've tought about it in the past, but there are 2 main problems:

  1. There is no documentation about how TypeScript syntax integrates with EcmaScript syntax and that makes the development process really hard. I found this documentation that is exactly what is needed but it's not mantained anymore and it's outdated.
  2. Estree does not provide interfaces for TypeScript syntax and that's another big problem since it could save a lot of headhaches in the definition of an abstract syntax tree

I didn't want to implement JSX too but, when it was requested in another issue, i saw that there was all the necessary documentation to proceed, so i had not excuses. Another big difference is that JSX implements only one new syntax, while TS is a lot more complicated and it's really a big amount of work for one person.

I close the issue for now, but i will revaluate it if some documentation will come out.

tyrann0us commented 2 years ago

Hi @mck89, just following up on this one. Has something changed in the two years since you closed this issue? Is TypeScript support something you'd consider adding now?

Allow me to give you some context: In WordPress, the so-called "block editor" is written in React and more and more developers use TypeScript (.tsx) to develop React components and blocks for it. Now, there are translation functions (refer here for details, PHP counterpart details here) that allow you to translate strings in your components. For this, all these strings must be "extracted" from the source code. For the WordPress context, there exists a tool called GlotPress that provides a translation UI. The actual string extraction is done by Traduttore, which in turn uses the official WordPress CLI (WP-CLI) to parse the source code.

And here's where it's getting interesting: WP-CLI's string extraction command (wp i18n make-pot) uses your great library to parse JS files. But since it doesn't support TypeScript, developers are not able to extract translatable strings from their source code if they use TypeScript.

Please see this issue for details: https://github.com/wp-cli/i18n-command/issues/176

tyrann0us commented 2 years ago

Hi @mck89, admittedly, I don’t know anything about language parsers and ASTs. However, from a quick Google search it looks like https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/typescript-estree is a npm-based TypeScript parser. Could this theoretically act as a source for TypeScript support in Peast? Basically porting it to a PHP version? Thanks!

mck89 commented 2 years ago

Well that can be useful for the missing ESTree documentation, but that was just the easy part. Typescript still does not provide a documentation of how it is mixed with EcmaScript and reverse engineering it from another project is really hard, especially for that project that is a plugin of eslint, so i should do reverse engineering on two big projects.

Right now i don't think i can implement it, the lack of documentation is a big problem and i can't spend too much time on this project.

That said, i don't mean that i can't be done, but right now i can't implement it. As always i'm open to receive any pull request if someone wants to contribute.