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 3 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
184 changes: 184 additions & 0 deletions packages/core/integration-tests/test/sourcemaps.js
Expand Up @@ -2,6 +2,7 @@ const assert = require('assert');
const fs = require('@parcel/fs');
const path = require('path');
const mapValidator = require('sourcemap-validator');
const SourceMap = require('../src/SourceMap');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the tests are failing because of this import. Maybe this test should be inside the parcel-bundler package rather than the integration tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably should although maybe it would be a good idea to extract the sourcemap implementation into it's own package, for use in plugins and integration tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah eventually

const {bundler, bundle, run, assertBundleTree} = require('./utils');

describe('sourcemaps', function() {
Expand Down Expand Up @@ -314,4 +315,187 @@ describe('sourcemaps', function() {
);
mapValidator(jsOutput, map);
});

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);
});
});
43 changes: 23 additions & 20 deletions packages/core/parcel-bundler/src/SourceMap.js
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