Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

refactor(rome_js_formatter): Track token offsets instead of tokens #2481

Merged
merged 2 commits into from
Apr 22, 2022

Conversation

MichaReiser
Copy link
Contributor

@MichaReiser MichaReiser commented Apr 22, 2022

The Formatter tracks all formatted tokens in debug builds to catch if the same token gets formatted twice or not formatted at all.

The current implementation tracks the JsSyntaxTokens. The downside of doing this is that a language-agnostic implementation needs to track SyntaxToken<L> which:

  • has the consequence that Formatter must be generic over L
  • which in turn has the consequence that Format must be generic over L

This PR changes the node tracking to instead only track the absolute offsets of the formatted nodes rather than holding on to the node itself. This removes the need for a generic Language parameter and provides the same functionality.

A next step is to move out all format_ functions from the Formatter and put them into standalone functions / extension traits.

Test

  • Commented out the track_replaced call in format_replaced and verified that some tests now panic with a message that token X wasn't formatted.
  • Added one an additional track_replaced call to format_replaced and verified that tests are now failing with a message saying that the token X was formatted twice.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Apr 22, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1a087ec
Status:⚡️  Build in progress...

View logs

Copy link
Contributor

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before we had the message "You tried to print the token '{:?}' twice, and this is not valid" which was somehow descriptive and helped what went wrong.

Now some message are different, which makes sense. Although they don't provide much context sometimes. Would you mind expand this section , explaining how a developer is supposed to do once they find these new messages?

crates/rome_js_formatter/src/printed_tokens.rs Outdated Show resolved Hide resolved
@MichaReiser
Copy link
Contributor Author

Before we had the message "You tried to print the token '{:?}' twice, and this is not valid" which was somehow descriptive and helped what went wrong.

Now some message are different, which makes sense. Although they don't provide much context sometimes. Would you mind expand this section , explaining how a developer is supposed to do once they find these new messages?

I removed the mode and extended the panic message

Copy link
Contributor

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Way better! Thank you!

Base automatically changed from refactor/format-root to main April 22, 2022 11:29
The `Formatter` tracks all formatted token in debug builds to catch if the same token gets formatted twice or not formatted at all.

The current implementation tracks the `JsSyntaxToken`s. The downside of doing this is that a language agnostic implementation needs to track `SyntaxToken<L>` which:

* has the consequence that `Formatter` must be generic over `L`
* which in turn has the consequence that `Format` must be generic over `L`

This PR changes the node tracking to instead only track the absolute offsets of the formatted nodes rather than holding on to the node itself. This removes the need for a generic `Language` parameter and provides the same functionality.

## Test

* Commented out the `track_replaced` call in `format_replaced` and verified that some tests now panic with a message that token X wasn't formatted.
* Added one an additional `track_replaced` call to `format_replaced` and verified that tests are now failing with a message saying that the token X was formatted twice.
@MichaReiser MichaReiser force-pushed the refactor/formatter-token-tracking branch from 2736b5a to 1a087ec Compare April 22, 2022 11:32
@MichaReiser MichaReiser temporarily deployed to aws April 22, 2022 11:32 Inactive
@github-actions
Copy link

Parser conformance results on ubuntu-latest

js/262

Test result main count This PR count Difference
Total 45878 45878 0
Passed 44938 44938 0
Failed 940 940 0
Panics 0 0 0
Coverage 97.95% 97.95% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 39 39 0
Passed 36 36 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.31% 92.31% 0.00%

ts/babel

Test result main count This PR count Difference
Total 588 588 0
Passed 519 519 0
Failed 69 69 0
Panics 0 0 0
Coverage 88.27% 88.27% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 16257 16257 0
Passed 12391 12391 0
Failed 3866 3866 0
Panics 0 0 0
Coverage 76.22% 76.22% 0.00%

@github-actions
Copy link

@MichaReiser MichaReiser merged commit 63a273b into main Apr 22, 2022
@MichaReiser MichaReiser deleted the refactor/formatter-token-tracking branch April 22, 2022 11:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants