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

Fix(rome_js_formatter):🐛 fix parenthesis expression bad case #2564

Merged
merged 5 commits into from
May 11, 2022

Conversation

IWANABETHATGUY
Copy link
Contributor

Summary

Fix #2558

Test Plan

fix the bad case

a[("test")]

@IWANABETHATGUY IWANABETHATGUY changed the title fix: 🐛 fix parenthesis expression bad case Fix(rome_js_formatter):🐛 fix parenthesis expression bad case May 10, 2022
@MichaReiser
Copy link
Contributor

I believe this fix is too specific to member expressions. What about computed member assignments? Or any other place where expressions can appear.

This is prettier's rule for formatting string literal expressions

https://github.com/prettier/prettier/blob/6d02cd9650be29d1b11ab6ff1442eac4f5415c06/src/language-js/needs-parens.js#L507-L528

The way I understand this is that:

  • It parenthesis all StringLiteralExpressions that are direct children of an ExpressionStatement
  • Removes parenthesis for all other StringLiteralExpressions

This should simplify the logic a bit.

@IWANABETHATGUY
Copy link
Contributor Author

I believe this fix is too specific to member expressions. What about computed member assignments? Or any other place where expressions can appear.

This is prettier's rule for formatting string literal expressions

https://github.com/prettier/prettier/blob/6d02cd9650be29d1b11ab6ff1442eac4f5415c06/src/language-js/needs-parens.js#L507-L528

The way I understand this is that:

  • It parenthesis all StringLiteralExpressions that are direct children of an ExpressionStatement
  • Removes parenthesis for all other StringLiteralExpressions

This should simplify the logic a bit.

Addressed that issue.

@@ -3,6 +3,4 @@ nock(/test/)
[httpMethodNock(method)]('/foo')
.reply(200, {
foo: 'bar',
});

a[("test")]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I remove this, due to this case has been added in another test file.

@IWANABETHATGUY
Copy link
Contributor Author

!bench_formatter

@github-actions
Copy link

Formatter Benchmark Results

group                                    main                                   pr
-----                                    ----                                   --
formatter/checker.ts                     1.00    402.2±1.52ms     6.5 MB/sec    1.02    409.8±1.94ms     6.3 MB/sec
formatter/compiler.js                    1.01    253.4±0.91ms     4.1 MB/sec    1.00    251.0±0.84ms     4.2 MB/sec
formatter/d3.min.js                      1.01    193.5±0.85ms  1386.9 KB/sec    1.00    191.4±0.94ms  1402.1 KB/sec
formatter/dojo.js                        1.00     13.2±0.03ms     5.2 MB/sec    1.00     13.1±0.03ms     5.2 MB/sec
formatter/ios.d.ts                       1.00    283.4±0.92ms     6.6 MB/sec    1.03    291.1±0.88ms     6.4 MB/sec
formatter/jquery.min.js                  1.00     48.3±0.55ms  1751.9 KB/sec    1.11     53.6±0.54ms  1578.5 KB/sec
formatter/math.js                        1.01    389.4±1.51ms  1702.6 KB/sec    1.00    385.1±1.29ms  1722.0 KB/sec
formatter/parser.ts                      1.13      9.1±0.02ms     5.3 MB/sec    1.00      8.0±0.01ms     6.0 MB/sec
formatter/pixi.min.js                    1.05    217.5±1.14ms     2.0 MB/sec    1.00   207.4±10.98ms     2.1 MB/sec
formatter/react-dom.production.min.js    1.00     65.0±0.69ms  1814.5 KB/sec    1.00     64.7±0.74ms  1821.3 KB/sec
formatter/react.production.min.js        1.00      3.1±0.01ms     2.0 MB/sec    1.00      3.1±0.00ms     2.0 MB/sec
formatter/router.ts                      1.12      6.9±0.04ms     8.8 MB/sec    1.00      6.2±0.01ms     9.9 MB/sec
formatter/tex-chtml-full.js              1.00    481.6±1.02ms  1937.6 KB/sec    1.00    479.5±1.30ms  1946.2 KB/sec
formatter/three.min.js                   1.01    248.5±0.98ms     2.4 MB/sec    1.00    246.5±0.86ms     2.4 MB/sec
formatter/typescript.js                  1.00  1592.1±13.34ms     6.0 MB/sec    1.01   1600.3±4.41ms     5.9 MB/sec
formatter/vue.global.prod.js             1.02     87.0±0.62ms  1418.9 KB/sec    1.00     85.2±0.78ms  1447.7 KB/sec

Comment on lines 105 to 119
// if expression is a StringLiteralExpression, we could not just return false, here is once case:
// ```js
// a[("test")]
// ```
// parent_precedence should be `High` due to the parenthesized_expression's parent is ComputedMemberExpression,
// and node_precedence should be `Low` due to expression is StringLiteralExpression.
// the parenthesis should be omitted.
if parent_precedence > node_precedence
&& !matches!(
expression,
JsAnyExpression::JsAnyLiteralExpression(
JsAnyLiteralExpression::JsStringLiteralExpression(_)
)
)
{
Copy link
Contributor

Choose a reason for hiding this comment

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

The easiest is probably to handle StringLiteralExpressions before this line because the precedence isn't relevant for string literals, so there's no point in computing the precedence first.

@IWANABETHATGUY
Copy link
Contributor Author

!bench_formatter

@github-actions
Copy link

Formatter Benchmark Results

group                                    main                                   pr
-----                                    ----                                   --
formatter/checker.ts                     1.04    407.7±3.00ms     6.4 MB/sec    1.00    393.1±1.01ms     6.6 MB/sec
formatter/compiler.js                    1.05    254.0±1.38ms     4.1 MB/sec    1.00    241.4±1.02ms     4.3 MB/sec
formatter/d3.min.js                      1.03    190.8±0.97ms  1406.5 KB/sec    1.00    185.3±1.61ms  1448.4 KB/sec
formatter/dojo.js                        1.05     13.6±0.01ms     5.0 MB/sec    1.00     12.9±0.01ms     5.3 MB/sec
formatter/ios.d.ts                       1.04    288.2±0.88ms     6.5 MB/sec    1.00    277.0±0.72ms     6.7 MB/sec
formatter/jquery.min.js                  1.04     53.6±0.38ms  1579.4 KB/sec    1.00     51.8±0.38ms  1635.3 KB/sec
formatter/math.js                        1.05    389.9±1.30ms  1700.7 KB/sec    1.00    371.3±0.98ms  1785.7 KB/sec
formatter/parser.ts                      1.05      9.4±0.00ms     5.2 MB/sec    1.00      8.9±0.01ms     5.4 MB/sec
formatter/pixi.min.js                    1.04    215.5±1.06ms     2.0 MB/sec    1.00    208.2±1.31ms     2.1 MB/sec
formatter/react-dom.production.min.js    1.05     63.1±0.52ms  1868.2 KB/sec    1.00     60.3±0.46ms  1953.8 KB/sec
formatter/react.production.min.js        1.04      3.1±0.00ms  2009.7 KB/sec    1.00      3.0±0.01ms     2.0 MB/sec
formatter/router.ts                      1.04      7.1±0.01ms     8.5 MB/sec    1.00      6.9±0.00ms     8.9 MB/sec
formatter/tex-chtml-full.js              1.04    483.7±1.57ms  1929.1 KB/sec    1.00    462.9±0.89ms  2015.7 KB/sec
formatter/three.min.js                   1.04    246.8±0.85ms     2.4 MB/sec    1.00    236.9±1.88ms     2.5 MB/sec
formatter/typescript.js                  1.04  1615.0±12.22ms     5.9 MB/sec    1.00   1548.3±3.84ms     6.1 MB/sec
formatter/vue.global.prod.js             1.04     84.2±0.58ms  1464.4 KB/sec    1.00     81.0±1.08ms  1524.0 KB/sec

@MichaReiser MichaReiser merged commit c405c41 into rome:main May 11, 2022
@IWANABETHATGUY IWANABETHATGUY deleted the fix/parenthesis-expression branch May 11, 2022 08:13
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants