Skip to content

#236, again - #238

Closed
Rich-Harris wants to merge 10 commits into
sveltejs:mainfrom
leonidaz:feat/tokens-alt-followups
Closed

Rich-Harris wants to merge 10 commits into
sveltejs:mainfrom
leonidaz:feat/tokens-alt-followups

Conversation

@Rich-Harris

Copy link
Copy Markdown
Member

the previous PR was auto-deleted

leonidaz and others added 10 commits September 24, 2026 09:25
The root visitor maps every node's start and end, which covers tokens that
open a node. Tokens inside a node have no node boundary to anchor them: the
brackets of a computed key, the parentheses of parameter lists, calls and
control-flow heads, the braces of import and export lists, static blocks,
switch and enum bodies, template interpolations, and every keyword written
after a node's first token (`function` after `async`, `else`, `finally`,
`while` in `do … while`, `default` after `export`, `type` after `import`,
class and member modifiers, `extends`, `implements`, `in`, `of`, `as`,
`satisfies`).

A new `tokens` option takes the parser's token list (Acorn's `onToken`, Babel's
`tokens`, ESLint-style `ast.tokens`). The tokens of interest are indexed once,
sorted if the parser emitted them out of order, and each one is found by a
bounded binary search from a position the AST does know. Without tokens they
stay unmapped.

Closing delimiters that end a node (`)` of a call, `]` of an array or computed
member, `}` of an object, switch, enum or interface body) are mapped from the
node's end without tokens, as BlockStatement already does.

Printed output is unchanged; 61 sample maps gain segments.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
A decorated class's decorators are printed before `export`, so the root visitor
cannot map the export node's start where it is written. Emit that start once the
decorators are out: the export node begins at `export` with both Acorn and
typescript-estree, whichever side of it the decorators were written on.

Instead of clearing the declaration's decorators and location while it is
printed, remember that its decorators were printed. The root visitor then maps
its start when that start is the `class` keyword (typescript-estree, decorators
written before `export`) and skips it when it is a decorator (Acorn, see
sveltejs/acorn-typescript#117), and the keyword lookup can locate `class`,
`abstract` and the rest from the tokens because the node keeps its location.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Deferred from the first version of this branch until sveltejs#212 gave the catch
clause its own visitor.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…nst token matching

`import type {} from` was written as one string since sveltejs#219, so with tokens
neither the keyword nor the braces could match; write them one token at a time.

Add tests for the comment handling the token matcher relies on: a comment whose
text is a bracket (`/* ( */`, `// )`, a commented-out block, a comment line
that is a lone `}`) must not be consumed as the next source token, and the
real brackets after it must still map exactly.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@pkg-svelte-dev

Copy link
Copy Markdown

Install the latest version of esrap from 2729d07:

pnpm add https://pkg.svelte.dev/esrap/c/2729d072b2afcc75ce648da0af983338e91254fe

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

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 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2729d07

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

@Rich-Harris

Copy link
Copy Markdown
Member Author

ugh it won't let me push changes after merging main

@Rich-Harris

Copy link
Copy Markdown
Member Author

@leonidaz mind opening a new version of #236? (I could do it myself but then I can't approve it...)

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