From 0a021c799b040e079efec0ebafaec6c135dc05da Mon Sep 17 00:00:00 2001
From: Rich Harris <richard.a.harris@gmail.com>
Date: Thu, 21 May 2015 10:46:09 -0400
Subject: [PATCH] rename external namespace imports correctly

---
 src/Bundle.js                                            | 3 ++-
 src/ExternalModule.js                                    | 5 ++---
 src/Module.js                                            | 9 ++++++---
 .../_config.js                                           | 3 +++
 .../main.js                                              | 6 ++++++
 5 files changed, 19 insertions(+), 7 deletions(-)
 create mode 100644 test/function/import-namespace-from-external-module-renamed/_config.js
 create mode 100644 test/function/import-namespace-from-external-module-renamed/main.js

diff --git a/src/Bundle.js b/src/Bundle.js
index 7e5929a..45babe4 100644
--- a/src/Bundle.js
+++ b/src/Bundle.js
@@ -105,7 +105,8 @@ export default class Bundle {
 
 		// Assign names to external modules
 		this.externalModules.forEach( module => {
-			let name = makeLegalIdentifier( module.id );
+			// TODO is this right?
+			let name = makeLegalIdentifier( module.suggestedNames['*'] || module.suggestedNames.default || module.id );
 
 			if ( has( definers, name ) ) {
 				conflicts[ name ] = true;
diff --git a/src/ExternalModule.js b/src/ExternalModule.js
index 19cadad..929312f 100644
--- a/src/ExternalModule.js
+++ b/src/ExternalModule.js
@@ -2,7 +2,6 @@ export default class ExternalModule {
 	constructor ( id ) {
 		this.id = id;
 		this.name = null;
-		this.module = null;
 
 		this.isExternal = true;
 		this.importedByBundle = [];
@@ -10,8 +9,8 @@ export default class ExternalModule {
 		this.canonicalNames = {};
 		this.suggestedNames = {};
 
-		this.needsDefault = null;
-		this.needsNamed = null;
+		this.needsDefault = false;
+		this.needsNamed = false;
 	}
 
 	getCanonicalName ( name ) {
diff --git a/src/Module.js b/src/Module.js
index 9c16d36..cf5bf59 100644
--- a/src/Module.js
+++ b/src/Module.js
@@ -223,11 +223,17 @@ export default class Module {
 				.then( module => {
 					importDeclaration.module = module;
 
+					// suggest names. TODO should this apply to non default/* imports?
 					if ( importDeclaration.name === 'default' ) {
 						// TODO this seems ropey
 						const localName = importDeclaration.localName;
 						const suggestion = has( this.suggestedNames, localName ) ? this.suggestedNames[ localName ] : localName;
 						module.suggestName( 'default', suggestion );
+					} else if ( importDeclaration.name === '*' ) {
+						const localName = importDeclaration.localName;
+						const suggestion = has( this.suggestedNames, localName ) ? this.suggestedNames[ localName ] : localName;
+						module.suggestName( '*', suggestion );
+						module.suggestName( 'default', `${suggestion}__default` );
 					}
 
 					if ( module.isExternal ) {
@@ -242,9 +248,6 @@ export default class Module {
 					}
 
 					if ( importDeclaration.name === '*' ) {
-						module.suggestName( '*', importDeclaration.localName );
-						module.suggestName( 'default', `${importDeclaration.localName}__default` );
-
 						// we need to create an internal namespace
 						if ( !~this.bundle.internalNamespaceModules.indexOf( module ) ) {
 							this.bundle.internalNamespaceModules.push( module );
diff --git a/test/function/import-namespace-from-external-module-renamed/_config.js b/test/function/import-namespace-from-external-module-renamed/_config.js
new file mode 100644
index 0000000..86394b3
--- /dev/null
+++ b/test/function/import-namespace-from-external-module-renamed/_config.js
@@ -0,0 +1,3 @@
+module.exports = {
+	description: 'imports a namespace from an external module and renames it'
+};
diff --git a/test/function/import-namespace-from-external-module-renamed/main.js b/test/function/import-namespace-from-external-module-renamed/main.js
new file mode 100644
index 0000000..2208cd4
--- /dev/null
+++ b/test/function/import-namespace-from-external-module-renamed/main.js
@@ -0,0 +1,6 @@
+import * as node_path from 'path';
+
+var path1 = 'foo/bar/baz';
+var path2 = 'foo/baz/bar';
+
+assert.equal( node_path.relative( path1, path2 ), '../../baz/bar' );