Svelte2tsx Fix SourceMapping range endings #973
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As requested by @dummdidumm, this PR is currently only a draft "POC" demonstration of the fix on a subset of the codebase (actions).
Issue
For what I suppose is an unintuitive yet implicit rule of SourceMapping, it appears that looking up the origin of a range (such as an identifier) implies looking up the first character and the character after the last one in the range.
In other words svelte2tsx's sourcemap is wrong whenever the character following a range in tsx does not exactly map to the character following that same range in svelte.
Example
Input
Output
Mapped characters
Issue : looking up
fooreturnsfoTo lookup the original position of
foo, the vscode language server queries the positions offand((is not mapped, its position falls back to the last mapped character in that line,oIn the end, looking up the position of the identifier
fooresolves intofoCurrent workaround
The Svelte language server currently hacks around that issue by monkey patching positions after the fact
language-tools/packages/language-server/src/lib/documents/DocumentMapper.ts
Lines 220 to 228 in 0ac9826
It works but the output is still wrong, meaning that other tools relying on svelte2tsx must monkey patch too.
Unfortunately the Svelte Typescript Plugin #842 cannot access internal typescript ranges, only individual positions.
Solution
This issue occurs in many parts of the codebase. MagicString does not provide a solution, and it's not evident that it would, perhaps because that library is focused on strings, and this would require it to process AST Nodes.
This PR introduces tagged template string helpers, kind of similar to
Rich-Harris/code-redHere is the output for the above example, notice the character after
fooin tsx being mapped to the character afterfooin the original code