Skip to content

Commit adaece9

Browse files
authored
fix: correct mappings for update containing new line (#261)
1 parent c655c18 commit adaece9

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

src/utils/Mappings.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,35 @@ export default class Mappings {
1212

1313
addEdit(sourceIndex, content, loc, nameIndex) {
1414
if (content.length) {
15+
let contentLineEnd = content.indexOf('\n', 0);
16+
let previousContentLineEnd = -1;
17+
while (contentLineEnd >= 0) {
18+
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
19+
if (nameIndex >= 0) {
20+
segment.push(nameIndex);
21+
}
22+
this.rawSegments.push(segment);
23+
24+
this.generatedCodeLine += 1;
25+
this.raw[this.generatedCodeLine] = this.rawSegments = [];
26+
this.generatedCodeColumn = 0;
27+
28+
previousContentLineEnd = contentLineEnd;
29+
contentLineEnd = content.indexOf('\n', contentLineEnd + 1);
30+
}
31+
1532
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
1633
if (nameIndex >= 0) {
1734
segment.push(nameIndex);
1835
}
1936
this.rawSegments.push(segment);
37+
38+
this.advance(content.slice(previousContentLineEnd + 1));
2039
} else if (this.pending) {
2140
this.rawSegments.push(this.pending);
41+
this.advance(content);
2242
}
2343

24-
this.advance(content);
2544
this.pending = null;
2645
}
2746

test/MagicString.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ describe('MagicString', () => {
447447
includeContent: true,
448448
hires: 'boundary'
449449
});
450-
450+
451451
assert.equal(map.mappings, 'AAAA,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAG,CAAC,CAAC,CAAC');
452452

453453
const smc = new SourceMapConsumer(map);
@@ -469,6 +469,28 @@ describe('MagicString', () => {
469469
assert.equal(loc.line, 1);
470470
assert.equal(loc.column, 33);
471471
});
472+
473+
it('generates a correct source map with update using a content containing a new line', () => {
474+
const s = new MagicString('foobar');
475+
s.update(3, 4, '\nbb');
476+
assert.equal(s.toString(), 'foo\nbbar');
477+
478+
const map = s.generateMap({ hires: true });
479+
480+
const smc = new SourceMapConsumer(map);
481+
const loc = smc.originalPositionFor({ line: 1, column: 3 });
482+
assert.equal(loc.line, 1);
483+
assert.equal(loc.column, 3);
484+
const loc2 = smc.originalPositionFor({ line: 2, column: 0 });
485+
assert.equal(loc2.line, 1);
486+
assert.equal(loc2.column, 3);
487+
const loc3 = smc.originalPositionFor({ line: 2, column: 1 });
488+
assert.equal(loc3.line, 1);
489+
assert.equal(loc3.column, 3);
490+
const loc4 = smc.originalPositionFor({ line: 2, column: 2 });
491+
assert.equal(loc4.line, 1);
492+
assert.equal(loc4.column, 4);
493+
});
472494
});
473495

474496
describe('getIndentString', () => {
@@ -1490,7 +1512,7 @@ describe('MagicString', () => {
14901512
const s = new MagicString(' abcde fghijkl ');
14911513

14921514
assert.ok(!s.hasChanged());
1493-
1515+
14941516
assert.ok(s.clone().prepend(' ').hasChanged());
14951517
assert.ok(s.clone().overwrite(1, 2, 'b').hasChanged());
14961518
assert.ok(s.clone().remove(1, 6).hasChanged());
@@ -1504,7 +1526,7 @@ describe('MagicString', () => {
15041526
assert.ok(clone.hasChanged());
15051527
});
15061528
});
1507-
1529+
15081530
describe('replace', () => {
15091531
it('works with string replace', () => {
15101532
const code = '1 2 1 2';
@@ -1575,7 +1597,7 @@ describe('MagicString', () => {
15751597
);
15761598
});
15771599
});
1578-
1600+
15791601
describe('replaceAll', () => {
15801602
it('works with string replace', () => {
15811603
assert.strictEqual(

0 commit comments

Comments
 (0)