Skip to content

feat: more granular sourcemaps - #198

Merged
Rich-Harris merged 9 commits into
mainfrom
more-granular-sourcemaps
Sep 24, 2026
Merged

Rich-Harris merged 9 commits into
mainfrom
more-granular-sourcemaps

Conversation

@Rich-Harris

Copy link
Copy Markdown
Member

This PR adds mappings around every visited node with a loc property, making sourcemaps more granular without us needing to do context.location(...) at a bunch of different sites.

Notably, this includes things like Property and MethodDefinition, which means that opening [ characters on computed property keys are correctly mapped. In the common case where the closing ] immediately follows the key expression, this is sufficient for TypeScript diagnostics to match perfectly; in the uncommon case, it degrades to acceptable behaviour:

class Bag implements Iterable<string> {
  [ Symbol.iterator ]() {
  ^^^^^^^^^^^^^^^^^^
    return 42;
  }
}

See #183 (comment) for the explanation/repro

To get this more correct behaviour, boundaryTokens (from #148) must be false. Candidly, we should delete this API, without regard for whether it's a breaking change. It was merged carelessly and without authority, and would not have passed a substantive review. Even aside from the buggy behaviour, it's not a desirable API.

(It may still be necessary to merge some version of #183 to get return-type hints etc; that remains to be seen. It certainly makes it less urgent. Regardless, boundaryTokens would be unnecessary there too — the presence of tokens would be sufficient as an API.)

One design note: the logic exists in the ts root visitor, rather than on context.visit, because the latter doesn't know about loc. We could change that, and then any other visitors would inherit the same behaviour. For now I've left that as a future option.

@pkg-svelte-dev

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

Copy link
Copy Markdown

Install the latest version of esrap from a58cc62:

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

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

@changeset-bot

changeset-bot Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: a58cc62

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

@Rich-Harris

Copy link
Copy Markdown
Member Author

Actually, I misspoke — context.visit can know about loc:

diff --git a/src/context.js b/src/context.js
index 3f59d20..39ac267 100644
--- a/src/context.js
+++ b/src/context.js
@@ -84,7 +84,7 @@ export class Context {
 	}
 
 	/**
-	 * @param {{ type: string }} node
+	 * @param {BaseNode} node
 	 */
 	visit(node) {
 		const visitor = this.#visitors[node.type];
@@ -103,6 +103,10 @@ export class Context {
 			throw new Error(message);
 		}
 
+		if (node.loc) {
+			this.location(node.loc.start.line, node.loc.start.column);
+		}
+
 		if (this.#visitors._) {
 			// @ts-ignore
 			this.#visitors._(node, this, (node) => visitor(node, this));
@@ -110,6 +114,10 @@ export class Context {
 			// @ts-ignore
 			visitor(node, this);
 		}
+
+		if (node.loc) {
+			this.location(node.loc.end.line, node.loc.end.column);
+		}
 	}
 
 	empty() {
diff --git a/src/languages/ts/index.js b/src/languages/ts/index.js
index 94bfa70..08d64e8 100644
--- a/src/languages/ts/index.js
+++ b/src/languages/ts/index.js
@@ -1124,16 +1124,10 @@ export default (options = {}) => {
 					true,
 					node.type in EXPRESSIONS_PRECEDENCE && !BINDINGS.has(node)
 				);
-
-				context.location(node.loc.start.line, node.loc.start.column);
 			}
 
 			visit(node);
 
-			if (node.loc) {
-				context.location(node.loc.end.line, node.loc.end.column);
-			}
-
 			if (jsdoc_type_casts > 0) {
 				context.write(')'.repeat(jsdoc_type_casts));
 			}

Some tests fail when I make that change; will take a closer look tomorrow.

Worth noting that the second argument to context.write would no longer be necessary with this change (though getting rid of it would require a major bump)

@Rich-Harris

Copy link
Copy Markdown
Member Author

Okay, the logic does need to live in ts rather than context, because context is unaware of comments (which in some cases need to be inserted before the node itself, i.e. you can't map the node start and then add comments that precede it). So I think this is ready to go

leonidaz added a commit to tsrx-org/tsrx that referenced this pull request Sep 23, 2026
Point the name-anchor fallback at #208, which tracks replacing it with an
ordinary token once esrap maps the `#` itself (sveltejs/esrap#198).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
leonidaz added a commit to tsrx-org/tsrx that referenced this pull request Sep 23, 2026
* fix(core): map private names so their diagnostics report (#203)

`tsrx-tsc` exited 0 for `export class Model { #value: number = "bad"; }`
and for an uninitialized `#value: number`. TypeScript reports both errors
(TS2322, TS2564) on the private name, but the source-mapping walker in
segments.js returned early for PrivateIdentifier without adding a mapping.
Volar drops any diagnostic it cannot map back to the source.

Map each private name as the whole `#name`. esrap prints the `#` on its
own and anchors the node's source start on the name after it. The lookup
accepts that anchor or one on the `#`. The mapping covers field, method,
and accessor keys, `this.#name` reads, `#name in obj` checks, and the
extra source position a dynamic tag's closing `</{this.#Tag}>` records.

Fixes #203

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs(core): mark the private-name esrap workaround for removal (#208)

Point the name-anchor fallback at #208, which tracks replacing it with an
ordinary token once esrap maps the `#` itself (sveltejs/esrap#198).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@teemingc

Copy link
Copy Markdown
Member

Seems like the tests are failing after merging main

leonidaz added a commit to tsrx-org/tsrx that referenced this pull request Sep 23, 2026
…ort (#210) (#217)

`tsrx-tsc` exited 0 for `export class Model { value(): number {} }`,
which TypeScript rejects with TS2355. TypeScript reports missing-return
errors (TS2355, TS2366, TS2534, TS1064) on the return type, but the
source-mapping walker in segments.js returned early for primitive type
keywords without adding a mapping. Volar drops any diagnostic it cannot
map back to the source.

Map each primitive type keyword (`number`, `string`, `this`, and the
rest) as a token, as identifiers are. esrap writes these keywords with
their node's location.

The walker also mapped a method's value as a `function` keyword when the
file had no `function` or `async` keyword at all: an empty collected
token list fell through to arithmetic meant for callers that collect no
tokens, which mapped the 8 characters after the method's `(`. That stray
span is how `value(): this {}` reported before. An empty list now means
there is no keyword, and the arithmetic fallback checks the source text.

Composite return types (`number[]`, `{ a: number }`, `keyof T`) still
lose these diagnostics because esrap records no location at their
boundaries. #216 tracks replacing the keyword tokens with a whole return
type mapping once esrap ships sveltejs/esrap#198.

Fixes #210

Co-authored-by: Claude Opus 5.5 <[email protected]>
Comment thread src/index.js Resolved

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

Was a little daunted to review this at first but most of the file changes are test fixtures and GPT helped explain the codebase a bit

Co-authored-by: Tee Ming <[email protected]>
@Rich-Harris
Rich-Harris merged commit 49d79ea into main Sep 24, 2026
7 checks passed
@Rich-Harris
Rich-Harris deleted the more-granular-sourcemaps branch September 24, 2026 00:55
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