Skip to content

Commit

Permalink
adding support for import * as foo from "foo"
Browse files Browse the repository at this point in the history
  • Loading branch information
caridy committed Oct 23, 2014
1 parent 201c7c4 commit 9dd8ff1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
37 changes: 25 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,19 +415,32 @@ SystemFormatter.prototype.buildSetterFunctionDeclarations = function(mod) {

// import {foo} from "foo"; should hoist variables declaration
mod.imports.names.forEach(function (name) {
var importDeclaration = mod.imports.findSpecifierByName(name),
id = mod.getModule(importDeclaration.declaration.node.source.value).id;
var specifier = mod.imports.findSpecifierByName(name),
id = mod.getModule(specifier.declaration.node.source.value).id;

if (specifier.from) {
// import { value } from './a';
// import a from './a';
getFnDeclarationBody(id).push(
b.expressionStatement(b.assignmentExpression("=",
b.identifier(specifier.name),
b.memberExpression(
b.identifier('m'),
b.literal(specifier.from),
true
)
))
);
} else {
// import * as a from './a'
getFnDeclarationBody(id).push(
b.expressionStatement(b.assignmentExpression("=",
b.identifier(specifier.name),
b.identifier('m')
))
);
}

getFnDeclarationBody(id).push(
b.expressionStatement(b.assignmentExpression("=",
b.identifier(importDeclaration.name),
b.memberExpression(
b.identifier('m'),
b.literal(importDeclaration.from),
true
)
))
);
});

mod.exports.names.forEach(function(name) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "es6-module-transpiler-system-formatter",
"version": "0.2.0",
"version": "0.2.1",
"description": "ES6 Module Transpiler Extension to Output System.register() Format.",
"author": "Caridy Patino <caridy@gmail.com>",
"homepage": "https://github.com/caridy/es6-module-transpiler-system-formatter",
Expand All @@ -23,7 +23,7 @@
"recast": "~0.8.0"
},
"devDependencies": {
"es6-module-transpiler": "~0.8.2",
"es6-module-transpiler": "~0.9.0",
"chai": "~1.8.1",
"mocha": "~1.15.1",
"xunit-file": "*",
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* jshint esnext:true */

import * as foo from './3';

var keys = [];
for (var key in foo) {
keys.push(key);
}

export default keys;
10 changes: 10 additions & 0 deletions test/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ describe('system-formatter', function() {
});
});

it('should support import namespace', function(next) {
System.import('build/test/fixtures/4').then(function(mod4) {
expect(mod4.default.length).to.be.equal(3);
expect(mod4.default[0]).to.be.equal('a');
expect(mod4.default[1]).to.be.equal('b');
expect(mod4.default[2]).to.be.equal('z');
next();
});
});

});

0 comments on commit 9dd8ff1

Please sign in to comment.