Skip to content

feat: optional boundaryTokens source-map anchors for structural tokens - #148

Merged
trueadm merged 1 commit into
sveltejs:mainfrom
Ripple-TS:parens
Jul 18, 2026
Merged

trueadm merged 1 commit into
sveltejs:mainfrom
Ripple-TS:parens

Conversation

@leonidaz

Copy link
Copy Markdown
Contributor

Structural tokens — array/object brackets and braces, preserved parentheses, unary operators, and the closing tokens of calls and computed member access are written without location markers. Since source-map consumers attribute every generated character to the nearest PRECEDING segment, these characters (and any whitespace after them) are attributed to the previous token's source position, and a node whose source span begins or ends on one of them has no resolvable boundary position at all. Tools that map node-boundary positions back to source (e.g. Volar-style virtual-file tooling translating diagnostics) must then fail or degrade.

Add a boundaryTokens language option that anchors each such token with a one-character location at the node boundary it represents. Printed output is byte-identical; maps gain segments (measured ~13% mappings growth on a dense file), which is why the option is OFF by default - existing consumers are completely unaffected.

Structural tokens — array/object brackets and braces, preserved
parentheses, unary operators, and the closing tokens of calls and
computed member access — are written without location markers. Since
source-map consumers attribute every generated character to the
nearest PRECEDING segment, these characters (and any whitespace after
them) are attributed to the previous token's source position, and a
node whose source span begins or ends on one of them has no resolvable
boundary position at all. Tools that map node-boundary positions back
to source (e.g. Volar-style virtual-file tooling translating
diagnostics) must then fail or degrade.

Add a `boundaryTokens` language option that anchors each such token
with a one-character location at the node boundary it represents.
Printed output is byte-identical; maps gain segments (measured ~13%
mappings growth on a dense file), which is why the option is OFF by
default — existing consumers are completely unaffected.
@pkg-svelte-dev

Copy link
Copy Markdown

Install the latest version of esrap from 6a4aa4e:

pnpm add https://pkg.svelte.dev/esrap/c/6a4aa4eec33c064f134d35051587ac558bdbef76

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

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 Jul 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6a4aa4e

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

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

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

@trueadm trueadm left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks like a good quality of life win.

@trueadm
trueadm merged commit b7e623d into sveltejs:main Jul 18, 2026
6 checks passed
leonidaz added a commit to leonidaz/esrap that referenced this pull request Sep 2, 2026
…ryTokens maps

The AST records where a computed key's expression is, not where its
brackets are. `boundaryTokens` (sveltejs#148) anchored those brackets by
assuming they touch the key, which is wrong whenever whitespace,
newlines or comments sit between bracket and key.

Add a `tokens` option: the parser's token stream (Acorn `onToken`,
Babel `tokens`, ESLint-style `ast.tokens`). With it, every delimiter
internal to a node — computed-key brackets, the parentheses of calls,
functions, signatures and control-flow statements, the braces of
import/export lists, enums, static blocks and template interpolations
— is anchored to its real source position via a bounded nearest-token
search. Without it those delimiters are left unmapped rather than
guessed. Node-boundary anchors are unchanged and output stays
byte-identical.

The token index sorts on demand: acorn-typescript re-emits tokens when
it backtracks, so its stream is not monotone and a plain binary search
would silently miss brackets.

The anchoring lives in `boundary-tokens.js`, memoised per options object
and shared with `tsx`; `write_params`, `write_keyword_paren` and
`write_parenthesized` replace the per-visitor copies of the same
open/close pattern.
leonidaz added a commit to leonidaz/esrap that referenced this pull request Sep 2, 2026
…ryTokens maps

The AST records where a computed key's expression is, not where its
brackets are. `boundaryTokens` (sveltejs#148) anchored those brackets by
assuming they touch the key, which is wrong whenever whitespace,
newlines or comments sit between bracket and key.

Add a `tokens` option: the parser's token stream (Acorn `onToken`,
Babel `tokens`, ESLint-style `ast.tokens`). With it, every delimiter
internal to a node — computed-key brackets, the parentheses of calls,
functions, signatures and control-flow statements, the braces of
import/export lists, enums, static blocks and template interpolations
— is anchored to its real source position via a bounded nearest-token
search. Without it those delimiters are left unmapped rather than
guessed. Node-boundary anchors are unchanged and output stays
byte-identical.

The token index sorts on demand: acorn-typescript re-emits tokens when
it backtracks, so its stream is not monotone and a plain binary search
would silently miss brackets.

The anchoring lives in `boundary-tokens.js`, memoised per options object
and shared with `tsx`; `write_params`, `write_keyword_paren` and
`write_parenthesized` replace the per-visitor copies of the same
open/close pattern.
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