diff --git a/lib/6to5/transformation/modules/_default.js b/lib/6to5/transformation/modules/_default.js index c944078b87..de9456cdee 100644 --- a/lib/6to5/transformation/modules/_default.js +++ b/lib/6to5/transformation/modules/_default.js @@ -3,6 +3,7 @@ module.exports = DefaultFormatter; var traverse = require("../../traverse"); +var object = require("../../helpers/object"); var util = require("../../util"); var t = require("../../types"); var _ = require("lodash"); @@ -28,7 +29,7 @@ var exportsVisitor = { }; DefaultFormatter.prototype.getLocalExports = function () { - var localExports = util.object(); + var localExports = object(); traverse(this.file.ast, exportsVisitor, this.file.scope, localExports); return localExports; }; @@ -42,14 +43,14 @@ var importsVisitor = { }; DefaultFormatter.prototype.getLocalImports = function () { - var localImports = util.object(); + var localImports = object(); traverse(this.file.ast, importsVisitor, this.file.scope, localImports); return localImports; }; DefaultFormatter.prototype.isLocalReference = function (node) { var localImports = this.localImports; - return t.isIdentifier(node) && _.has(localImports, node.name) && localImports[node.name] !== node; + return t.isIdentifier(node) && localImports[node.name] && localImports[node.name] !== node; }; DefaultFormatter.prototype.checkLocalReference = function (node) { @@ -208,7 +209,7 @@ DefaultFormatter.prototype._exportSpecifier = function (getRef, specifier, node, // importing a default so we need to normalise it ref = t.callExpression(this.file.addHelper("interop-require"), [getRef()]); } else { - ref = t.memberExpression(getRef(), specifier.id); + ref = t.memberExpression(getRef(), t.getSpecifierId(specifier)); } // export { foo } from "test"; diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index feb1cee120..9c46440cb5 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -98,7 +98,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) { ref = t.callExpression(this.file.addHelper("interop-require"), [ref]); } else { // import {foo} from "foo"; - ref = t.memberExpression(ref, specifier.id, false); + ref = t.memberExpression(ref, t.getSpecifierId(specifier), false); } nodes.push(t.variableDeclaration("var", [ diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index ec74d66cb1..2bf7f61f1b 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -53,7 +53,7 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) nodes.push(util.template("require-assign-key", { VARIABLE_NAME: variableName, MODULE_NAME: node.source, - KEY: specifier.id + KEY: t.getSpecifierId(specifier) })); } } diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index a485af8248..b6c3bb47cb 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -1,6 +1,6 @@ "use strict"; -var toFastProperties = require("../to-fast-properties"); +var toFastProperties = require("../helpers/to-fast-properties"); var esutils = require("esutils"); var _ = require("lodash"); @@ -597,6 +597,21 @@ t.getSpecifierName = function (specifier) { return specifier.name || specifier.id; }; +/** + * Description + * + * @param {Object} specifier + * @returns {String} + */ + +t.getSpecifierId = function (specifier) { + if (specifier.default) { + return t.identifier("default"); + } else { + return specifier.id; + } +}; + /** * Description * @@ -605,7 +620,7 @@ t.getSpecifierName = function (specifier) { */ t.isSpecifierDefault = function (specifier) { - return t.isIdentifier(specifier.id) && specifier.id.name === "default"; + return specifier.default || t.isIdentifier(specifier.id) && specifier.id.name === "default"; }; toFastProperties(t);