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

refactor(formatter): Change binary like expression to format left to right #2640

Merged
merged 6 commits into from
Jun 7, 2022

Conversation

MichaReiser
Copy link
Contributor

@MichaReiser MichaReiser commented Jun 2, 2022

Summary

Refactors the binary like expression formatting to lazily write the expressions rather than allocating vectors with the sub results.

The basic idea remains the same. The implementation flattens an expression. What's different to before is that this is now a two-stage process:

  1. Flatten the binary expression and specify for each "part" how it should be formatted (left, right, or a group)
  2. Format the flattened expressions

I think this also improves readability because it makes it clear what concepts exist in this algorithm (left hand side, right hand side, and a sub-group).

Test Plan

cargo test

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jun 2, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: ea01272
Status: ✅  Deploy successful!
Preview URL: https://0189a681.tools-8rn.pages.dev

View logs

@MichaReiser MichaReiser force-pushed the refactor/format-and-write-macros branch 2 times, most recently from a464b8d to 8ac52ad Compare June 2, 2022 12:04
Base automatically changed from refactor/format-and-write-macros to main June 6, 2022 10:58
@MichaReiser MichaReiser force-pushed the refactor/binary-like-left-to-right branch from bc3effd to 5bdaae9 Compare June 6, 2022 11:04
@MichaReiser MichaReiser temporarily deployed to aws June 6, 2022 11:04 Inactive
@github-actions
Copy link

github-actions bot commented Jun 6, 2022

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%

symbols/microsoft

Test result main count This PR count Difference
Total 5946 5946 0
Passed 350 350 0
Failed 5591 5590 ✅ ⏬ -1
Panics 5 6 ❌ ⏫ +1
Coverage 5.89% 5.89% 0.00%
💥 Failed to Panic (1):
typeGuardsAsAssertions.symbols

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

github-actions bot commented Jun 6, 2022

@MichaReiser MichaReiser temporarily deployed to aws June 6, 2022 11:12 Inactive
@MichaReiser
Copy link
Contributor Author

!bench_formatter

@github-actions
Copy link

github-actions bot commented Jun 6, 2022

Formatter Benchmark Results

group                                    main                                   pr
-----                                    ----                                   --
formatter/checker.ts                     1.00   399.5±21.52ms     6.5 MB/sec    1.06   422.2±11.03ms     6.2 MB/sec
formatter/compiler.js                    1.02   252.2±11.16ms     4.2 MB/sec    1.00    247.9±8.14ms     4.2 MB/sec
formatter/d3.min.js                      1.00    182.2±7.64ms  1473.3 KB/sec    1.02    185.4±8.17ms  1448.0 KB/sec
formatter/dojo.js                        1.00     12.5±0.46ms     5.5 MB/sec    1.04     13.1±0.67ms     5.2 MB/sec
formatter/ios.d.ts                       1.00   273.1±11.81ms     6.8 MB/sec    1.04   283.2±11.69ms     6.6 MB/sec
formatter/jquery.min.js                  1.01     53.9±2.74ms  1570.6 KB/sec    1.00     53.1±2.60ms  1593.3 KB/sec
formatter/math.js                        1.02   422.9±21.02ms  1567.8 KB/sec    1.00   413.5±20.05ms  1603.7 KB/sec
formatter/parser.ts                      1.00      8.4±0.32ms     5.7 MB/sec    1.14      9.6±0.34ms     5.1 MB/sec
formatter/pixi.min.js                    1.00    205.4±6.83ms     2.1 MB/sec    1.03    210.7±7.20ms     2.1 MB/sec
formatter/react-dom.production.min.js    1.08     66.0±4.46ms  1785.5 KB/sec    1.00     61.2±3.31ms  1924.8 KB/sec
formatter/react.production.min.js        1.01      3.2±0.15ms  1988.7 KB/sec    1.00      3.1±0.83ms  2003.6 KB/sec
formatter/router.ts                      1.00      6.5±0.27ms     9.4 MB/sec    1.07      6.9±0.31ms     8.8 MB/sec
formatter/tex-chtml-full.js              1.00   539.4±16.25ms  1729.8 KB/sec    1.00   537.2±16.22ms  1737.1 KB/sec
formatter/three.min.js                   1.00    246.7±9.74ms     2.4 MB/sec    1.00    246.8±9.38ms     2.4 MB/sec
formatter/typescript.js                  1.06  1599.7±45.27ms     5.9 MB/sec    1.00  1513.1±31.02ms     6.3 MB/sec
formatter/vue.global.prod.js             1.00     79.4±3.36ms  1553.5 KB/sec    1.04     82.7±4.67ms  1492.0 KB/sec

/// The left or right sub part of a binary expression.
#[derive(Debug)]
enum FlattenedExpression {
/// The right hand sie of a binary expression. Needs to format the parent operator and the right expression
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// The right hand sie of a binary expression. Needs to format the parent operator and the right expression
/// The right hand side of a binary expression. Needs to format the parent operator and the right expression

Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't "right expression" self in this case? If not, I am really missing the point of this struct

Copy link
Contributor Author

@MichaReiser MichaReiser Jun 6, 2022

Choose a reason for hiding this comment

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

The point of this struct is that it represents this

/// some &&
/// thing &&
/// elsewhere &&
/// happy

Except that our current formatting actually formats this differently: It creates the following groups

some
&& thing
&& elsewhere
&& happy

Not sure if that's intentional but this isn't something I'm changing as part of this PR.

// if none of the previous conditions is met,
// we take take out the first element from the rest of group, then we hard group the "head"
// and we indent the rest of the groups in a new line
write!(f, [groups.next().unwrap()])?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we add a // SAFETY comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm maybe... That's copied 1:1, let me see if I can figure it out

Copy link
Contributor

@ematipico ematipico Jun 6, 2022

Choose a reason for hiding this comment

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

I missed this change unfortunately. But here we can safely tell that we always have at least two groups in a binary like expression.

Also, the comments seems to be outdated. We don't format the "head" in this branch.

@MichaReiser MichaReiser temporarily deployed to aws June 6, 2022 12:37 Inactive
@MichaReiser MichaReiser temporarily deployed to aws June 6, 2022 12:39 Inactive
@MichaReiser MichaReiser merged commit 8c3a819 into main Jun 7, 2022
@MichaReiser MichaReiser deleted the refactor/binary-like-left-to-right branch June 7, 2022 06:12
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.

2 participants