Skip to content

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

Closed

Description

@leonidaz

The mapping at the end of a typed identifier's name points past its type annotation:

function f(id: string) {}
let x: number = 1;

The code prints unchanged, but the segment at the end of id (generated column 13, the :) maps to source column 21, the ) after string. Likewise the end of x (column 5) maps to column 13, after number. A lookup on the generated : therefore resolves to the end of the annotation, and a consumer that builds a source range from the name's start and end mappings gets id: string rather than id.

Identifier writes the name with context.write(name, node), which adds mappings at node.loc.start and node.loc.end. For a typed binding, both Acorn (@sveltejs/acorn-typescript) and @typescript-eslint/typescript-estree give the Identifier a range covering the whole id: string (including ? or ! when present), so node.loc.end isn't the end of the name. Destructured parameters ({ a }: T, [b]: U) are not affected.

This persists on #198, where the generated : gets two segments: this one, then the type annotation's start.

The AST doesn't record where a typed name ends. For typed identifiers, one option is to derive the end from the name's length (which would be off only for names written with unicode escapes). The other is to skip the end mapping.

Reproduced on esrap 2.3.9 (main) with both parsers (typescript-estree 8.70).

Activity

  1. added a commit that references this issue on Sep 25, 2026
    c4110b0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions