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(sourcemaps): Handle null mappings properly #2149

Merged
merged 5 commits into from
Jan 7, 2019
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
43 changes: 23 additions & 20 deletions packages/core/parcel-bundler/src/SourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ class SourceMap {

purifyMappings(mappings) {
if (Array.isArray(mappings)) {
return mappings.filter(mapping => {
return (
return mappings.filter(
mapping =>
mapping &&
mapping.source &&
mapping.original &&
typeof mapping.original.line === 'number' &&
mapping.original.line > 0 &&
typeof mapping.original.column === 'number' &&
(typeof mapping.original === 'object' &&
(mapping.original === null ||
(typeof mapping.original.line === 'number' &&
mapping.original.line > 0 &&
typeof mapping.original.column === 'number' &&
mapping.source))) &&
mapping.generated &&
typeof mapping.generated.line === 'number' &&
mapping.generated.line > 0 &&
typeof mapping.generated.column === 'number'
);
});
);
}

return [];
Expand All @@ -34,12 +34,14 @@ class SourceMap {
return map;
}
map = typeof map === 'string' ? JSON.parse(map) : map;
if (map.sourceRoot) delete map.sourceRoot;
return await new SourceMapConsumer(map);
}

async addMap(map, lineOffset = 0, columnOffset = 0) {
if (!(map instanceof SourceMap) && map.version) {
if (typeof map === 'string' || (typeof map === 'object' && map.version)) {
let consumer = await this.getConsumer(map);
if (!consumer) return this;

consumer.eachMapping(mapping => {
this.addConsumerMapping(mapping, lineOffset, columnOffset);
Expand All @@ -55,7 +57,7 @@ class SourceMap {
// Only needs to happen in source-map 0.7
consumer.destroy();
}
} else {
} else if (map.mappings && map.sources) {
if (!map.eachMapping) {
map = new SourceMap(map.mappings, map.sources);
}
Expand Down Expand Up @@ -91,21 +93,22 @@ class SourceMap {
}

addConsumerMapping(mapping, lineOffset = 0, columnOffset = 0) {
let original = null;
if (
!mapping.source ||
!mapping.originalLine ||
(!mapping.originalColumn && mapping.originalColumn !== 0)
typeof mapping.originalLine === 'number' &&
mapping.originalLine > 0 &&
typeof mapping.originalColumn === 'number'
) {
return;
original = {
line: mapping.originalLine,
column: mapping.originalColumn
};
}

this.mappings.push({
source: mapping.source,
source: original ? mapping.source : null,
name: mapping.name,
original: {
line: mapping.originalLine,
column: mapping.originalColumn
},
original,
generated: {
line: mapping.generatedLine + lineOffset,
column: mapping.generatedColumn + columnOffset
Expand Down
199 changes: 199 additions & 0 deletions packages/core/parcel-bundler/test/sourcemaps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
const assert = require('assert');
const fs = require('@parcel/fs');
const SourceMap = require('../src/SourceMap');

describe('sourcemaps', function() {
it('should purify mappings properly', async function() {
let mappings = [
{
source: 'index.js',
name: 'A',
original: {
line: 0,
column: 0
},
generated: {
line: 0,
column: 0
}
},
{
generated: {
line: 1,
column: 0
},
original: null,
source: null,
name: null
},
{
generated: {
line: 1,
column: 0
},
original: null,
source: null,
name: null
},
{
generated: {
line: 1,
column: 0
},
original: {
line: 0,
column: 0
},
source: null,
name: null
},
{
generated: {
line: 1,
column: 0
},
original: {
line: 1,
column: 0
},
source: null,
name: null
},
{
generated: {
line: 1,
column: 0
},
original: {
line: 1,
column: 0
},
source: 'index.js',
name: null
},
{
generated: {
line: 1,
column: 0
},
original: {
line: 0,
column: 0
},
source: 'index.js',
name: null
},
{
source: 'index.js',
name: 'A',
original: {
line: 1,
column: 18
},
generated: {
line: 4,
column: 187
}
}
];

let expectedResult = [
{
generated: {
line: 1,
column: 0
},
original: null,
source: null,
name: null
},
{
generated: {
line: 1,
column: 0
},
original: null,
source: null,
name: null
},
{
generated: {
line: 1,
column: 0
},
original: {
line: 1,
column: 0
},
source: 'index.js',
name: null
},
{
source: 'index.js',
name: 'A',
original: {
line: 1,
column: 18
},
generated: {
line: 4,
column: 187
}
}
];

let sourcemap = new SourceMap(mappings, {});
assert.deepEqual(sourcemap.mappings, expectedResult);
});

it('should be able to handle null mappings properly', async function() {
let mappings = [
{
generated: {
line: 1,
column: 0
},
original: {
line: 1,
column: 0
},
source: 'input.js',
name: 'console'
},
{
generated: {
line: 1,
column: 7
},
original: null,
source: null,
name: null
}
];

let sources = {
'input.js': 'console.log("hello world!");'
};

let sourcemap = new SourceMap(mappings, sources);
assert.equal(sourcemap.mappings.length, 2);
assert.deepEqual(sourcemap.mappings, mappings);

let mapString = sourcemap.stringify('index.map', '/');
let combinedSourcemap = new SourceMap(mappings, sources);
await combinedSourcemap.addMap(mapString);

let newMapString = combinedSourcemap.stringify('index.map', '/');
assert.equal(mapString, newMapString);

let newSourcemap = new SourceMap([], {});
await newSourcemap.addMap(sourcemap);

assert.deepEqual(newSourcemap.mappings, mappings);

newSourcemap = new SourceMap([], {});
await newSourcemap.addMap(mapString);

assert.deepEqual(newSourcemap.mappings, mappings);
});
});
Loading