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

fix bundle mappings after remove and move in multiple sources #173

Merged
merged 1 commit into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}

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"}');
});
});
});