Skip to content

fix: map parentheses and braces added by the printer to what they wrap - #214

Merged
Rich-Harris merged 2 commits into
sveltejs:mainfrom
leonidaz:fix/synthesized-parens
Sep 24, 2026
Merged

Rich-Harris merged 2 commits into
sveltejs:mainfrom
leonidaz:fix/synthesized-parens

Conversation

@leonidaz

Copy link
Copy Markdown
Contributor

Closes #211.

When the printer adds parentheses the AST does not have, the ( and ) were written without a location. That happens in maybe_wrap (every precedence-driven wrap, which includes authored parentheses that Acorn drops with preserveParens off), around a lower-precedence class base, around an await argument, and around a decorator expression. TypeScript reports on the parenthesized span, so its range starts at the (, and that column resolved to whatever was mapped before it: for class B extends function () {} {} the end of B, for (a || b)() nothing at all.

Change: maybe_wrap sets the location to the wrapped node's loc.start before writing ( and to its loc.end after writing ). The class-base, await and decorator sites now go through maybe_wrap instead of their own copy of the wrap. The braces the if visitor adds around a dangling-else consequent get the same treatment, mapped to the consequent's span.

This stays within the rule from #198: the positions come from the wrapped node, nothing is estimated. Printed output is unchanged; twenty sample maps gain segments.

Tests: four cases in sourcemap-keywords.test.js assert that the synthesized ( maps to the start of what it wraps: class heritage, a call on a dropped parenthesized expression, an await argument and a decorator expression.

Not changed here: the class-base wrap is more conservative than the grammar requires (extends function () {} and extends maybe! are valid without parentheses). That is an output change and can be its own PR if wanted.

Validation:

  • pnpm test — 319 tests passed
  • pnpm lint — passed
  • pnpm check — the two pre-existing TS2580 Buffer errors in src/index.js on main, nothing new

Like #212 and #213 this appends tests to sourcemap-keywords.test.js and regenerates sample maps, so whichever lands second needs a trivial rebase.

🤖 Generated with Claude Code

When the printer adds parentheses the AST does not have (a lower-precedence
class base, await argument or decorator expression, or any maybe_wrap site,
including authored parentheses that Acorn drops), the opening parenthesis had
no location, so a diagnostic reported on the parenthesized span had nothing to
map its start to. Map the synthesized parentheses to the wrapped node's start
and end. Same for the braces added around a dangling-else consequent.

Closes sveltejs#211

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@pkg-svelte-dev

pkg-svelte-dev Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Install the latest version of esrap from dc490e4:

pnpm add https://pkg.svelte.dev/esrap/c/dc490e4bb4f93738fe96b83f65b38f7946f735b7

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/esrap/pr/214

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: dc490e4

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

@Rich-Harris Rich-Harris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thank you

@Rich-Harris
Rich-Harris merged commit a0c2db4 into sveltejs:main Sep 24, 2026
7 checks passed
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.

Sourcemaps: parentheses added by the printer have no location

2 participants