Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sourcemap): fall back to low-resolution line mapping (#4334)
* fix(sourcemap): fall back to low-resolution line mapping …inside `Link.traceSegment` instead of returning null, so that a low-resolution sourcemap preceding a high-resolution sourcemap in the sourcemap chain doesn't fail. * fix: fall back to low resolution mapping in `getOriginalLocation` * Generalize low-resolution sourcemap handling and add tests * Slightly speed up mapping generation * Improve coverage Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com> Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
- Loading branch information
1 parent
10dc326
commit b255b52
Showing
7 changed files
with
131 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
test/function/samples/warning-low-resolution-location/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const path = require('path'); | ||
const { encode } = require('sourcemap-codec'); | ||
const ID_MAIN = path.join(__dirname, 'main.js'); | ||
|
||
module.exports = { | ||
description: 'handles when a low resolution sourcemap is used to report an error', | ||
options: { | ||
plugins: { | ||
name: 'test-plugin', | ||
transform() { | ||
// each entry of each line consist of | ||
// [generatedColumn, sourceIndex, sourceLine, sourceColumn]; | ||
// this mapping only maps the first line to itself | ||
const decodedMap = [[[0], [0, 0, 0, 0], [1]]]; | ||
return { code: 'export default this', map: { mappings: encode(decodedMap), sources: [] } }; | ||
} | ||
} | ||
}, | ||
warnings: [ | ||
{ | ||
code: 'THIS_IS_UNDEFINED', | ||
frame: ` | ||
1: console.log('original source'); | ||
^`, | ||
id: ID_MAIN, | ||
loc: { | ||
column: 0, | ||
file: ID_MAIN, | ||
line: 1 | ||
}, | ||
message: | ||
"The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten", | ||
pos: 15, | ||
url: 'https://rollupjs.org/guide/en/#error-this-is-undefined' | ||
} | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('original source'); |
47 changes: 47 additions & 0 deletions
47
test/sourcemaps/samples/transform-low-resolution/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const assert = require('assert'); | ||
const MagicString = require('magic-string'); | ||
const { SourceMapConsumer } = require('source-map'); | ||
const { encode } = require('sourcemap-codec'); | ||
const getLocation = require('../../getLocation'); | ||
|
||
module.exports = { | ||
description: 'handles combining low-resolution and high-resolution source-maps when transforming', | ||
options: { | ||
output: { name: 'bundle' }, | ||
plugins: [ | ||
{ | ||
transform(code) { | ||
// each entry of each line consist of | ||
// [generatedColumn, sourceIndex, sourceLine, sourceColumn]; | ||
// this mapping only maps the second line to the first with no column | ||
// details | ||
const decodedMap = [[], [[0, 0, 0, 0]]]; | ||
return { | ||
code: `console.log('added');\n${code}`, | ||
map: { mappings: encode(decodedMap) } | ||
}; | ||
} | ||
}, | ||
{ | ||
transform(code) { | ||
const s = new MagicString(code); | ||
s.prepend("console.log('second');\n"); | ||
|
||
return { | ||
code: s.toString(), | ||
map: s.generateMap({ hires: true }) | ||
}; | ||
} | ||
} | ||
] | ||
}, | ||
async test(code, map) { | ||
const smc = await new SourceMapConsumer(map); | ||
|
||
const generatedLoc = getLocation(code, code.indexOf("'baz'")); | ||
const originalLoc = smc.originalPositionFor(generatedLoc); | ||
|
||
assert.strictEqual(originalLoc.line, 1); | ||
assert.strictEqual(originalLoc.column, 0); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export let foo = 'bar'; foo += 'baz'; |