Skip to content

Commit

Permalink
fix: correct mappings for update containing new line (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Sep 29, 2023
1 parent c655c18 commit adaece9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
21 changes: 20 additions & 1 deletion src/utils/Mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,35 @@ export default class Mappings {

addEdit(sourceIndex, content, loc, nameIndex) {
if (content.length) {
let contentLineEnd = content.indexOf('\n', 0);
let previousContentLineEnd = -1;
while (contentLineEnd >= 0) {
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
if (nameIndex >= 0) {
segment.push(nameIndex);
}
this.rawSegments.push(segment);

this.generatedCodeLine += 1;
this.raw[this.generatedCodeLine] = this.rawSegments = [];
this.generatedCodeColumn = 0;

previousContentLineEnd = contentLineEnd;
contentLineEnd = content.indexOf('\n', contentLineEnd + 1);
}

const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
if (nameIndex >= 0) {
segment.push(nameIndex);
}
this.rawSegments.push(segment);

this.advance(content.slice(previousContentLineEnd + 1));
} else if (this.pending) {
this.rawSegments.push(this.pending);
this.advance(content);
}

this.advance(content);
this.pending = null;
}

Expand Down
30 changes: 26 additions & 4 deletions test/MagicString.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ describe('MagicString', () => {
includeContent: true,
hires: 'boundary'
});

assert.equal(map.mappings, 'AAAA,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAG,CAAC,CAAC,CAAC');

const smc = new SourceMapConsumer(map);
Expand All @@ -469,6 +469,28 @@ describe('MagicString', () => {
assert.equal(loc.line, 1);
assert.equal(loc.column, 33);
});

it('generates a correct source map with update using a content containing a new line', () => {
const s = new MagicString('foobar');
s.update(3, 4, '\nbb');
assert.equal(s.toString(), 'foo\nbbar');

const map = s.generateMap({ hires: true });

const smc = new SourceMapConsumer(map);
const loc = smc.originalPositionFor({ line: 1, column: 3 });
assert.equal(loc.line, 1);
assert.equal(loc.column, 3);
const loc2 = smc.originalPositionFor({ line: 2, column: 0 });
assert.equal(loc2.line, 1);
assert.equal(loc2.column, 3);
const loc3 = smc.originalPositionFor({ line: 2, column: 1 });
assert.equal(loc3.line, 1);
assert.equal(loc3.column, 3);
const loc4 = smc.originalPositionFor({ line: 2, column: 2 });
assert.equal(loc4.line, 1);
assert.equal(loc4.column, 4);
});
});

describe('getIndentString', () => {
Expand Down Expand Up @@ -1490,7 +1512,7 @@ describe('MagicString', () => {
const s = new MagicString(' abcde fghijkl ');

assert.ok(!s.hasChanged());

assert.ok(s.clone().prepend(' ').hasChanged());
assert.ok(s.clone().overwrite(1, 2, 'b').hasChanged());
assert.ok(s.clone().remove(1, 6).hasChanged());
Expand All @@ -1504,7 +1526,7 @@ describe('MagicString', () => {
assert.ok(clone.hasChanged());
});
});

describe('replace', () => {
it('works with string replace', () => {
const code = '1 2 1 2';
Expand Down Expand Up @@ -1575,7 +1597,7 @@ describe('MagicString', () => {
);
});
});

describe('replaceAll', () => {
it('works with string replace', () => {
assert.strictEqual(
Expand Down

0 comments on commit adaece9

Please sign in to comment.