Support codemods: use a CST
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 52.3k
- Forks
- 5k
- Avg merge
- 19h 2m
- Merged PRs (30d)
- 117
Description
Related issues: #2068, #4675, #5998, and my own earlier #12709.
Currently this is possible:
- Give recast your favorite parser and have it parse source text into an AST and make a copy of that AST for later comparison.
- Modify the AST as your desired transform dictates. You cannot manipulate concrete syntax, so I hope you don't want line breaks.
- Recast prints combining a format-preserving printer with a generic pretty printer.
- Prettier parses the source text into a new AST
- Prettier prints its AST into a string using the source text to inform some formatting decisions.
I do not like this workflow:
- It involves three complete copies of the AST and an extra intermediate representation of the text.
- Recast has significant bugs in its printing.
- Recast's pretty printer is largely unneeded and unmaintained since most of the development on it has occurred in a fork known as "prettier".
I have started work on implementing an alternate workflow:
- Parse source text into an AST with your favorite parser
- Integrate concrete syntax with that AST by adding
node.tokensto every node. Now you have a concrete syntax tree, or CST! - Modify this CST according to the logic of your transform. Transforms are free to tweak the concrete syntax.
- Pass the CST to prettier, which mutates the tokens to achieve pretty-printing.
- Print the tokens through simple traversal.
In order to make this possible, I am building a library whose working name is cst-tokens. Its functionality centers around building and synchronizing concrete syntax stored in node.tokens arrays.
My research indicates that this structure could be used to satisfy the kinds of queries prettier currently can only make against source text (i.e. originalText), though actually implementing those queries is still likely to be nontrivial.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading related issues #2068, #4675, #5998, and #12709, then review the linked cst-tokens library. Compare its node.tokens synchronization approach with Prettier's current originalText queries and printer workflow. Done would require an agreed implementation path for CST support; this issue names no files or tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100