Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AST instead of ejs templates for generators #2113

Closed
clayrisser opened this issue Dec 4, 2018 · 4 comments
Closed

Use AST instead of ejs templates for generators #2113

clayrisser opened this issue Dec 4, 2018 · 4 comments
Labels

Comments

@clayrisser
Copy link
Contributor

I think it would be better to generate code from an abstract syntax tree rather than using templates.

In the past, I have found the @babel/generator to be a solid choice for generating code from AST, however, that would not work for typescript. Perhaps, the getTextOfNode() method in the typescript compiler utilities would do the job.

https://github.com/Microsoft/TypeScript/blob/aa3734c14834afb454c49bc956489980d990a765/src/compiler/utilities.ts#L503

This is definitely a much more flexible approach because it conforms to the user's style of choice. It also allows for dynamically modifying existing files without the huge risk of destroying code.

@bajtos
Copy link
Member

bajtos commented Jan 25, 2019

I think it would be better to generate code from an abstract syntax tree rather than using templates.

In my experience, I find templates much easier to read and reason about, because they resemble the actual source code produced.

However, I am open to experimentation! Would you like to create a proof-of-concept pull request showing how to use an AST-based template in one of our lb4 CLI command and convince us that it's a better approach?

It also allows for dynamically modifying existing files without the huge risk of destroying code.

Modification of existing code is a different task, it was never our intention to use templates for that. Based on the discussion in #1656, we may use ts-simple-ast package for this task. See also #1359.

@stale
Copy link

stale bot commented Jan 20, 2020

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

@stale stale bot added the stale label Jan 20, 2020
@clayrisser
Copy link
Contributor Author

clayrisser commented Jan 20, 2020

@bajtos
I built a project that should make it easy to reason with AST for code generation.

https://github.com/codejamninja/react-ast

@stale stale bot removed the stale label Jan 20, 2020
@deepakrkris
Copy link
Contributor

looks like this issue was covered, mostly with #2426 , closing now, please open a new issue for additional topics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants