Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

source mapping bug with multiple sources #172

Closed
kzc opened this issue Jan 11, 2020 · 0 comments · Fixed by #173
Closed

source mapping bug with multiple sources #172

kzc opened this issue Jan 11, 2020 · 0 comments · Fixed by #173

Comments

@kzc
Copy link
Contributor

kzc commented Jan 11, 2020

The fix in #159 wasn't general enough - see rollup/rollup#3001 (comment).

Using [email protected]:

$ cat bug.js
const MagicString = require('./dist/magic-string.umd.js')
const SourceMapConsumer = require( 'source-map' ).SourceMapConsumer;

const s1 = 'ABCDE';
const ms1 = new MagicString(s1, { filename: 'first' });

const s2 = 'VWXYZ';
const ms2 = new MagicString(s2, { filename: 'second' });

const bundle = new MagicString.Bundle();
bundle.addSource(ms1);
bundle.addSource(ms2);

ms1.remove(1,4);   // AE
ms1.move(0, 1, 5); // EA

ms2.remove(2,4);   // VWZ
ms2.move(0, 1, 5); // WZV

const map = bundle.generateMap({file: 'result', hires: true, includeContent: true});
const smc = new SourceMapConsumer(map);

let output = '';
output += bundle.toString() + '\n';;

const result1 = ms1.toString();
const result2 = ms2.toString();

var line = 1;
for (let i = 0; i < result1.length; i++) {
    let loc = smc.originalPositionFor({ line: line, column: i });
    output += `${s1[loc.column]} = ${result1[i]}\n`;
}

var line = 2;
for (let i = 0; i < result2.length; i++) {
    let loc = smc.originalPositionFor({ line: line, column: i });
    output += `${s2[loc.column]} = ${result2[i]}\n`;
}

output += map.toString();
console.log(output);

Incorrect result:

$ cat bug.js | node
EA
WZV
E = E
A = A
W = W
X = Z
V = V
{"version":3,"file":"result","sources":["first","second"],"sourcesContent":["ABCDE","VWXYZ"],"names":[],"mappings":"AAAI,CAAJ;ACAC,CAAC,AAAE,CAAJ"}

Here's the fix:

--- a/src/utils/Mappings.js
+++ b/src/utils/Mappings.js
@@ -48,9 +48,7 @@ export default class Mappings {
                        originalCharIndex += 1;
                }
 
-               this.pending = sourceIndex > 0
-                       ? [this.generatedCodeColumn, sourceIndex, loc.line, loc.column]
-                       : null;
+               this.pending = null;
        }
 
        advance(str) {

Correct result with fix:

$ cat bug.js | node
EA
WZV
E = E
A = A
W = W
Z = Z
V = V
{"version":3,"file":"result","sources":["first","second"],"sourcesContent":["ABCDE","VWXYZ"],"names":[],"mappings":"AAAI,CAAJ;ACAC,CAAG,CAAJ"}
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 a pull request may close this issue.

1 participant