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 stackoverflow on Windows for nested seque… #2469

Merged
merged 1 commit into from
Apr 20, 2022

Conversation

MichaReiser
Copy link
Contributor

@MichaReiser MichaReiser commented Apr 20, 2022

…nce expressions

Formatting deeply nested sequence expressions is causing a stack overflow on Windows because they are parsed left-recursive:

a, b,c,d

Parses to

JsExpressionStatement {
    expression: JsSequenceExpression {
        left: JsSequenceExpression {
            left: JsSequenceExpression {
                left: JsIdentifierExpression {
                    name: JsReferenceIdentifier {
                        value_token: IDENT@0..1 "a" [] [],
                    },
                },
                comma_token: COMMA@1..3 "," [] [Whitespace(" ")],
                right: JsIdentifierExpression {
                    name: JsReferenceIdentifier {
                        value_token: IDENT@3..4 "b" [] [],
                    },
                },
            },
            comma_token: COMMA@4..5 "," [] [],
            right: JsIdentifierExpression {
                name: JsReferenceIdentifier {
                    value_token: IDENT@5..6 "c" [] [],
                },
            },
        },
        comma_token: COMMA@6..7 "," [] [],
        right: JsIdentifierExpression {
            name: JsReferenceIdentifier {
                value_token: IDENT@7..8 "d" [] [],
            },
        },
    },
    semicolon_token: missing (optional),
}

This PR changes the formatting of sequence expression to unwind the recursion by using a loop
that first finds the left most sequence expression and then concatenates the expressions.

test

cargo bench_formatter --filter d3 --criterion=false

No longer panics on Windows

…nce expressions

Formatting deeply nested sequence expressions is causing a stack overflow on Windows because they are parsed left-recursive:

```js
a, b,c,d
```

Parses to

```yaml
JsExpressionStatement {
	expression: JsSequenceExpression {
		left: JsSequenceExpression {
			left: JsSequenceExpression {
				left: JsIdentifierExpression {
					name: JsReferenceIdentifier {
						value_token: IDENT@0..1 "a" [] [],
					},
				},
				comma_token: COMMA@1..3 "," [] [Whitespace(" ")],
				right: JsIdentifierExpression {
					name: JsReferenceIdentifier {
						value_token: IDENT@3..4 "b" [] [],
					},
				},
			},
			comma_token: COMMA@4..5 "," [] [],
			right: JsIdentifierExpression {
				name: JsReferenceIdentifier {
					value_token: IDENT@5..6 "c" [] [],
				},
			},
		},
		comma_token: COMMA@6..7 "," [] [],
		right: JsIdentifierExpression {
			name: JsReferenceIdentifier {
				value_token: IDENT@7..8 "d" [] [],
			},
		},
	},
	semicolon_token: missing (optional),
}
```

This PR changes the formatting of sequence expression to unwind the recursion by using a loop
that first finds the left most sequence expression and then concatenates the expressions.

## test

```
cargo bench_formatter --filter d3 --criterion=false
```

No longer panics on Windows
@MichaReiser MichaReiser temporarily deployed to aws April 20, 2022 08:54 Inactive
@MichaReiser
Copy link
Contributor Author

!bench_formatter

@MichaReiser MichaReiser requested review from leops, ematipico and xunilrj and removed request for leops April 20, 2022 08:55
@github-actions
Copy link

Parser conformance results on ubuntu-latest

js/262

Test result main count This PR count Difference
Total 45323 45323 0
Passed 44383 44383 0
Failed 940 940 0
Panics 0 0 0
Coverage 97.93% 97.93% 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 16171 16171 0
Passed 12315 12315 0
Failed 3856 3856 0
Panics 0 0 0
Coverage 76.15% 76.15% 0.00%

@github-actions
Copy link

@cloudflare-workers-and-pages
Copy link

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: ee7e90c
Status: ✅  Deploy successful!
Preview URL: https://98d22f35.tools-8rn.pages.dev

View logs

while let Some(parent) = current.syntax().parent() {
if let Some(parent_sequence) = JsSequenceExpression::cast(parent) {
let JsSequenceExpressionFields {
left: _left,
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
left: _left,
left: _,

we use _ already when we want to ignore a node

@xunilrj xunilrj merged commit 9461cdd into main Apr 20, 2022
@xunilrj xunilrj deleted the fix/recursive-sequence-expressions branch April 20, 2022 09:06
@github-actions
Copy link

Formatter Benchmark Results

group                                    main                                   pr
-----                                    ----                                   --
formatter/checker.ts                     1.01   567.3±16.41ms     4.6 MB/sec    1.00   561.3±17.19ms     4.6 MB/sec
formatter/compiler.js                    1.00    323.2±7.93ms     3.2 MB/sec    1.00    324.8±6.79ms     3.2 MB/sec
formatter/d3.min.js                      1.00    258.5±7.63ms  1038.1 KB/sec    1.00    258.9±8.52ms  1036.8 KB/sec
formatter/dojo.js                        1.00     16.9±0.59ms     4.1 MB/sec    1.01     17.0±0.72ms     4.0 MB/sec
formatter/ios.d.ts                       1.01    397.0±9.14ms     4.7 MB/sec    1.00    394.6±9.56ms     4.7 MB/sec
formatter/jquery.min.js                  1.00     69.0±1.76ms  1227.2 KB/sec    1.01     69.4±2.66ms  1218.9 KB/sec
formatter/math.js                        1.00   519.6±17.72ms  1276.2 KB/sec    1.01   524.7±12.33ms  1263.8 KB/sec
formatter/parser.ts                      1.03     12.2±0.57ms     4.0 MB/sec    1.00     11.9±0.39ms     4.1 MB/sec
formatter/pixi.min.js                    1.00   293.2±12.69ms  1533.0 KB/sec    1.00    292.2±7.55ms  1538.2 KB/sec
formatter/react-dom.production.min.js    1.01     89.1±4.19ms  1322.8 KB/sec    1.00     88.0±2.94ms  1338.8 KB/sec
formatter/react.production.min.js        1.00      4.0±0.16ms  1569.9 KB/sec    1.00      4.0±0.14ms  1566.9 KB/sec
formatter/router.ts                      1.06      8.8±0.53ms     6.8 MB/sec    1.00      8.3±0.32ms     7.2 MB/sec
formatter/tex-chtml-full.js              1.00   631.9±13.20ms  1476.8 KB/sec    1.01   638.3±11.84ms  1462.0 KB/sec
formatter/three.min.js                   1.00    317.3±7.33ms  1894.9 KB/sec    1.04   331.1±10.55ms  1815.6 KB/sec
formatter/typescript.js                  1.00       2.1±0.04s     4.6 MB/sec    1.00       2.1±0.04s     4.6 MB/sec
formatter/vue.global.prod.js             1.00    116.3±3.87ms  1060.8 KB/sec    1.00    115.8±3.75ms  1065.6 KB/sec

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