From 54124770cd94a0a9e6095031e8e835f080c7b64b Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Wed, 12 Oct 2016 09:26:01 -0400 Subject: [PATCH] fix missing namespace member warnings (closes #1045) --- src/ast/nodes/MemberExpression.js | 14 ++++++++++++-- test/function/namespace-missing-export/_config.js | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/ast/nodes/MemberExpression.js b/src/ast/nodes/MemberExpression.js index ea373cd5840..eca57a280f3 100644 --- a/src/ast/nodes/MemberExpression.js +++ b/src/ast/nodes/MemberExpression.js @@ -1,3 +1,5 @@ +import getLocation from '../../utils/getLocation.js'; +import relativeId from '../../utils/relativeId.js'; import isReference from '../utils/isReference.js'; import Node from '../Node.js'; import { UNKNOWN } from '../values.js'; @@ -26,12 +28,16 @@ export default class MemberExpression extends Node { let declaration = scope.findDeclaration( keypath.root.name ); while ( declaration.isNamespace && keypath.parts.length ) { + const exporterId = declaration.module.id; + const part = keypath.parts[0]; declaration = declaration.module.traceExport( part.name ); if ( !declaration ) { - this.module.bundle.onwarn( `Export '${part.name}' is not defined by '${this.module.id}'` ); - break; + const { line, column } = getLocation( this.module.code, this.start ); + this.module.bundle.onwarn( `${relativeId( this.module.id )} (${line}:${column}) '${part.name}' is not exported by '${relativeId( exporterId )}'. See https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module` ); + this.replacement = 'undefined'; + return; } keypath.parts.shift(); @@ -64,6 +70,10 @@ export default class MemberExpression extends Node { if ( name !== this.name ) code.overwrite( this.start, this.end, name, true ); } + else if ( this.replacement ) { + code.overwrite( this.start, this.end, this.replacement, true ); + } + super.render( code, es ); } diff --git a/test/function/namespace-missing-export/_config.js b/test/function/namespace-missing-export/_config.js index 7835ef40ebf..6ecf3b9cdfa 100644 --- a/test/function/namespace-missing-export/_config.js +++ b/test/function/namespace-missing-export/_config.js @@ -3,7 +3,7 @@ var assert = require( 'assert' ); module.exports = { options: { onwarn: function ( msg ) { - assert.ok( /Export 'foo' is not defined by/.test( msg ) ); + assert.equal( msg, `main.js (3:21) 'foo' is not exported by 'empty.js'. See https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module` ); } } };