From 9674a6ca315b6b23dc28d3e516ae65c69228a0ae Mon Sep 17 00:00:00 2001 From: Hendrik Liebau Date: Mon, 5 Feb 2018 21:36:01 +0100 Subject: [PATCH] create an error with a code frame when parsing fails 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. --- src/index.js | 2 ++ test/samples/invalid-syntax/main.js | 1 + test/test.js | 11 +++++++++++ 3 files changed, 14 insertions(+) create mode 100644 test/samples/invalid-syntax/main.js diff --git a/src/index.js b/src/index.js index f9c4f60..d3b2a32 100644 --- a/src/index.js +++ b/src/index.js @@ -187,6 +187,8 @@ export default function commonjs ( options = {} ) { commonjsModules.set( id, true ); return transformed; + }).catch(err => { + this.error(err, err.loc); }); } }; diff --git a/test/samples/invalid-syntax/main.js b/test/samples/invalid-syntax/main.js new file mode 100644 index 0000000..aabc960 --- /dev/null +++ b/test/samples/invalid-syntax/main.js @@ -0,0 +1 @@ +export const foo = 2, \ No newline at end of file diff --git a/test/test.js b/test/test.js index f7f8723..647fff8 100644 --- a/test/test.js +++ b/test/test.js @@ -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 ^' ); + } + }); }); });