Empty template spans break sourcemaps
Septh opened this issue · 1 comments
I have set up a repro here: https://github.com/Septh/sucrase-broken-sourcemaps.
After cloning and installing, run node sucrase.js
to compile the TypeScript code; you can then upload the resulting index.js
file to Evan Wallace's excellent source-map-visualization tool.
Here's what I get:
As you can see, the original column number stays 'stuck' at the end of the first template string.
I did some digging and I think i figured out what's going on.
The issue arises only when the tokenizer generates zero-length tt.template
tokens:
`foo ${var} bar` // <- sourcemaps are correct
`foo ${var}` // <- sourcemaps are wrong (last tt.template is 2:10-2:10)
`${var} bar` // <- sourcemaps are wrong (first tt.template is 3:2-3:2)
(the line:column
values come from the playground).
When a token is empty, the computeSourceColumns
function in /src/computeSourceMap.ts
fails to map the following tokens to their original source code positions because the condition at line 79 will never be true again.