Skip to content

fix: map the end of a typed identifier to the end of its name - #215

Merged
Rich-Harris merged 5 commits into
sveltejs:mainfrom
leonidaz:fix/typed-identifier-end
Sep 25, 2026
Merged

Rich-Harris merged 5 commits into
sveltejs:mainfrom
leonidaz:fix/typed-identifier-end

Conversation

@leonidaz

Copy link
Copy Markdown
Contributor

Closes #205.

For a typed, optional or definite binding, both Acorn and typescript-estree give the Identifier a range that runs to the end of its annotation (id: string, id?: string, x!: number). The Identifier visitor writes the name with the node's location, so the segment at the end of the name pointed past the annotation: in function f(id: string) {} the generated : mapped to the ). A consumer building the name's range from its start and end segments got id: string instead of id.

Change: when the identifier has a type annotation, is optional or is definite, the name is written with an end derived from its length. Untyped identifiers keep the node's own location, so a name written with unicode escapes is unaffected; a typed name written with escapes would be short by the escape length, which I have not seen in practice.

The root visitor's end mapping after the annotation is untouched, so the position after string still maps to the end of the whole declaration. Printed output is unchanged; 36 sample maps change one segment each.

Tests: five cases in sourcemap-keywords.test.js assert that the segment at the end of the name maps to the end of the name and not one column past it, for typed, optional and definite bindings, and that an untyped name is unchanged.

Validation:

  • pnpm test — 320 tests passed
  • pnpm lint — passed
  • pnpm check — the two pre-existing TS2580 Buffer errors in src/index.js on main, nothing new

Like #212, #213 and #214 this appends tests to sourcemap-keywords.test.js and regenerates sample maps, so whichever lands later needs a trivial rebase.

🤖 Generated with Claude Code

For a typed, optional or definite binding both Acorn and typescript-estree give
the Identifier a range that runs to the end of its annotation, so the segment at
the end of the name pointed past the annotation. Derive the name's end from its
length instead. Untyped identifiers keep the node's own location, so names
written with unicode escapes are unaffected.

Closes sveltejs#205

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

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

Copy link
Copy Markdown

Install the latest version of esrap from 8043982:

pnpm add https://pkg.svelte.dev/esrap/c/8043982405b71a3ad6a28205b79ca55af9f72907

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

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 24, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8043982

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

thank you

@Rich-Harris
Rich-Harris merged commit c4110b0 into sveltejs:main Sep 25, 2026
7 checks passed
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.

Sourcemaps: the end of a typed name maps to the end of its type annotation

2 participants