prettier / prettier/prettier

Support codemods: use a CST

Open
#12,806 24 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

type:meta
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.tokens to 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.