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: correct mappings for update containing new line #261

Merged
merged 1 commit into from
Sep 29, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading