Browse Source

dont throw on single-length sourcemap segment (#618)

gh-669
Rich Harris 9 years ago
parent
commit
2b915cb6fc
  1. 3
      src/utils/collapseSourcemaps.js
  2. 37
      test/sourcemaps/single-length-segments/_config.js
  3. 7
      test/sourcemaps/single-length-segments/main.js
  4. 15
      test/sourcemaps/single-length-segments/output.js
  5. 8
      test/sourcemaps/single-length-segments/output.js.map

3
src/utils/collapseSourcemaps.js

@ -71,8 +71,7 @@ Link.prototype = { // TODO bring into line with others post-https://github.com/r
if ( segment[0] === column ) { if ( segment[0] === column ) {
const source = this.sources[ segment[1] ]; const source = this.sources[ segment[1] ];
if ( !source ) return null;
if ( !source ) throw new Error( 'Bad sourcemap' );
return source.traceSegment( segment[2], segment[3], this.names[ segment[4] ] || name ); return source.traceSegment( segment[2], segment[3], this.names[ segment[4] ] || name );
} }

37
test/sourcemaps/single-length-segments/_config.js

@ -0,0 +1,37 @@
var fs = require( 'fs' );
var path = require( 'path' );
var assert = require( 'assert' );
var getLocation = require( '../../utils/getLocation' );
var SourceMapConsumer = require( 'source-map' ).SourceMapConsumer;
var original = fs.readFileSync( path.resolve( __dirname, 'main.js' ), 'utf-8' );
module.exports = {
description: 'handles single-length sourcemap segments',
options: {
plugins: [
{
transform: function () {
return {
code: fs.readFileSync( path.resolve( __dirname, 'output.js' ), 'utf-8' ),
map: fs.readFileSync( path.resolve( __dirname, 'output.js.map' ), 'utf-8' )
};
}
}
],
moduleName: 'x'
},
test: function ( code, map ) {
var smc = new SourceMapConsumer( map );
[ 'Foo', 'log' ].forEach( function ( token ) {
var generatedLoc = getLocation( code, code.indexOf( token ) );
var originalLoc = smc.originalPositionFor( generatedLoc );
var expectedLoc = getLocation( original, original.indexOf( token ) );
assert.ok( /main/.test( originalLoc.source ) );
assert.equal( originalLoc.line, expectedLoc.line );
assert.equal( originalLoc.column, expectedLoc.column );
});
}
};

7
test/sourcemaps/single-length-segments/main.js

@ -0,0 +1,7 @@
class Foo {
bar () {
console.log( 42 );
}
}
export { Foo };

15
test/sourcemaps/single-length-segments/output.js

@ -0,0 +1,15 @@
var Foo = function () {
function Foo() {
babelHelpers.classCallCheck(this, Foo);
}
babelHelpers.createClass(Foo, [{
key: "bar",
value: function bar() {
console.log(42);
}
}]);
return Foo;
}();
export { Foo };

8
test/sourcemaps/single-length-segments/output.js.map

@ -0,0 +1,8 @@
{
"version":3,
"sources":["index.js"],
"names":[],
"mappings":"IAAM,G;;;;;;;wBACE;AACN,WAAQ,GAAR,CAAa,EAAb;AACA;;;;;AAGF,SAAS,GAAT",
"file":"index.js",
"sourcesContent":["class Foo {\n\tbar () {\n\t\tconsole.log( 42 );\n\t}\n}\n\nexport { Foo };\n"]
}
Loading…
Cancel
Save