From 61c3e0a3de80470f167ea9fbeab2eeaa98131eb5 Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Sun, 4 Jan 2015 11:45:09 +0100 Subject: [PATCH 01/12] Modify test expectations --- .../es6-modules-amd/exports-default/expected.js | 16 ++++++++-------- .../es6-modules-amd/overview/expected.js | 2 +- .../es6-modules-umd/exports-default/expected.js | 16 ++++++++-------- .../es6-modules-umd/overview/expected.js | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js b/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js index 1e004cf1f9..fee6f5b81a 100644 --- a/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js @@ -1,17 +1,17 @@ "use strict"; define(["exports"], function (exports) { - exports["default"] = foo; - exports["default"] = 42; - exports["default"] = {}; - exports["default"] = []; - exports["default"] = foo; - exports["default"] = function () {}; + exports = foo; + exports = 42; + exports = {}; + exports = []; + exports = foo; + exports = function () {}; - exports["default"] = function () {}; + exports = function () {}; function foo() {} var Foo = function Foo() {}; - exports["default"] = Foo; + exports = Foo; }); diff --git a/test/fixtures/transformation/es6-modules-amd/overview/expected.js b/test/fixtures/transformation/es6-modules-amd/overview/expected.js index 1c755a0fab..1846b74937 100644 --- a/test/fixtures/transformation/es6-modules-amd/overview/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/overview/expected.js @@ -13,5 +13,5 @@ define(["exports", "foo", "foo-bar", "./directory/foo-bar"], function (exports, exports.test = test; var test = exports.test = 5; - exports["default"] = test; + exports = test; }); diff --git a/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js b/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js index 72d833f2fc..d8e804ede2 100644 --- a/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js @@ -7,17 +7,17 @@ factory(exports); } })(function (exports) { - exports["default"] = foo; - exports["default"] = 42; - exports["default"] = {}; - exports["default"] = []; - exports["default"] = foo; - exports["default"] = function () {}; + exports = foo; + exports = 42; + exports = {}; + exports = []; + exports = foo; + exports = function () {}; - exports["default"] = function () {}; + exports = function () {}; function foo() {} var Foo = function Foo() {}; - exports["default"] = Foo; + exports = Foo; }); diff --git a/test/fixtures/transformation/es6-modules-umd/overview/expected.js b/test/fixtures/transformation/es6-modules-umd/overview/expected.js index e3714c727b..9b26cd1fcd 100644 --- a/test/fixtures/transformation/es6-modules-umd/overview/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/overview/expected.js @@ -19,5 +19,5 @@ exports.test = test; var test = exports.test = 5; - exports["default"] = test; + exports = test; }); From 7ac98c1532ca5d6bdc63d7d114d83df6f4aa8b8e Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Sun, 4 Jan 2015 11:46:28 +0100 Subject: [PATCH 02/12] Add custom export declaration fn for AMD modules --- lib/6to5/transformation/modules/amd.js | 33 +++++++++++++++++++ .../templates/amd-export-default-assign.js | 1 + .../templates/exports-default.js | 1 + 3 files changed, 35 insertions(+) create mode 100644 lib/6to5/transformation/templates/amd-export-default-assign.js create mode 100644 lib/6to5/transformation/templates/exports-default.js diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index bb4b4137ae..af44fe85ea 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -96,6 +96,39 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) { ])); }; +AMDFormatter.prototype.exportDeclaration = function (node, nodes) { + if (node.default) { + var declar = node.declaration; + var assign; + + // exports = VALUE; + var templateName = "exports-default"; + + if (t.isFunction(declar) || !this.hasNonDefaultExports) { + assign = util.template(templateName, { + VALUE: this._pushStatement(declar, nodes) + }, true); + + // hoist to the top if this default is a function + nodes.push(this._hoistExport(declar, assign, 3)); + return; + } else { + // this export isn't a function so we can't hoist it to the top so we need to set it + // at the very end of the file with something like: + // + // module.exports = Object.assign(exports["default"], exports) + // + + assign = util.template("amd-export-default-assign", true); + assign._blockHoist = 0; + + nodes.push(assign); + } + } + + DefaultFormatter.prototype.exportDeclaration.apply(this, arguments); +}; + AMDFormatter.prototype.exportSpecifier = function (specifier, node, nodes) { var self = this; return this._exportSpecifier(function () { diff --git a/lib/6to5/transformation/templates/amd-export-default-assign.js b/lib/6to5/transformation/templates/amd-export-default-assign.js new file mode 100644 index 0000000000..071592cf40 --- /dev/null +++ b/lib/6to5/transformation/templates/amd-export-default-assign.js @@ -0,0 +1 @@ +exports = Object.assign(exports["default"], exports); diff --git a/lib/6to5/transformation/templates/exports-default.js b/lib/6to5/transformation/templates/exports-default.js new file mode 100644 index 0000000000..2e903c2d4c --- /dev/null +++ b/lib/6to5/transformation/templates/exports-default.js @@ -0,0 +1 @@ +exports = VALUE; From 359c4e8786554dadcd3aa3b562b5623ec6252629 Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Sun, 4 Jan 2015 12:51:19 +0100 Subject: [PATCH 03/12] Use `module.exports` in expectations --- .../exports-default/expected.js | 18 +++++++-------- .../es6-modules-amd/overview/expected.js | 4 ++-- .../exports-default/expected.js | 22 +++++++++---------- .../es6-modules-umd/overview/expected.js | 8 +++---- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js b/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js index fee6f5b81a..cd75b3299d 100644 --- a/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js @@ -1,17 +1,17 @@ "use strict"; -define(["exports"], function (exports) { - exports = foo; - exports = 42; - exports = {}; - exports = []; - exports = foo; - exports = function () {}; +define(["exports", "module"], function (exports, module) { + module.exports = foo; + module.exports = 42; + module.exports = {}; + module.exports = []; + module.exports = foo; + module.exports = function () {}; - exports = function () {}; + module.exports = function () {}; function foo() {} var Foo = function Foo() {}; - exports = Foo; + module.exports = Foo; }); diff --git a/test/fixtures/transformation/es6-modules-amd/overview/expected.js b/test/fixtures/transformation/es6-modules-amd/overview/expected.js index 1846b74937..ed099200aa 100644 --- a/test/fixtures/transformation/es6-modules-amd/overview/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/overview/expected.js @@ -1,6 +1,6 @@ "use strict"; -define(["exports", "foo", "foo-bar", "./directory/foo-bar"], function (exports, _foo, _fooBar, _directoryFooBar) { +define(["exports", "module", "foo", "foo-bar", "./directory/foo-bar"], function (exports, module, _foo, _fooBar, _directoryFooBar) { var _interopRequire = function (obj) { return obj && (obj["default"] || obj); }; @@ -13,5 +13,5 @@ define(["exports", "foo", "foo-bar", "./directory/foo-bar"], function (exports, exports.test = test; var test = exports.test = 5; - exports = test; + module.exports = test; }); diff --git a/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js b/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js index d8e804ede2..618b6fd147 100644 --- a/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js @@ -2,22 +2,22 @@ (function (factory) { if (typeof define === "function" && define.amd) { - define(["exports"], factory); + define(["exports", "module"], factory); } else if (typeof exports !== "undefined") { - factory(exports); + factory(exports, module); } -})(function (exports) { - exports = foo; - exports = 42; - exports = {}; - exports = []; - exports = foo; - exports = function () {}; +})(function (exports, module) { + module.exports = foo; + module.exports = 42; + module.exports = {}; + module.exports = []; + module.exports = foo; + module.exports = function () {}; - exports = function () {}; + module.exports = function () {}; function foo() {} var Foo = function Foo() {}; - exports = Foo; + module.exports = Foo; }); diff --git a/test/fixtures/transformation/es6-modules-umd/overview/expected.js b/test/fixtures/transformation/es6-modules-umd/overview/expected.js index 9b26cd1fcd..af8ba6172f 100644 --- a/test/fixtures/transformation/es6-modules-umd/overview/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/overview/expected.js @@ -2,11 +2,11 @@ (function (factory) { if (typeof define === "function" && define.amd) { - define(["exports", "foo", "foo-bar", "./directory/foo-bar"], factory); + define(["exports", "module", "foo", "foo-bar", "./directory/foo-bar"], factory); } else if (typeof exports !== "undefined") { - factory(exports, require("foo"), require("foo-bar"), require("./directory/foo-bar")); + factory(exports, module, require("foo"), require("foo-bar"), require("./directory/foo-bar")); } -})(function (exports, _foo, _fooBar, _directoryFooBar) { +})(function (exports, module, _foo, _fooBar, _directoryFooBar) { var _interopRequire = function (obj) { return obj && (obj["default"] || obj); }; @@ -19,5 +19,5 @@ exports.test = test; var test = exports.test = 5; - exports = test; + module.exports = test; }); From 941a8cc5ea682478ccae17fc8897ec0c27d30644 Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Sun, 4 Jan 2015 12:51:34 +0100 Subject: [PATCH 04/12] Remove unused templates --- lib/6to5/transformation/templates/amd-export-default-assign.js | 1 - lib/6to5/transformation/templates/exports-default.js | 1 - 2 files changed, 2 deletions(-) delete mode 100644 lib/6to5/transformation/templates/amd-export-default-assign.js delete mode 100644 lib/6to5/transformation/templates/exports-default.js diff --git a/lib/6to5/transformation/templates/amd-export-default-assign.js b/lib/6to5/transformation/templates/amd-export-default-assign.js deleted file mode 100644 index 071592cf40..0000000000 --- a/lib/6to5/transformation/templates/amd-export-default-assign.js +++ /dev/null @@ -1 +0,0 @@ -exports = Object.assign(exports["default"], exports); diff --git a/lib/6to5/transformation/templates/exports-default.js b/lib/6to5/transformation/templates/exports-default.js deleted file mode 100644 index 2e903c2d4c..0000000000 --- a/lib/6to5/transformation/templates/exports-default.js +++ /dev/null @@ -1 +0,0 @@ -exports = VALUE; From af4009694a226b2fb78c8c93d04fa32fcaa0b0f2 Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Sun, 4 Jan 2015 12:53:05 +0100 Subject: [PATCH 05/12] Simplify AMD export declaration fn to re-use CommonJS one --- lib/6to5/transformation/modules/amd.js | 32 ++++---------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index af44fe85ea..56387fafb4 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -1,6 +1,7 @@ module.exports = AMDFormatter; var DefaultFormatter = require("./_default"); +var CommonFormatter = require("./common"); var util = require("../../util"); var t = require("../../types"); var _ = require("lodash"); @@ -96,37 +97,12 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) { ])); }; -AMDFormatter.prototype.exportDeclaration = function (node, nodes) { +AMDFormatter.prototype.exportDeclaration = function (node) { if (node.default) { - var declar = node.declaration; - var assign; - - // exports = VALUE; - var templateName = "exports-default"; - - if (t.isFunction(declar) || !this.hasNonDefaultExports) { - assign = util.template(templateName, { - VALUE: this._pushStatement(declar, nodes) - }, true); - - // hoist to the top if this default is a function - nodes.push(this._hoistExport(declar, assign, 3)); - return; - } else { - // this export isn't a function so we can't hoist it to the top so we need to set it - // at the very end of the file with something like: - // - // module.exports = Object.assign(exports["default"], exports) - // - - assign = util.template("amd-export-default-assign", true); - assign._blockHoist = 0; - - nodes.push(assign); - } + this.passModuleArg = true; } - DefaultFormatter.prototype.exportDeclaration.apply(this, arguments); + CommonFormatter.prototype.exportDeclaration.apply(this, arguments); }; AMDFormatter.prototype.exportSpecifier = function (specifier, node, nodes) { From a4c8895dc300541bb4fbfb7d1450abbc44acec25 Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Sun, 4 Jan 2015 12:58:52 +0100 Subject: [PATCH 06/12] Conditionally pass "module" arg to AMD/UMD factory --- lib/6to5/transformation/modules/amd.js | 5 ++++- lib/6to5/transformation/modules/umd.js | 21 +++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index 56387fafb4..bfa4bfcf7c 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -31,12 +31,15 @@ AMDFormatter.prototype.transform = function (ast) { // build an array of module names - var names = [t.literal("exports")].concat(this.buildDependencyLiterals()); + var names = [t.literal("exports")]; + if (this.passModuleArg) names.push(t.literal("module")); + names = names.concat(this.buildDependencyLiterals()); names = t.arrayExpression(names); // build up define container var params = _.values(this.ids); + if (this.passModuleArg) params.unshift(t.identifier("module")); params.unshift(t.identifier("exports")); var container = t.functionExpression(null, params, t.blockStatement(body)); diff --git a/lib/6to5/transformation/modules/umd.js b/lib/6to5/transformation/modules/umd.js index e23c7c6fc2..4f0ec24ce9 100644 --- a/lib/6to5/transformation/modules/umd.js +++ b/lib/6to5/transformation/modules/umd.js @@ -25,22 +25,31 @@ UMDFormatter.prototype.transform = function (ast) { // factory var ids = _.values(this.ids); - var args = [t.identifier("exports")].concat(ids); + var args = [t.identifier("exports")]; + if (this.passModuleArg) args.push(t.identifier("module")); + args = args.concat(ids); var factory = t.functionExpression(null, args, t.blockStatement(body)); // runner - var defineArgs = [t.arrayExpression([t.literal("exports")].concat(names))]; + var defineArgs = [t.literal("exports")]; + if (this.passModuleArg) defineArgs.push(t.literal("module")); + defineArgs = defineArgs.concat(names); + defineArgs = [t.arrayExpression(defineArgs)]; + + var commonArgs = []; + if (this.passModuleArg) commonArgs.push(t.identifier("module")); + commonArgs = commonArgs.concat(names.map(function (name) { + return t.callExpression(t.identifier("require"), [name]); + })); + var moduleName = this.getModuleName(); if (moduleName) defineArgs.unshift(t.literal(moduleName)); var runner = util.template("umd-runner-body", { AMD_ARGUMENTS: defineArgs, - - COMMON_ARGUMENTS: names.map(function (name) { - return t.callExpression(t.identifier("require"), [name]); - }) + COMMON_ARGUMENTS: commonArgs }); // From 17ce21509de57069c653c826fe16a9473a4430fc Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Sun, 4 Jan 2015 12:59:25 +0100 Subject: [PATCH 07/12] Move exports identifier from UMD template to commonArgs --- lib/6to5/transformation/modules/umd.js | 2 +- lib/6to5/transformation/templates/umd-runner-body.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/6to5/transformation/modules/umd.js b/lib/6to5/transformation/modules/umd.js index 4f0ec24ce9..bf7d285b07 100644 --- a/lib/6to5/transformation/modules/umd.js +++ b/lib/6to5/transformation/modules/umd.js @@ -38,7 +38,7 @@ UMDFormatter.prototype.transform = function (ast) { defineArgs = defineArgs.concat(names); defineArgs = [t.arrayExpression(defineArgs)]; - var commonArgs = []; + var commonArgs = [t.identifier("exports")]; if (this.passModuleArg) commonArgs.push(t.identifier("module")); commonArgs = commonArgs.concat(names.map(function (name) { return t.callExpression(t.identifier("require"), [name]); diff --git a/lib/6to5/transformation/templates/umd-runner-body.js b/lib/6to5/transformation/templates/umd-runner-body.js index f3c9659b5b..a97dce38dc 100644 --- a/lib/6to5/transformation/templates/umd-runner-body.js +++ b/lib/6to5/transformation/templates/umd-runner-body.js @@ -2,6 +2,6 @@ if (typeof define === "function" && define.amd) { define(AMD_ARGUMENTS, factory); } else if (typeof exports !== "undefined") { - factory(exports, COMMON_ARGUMENTS); + factory(COMMON_ARGUMENTS); } }); From 520dd7e947b51bc9c28492e35fbf58c0ee065a5c Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Mon, 5 Jan 2015 14:51:50 +0100 Subject: [PATCH 08/12] Add types.TypeOfExpression --- lib/6to5/generation/generators/expressions.js | 5 +++++ lib/6to5/types/alias-keys.json | 1 + lib/6to5/types/builder-keys.json | 1 + lib/6to5/types/visitor-keys.json | 1 + 4 files changed, 8 insertions(+) diff --git a/lib/6to5/generation/generators/expressions.js b/lib/6to5/generation/generators/expressions.js index 17f5dbda68..ea767f86a4 100644 --- a/lib/6to5/generation/generators/expressions.js +++ b/lib/6to5/generation/generators/expressions.js @@ -116,3 +116,8 @@ exports.MemberExpression = function (node, print) { print(node.property); } }; + +exports.TypeOfExpression = function (node, print) { + this.push("typeof "); + print(node.expression); +}; diff --git a/lib/6to5/types/alias-keys.json b/lib/6to5/types/alias-keys.json index 56ad154225..ae414db878 100644 --- a/lib/6to5/types/alias-keys.json +++ b/lib/6to5/types/alias-keys.json @@ -1,5 +1,6 @@ { "ExpressionStatement": ["Statement"], + "TypeOfExpression": ["Statement"], "BreakStatement": ["Statement"], "ContinueStatement": ["Statement"], "DebuggerStatement": ["Statement"], diff --git a/lib/6to5/types/builder-keys.json b/lib/6to5/types/builder-keys.json index e9ac7987be..d5836d6bcb 100644 --- a/lib/6to5/types/builder-keys.json +++ b/lib/6to5/types/builder-keys.json @@ -7,6 +7,7 @@ "CallExpression": ["callee", "arguments"], "ConditionalExpression": ["test", "consequent", "alternate"], "ExpressionStatement": ["expression"], + "TypeOfExpression": ["expression"], "File": ["program", "comments", "tokens"], "FunctionExpression": ["id", "params", "body", "generator"], "Identifier": ["name"], diff --git a/lib/6to5/types/visitor-keys.json b/lib/6to5/types/visitor-keys.json index 32292aa669..7a963a8faf 100644 --- a/lib/6to5/types/visitor-keys.json +++ b/lib/6to5/types/visitor-keys.json @@ -26,6 +26,7 @@ "ExportDeclaration": ["declaration", "specifiers", "source"], "ExportSpecifier": ["id", "name"], "ExpressionStatement": ["expression"], + "TypeOfExpression": ["expression"], "File": ["program"], "ForInStatement": ["left", "right", "body"], "ForOfStatement": ["left", "right", "body"], From 56f1683f06eb73954d0fe5aaf52cbe06d7f7fc90 Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Mon, 5 Jan 2015 14:57:52 +0100 Subject: [PATCH 09/12] Also test for "module" identifier if needed --- lib/6to5/transformation/modules/umd.js | 6 ++++++ lib/6to5/transformation/templates/umd-runner-body.js | 2 +- .../es6-modules-umd/exports-default/expected.js | 2 +- .../transformation/es6-modules-umd/overview/expected.js | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/6to5/transformation/modules/umd.js b/lib/6to5/transformation/modules/umd.js index bf7d285b07..1a502a24a1 100644 --- a/lib/6to5/transformation/modules/umd.js +++ b/lib/6to5/transformation/modules/umd.js @@ -38,6 +38,11 @@ UMDFormatter.prototype.transform = function (ast) { defineArgs = defineArgs.concat(names); defineArgs = [t.arrayExpression(defineArgs)]; + // typeof exports !== "undefined" && typeof module !== "undefined" + var testExports = t.binaryExpression("!==", t.typeOfExpression(t.identifier("exports")), t.literal("undefined")), + testModule = t.binaryExpression("!==", t.typeOfExpression(t.identifier("module")), t.literal("undefined")), + commonTests = this.passModuleArg ? t.logicalExpression("&&", testExports, testModule) : testExports; + var commonArgs = [t.identifier("exports")]; if (this.passModuleArg) commonArgs.push(t.identifier("module")); commonArgs = commonArgs.concat(names.map(function (name) { @@ -49,6 +54,7 @@ UMDFormatter.prototype.transform = function (ast) { var runner = util.template("umd-runner-body", { AMD_ARGUMENTS: defineArgs, + COMMON_TEST: commonTests, COMMON_ARGUMENTS: commonArgs }); diff --git a/lib/6to5/transformation/templates/umd-runner-body.js b/lib/6to5/transformation/templates/umd-runner-body.js index a97dce38dc..725995b904 100644 --- a/lib/6to5/transformation/templates/umd-runner-body.js +++ b/lib/6to5/transformation/templates/umd-runner-body.js @@ -1,7 +1,7 @@ (function (factory) { if (typeof define === "function" && define.amd) { define(AMD_ARGUMENTS, factory); - } else if (typeof exports !== "undefined") { + } else if (COMMON_TEST) { factory(COMMON_ARGUMENTS); } }); diff --git a/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js b/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js index 618b6fd147..f61ef367a2 100644 --- a/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js @@ -3,7 +3,7 @@ (function (factory) { if (typeof define === "function" && define.amd) { define(["exports", "module"], factory); - } else if (typeof exports !== "undefined") { + } else if (typeof exports !== "undefined" && typeof module !== "undefined") { factory(exports, module); } })(function (exports, module) { diff --git a/test/fixtures/transformation/es6-modules-umd/overview/expected.js b/test/fixtures/transformation/es6-modules-umd/overview/expected.js index af8ba6172f..594322dac5 100644 --- a/test/fixtures/transformation/es6-modules-umd/overview/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/overview/expected.js @@ -3,7 +3,7 @@ (function (factory) { if (typeof define === "function" && define.amd) { define(["exports", "module", "foo", "foo-bar", "./directory/foo-bar"], factory); - } else if (typeof exports !== "undefined") { + } else if (typeof exports !== "undefined" && typeof module !== "undefined") { factory(exports, module, require("foo"), require("foo-bar"), require("./directory/foo-bar")); } })(function (exports, module, _foo, _fooBar, _directoryFooBar) { From abc90778e86ce5afb4eada2e6a499c08c37a709e Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Mon, 5 Jan 2015 21:17:12 +0100 Subject: [PATCH 10/12] Test for exportIdentifier before building exportDeclaration --- lib/6to5/transformation/modules/common.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index d527e49e73..71223d8ef7 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -55,7 +55,7 @@ CommonJSFormatter.prototype.importDeclaration = function (node, nodes) { }; CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) { - if (node.default) { + if (node.default && !this.exportIdentifier) { var declar = node.declaration; var assign; From 216b8c2fd11160e3c80cd3623e014932869bb52d Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Tue, 6 Jan 2015 10:05:44 +0100 Subject: [PATCH 11/12] Revert "Add types.TypeOfExpression" This reverts commit 520dd7e947b51bc9c28492e35fbf58c0ee065a5c. --- lib/6to5/generation/generators/expressions.js | 5 ----- lib/6to5/types/alias-keys.json | 1 - lib/6to5/types/builder-keys.json | 1 - lib/6to5/types/visitor-keys.json | 1 - 4 files changed, 8 deletions(-) diff --git a/lib/6to5/generation/generators/expressions.js b/lib/6to5/generation/generators/expressions.js index ea767f86a4..17f5dbda68 100644 --- a/lib/6to5/generation/generators/expressions.js +++ b/lib/6to5/generation/generators/expressions.js @@ -116,8 +116,3 @@ exports.MemberExpression = function (node, print) { print(node.property); } }; - -exports.TypeOfExpression = function (node, print) { - this.push("typeof "); - print(node.expression); -}; diff --git a/lib/6to5/types/alias-keys.json b/lib/6to5/types/alias-keys.json index ae414db878..56ad154225 100644 --- a/lib/6to5/types/alias-keys.json +++ b/lib/6to5/types/alias-keys.json @@ -1,6 +1,5 @@ { "ExpressionStatement": ["Statement"], - "TypeOfExpression": ["Statement"], "BreakStatement": ["Statement"], "ContinueStatement": ["Statement"], "DebuggerStatement": ["Statement"], diff --git a/lib/6to5/types/builder-keys.json b/lib/6to5/types/builder-keys.json index d5836d6bcb..e9ac7987be 100644 --- a/lib/6to5/types/builder-keys.json +++ b/lib/6to5/types/builder-keys.json @@ -7,7 +7,6 @@ "CallExpression": ["callee", "arguments"], "ConditionalExpression": ["test", "consequent", "alternate"], "ExpressionStatement": ["expression"], - "TypeOfExpression": ["expression"], "File": ["program", "comments", "tokens"], "FunctionExpression": ["id", "params", "body", "generator"], "Identifier": ["name"], diff --git a/lib/6to5/types/visitor-keys.json b/lib/6to5/types/visitor-keys.json index 7a963a8faf..32292aa669 100644 --- a/lib/6to5/types/visitor-keys.json +++ b/lib/6to5/types/visitor-keys.json @@ -26,7 +26,6 @@ "ExportDeclaration": ["declaration", "specifiers", "source"], "ExportSpecifier": ["id", "name"], "ExpressionStatement": ["expression"], - "TypeOfExpression": ["expression"], "File": ["program"], "ForInStatement": ["left", "right", "body"], "ForOfStatement": ["left", "right", "body"], From dfa51954b85712656f3ca5fa096bdb61acf223ed Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Tue, 6 Jan 2015 10:12:08 +0100 Subject: [PATCH 12/12] Use unaryExpression for typeof --- lib/6to5/transformation/modules/umd.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/6to5/transformation/modules/umd.js b/lib/6to5/transformation/modules/umd.js index 1a502a24a1..299a4aee22 100644 --- a/lib/6to5/transformation/modules/umd.js +++ b/lib/6to5/transformation/modules/umd.js @@ -39,8 +39,8 @@ UMDFormatter.prototype.transform = function (ast) { defineArgs = [t.arrayExpression(defineArgs)]; // typeof exports !== "undefined" && typeof module !== "undefined" - var testExports = t.binaryExpression("!==", t.typeOfExpression(t.identifier("exports")), t.literal("undefined")), - testModule = t.binaryExpression("!==", t.typeOfExpression(t.identifier("module")), t.literal("undefined")), + var testExports = t.binaryExpression("!==", t.unaryExpression("typeof", t.identifier("exports"), true), t.literal("undefined")), + testModule = t.binaryExpression("!==", t.unaryExpression("typeof", t.identifier("module"), true), t.literal("undefined")), commonTests = this.passModuleArg ? t.logicalExpression("&&", testExports, testModule) : testExports; var commonArgs = [t.identifier("exports")];