From 41bfd877f3c47ad434251f7fe29cb456db611afa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Thu, 8 Oct 2015 10:34:13 +0200 Subject: [PATCH 1/2] Informative error message when namespace property doesn't exist TypeError: Cannot read property 'addReference' of undefined -> Export 'foo' is not defined by '' --- src/Module.js | 8 ++++++++ test/function/namespace-missing-export/_config.js | 12 ++++++++++++ test/function/namespace-missing-export/empty.js | 0 test/function/namespace-missing-export/main.js | 3 +++ 4 files changed, 23 insertions(+) create mode 100644 test/function/namespace-missing-export/_config.js create mode 100644 test/function/namespace-missing-export/empty.js create mode 100644 test/function/namespace-missing-export/main.js diff --git a/src/Module.js b/src/Module.js index 96d6869..b8ea3a2 100644 --- a/src/Module.js +++ b/src/Module.js @@ -76,6 +76,14 @@ class SyntheticNamespaceDeclaration { const original = this.originals[ reference.name ]; + // throw with an informative error message if the reference doesn't exist. + if ( !original ) { + const err = new Error( `Export '${reference.name}' is not defined by '${this.module.id}'` ); + err.code = 'MISSING_EXPORT'; + err.file = this.id; + throw err; + } + original.addReference( reference ); return; } diff --git a/test/function/namespace-missing-export/_config.js b/test/function/namespace-missing-export/_config.js new file mode 100644 index 0000000..6236fac --- /dev/null +++ b/test/function/namespace-missing-export/_config.js @@ -0,0 +1,12 @@ +var assert = require( 'assert' ); + +module.exports = { + solo: true, + + error: function ( err ) { + console.log( err.message ); + // assert.equal( path.normalize(err.file), path.resolve( __dirname, 'main.js' ) ); + // assert.deepEqual( err.loc, { line: 8, column: 0 }); + assert.ok( /Export "foo" is not defined by/.test( err.message ) ); + } +}; diff --git a/test/function/namespace-missing-export/empty.js b/test/function/namespace-missing-export/empty.js new file mode 100644 index 0000000..e69de29 diff --git a/test/function/namespace-missing-export/main.js b/test/function/namespace-missing-export/main.js new file mode 100644 index 0000000..bfb885d --- /dev/null +++ b/test/function/namespace-missing-export/main.js @@ -0,0 +1,3 @@ +import * as mod from './empty.js'; + +mod.foo(); From 4e692342faf4f11b91b6b0af54b69e53b9816bae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Thu, 8 Oct 2015 10:41:49 +0200 Subject: [PATCH 2/2] Fix foolish mistakes --- src/Module.js | 2 +- test/function/namespace-missing-export/_config.js | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Module.js b/src/Module.js index b8ea3a2..3a5350e 100644 --- a/src/Module.js +++ b/src/Module.js @@ -80,7 +80,7 @@ class SyntheticNamespaceDeclaration { if ( !original ) { const err = new Error( `Export '${reference.name}' is not defined by '${this.module.id}'` ); err.code = 'MISSING_EXPORT'; - err.file = this.id; + err.file = this.module.id; throw err; } diff --git a/test/function/namespace-missing-export/_config.js b/test/function/namespace-missing-export/_config.js index 6236fac..963724b 100644 --- a/test/function/namespace-missing-export/_config.js +++ b/test/function/namespace-missing-export/_config.js @@ -1,12 +1,9 @@ var assert = require( 'assert' ); +var path = require( 'path' ); module.exports = { - solo: true, - error: function ( err ) { - console.log( err.message ); - // assert.equal( path.normalize(err.file), path.resolve( __dirname, 'main.js' ) ); - // assert.deepEqual( err.loc, { line: 8, column: 0 }); - assert.ok( /Export "foo" is not defined by/.test( err.message ) ); + assert.equal( path.normalize( err.file ), path.resolve( __dirname, 'empty.js' ) ); + assert.ok( /Export 'foo' is not defined by/.test( err.message ) ); } };