fix: add parentheses required by specific grammar positions#136
Open
MathiasWP wants to merge 9 commits into
Open
fix: add parentheses required by specific grammar positions#136MathiasWP wants to merge 9 commits into
MathiasWP wants to merge 9 commits into
Conversation
… constants Derives UNARY/BINARY/LOGICAL precedence from EXPRESSIONS_PRECEDENCE so the checks in needs_parens can never drift out of sync with the table, which is what caused sveltejs#131 (a stale `13`/`12` literal). Behavior is unchanged.
Three classes of bug in the same family as sveltejs#131 (dropped parentheses that change meaning or produce invalid output): - nested same-sign unary operators were glued together. `-(-a)` printed as `--a` (the decrement operator) — valid but wrong; `-(--a)` printed as `---a` — invalid. Fixed by emitting a space when a unary `+`/`-` is followed by a unary/prefix-update of the same sign. - the optional-chaining boundary (ChainExpression) was dropped. `(a?.b)()` printed as `a?.b()`, silently changing short-circuit behaviour; `new (a?.b)()` and the tagged-template form became invalid. Fixed by parenthesizing a ChainExpression used as a callee/object/tag. - `await` as the left operand of `**` lost its parentheses. `(await a) ** b` printed as the SyntaxError `await a ** b`. Fixed by covering AwaitExpression in the `**` left-operand check. Adds test/paren-correctness.test.js covering all three plus a control that genuine single optional chains are not over-parenthesized.
Remove UNARY_PRECEDENCE/BINARY_PRECEDENCE/LOGICAL_PRECEDENCE consts and reference EXPRESSIONS_PRECEDENCE directly at each use site.
Some positions require parentheses that esrap was dropping, producing invalid
output or changing meaning:
- the `extends` super-class (class heritage) — class A extends (B || C) {}
- a tagged-template tag — (x || y)`tpl`
- a decorator expression — @(a ? b : c)
- an angle-bracket type assertion as the left of ** — (<T>x) ** y
- an expression statement starting with {, function, or class — ({} + []),
(class {}), (function(){})`x` — via a precedence-aware left-spine walk that
mirrors the parenthesization the visitors already apply (so already-safe
forms like ({}).x are not double-wrapped)
Adds test/required-parens-positions.test.js.
🦋 Changeset detectedLatest commit: b90d727 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
guyutongxue
added a commit
to piovium/espolar
that referenced
this pull request
Jun 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #134 (builds on its
needs_parenschanges — the diff narrows to just this branch once #134 lands).Adds parentheses that the position requires but esrap was dropping (invalid output or changed meaning):
extendssuper-class —class A extends (B || C) {}(x || y)`tpl`@(a ? b : c)**—(<T>x) ** y{,function, orclass—({} + []),(class {}),(function(){})`x`