Skip to content

fix: remove inaccurate keyword sourcemap locations - #192

Merged
Rich-Harris merged 17 commits into
mainfrom
revert-116
Sep 23, 2026
Merged

Rich-Harris merged 17 commits into
mainfrom
revert-116

Conversation

@Rich-Harris

@Rich-Harris Rich-Harris commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

Closes #190. This reverts the buggy parts of #116 — instead of inserting possibly-fictional mappings for keywords, we only insert mappings around tokens that we know the original location of

@pkg-svelte-dev

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

Copy link
Copy Markdown

Install the latest version of esrap from e9c6850:

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

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

@changeset-bot

changeset-bot Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e9c6850

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 requested review from dummdidumm and a lite review from Copilot and removed request for Copilot September 22, 2026 14:45
@Rich-Harris

Copy link
Copy Markdown
Member Author

Turns out we might also need to map the start of statements (if, etc) for debugging purposes.

Also, in a case like this...

typeof blah;
let blah;

...the error points to the start of typeof, so we need a mapping there. At the moment the UnaryExpression visitor adds start and end mappings for its operators but only if boundaryTokens is true, which is insufficient. Need to figure out what's happening with #183 before resolving this.

dummdidumm
dummdidumm previously approved these changes Sep 22, 2026
@dummdidumm
dummdidumm dismissed their stale review September 22, 2026 15:27

turns out we need this after all?

@leonidaz

Copy link
Copy Markdown
Contributor

@Rich-Harris agreed that #116 has a bug, but I think this removes more than the bug. Here is what I found, and a way to fix it properly.

Where #116 is actually wrong

#116 maps keywords in two different ways.

The first keyword of a node is mapped at node.loc.start. That is always exact: if, for, return, throw, class, function, import, export, const, try, switch, the first modifier of a class member, and so on.

Every later keyword is placed by adding up string lengths: async + function, export + default, import + type, abstract + class, the rest of a modifier chain, and else, finally and while (in do … while) at "end of the previous part, plus one space". That is the part that breaks on

for /* foo */ await /* bar */ (const x of y) {}

and on any extra whitespace or line break. Only the arithmetic is guessing. This PR deletes both kinds.

Who uses the first-keyword mappings

tsrx and Ripple map TypeScript diagnostics from esrap's output back to the source through Volar, which only reports a diagnostic if its start falls inside a mapping. TypeScript puts a fair number of errors on a range that begins with a keyword:

code range starts at
TS7027 Unreachable code detected the statement: const …, return …, throw …, if …
TS6192 All imports in import declaration are unused import
TS1040 'async' modifier cannot be used in an ambient context async
TS1103 'for await' loops are only allowed within async functions await in for await

I ran both projects' mapping pipelines against this branch with the same input. Nothing crashes, but mappings silently disappear: tsrx loses its import anchors and every function / async keyword token, and Ripple loses every export mapping plus function after async. Both projects removed their own keyword markers when 2.3.0 shipped #116, on the strength of esrap mapping them, so those would have to come back on the consumer side.

Your own follow-up says the same thing from the debugging side: statement starts like if, and typeof in typeof blah; let blah;, need a mapping. Those are node starts too.

A fix that keeps the exact part and removes the guessing

  1. Keep the first-keyword mappings. They are node.loc.start, one line each, and never wrong. This includes making the UnaryExpression operator mapping unconditional rather than tied to boundaryTokens, as you suggested.
  2. Delete all the arithmetic. No more column + 'async '.length, no more "end of consequent plus one" for else.
  3. Locate the later keywords from the parser's tokens when they are supplied, with the same lookup feat: locate internal delimiters exactly from parser tokens in boundaryTokens maps #183 adds for delimiters: find the nearest await token between for and (, the else token between the consequent and the alternate, the function token after async, the default after export, the type after import. That is exact by construction, comments and whitespace included, so it fixes Simplify keyword locations #190 rather than working around it. Without tokens those keywords are left unmapped, the same rule feat: locate internal delimiters exactly from parser tokens in boundaryTokens maps #183 applies to the brackets of a computed key.

Net effect compared to this PR: the same bug fixed, every mapping that consumers rely on kept, and the keywords that currently need guessing become exact instead of disappearing.

Order

#183 keeps all of main's keyword mappings and its write_keyword_paren helper is built on write_keyword, which this PR deletes, so the two conflict. Two options:

Either way I'd ask that step 1 survives, because that is what tsrx and Ripple are using today.

@Rich-Harris Rich-Harris changed the title fix: remove unnecessary and inaccurate keyword sourcemap locations fix: remove inaccurate keyword sourcemap locations Sep 22, 2026
Comment thread .changeset/fewer-keyword-mappings.md Resolved

@dummdidumm dummdidumm 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.

a few findings but on the whole looks good

Comment thread src/languages/ts/index.js Resolved
Comment thread src/languages/ts/index.js Resolved
Comment thread src/languages/ts/index.js Resolved
Comment thread src/languages/ts/index.js Resolved
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.

Simplify keyword locations

3 participants