Skip to content

fix: map while/else/catch/finally/case/default keywords to source - #117

Merged
dummdidumm merged 3 commits into
keyword-sourcemapsfrom
keyword-sourcemaps-more
May 11, 2026
Merged

dummdidumm merged 3 commits into
keyword-sourcemapsfrom
keyword-sourcemaps-more

Conversation

@jycouet

@jycouet jycouet commented May 9, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #116 - extends keyword sourcemap coverage to control-flow keywords that were previously emitted as raw text.

Implementation changes (src/languages/ts/index.js)

  • DoWhileStatement: derive while source position from test.column - 7 when test is on the same line as body end; fall back to plain text otherwise.
  • IfStatement alternate: derive else source position from alternate.column - 5 when alternate is on the same line as consequent end.
  • SwitchCase: route case and default through write_keyword using the case node's own loc.
  • TryStatement: catch uses write_keyword(handler, ...); finally derives its position from finalizer.column - 8 since the BlockStatement finalizer doesn't carry the keyword's loc.
  • handle_var_declaration: collapsed the if (node.loc) / else split into a single create_keyword_write path with (n) => !!n.loc predicate.
  • Minor: dropped redundant @type {string} casts on node.accessibility.

Why the .map updates are better

Each control-flow keyword now has its own segment pointing back to source. Before, while/else/case/default/catch/finally were raw strings with no mapping - debuggers stepping on them would land on the previous mapped token (or nowhere). After: breakpoints on keywords resolve, stack traces pointing at a case/catch show the right source line, devtools highlight the actual keyword.

  • try-catch: new dedicated segments for catch and finally.
  • switch: new segments for case and default.
  • empty-body: do ... while and else if now get separate while/else segments.

Test improvements (test/sourcemap-keywords.test.js)

Previously only class-level keywords were covered (static/get). Added 6 new tests asserting seg[3] === source.indexOf(keyword):

  • throw / return / await
  • if / else
  • try / catch / finally
  • do / while
  • switch / case / default
  • decorator-prefixed class - regression guard for the fallback path (no throw when keyword loc isn't reliably derivable).

@changeset-bot

changeset-bot Bot commented May 9, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 20ded7d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented May 9, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/esrap@117

commit: bed4643

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

nice!

@dummdidumm
dummdidumm merged commit ac4ab00 into keyword-sourcemaps May 11, 2026
8 checks passed
@dummdidumm
dummdidumm deleted the keyword-sourcemaps-more branch May 11, 2026 06:10
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