mirror of https://github.com/lukechilds/rollup.git
Rich-Harris
10 years ago
9 changed files with 136 additions and 19 deletions
@ -0,0 +1,39 @@ |
|||||
|
var path = require( 'path' ); |
||||
|
var assert = require( 'assert' ); |
||||
|
var getLocation = require( '../../utils/getLocation' ); |
||||
|
var SourceMapConsumer = require( 'source-map' ).SourceMapConsumer; |
||||
|
|
||||
|
module.exports = { |
||||
|
description: 'basic sourcemap support', |
||||
|
test: function ( code, map ) { |
||||
|
assert.equal( map.version, 3 ); |
||||
|
assert.equal( map.file, 'bundle.js' ); |
||||
|
|
||||
|
var smc = new SourceMapConsumer( map ); |
||||
|
var generatedLoc, originalLoc; |
||||
|
|
||||
|
// main.js
|
||||
|
generatedLoc = getLocation( code, code.indexOf( "console.log( 'hello from main.js' )" ) ); |
||||
|
originalLoc = smc.originalPositionFor( generatedLoc ); |
||||
|
|
||||
|
assert.equal( originalLoc.line, 4 ); |
||||
|
assert.equal( originalLoc.column, 0 ); |
||||
|
assert.equal( path.resolve( originalLoc.source ), path.resolve( __dirname, 'main.js' ) ); |
||||
|
|
||||
|
// foo.js
|
||||
|
generatedLoc = getLocation( code, code.indexOf( "console.log( 'hello from foo.js' )" ) ); |
||||
|
originalLoc = smc.originalPositionFor( generatedLoc ); |
||||
|
|
||||
|
assert.equal( originalLoc.line, 2 ); |
||||
|
assert.equal( originalLoc.column, 1 ); |
||||
|
assert.equal( path.resolve( originalLoc.source ), path.resolve( __dirname, 'foo.js' ) ); |
||||
|
|
||||
|
// bar.js
|
||||
|
generatedLoc = getLocation( code, code.indexOf( "console.log( 'hello from bar.js' )" ) ); |
||||
|
originalLoc = smc.originalPositionFor( generatedLoc ); |
||||
|
|
||||
|
assert.equal( originalLoc.line, 2 ); |
||||
|
assert.equal( originalLoc.column, 1 ); |
||||
|
assert.equal( path.resolve( originalLoc.source ), path.resolve( __dirname, 'bar.js' ) ); |
||||
|
} |
||||
|
}; |
@ -0,0 +1,3 @@ |
|||||
|
export default function bar () { |
||||
|
console.log( 'hello from bar.js' ); |
||||
|
} |
@ -0,0 +1,3 @@ |
|||||
|
export default function foo () { |
||||
|
console.log( 'hello from foo.js' ); |
||||
|
} |
@ -0,0 +1,7 @@ |
|||||
|
import foo from './foo'; |
||||
|
import bar from './bar'; |
||||
|
|
||||
|
console.log( 'hello from main.js' ); |
||||
|
|
||||
|
foo(); |
||||
|
bar(); |
@ -0,0 +1,20 @@ |
|||||
|
module.exports = function getLocation ( source, charIndex ) { |
||||
|
var lines = source.split( '\n' ); |
||||
|
var len = lines.length; |
||||
|
|
||||
|
var lineStart = 0; |
||||
|
var i; |
||||
|
|
||||
|
for ( i = 0; i < len; i += 1 ) { |
||||
|
var line = lines[i]; |
||||
|
var lineEnd = lineStart + line.length + 1; // +1 for newline
|
||||
|
|
||||
|
if ( lineEnd > charIndex ) { |
||||
|
return { line: i + 1, column: charIndex - lineStart }; |
||||
|
} |
||||
|
|
||||
|
lineStart = lineEnd; |
||||
|
} |
||||
|
|
||||
|
throw new Error( 'Could not determine location of character' ); |
||||
|
} |
Loading…
Reference in new issue