Skip to content

fix: map the empty type-only import's tokens, and guard comments against token matching - #240

Open
leonidaz wants to merge 1 commit into
sveltejs:mainfrom
leonidaz:feat/tokens-alt-followups
Open

leonidaz wants to merge 1 commit into
sveltejs:mainfrom
leonidaz:feat/tokens-alt-followups

Conversation

@leonidaz

Copy link
Copy Markdown
Contributor

Follow-up to #235, which landed before this could be folded in (it was #236, stacked on tokens-alt, and closed automatically when that branch was deleted). Same code, rebased on main.

Empty type-only import. #219 prints import type {} from as a single string, and context.write only maps a write whose trimmed text is exactly one token, so with tokens neither type nor the braces matched. Written one token at a time now. Two test cases: the braces of import type /* before */ { /* inside */ } from 'module' and the type keyword of import /* c */ type {} from 'm'.

Guard for comments that look like tokens. The matcher relies on write_comment never handing it a bare bracket, which #235 arranges by writing a one-line comment's delimiters together with its text. Nothing tested that, so this adds the cases where it can go wrong: a comment is flushed after enter(node), so a comment whose text is the next node's own opening bracket is the one that can be consumed. Against the pre-#235 comment writer, const o = /* { */ { a: 1 }; loses the mapping of its }; with the current writer all of these pass:

  • a comment before an object, an array, an arrow function (/* { */, /* [ */, /* ( */)
  • a multi-line comment whose inner line is a lone ( or {, before an arrow or a block
  • a line comment // ), and a commented-out block containing } lines

Each case checks that every bracket outside comments in the output maps to the matching source bracket.

Validation:

  • pnpm test — 525 tests passed
  • pnpm lint — passed
  • pnpm check — passed

🤖 Generated with Claude Code

…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 d371052:

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

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

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: d371052

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

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.

1 participant