Skip to content

Commit

Permalink
fix bundle mappings after remove and move in multiple sources (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
kzc authored and mourner committed Jan 13, 2020
1 parent 900c6cb commit c029ac7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/utils/Mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

This comment has been minimized.

Copy link
@lukastaegert

lukastaegert Mar 6, 2020

Contributor

@Rich-Harris @mourner As this is already on master and actually an important fix, it would be really nice to have it released, would this be possible? Would be nice to have this in Rollup 2.

This comment has been minimized.

Copy link
@Rich-Harris

Rich-Harris Mar 6, 2020

Owner

done — thanks

}

advance(str) {
Expand Down
51 changes: 51 additions & 0 deletions test/MagicString.Bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,4 +666,55 @@ var template = (function () {
assert.strictEqual(b.toString(), 'abcdef');
});
});

describe('mappings', () => {
it('should produce correct mappings after remove and move in multiple sources', () => {
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(2,4); // ABE
ms1.move(0, 1, 5); // BEA

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);

const result1 = ms1.toString();
assert.strictEqual(result1, 'BEA');

const result2 = ms2.toString();
assert.strictEqual(result2, 'WZV');

assert.strictEqual(bundle.toString(), 'BEA\nWZV');

// B = B
// E = E
// A = A
let line = 1;
for (let i = 0; i < result1.length; i++) {
const loc = smc.originalPositionFor({ line, column: i });
assert.strictEqual(s1[loc.column], result1[i]);
}

// W = W
// Z = Z
// V = V
line = 2;
for (let i = 0; i < result2.length; i++) {
const loc = smc.originalPositionFor({ line, column: i });
assert.strictEqual(s2[loc.column], result2[i]);
}

assert.strictEqual(map.toString(), '{"version":3,"file":"result","sources":["first","second"],"sourcesContent":["ABCDE","VWXYZ"],"names":[],"mappings":"AAAC,CAAG,CAAJ;ACAC,CAAG,CAAJ"}');
});
});
});

0 comments on commit c029ac7

Please sign in to comment.