Browse Source

Merge pull request #433 from rollup/gh-430

Chained namespace imports
gh-438-b
Rich Harris 9 years ago
parent
commit
f3b2f95cca
  1. 7
      src/Declaration.js
  2. 30
      src/Reference.js
  3. 25
      src/Statement.js
  4. 3
      test/function/import-chain-as/_config.js
  5. 1
      test/function/import-chain-as/first.js
  6. 5
      test/function/import-chain-as/main.js
  7. 2
      test/function/import-chain-as/second.js

7
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;

30
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 = [];
}
}

25
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 ) {

3
test/function/import-chain-as/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'imports as- chained exports'
};

1
test/function/import-chain-as/first.js

@ -0,0 +1 @@
export var value = 42;

5
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 } });

2
test/function/import-chain-as/second.js

@ -0,0 +1,2 @@
import * as first from './first';
export { first };
Loading…
Cancel
Save