From bc0b6c514a0838921c3985c4a744f4fde6b14846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Thu, 12 Nov 2015 12:07:49 +0100 Subject: [PATCH] Add error message when reexporting missing identifier --- src/Module.js | 12 +++++++++++- test/function/reexport-missing-error/_config.js | 8 ++++++++ test/function/reexport-missing-error/empty.js | 1 + test/function/reexport-missing-error/main.js | 1 + 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/function/reexport-missing-error/_config.js create mode 100644 test/function/reexport-missing-error/empty.js create mode 100644 test/function/reexport-missing-error/main.js diff --git a/src/Module.js b/src/Module.js index 42eaa7a..cee48de 100644 --- a/src/Module.js +++ b/src/Module.js @@ -194,6 +194,7 @@ export default class Module { else { node.specifiers.forEach( specifier => { this.reexports[ specifier.exported.name ] = { + start: specifier.start, source, localName: specifier.local.name, module: null // filled in later @@ -724,7 +725,16 @@ export default class Module { // export { foo } from './other.js' const reexportDeclaration = this.reexports[ name ]; if ( reexportDeclaration ) { - return reexportDeclaration.module.traceExport( reexportDeclaration.localName ); + const declaration = reexportDeclaration.module.traceExport( reexportDeclaration.localName ); + + if ( !declaration ) { + const err = new Error( `'${reexportDeclaration.localName}' is not exported by '${reexportDeclaration.module.id}' (imported by '${this.id}')` ); + err.file = this.id; + err.loc = getLocation( this.code, reexportDeclaration.start ); + throw err; + } + + return declaration; } const exportDeclaration = this.exports[ name ]; diff --git a/test/function/reexport-missing-error/_config.js b/test/function/reexport-missing-error/_config.js new file mode 100644 index 0000000..6918683 --- /dev/null +++ b/test/function/reexport-missing-error/_config.js @@ -0,0 +1,8 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'reexporting a missing identifier should print an error', + error: function ( error ) { + assert.ok( /^'foo' is not exported/.test( error.message ) ); + } +}; diff --git a/test/function/reexport-missing-error/empty.js b/test/function/reexport-missing-error/empty.js new file mode 100644 index 0000000..8d5a2a2 --- /dev/null +++ b/test/function/reexport-missing-error/empty.js @@ -0,0 +1 @@ +// this space left intentionally blank diff --git a/test/function/reexport-missing-error/main.js b/test/function/reexport-missing-error/main.js new file mode 100644 index 0000000..2440c7f --- /dev/null +++ b/test/function/reexport-missing-error/main.js @@ -0,0 +1 @@ +export { foo as bar } from './empty.js';