diff --git a/lib/6to5/transformation/templates/corejs-iterator.js b/lib/6to5/transformation/templates/corejs-iterator.js new file mode 100644 index 0000000000..c30ea8a4c2 --- /dev/null +++ b/lib/6to5/transformation/templates/corejs-iterator.js @@ -0,0 +1 @@ +CORE_ID.$for.getIterator(VALUE); diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index d9fa1c90b4..e40af5089f 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -80,6 +80,8 @@ _.each({ _declarations: require("./transformers/_declarations"), + coreAliasing: require("./transformers/optional-core-aliasing"), + // wrap up _aliasFunctions: require("./transformers/_alias-functions"), useStrict: require("./transformers/use-strict"), diff --git a/lib/6to5/transformation/transformer.js b/lib/6to5/transformation/transformer.js index 675b2743f2..92b1082836 100644 --- a/lib/6to5/transformation/transformer.js +++ b/lib/6to5/transformation/transformer.js @@ -5,9 +5,11 @@ var t = require("../types"); var _ = require("lodash"); function Transformer(key, transformer, opts) { - this.transformer = Transformer.normalise(transformer); - this.opts = opts || {}; - this.key = key; + this.experimental = !!transformer.experimental; + this.transformer = Transformer.normalise(transformer); + this.optional = !!transformer.optional; + this.opts = opts || {}; + this.key = key; } Transformer.normalise = function (transformer) { @@ -16,8 +18,13 @@ Transformer.normalise = function (transformer) { } _.each(transformer, function (fns, type) { + // hidden property if (type[0] === "_") return; + if (_.isFunction(fns)) fns = { enter: fns }; + + if (!_.isObject(fns)) return; + transformer[type] = fns; var aliases = t.FLIPPED_ALIAS_KEYS[type]; @@ -41,13 +48,9 @@ Transformer.prototype.astRun = function (file, key) { }; Transformer.prototype.transform = function (file) { - if (!this.canRun(file)) return; - var transformer = this.transformer; var ast = file.ast; - this.astRun(file, "enter"); - var build = function (exit) { return function (node, parent, scope) { var fns = transformer[node.type]; @@ -61,12 +64,14 @@ Transformer.prototype.transform = function (file) { }; }; + this.astRun(file, "before"); + traverse(ast, { enter: build(), exit: build(true) }); - this.astRun(file, "exit"); + this.astRun(file, "after"); }; Transformer.prototype.canRun = function (file) { @@ -80,5 +85,9 @@ Transformer.prototype.canRun = function (file) { var whitelist = opts.whitelist; if (whitelist.length && !_.contains(whitelist, key)) return false; + if (this.optional && !_.contains(opts.optional, key)) return false; + + if (this.experimental && !opts.experimental) return false; + return true; }; diff --git a/lib/6to5/types/builder-keys.json b/lib/6to5/types/builder-keys.json index beca94c0e4..e9ac7987be 100644 --- a/lib/6to5/types/builder-keys.json +++ b/lib/6to5/types/builder-keys.json @@ -11,6 +11,8 @@ "FunctionExpression": ["id", "params", "body", "generator"], "Identifier": ["name"], "IfStatement": ["test", "consequent", "alternate"], + "ImportDeclaration": ["specifiers", "source"], + "ImportSpecifier": ["id", "name"], "Literal": ["value"], "LogicalExpression": ["operator", "left", "right"], "MemberExpression": ["object", "property", "computed"], diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/actual.js b/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/actual.js new file mode 100644 index 0000000000..10aa8799ed --- /dev/null +++ b/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/actual.js @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000000..712e62b592 --- /dev/null +++ b/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js @@ -0,0 +1,35 @@ +"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 _interopRequire = function (obj) { + return obj && (obj["default"] || obj); +}; + +var _core = _interopRequire(require("core-js/library")); + +var _ref = ["hello", [", ", "junk"], ["world"]]; + +var _ref2 = _slicedToArray(_ref, 4); + +var a = _ref2[0]; +var _ref3 = _slicedToArray(_ref2[1], 1); + +var b = _ref3[0]; +var _ref4 = _slicedToArray(_ref2[2], 1); + +var c = _ref4[0]; +var d = _ref2[3]; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-for-of/actual.js b/test/fixtures/transformation/optional-core-aliasing/es6-for-of/actual.js new file mode 100644 index 0000000000..48e5f59b2c --- /dev/null +++ b/test/fixtures/transformation/optional-core-aliasing/es6-for-of/actual.js @@ -0,0 +1,3 @@ +for (var i of arr) { + +} 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 new file mode 100644 index 0000000000..3bf1f53a05 --- /dev/null +++ b/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js @@ -0,0 +1,11 @@ +"use strict"; + +var _interopRequire = function (obj) { + return obj && (obj["default"] || obj); +}; + +var _core = _interopRequire(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/actual.js b/test/fixtures/transformation/optional-core-aliasing/es6-spread/actual.js new file mode 100644 index 0000000000..5ebb80da66 --- /dev/null +++ b/test/fixtures/transformation/optional-core-aliasing/es6-spread/actual.js @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000000..4d00347582 --- /dev/null +++ b/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js @@ -0,0 +1,13 @@ +"use strict"; + +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 lyrics = ["head", "and", "toes"].concat(_toArray(parts)); diff --git a/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/actual.js b/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/actual.js new file mode 100644 index 0000000000..9dda19048b --- /dev/null +++ b/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/actual.js @@ -0,0 +1 @@ +var arr = [for (i of nums) i * i]; 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 new file mode 100644 index 0000000000..ccfa596f71 --- /dev/null +++ b/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js @@ -0,0 +1,15 @@ +"use strict"; + +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 arr = _toArray(nums).map(function (i) { + return i * i; +}); diff --git a/test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/actual.js b/test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/actual.js new file mode 100644 index 0000000000..a16dd23cb9 --- /dev/null +++ b/test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/actual.js @@ -0,0 +1 @@ +var z = { ...x }; diff --git a/test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/expected.js b/test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/expected.js new file mode 100644 index 0000000000..940d6e9d13 --- /dev/null +++ b/test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/expected.js @@ -0,0 +1,9 @@ +"use strict"; + +var _interopRequire = function (obj) { + return obj && (obj["default"] || obj); +}; + +var _core = _interopRequire(require("core-js/library")); + +var z = _core.Object.assign({}, x); diff --git a/test/fixtures/transformation/optional-core-aliasing/options.json b/test/fixtures/transformation/optional-core-aliasing/options.json new file mode 100644 index 0000000000..371b5f0b20 --- /dev/null +++ b/test/fixtures/transformation/optional-core-aliasing/options.json @@ -0,0 +1,4 @@ +{ + "optional": ["coreAliasing"], + "experimental": true +}