Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
create an error with a code frame when parsing fails
Browse files Browse the repository at this point in the history
By calling `this.error` in `transform` instead of throwing the error
directly (in `tryParse`) rollup will augment the error and add a code
frame based on the given location before throwing it.
  • Loading branch information
unstubbable committed Feb 5, 2018
1 parent d195bab commit 9674a6c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Expand Up @@ -187,6 +187,8 @@ export default function commonjs ( options = {} ) {

commonjsModules.set( id, true );
return transformed;
}).catch(err => {
this.error(err, err.loc);
});
}
};
Expand Down
1 change: 1 addition & 0 deletions test/samples/invalid-syntax/main.js
@@ -0,0 +1 @@
export const foo = 2,
11 changes: 11 additions & 0 deletions test/test.js
Expand Up @@ -482,5 +482,16 @@ describe( 'rollup-plugin-commonjs', () => {
});
assert.equal( warns.length, 0 );
});

it( 'creates an error with a code frame when parsing fails', async () => {
try {
await rollup({
input: 'samples/invalid-syntax/main.js',
plugins: [ commonjs() ]
});
} catch (error) {
assert.equal( error.frame, '1: export const foo = 2,\n ^' );
}
});
});
});

0 comments on commit 9674a6c

Please sign in to comment.