diff --git a/src/Declaration.js b/src/Declaration.js index e1d5ef9..831a966 100644 --- a/src/Declaration.js +++ b/src/Declaration.js @@ -1,5 +1,6 @@ import { blank, keys } from './utils/object.js'; import run from './utils/run.js'; +import { SyntheticReference } from './Reference.js'; export default class Declaration { constructor ( node, isParam, statement ) { @@ -176,6 +177,12 @@ export class SyntheticNamespaceDeclaration { if ( !this.needsNamespaceBlock ) { this.needsNamespaceBlock = true; this.module.bundle.internalNamespaces.push( this ); + + // add synthetic references, in case of chained + // namespace imports + keys( this.originals ).forEach( name => { + this.originals[ name ].addReference( new SyntheticReference( name ) ); + }); } reference.declaration = this; diff --git a/src/Reference.js b/src/Reference.js new file mode 100644 index 0000000..e86ad9b --- /dev/null +++ b/src/Reference.js @@ -0,0 +1,30 @@ +export class Reference { + constructor ( node, scope, statement ) { + this.node = node; + this.scope = scope; + this.statement = statement; + + this.declaration = null; // bound later + + this.parts = []; + + let root = node; + while ( root.type === 'MemberExpression' ) { + this.parts.unshift( root.property.name ); + root = root.object; + } + + this.name = root.name; + + this.start = node.start; + this.end = node.start + this.name.length; // can be overridden in the case of namespace members + this.rewritten = false; + } +} + +export class SyntheticReference { + constructor ( name ) { + this.name = name; + this.parts = []; + } +} diff --git a/src/Statement.js b/src/Statement.js index 9d75f13..06d8827 100644 --- a/src/Statement.js +++ b/src/Statement.js @@ -6,30 +6,7 @@ import isFunctionDeclaration from './ast/isFunctionDeclaration.js'; import isReference from './ast/isReference.js'; import getLocation from './utils/getLocation.js'; import run from './utils/run.js'; - -class Reference { - constructor ( node, scope, statement ) { - this.node = node; - this.scope = scope; - this.statement = statement; - - this.declaration = null; // bound later - - this.parts = []; - - let root = node; - while ( root.type === 'MemberExpression' ) { - this.parts.unshift( root.property.name ); - root = root.object; - } - - this.name = root.name; - - this.start = node.start; - this.end = node.start + this.name.length; // can be overridden in the case of namespace members - this.rewritten = false; - } -} +import { Reference } from './Reference.js'; export default class Statement { constructor ( node, module, start, end ) { diff --git a/test/function/import-chain-as/_config.js b/test/function/import-chain-as/_config.js new file mode 100644 index 0000000..9294fd4 --- /dev/null +++ b/test/function/import-chain-as/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'imports as- chained exports' +}; diff --git a/test/function/import-chain-as/first.js b/test/function/import-chain-as/first.js new file mode 100644 index 0000000..83bab40 --- /dev/null +++ b/test/function/import-chain-as/first.js @@ -0,0 +1 @@ +export var value = 42; diff --git a/test/function/import-chain-as/main.js b/test/function/import-chain-as/main.js new file mode 100644 index 0000000..d1dc2b9 --- /dev/null +++ b/test/function/import-chain-as/main.js @@ -0,0 +1,5 @@ +import * as second from './second'; + +assert.equal( second.first.value, 42 ); +console.log( 'second', second ) +assert.deepEqual( second, { first: { value: 42 } }); diff --git a/test/function/import-chain-as/second.js b/test/function/import-chain-as/second.js new file mode 100644 index 0000000..857ec01 --- /dev/null +++ b/test/function/import-chain-as/second.js @@ -0,0 +1,2 @@ +import * as first from './first'; +export { first };