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

Format-preserving code printer #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update 0000-format-preserving-code-printer.md
  • Loading branch information
nicolo-ribaudo authored Jun 30, 2024
commit 38bdd84a5a66e4f9be415f29a22438e617acdfed
17 changes: 9 additions & 8 deletions rfcs/0000-format-preserving-code-printer.md
Original file line number Diff line number Diff line change
@@ -86,9 +86,12 @@ We currently have two options to preserve some formatting in the output code:
- `retainLines`, that preserves line numbers
- `retainFunctionParens`, that preserves parentheses around function expressions (because some engines use them as a hint to eagerly compile the function body)

To implement this RFC, we need two new options:
- `retainParens`, to preserve all parentheses from the input code.
- `retainColumns`, to preserve the _column_ of the tokens other than their lines.
To implement this RFC, we would introduce one new option:
- `preserveFormat`, to preserve line-by-line the format of the original code (where there are no new nodes injected).

`preserveFormat` can be used together with `retainLines`, to make sure that even when there are new nodes injected:
- all other nodes are in their original location
- all lines not affected by the new/transformed nodes preserve their formatting

## Implementation

@@ -153,9 +156,9 @@ Once that is done, it'd be good to have examples of how it can be used to refact
If you plan to implement this on your own, what help would you need from the team?
-->

**What is the exact interaction between `retainLines` and `retainColumns`?**
**How to format new injected nodes?**

It would be useful to be able to use `retainColumns` without also having to use `retainLines`, but with it still trying to preserve relative column changes.
It would be useful to be able to use `preserveFormat` without also having to use `retainLines`, but with it still trying to preserve relative column changes.

For example, given this input code:
```js
@@ -177,9 +180,7 @@ let a =
1 + 2;
```

Maybe instead of `retainLines`/`retainColumns` we should have `retainLines`/`preserveFormat`, where for now `preserveFormat` requires `retainLines` (giving the first output) but in the future could work independently (giving the second output). `preserveFormat` would also imply `retainParens`.

What approach is the best?
I am not sure yet about how such algorithm could work.

**What to do about `<!--` and `-->` comments?**