Skip to content

fix: add parentheses required by specific grammar positions#136

Open
MathiasWP wants to merge 9 commits into
sveltejs:mainfrom
MathiasWP:fix/required-parens-positions
Open

fix: add parentheses required by specific grammar positions#136
MathiasWP wants to merge 9 commits into
sveltejs:mainfrom
MathiasWP:fix/required-parens-positions

Conversation

@MathiasWP

Copy link
Copy Markdown
Contributor

Stacked on #134 (builds on its needs_parens changes — 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):

  • the extends super-class — class A extends (B || C) {}
  • a tagged-template tag — (x || y)`tpl`
  • a decorator expression — @(a ? b : c)
  • an angle-bracket type assertion left of **(<T>x) ** y
  • an expression statement that would start with {, function, or class({} + []), (class {}), (function(){})`x`

MathiasWP and others added 9 commits June 3, 2026 16:39
… 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-bot

changeset-bot Bot commented Jun 3, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b90d727

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
esrap Patch

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants