Skip to content

Commit

Permalink
Merge pull request #1728 from misoguy/master
Browse files Browse the repository at this point in the history
Fix error.loc object to match location with the real file after transform plugins
  • Loading branch information
lukastaegert committed Nov 16, 2017
2 parents 013a382 + bb7411a commit d9558ae
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/Module.js
Expand Up @@ -14,6 +14,8 @@ import extractNames from './ast/utils/extractNames.js';
import enhance from './ast/enhance.js';
import clone from './ast/clone.js';
import ModuleScope from './ast/scopes/ModuleScope.js';
import { encode } from 'sourcemap-codec';
import { SourceMapConsumer } from 'source-map';

function tryParse ( module, acornOptions ) {
try {
Expand Down Expand Up @@ -282,14 +284,40 @@ export default class Module {
}
}

getOriginalLocation (sourcemapChain, line, column) {
let location = {
line,
column
};
const filteredSourcemapChain =
sourcemapChain.filter(sourcemap => sourcemap.mappings).map(sourcemap => {
const encodedSourcemap = sourcemap;
if (sourcemap.mappings) {
encodedSourcemap.mappings = encode(encodedSourcemap.mappings);
}
return encodedSourcemap;
});
while (filteredSourcemapChain.length > 0) {
const sourcemap = filteredSourcemapChain.pop();
const smc = new SourceMapConsumer(sourcemap);
location = smc.originalPositionFor({
line: location.line,
column: location.column
});
}
return location;
}

error ( props, pos ) {
if ( pos !== undefined ) {
props.pos = pos;

const { line, column } = locate( this.code, pos, { offsetLine: 1 } ); // TODO trace sourcemaps

props.loc = { file: this.id, line, column };
props.frame = getCodeFrame( this.code, line, column );
const location = this.getOriginalLocation(this.sourcemapChain, line, column);

props.loc = { file: this.id, line: location.line, column: location.column };
props.frame = getCodeFrame( this.originalCode, location.line, location.column );
}

error( props );
Expand Down
@@ -0,0 +1,39 @@
var path = require( 'path' );
var assert = require( 'assert' );
var MagicString = require( 'magic-string' );

module.exports = {
description: 'error after transform should throw with correct location of file',
options: {
plugins: [
{
transform: function ( source, id ) {
var s = new MagicString( source );
s.prepend( "import _assign from 'object-assign';\n" );

return {
code: s.toString(),
map: s.generateMap({ hires: true })
};
}
}
]
},
error: {
code: 'MISSING_EXPORT',
message: `'default' is not exported by empty.js`,
pos: 44,
loc: {
file: path.resolve( __dirname, 'main.js' ),
line: 1,
column: 7
},
frame: `
1: import a from './empty.js';
^
2:
3: Object.assign({}, a);
`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module`
}
};
Empty file.
@@ -0,0 +1,3 @@
import a from './empty.js';

Object.assign({}, a);

0 comments on commit d9558ae

Please sign in to comment.