From e8237910e89218d86225233493a74e75664da2a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Wed, 14 Jan 2015 01:39:37 -0200 Subject: [PATCH 01/15] Add a transformer to import the runtime from a file The `externalRuntime` optional transformer can be used in conjunction with the `runtime` option to import and use the runtime from a module instead of polluting the global environment. --- lib/6to5/transformation/modules/amd.js | 2 +- lib/6to5/transformation/modules/common.js | 12 +++++------- lib/6to5/transformation/transform.js | 1 + .../transformers/optional-external-runtime.js | 11 +++++++++++ 4 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 lib/6to5/transformation/transformers/optional-external-runtime.js diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index 3940c9d18d..d97ba66118 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -87,7 +87,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) { var key = t.getSpecifierName(specifier); var ref = this._push(node); - if (t.isImportBatchSpecifier(specifier)) { + if (t.isImportBatchSpecifier(specifier) || node._noInteropRequire) { // import * as bar from "foo"; } else if (t.isSpecifierDefault(specifier) && !this.noInteropRequire) { // import foo from "foo"; diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index 8e6044f2e9..c44f34078f 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -26,13 +26,11 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) // import foo from "foo"; if (t.isSpecifierDefault(specifier)) { - nodes.push(t.variableDeclaration("var", [ - t.variableDeclarator(variableName, - t.callExpression(this.file.addHelper("interop-require"), [util.template("require", { - MODULE_NAME: node.source - })]) - ) - ])); + var ref = util.template("require", {MODULE_NAME: node.source}); + if (!node._noInteropRequire) { + ref = t.callExpression(this.file.addHelper("interop-require"), [ref]); + } + nodes.push(t.variableDeclaration("var", [t.variableDeclarator(variableName, ref)])); } else { if (specifier.type === "ImportBatchSpecifier") { // import * as bar from "foo"; diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 2a5cbb26a2..a1c7a104ac 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -95,6 +95,7 @@ _.each({ _moduleFormatter: require("./transformers/_module-formatter"), typeofSymbol: require("./transformers/optional-typeof-symbol"), + externalRuntime: require("./transformers/optional-external-runtime"), coreAliasing: require("./transformers/optional-core-aliasing"), undefinedToVoid: require("./transformers/optional-undefined-to-void"), diff --git a/lib/6to5/transformation/transformers/optional-external-runtime.js b/lib/6to5/transformation/transformers/optional-external-runtime.js new file mode 100644 index 0000000000..a2f52667cf --- /dev/null +++ b/lib/6to5/transformation/transformers/optional-external-runtime.js @@ -0,0 +1,11 @@ +exports.optional = true; + +// In theory, it would be more appropriate to do this in `manipulateOptions`, +// but we need an identifier for the import and we can't get that before the +// AST is built. +exports.ast = { + enter: function (ast, file) { + file.opts.runtime = file.addImport(file.opts.runtime, "to5Runtime").name; + file.dynamicImports[file.dynamicImports.length - 1]._noInteropRequire = true; + } +}; From e9a024e58a14c8fdc452425714c8adec83b01544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Wed, 14 Jan 2015 02:38:31 -0200 Subject: [PATCH 02/15] Add simple test for externalRuntime transformer Just import a module namespace and see what happens. --- .../transformation/optional-external-runtime/basic/actual.js | 1 + .../optional-external-runtime/basic/expected.js | 5 +++++ .../transformation/optional-external-runtime/options.json | 4 ++++ 3 files changed, 10 insertions(+) create mode 100644 test/fixtures/transformation/optional-external-runtime/basic/actual.js create mode 100644 test/fixtures/transformation/optional-external-runtime/basic/expected.js create mode 100644 test/fixtures/transformation/optional-external-runtime/options.json diff --git a/test/fixtures/transformation/optional-external-runtime/basic/actual.js b/test/fixtures/transformation/optional-external-runtime/basic/actual.js new file mode 100644 index 0000000000..9e9515cece --- /dev/null +++ b/test/fixtures/transformation/optional-external-runtime/basic/actual.js @@ -0,0 +1 @@ +import * as foo from "someModule"; diff --git a/test/fixtures/transformation/optional-external-runtime/basic/expected.js b/test/fixtures/transformation/optional-external-runtime/basic/expected.js new file mode 100644 index 0000000000..b10bbe6f09 --- /dev/null +++ b/test/fixtures/transformation/optional-external-runtime/basic/expected.js @@ -0,0 +1,5 @@ +"use strict"; + +var _to5Runtime = require("6to5-runtime"); + +var foo = _to5Runtime.interopRequireWildcard(require("someModule")); diff --git a/test/fixtures/transformation/optional-external-runtime/options.json b/test/fixtures/transformation/optional-external-runtime/options.json new file mode 100644 index 0000000000..44c5164a67 --- /dev/null +++ b/test/fixtures/transformation/optional-external-runtime/options.json @@ -0,0 +1,4 @@ +{ + "runtime": "6to5-runtime", + "optional": ["externalRuntime"] +} From 84ee0efe32c1ad17b869c2f7a52d451c09734379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Wed, 14 Jan 2015 12:13:08 -0200 Subject: [PATCH 03/15] Add test for externalRuntime with AMD modules --- .../transformation/optional-external-runtime/amd/actual.js | 1 + .../optional-external-runtime/amd/expected.js | 6 ++++++ .../optional-external-runtime/amd/options.json | 3 +++ .../optional-external-runtime/{basic => commonjs}/actual.js | 0 .../{basic => commonjs}/expected.js | 0 5 files changed, 10 insertions(+) create mode 100644 test/fixtures/transformation/optional-external-runtime/amd/actual.js create mode 100644 test/fixtures/transformation/optional-external-runtime/amd/expected.js create mode 100644 test/fixtures/transformation/optional-external-runtime/amd/options.json rename test/fixtures/transformation/optional-external-runtime/{basic => commonjs}/actual.js (100%) rename test/fixtures/transformation/optional-external-runtime/{basic => commonjs}/expected.js (100%) diff --git a/test/fixtures/transformation/optional-external-runtime/amd/actual.js b/test/fixtures/transformation/optional-external-runtime/amd/actual.js new file mode 100644 index 0000000000..09493dc9be --- /dev/null +++ b/test/fixtures/transformation/optional-external-runtime/amd/actual.js @@ -0,0 +1 @@ +import foo from "someModule"; diff --git a/test/fixtures/transformation/optional-external-runtime/amd/expected.js b/test/fixtures/transformation/optional-external-runtime/amd/expected.js new file mode 100644 index 0000000000..20e759af12 --- /dev/null +++ b/test/fixtures/transformation/optional-external-runtime/amd/expected.js @@ -0,0 +1,6 @@ +define(["exports", "6to5-runtime", "someModule"], function (exports, _to5Runtime2, _someModule) { + "use strict"; + + var _to5Runtime = _to5Runtime2; + var foo = _to5Runtime.interopRequire(_someModule); +}); diff --git a/test/fixtures/transformation/optional-external-runtime/amd/options.json b/test/fixtures/transformation/optional-external-runtime/amd/options.json new file mode 100644 index 0000000000..b76b119f33 --- /dev/null +++ b/test/fixtures/transformation/optional-external-runtime/amd/options.json @@ -0,0 +1,3 @@ +{ + "modules": "amd" +} diff --git a/test/fixtures/transformation/optional-external-runtime/basic/actual.js b/test/fixtures/transformation/optional-external-runtime/commonjs/actual.js similarity index 100% rename from test/fixtures/transformation/optional-external-runtime/basic/actual.js rename to test/fixtures/transformation/optional-external-runtime/commonjs/actual.js diff --git a/test/fixtures/transformation/optional-external-runtime/basic/expected.js b/test/fixtures/transformation/optional-external-runtime/commonjs/expected.js similarity index 100% rename from test/fixtures/transformation/optional-external-runtime/basic/expected.js rename to test/fixtures/transformation/optional-external-runtime/commonjs/expected.js From e985d8b25de9f3b281a8a700c34c64099acc74b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Wed, 14 Jan 2015 14:00:33 -0200 Subject: [PATCH 04/15] Don't use interopRequire for dynamic imports They don't need it at all and this also allows use of `externalRuntime` without fear of the runtime ending up being loaded after it's used. --- lib/6to5/transformation/modules/amd.js | 2 +- lib/6to5/transformation/modules/common.js | 3 ++- .../transformers/optional-external-runtime.js | 1 - .../optional-bluebird-coroutines/expression/expected.js | 6 +----- .../optional-bluebird-coroutines/statement/expected.js | 6 +----- .../optional-core-aliasing/aliased-constructors/expected.js | 6 +----- .../optional-core-aliasing/es6-destructuring/expected.js | 6 +----- .../optional-core-aliasing/es6-for-of/expected.js | 6 +----- .../optional-core-aliasing/es6-spread/expected.js | 6 +----- .../es7-array-comprehensions/expected.js | 6 +----- 10 files changed, 10 insertions(+), 38 deletions(-) diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index d97ba66118..0eb61407b2 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -87,7 +87,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) { var key = t.getSpecifierName(specifier); var ref = this._push(node); - if (t.isImportBatchSpecifier(specifier) || node._noInteropRequire) { + if (t.isImportBatchSpecifier(specifier) || _.contains(this.file.dynamicImports, node)) { // import * as bar from "foo"; } else if (t.isSpecifierDefault(specifier) && !this.noInteropRequire) { // import foo from "foo"; diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index c44f34078f..950b35790e 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -6,6 +6,7 @@ var DefaultFormatter = require("./_default"); var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); +var _ = require("lodash"); function CommonJSFormatter(file) { DefaultFormatter.apply(this, arguments); @@ -27,7 +28,7 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) // import foo from "foo"; if (t.isSpecifierDefault(specifier)) { var ref = util.template("require", {MODULE_NAME: node.source}); - if (!node._noInteropRequire) { + if (!_.contains(this.file.dynamicImports, node)) { ref = t.callExpression(this.file.addHelper("interop-require"), [ref]); } nodes.push(t.variableDeclaration("var", [t.variableDeclarator(variableName, ref)])); diff --git a/lib/6to5/transformation/transformers/optional-external-runtime.js b/lib/6to5/transformation/transformers/optional-external-runtime.js index a2f52667cf..ff39454e4d 100644 --- a/lib/6to5/transformation/transformers/optional-external-runtime.js +++ b/lib/6to5/transformation/transformers/optional-external-runtime.js @@ -6,6 +6,5 @@ exports.optional = true; exports.ast = { enter: function (ast, file) { file.opts.runtime = file.addImport(file.opts.runtime, "to5Runtime").name; - file.dynamicImports[file.dynamicImports.length - 1]._noInteropRequire = true; } }; diff --git a/test/fixtures/transformation/optional-bluebird-coroutines/expression/expected.js b/test/fixtures/transformation/optional-bluebird-coroutines/expression/expected.js index 13943a98de..ed68858b19 100644 --- a/test/fixtures/transformation/optional-bluebird-coroutines/expression/expected.js +++ b/test/fixtures/transformation/optional-bluebird-coroutines/expression/expected.js @@ -1,10 +1,6 @@ "use strict"; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _bluebird = _interopRequire(require("bluebird")); +var _bluebird = require("bluebird"); var foo = _bluebird.coroutine(function* () { var wat = yield bar(); diff --git a/test/fixtures/transformation/optional-bluebird-coroutines/statement/expected.js b/test/fixtures/transformation/optional-bluebird-coroutines/statement/expected.js index 9b200b29f0..c5dbaaf5d1 100644 --- a/test/fixtures/transformation/optional-bluebird-coroutines/statement/expected.js +++ b/test/fixtures/transformation/optional-bluebird-coroutines/statement/expected.js @@ -1,10 +1,6 @@ "use strict"; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _bluebird = _interopRequire(require("bluebird")); +var _bluebird = require("bluebird"); var foo = _bluebird.coroutine(function* foo() { var wat = yield bar(); diff --git a/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js b/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js index 7c6e3f9ba2..8c6ff60ed1 100644 --- a/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js @@ -1,10 +1,6 @@ "use strict"; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(require("core-js/library")); +var _core = require("core-js/library"); obj.constructor === Object; obj.constructor === _core.Promise; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js b/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js index fc43ff5423..15e3ce78b9 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js @@ -16,11 +16,7 @@ var _slicedToArray = function (arr, i) { } }; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(require("core-js/library")); +var _core = require("core-js/library"); var _ref = ["hello", [", ", "junk"], ["world"]]; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js b/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js index 3bf1f53a05..2669fd4d0c 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js @@ -1,10 +1,6 @@ "use strict"; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(require("core-js/library")); +var _core = require("core-js/library"); for (var _iterator = _core.$for.getIterator(arr), _step; !(_step = _iterator.next()).done;) { var i = _step.value; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js b/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js index 4d00347582..2a41ccd997 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js @@ -4,10 +4,6 @@ var _toArray = function (arr) { return Array.isArray(arr) ? arr : _core.Array.from(arr); }; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(require("core-js/library")); +var _core = require("core-js/library"); var lyrics = ["head", "and", "toes"].concat(_toArray(parts)); diff --git a/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js b/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js index 01333ca8f9..0bf67a612a 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js @@ -1,10 +1,6 @@ "use strict"; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(require("core-js/library")); +var _core = require("core-js/library"); var arr = (function () { var _arr = []; From 81bec3e5c4e559cacadbc07dd24aac8d5099caa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Sat, 17 Jan 2015 22:46:02 -0200 Subject: [PATCH 05/15] Move the _blockHoist transformer after regenerator Otherwise wrapped generator functions are hoisted to the top of the scope even if we want something else to come before them. --- lib/6to5/transformation/transform.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 2a5cbb26a2..74079d5d0b 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -80,13 +80,12 @@ _.each({ constants: require("./transformers/es6-constants"), letScoping: require("./transformers/es6-let-scoping"), - _blockHoist: require("./transformers/_block-hoist"), - generators: require("./transformers/es6-generators"), restParameters: require("./transformers/es6-rest-parameters"), protoToAssign: require("./transformers/optional-proto-to-assign"), + _blockHoist: require("./transformers/_block-hoist"), _declarations: require("./transformers/_declarations"), // wrap up From d4cc8fefd1c18645c44015b7b6d021aebcc1f37e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Sat, 17 Jan 2015 23:02:20 -0200 Subject: [PATCH 06/15] Add transformer to import regeneratorRuntime from a module Thanks to facebook/regenerator#167 Regenerator's runtime can be now required as a module (though not in an easily self-containable way). This transformer, together with `coreAliasing` and `externalRuntime`, allows users to ditch a runtime dependency on `6to5`/`6to5-core` to depend directly on `core-js` and `regenerator`. --- lib/6to5/transformation/transform.js | 1 + .../optional-external-regenerator-runtime.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 lib/6to5/transformation/transformers/optional-external-regenerator-runtime.js diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 74079d5d0b..9d32b62fa9 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -95,6 +95,7 @@ _.each({ typeofSymbol: require("./transformers/optional-typeof-symbol"), coreAliasing: require("./transformers/optional-core-aliasing"), + externalRegenerator: require("./transformers/optional-external-regenerator-runtime"), undefinedToVoid: require("./transformers/optional-undefined-to-void"), // spec diff --git a/lib/6to5/transformation/transformers/optional-external-regenerator-runtime.js b/lib/6to5/transformation/transformers/optional-external-regenerator-runtime.js new file mode 100644 index 0000000000..9294a6c76e --- /dev/null +++ b/lib/6to5/transformation/transformers/optional-external-regenerator-runtime.js @@ -0,0 +1,16 @@ +var t = require("../../types"); + +exports.optional = true; + +exports.ast = { + enter: function (ast, file) { + file._regeneratorId = file.addImport("regenerator/runtime-module", "regeneratorRuntime"); + } +}; + +exports.MemberExpression = function (node, parent, scope, context, file) { + var obj = node.object; + var prop = node.property; + if (!t.isReferenced(node, parent) || !t.isReferenced(obj, node)) return; + if (obj.name === "regeneratorRuntime") return t.memberExpression(file._regeneratorId, prop); +}; From 7e6da1d368c75fe980bf271feaa90643359e7045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Sun, 18 Jan 2015 01:41:20 -0200 Subject: [PATCH 07/15] Add tests for the externalRegenerator transformer --- .../basic/actual.js | 2 ++ .../basic/expected.js | 17 +++++++++++++++++ .../options.json | 3 +++ 3 files changed, 22 insertions(+) create mode 100644 test/fixtures/transformation/optional-external-regenerator-runtime/basic/actual.js create mode 100644 test/fixtures/transformation/optional-external-regenerator-runtime/basic/expected.js create mode 100644 test/fixtures/transformation/optional-external-regenerator-runtime/options.json diff --git a/test/fixtures/transformation/optional-external-regenerator-runtime/basic/actual.js b/test/fixtures/transformation/optional-external-regenerator-runtime/basic/actual.js new file mode 100644 index 0000000000..ae6cc4d8c8 --- /dev/null +++ b/test/fixtures/transformation/optional-external-regenerator-runtime/basic/actual.js @@ -0,0 +1,2 @@ +void function* () { +}; diff --git a/test/fixtures/transformation/optional-external-regenerator-runtime/basic/expected.js b/test/fixtures/transformation/optional-external-regenerator-runtime/basic/expected.js new file mode 100644 index 0000000000..4121cc9700 --- /dev/null +++ b/test/fixtures/transformation/optional-external-regenerator-runtime/basic/expected.js @@ -0,0 +1,17 @@ +"use strict"; + +var _interopRequire = function (obj) { + return obj && (obj["default"] || obj); +}; + +var _regeneratorRuntime = _interopRequire(require("regenerator/runtime-module")); + +void _regeneratorRuntime.mark(function callee$0$0() { + return _regeneratorRuntime.wrap(function callee$0$0$(context$1$0) { + while (1) switch (context$1$0.prev = context$1$0.next) { + case 0: + case "end": + return context$1$0.stop(); + } + }, callee$0$0, this); +}); diff --git a/test/fixtures/transformation/optional-external-regenerator-runtime/options.json b/test/fixtures/transformation/optional-external-regenerator-runtime/options.json new file mode 100644 index 0000000000..40d336b5be --- /dev/null +++ b/test/fixtures/transformation/optional-external-regenerator-runtime/options.json @@ -0,0 +1,3 @@ +{ + "optional": ["externalRegenerator"] +} From 8d288c01ca0dc6ad87643a163d9ec156ebbdd1e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Sun, 18 Jan 2015 20:39:35 -0200 Subject: [PATCH 08/15] Import ES6 functions and helpers from 6to5-runtime The new `6to5-runtime` packs core-js, 6to5's helpers and the Regenerator runtime, and we're moving `coreAliasing`, `externalRuntime` and `externalRegenerator` to use it instead of forcing the user to depend on these packages directly. --- .../transformation/transformers/optional-core-aliasing.js | 2 +- .../transformers/optional-external-regenerator-runtime.js | 2 +- .../transformation/transformers/optional-external-runtime.js | 2 +- .../optional-core-aliasing/aliased-constructors/expected.js | 2 +- .../optional-core-aliasing/es6-destructuring/expected.js | 2 +- .../optional-core-aliasing/es6-for-of/expected.js | 2 +- .../optional-core-aliasing/es6-spread/expected.js | 2 +- .../es7-array-comprehensions/expected.js | 2 +- .../optional-external-regenerator-runtime/basic/expected.js | 2 +- .../transformation/optional-external-runtime/amd/expected.js | 4 ++-- .../optional-external-runtime/commonjs/expected.js | 2 +- .../transformation/optional-external-runtime/options.json | 1 - 12 files changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/6to5/transformation/transformers/optional-core-aliasing.js b/lib/6to5/transformation/transformers/optional-core-aliasing.js index c0db8bd9bd..62613bf198 100644 --- a/lib/6to5/transformation/transformers/optional-core-aliasing.js +++ b/lib/6to5/transformation/transformers/optional-core-aliasing.js @@ -23,7 +23,7 @@ exports.optional = true; exports.ast = { enter: function (ast, file) { - file._coreId = file.addImport("core-js/library", "core"); + file._coreId = file.addImport("6to5-runtime/core-js", "core"); }, exit: function (ast, file) { diff --git a/lib/6to5/transformation/transformers/optional-external-regenerator-runtime.js b/lib/6to5/transformation/transformers/optional-external-regenerator-runtime.js index 9294a6c76e..6fee6be612 100644 --- a/lib/6to5/transformation/transformers/optional-external-regenerator-runtime.js +++ b/lib/6to5/transformation/transformers/optional-external-regenerator-runtime.js @@ -4,7 +4,7 @@ exports.optional = true; exports.ast = { enter: function (ast, file) { - file._regeneratorId = file.addImport("regenerator/runtime-module", "regeneratorRuntime"); + file._regeneratorId = file.addImport("6to5-runtime/regenerator", "regeneratorRuntime"); } }; diff --git a/lib/6to5/transformation/transformers/optional-external-runtime.js b/lib/6to5/transformation/transformers/optional-external-runtime.js index ff39454e4d..56c14e0fc4 100644 --- a/lib/6to5/transformation/transformers/optional-external-runtime.js +++ b/lib/6to5/transformation/transformers/optional-external-runtime.js @@ -5,6 +5,6 @@ exports.optional = true; // AST is built. exports.ast = { enter: function (ast, file) { - file.opts.runtime = file.addImport(file.opts.runtime, "to5Runtime").name; + file.opts.runtime = file.addImport("6to5-runtime/6to5", "to5Runtime").name; } }; diff --git a/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js b/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js index 8c6ff60ed1..b517b90ea6 100644 --- a/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js @@ -1,6 +1,6 @@ "use strict"; -var _core = require("core-js/library"); +var _core = require("6to5-runtime/core-js"); obj.constructor === Object; obj.constructor === _core.Promise; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js b/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js index 15e3ce78b9..7abd2c0cd5 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js @@ -16,7 +16,7 @@ var _slicedToArray = function (arr, i) { } }; -var _core = require("core-js/library"); +var _core = require("6to5-runtime/core-js"); var _ref = ["hello", [", ", "junk"], ["world"]]; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js b/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js index 2669fd4d0c..0fdc4cc4f5 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js @@ -1,6 +1,6 @@ "use strict"; -var _core = require("core-js/library"); +var _core = require("6to5-runtime/core-js"); for (var _iterator = _core.$for.getIterator(arr), _step; !(_step = _iterator.next()).done;) { var i = _step.value; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js b/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js index 2a41ccd997..d01fa15ace 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js @@ -4,6 +4,6 @@ var _toArray = function (arr) { return Array.isArray(arr) ? arr : _core.Array.from(arr); }; -var _core = require("core-js/library"); +var _core = require("6to5-runtime/core-js"); var lyrics = ["head", "and", "toes"].concat(_toArray(parts)); diff --git a/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js b/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js index 0bf67a612a..9ea3b708c6 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js @@ -1,6 +1,6 @@ "use strict"; -var _core = require("core-js/library"); +var _core = require("6to5-runtime/core-js"); var arr = (function () { var _arr = []; diff --git a/test/fixtures/transformation/optional-external-regenerator-runtime/basic/expected.js b/test/fixtures/transformation/optional-external-regenerator-runtime/basic/expected.js index 00320c54b6..275ec24ac5 100644 --- a/test/fixtures/transformation/optional-external-regenerator-runtime/basic/expected.js +++ b/test/fixtures/transformation/optional-external-regenerator-runtime/basic/expected.js @@ -1,6 +1,6 @@ "use strict"; -var _regeneratorRuntime = require("regenerator/runtime-module"); +var _regeneratorRuntime = require("6to5-runtime/regenerator"); void _regeneratorRuntime.mark(function callee$0$0() { return _regeneratorRuntime.wrap(function callee$0$0$(context$1$0) { diff --git a/test/fixtures/transformation/optional-external-runtime/amd/expected.js b/test/fixtures/transformation/optional-external-runtime/amd/expected.js index 20e759af12..6e504dd685 100644 --- a/test/fixtures/transformation/optional-external-runtime/amd/expected.js +++ b/test/fixtures/transformation/optional-external-runtime/amd/expected.js @@ -1,6 +1,6 @@ -define(["exports", "6to5-runtime", "someModule"], function (exports, _to5Runtime2, _someModule) { +define(["exports", "6to5-runtime/6to5", "someModule"], function (exports, _to5Runtime6to5, _someModule) { "use strict"; - var _to5Runtime = _to5Runtime2; + var _to5Runtime = _to5Runtime6to5; var foo = _to5Runtime.interopRequire(_someModule); }); diff --git a/test/fixtures/transformation/optional-external-runtime/commonjs/expected.js b/test/fixtures/transformation/optional-external-runtime/commonjs/expected.js index b10bbe6f09..d660411bc0 100644 --- a/test/fixtures/transformation/optional-external-runtime/commonjs/expected.js +++ b/test/fixtures/transformation/optional-external-runtime/commonjs/expected.js @@ -1,5 +1,5 @@ "use strict"; -var _to5Runtime = require("6to5-runtime"); +var _to5Runtime = require("6to5-runtime/6to5"); var foo = _to5Runtime.interopRequireWildcard(require("someModule")); diff --git a/test/fixtures/transformation/optional-external-runtime/options.json b/test/fixtures/transformation/optional-external-runtime/options.json index 44c5164a67..5149c1159a 100644 --- a/test/fixtures/transformation/optional-external-runtime/options.json +++ b/test/fixtures/transformation/optional-external-runtime/options.json @@ -1,4 +1,3 @@ { - "runtime": "6to5-runtime", "optional": ["externalRuntime"] } From c81cc5cda06cc763a4e4cbe7ce702fb3303b5919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Sun, 18 Jan 2015 21:15:30 -0200 Subject: [PATCH 09/15] Unify coreAliasing, externalRuntime and externalRegenerator Merge all three transformers into the `selfContained` transformer, as discussed in Gitter. Name may not be final. --- lib/6to5/transformation/transform.js | 4 +-- .../optional-external-regenerator-runtime.js | 16 ---------- .../transformers/optional-external-runtime.js | 10 ------- ...aliasing.js => optional-self-contained.js} | 9 ++++++ .../es6-destructuring/actual.js | 1 - .../es6-destructuring/expected.js | 30 ------------------- .../es6-spread/actual.js | 1 - .../es6-spread/expected.js | 9 ------ .../optional-core-aliasing/options.json | 4 --- .../options.json | 3 -- .../optional-external-runtime/amd/expected.js | 6 ---- .../optional-external-runtime/options.json | 3 -- .../aliased-constructors/actual.js | 0 .../aliased-constructors/expected.js | 4 +++ .../es6-for-of/actual.js | 0 .../es6-for-of/expected.js | 4 +++ .../es7-array-comprehensions/actual.js | 0 .../es7-array-comprehensions/expected.js | 4 +++ .../optional-self-contained/options.json | 4 +++ .../regenerator-runtime}/actual.js | 0 .../regenerator-runtime}/expected.js | 4 +++ .../runtime-amd}/actual.js | 0 .../runtime-amd/expected.js | 8 +++++ .../runtime-amd}/options.json | 0 .../runtime-commonjs}/actual.js | 0 .../runtime-commonjs}/expected.js | 4 +++ 26 files changed, 42 insertions(+), 86 deletions(-) delete mode 100644 lib/6to5/transformation/transformers/optional-external-regenerator-runtime.js delete mode 100644 lib/6to5/transformation/transformers/optional-external-runtime.js rename lib/6to5/transformation/transformers/{optional-core-aliasing.js => optional-self-contained.js} (81%) delete mode 100644 test/fixtures/transformation/optional-core-aliasing/es6-destructuring/actual.js delete mode 100644 test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js delete mode 100644 test/fixtures/transformation/optional-core-aliasing/es6-spread/actual.js delete mode 100644 test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js delete mode 100644 test/fixtures/transformation/optional-core-aliasing/options.json delete mode 100644 test/fixtures/transformation/optional-external-regenerator-runtime/options.json delete mode 100644 test/fixtures/transformation/optional-external-runtime/amd/expected.js delete mode 100644 test/fixtures/transformation/optional-external-runtime/options.json rename test/fixtures/transformation/{optional-core-aliasing => optional-self-contained}/aliased-constructors/actual.js (100%) rename test/fixtures/transformation/{optional-core-aliasing => optional-self-contained}/aliased-constructors/expected.js (61%) rename test/fixtures/transformation/{optional-core-aliasing => optional-self-contained}/es6-for-of/actual.js (100%) rename test/fixtures/transformation/{optional-core-aliasing => optional-self-contained}/es6-for-of/expected.js (61%) rename test/fixtures/transformation/{optional-core-aliasing => optional-self-contained}/es7-array-comprehensions/actual.js (100%) rename test/fixtures/transformation/{optional-core-aliasing => optional-self-contained}/es7-array-comprehensions/expected.js (70%) create mode 100644 test/fixtures/transformation/optional-self-contained/options.json rename test/fixtures/transformation/{optional-external-regenerator-runtime/basic => optional-self-contained/regenerator-runtime}/actual.js (100%) rename test/fixtures/transformation/{optional-external-regenerator-runtime/basic => optional-self-contained/regenerator-runtime}/expected.js (79%) rename test/fixtures/transformation/{optional-external-runtime/amd => optional-self-contained/runtime-amd}/actual.js (100%) create mode 100644 test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js rename test/fixtures/transformation/{optional-external-runtime/amd => optional-self-contained/runtime-amd}/options.json (100%) rename test/fixtures/transformation/{optional-external-runtime/commonjs => optional-self-contained/runtime-commonjs}/actual.js (100%) rename test/fixtures/transformation/{optional-external-runtime/commonjs => optional-self-contained/runtime-commonjs}/expected.js (54%) diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 4c64371d3a..c90a23290c 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -94,9 +94,7 @@ _.each({ _moduleFormatter: require("./transformers/_module-formatter"), typeofSymbol: require("./transformers/optional-typeof-symbol"), - externalRuntime: require("./transformers/optional-external-runtime"), - coreAliasing: require("./transformers/optional-core-aliasing"), - externalRegenerator: require("./transformers/optional-external-regenerator-runtime"), + selfContained: require("./transformers/optional-self-contained"), undefinedToVoid: require("./transformers/optional-undefined-to-void"), // spec diff --git a/lib/6to5/transformation/transformers/optional-external-regenerator-runtime.js b/lib/6to5/transformation/transformers/optional-external-regenerator-runtime.js deleted file mode 100644 index 6fee6be612..0000000000 --- a/lib/6to5/transformation/transformers/optional-external-regenerator-runtime.js +++ /dev/null @@ -1,16 +0,0 @@ -var t = require("../../types"); - -exports.optional = true; - -exports.ast = { - enter: function (ast, file) { - file._regeneratorId = file.addImport("6to5-runtime/regenerator", "regeneratorRuntime"); - } -}; - -exports.MemberExpression = function (node, parent, scope, context, file) { - var obj = node.object; - var prop = node.property; - if (!t.isReferenced(node, parent) || !t.isReferenced(obj, node)) return; - if (obj.name === "regeneratorRuntime") return t.memberExpression(file._regeneratorId, prop); -}; diff --git a/lib/6to5/transformation/transformers/optional-external-runtime.js b/lib/6to5/transformation/transformers/optional-external-runtime.js deleted file mode 100644 index 56c14e0fc4..0000000000 --- a/lib/6to5/transformation/transformers/optional-external-runtime.js +++ /dev/null @@ -1,10 +0,0 @@ -exports.optional = true; - -// In theory, it would be more appropriate to do this in `manipulateOptions`, -// but we need an identifier for the import and we can't get that before the -// AST is built. -exports.ast = { - enter: function (ast, file) { - file.opts.runtime = file.addImport("6to5-runtime/6to5", "to5Runtime").name; - } -}; diff --git a/lib/6to5/transformation/transformers/optional-core-aliasing.js b/lib/6to5/transformation/transformers/optional-self-contained.js similarity index 81% rename from lib/6to5/transformation/transformers/optional-core-aliasing.js rename to lib/6to5/transformation/transformers/optional-self-contained.js index 62613bf198..7fe1b5b4d9 100644 --- a/lib/6to5/transformation/transformers/optional-core-aliasing.js +++ b/lib/6to5/transformation/transformers/optional-self-contained.js @@ -23,7 +23,9 @@ exports.optional = true; exports.ast = { enter: function (ast, file) { + file.opts.runtime = file.addImport("6to5-runtime/6to5", "to5Runtime").name; file._coreId = file.addImport("6to5-runtime/core-js", "core"); + file._regeneratorId = file.addImport("6to5-runtime/regenerator", "regeneratorRuntime"); }, exit: function (ast, file) { @@ -67,3 +69,10 @@ exports.ast = { }); } }; + +exports.MemberExpression = function (node, parent, scope, context, file) { + var obj = node.object; + var prop = node.property; + if (!t.isReferenced(node, parent) || !t.isReferenced(obj, node)) return; + if (obj.name === "regeneratorRuntime") return t.memberExpression(file._regeneratorId, prop); +}; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/actual.js b/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/actual.js deleted file mode 100644 index 10aa8799ed..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/actual.js +++ /dev/null @@ -1 +0,0 @@ -var [a, [b], [c], d] = ["hello", [", ", "junk"], ["world"]]; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js b/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js deleted file mode 100644 index 7abd2c0cd5..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js +++ /dev/null @@ -1,30 +0,0 @@ -"use strict"; - -var _slicedToArray = function (arr, i) { - if (Array.isArray(arr)) { - return arr; - } else { - var _arr = []; - - for (var _iterator = _core.$for.getIterator(arr), _step; !(_step = _iterator.next()).done;) { - _arr.push(_step.value); - - if (i && _arr.length === i) break; - } - - return _arr; - } -}; - -var _core = require("6to5-runtime/core-js"); - -var _ref = ["hello", [", ", "junk"], ["world"]]; - -var a = _ref[0]; -var _ref$1 = _slicedToArray(_ref[1], 1); - -var b = _ref$1[0]; -var _ref$2 = _slicedToArray(_ref[2], 1); - -var c = _ref$2[0]; -var d = _ref[3]; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-spread/actual.js b/test/fixtures/transformation/optional-core-aliasing/es6-spread/actual.js deleted file mode 100644 index 5ebb80da66..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/es6-spread/actual.js +++ /dev/null @@ -1 +0,0 @@ -var lyrics = ["head", "and", "toes", ...parts]; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js b/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js deleted file mode 100644 index d01fa15ace..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js +++ /dev/null @@ -1,9 +0,0 @@ -"use strict"; - -var _toArray = function (arr) { - return Array.isArray(arr) ? arr : _core.Array.from(arr); -}; - -var _core = require("6to5-runtime/core-js"); - -var lyrics = ["head", "and", "toes"].concat(_toArray(parts)); diff --git a/test/fixtures/transformation/optional-core-aliasing/options.json b/test/fixtures/transformation/optional-core-aliasing/options.json deleted file mode 100644 index 371b5f0b20..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/options.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "optional": ["coreAliasing"], - "experimental": true -} diff --git a/test/fixtures/transformation/optional-external-regenerator-runtime/options.json b/test/fixtures/transformation/optional-external-regenerator-runtime/options.json deleted file mode 100644 index 40d336b5be..0000000000 --- a/test/fixtures/transformation/optional-external-regenerator-runtime/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "optional": ["externalRegenerator"] -} diff --git a/test/fixtures/transformation/optional-external-runtime/amd/expected.js b/test/fixtures/transformation/optional-external-runtime/amd/expected.js deleted file mode 100644 index 6e504dd685..0000000000 --- a/test/fixtures/transformation/optional-external-runtime/amd/expected.js +++ /dev/null @@ -1,6 +0,0 @@ -define(["exports", "6to5-runtime/6to5", "someModule"], function (exports, _to5Runtime6to5, _someModule) { - "use strict"; - - var _to5Runtime = _to5Runtime6to5; - var foo = _to5Runtime.interopRequire(_someModule); -}); diff --git a/test/fixtures/transformation/optional-external-runtime/options.json b/test/fixtures/transformation/optional-external-runtime/options.json deleted file mode 100644 index 5149c1159a..0000000000 --- a/test/fixtures/transformation/optional-external-runtime/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "optional": ["externalRuntime"] -} diff --git a/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/actual.js b/test/fixtures/transformation/optional-self-contained/aliased-constructors/actual.js similarity index 100% rename from test/fixtures/transformation/optional-core-aliasing/aliased-constructors/actual.js rename to test/fixtures/transformation/optional-self-contained/aliased-constructors/actual.js diff --git a/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js b/test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js similarity index 61% rename from test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js rename to test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js index b517b90ea6..2b56ea0257 100644 --- a/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js +++ b/test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js @@ -1,7 +1,11 @@ "use strict"; +var _to5Runtime = require("6to5-runtime/6to5"); + var _core = require("6to5-runtime/core-js"); +var _regeneratorRuntime = require("6to5-runtime/regenerator"); + obj.constructor === Object; obj.constructor === _core.Promise; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-for-of/actual.js b/test/fixtures/transformation/optional-self-contained/es6-for-of/actual.js similarity index 100% rename from test/fixtures/transformation/optional-core-aliasing/es6-for-of/actual.js rename to test/fixtures/transformation/optional-self-contained/es6-for-of/actual.js diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js b/test/fixtures/transformation/optional-self-contained/es6-for-of/expected.js similarity index 61% rename from test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js rename to test/fixtures/transformation/optional-self-contained/es6-for-of/expected.js index 0fdc4cc4f5..c520e9da0a 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js +++ b/test/fixtures/transformation/optional-self-contained/es6-for-of/expected.js @@ -1,7 +1,11 @@ "use strict"; +var _to5Runtime = require("6to5-runtime/6to5"); + var _core = require("6to5-runtime/core-js"); +var _regeneratorRuntime = require("6to5-runtime/regenerator"); + for (var _iterator = _core.$for.getIterator(arr), _step; !(_step = _iterator.next()).done;) { var i = _step.value; } diff --git a/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/actual.js b/test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/actual.js similarity index 100% rename from test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/actual.js rename to test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/actual.js diff --git a/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js b/test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/expected.js similarity index 70% rename from test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js rename to test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/expected.js index 9ea3b708c6..1a7d5c5482 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js +++ b/test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/expected.js @@ -1,7 +1,11 @@ "use strict"; +var _to5Runtime = require("6to5-runtime/6to5"); + var _core = require("6to5-runtime/core-js"); +var _regeneratorRuntime = require("6to5-runtime/regenerator"); + var arr = (function () { var _arr = []; diff --git a/test/fixtures/transformation/optional-self-contained/options.json b/test/fixtures/transformation/optional-self-contained/options.json new file mode 100644 index 0000000000..079cf304ad --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/options.json @@ -0,0 +1,4 @@ +{ + "optional": ["selfContained"], + "experimental": true +} diff --git a/test/fixtures/transformation/optional-external-regenerator-runtime/basic/actual.js b/test/fixtures/transformation/optional-self-contained/regenerator-runtime/actual.js similarity index 100% rename from test/fixtures/transformation/optional-external-regenerator-runtime/basic/actual.js rename to test/fixtures/transformation/optional-self-contained/regenerator-runtime/actual.js diff --git a/test/fixtures/transformation/optional-external-regenerator-runtime/basic/expected.js b/test/fixtures/transformation/optional-self-contained/regenerator-runtime/expected.js similarity index 79% rename from test/fixtures/transformation/optional-external-regenerator-runtime/basic/expected.js rename to test/fixtures/transformation/optional-self-contained/regenerator-runtime/expected.js index 275ec24ac5..92dc0c8704 100644 --- a/test/fixtures/transformation/optional-external-regenerator-runtime/basic/expected.js +++ b/test/fixtures/transformation/optional-self-contained/regenerator-runtime/expected.js @@ -1,5 +1,9 @@ "use strict"; +var _to5Runtime = require("6to5-runtime/6to5"); + +var _core = require("6to5-runtime/core-js"); + var _regeneratorRuntime = require("6to5-runtime/regenerator"); void _regeneratorRuntime.mark(function callee$0$0() { diff --git a/test/fixtures/transformation/optional-external-runtime/amd/actual.js b/test/fixtures/transformation/optional-self-contained/runtime-amd/actual.js similarity index 100% rename from test/fixtures/transformation/optional-external-runtime/amd/actual.js rename to test/fixtures/transformation/optional-self-contained/runtime-amd/actual.js diff --git a/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js b/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js new file mode 100644 index 0000000000..17b753e8c5 --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js @@ -0,0 +1,8 @@ +define(["exports", "6to5-runtime/6to5", "6to5-runtime/core-js", "6to5-runtime/regenerator", "someModule"], function (exports, _to5Runtime6to5, _to5RuntimeCoreJs, _to5RuntimeRegenerator, _someModule) { + "use strict"; + + var _to5Runtime = _to5Runtime6to5; + var _core = _to5RuntimeCoreJs; + var _regeneratorRuntime = _to5RuntimeRegenerator; + var foo = _to5Runtime.interopRequire(_someModule); +}); diff --git a/test/fixtures/transformation/optional-external-runtime/amd/options.json b/test/fixtures/transformation/optional-self-contained/runtime-amd/options.json similarity index 100% rename from test/fixtures/transformation/optional-external-runtime/amd/options.json rename to test/fixtures/transformation/optional-self-contained/runtime-amd/options.json diff --git a/test/fixtures/transformation/optional-external-runtime/commonjs/actual.js b/test/fixtures/transformation/optional-self-contained/runtime-commonjs/actual.js similarity index 100% rename from test/fixtures/transformation/optional-external-runtime/commonjs/actual.js rename to test/fixtures/transformation/optional-self-contained/runtime-commonjs/actual.js diff --git a/test/fixtures/transformation/optional-external-runtime/commonjs/expected.js b/test/fixtures/transformation/optional-self-contained/runtime-commonjs/expected.js similarity index 54% rename from test/fixtures/transformation/optional-external-runtime/commonjs/expected.js rename to test/fixtures/transformation/optional-self-contained/runtime-commonjs/expected.js index d660411bc0..9fd1cd7ec9 100644 --- a/test/fixtures/transformation/optional-external-runtime/commonjs/expected.js +++ b/test/fixtures/transformation/optional-self-contained/runtime-commonjs/expected.js @@ -2,4 +2,8 @@ var _to5Runtime = require("6to5-runtime/6to5"); +var _core = require("6to5-runtime/core-js"); + +var _regeneratorRuntime = require("6to5-runtime/regenerator"); + var foo = _to5Runtime.interopRequireWildcard(require("someModule")); From 02019d4d8f728817809d1e51d7889ae11f3e8d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Sun, 18 Jan 2015 21:18:25 -0200 Subject: [PATCH 10/15] Drop unnecessary dynamic import redeclaration in AMD Instead of assigning the import a name and them declaring a new variabled referencing that name, output the import already with the destination name, since we know there must be no collisions because these IDs are also generated. --- lib/6to5/transformation/modules/amd.js | 6 +++++- .../optional-self-contained/runtime-amd/expected.js | 5 +---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index 0eb61407b2..6d06ddbe46 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -87,7 +87,11 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) { var key = t.getSpecifierName(specifier); var ref = this._push(node); - if (t.isImportBatchSpecifier(specifier) || _.contains(this.file.dynamicImports, node)) { + if (_.contains(this.file.dynamicImports, node)) { + // Prevent unnecessary renaming of dynamic imports. + this.ids[node.source.value] = key; + return; + } else if (t.isImportBatchSpecifier(specifier)) { // import * as bar from "foo"; } else if (t.isSpecifierDefault(specifier) && !this.noInteropRequire) { // import foo from "foo"; diff --git a/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js b/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js index 17b753e8c5..928902ba02 100644 --- a/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js +++ b/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js @@ -1,8 +1,5 @@ -define(["exports", "6to5-runtime/6to5", "6to5-runtime/core-js", "6to5-runtime/regenerator", "someModule"], function (exports, _to5Runtime6to5, _to5RuntimeCoreJs, _to5RuntimeRegenerator, _someModule) { +define(["exports", "6to5-runtime/6to5", "6to5-runtime/core-js", "6to5-runtime/regenerator", "someModule"], function (exports, _to5Runtime, _core, _regeneratorRuntime, _someModule) { "use strict"; - var _to5Runtime = _to5Runtime6to5; - var _core = _to5RuntimeCoreJs; - var _regeneratorRuntime = _to5RuntimeRegenerator; var foo = _to5Runtime.interopRequire(_someModule); }); From e2bf61379f26c809492a96c6ce4455bb1ea1b598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Sun, 18 Jan 2015 23:10:26 -0200 Subject: [PATCH 11/15] Add full-blown test for the selfContained transformer --- .../optional-self-contained/full/actual.js | 6 +++++ .../optional-self-contained/full/expected.js | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 test/fixtures/transformation/optional-self-contained/full/actual.js create mode 100644 test/fixtures/transformation/optional-self-contained/full/expected.js diff --git a/test/fixtures/transformation/optional-self-contained/full/actual.js b/test/fixtures/transformation/optional-self-contained/full/actual.js new file mode 100644 index 0000000000..3e8b1bcdbc --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/full/actual.js @@ -0,0 +1,6 @@ +import foo, * as bar from "someModule"; + +export const myWord = Symbol("abc"); +export function* giveWord () { + yield myWord; +} diff --git a/test/fixtures/transformation/optional-self-contained/full/expected.js b/test/fixtures/transformation/optional-self-contained/full/expected.js new file mode 100644 index 0000000000..cbd7e48fbf --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/full/expected.js @@ -0,0 +1,27 @@ +"use strict"; + +var _to5Runtime = require("6to5-runtime/6to5"); + +var _core = require("6to5-runtime/core-js"); + +var _regeneratorRuntime = require("6to5-runtime/regenerator"); + +var giveWord = _regeneratorRuntime.mark(function giveWord() { + return _regeneratorRuntime.wrap(function giveWord$(context$1$0) { + while (1) switch (context$1$0.prev = context$1$0.next) { + case 0: + context$1$0.next = 2; + return myWord; + case 2: + case "end": + return context$1$0.stop(); + } + }, giveWord, this); +}); + +exports.giveWord = giveWord; +var foo = _to5Runtime.interopRequire(require("someModule")); + +var bar = _to5Runtime.interopRequireWildcard(require("someModule")); + +var myWord = exports.myWord = _core.Symbol("abc"); From 157f5c3304e8b85be8ada99a634393ea22e45045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Mon, 19 Jan 2015 10:54:33 -0200 Subject: [PATCH 12/15] Remove regenerator edge case from selfContained test Right now exporting a generator doesn't work because we hoist the export assignment before the function expression assignment. This will be dealt with at another time as it is not specific to this transformer. --- .../transformation/optional-self-contained/full/actual.js | 2 +- .../transformation/optional-self-contained/full/expected.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/test/fixtures/transformation/optional-self-contained/full/actual.js b/test/fixtures/transformation/optional-self-contained/full/actual.js index 3e8b1bcdbc..544c01be3d 100644 --- a/test/fixtures/transformation/optional-self-contained/full/actual.js +++ b/test/fixtures/transformation/optional-self-contained/full/actual.js @@ -1,6 +1,6 @@ import foo, * as bar from "someModule"; export const myWord = Symbol("abc"); -export function* giveWord () { +function* giveWord () { yield myWord; } diff --git a/test/fixtures/transformation/optional-self-contained/full/expected.js b/test/fixtures/transformation/optional-self-contained/full/expected.js index cbd7e48fbf..5c31f034a6 100644 --- a/test/fixtures/transformation/optional-self-contained/full/expected.js +++ b/test/fixtures/transformation/optional-self-contained/full/expected.js @@ -19,7 +19,6 @@ var giveWord = _regeneratorRuntime.mark(function giveWord() { }, giveWord, this); }); -exports.giveWord = giveWord; var foo = _to5Runtime.interopRequire(require("someModule")); var bar = _to5Runtime.interopRequireWildcard(require("someModule")); From 3f96cf396346390533de4b27c70fe7722f689160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Mon, 19 Jan 2015 11:55:03 -0200 Subject: [PATCH 13/15] Rename 6to5-runtime's helpers module to 'helpers' --- lib/6to5/transformation/transformers/optional-self-contained.js | 2 +- .../optional-self-contained/aliased-constructors/expected.js | 2 +- .../optional-self-contained/es6-for-of/expected.js | 2 +- .../es7-array-comprehensions/expected.js | 2 +- .../transformation/optional-self-contained/full/expected.js | 2 +- .../optional-self-contained/regenerator-runtime/expected.js | 2 +- .../optional-self-contained/runtime-amd/expected.js | 2 +- .../optional-self-contained/runtime-commonjs/expected.js | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/6to5/transformation/transformers/optional-self-contained.js b/lib/6to5/transformation/transformers/optional-self-contained.js index 7fe1b5b4d9..0ab0cdbd3a 100644 --- a/lib/6to5/transformation/transformers/optional-self-contained.js +++ b/lib/6to5/transformation/transformers/optional-self-contained.js @@ -23,7 +23,7 @@ exports.optional = true; exports.ast = { enter: function (ast, file) { - file.opts.runtime = file.addImport("6to5-runtime/6to5", "to5Runtime").name; + file.opts.runtime = file.addImport("6to5-runtime/helpers", "to5Runtime").name; file._coreId = file.addImport("6to5-runtime/core-js", "core"); file._regeneratorId = file.addImport("6to5-runtime/regenerator", "regeneratorRuntime"); }, diff --git a/test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js b/test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js index 2b56ea0257..2242afb8f8 100644 --- a/test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js +++ b/test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js @@ -1,6 +1,6 @@ "use strict"; -var _to5Runtime = require("6to5-runtime/6to5"); +var _to5Runtime = require("6to5-runtime/helpers"); var _core = require("6to5-runtime/core-js"); diff --git a/test/fixtures/transformation/optional-self-contained/es6-for-of/expected.js b/test/fixtures/transformation/optional-self-contained/es6-for-of/expected.js index c520e9da0a..4d781e1d60 100644 --- a/test/fixtures/transformation/optional-self-contained/es6-for-of/expected.js +++ b/test/fixtures/transformation/optional-self-contained/es6-for-of/expected.js @@ -1,6 +1,6 @@ "use strict"; -var _to5Runtime = require("6to5-runtime/6to5"); +var _to5Runtime = require("6to5-runtime/helpers"); var _core = require("6to5-runtime/core-js"); diff --git a/test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/expected.js b/test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/expected.js index 1a7d5c5482..374a18cb98 100644 --- a/test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/expected.js +++ b/test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/expected.js @@ -1,6 +1,6 @@ "use strict"; -var _to5Runtime = require("6to5-runtime/6to5"); +var _to5Runtime = require("6to5-runtime/helpers"); var _core = require("6to5-runtime/core-js"); diff --git a/test/fixtures/transformation/optional-self-contained/full/expected.js b/test/fixtures/transformation/optional-self-contained/full/expected.js index 5c31f034a6..8caafa65b9 100644 --- a/test/fixtures/transformation/optional-self-contained/full/expected.js +++ b/test/fixtures/transformation/optional-self-contained/full/expected.js @@ -1,6 +1,6 @@ "use strict"; -var _to5Runtime = require("6to5-runtime/6to5"); +var _to5Runtime = require("6to5-runtime/helpers"); var _core = require("6to5-runtime/core-js"); diff --git a/test/fixtures/transformation/optional-self-contained/regenerator-runtime/expected.js b/test/fixtures/transformation/optional-self-contained/regenerator-runtime/expected.js index 92dc0c8704..e7fa729a14 100644 --- a/test/fixtures/transformation/optional-self-contained/regenerator-runtime/expected.js +++ b/test/fixtures/transformation/optional-self-contained/regenerator-runtime/expected.js @@ -1,6 +1,6 @@ "use strict"; -var _to5Runtime = require("6to5-runtime/6to5"); +var _to5Runtime = require("6to5-runtime/helpers"); var _core = require("6to5-runtime/core-js"); diff --git a/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js b/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js index 928902ba02..292e6b96cf 100644 --- a/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js +++ b/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js @@ -1,4 +1,4 @@ -define(["exports", "6to5-runtime/6to5", "6to5-runtime/core-js", "6to5-runtime/regenerator", "someModule"], function (exports, _to5Runtime, _core, _regeneratorRuntime, _someModule) { +define(["exports", "6to5-runtime/helpers", "6to5-runtime/core-js", "6to5-runtime/regenerator", "someModule"], function (exports, _to5Runtime, _core, _regeneratorRuntime, _someModule) { "use strict"; var foo = _to5Runtime.interopRequire(_someModule); diff --git a/test/fixtures/transformation/optional-self-contained/runtime-commonjs/expected.js b/test/fixtures/transformation/optional-self-contained/runtime-commonjs/expected.js index 9fd1cd7ec9..d7a8de73f8 100644 --- a/test/fixtures/transformation/optional-self-contained/runtime-commonjs/expected.js +++ b/test/fixtures/transformation/optional-self-contained/runtime-commonjs/expected.js @@ -1,6 +1,6 @@ "use strict"; -var _to5Runtime = require("6to5-runtime/6to5"); +var _to5Runtime = require("6to5-runtime/helpers"); var _core = require("6to5-runtime/core-js"); From 043bf13d245471d576ec59e69525b43da1ef61d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Mon, 19 Jan 2015 12:16:25 -0200 Subject: [PATCH 14/15] Revert moving _blockHoist after regenerator This reverts commits 81bec3e5c4e559cacadbc07dd24aac8d5099caa6 and 157f5c3304e8b85be8ada99a634393ea22e45045. --- lib/6to5/transformation/transform.js | 3 ++- .../transformation/optional-self-contained/full/actual.js | 2 +- .../transformation/optional-self-contained/full/expected.js | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index c90a23290c..90f615542c 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -80,12 +80,13 @@ _.each({ constants: require("./transformers/es6-constants"), letScoping: require("./transformers/es6-let-scoping"), + _blockHoist: require("./transformers/_block-hoist"), + generators: require("./transformers/es6-generators"), restParameters: require("./transformers/es6-rest-parameters"), protoToAssign: require("./transformers/optional-proto-to-assign"), - _blockHoist: require("./transformers/_block-hoist"), _declarations: require("./transformers/_declarations"), // wrap up diff --git a/test/fixtures/transformation/optional-self-contained/full/actual.js b/test/fixtures/transformation/optional-self-contained/full/actual.js index 544c01be3d..3e8b1bcdbc 100644 --- a/test/fixtures/transformation/optional-self-contained/full/actual.js +++ b/test/fixtures/transformation/optional-self-contained/full/actual.js @@ -1,6 +1,6 @@ import foo, * as bar from "someModule"; export const myWord = Symbol("abc"); -function* giveWord () { +export function* giveWord () { yield myWord; } diff --git a/test/fixtures/transformation/optional-self-contained/full/expected.js b/test/fixtures/transformation/optional-self-contained/full/expected.js index 8caafa65b9..2fe954e8df 100644 --- a/test/fixtures/transformation/optional-self-contained/full/expected.js +++ b/test/fixtures/transformation/optional-self-contained/full/expected.js @@ -19,6 +19,7 @@ var giveWord = _regeneratorRuntime.mark(function giveWord() { }, giveWord, this); }); +exports.giveWord = giveWord; var foo = _to5Runtime.interopRequire(require("someModule")); var bar = _to5Runtime.interopRequireWildcard(require("someModule")); From 9d2e12dfa67121c916a6bc8da897788be3b49242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Mon, 19 Jan 2015 12:31:47 -0200 Subject: [PATCH 15/15] Amendments from 6to5/6to5#535 comments --- lib/6to5/transformation/modules/common.js | 4 +++- .../transformers/optional-self-contained.js | 9 ++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index 950b35790e..73653c8d04 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -27,7 +27,9 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) // import foo from "foo"; if (t.isSpecifierDefault(specifier)) { - var ref = util.template("require", {MODULE_NAME: node.source}); + var ref = util.template("require", { + MODULE_NAME: node.source + }); if (!_.contains(this.file.dynamicImports, node)) { ref = t.callExpression(this.file.addHelper("interop-require"), [ref]); } diff --git a/lib/6to5/transformation/transformers/optional-self-contained.js b/lib/6to5/transformation/transformers/optional-self-contained.js index 0ab0cdbd3a..0fd9eac9f3 100644 --- a/lib/6to5/transformation/transformers/optional-self-contained.js +++ b/lib/6to5/transformation/transformers/optional-self-contained.js @@ -70,9 +70,8 @@ exports.ast = { } }; -exports.MemberExpression = function (node, parent, scope, context, file) { - var obj = node.object; - var prop = node.property; - if (!t.isReferenced(node, parent) || !t.isReferenced(obj, node)) return; - if (obj.name === "regeneratorRuntime") return t.memberExpression(file._regeneratorId, prop); +exports.Identifier = function (node, parent, scope, context, file) { + if (node.name === "regeneratorRuntime" && t.isReferenced(node, parent)) { + node.name = file._regeneratorId; + } };