From 8ce5c5b6088fc3385cc64a25e0fbfe0e9045a8c0 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 14 Jan 2015 15:09:15 +1100 Subject: [PATCH 001/129] add jsx to possible extensions --- lib/6to5/register.js | 2 +- lib/6to5/util.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/6to5/register.js b/lib/6to5/register.js index d73532e1e9..f8647bea85 100644 --- a/lib/6to5/register.js +++ b/lib/6to5/register.js @@ -123,7 +123,7 @@ var hookExtensions = function (_exts) { }); }; -hookExtensions([".es6", ".es", ".js"]); +hookExtensions(util.canCompile.EXTENSIONS); module.exports = function (opts) { // normalise options diff --git a/lib/6to5/util.js b/lib/6to5/util.js index d161eec184..66af587e4f 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -12,11 +12,13 @@ var _ = require("lodash"); exports.inherits = util.inherits; exports.canCompile = function (filename, altExts) { - var exts = altExts || [".js", ".jsx", ".es6", ".es"]; + var exts = altExts || exports.canCompile.EXTENSIONS; var ext = path.extname(filename); return _.contains(exts, ext); }; +exports.canCompile.EXTENSIONS = [".js", ".jsx", ".es6", ".es"]; + exports.isInteger = function (i) { return _.isNumber(i) && i % 1 === 0; }; From e8dba2ad1e6490775dbc2fc6b96608921783fbf8 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 14 Jan 2015 18:05:11 +1100 Subject: [PATCH 002/129] add 2.12.1 changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e6ffb0938..1b6bc66cc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ _Note: Gaps between patch versions are faulty/broken releases._ +## 2.12.1 + + * **Internal** + * Add `.jsx` to list of allowed extensions. + ## 2.12.0 * **Bug Fix** From f6a2acdfb147b65d4b713015e5f583eb2cd3cee6 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 14 Jan 2015 18:06:51 +1100 Subject: [PATCH 003/129] v2.12.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a76b3beabe..eaaa35e15a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "6to5", "description": "Turn ES6 code into readable vanilla ES5 with source maps", - "version": "2.12.0", + "version": "2.12.1", "author": "Sebastian McKenzie ", "homepage": "https://github.com/6to5/6to5", "repository": { From f5f17f0ccbd771e25db25a9f93d6d29d4c635a12 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 14 Jan 2015 18:39:07 +1100 Subject: [PATCH 004/129] ignore function params, rest and catch clauses - webpack/webpack#688 --- CHANGELOG.md | 5 +++++ lib/6to5/types/index.js | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b6bc66cc2..81891ce2db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ _Note: Gaps between patch versions are faulty/broken releases._ +## 2.12.2 + + * **Internal** + * Exclude nodes in function parameters and catch clauses from `isReferenced` check. + ## 2.12.1 * **Internal** diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index ef433cd5b1..99308880ad 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -193,6 +193,17 @@ t.isReferenced = function (node, parent) { // we're a property key and we aren't computed so we aren't referenced if (t.isProperty(parent) && parent.key === node && !parent.computed) return false; + if (t.isFunction(parent)) { + // we're a function param + if (_.contains(parent.params, node)) return false; + + // we're a rest parameter + if (_.contains(parent.params, node)) return false; + } + + // we're a catch clause param + if (t.isCatchClause(parent) && parent.param === node) return false; + // we're a variable declarator id so we aren't referenced if (t.isVariableDeclarator(parent) && parent.id === node) return false; From a3b814a8973ce19b920b896646f7d092f6eb499a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 14 Jan 2015 18:46:36 +1100 Subject: [PATCH 005/129] fix default parameters closure scope --- lib/6to5/transformation/transformers/es6-default-parameters.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/6to5/transformation/transformers/es6-default-parameters.js b/lib/6to5/transformation/transformers/es6-default-parameters.js index db82d8e2d4..674c1096dc 100644 --- a/lib/6to5/transformation/transformers/es6-default-parameters.js +++ b/lib/6to5/transformation/transformers/es6-default-parameters.js @@ -45,7 +45,7 @@ exports.Function = function (node, parent, file, scope) { } // we're accessing a variable that's already defined within this function - var has = scope.get(param.name); + var has = scope.get(param.name, true); if (has && node.params.indexOf(has) < 0) { iife = true; } From 474471904053a12038d61cd5079f2607dce9d097 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 14 Jan 2015 18:48:29 +1100 Subject: [PATCH 006/129] v2.12.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index eaaa35e15a..a6c9ec91a7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "6to5", "description": "Turn ES6 code into readable vanilla ES5 with source maps", - "version": "2.12.1", + "version": "2.12.2", "author": "Sebastian McKenzie ", "homepage": "https://github.com/6to5/6to5", "repository": { From 245fcfe11021d68e5dbadd6495feaeb2789860b3 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 01:09:13 +1100 Subject: [PATCH 007/129] move mutator map checking to after loose test --- .../transformation/transformers/es6-classes.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index c31d1787b9..73ff53c5f5 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -179,13 +179,6 @@ Class.prototype.pushMethod = function (node) { var methodName = node.key; var kind = node.kind; - var mutatorMap = this.instanceMutatorMap; - if (node.static) { - this.hasStaticMutators = true; - mutatorMap = this.staticMutatorMap; - } else { - this.hasInstanceMutators = true; - } if (kind === "") { if (this.isLoose) { @@ -204,6 +197,14 @@ Class.prototype.pushMethod = function (node) { kind = "value"; } + var mutatorMap = this.instanceMutatorMap; + if (node.static) { + this.hasStaticMutators = true; + mutatorMap = this.staticMutatorMap; + } else { + this.hasInstanceMutators = true; + } + util.pushMutatorMap(mutatorMap, methodName, kind, node.computed, node); }; From 87b890c172fffaf8c8ee7b0b89c66f2351ffac8a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 01:14:49 +1100 Subject: [PATCH 008/129] fix parentheses deletion not support multilines - fixes #490 --- bin/_6to5-node | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/_6to5-node b/bin/_6to5-node index 35de287fa9..d711e5746b 100644 --- a/bin/_6to5-node +++ b/bin/_6to5-node @@ -96,8 +96,8 @@ function replEval(code, context, filename, callback) { var result; try { - if (/^\((.*?)\n\)$/.test(code)) { - code = code.slice(1, -2); // remove "(" and "\n)" + if (code[0] === "(" && code[code.length - 1] === ")") { + code = code.slice(1, -1); // remove "(" and ")" } result = _eval(code, filename); From 07ddfbeb5dd1199d4cf079c936becf8a15625fae Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 01:20:06 +1100 Subject: [PATCH 009/129] remove pointless prototypeProperties helper call --- .../es6-classes-loose/accessing-super-class/expected.js | 7 ------- .../es6-classes-loose/calling-super-properties/expected.js | 7 ------- 2 files changed, 14 deletions(-) diff --git a/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js b/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js index 6c9bd8d668..b75a433fa1 100644 --- a/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js +++ b/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js @@ -1,11 +1,6 @@ "use strict"; var _slice = Array.prototype.slice; -var _prototypeProperties = function (child, staticProps, instanceProps) { - if (staticProps) Object.defineProperties(child, staticProps); - if (instanceProps) Object.defineProperties(child.prototype, instanceProps); -}; - var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); @@ -51,7 +46,5 @@ var Test = (function (Foo) { (_Foo$foo2 = Foo.foo).call.apply(_Foo$foo2, [this, "test"].concat(_slice.call(arguments))); }; - _prototypeProperties(Test, {}, {}); - return Test; })(Foo); diff --git a/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js b/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js index 529795221c..a69f118608 100644 --- a/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js @@ -1,10 +1,5 @@ "use strict"; -var _prototypeProperties = function (child, staticProps, instanceProps) { - if (staticProps) Object.defineProperties(child, staticProps); - if (instanceProps) Object.defineProperties(child.prototype, instanceProps); -}; - var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); @@ -32,7 +27,5 @@ var Test = (function (Foo) { return Foo.wow.call(this); }; - _prototypeProperties(Test, {}); - return Test; })(Foo); From 6c5606b7e8a9851dc41105cf303eba1bcbf0f1bf Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 02:24:05 +1100 Subject: [PATCH 010/129] call transformer methods on traverse context --- lib/6to5/transformation/transformer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/6to5/transformation/transformer.js b/lib/6to5/transformation/transformer.js index d9587a38c0..f4de701b51 100644 --- a/lib/6to5/transformation/transformer.js +++ b/lib/6to5/transformation/transformer.js @@ -65,7 +65,7 @@ Transformer.prototype.transform = function (file) { if (exit) fn = fns.exit; if (!fn) return; - return fn(node, parent, file, scope); + return fn.call(this, node, parent, file, scope); }; }; From 44f06c0b4c4a124ebf362c054efc179e376c951a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 02:24:23 +1100 Subject: [PATCH 011/129] instance and static class method names --- .../transformers/es6-classes.js | 20 ++++++++++++++++--- .../es6-property-method-assignment.js | 16 +++++++++------ lib/6to5/types/index.js | 4 ++++ .../accessing-super-class/expected.js | 4 ++-- .../calling-super-properties/expected.js | 2 +- .../accessing-super-class/expected.js | 4 ++-- .../calling-super-properties/expected.js | 2 +- .../es6-classes/instance-method/expected.js | 2 +- .../es6-classes/statement/expected.js | 10 +++++----- .../es6-classes/static/expected.js | 2 +- .../private/expected.js | 4 ++-- 11 files changed, 46 insertions(+), 24 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 73ff53c5f5..d87aaa401f 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -1,12 +1,24 @@ -var traverse = require("../../traverse"); -var util = require("../../util"); -var t = require("../../types"); +var propertyMethodAssignment = require("./es6-property-method-assignment"); +var traverse = require("../../traverse"); +var util = require("../../util"); +var t = require("../../types"); exports.ClassDeclaration = function (node, parent, file, scope) { return new Class(node, file, scope, true).run(); }; exports.ClassExpression = function (node, parent, file, scope) { + if (!node.id) { + if (t.isProperty(parent) && parent.value === node && !parent.computed && t.isIdentifier(parent.key)) { + // var o = { foo: class {} }; + node.id = parent.key + } + + if (t.isVariableDeclarator(parent)) { + node.id = parent.id; + } + } + return new Class(node, file, scope, false).run(); }; @@ -181,6 +193,8 @@ Class.prototype.pushMethod = function (node) { var kind = node.kind; if (kind === "") { + propertyMethodAssignment._namedMethod(node, this.file, this.scope); + if (this.isLoose) { // use assignments instead of define properties for loose classes diff --git a/lib/6to5/transformation/transformers/es6-property-method-assignment.js b/lib/6to5/transformation/transformers/es6-property-method-assignment.js index b826f6977d..5dbe72c55a 100644 --- a/lib/6to5/transformation/transformers/es6-property-method-assignment.js +++ b/lib/6to5/transformation/transformers/es6-property-method-assignment.js @@ -2,13 +2,9 @@ var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); -exports.Property = function (node, parent, file, scope) { - if (!node.method) return; - - node.method = false; - +exports._namedMethod = function (node, file, scope) { var key = t.toComputedKey(node, node.key); - if (!t.isLiteral(key)) return; // we can't set a function id with this + if (!t.isLiteral(key)) return node; // we can't set a function id with this var id = t.toIdentifier(key.value); key = t.identifier(id); @@ -46,6 +42,14 @@ exports.Property = function (node, parent, file, scope) { } }; +exports.Property = function (node, parent, file, scope) { + if (!node.method) return; + + node.method = false; + + exports._namedMethod(node, file, scope); +}; + exports.ObjectExpression = function (node) { var mutatorMap = {}; var hasAny = false; diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 99308880ad..c00e9506af 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -201,6 +201,10 @@ t.isReferenced = function (node, parent) { if (_.contains(parent.params, node)) return false; } + if (t.isMethodDefinition(parent) && parent.key === node && !parent.computed) { + return false; + } + // we're a catch clause param if (t.isCatchClause(parent) && parent.param === node) return false; diff --git a/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js b/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js index b75a433fa1..92710413da 100644 --- a/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js +++ b/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js @@ -32,14 +32,14 @@ var Test = (function (Foo) { _inherits(Test, Foo); - Test.prototype.test = function () { + Test.prototype.test = function test() { var _Foo$prototype$test3, _Foo$prototype$test4; Foo.prototype.test.call(this); (_Foo$prototype$test3 = Foo.prototype.test).call.apply(_Foo$prototype$test3, [this].concat(_slice.call(arguments))); (_Foo$prototype$test4 = Foo.prototype.test).call.apply(_Foo$prototype$test4, [this, "test"].concat(_slice.call(arguments))); }; - Test.foo = function () { + Test.foo = function foo() { var _Foo$foo, _Foo$foo2; Foo.foo.call(this); (_Foo$foo = Foo.foo).call.apply(_Foo$foo, [this].concat(_slice.call(arguments))); diff --git a/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js b/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js index a69f118608..065e6fc263 100644 --- a/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js @@ -23,7 +23,7 @@ var Test = (function (Foo) { _inherits(Test, Foo); - Test.test = function () { + Test.test = function test() { return Foo.wow.call(this); }; diff --git a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js index a762c97203..e783b08f76 100644 --- a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js @@ -61,7 +61,7 @@ var Test = (function (Foo) { _prototypeProperties(Test, { foo: { - value: function () { + value: function foo() { var _get4; _get(Object.getPrototypeOf(Test), "foo", this).call(this); _get(Object.getPrototypeOf(Test), "foo", this).apply(this, arguments); @@ -73,7 +73,7 @@ var Test = (function (Foo) { } }, { test: { - value: function () { + value: function test() { var _get5; _get(Object.getPrototypeOf(Test.prototype), "test", this).call(this); _get(Object.getPrototypeOf(Test.prototype), "test", this).apply(this, arguments); diff --git a/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js b/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js index fc04a0bab4..b82d42c705 100644 --- a/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js @@ -52,7 +52,7 @@ var Test = (function (Foo) { _prototypeProperties(Test, { test: { - value: function () { + value: function test() { return _get(Object.getPrototypeOf(Test), "wow", this).call(this); }, writable: true, diff --git a/test/fixtures/transformation/es6-classes/instance-method/expected.js b/test/fixtures/transformation/es6-classes/instance-method/expected.js index 8887b8d645..f632a93cba 100644 --- a/test/fixtures/transformation/es6-classes/instance-method/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-method/expected.js @@ -10,7 +10,7 @@ var Test = (function () { _prototypeProperties(Test, null, { test: { - value: function () { + value: function test() { return 5 + 5; }, writable: true, diff --git a/test/fixtures/transformation/es6-classes/statement/expected.js b/test/fixtures/transformation/es6-classes/statement/expected.js index 1a10318ee2..ac555c8256 100644 --- a/test/fixtures/transformation/es6-classes/statement/expected.js +++ b/test/fixtures/transformation/es6-classes/statement/expected.js @@ -9,16 +9,16 @@ var BaseView = function BaseView() { this.autoRender = true; }; -var BaseView = function () { +var BaseView = function BaseView() { this.autoRender = true; }; var BaseView = (function () { - var _class2 = function () {}; + function BaseView() {} - _prototypeProperties(_class2, null, { + _prototypeProperties(BaseView, null, { foo: { - value: function () { + value: function foo() { this.autoRender = true; }, writable: true, @@ -27,5 +27,5 @@ var BaseView = (function () { } }); - return _class2; + return BaseView; })(); diff --git a/test/fixtures/transformation/es6-classes/static/expected.js b/test/fixtures/transformation/es6-classes/static/expected.js index 408d9a036a..b219f347c7 100644 --- a/test/fixtures/transformation/es6-classes/static/expected.js +++ b/test/fixtures/transformation/es6-classes/static/expected.js @@ -10,7 +10,7 @@ var A = (function () { _prototypeProperties(A, { a: { - value: function () {}, + value: function a() {}, writable: true, enumerable: true, configurable: true diff --git a/test/fixtures/transformation/es7-abstract-references/private/expected.js b/test/fixtures/transformation/es7-abstract-references/private/expected.js index d94a941e04..1af25539ca 100644 --- a/test/fixtures/transformation/es7-abstract-references/private/expected.js +++ b/test/fixtures/transformation/es7-abstract-references/private/expected.js @@ -16,7 +16,7 @@ var H = (function () { var J = new WeakMap(), K = new WeakMap(); var I = new WeakMap(); - var _class = function () {}; + function H() {} - return _class; + return H; })(); From b60eca0a7693d2571bbb488fad11520ac8af13c0 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 02:24:32 +1100 Subject: [PATCH 012/129] better typeof symbol transformer --- .../transformers/optional-typeof-symbol.js | 14 +++++++++++++- .../optional-typeof-symbol/basic/expected.js | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/6to5/transformation/transformers/optional-typeof-symbol.js b/lib/6to5/transformation/transformers/optional-typeof-symbol.js index 55be121b08..36b8458936 100644 --- a/lib/6to5/transformation/transformers/optional-typeof-symbol.js +++ b/lib/6to5/transformation/transformers/optional-typeof-symbol.js @@ -3,7 +3,19 @@ var t = require("../../types"); exports.optional = true; exports.UnaryExpression = function (node, parent, file) { + this.skip(); + if (node.operator === "typeof") { - return t.callExpression(file.addHelper("typeof"), [node.argument]); + var call = t.callExpression(file.addHelper("typeof"), [node.argument]); + if (t.isIdentifier(node.argument)) { + var undefLiteral = t.literal("undefined"); + return t.conditionalExpression( + t.binaryExpression("===", t.unaryExpression("typeof", node.argument), undefLiteral), + undefLiteral, + call + ); + } else { + return call; + } } }; diff --git a/test/fixtures/transformation/optional-typeof-symbol/basic/expected.js b/test/fixtures/transformation/optional-typeof-symbol/basic/expected.js index c33ed64571..a93f2923c6 100644 --- a/test/fixtures/transformation/optional-typeof-symbol/basic/expected.js +++ b/test/fixtures/transformation/optional-typeof-symbol/basic/expected.js @@ -5,4 +5,4 @@ var _typeof = function (obj) { }; var s = Symbol("s"); -assert.equal(_typeof(s), "symbol"); +assert.equal(typeof s === "undefined" ? "undefined" : _typeof(s), "symbol"); From bf66d78210cc0384719727a4ccbc449ab407e164 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 02:27:17 +1100 Subject: [PATCH 013/129] add 2.12.3 changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81891ce2db..c6043c395b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ _Note: Gaps between patch versions are faulty/broken releases._ +## 2.12.3 + + * **Spec Compliancy** + * Optional `typeof` transformer checks for `undefined` before passing it to the helper. + * Class methods are now named. + ## 2.12.2 * **Internal** From 98c0e185b80188d35bc9f8a8a348f7ec6aeb1a49 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 02:27:45 +1100 Subject: [PATCH 014/129] add missing semicolon --- lib/6to5/transformation/transformers/es6-classes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index d87aaa401f..69d3a41205 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -11,7 +11,7 @@ exports.ClassExpression = function (node, parent, file, scope) { if (!node.id) { if (t.isProperty(parent) && parent.value === node && !parent.computed && t.isIdentifier(parent.key)) { // var o = { foo: class {} }; - node.id = parent.key + node.id = parent.key; } if (t.isVariableDeclarator(parent)) { From e01b7d288fe7f88267cf9c3fbcdc52e2a378f8a1 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 02:29:44 +1100 Subject: [PATCH 015/129] v2.12.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a6c9ec91a7..c51ab1165a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "6to5", "description": "Turn ES6 code into readable vanilla ES5 with source maps", - "version": "2.12.2", + "version": "2.12.3", "author": "Sebastian McKenzie ", "homepage": "https://github.com/6to5/6to5", "repository": { From c6f3a55c03581d42c91a05e3864b68df40768534 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 14 Jan 2015 14:00:58 +0300 Subject: [PATCH 016/129] Refactor buffer for clarity and avoid regex for performance --- lib/6to5/generation/buffer.js | 42 ++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/lib/6to5/generation/buffer.js b/lib/6to5/generation/buffer.js index c9b4e662fe..e702efd00c 100644 --- a/lib/6to5/generation/buffer.js +++ b/lib/6to5/generation/buffer.js @@ -67,32 +67,58 @@ Buffer.prototype.removeLast = function (cha) { Buffer.prototype.newline = function (i, removeLast) { if (this.format.compact) return; - - if (_.isBoolean(i)) { - removeLast = i; - i = null; - } + removeLast = removeLast || false; if (_.isNumber(i)) { if (this.endsWith("{\n")) i--; if (this.endsWith(util.repeat(i, "\n"))) return; - for (var j = 0; j < i; j++) { - this.newline(null, removeLast); + while (i--) { + this._newline(removeLast); } return; } + if (_.isBoolean(i)) { + removeLast = i; + } + + this._newline(removeLast); +}; + +Buffer.prototype._newline = function (removeLast) { if (removeLast && this.isLast("\n")) this.removeLast("\n"); this.removeLast(" "); // remove whitespace if last character was a newline - this.buf = this.buf.replace(/\n +$/, "\n"); + var trimStart = this._getPostNewlineTrailingSpaceStart(); + if (trimStart) { + this.buf = this.buf.substring(0, trimStart); + } this._push("\n"); }; +Buffer.prototype._getPostNewlineTrailingSpaceStart = function () { + var lastNewlineIndex = this.buf.lastIndexOf('\n'); + if (lastNewlineIndex === -1) + return; + + var index = this.buf.length - 1; + while (index > lastNewlineIndex) { + if (this.buf[index] !== ' ') { + break; + } + + index--; + } + + if (index === lastNewlineIndex) { + return index + 1; + } +}; + Buffer.prototype.push = function (str, noIndent) { if (this._indent && !noIndent && str !== "\n") { // we have an indent level and we aren't pushing a newline From c7c90acf3fb5863010c608bda3f9dc2106a3d3dd Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 14 Jan 2015 19:08:33 +0300 Subject: [PATCH 017/129] Store ranges instead of line indexes for performance --- lib/6to5/generation/whitespace.js | 16 +++------ lib/6to5/util.js | 60 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/lib/6to5/generation/whitespace.js b/lib/6to5/generation/whitespace.js index c7dda860da..6d16536f26 100644 --- a/lib/6to5/generation/whitespace.js +++ b/lib/6to5/generation/whitespace.js @@ -1,6 +1,7 @@ module.exports = Whitespace; -var _ = require("lodash"); +var _ = require("lodash"); +var util = require("../util"); function Whitespace(tokens, comments) { this.tokens = _.sortBy(tokens.concat(comments), "start"); @@ -61,14 +62,5 @@ Whitespace.prototype.getNewlinesBetween = function (startToken, endToken) { var start = startToken ? startToken.loc.end.line : 1; var end = endToken.loc.start.line; - var lines = 0; - - for (var line = start; line < end; line++) { - if (!_.contains(this.used, line)) { - this.used.push(line); - lines++; - } - } - - return lines; -}; + return util.mergeIntegerRange(this.used, start, end); +}; \ No newline at end of file diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 66af587e4f..23d9ec9227 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -204,6 +204,66 @@ exports.repeat = function (width, cha) { return result; }; +exports.mergeIntegerRange = function (ranges, start, end) { + var pointsAdded = 0; + var insertIndex; + var rangeIndex; + var point; + var range; + var matchingRange; + var rangeImmediatelyToLeft; + var rangeImmediatelyToRight; + + for (point = start; point < end; point++) { + matchingRange = null; + rangeImmediatelyToLeft = null; + rangeImmediatelyToRight = null; + insertIndex = ranges.length; + + for (rangeIndex = 0; rangeIndex < ranges.length; rangeIndex++) { + range = ranges[rangeIndex]; + + if (point >= range.start && point <= range.end) { + matchingRange = range; + break; + } + + if (point < range.start) { + insertIndex = rangeIndex; + } + + if (point === range.end + 1) { + rangeImmediatelyToLeft = range; + } + + if (point === range.start - 1) { + rangeImmediatelyToRight = range; + } + } + + if (matchingRange) + continue; + + pointsAdded++; + + if (rangeImmediatelyToLeft && rangeImmediatelyToRight) { + ranges.splice(ranges.indexOf(rangeImmediatelyToRight), 1); + rangeImmediatelyToLeft.end = rangeImmediatelyToRight.end; + } else if (rangeImmediatelyToLeft) { + rangeImmediatelyToLeft.end = point; + } else if (rangeImmediatelyToRight) { + rangeImmediatelyToRight.start = point; + } else { + ranges.splice(insertIndex, 0, { + start: point, + end: point + }); + } + } + + return pointsAdded; +}; + exports.normaliseAst = function (ast, comments, tokens) { if (ast && ast.type === "Program") { return t.file(ast, comments || [], tokens || []); From 19eaa181a524f9a1ed9f4073f284a5dde271a6d0 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 14 Jan 2015 20:02:40 +0300 Subject: [PATCH 018/129] Speed up common case where consumer moves only forward --- lib/6to5/generation/whitespace.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/6to5/generation/whitespace.js b/lib/6to5/generation/whitespace.js index 6d16536f26..5e356c2f27 100644 --- a/lib/6to5/generation/whitespace.js +++ b/lib/6to5/generation/whitespace.js @@ -3,9 +3,22 @@ module.exports = Whitespace; var _ = require("lodash"); var util = require("../util"); +// a helper to iterate array from arbitrary index +function getLookupIndex(i, base, max) { + i += base; + + if (i >= max) + i -= max; + + return i; +} + function Whitespace(tokens, comments) { this.tokens = _.sortBy(tokens.concat(comments), "start"); this.used = []; + + // speed up common case where methods are called for next tokens + this._lastFoundIndex = 0; } Whitespace.prototype.getNewlinesBefore = function (node) { @@ -14,13 +27,15 @@ Whitespace.prototype.getNewlinesBefore = function (node) { var tokens = this.tokens; var token; - for (var i = 0; i < tokens.length; i++) { + for (var j = 0; j < tokens.length; j++) { + var i = getLookupIndex(j, this._lastFoundIndex, this.tokens.length); token = tokens[i]; // this is the token this node starts with if (node.start === token.start) { startToken = tokens[i - 1]; endToken = token; + this._lastFoundIndex = i; break; } } @@ -34,13 +49,15 @@ Whitespace.prototype.getNewlinesAfter = function (node) { var tokens = this.tokens; var token; - for (var i = 0; i < tokens.length; i++) { + for (var j = 0; j < tokens.length; j++) { + var i = getLookupIndex(j, this._lastFoundIndex, this.tokens.length); token = tokens[i]; // this is the token this node ends with if (node.end === token.end) { startToken = token; endToken = tokens[i + 1]; + this._lastFoundIndex = i; break; } } From 58a91ee9e9173acb2d0b8ec5d3c3acb2db1ef68e Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 14 Jan 2015 20:59:32 +0300 Subject: [PATCH 019/129] Optimize node type lookup --- lib/6to5/generation/node/index.js | 2 +- lib/6to5/types/index.js | 91 ++++++++++++++++++------------- 2 files changed, 54 insertions(+), 39 deletions(-) diff --git a/lib/6to5/generation/node/index.js b/lib/6to5/generation/node/index.js index 42daaafaec..bedd7ba60a 100644 --- a/lib/6to5/generation/node/index.js +++ b/lib/6to5/generation/node/index.js @@ -13,7 +13,7 @@ var find = function (obj, node, parent) { for (var i = 0; i < types.length; i++) { var type = types[i]; - if (t["is" + type](node)) { + if (t.is(type, node)) { var fn = obj[type]; result = fn(node, parent); if (result != null) break; diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index c00e9506af..1e2f6487d9 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -7,14 +7,18 @@ t.NATIVE_TYPE_NAMES = ["Array", "Object", "Number", "Boolean", "Date", "Array", // -var addAssert = function (type, is) { +function registerType(type, skipAliases) { + var is = t["is" + type] = function (node, opts) { + return t.is(type, node, opts, skipAliases); + }; + t["assert" + type] = function (node, opts) { opts = opts || {}; if (!is(node, opts)) { throw new Error("Expected type " + JSON.stringify(type) + " with option " + JSON.stringify(opts)); } }; -}; +} t.STATEMENT_OR_BLOCK_KEYS = ["consequent", "body"]; @@ -22,14 +26,48 @@ t.STATEMENT_OR_BLOCK_KEYS = ["consequent", "body"]; t.VISITOR_KEYS = require("./visitor-keys"); -_.each(t.VISITOR_KEYS, function (keys, type) { - var is = t["is" + type] = function (node, opts) { - return node && node.type === type && t.shallowEqual(node, opts); - }; +t.ALIAS_KEYS = require("./alias-keys"); - addAssert(type, is); +t.FLIPPED_ALIAS_KEYS = {}; + +_.each(t.VISITOR_KEYS, function (keys, type) { + registerType(type, true); }); +_.each(t.ALIAS_KEYS, function (aliases, type) { + _.each(aliases, function (alias) { + var types = t.FLIPPED_ALIAS_KEYS[alias] = t.FLIPPED_ALIAS_KEYS[alias] || []; + types.push(type); + }); +}); + +_.each(t.FLIPPED_ALIAS_KEYS, function (types, type) { + t[type.toUpperCase() + "_TYPES"] = types; + registerType(type, false); +}); + +t.is = function (type, node, opts, skipAliases) { + if (!node) return; + + var typeMatches = (type === node.type); + + if (!typeMatches && !skipAliases) { + var aliases = t.FLIPPED_ALIAS_KEYS[type]; + + if (typeof aliases !== 'undefined') + typeMatches = aliases.indexOf(node.type) > -1; + } + + if (!typeMatches) { + return false; + } + + if (typeof opts !== 'undefined') + return t.shallowEqual(node, opts); + + return true; +}; + // t.BUILDER_KEYS = _.defaults(require("./builder-keys"), t.VISITOR_KEYS); @@ -45,29 +83,6 @@ _.each(t.BUILDER_KEYS, function (keys, type) { }; }); -// - -t.ALIAS_KEYS = require("./alias-keys"); - -t.FLIPPED_ALIAS_KEYS = {}; - -_.each(t.ALIAS_KEYS, function (aliases, type) { - _.each(aliases, function (alias) { - var types = t.FLIPPED_ALIAS_KEYS[alias] = t.FLIPPED_ALIAS_KEYS[alias] || []; - types.push(type); - }); -}); - -_.each(t.FLIPPED_ALIAS_KEYS, function (types, type) { - t[type.toUpperCase() + "_TYPES"] = types; - - var is = t["is" + type] = function (node, opts) { - return node && types.indexOf(node.type) >= 0 && t.shallowEqual(node, opts); - }; - - addAssert(type, is); -}); - /** * Description * @@ -139,17 +154,17 @@ t.toSequenceExpression = function (nodes, scope) { // t.shallowEqual = function (actual, expected) { - var same = true; + var keys = Object.keys(expected); + var key; - if (expected) { - _.each(expected, function (val, key) { - if (actual[key] !== val) { - return same = false; - } - }); + for (var i = 0; i < keys.length; i++) { + key = keys[i]; + + if (actual[key] !== expected[key]) + return false; } - return same; + return true; }; /** From da65500545fb4581e0398aa4c792c0c3418680e9 Mon Sep 17 00:00:00 2001 From: Chris Wheatley Date: Wed, 14 Jan 2015 22:11:46 +0000 Subject: [PATCH 020/129] expand upon the contributing guidelines --- CONTRIBUTING.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ad8ff721f5..3a6648b98c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,6 +1,18 @@ # Contributing -Before contributing, please read the [code of conduct](https://github.com/6to5/6to5/blob/master/CODE_OF_CONDUCT.md). +Contributions are always welcome, no matter how large or small. Before contributing, please read the [code of conduct](https://github.com/6to5/6to5/blob/master/CODE_OF_CONDUCT.md). + +## Developing + +#### Workflow + +* Fork the repository +* Clone your fork and change directory to it (`git clone git@github.com:yourUserName/6to5.git && cd 6to5`) +* Link your forked clone (`npm link`) +* Develop your changes ensuring you're fetching changes from upstream often +* Create new pull request explaining your proposed change or reference an issue in your commit message + +#### Code Standards * **General** * No ES6 syntax features or methods, exclusively ES5. From a14f971b8a7c4d4fce3d5a83f9afe93309db5095 Mon Sep 17 00:00:00 2001 From: Chris Wheatley Date: Wed, 14 Jan 2015 22:25:20 +0000 Subject: [PATCH 021/129] add further commands required for workflow --- CONTRIBUTING.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3a6648b98c..c87a5112a3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,8 +8,10 @@ Contributions are always welcome, no matter how large or small. Before contribut * Fork the repository * Clone your fork and change directory to it (`git clone git@github.com:yourUserName/6to5.git && cd 6to5`) +* Install the project dependencies (`npm install`) * Link your forked clone (`npm link`) -* Develop your changes ensuring you're fetching changes from upstream often +* Develop your changes ensuring you're fetching updates from upstream often +* Ensure the test are passing (`make test`) * Create new pull request explaining your proposed change or reference an issue in your commit message #### Code Standards From 25cb0c6344ad53e0813d1fd2b6a6460588ed7a17 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Wed, 14 Jan 2015 18:23:49 -0500 Subject: [PATCH 022/129] Update CONTRIBUTING.md Explain git init submodules step when checking out --- CONTRIBUTING.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c87a5112a3..a84ed57fc5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,6 +8,7 @@ Contributions are always welcome, no matter how large or small. Before contribut * Fork the repository * Clone your fork and change directory to it (`git clone git@github.com:yourUserName/6to5.git && cd 6to5`) +* Install git submodules (`git submodule init && git submodule update`) * Install the project dependencies (`npm install`) * Link your forked clone (`npm link`) * Develop your changes ensuring you're fetching updates from upstream often From 7d732ef5035307318c95241e98afc7b9cb9c0426 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Wed, 14 Jan 2015 19:01:53 -0500 Subject: [PATCH 023/129] Update CONTRIBUTING.md --- CONTRIBUTING.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a84ed57fc5..15314b185c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,8 +8,7 @@ Contributions are always welcome, no matter how large or small. Before contribut * Fork the repository * Clone your fork and change directory to it (`git clone git@github.com:yourUserName/6to5.git && cd 6to5`) -* Install git submodules (`git submodule init && git submodule update`) -* Install the project dependencies (`npm install`) +* Install the project dependencies (`make bootstrap`) * Link your forked clone (`npm link`) * Develop your changes ensuring you're fetching updates from upstream often * Ensure the test are passing (`make test`) From e5463cc2a97cafbf9d35656038f19b45dd75ffce Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Wed, 14 Jan 2015 18:49:23 -0500 Subject: [PATCH 024/129] Improve performance of rest parameter. Rather than initing an empty array and filling, create an array of the correct size up-front. Minor gain on chromium, but considerably (~5x) faster in spidermonkey/firefox. --- lib/6to5/transformation/templates/rest.js | 2 +- .../transformers/es6-rest-parameters.js | 20 +++++++++++++------ .../arrow-functions/expected.js | 6 +++--- .../es6-rest-parameters/multiple/expected.js | 12 +++++------ .../es6-rest-parameters/single/expected.js | 12 +++++------ 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/lib/6to5/transformation/templates/rest.js b/lib/6to5/transformation/templates/rest.js index f396f01117..c15ce8815d 100644 --- a/lib/6to5/transformation/templates/rest.js +++ b/lib/6to5/transformation/templates/rest.js @@ -1,3 +1,3 @@ -for (var KEY = START; KEY < ARGUMENTS.length; KEY++) { +for (var LEN = ARGUMENTS.length, ARRAY = Array(ARRAY_LEN), KEY = START; KEY < LEN; KEY++) { ARRAY[ARRAY_KEY] = ARGUMENTS[KEY]; } diff --git a/lib/6to5/transformation/transformers/es6-rest-parameters.js b/lib/6to5/transformation/transformers/es6-rest-parameters.js index 266b6f62f7..d1de130e31 100644 --- a/lib/6to5/transformation/transformers/es6-rest-parameters.js +++ b/lib/6to5/transformation/transformers/es6-rest-parameters.js @@ -14,23 +14,31 @@ exports.Function = function (node, parent, file) { var start = t.literal(node.params.length); var key = file.generateUidIdentifier("key"); + var len = file.generateUidIdentifier("len"); var arrKey = key; if (node.params.length) { - arrKey = t.binaryExpression("-", arrKey, start); + arrKey = t.binaryExpression("-", key, start); + } + + var arrLen = len; + if (node.params.length) { + arrLen = t.conditionalExpression( + t.binaryExpression(">", len, start), + t.binaryExpression("-", len, start), + t.literal(0) + ); } node.body.body.unshift( - t.variableDeclaration("var", [ - t.variableDeclarator(rest, t.arrayExpression([])) - ]), - util.template("rest", { ARGUMENTS: argsId, ARRAY_KEY: arrKey, + ARRAY_LEN: arrLen, START: start, ARRAY: rest, - KEY: key + KEY: key, + LEN: len, }) ); }; diff --git a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js index cb971ebc77..fa875fd363 100644 --- a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js @@ -1,9 +1,9 @@ "use strict"; var concat = function () { - var arrs = []; - - for (var _key = 0; _key < arguments.length; _key++) { + for (var _len = arguments.length, + arrs = Array(_len), + _key = 0; _key < _len; _key++) { arrs[_key] = arguments[_key]; } }; diff --git a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js index 56ff1c407f..dc6e082ce6 100644 --- a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js @@ -1,17 +1,17 @@ "use strict"; var t = function (f) { - var items = []; - - for (var _key = 1; _key < arguments.length; _key++) { + for (var _len = arguments.length, + items = Array(_len > 1 ? _len - 1 : 0), + _key = 1; _key < _len; _key++) { items[_key - 1] = arguments[_key]; } }; function t(f) { - var items = []; - - for (var _key2 = 1; _key2 < arguments.length; _key2++) { + for (var _len2 = arguments.length, + items = Array(_len2 > 1 ? _len2 - 1 : 0), + _key2 = 1; _key2 < _len2; _key2++) { items[_key2 - 1] = arguments[_key2]; } } diff --git a/test/fixtures/transformation/es6-rest-parameters/single/expected.js b/test/fixtures/transformation/es6-rest-parameters/single/expected.js index 316b3ec9a4..d6f9e1a5fe 100644 --- a/test/fixtures/transformation/es6-rest-parameters/single/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/single/expected.js @@ -1,17 +1,17 @@ "use strict"; var t = function () { - var items = []; - - for (var _key = 0; _key < arguments.length; _key++) { + for (var _len = arguments.length, + items = Array(_len), + _key = 0; _key < _len; _key++) { items[_key] = arguments[_key]; } }; function t() { - var items = []; - - for (var _key2 = 0; _key2 < arguments.length; _key2++) { + for (var _len2 = arguments.length, + items = Array(_len2), + _key2 = 0; _key2 < _len2; _key2++) { items[_key2] = arguments[_key2]; } } From a452f781b80ac1a239b6d0d4ed527fc6733d1b9f Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 15 Jan 2015 03:38:46 +0300 Subject: [PATCH 025/129] Slightly refactor and add explanations for optimized functions --- lib/6to5/generation/buffer.js | 13 ++++++------- lib/6to5/generation/whitespace.js | 23 +++++++++++++++++++++-- lib/6to5/types/index.js | 26 ++++++++++++++++++++++---- lib/6to5/util.js | 18 ++++++++++++++++++ 4 files changed, 67 insertions(+), 13 deletions(-) diff --git a/lib/6to5/generation/buffer.js b/lib/6to5/generation/buffer.js index e702efd00c..2fadf5db33 100644 --- a/lib/6to5/generation/buffer.js +++ b/lib/6to5/generation/buffer.js @@ -92,15 +92,14 @@ Buffer.prototype._newline = function (removeLast) { this.removeLast(" "); // remove whitespace if last character was a newline - var trimStart = this._getPostNewlineTrailingSpaceStart(); - if (trimStart) { - this.buf = this.buf.substring(0, trimStart); - } - + this._removeSpacesAfterLastNewline(); this._push("\n"); }; -Buffer.prototype._getPostNewlineTrailingSpaceStart = function () { +/** + * If buffer ends with a newline and some spaces after it, trim those spaces. + */ +Buffer.prototype._removeSpacesAfterLastNewline = function () { var lastNewlineIndex = this.buf.lastIndexOf('\n'); if (lastNewlineIndex === -1) return; @@ -115,7 +114,7 @@ Buffer.prototype._getPostNewlineTrailingSpaceStart = function () { } if (index === lastNewlineIndex) { - return index + 1; + this.buf = this.buf.substring(0, index + 1); } }; diff --git a/lib/6to5/generation/whitespace.js b/lib/6to5/generation/whitespace.js index 5e356c2f27..637b155c3a 100644 --- a/lib/6to5/generation/whitespace.js +++ b/lib/6to5/generation/whitespace.js @@ -3,7 +3,15 @@ module.exports = Whitespace; var _ = require("lodash"); var util = require("../util"); -// a helper to iterate array from arbitrary index +/** + * Returns `i`th number from `base`, continuing from 0 when `max` is reached. + * Useful for shifting `for` loop by a fixed number but going over all items. + * + * @param {Number} i Current index in the loop + * @param {Number} base Start index for which to return 0 + * @param {Number} max Array length + * @returns {Number} shiftedIndex + */ function getLookupIndex(i, base, max) { i += base; @@ -17,7 +25,14 @@ function Whitespace(tokens, comments) { this.tokens = _.sortBy(tokens.concat(comments), "start"); this.used = []; - // speed up common case where methods are called for next tokens + // Profiling this code shows that while generator passes over it, indexes + // returned by `getNewlinesBefore` and `getNewlinesAfter` are always increasing. + + // We use this implementation detail for an optimization: instead of always + // starting to look from `this.tokens[0]`, we will start `for` loops from the + // previous successful match. We will enumerate all tokens—but the common + // case will be much faster. + this._lastFoundIndex = 0; } @@ -28,6 +43,7 @@ Whitespace.prototype.getNewlinesBefore = function (node) { var token; for (var j = 0; j < tokens.length; j++) { + // optimize for forward traversal by shifting for loop index var i = getLookupIndex(j, this._lastFoundIndex, this.tokens.length); token = tokens[i]; @@ -35,6 +51,7 @@ Whitespace.prototype.getNewlinesBefore = function (node) { if (node.start === token.start) { startToken = tokens[i - 1]; endToken = token; + this._lastFoundIndex = i; break; } @@ -50,6 +67,7 @@ Whitespace.prototype.getNewlinesAfter = function (node) { var token; for (var j = 0; j < tokens.length; j++) { + // optimize for forward traversal by shifting for loop index var i = getLookupIndex(j, this._lastFoundIndex, this.tokens.length); token = tokens[i]; @@ -57,6 +75,7 @@ Whitespace.prototype.getNewlinesAfter = function (node) { if (node.end === token.end) { startToken = token; endToken = tokens[i + 1]; + this._lastFoundIndex = i; break; } diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 1e2f6487d9..75270807bf 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -7,9 +7,16 @@ t.NATIVE_TYPE_NAMES = ["Array", "Object", "Number", "Boolean", "Date", "Array", // -function registerType(type, skipAliases) { +/** + * Registers `is[Type]` and `assert[Type]` generated functions for a given `type`. + * Pass `skipAliasCheck` to force it to directly compare `node.type` with `type`. + * + * @param {String} type + * @param {Boolean?} skipAliasCheck + */ +function registerType(type, skipAliasCheck) { var is = t["is" + type] = function (node, opts) { - return t.is(type, node, opts, skipAliases); + return t.is(type, node, opts, skipAliasCheck); }; t["assert" + type] = function (node, opts) { @@ -46,12 +53,23 @@ _.each(t.FLIPPED_ALIAS_KEYS, function (types, type) { registerType(type, false); }); -t.is = function (type, node, opts, skipAliases) { +/** + * Returns whether `node` is of given `type`. + * For better performance, use this instead of `is[Type]` when `type` is unknown. + * Optionally, pass `skipAliasCheck` to directly compare `node.type` with `type`. + * + * @param {String} type + * @param {Node} node + * @param {Object?} opts + * @param {Boolean?} skipAliasCheck + * @returns {Boolean} isOfType + */ +t.is = function (type, node, opts, skipAliasCheck) { if (!node) return; var typeMatches = (type === node.type); - if (!typeMatches && !skipAliases) { + if (!typeMatches && !skipAliasCheck) { var aliases = t.FLIPPED_ALIAS_KEYS[type]; if (typeof aliases !== 'undefined') diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 23d9ec9227..c594f3e653 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -204,6 +204,24 @@ exports.repeat = function (width, cha) { return result; }; +/** + * Merges a range of integers defined by `start` and `end` into an existing + * array of `ranges`, optimizing for common cases and attempting to keep them + * sorted. Returns the count of integers that have been added to `ranges` during + * the call. + * + * @example + * > var ranges = [{ start: 1, end: 3}, { start: 8, end : 10 }]; + * > mergeIntegerRange(ranges, 6, 9); + * 2 + * > ranges + * [{ start: 1, end: 3}, { start: 6, end : 10 }]; + * + * @param {Array} ranges An array of ranges that function will mutate + * @param {Number} start Beginning of range being added + * @param {Number} end End of range being added + * @returns {Number} pointsAdded How many new integers have been added + */ exports.mergeIntegerRange = function (ranges, start, end) { var pointsAdded = 0; var insertIndex; From a1b326a0abb2f6cc67c66644cf45aa3af1e79ad8 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 15 Jan 2015 03:50:52 +0300 Subject: [PATCH 026/129] Use object lookup instead of array --- lib/6to5/generation/whitespace.js | 13 ++++-- lib/6to5/util.js | 78 ------------------------------- 2 files changed, 10 insertions(+), 81 deletions(-) diff --git a/lib/6to5/generation/whitespace.js b/lib/6to5/generation/whitespace.js index 637b155c3a..aa0993bc2a 100644 --- a/lib/6to5/generation/whitespace.js +++ b/lib/6to5/generation/whitespace.js @@ -1,7 +1,6 @@ module.exports = Whitespace; var _ = require("lodash"); -var util = require("../util"); /** * Returns `i`th number from `base`, continuing from 0 when `max` is reached. @@ -23,7 +22,7 @@ function getLookupIndex(i, base, max) { function Whitespace(tokens, comments) { this.tokens = _.sortBy(tokens.concat(comments), "start"); - this.used = []; + this.used = {}; // Profiling this code shows that while generator passes over it, indexes // returned by `getNewlinesBefore` and `getNewlinesAfter` are always increasing. @@ -97,6 +96,14 @@ Whitespace.prototype.getNewlinesAfter = function (node) { Whitespace.prototype.getNewlinesBetween = function (startToken, endToken) { var start = startToken ? startToken.loc.end.line : 1; var end = endToken.loc.start.line; + var lines = 0; - return util.mergeIntegerRange(this.used, start, end); + for (var line = start; line < end; line++) { + if (typeof this.used[line] === 'undefined') { + this.used[line] = true; + lines++; + } + } + + return lines; }; \ No newline at end of file diff --git a/lib/6to5/util.js b/lib/6to5/util.js index c594f3e653..66af587e4f 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -204,84 +204,6 @@ exports.repeat = function (width, cha) { return result; }; -/** - * Merges a range of integers defined by `start` and `end` into an existing - * array of `ranges`, optimizing for common cases and attempting to keep them - * sorted. Returns the count of integers that have been added to `ranges` during - * the call. - * - * @example - * > var ranges = [{ start: 1, end: 3}, { start: 8, end : 10 }]; - * > mergeIntegerRange(ranges, 6, 9); - * 2 - * > ranges - * [{ start: 1, end: 3}, { start: 6, end : 10 }]; - * - * @param {Array} ranges An array of ranges that function will mutate - * @param {Number} start Beginning of range being added - * @param {Number} end End of range being added - * @returns {Number} pointsAdded How many new integers have been added - */ -exports.mergeIntegerRange = function (ranges, start, end) { - var pointsAdded = 0; - var insertIndex; - var rangeIndex; - var point; - var range; - var matchingRange; - var rangeImmediatelyToLeft; - var rangeImmediatelyToRight; - - for (point = start; point < end; point++) { - matchingRange = null; - rangeImmediatelyToLeft = null; - rangeImmediatelyToRight = null; - insertIndex = ranges.length; - - for (rangeIndex = 0; rangeIndex < ranges.length; rangeIndex++) { - range = ranges[rangeIndex]; - - if (point >= range.start && point <= range.end) { - matchingRange = range; - break; - } - - if (point < range.start) { - insertIndex = rangeIndex; - } - - if (point === range.end + 1) { - rangeImmediatelyToLeft = range; - } - - if (point === range.start - 1) { - rangeImmediatelyToRight = range; - } - } - - if (matchingRange) - continue; - - pointsAdded++; - - if (rangeImmediatelyToLeft && rangeImmediatelyToRight) { - ranges.splice(ranges.indexOf(rangeImmediatelyToRight), 1); - rangeImmediatelyToLeft.end = rangeImmediatelyToRight.end; - } else if (rangeImmediatelyToLeft) { - rangeImmediatelyToLeft.end = point; - } else if (rangeImmediatelyToRight) { - rangeImmediatelyToRight.start = point; - } else { - ranges.splice(insertIndex, 0, { - start: point, - end: point - }); - } - } - - return pointsAdded; -}; - exports.normaliseAst = function (ast, comments, tokens) { if (ast && ast.type === "Program") { return t.file(ast, comments || [], tokens || []); From c7ba566ad6d89d6096813dbc659f4d5e1f85dc0a Mon Sep 17 00:00:00 2001 From: Shinnosuke Watanabe Date: Thu, 15 Jan 2015 09:55:10 +0900 Subject: [PATCH 027/129] Use container-based infrastructure on Travis CI http://docs.travis-ci.com/user/workers/container-based-infrastructure/ --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 7981fb7440..b5e68b204e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ +sudo: false language: node_js node_js: - "0.10" From 081dd33e73ce3ce8a639e84a9af901b388b491b5 Mon Sep 17 00:00:00 2001 From: Shinnosuke Watanabe Date: Thu, 15 Jan 2015 10:07:54 +0900 Subject: [PATCH 028/129] Update repository info and homepage URL * link to https://6to5.org * use user/repo style --- package.json | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index c51ab1165a..baaca5241e 100644 --- a/package.json +++ b/package.json @@ -3,14 +3,8 @@ "description": "Turn ES6 code into readable vanilla ES5 with source maps", "version": "2.12.3", "author": "Sebastian McKenzie ", - "homepage": "https://github.com/6to5/6to5", - "repository": { - "type": "git", - "url": "https://github.com/6to5/6to5.git" - }, - "bugs": { - "url": "https://github.com/6to5/6to5/issues" - }, + "homepage": "https://6to5.org/", + "repository": "6to5/6to5", "preferGlobal": true, "main": "lib/6to5/index.js", "bin": { From 1002cf7796b066e58b214bb2c7d572b62bd5ddf5 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 15 Jan 2015 04:18:18 +0300 Subject: [PATCH 029/129] Avoid Node allocations by making prototype call statics --- lib/6to5/generation/node/index.js | 43 +++++++++++++------------------ 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/lib/6to5/generation/node/index.js b/lib/6to5/generation/node/index.js index bedd7ba60a..179b3015d7 100644 --- a/lib/6to5/generation/node/index.js +++ b/lib/6to5/generation/node/index.js @@ -28,13 +28,11 @@ function Node(node, parent) { this.node = node; } -Node.prototype.isUserWhitespacable = function () { - return t.isUserWhitespacable(this.node); +Node.isUserWhitespacable = function (node) { + return t.isUserWhitespacable(node); }; -Node.prototype.needsWhitespace = function (type) { - var parent = this.parent; - var node = this.node; +Node.needsWhitespace = function (node, parent, type) { if (!node) return 0; if (t.isExpressionStatement(node)) { @@ -51,18 +49,15 @@ Node.prototype.needsWhitespace = function (type) { return lines || 0; }; -Node.prototype.needsWhitespaceBefore = function () { - return this.needsWhitespace("before"); +Node.needsWhitespaceBefore = function (node, parent) { + return Node.needsWhitespace(node, parent, "before"); }; -Node.prototype.needsWhitespaceAfter = function () { - return this.needsWhitespace("after"); +Node.needsWhitespaceAfter = function (node, parent) { + return Node.needsWhitespace(node, parent, "after"); }; -Node.prototype.needsParens = function () { - var parent = this.parent; - var node = this.node; - +Node.needsParens = function (node, parent) { if (!parent) return false; if (t.isNewExpression(parent) && parent.callee === node) { @@ -77,10 +72,7 @@ Node.prototype.needsParens = function () { return find(parens, node, parent); }; -Node.prototype.needsParensNoLineTerminator = function () { - var parent = this.parent; - var node = this.node; - +Node.needsParensNoLineTerminator = function (node, parent) { if (!parent) return false; // no comments @@ -100,17 +92,18 @@ Node.prototype.needsParensNoLineTerminator = function () { return false; }; -_.each(Node.prototype, function (fn, key) { - Node[key] = function (node, parent) { - var n = new Node(node, parent); - +_.each(Node, function (fn, key) { + Node.prototype[key] = function () { // Avoid leaking arguments to prevent deoptimization - var skipCount = 2; - var args = new Array(arguments.length - skipCount); + var args = new Array(arguments.length + 2); + + args[0] = this.node; + args[1] = this.parent; + for (var i = 0; i < args.length; i++) { - args[i] = arguments[i + 2]; + args[i + 2] = arguments[i]; } - return n[key].apply(n, args); + return Node[key].apply(null, args); }; }); From 41ff1d4ace428a78a8a34b0983cd58a161fe63be Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 02:45:08 +1100 Subject: [PATCH 030/129] upgrade acorn-6to5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index baaca5241e..8f86be613b 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "test": "make test" }, "dependencies": { - "acorn-6to5": "0.11.1-14", + "acorn-6to5": "0.11.1-16", "ast-types": "~0.6.1", "chokidar": "0.11.1", "commander": "2.5.0", From 6db7fce543f1dad1b5255f70987a8a97f7e8d040 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 02:48:22 +1100 Subject: [PATCH 031/129] fix computed member expressions in playground memoization --- .../transformers/playground-memoization-operator.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/6to5/transformation/transformers/playground-memoization-operator.js b/lib/6to5/transformation/transformers/playground-memoization-operator.js index d8aad48236..1f5fa4bb9f 100644 --- a/lib/6to5/transformation/transformers/playground-memoization-operator.js +++ b/lib/6to5/transformation/transformers/playground-memoization-operator.js @@ -6,9 +6,11 @@ var isMemo = function (node) { return is; }; -var getPropRef = function (nodes, prop, file, scope) { - if (t.isIdentifier(prop)) { - return t.literal(prop.name); +var getPropRef = function (nodes, member, file, scope) { + var prop = member.property; + var key = t.toComputedKey(member, member.property); + if (t.isLiteral(key)) { + return key; } else { var temp = scope.generateUidBasedOnNode(prop, file); nodes.push(t.variableDeclaration("var", [ @@ -54,7 +56,7 @@ exports.ExpressionStatement = function (node, parent, file, scope) { var left = expr.left; var obj = getObjRef(nodes, left.object, file, scope); - var prop = getPropRef(nodes, left.property, file, scope); + var prop = getPropRef(nodes, left, file, scope); nodes.push(t.ifStatement( buildHasOwn(obj, prop, file), @@ -72,7 +74,7 @@ exports.AssignmentExpression = function (node, parent, file, scope) { var left = node.left; var obj = getObjRef(nodes, left.object, file, scope); - var prop = getPropRef(nodes, left.property, file, scope); + var prop = getPropRef(nodes, left, file, scope); nodes.push(t.logicalExpression( "&&", From 688d619bfe2f7ed93dcfe5f2e62c20d804bd5999 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 19:29:27 +1100 Subject: [PATCH 032/129] add identifier check to class id inheritance --- lib/6to5/transformation/transformers/es6-classes.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 69d3a41205..04be814120 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -14,7 +14,8 @@ exports.ClassExpression = function (node, parent, file, scope) { node.id = parent.key; } - if (t.isVariableDeclarator(parent)) { + if (t.isVariableDeclarator(parent) && t.isIdentifier(parent.id)) { + // var foo = class {}; node.id = parent.id; } } From 060aa5ba0b3bf6f03438bc2819117f0d763618c6 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 19:29:41 +1100 Subject: [PATCH 033/129] fix rest parameter check in types.isReferenced --- lib/6to5/types/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 75270807bf..a969003eaf 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -231,7 +231,7 @@ t.isReferenced = function (node, parent) { if (_.contains(parent.params, node)) return false; // we're a rest parameter - if (_.contains(parent.params, node)) return false; + if (parent.rest === node) return false; } if (t.isMethodDefinition(parent) && parent.key === node && !parent.computed) { From 55054e45da08aa53dcaf67e8537e3a38a22caaf4 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 19:34:33 +1100 Subject: [PATCH 034/129] Revert "Improve performance of rest parameter." This reverts commit e5463cc2a97cafbf9d35656038f19b45dd75ffce. --- lib/6to5/transformation/templates/rest.js | 2 +- .../transformers/es6-rest-parameters.js | 20 ++++++------------- .../arrow-functions/expected.js | 6 +++--- .../es6-rest-parameters/multiple/expected.js | 12 +++++------ .../es6-rest-parameters/single/expected.js | 12 +++++------ 5 files changed, 22 insertions(+), 30 deletions(-) diff --git a/lib/6to5/transformation/templates/rest.js b/lib/6to5/transformation/templates/rest.js index c15ce8815d..f396f01117 100644 --- a/lib/6to5/transformation/templates/rest.js +++ b/lib/6to5/transformation/templates/rest.js @@ -1,3 +1,3 @@ -for (var LEN = ARGUMENTS.length, ARRAY = Array(ARRAY_LEN), KEY = START; KEY < LEN; KEY++) { +for (var KEY = START; KEY < ARGUMENTS.length; KEY++) { ARRAY[ARRAY_KEY] = ARGUMENTS[KEY]; } diff --git a/lib/6to5/transformation/transformers/es6-rest-parameters.js b/lib/6to5/transformation/transformers/es6-rest-parameters.js index d1de130e31..266b6f62f7 100644 --- a/lib/6to5/transformation/transformers/es6-rest-parameters.js +++ b/lib/6to5/transformation/transformers/es6-rest-parameters.js @@ -14,31 +14,23 @@ exports.Function = function (node, parent, file) { var start = t.literal(node.params.length); var key = file.generateUidIdentifier("key"); - var len = file.generateUidIdentifier("len"); var arrKey = key; if (node.params.length) { - arrKey = t.binaryExpression("-", key, start); - } - - var arrLen = len; - if (node.params.length) { - arrLen = t.conditionalExpression( - t.binaryExpression(">", len, start), - t.binaryExpression("-", len, start), - t.literal(0) - ); + arrKey = t.binaryExpression("-", arrKey, start); } node.body.body.unshift( + t.variableDeclaration("var", [ + t.variableDeclarator(rest, t.arrayExpression([])) + ]), + util.template("rest", { ARGUMENTS: argsId, ARRAY_KEY: arrKey, - ARRAY_LEN: arrLen, START: start, ARRAY: rest, - KEY: key, - LEN: len, + KEY: key }) ); }; diff --git a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js index fa875fd363..cb971ebc77 100644 --- a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js @@ -1,9 +1,9 @@ "use strict"; var concat = function () { - for (var _len = arguments.length, - arrs = Array(_len), - _key = 0; _key < _len; _key++) { + var arrs = []; + + for (var _key = 0; _key < arguments.length; _key++) { arrs[_key] = arguments[_key]; } }; diff --git a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js index dc6e082ce6..56ff1c407f 100644 --- a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js @@ -1,17 +1,17 @@ "use strict"; var t = function (f) { - for (var _len = arguments.length, - items = Array(_len > 1 ? _len - 1 : 0), - _key = 1; _key < _len; _key++) { + var items = []; + + for (var _key = 1; _key < arguments.length; _key++) { items[_key - 1] = arguments[_key]; } }; function t(f) { - for (var _len2 = arguments.length, - items = Array(_len2 > 1 ? _len2 - 1 : 0), - _key2 = 1; _key2 < _len2; _key2++) { + var items = []; + + for (var _key2 = 1; _key2 < arguments.length; _key2++) { items[_key2 - 1] = arguments[_key2]; } } diff --git a/test/fixtures/transformation/es6-rest-parameters/single/expected.js b/test/fixtures/transformation/es6-rest-parameters/single/expected.js index d6f9e1a5fe..316b3ec9a4 100644 --- a/test/fixtures/transformation/es6-rest-parameters/single/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/single/expected.js @@ -1,17 +1,17 @@ "use strict"; var t = function () { - for (var _len = arguments.length, - items = Array(_len), - _key = 0; _key < _len; _key++) { + var items = []; + + for (var _key = 0; _key < arguments.length; _key++) { items[_key] = arguments[_key]; } }; function t() { - for (var _len2 = arguments.length, - items = Array(_len2), - _key2 = 0; _key2 < _len2; _key2++) { + var items = []; + + for (var _key2 = 0; _key2 < arguments.length; _key2++) { items[_key2] = arguments[_key2]; } } From 80764c214a65c1436d9f90db5b99f87710d32d6a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 19:52:53 +1100 Subject: [PATCH 035/129] add rest parameter comments --- lib/6to5/transformation/transformers/es6-rest-parameters.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/6to5/transformation/transformers/es6-rest-parameters.js b/lib/6to5/transformation/transformers/es6-rest-parameters.js index 266b6f62f7..68fe384992 100644 --- a/lib/6to5/transformation/transformers/es6-rest-parameters.js +++ b/lib/6to5/transformation/transformers/es6-rest-parameters.js @@ -10,6 +10,8 @@ exports.Function = function (node, parent, file) { t.ensureBlock(node); var argsId = t.identifier("arguments"); + + // otherwise `arguments` will be remapped in arrow functions argsId._ignoreAliasFunctions = true; var start = t.literal(node.params.length); @@ -17,6 +19,9 @@ exports.Function = function (node, parent, file) { var arrKey = key; if (node.params.length) { + // this method has additional params, so we need to subtract + // the index of the current argument position from the + // position in the array that we want to populate arrKey = t.binaryExpression("-", arrKey, start); } From 6f7da38957c389e53af7aaf480ef8e48d8c6a514 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 20:07:32 +1100 Subject: [PATCH 036/129] abstract out named method helper --- .../transformation/helpers/name-method.js | 43 ++++++++++++++++ .../transformers/es6-classes.js | 3 +- .../es6-property-method-assignment.js | 49 ++----------------- 3 files changed, 50 insertions(+), 45 deletions(-) create mode 100644 lib/6to5/transformation/helpers/name-method.js diff --git a/lib/6to5/transformation/helpers/name-method.js b/lib/6to5/transformation/helpers/name-method.js new file mode 100644 index 0000000000..00d3c77a08 --- /dev/null +++ b/lib/6to5/transformation/helpers/name-method.js @@ -0,0 +1,43 @@ +var traverse = require("../../traverse"); +var util = require("../../util"); +var t = require("../../types"); + +module.exports = function (node, file, scope) { + var key = t.toComputedKey(node, node.key); + if (!t.isLiteral(key)) return node; // we can't set a function id with this + + var id = t.toIdentifier(key.value); + key = t.identifier(id); + + var selfReference = false; + var outerDeclar = scope.get(id, true); + + traverse(node, { + enter: function (node, parent, scope) { + // check if this node is an identifier that matches the same as our function id + if (!t.isIdentifier(node, { name: id })) return; + + // check if this node is the one referenced + if (!t.isReferenced(node, parent)) return; + + // check that we don't have a local variable declared as that removes the need + // for the wrapper + var localDeclar = scope.get(id, true); + if (localDeclar !== outerDeclar) return; + + selfReference = true; + this.stop(); + } + }, scope); + + if (selfReference) { + node.value = util.template("property-method-assignment-wrapper", { + FUNCTION: node.value, + FUNCTION_ID: key, + FUNCTION_KEY: file.generateUidIdentifier(id, scope), + WRAPPER_KEY: file.generateUidIdentifier(id + "Wrapper", scope) + }); + } else { + node.value.id = key; + } +}; diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 04be814120..1259e39f31 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -1,4 +1,5 @@ var propertyMethodAssignment = require("./es6-property-method-assignment"); +var nameMethod = require("../helpers/name-method"); var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); @@ -194,7 +195,7 @@ Class.prototype.pushMethod = function (node) { var kind = node.kind; if (kind === "") { - propertyMethodAssignment._namedMethod(node, this.file, this.scope); + nameMethod(node, this.file, this.scope); if (this.isLoose) { // use assignments instead of define properties for loose classes diff --git a/lib/6to5/transformation/transformers/es6-property-method-assignment.js b/lib/6to5/transformation/transformers/es6-property-method-assignment.js index 5dbe72c55a..99399cb117 100644 --- a/lib/6to5/transformation/transformers/es6-property-method-assignment.js +++ b/lib/6to5/transformation/transformers/es6-property-method-assignment.js @@ -1,53 +1,14 @@ -var traverse = require("../../traverse"); -var util = require("../../util"); -var t = require("../../types"); - -exports._namedMethod = function (node, file, scope) { - var key = t.toComputedKey(node, node.key); - if (!t.isLiteral(key)) return node; // we can't set a function id with this - - var id = t.toIdentifier(key.value); - key = t.identifier(id); - - var selfReference = false; - var outerDeclar = scope.get(id, true); - - traverse(node, { - enter: function (node, parent, scope) { - // check if this node is an identifier that matches the same as our function id - if (!t.isIdentifier(node, { name: id })) return; - - // check if this node is the one referenced - if (!t.isReferenced(node, parent)) return; - - // check that we don't have a local variable declared as that removes the need - // for the wrapper - var localDeclar = scope.get(id, true); - if (localDeclar !== outerDeclar) return; - - selfReference = true; - this.stop(); - } - }, scope); - - if (selfReference) { - node.value = util.template("property-method-assignment-wrapper", { - FUNCTION: node.value, - FUNCTION_ID: key, - FUNCTION_KEY: file.generateUidIdentifier(id, scope), - WRAPPER_KEY: file.generateUidIdentifier(id + "Wrapper", scope) - }); - } else { - node.value.id = key; - } -}; +var nameMethod = require("../helpers/name-method"); +var traverse = require("../../traverse"); +var util = require("../../util"); +var t = require("../../types"); exports.Property = function (node, parent, file, scope) { if (!node.method) return; node.method = false; - exports._namedMethod(node, file, scope); + nameMethod(node, file, scope); }; exports.ObjectExpression = function (node) { From 50d7a46c7fa8859baf38ad039f59980d5770eeea Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 20:09:50 +1100 Subject: [PATCH 037/129] abstract away remap async to generator helper --- .../helpers/remap-async-to-generator.js | 29 +++++++++++++++ .../optional-async-to-generator.js | 5 ++- .../optional-bluebird-coroutines.js | 37 ++++--------------- 3 files changed, 39 insertions(+), 32 deletions(-) create mode 100644 lib/6to5/transformation/helpers/remap-async-to-generator.js diff --git a/lib/6to5/transformation/helpers/remap-async-to-generator.js b/lib/6to5/transformation/helpers/remap-async-to-generator.js new file mode 100644 index 0000000000..86a2c00ba7 --- /dev/null +++ b/lib/6to5/transformation/helpers/remap-async-to-generator.js @@ -0,0 +1,29 @@ +var traverse = require("../../traverse"); +var t = require("../../types"); + +module.exports = function (node, callId) { + node.async = false; + node.generator = true; + + traverse(node, { + enter: function (node) { + if (t.isFunction(node)) this.skip(); + + if (t.isAwaitExpression(node)) { + node.type = "YieldExpression"; + } + } + }); + + var call = t.callExpression(callId, [node]); + + if (t.isFunctionDeclaration(node)) { + var declar = t.variableDeclaration("var", [ + t.variableDeclarator(node.id, call) + ]); + declar._blockHoist = true; + return declar; + } else { + return call; + } +}; diff --git a/lib/6to5/transformation/transformers/optional-async-to-generator.js b/lib/6to5/transformation/transformers/optional-async-to-generator.js index 551c9f7c71..143445a9c2 100644 --- a/lib/6to5/transformation/transformers/optional-async-to-generator.js +++ b/lib/6to5/transformation/transformers/optional-async-to-generator.js @@ -1,4 +1,5 @@ -var bluebirdCoroutines = require("./optional-bluebird-coroutines"); +var remapAsyncToGenerator = require("../helpers/remap-async-to-generator"); +var bluebirdCoroutines = require("./optional-bluebird-coroutines"); exports.optional = true; @@ -7,5 +8,5 @@ exports.manipulateOptions = bluebirdCoroutines.manipulateOptions; exports.Function = function (node, parent, file) { if (!node.async || node.generator) return; - return bluebirdCoroutines._Function(node, file.addHelper("async-to-generator")); + return remapAsyncToGenerator(node, file.addHelper("async-to-generator")); }; diff --git a/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js b/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js index cda714330c..08c70ab9d0 100644 --- a/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js +++ b/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js @@ -1,5 +1,6 @@ -var traverse = require("../../traverse"); -var t = require("../../types"); +var remapAsyncToGenerator = require("../helpers/remap-async-to-generator"); +var traverse = require("../../traverse"); +var t = require("../../types"); exports.manipulateOptions = function (opts) { opts.experimental = true; @@ -8,36 +9,12 @@ exports.manipulateOptions = function (opts) { exports.optional = true; -exports._Function = function (node, callId) { - node.async = false; - node.generator = true; - - traverse(node, { - enter: function (node) { - if (t.isFunction(node)) this.skip(); - - if (t.isAwaitExpression(node)) { - node.type = "YieldExpression"; - } - } - }); - - var call = t.callExpression(callId, [node]); - - if (t.isFunctionDeclaration(node)) { - var declar = t.variableDeclaration("var", [ - t.variableDeclarator(node.id, call) - ]); - declar._blockHoist = true; - return declar; - } else { - return call; - } -}; exports.Function = function (node, parent, file) { if (!node.async || node.generator) return; - var id = file.addImport("bluebird"); - return exports._Function(node, t.memberExpression(id, t.identifier("coroutine"))); + return remapAsyncToGenerator( + node, + t.memberExpression(file.addImport("bluebird"), t.identifier("coroutine")) + ); }; From 790c924b54118955893f1625b4b3d2c53cee4068 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 20:13:16 +1100 Subject: [PATCH 038/129] abstract out build comprehension helper --- lib/6to5/transformation/helpers/use-strict.js | 19 +++++++++++++++ lib/6to5/transformation/modules/system.js | 4 ++-- .../transformers/_block-hoist.js | 4 ++-- .../transformers/_declarations.js | 4 ++-- .../transformation/transformers/use-strict.js | 23 +++---------------- 5 files changed, 28 insertions(+), 26 deletions(-) create mode 100644 lib/6to5/transformation/helpers/use-strict.js diff --git a/lib/6to5/transformation/helpers/use-strict.js b/lib/6to5/transformation/helpers/use-strict.js new file mode 100644 index 0000000000..6ef8e3baf0 --- /dev/null +++ b/lib/6to5/transformation/helpers/use-strict.js @@ -0,0 +1,19 @@ +var t = require("../../types"); + +exports.has = function (node) { + var first = node.body[0]; + return t.isExpressionStatement(first) && t.isLiteral(first.expression, { value: "use strict" }); +}; + +exports.wrap = function (node, callback) { + var useStrictNode; + if (exports.has(node)) { + useStrictNode = node.body.shift(); + } + + callback(); + + if (useStrictNode) { + node.body.unshift(useStrictNode); + } +}; diff --git a/lib/6to5/transformation/modules/system.js b/lib/6to5/transformation/modules/system.js index d8a01218e3..b70c8423a4 100644 --- a/lib/6to5/transformation/modules/system.js +++ b/lib/6to5/transformation/modules/system.js @@ -1,7 +1,7 @@ module.exports = SystemFormatter; var AMDFormatter = require("./amd"); -var useStrict = require("../transformers/use-strict"); +var useStrict = require("../helpers/use-strict"); var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); @@ -171,7 +171,7 @@ SystemFormatter.prototype.transform = function (ast) { handlerBody.push(returnStatement); - if (useStrict._has(block)) { + if (useStrict.has(block)) { handlerBody.unshift(block.body.shift()); } diff --git a/lib/6to5/transformation/transformers/_block-hoist.js b/lib/6to5/transformation/transformers/_block-hoist.js index e024a8a193..048ea908c4 100644 --- a/lib/6to5/transformation/transformers/_block-hoist.js +++ b/lib/6to5/transformation/transformers/_block-hoist.js @@ -1,4 +1,4 @@ -var useStrict = require("./use-strict"); +var useStrict = require("../helpers/use-strict"); var _ = require("lodash"); // Priority: @@ -18,7 +18,7 @@ exports.Program = { } if (!hasChange) return; - useStrict._wrap(node, function () { + useStrict.wrap(node, function () { var nodePriorities = _.groupBy(node.body, function (bodyNode) { var priority = bodyNode._blockHoist; if (priority == null) priority = 1; diff --git a/lib/6to5/transformation/transformers/_declarations.js b/lib/6to5/transformation/transformers/_declarations.js index 20fbfadb6e..e2b6c299b2 100644 --- a/lib/6to5/transformation/transformers/_declarations.js +++ b/lib/6to5/transformation/transformers/_declarations.js @@ -1,4 +1,4 @@ -var useStrict = require("./use-strict"); +var useStrict = require("../helpers/use-strict"); var t = require("../../types"); exports.secondPass = true; @@ -8,7 +8,7 @@ exports.Program = function (node) { var kinds = {}; var kind; - useStrict._wrap(node, function () { + useStrict.wrap(node, function () { for (var i in node._declarations) { var declar = node._declarations[i]; diff --git a/lib/6to5/transformation/transformers/use-strict.js b/lib/6to5/transformation/transformers/use-strict.js index bd9fd9c651..41eec48c14 100644 --- a/lib/6to5/transformation/transformers/use-strict.js +++ b/lib/6to5/transformation/transformers/use-strict.js @@ -1,26 +1,9 @@ -var t = require("../../types"); - -exports._has = function (node) { - var first = node.body[0]; - return t.isExpressionStatement(first) && t.isLiteral(first.expression, { value: "use strict" }); -}; - -exports._wrap = function (node, callback) { - var useStrictNode; - if (exports._has(node)) { - useStrictNode = node.body.shift(); - } - - callback(); - - if (useStrictNode) { - node.body.unshift(useStrictNode); - } -}; +var useStrict = require("../helpers/use-strict"); +var t = require("../../types"); exports.ast = { exit: function (ast) { - if (!exports._has(ast.program)) { + if (!useStrict.has(ast.program)) { ast.program.body.unshift(t.expressionStatement(t.literal("use strict"))); } } From 60dae7a88d80cb0daf5b998a4e69994a6db91ed1 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 20:13:24 +1100 Subject: [PATCH 039/129] add build comprehension helper --- .../helpers/build-comprehension.js | 23 ++++++++++++++ .../transformers/es7-array-comprehension.js | 31 +++---------------- .../es7-generator-comprehension.js | 4 +-- 3 files changed, 30 insertions(+), 28 deletions(-) create mode 100644 lib/6to5/transformation/helpers/build-comprehension.js diff --git a/lib/6to5/transformation/helpers/build-comprehension.js b/lib/6to5/transformation/helpers/build-comprehension.js new file mode 100644 index 0000000000..9ba979caef --- /dev/null +++ b/lib/6to5/transformation/helpers/build-comprehension.js @@ -0,0 +1,23 @@ +var t = require("../../types"); + +module.exports = function build(node, buildBody) { + var self = node.blocks.shift(); + if (!self) return; + + var child = build(node, buildBody); + if (!child) { + // last item + child = buildBody(); + + // add a filter as this is our final stop + if (node.filter) { + child = t.ifStatement(node.filter, t.blockStatement([child])); + } + } + + return t.forOfStatement( + t.variableDeclaration("let", [t.variableDeclarator(self.left)]), + self.right, + t.blockStatement([child]) + ); +}; diff --git a/lib/6to5/transformation/transformers/es7-array-comprehension.js b/lib/6to5/transformation/transformers/es7-array-comprehension.js index a988678d93..0a18a94e8a 100644 --- a/lib/6to5/transformation/transformers/es7-array-comprehension.js +++ b/lib/6to5/transformation/transformers/es7-array-comprehension.js @@ -1,6 +1,7 @@ -var traverse = require("../../traverse"); -var util = require("../../util"); -var t = require("../../types"); +var buildComprehension = require("../helpers/build-comprehension"); +var traverse = require("../../traverse"); +var util = require("../../util"); +var t = require("../../types"); exports.experimental = true; @@ -22,7 +23,7 @@ var build = function (node, parent, file, scope) { var returnStatement = body.pop(); - body.push(exports._build(node, function () { + body.push(buildComprehension(node, function () { return util.template("array-push", { STATEMENT: node.body, KEY: uid @@ -33,28 +34,6 @@ var build = function (node, parent, file, scope) { return container; }; -exports._build = function (node, buildBody) { - var self = node.blocks.shift(); - if (!self) return; - - var child = exports._build(node, buildBody); - if (!child) { - // last item - child = buildBody(); - - // add a filter as this is our final stop - if (node.filter) { - child = t.ifStatement(node.filter, t.blockStatement([child])); - } - } - - return t.forOfStatement( - t.variableDeclaration("let", [t.variableDeclarator(self.left)]), - self.right, - t.blockStatement([child]) - ); -}; - exports.ComprehensionExpression = function (node, parent, file, scope) { if (node.generator) return; diff --git a/lib/6to5/transformation/transformers/es7-generator-comprehension.js b/lib/6to5/transformation/transformers/es7-generator-comprehension.js index 9d950fecd3..84d75634a1 100644 --- a/lib/6to5/transformation/transformers/es7-generator-comprehension.js +++ b/lib/6to5/transformation/transformers/es7-generator-comprehension.js @@ -1,4 +1,4 @@ -var arrayComprehension = require("./es7-array-comprehension"); +var buildComprehension = require("../helpers/build-comprehension"); var t = require("../../types"); exports.experimental = true; @@ -10,7 +10,7 @@ exports.ComprehensionExpression = function (node) { var container = t.functionExpression(null, [], t.blockStatement(body), true); container._aliasFunction = true; - body.push(arrayComprehension._build(node, function () { + body.push(buildComprehension(node, function () { return t.expressionStatement(t.yieldExpression(node.body)); })); From db5750643a2c8df7d0306dc32a48ae169298aa1f Mon Sep 17 00:00:00 2001 From: Shinnosuke Watanabe Date: Thu, 15 Jan 2015 18:29:40 +0900 Subject: [PATCH 040/129] use output-file-sync instead of mkdirp https://github.com/shinnn/output-file-sync --- bin/6to5/dir.js | 19 ++++++++----------- package.json | 2 +- test/bin.js | 25 +++++++++++-------------- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/bin/6to5/dir.js b/bin/6to5/dir.js index 376d0bf571..61a08db308 100644 --- a/bin/6to5/dir.js +++ b/bin/6to5/dir.js @@ -1,9 +1,9 @@ -var chokidar = require("chokidar"); -var mkdirp = require("mkdirp"); -var path = require("path"); -var util = require("./util"); -var fs = require("fs"); -var _ = require("lodash"); +var chokidar = require("chokidar"); +var outputFileSync = require("output-file-sync"); +var path = require("path"); +var util = require("./util"); +var fs = require("fs"); +var _ = require("lodash"); module.exports = function (commander, filenames, opts) { if (commander.sourceMapsInline) { @@ -15,16 +15,13 @@ module.exports = function (commander, filenames, opts) { var data = util.compile(src, { sourceMapName: dest }); - var up = path.normalize(dest + "/.."); - mkdirp.sync(up); - if (commander.sourceMaps) { var mapLoc = dest + ".map"; data.code = util.addSourceMappingUrl(data.code, mapLoc); - fs.writeFileSync(mapLoc, JSON.stringify(data.map)); + outputFileSync(mapLoc, JSON.stringify(data.map)); } - fs.writeFileSync(dest, data.code); + outputFileSync(dest, data.code); console.log(src + " -> " + dest); }; diff --git a/package.json b/package.json index 8f86be613b..baba6078c5 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "fs-readdir-recursive": "0.1.0", "jshint": "2.5.10", "lodash": "2.4.1", - "mkdirp": "0.5.0", + "output-file-sync": "^1.1.0", "private": "0.1.6", "regenerator": "0.8.3", "regexpu": "0.3.0", diff --git a/test/bin.js b/test/bin.js index e8bd33b22e..91cb7c7716 100644 --- a/test/bin.js +++ b/test/bin.js @@ -1,15 +1,15 @@ if (process.env.running_under_istanbul) return; -var readdir = require("fs-readdir-recursive"); -var helper = require("./_helper"); -var assert = require("assert"); -var rimraf = require("rimraf"); -var mkdirp = require("mkdirp"); -var child = require("child_process"); -var path = require("path"); -var chai = require("chai"); -var fs = require("fs"); -var _ = require("lodash"); +var readdir = require("fs-readdir-recursive"); +var helper = require("./_helper"); +var assert = require("assert"); +var rimraf = require("rimraf"); +var outputFileSync = require("output-file-sync"); +var child = require("child_process"); +var path = require("path"); +var chai = require("chai"); +var fs = require("fs"); +var _ = require("lodash"); var fixtureLoc = __dirname + "/fixtures/bin"; var tmpLoc = __dirname + "/tmp"; @@ -27,10 +27,7 @@ var readDir = function (loc) { var saveInFiles = function (files) { _.each(files, function (content, filename) { - var up = path.normalize(filename + "/.."); - mkdirp.sync(up); - - fs.writeFileSync(filename, content); + outputFileSync(filename, content); }); }; From 1733bac3bbf6612dc9697ac028676b04d05b51a4 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 23:07:46 +1100 Subject: [PATCH 041/129] move chokidar to top --- bin/6to5/dir.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/6to5/dir.js b/bin/6to5/dir.js index 61a08db308..962379e49b 100644 --- a/bin/6to5/dir.js +++ b/bin/6to5/dir.js @@ -1,5 +1,5 @@ -var chokidar = require("chokidar"); var outputFileSync = require("output-file-sync"); +var chokidar = require("chokidar"); var path = require("path"); var util = require("./util"); var fs = require("fs"); From fcd3c9ce6549283a1443d96e0ab965717e7e5015 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 23:07:52 +1100 Subject: [PATCH 042/129] remove register blacklist tests --- lib/6to5/register.js | 46 -------------------------------------------- 1 file changed, 46 deletions(-) diff --git a/lib/6to5/register.js b/lib/6to5/register.js index f8647bea85..8b4d99d915 100644 --- a/lib/6to5/register.js +++ b/lib/6to5/register.js @@ -21,51 +21,6 @@ sourceMapSupport.install({ } }); -// - -var blacklist = []; - -var blacklistTest = function (transformer, code) { - try { - if (_.isFunction(code)) { - code(); - } else { - new Function(code); - } - blacklist.push(transformer); - } catch (err) { - if (err.name !== "SyntaxError") throw err; - } -}; - -blacklistTest("arrayComprehension", "var foo = [for (foo of bar) foo * foo];"); -blacklistTest("generatorComprehension", "var foo = (for (foo of bar) foo * foo)"); -blacklistTest("arrowFunctions", "var foo = x => x * x;"); -blacklistTest("classes", "class Foo {}"); -blacklistTest("computedPropertyNames", "var foo = { [foo]: bar };"); -blacklistTest("constants", function () { - try { - new Function("const foo = 'foo';\nfoo = 'wow';"); - } catch (err) { - return; // constants are supported - } - throw new SyntaxError; -}); -blacklistTest("defaultParamaters", "var foo = function (bar = 0) {};"); -blacklistTest("destructuring", "var { x, y } = { x: 0, y: 0 };"); -blacklistTest("forOf", "for (var foo of bar) {}"); -blacklistTest("generators", "function* foo() {}\nasync function bar() {}"); // generators/async functions delegated to same transformer -blacklistTest("letScoping", "let foo = 0;"); -blacklistTest("modules", 'import foo from "from";'); -blacklistTest("propertyMethodAssignment", "{ get foo() {} }"); -blacklistTest("propertyNameShorthand", "var foo = { x, y };"); -blacklistTest("restParameters", "function foo(...bar) {}"); -blacklistTest("spread", "foo(...bar);"); -blacklistTest("templateLiterals", "var foo = `foo`;"); -blacklistTest("unicodeRegex", function () { new RegExp("foo", "u"); }); - -// - var transformOpts = {}; var ignoreRegex = /node_modules/; var onlyRegex; @@ -95,7 +50,6 @@ var loader = function (m, filename) { result = result || to5.transformFileSync(filename, _.extend({ whitelist: whitelist, - blacklist: blacklist, sourceMap: true, ast: false }, transformOpts)); From 88eacecd727a3802782809268542b77a0e3c9009 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 23:08:06 +1100 Subject: [PATCH 043/129] clarify traceur ignore tests --- test/traceur.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/traceur.js b/test/traceur.js index e066effd17..2d2866c015 100644 --- a/test/traceur.js +++ b/test/traceur.js @@ -14,6 +14,7 @@ require("./_transformation-helper")({ loc: traceurLoc + "/test/feature", ignoreSuites: [ + // these are all internal traceur tests or non-standard features "ObjectMixin", "Annotations", "TestRunner", @@ -40,10 +41,13 @@ require("./_transformation-helper")({ "Syntax/UseStrictEscapeSequence", "Syntax/UseStrictLineContinuation", - // the spec for these doesn't define syntax (as far as i could tell) + // experimental es7 - the spec hasn't been finalised yet // these both fail because of filter between blocks "ArrayComprehension/Simple", - "GeneratorComprehension/Simple" + "GeneratorComprehension/Simple", + + // yield has been added as a keyword in ES6 so this test is actually incorrect + "Yield/YieldIdentifier" ] }, { optional: ["typeofSymbol"], From 3cf8a6df01cd29212757ed3519e2815049cb6a71 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 23:08:28 +1100 Subject: [PATCH 044/129] rename replaceInstanceSuperReferences to replaceSuperReferences in classes transformer and add breaks on method definitions --- lib/6to5/transformation/transformers/es6-classes.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 1259e39f31..1bffd19f76 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -136,7 +136,7 @@ Class.prototype.buildBody = function () { for (var i in classBody) { var node = classBody[i]; if (t.isMethodDefinition(node)) { - this.replaceInstanceSuperReferences(node); + this.replaceSuperReferences(node); if (node.key.name === "constructor") { this.pushConstructor(node); @@ -301,7 +301,7 @@ Class.prototype.looseSuperProperty = function (methodNode, id, parent) { * @param {Node} methodNode MethodDefinition */ -Class.prototype.replaceInstanceSuperReferences = function (methodNode) { +Class.prototype.replaceSuperReferences = function (methodNode) { var method = methodNode.value; var self = this; @@ -324,6 +324,11 @@ Class.prototype.replaceInstanceSuperReferences = function (methodNode) { return this.skip(); } + if (t.isProperty(node, { method: true }) || t.isMethodDefinition(node)) { + // break on object methods + return this.skip(); + } + var getThisReference = function () { if (topLevel) { // top level so `this` is the instance From 9624663e6091b96bbb5785cb6cfce85fb4391242 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Thu, 15 Jan 2015 08:41:53 -0500 Subject: [PATCH 045/129] Improve performance of rest parameter. Rather than initing an empty array and filling, create an array of the correct size up-front. Minor gain on chromium, but considerably (~5x) faster in spidermonkey/firefox. --- lib/6to5/transformation/templates/rest.js | 2 +- .../transformers/es6-rest-parameters.js | 20 +++++++++++++------ .../arrow-functions/expected.js | 6 +++--- .../es6-rest-parameters/multiple/expected.js | 12 +++++------ .../es6-rest-parameters/single/expected.js | 12 +++++------ 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/lib/6to5/transformation/templates/rest.js b/lib/6to5/transformation/templates/rest.js index f396f01117..c15ce8815d 100644 --- a/lib/6to5/transformation/templates/rest.js +++ b/lib/6to5/transformation/templates/rest.js @@ -1,3 +1,3 @@ -for (var KEY = START; KEY < ARGUMENTS.length; KEY++) { +for (var LEN = ARGUMENTS.length, ARRAY = Array(ARRAY_LEN), KEY = START; KEY < LEN; KEY++) { ARRAY[ARRAY_KEY] = ARGUMENTS[KEY]; } diff --git a/lib/6to5/transformation/transformers/es6-rest-parameters.js b/lib/6to5/transformation/transformers/es6-rest-parameters.js index 68fe384992..aab79e8103 100644 --- a/lib/6to5/transformation/transformers/es6-rest-parameters.js +++ b/lib/6to5/transformation/transformers/es6-rest-parameters.js @@ -16,26 +16,34 @@ exports.Function = function (node, parent, file) { var start = t.literal(node.params.length); var key = file.generateUidIdentifier("key"); + var len = file.generateUidIdentifier("len"); var arrKey = key; if (node.params.length) { // this method has additional params, so we need to subtract // the index of the current argument position from the // position in the array that we want to populate - arrKey = t.binaryExpression("-", arrKey, start); + arrKey = t.binaryExpression("-", key, start); + } + + var arrLen = len; + if (node.params.length) { + arrLen = t.conditionalExpression( + t.binaryExpression(">", len, start), + t.binaryExpression("-", len, start), + t.literal(0) + ); } node.body.body.unshift( - t.variableDeclaration("var", [ - t.variableDeclarator(rest, t.arrayExpression([])) - ]), - util.template("rest", { ARGUMENTS: argsId, ARRAY_KEY: arrKey, + ARRAY_LEN: arrLen, START: start, ARRAY: rest, - KEY: key + KEY: key, + LEN: len, }) ); }; diff --git a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js index cb971ebc77..fa875fd363 100644 --- a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js @@ -1,9 +1,9 @@ "use strict"; var concat = function () { - var arrs = []; - - for (var _key = 0; _key < arguments.length; _key++) { + for (var _len = arguments.length, + arrs = Array(_len), + _key = 0; _key < _len; _key++) { arrs[_key] = arguments[_key]; } }; diff --git a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js index 56ff1c407f..dc6e082ce6 100644 --- a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js @@ -1,17 +1,17 @@ "use strict"; var t = function (f) { - var items = []; - - for (var _key = 1; _key < arguments.length; _key++) { + for (var _len = arguments.length, + items = Array(_len > 1 ? _len - 1 : 0), + _key = 1; _key < _len; _key++) { items[_key - 1] = arguments[_key]; } }; function t(f) { - var items = []; - - for (var _key2 = 1; _key2 < arguments.length; _key2++) { + for (var _len2 = arguments.length, + items = Array(_len2 > 1 ? _len2 - 1 : 0), + _key2 = 1; _key2 < _len2; _key2++) { items[_key2 - 1] = arguments[_key2]; } } diff --git a/test/fixtures/transformation/es6-rest-parameters/single/expected.js b/test/fixtures/transformation/es6-rest-parameters/single/expected.js index 316b3ec9a4..d6f9e1a5fe 100644 --- a/test/fixtures/transformation/es6-rest-parameters/single/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/single/expected.js @@ -1,17 +1,17 @@ "use strict"; var t = function () { - var items = []; - - for (var _key = 0; _key < arguments.length; _key++) { + for (var _len = arguments.length, + items = Array(_len), + _key = 0; _key < _len; _key++) { items[_key] = arguments[_key]; } }; function t() { - var items = []; - - for (var _key2 = 0; _key2 < arguments.length; _key2++) { + for (var _len2 = arguments.length, + items = Array(_len2), + _key2 = 0; _key2 < _len2; _key2++) { items[_key2] = arguments[_key2]; } } From 347e490614ab6a82abf384c28631c499fefec319 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Thu, 15 Jan 2015 08:47:03 -0500 Subject: [PATCH 046/129] Rm unused dependencies (pass lint) --- lib/6to5/transformation/transformers/es6-classes.js | 9 ++++----- .../transformers/es6-property-method-assignment.js | 1 - .../transformers/optional-bluebird-coroutines.js | 1 - 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 1bffd19f76..3b5b137cfa 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -1,8 +1,7 @@ -var propertyMethodAssignment = require("./es6-property-method-assignment"); -var nameMethod = require("../helpers/name-method"); -var traverse = require("../../traverse"); -var util = require("../../util"); -var t = require("../../types"); +var nameMethod = require("../helpers/name-method"); +var traverse = require("../../traverse"); +var util = require("../../util"); +var t = require("../../types"); exports.ClassDeclaration = function (node, parent, file, scope) { return new Class(node, file, scope, true).run(); diff --git a/lib/6to5/transformation/transformers/es6-property-method-assignment.js b/lib/6to5/transformation/transformers/es6-property-method-assignment.js index 99399cb117..30a54102a5 100644 --- a/lib/6to5/transformation/transformers/es6-property-method-assignment.js +++ b/lib/6to5/transformation/transformers/es6-property-method-assignment.js @@ -1,5 +1,4 @@ var nameMethod = require("../helpers/name-method"); -var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js b/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js index 08c70ab9d0..5338fa1e1c 100644 --- a/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js +++ b/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js @@ -1,5 +1,4 @@ var remapAsyncToGenerator = require("../helpers/remap-async-to-generator"); -var traverse = require("../../traverse"); var t = require("../../types"); exports.manipulateOptions = function (opts) { From d7810cac085e04400eff13df56a175a594ee109a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 00:50:38 +1100 Subject: [PATCH 047/129] add ambiguous rest parameters comment --- .../transformation/transformers/es6-rest-parameters.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-rest-parameters.js b/lib/6to5/transformation/transformers/es6-rest-parameters.js index aab79e8103..68dfdd0ae8 100644 --- a/lib/6to5/transformation/transformers/es6-rest-parameters.js +++ b/lib/6to5/transformation/transformers/es6-rest-parameters.js @@ -19,15 +19,17 @@ exports.Function = function (node, parent, file) { var len = file.generateUidIdentifier("len"); var arrKey = key; + var arrLen = len; if (node.params.length) { // this method has additional params, so we need to subtract // the index of the current argument position from the // position in the array that we want to populate arrKey = t.binaryExpression("-", key, start); - } - var arrLen = len; - if (node.params.length) { + // we need to work out the size of the array that we're + // going to store all the rest parameters in, if there + // are less arguments than params then the array can be + // constructed with <1 which will cause an error arrLen = t.conditionalExpression( t.binaryExpression(">", len, start), t.binaryExpression("-", len, start), From 0627cd94d4d653fa04f254c4cdff8892e17a3f70 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 02:25:41 +1100 Subject: [PATCH 048/129] turn array for ins into fors --- lib/6to5/generation/generators/statements.js | 2 +- lib/6to5/transformation/modules/_default.js | 4 ++-- lib/6to5/transformation/transform.js | 2 +- .../transformation/transformers/_block-hoist.js | 2 +- .../transformation/transformers/_declarations.js | 2 ++ .../transformation/transformers/es6-classes.js | 2 +- .../transformers/es6-computed-property-names.js | 10 +++++----- .../transformation/transformers/es6-constants.js | 2 +- .../transformers/es6-default-parameters.js | 6 +++--- .../transformers/es6-destructuring.js | 14 +++++++------- .../transformation/transformers/es6-let-scoping.js | 6 +++--- .../transformation/transformers/es6-modules.js | 4 ++-- lib/6to5/transformation/transformers/es6-spread.js | 4 ++-- .../transformers/es6-template-literals.js | 6 +++--- .../transformers/es7-object-spread.js | 4 ++-- .../transformers/optional-proto-to-assign.js | 2 +- lib/6to5/transformation/transformers/react.js | 12 ++++++------ lib/6to5/types/index.js | 4 ++-- 18 files changed, 45 insertions(+), 43 deletions(-) diff --git a/lib/6to5/generation/generators/statements.js b/lib/6to5/generation/generators/statements.js index f3a6babeab..087f37fbc7 100644 --- a/lib/6to5/generation/generators/statements.js +++ b/lib/6to5/generation/generators/statements.js @@ -169,7 +169,7 @@ exports.VariableDeclaration = function (node, print, parent) { var inits = 0; var noInits = 0; - for (var i in node.declarations) { + for (var i = 0; i < node.declarations.length; i++) { if (node.declarations[i].init) { inits++; } else { diff --git a/lib/6to5/transformation/modules/_default.js b/lib/6to5/transformation/modules/_default.js index 5e8ceb12a7..42e377b3a8 100644 --- a/lib/6to5/transformation/modules/_default.js +++ b/lib/6to5/transformation/modules/_default.js @@ -244,13 +244,13 @@ DefaultFormatter.prototype.exportDeclaration = function (node, nodes) { var assign; if (t.isVariableDeclaration(declar)) { - for (var i in declar.declarations) { + for (var i = 0; i < declar.declarations.length; i++) { var decl = declar.declarations[i]; decl.init = this._exportsAssign(decl.id, decl.init, node).expression; var newDeclar = t.variableDeclaration(declar.kind, [decl]); - if (i === "0") t.inherits(newDeclar, declar); + if (i === 0) t.inherits(newDeclar, declar); nodes.push(newDeclar); } } else { diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index a3399e7b97..351fe6e7d8 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -20,7 +20,7 @@ transform.fromAst = function (ast, code, opts) { }; transform._ensureTransformerNames = function (type, keys) { - for (var i in keys) { + for (var i = 0; i < keys.length; i++) { var key = keys[i]; if (!_.has(transform.transformers, key)) { throw new ReferenceError("unknown transformer " + key + " specified in " + type); diff --git a/lib/6to5/transformation/transformers/_block-hoist.js b/lib/6to5/transformation/transformers/_block-hoist.js index 048ea908c4..cc17209797 100644 --- a/lib/6to5/transformation/transformers/_block-hoist.js +++ b/lib/6to5/transformation/transformers/_block-hoist.js @@ -12,7 +12,7 @@ exports.BlockStatement = exports.Program = { exit: function (node) { var hasChange = false; - for (var i in node.body) { + for (var i = 0; i < node.body.length; i++) { var bodyNode = node.body[i]; if (bodyNode && bodyNode._blockHoist != null) hasChange = true; } diff --git a/lib/6to5/transformation/transformers/_declarations.js b/lib/6to5/transformation/transformers/_declarations.js index e2b6c299b2..61852930b6 100644 --- a/lib/6to5/transformation/transformers/_declarations.js +++ b/lib/6to5/transformation/transformers/_declarations.js @@ -5,6 +5,8 @@ exports.secondPass = true; exports.BlockStatement = exports.Program = function (node) { + if (!node._declarations) return; + var kinds = {}; var kind; diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 3b5b137cfa..dc079ca164 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -132,7 +132,7 @@ Class.prototype.buildBody = function () { var classBody = this.node.body.body; var body = this.body; - for (var i in classBody) { + for (var i = 0; i < classBody.length; i++) { var node = classBody[i]; if (t.isMethodDefinition(node)) { this.replaceSuperReferences(node); diff --git a/lib/6to5/transformation/transformers/es6-computed-property-names.js b/lib/6to5/transformation/transformers/es6-computed-property-names.js index 94d4bd9e86..dcfff3b827 100644 --- a/lib/6to5/transformation/transformers/es6-computed-property-names.js +++ b/lib/6to5/transformation/transformers/es6-computed-property-names.js @@ -3,7 +3,7 @@ var t = require("../../types"); exports.ObjectExpression = function (node, parent, file, scope) { var hasComputed = false; - for (var i in node.properties) { + for (var i = 0; i < node.properties.length; i++) { hasComputed = t.isProperty(node.properties[i], { computed: true, kind: "init" }); if (hasComputed) break; } @@ -39,7 +39,7 @@ exports.ObjectExpression = function (node, parent, file, scope) { }; var loose = function (node, body, objId) { - for (var i in node.properties) { + for (var i = 0; i < node.properties.length; i++) { var prop = node.properties[i]; body.push(t.expressionStatement( @@ -58,7 +58,7 @@ var spec = function (node, body, objId, initProps, file) { // normalise key - for (var i in props) { + for (var i = 0; i < props.length; i++) { prop = props[i]; if (prop.kind !== "init") continue; @@ -73,7 +73,7 @@ var spec = function (node, body, objId, initProps, file) { var broken = false; - for (i in props) { + for (i = 0; i < props.length; i++) { prop = props[i]; if (prop.computed) { @@ -89,7 +89,7 @@ var spec = function (node, body, objId, initProps, file) { // add a simple assignment for all Symbol member expressions due to symbol polyfill limitations // otherwise use Object.defineProperty - for (i in props) { + for (i = 0; i < props.length; i++) { prop = props[i]; if (!prop) continue; diff --git a/lib/6to5/transformation/transformers/es6-constants.js b/lib/6to5/transformation/transformers/es6-constants.js index a3021188fb..dc8e0ce49e 100644 --- a/lib/6to5/transformation/transformers/es6-constants.js +++ b/lib/6to5/transformation/transformers/es6-constants.js @@ -44,7 +44,7 @@ exports.ForStatement = function (node, parent, file) { } if (t.isVariableDeclaration(child, { kind: "const" })) { - for (var i in child.declarations) { + for (var i = 0; i < child.declarations.length; i++) { var declar = child.declarations[i]; var ids = getIds(declar); diff --git a/lib/6to5/transformation/transformers/es6-default-parameters.js b/lib/6to5/transformation/transformers/es6-default-parameters.js index 674c1096dc..ac7a41ff38 100644 --- a/lib/6to5/transformation/transformers/es6-default-parameters.js +++ b/lib/6to5/transformation/transformers/es6-default-parameters.js @@ -31,7 +31,7 @@ exports.Function = function (node, parent, file, scope) { traverse(def, { enter: check }); }; - for (i in node.defaults) { + for (i = 0; i < node.defaults.length; i++) { def = node.defaults[i]; if (!def) continue; @@ -40,7 +40,7 @@ exports.Function = function (node, parent, file, scope) { // temporal dead zone check - here we prevent accessing of params that // are to the right - ie. uninitialized parameters var rightIds = ids.slice(i); - for (i in rightIds) { + for (i = 0; i < rightIds.length; i++) { checkTDZ(rightIds[i]); } @@ -58,7 +58,7 @@ exports.Function = function (node, parent, file, scope) { var lastNonDefaultParam = 0; - for (i in node.defaults) { + for (i = 0; i < node.defaults.length; i++) { def = node.defaults[i]; if (!def) { lastNonDefaultParam = +i + 1; diff --git a/lib/6to5/transformation/transformers/es6-destructuring.js b/lib/6to5/transformation/transformers/es6-destructuring.js index e92e3194a5..22d016b38f 100644 --- a/lib/6to5/transformation/transformers/es6-destructuring.js +++ b/lib/6to5/transformation/transformers/es6-destructuring.js @@ -46,12 +46,12 @@ var pushAssignmentPattern = function (opts, nodes, pattern, parentId) { }; var pushObjectPattern = function (opts, nodes, pattern, parentId) { - for (var i in pattern.properties) { + for (var i = 0; i < pattern.properties.length; i++) { var prop = pattern.properties[i]; if (t.isSpreadProperty(prop)) { // get all the keys that appear in this object before the current spread var keys = []; - for (var i2 in pattern.properties) { + for (var i2 = 0; i2 < pattern.properties.length; i2++) { var prop2 = pattern.properties[i2]; if (i2 >= i) break; @@ -88,7 +88,7 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) { var i; var hasSpreadElement = false; - for (i in pattern.elements) { + for (i = 0; i < pattern.elements.length; i++) { if (t.isSpreadElement(pattern.elements[i])) { hasSpreadElement = true; break; @@ -103,7 +103,7 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) { ])); parentId = _parentId; - for (i in pattern.elements) { + for (i = 0; i < pattern.elements.length; i++) { var elem = pattern.elements[i]; if (!elem) continue; @@ -278,7 +278,7 @@ exports.VariableDeclaration = function (node, parent, file, scope) { var declar; var hasPattern = false; - for (i in node.declarations) { + for (i = 0; i < node.declarations.length; i++) { declar = node.declarations[i]; if (t.isPattern(declar.id)) { hasPattern = true; @@ -287,7 +287,7 @@ exports.VariableDeclaration = function (node, parent, file, scope) { } if (!hasPattern) return; - for (i in node.declarations) { + for (i = 0; i < node.declarations.length; i++) { declar = node.declarations[i]; var patternId = declar.init; @@ -317,7 +317,7 @@ exports.VariableDeclaration = function (node, parent, file, scope) { if (!t.isProgram(parent) && !t.isBlockStatement(parent)) { declar = null; - for (i in nodes) { + for (i = 0; i < nodes.length; i++) { node = nodes[i]; declar = declar || t.variableDeclaration(node.kind, []); diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index a95c1b3a30..40aa3b3a73 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -25,7 +25,7 @@ var isVar = function (node, parent) { }; var standardiseLets = function (declars) { - for (var i in declars) { + for (var i = 0; i < declars.length; i++) { delete declars[i]._let; } }; @@ -246,7 +246,7 @@ LetScoping.prototype.getInfo = function () { opts.keys = opts.keys.concat(keys); } - for (i in block.body) { + for (i = 0; i < block.body.length; i++) { declar = block.body[i]; if (!isLet(declar, block)) continue; @@ -412,7 +412,7 @@ LetScoping.prototype.pushDeclar = function (node) { var replace = []; - for (var i in node.declarations) { + for (var i = 0; i < node.declarations.length; i++) { var declar = node.declarations[i]; if (!declar.init) continue; diff --git a/lib/6to5/transformation/transformers/es6-modules.js b/lib/6to5/transformation/transformers/es6-modules.js index b8c1b4964c..9d9631095b 100644 --- a/lib/6to5/transformation/transformers/es6-modules.js +++ b/lib/6to5/transformation/transformers/es6-modules.js @@ -10,7 +10,7 @@ exports.ImportDeclaration = function (node, parent, file) { var nodes = []; if (node.specifiers.length) { - for (var i in node.specifiers) { + for (var i = 0; i < node.specifiers.length; i++) { file.moduleFormatter.importSpecifier(node.specifiers[i], node, nodes, parent); } } else { @@ -39,7 +39,7 @@ exports.ExportDeclaration = function (node, parent, file) { file.moduleFormatter.exportDeclaration(node, nodes, parent); } else { - for (var i in node.specifiers) { + for (var i = 0; i < node.specifiers.length; i++) { file.moduleFormatter.exportSpecifier(node.specifiers[i], node, nodes, parent); } } diff --git a/lib/6to5/transformation/transformers/es6-spread.js b/lib/6to5/transformation/transformers/es6-spread.js index 5a7b031ce1..8547bfa550 100644 --- a/lib/6to5/transformation/transformers/es6-spread.js +++ b/lib/6to5/transformation/transformers/es6-spread.js @@ -6,7 +6,7 @@ var getSpreadLiteral = function (spread, file) { }; var hasSpread = function (nodes) { - for (var i in nodes) { + for (var i = 0; i < nodes.length; i++) { if (t.isSpreadElement(nodes[i])) { return true; } @@ -25,7 +25,7 @@ var build = function (props, file) { _props = []; }; - for (var i in props) { + for (var i = 0; i < props.length; i++) { var prop = props[i]; if (t.isSpreadElement(prop)) { push(); diff --git a/lib/6to5/transformation/transformers/es6-template-literals.js b/lib/6to5/transformation/transformers/es6-template-literals.js index 5e4f824cf3..8ad54914f7 100644 --- a/lib/6to5/transformation/transformers/es6-template-literals.js +++ b/lib/6to5/transformation/transformers/es6-template-literals.js @@ -11,7 +11,7 @@ exports.TaggedTemplateExpression = function (node, parent, file) { var strings = []; var raw = []; - for (var i in quasi.quasis) { + for (var i = 0; i < quasi.quasis.length; i++) { var elem = quasi.quasis[i]; strings.push(t.literal(elem.value.cooked)); raw.push(t.literal(elem.value.raw)); @@ -33,7 +33,7 @@ exports.TemplateLiteral = function (node) { var nodes = []; var i; - for (i in node.quasis) { + for (i = 0; i < node.quasis.length; i++) { var elem = node.quasis[i]; nodes.push(t.literal(elem.value.cooked)); @@ -49,7 +49,7 @@ exports.TemplateLiteral = function (node) { var root = buildBinaryExpression(nodes.shift(), nodes.shift()); - for (i in nodes) { + for (i = 0; i < nodes.length; i++) { root = buildBinaryExpression(root, nodes[i]); } diff --git a/lib/6to5/transformation/transformers/es7-object-spread.js b/lib/6to5/transformation/transformers/es7-object-spread.js index 81156bfb1f..a9ffecaf03 100644 --- a/lib/6to5/transformation/transformers/es7-object-spread.js +++ b/lib/6to5/transformation/transformers/es7-object-spread.js @@ -8,7 +8,7 @@ exports.ObjectExpression = function (node, parent, file) { var hasSpread = false; var i; var prop; - for (i in node.properties) { + for (i = 0; i < node.properties.length; i++) { prop = node.properties[i]; if (t.isSpreadProperty(prop)) { hasSpread = true; @@ -26,7 +26,7 @@ exports.ObjectExpression = function (node, parent, file) { props = []; }; - for (i in node.properties) { + for (i = 0; i < node.properties.length; i++) { prop = node.properties[i]; if (t.isSpreadProperty(prop)) { push(); diff --git a/lib/6to5/transformation/transformers/optional-proto-to-assign.js b/lib/6to5/transformation/transformers/optional-proto-to-assign.js index b2682d1802..e3f0a78022 100644 --- a/lib/6to5/transformation/transformers/optional-proto-to-assign.js +++ b/lib/6to5/transformation/transformers/optional-proto-to-assign.js @@ -44,7 +44,7 @@ exports.ExpressionStatement = function (node, parent, file) { exports.ObjectExpression = function (node, parent, file) { var proto; - for (var i in node.properties) { + for (var i = 0; i < node.properties.length; i++) { var prop = node.properties[i]; if (isProtoKey(prop)) { diff --git a/lib/6to5/transformation/transformers/react.js b/lib/6to5/transformation/transformers/react.js index 341d6b1492..37fafeb457 100644 --- a/lib/6to5/transformation/transformers/react.js +++ b/lib/6to5/transformation/transformers/react.js @@ -136,17 +136,17 @@ exports.XJSElement = { var callExpr = node.openingElement; var i; - for (i in node.children) { + for (i = 0; i < node.children.length; i++) { var child = node.children[i]; if (t.isLiteral(child) && _.isString(child.value)) { var lines = child.value.split(/\r\n|\n|\r/); - for (i in lines) { - var line = lines[i]; + for (i2 = 0; i2 < lines.length; i2++) { + var line = lines[i2]; - var isFirstLine = i === "0"; - var isLastLine = +i === lines.length - 1; + var isFirstLine = i2 === 0; + var isLastLine = i2 === lines.length - 1; // replace rendered whitespace tabs with spaces var trimmedLine = line.replace(/\t/g, " "); @@ -209,7 +209,7 @@ var addDisplayName = function (id, call) { var props = first.properties; var safe = true; - for (var i in props) { + for (var i = 0; i < props.length; i++) { prop = props[i]; if (t.isIdentifier(prop.key, { name: "displayName" })) { safe = false; diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index a969003eaf..2891d2df82 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -440,7 +440,7 @@ t.getIds = function (node, map, ignoreTypes) { if (t.isIdentifier(id)) { ids[id.name] = id; } else if (nodeKeys) { - for (i in nodeKeys) { + for (i = 0; i < nodeKeys.length; i++) { key = nodeKeys[i]; if (id[key]) { search.push(id[key]); @@ -448,7 +448,7 @@ t.getIds = function (node, map, ignoreTypes) { } } } else if (arrKeys) { - for (i in arrKeys) { + for (i = 0; i < arrKeys.length; i++) { key = arrKeys[i]; search = search.concat(id[key] || []); } From 75e9097e1996ce8262740330ff43a8a6db46f919 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 02:29:02 +1100 Subject: [PATCH 049/129] add 2.12.4 changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6043c395b..97457f9f8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,14 @@ _Note: Gaps between patch versions are faulty/broken releases._ +## 2.12.4 + + * **Polish** + * Rest parameters now allocate the array before populating. + * **Internal** + * `for...in` loops have been changed to optimised `for` loops - better performance and no enumeration of protoype keys. + * Parts of the code generator have now been optimised thanks to [gaearon](https://github.com/gaearon). + ## 2.12.3 * **Spec Compliancy** From 4668e1d67b14dc05cb9134e882698c73faed9f0c Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 02:29:35 +1100 Subject: [PATCH 050/129] fix linting errors --- lib/6to5/transformation/transformers/react.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/6to5/transformation/transformers/react.js b/lib/6to5/transformation/transformers/react.js index 37fafeb457..41b1ae4e14 100644 --- a/lib/6to5/transformation/transformers/react.js +++ b/lib/6to5/transformation/transformers/react.js @@ -134,15 +134,14 @@ exports.XJSOpeningElement = { exports.XJSElement = { exit: function (node) { var callExpr = node.openingElement; - var i; - for (i = 0; i < node.children.length; i++) { + for (var i = 0; i < node.children.length; i++) { var child = node.children[i]; if (t.isLiteral(child) && _.isString(child.value)) { var lines = child.value.split(/\r\n|\n|\r/); - for (i2 = 0; i2 < lines.length; i2++) { + for (var i2 = 0; i2 < lines.length; i2++) { var line = lines[i2]; var isFirstLine = i2 === 0; From 51c6a3fffcdc0fd218de9d5401c9a09f8f4aaa06 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 02:31:36 +1100 Subject: [PATCH 051/129] fix let scoping body not existing --- .../transformers/es6-let-scoping.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index 40aa3b3a73..9c7d0aeb16 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -231,10 +231,9 @@ LetScoping.prototype.getInfo = function () { } }; - var i; var declar; - for (i in opts.declarators) { + for (var i in opts.declarators) { declar = opts.declarators[i]; opts.declarators.push(declar); @@ -246,14 +245,16 @@ LetScoping.prototype.getInfo = function () { opts.keys = opts.keys.concat(keys); } - for (i = 0; i < block.body.length; i++) { - declar = block.body[i]; - if (!isLet(declar, block)) continue; + if (block.body) { + for (i = 0; i < block.body.length; i++) { + declar = block.body[i]; + if (!isLet(declar, block)) continue; - var declars = t.getIds(declar, true); - for (var key in declars) { - duplicates(declars[key], key); - opts.keys.push(key); + var declars = t.getIds(declar, true); + for (var key in declars) { + duplicates(declars[key], key); + opts.keys.push(key); + } } } From a01802300f05ff8caa2670809be6b00760de905c Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 02:36:56 +1100 Subject: [PATCH 052/129] fix default parameters transformer for loops --- .../transformers/es6-default-parameters.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-default-parameters.js b/lib/6to5/transformation/transformers/es6-default-parameters.js index ac7a41ff38..b369678daa 100644 --- a/lib/6to5/transformation/transformers/es6-default-parameters.js +++ b/lib/6to5/transformation/transformers/es6-default-parameters.js @@ -11,8 +11,6 @@ exports.Function = function (node, parent, file, scope) { }); var iife = false; - var i; - var def; var checkTDZ = function (ids) { var check = function (node, parent) { @@ -31,8 +29,8 @@ exports.Function = function (node, parent, file, scope) { traverse(def, { enter: check }); }; - for (i = 0; i < node.defaults.length; i++) { - def = node.defaults[i]; + for (var i = 0; i < node.defaults.length; i++) { + var def = node.defaults[i]; if (!def) continue; var param = node.params[i]; @@ -40,8 +38,8 @@ exports.Function = function (node, parent, file, scope) { // temporal dead zone check - here we prevent accessing of params that // are to the right - ie. uninitialized parameters var rightIds = ids.slice(i); - for (i = 0; i < rightIds.length; i++) { - checkTDZ(rightIds[i]); + for (var i2 = 0; i2 < rightIds.length; i2++) { + checkTDZ(rightIds[i2]); } // we're accessing a variable that's already defined within this function From 228b3dbc34c85e73c70e5518e058435604a7f108 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 02:38:45 +1100 Subject: [PATCH 053/129] fix linting errors --- lib/6to5/transformation/transformers/es6-default-parameters.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/6to5/transformation/transformers/es6-default-parameters.js b/lib/6to5/transformation/transformers/es6-default-parameters.js index b369678daa..0d99b6db8e 100644 --- a/lib/6to5/transformation/transformers/es6-default-parameters.js +++ b/lib/6to5/transformation/transformers/es6-default-parameters.js @@ -11,6 +11,7 @@ exports.Function = function (node, parent, file, scope) { }); var iife = false; + var def; var checkTDZ = function (ids) { var check = function (node, parent) { @@ -30,7 +31,7 @@ exports.Function = function (node, parent, file, scope) { }; for (var i = 0; i < node.defaults.length; i++) { - var def = node.defaults[i]; + def = node.defaults[i]; if (!def) continue; var param = node.params[i]; From b9ad4c277340a0a283530eab10dbffbe68cc3c73 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 02:41:23 +1100 Subject: [PATCH 054/129] v2.12.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index baba6078c5..f6273351b2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "6to5", "description": "Turn ES6 code into readable vanilla ES5 with source maps", - "version": "2.12.3", + "version": "2.12.4", "author": "Sebastian McKenzie ", "homepage": "https://6to5.org/", "repository": "6to5/6to5", From 51f6cfddca8dda996c124f4021199f42de3e86dc Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 02:47:20 +1100 Subject: [PATCH 055/129] remove instanbul inclusion --- test/test262.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test262.js b/test/test262.js index 12e31a8927..b8b8f8cb3d 100644 --- a/test/test262.js +++ b/test/test262.js @@ -1,4 +1,4 @@ -if (process.env.SIMPLE_6TO5_TESTS || process.env.running_under_istanbul) return; +if (process.env.SIMPLE_6TO5_TESTS) return; var transform = require("../lib/6to5/transformation/transform"); var readdir = require("fs-readdir-recursive"); From 87da9fcfc5260f8d82d6c2b5e235b61bfa1da00e Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 07:58:14 +1100 Subject: [PATCH 056/129] fix let scoping for loop - closes #509 --- lib/6to5/transformation/transformers/es6-let-scoping.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index 9c7d0aeb16..b0e7a4d97b 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -233,7 +233,7 @@ LetScoping.prototype.getInfo = function () { var declar; - for (var i in opts.declarators) { + for (var i = 0; i < opts.declarators.length; i++) { declar = opts.declarators[i]; opts.declarators.push(declar); From 2f8bdd7e27c477348db1bf2110e75563aa2fb0c0 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 07:59:40 +1100 Subject: [PATCH 057/129] add 2.12.5 changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97457f9f8e..8d8d07f9d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ _Note: Gaps between patch versions are faulty/broken releases._ +## 2.12.5 + + * **Internal** + * Fix incorrect `for...in` loop still causing `ember-script` issues. + ## 2.12.4 * **Polish** From 14ae438735f7f83b84cbbf4d2aef98e6066f18ef Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 08:06:38 +1100 Subject: [PATCH 058/129] remove declarators push in let scoping --- lib/6to5/transformation/transformers/es6-let-scoping.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index b0e7a4d97b..bdce7be036 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -235,7 +235,6 @@ LetScoping.prototype.getInfo = function () { for (var i = 0; i < opts.declarators.length; i++) { declar = opts.declarators[i]; - opts.declarators.push(declar); var keys = t.getIds(declar, true); _.each(keys, duplicates); From 750ec7783ff2b5a06d326ca8282c9679f892a76a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 08:08:52 +1100 Subject: [PATCH 059/129] v2.12.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f6273351b2..23f7b07695 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "6to5", "description": "Turn ES6 code into readable vanilla ES5 with source maps", - "version": "2.12.4", + "version": "2.12.5", "author": "Sebastian McKenzie ", "homepage": "https://6to5.org/", "repository": "6to5/6to5", From 20e97f2d9bdfdc70e9f6359d76d30f3b1256c02d Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 22:46:09 +1100 Subject: [PATCH 060/129] add flow type visitor keys - none currently as we don't need to traverse over any of them - closes #513 --- lib/6to5/types/visitor-keys.json | 38 +++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/6to5/types/visitor-keys.json b/lib/6to5/types/visitor-keys.json index aac62dd391..125e91c922 100644 --- a/lib/6to5/types/visitor-keys.json +++ b/lib/6to5/types/visitor-keys.json @@ -15,7 +15,6 @@ "ClassBody": ["body"], "ClassDeclaration": ["id", "body", "superClass"], "ClassExpression": ["id", "body", "superClass"], - "ClassProperty": ["key"], "ComprehensionBlock": ["left", "right", "body"], "ComprehensionExpression": ["filter", "blocks", "body"], "ConditionalExpression": ["test", "consequent", "alternate"], @@ -68,6 +67,40 @@ "VirtualPropertyExpression": ["object", "property"], "WhileStatement": ["test", "body"], "WithStatement": ["object", "body"], + "YieldExpression": ["argument"], + + "AnyTypeAnnotation": [], + "ArrayTypeAnnotation": [], + "BooleanTypeAnnotation": [], + "ClassProperty": ["key"], + "DeclareClass": [], + "DeclareFunction": [], + "DeclareModule": [], + "DeclareVariable": [], + "FunctionTypeAnnotation": [], + "FunctionTypeParam": [], + "GenericTypeAnnotation": [], + "InterfaceExtends": [], + "InterfaceDeclaration": [], + "IntersectionTypeAnnotation": [], + "NullableTypeAnnotation": [], + "NumberTypeAnnotation": [], + "StringLiteralTypeAnnotation": [], + "StringTypeAnnotation": [], + "TupleTypeAnnotation": [], + "TypeofTypeAnnotation": [], + "TypeAlias": [], + "TypeAnnotation": [], + "TypeParameterDeclaration": [], + "TypeParameterInstantiation": [], + "ObjectTypeAnnotation": [], + "ObjectTypeCallProperty": [], + "ObjectTypeIndexer": [], + "ObjectTypeProperty": [], + "QualifiedTypeIdentifier": [], + "UnionTypeAnnotation": [], + "VoidTypeAnnotation": [], + "XJSAttribute": ["name", "value"], "XJSClosingElement": ["name"], "XJSElement": ["openingElement", "closingElement", "children"], @@ -77,6 +110,5 @@ "XJSMemberExpression": ["object", "property"], "XJSNamespacedName": ["namespace", "name"], "XJSOpeningElement": ["name", "attributes"], - "XJSSpreadAttribute": ["argument"], - "YieldExpression": ["argument"] + "XJSSpreadAttribute": ["argument"] } From d68f1e99105a8c532097dfc7e1f434753f8c083e Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 22:47:55 +1100 Subject: [PATCH 061/129] add flow type generation skeleton --- lib/6to5/generation/generators/flow.js | 34 ++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/6to5/generation/generators/flow.js b/lib/6to5/generation/generators/flow.js index f96e45bde0..0e5c3c0cc2 100644 --- a/lib/6to5/generation/generators/flow.js +++ b/lib/6to5/generation/generators/flow.js @@ -1,3 +1,33 @@ -exports.ClassProperty = function () { - throw new Error("not implemented"); +exports.AnyTypeAnnotation = +exports.ArrayTypeAnnotation = +exports.BooleanTypeAnnotation = +exports.ClassProperty = +exports.DeclareClass = +exports.DeclareFunction = +exports.DeclareModule = +exports.DeclareVariable = +exports.FunctionTypeAnnotation = +exports.FunctionTypeParam = +exports.GenericTypeAnnotation = +exports.InterfaceExtends = +exports.InterfaceDeclaration = +exports.IntersectionTypeAnnotation = +exports.NullableTypeAnnotation = +exports.NumberTypeAnnotation = +exports.StringLiteralTypeAnnotation = +exports.StringTypeAnnotation = +exports.TupleTypeAnnotation = +exports.TypeofTypeAnnotation = +exports.TypeAlias = +exports.TypeAnnotation = +exports.TypeParameterDeclaration = +exports.TypeParameterInstantiation = +exports.ObjectTypeAnnotation = +exports.ObjectTypeCallProperty = +exports.ObjectTypeIndexer = +exports.ObjectTypeProperty = +exports.QualifiedTypeIdentifier = +exports.UnionTypeAnnotation = +exports.VoidTypeAnnotation = function () { + // todo: implement these once we have a `--keep-types` option }; From e78859fae09e32cda81d02271387b430bd7d8915 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 22:49:31 +1100 Subject: [PATCH 062/129] add 2.12.6 changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d8d07f9d5..af7f76644c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ _Note: Gaps between patch versions are faulty/broken releases._ +## 2.12.6 + + * **Bug Fix** + * Add missing flow type traversal keys. + ## 2.12.5 * **Internal** From 76b89452070f9d589ed37a2de3f3258d8f461b68 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 16 Jan 2015 22:52:12 +1100 Subject: [PATCH 063/129] v2.12.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 23f7b07695..63d222e27d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "6to5", "description": "Turn ES6 code into readable vanilla ES5 with source maps", - "version": "2.12.5", + "version": "2.12.6", "author": "Sebastian McKenzie ", "homepage": "https://6to5.org/", "repository": "6to5/6to5", From 938026abeb148521f0fe3308b9f169a802f34b69 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 16 Jan 2015 19:48:38 +0300 Subject: [PATCH 064/129] Use toFastProperties to speed up t.* method access --- lib/6to5/to-fast-properties.js | 14 ++++++++++++++ lib/6to5/types/index.js | 7 +++++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 lib/6to5/to-fast-properties.js diff --git a/lib/6to5/to-fast-properties.js b/lib/6to5/to-fast-properties.js new file mode 100644 index 0000000000..341a451d03 --- /dev/null +++ b/lib/6to5/to-fast-properties.js @@ -0,0 +1,14 @@ +/** + * A trick from Bluebird to force V8 to use fast properties for an object. + * Read more: http://stackoverflow.com/questions/24987896/ + * + * Use %HasFastProperties(obj) and --allow-natives-syntax to check whether + * a particular object already has fast properties. + */ +module.exports = function toFastProperties(obj) { + /*jshint -W027*/ + function f() {} + f.prototype = obj; + return f; + eval(obj); +}; diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 2891d2df82..7984bcfba8 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -1,5 +1,6 @@ -var esutils = require("esutils"); -var _ = require("lodash"); +var esutils = require("esutils"); +var _ = require("lodash"); +var toFastProperties = require("../to-fast-properties"); var t = exports; @@ -573,3 +574,5 @@ t.getSpecifierName = function (specifier) { t.isSpecifierDefault = function (specifier) { return t.isIdentifier(specifier.id) && specifier.id.name === "default"; }; + +toFastProperties(t); \ No newline at end of file From 4f01f67dd616ddb53a77fbdb414b58eddf98c086 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 16 Jan 2015 19:59:04 +0300 Subject: [PATCH 065/129] Avoid delete in favor of null assignment to prevent deoptimizations --- .../transformers/es6-rest-parameters.js | 2 +- lib/6to5/traverse/index.js | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-rest-parameters.js b/lib/6to5/transformation/transformers/es6-rest-parameters.js index 68dfdd0ae8..5bfcf88601 100644 --- a/lib/6to5/transformation/transformers/es6-rest-parameters.js +++ b/lib/6to5/transformation/transformers/es6-rest-parameters.js @@ -5,7 +5,7 @@ exports.Function = function (node, parent, file) { if (!node.rest) return; var rest = node.rest; - delete node.rest; + node.rest = null; t.ensureBlock(node); diff --git a/lib/6to5/traverse/index.js b/lib/6to5/traverse/index.js index 5667ce1f46..b27d840420 100644 --- a/lib/6to5/traverse/index.js +++ b/lib/6to5/traverse/index.js @@ -102,7 +102,6 @@ function traverse(parent, opts, scope) { return; } - // unknown node type to traverse var keys = t.VISITOR_KEYS[parent.type]; if (!keys) return; @@ -139,15 +138,15 @@ function traverse(parent, opts, scope) { traverse.removeProperties = function (tree) { var clear = function (node) { - delete node._declarations; - delete node.extendedRange; - delete node._scopeInfo; - delete node.tokens; - delete node.range; - delete node.start; - delete node.end; - delete node.loc; - delete node.raw; + node._declarations = null; + node.extendedRange = null; + node._scopeInfo = null; + node.tokens = null; + node.range = null; + node.start = null; + node.end = null; + node.loc = null; + node.raw = null; clearComments(node.trailingComments); clearComments(node.leadingComments); From 8dc499654742f2b181f469795e6c0f234182638a Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 16 Jan 2015 22:11:41 +0300 Subject: [PATCH 066/129] Refactor traversal into smaller methods --- lib/6to5/traverse/index.js | 131 ++++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 53 deletions(-) diff --git a/lib/6to5/traverse/index.js b/lib/6to5/traverse/index.js index b27d840420..625905944d 100644 --- a/lib/6to5/traverse/index.js +++ b/lib/6to5/traverse/index.js @@ -4,11 +4,11 @@ var Scope = require("./scope"); var t = require("../types"); var _ = require("lodash"); -function TraversalContext(previousContext) { +function TraversalContext() { this.didSkip = false; this.didRemove = false; this.didStop = false; - this.didFlatten = previousContext ? previousContext.didFlatten : false; + this.didFlatten = false; } TraversalContext.prototype.flatten = function () { @@ -17,7 +17,7 @@ TraversalContext.prototype.flatten = function () { TraversalContext.prototype.remove = function () { this.didRemove = true; - this.skip(); + this.didSkip = true; }; TraversalContext.prototype.skip = function () { @@ -26,13 +26,16 @@ TraversalContext.prototype.skip = function () { TraversalContext.prototype.stop = function () { this.didStop = true; - this.skip(); + this.didSkip = true; +}; + +TraversalContext.prototype.reset = function () { + this.didSkip = false; + this.didStop = false; + this.didRemove = false; }; TraversalContext.prototype.maybeReplace = function (result, obj, key, node) { - if (result === false) return node; - if (result == null) return node; - var isArray = Array.isArray(result); // inherit comments from original node to the first replacement node @@ -52,87 +55,109 @@ TraversalContext.prototype.maybeReplace = function (result, obj, key, node) { if (isArray) { this.flatten(); } - - return node; }; -TraversalContext.prototype.visit = function (obj, key, opts, scope, parent) { +TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent) { + this.reset(); + var node = obj[key]; - if (!node) return; // type is blacklisted - if (opts.blacklist && opts.blacklist.indexOf(node.type) > -1) return; + if (opts.blacklist && opts.blacklist.indexOf(node.type) > -1) + return; + + var ourScope = scope; + if (t.isScope(node)) + ourScope = new Scope(node, scope); var result; - var ourScope = scope; - if (t.isScope(node)) ourScope = new Scope(node, scope); // enter if (opts.enter) { result = opts.enter.call(this, node, parent, ourScope); - node = this.maybeReplace(result, obj, key, node); + + if (result) { + this.maybeReplace(result, obj, key, node); + node = obj[key]; + } if (this.didRemove) { - obj[key] = null; + node = obj[key] = null; this.flatten(); } // stop traversal - if (this.didSkip) return; + if (this.didSkip) + return this.didStop; } // traverse node - traverse(node, opts, ourScope); + traverseNode(node, opts, ourScope); // exit if (opts.exit) { result = opts.exit.call(this, node, parent, ourScope); - node = this.maybeReplace(result, obj, key, node); + + if (result) { + this.maybeReplace(result, obj, key, node); + } + } + + return this.didStop; +}; + +TraversalContext.prototype.visit = function (node, key, opts, scope) { + var nodes = node[key]; + if (!nodes) return; + + if (!Array.isArray(nodes)) { + return this.visitNode(node, key, opts, scope, node); + } + + if (nodes.length === 0) { + return; + } + + for (var k = 0; k < nodes.length; k++) { + if (nodes[k] && this.visitNode(nodes, k, opts, scope, node)) + return true; + } + + if (this.didFlatten) { + node[key] = _.flatten(node[key]); + + if (key === "body") { + // we can safely compact this + node[key] = _.compact(node[key]); + } } }; +function traverseNode(node, opts, scope) { + var keys = t.VISITOR_KEYS[node.type]; + if (!keys) return; + + opts = opts || {}; + var context = new TraversalContext(); + + for (var j = 0; j < keys.length; j++) { + if (context.visit(node, keys[j], opts, scope)) + return; + } +} + function traverse(parent, opts, scope) { // falsy node if (!parent) return; // array of nodes - if (Array.isArray(parent)) { - for (var i = 0; i < parent.length; i++) - traverse(parent[i], opts, scope); + if (!Array.isArray(parent)) { + traverseNode(parent, opts, scope); return; } - var keys = t.VISITOR_KEYS[parent.type]; - if (!keys) return; - - opts = opts || {}; - var context = null; - - for (var j = 0; j < keys.length; j++) { - var key = keys[j]; - var nodes = parent[key]; - if (!nodes) continue; - - if (Array.isArray(nodes)) { - for (var k = 0; k < nodes.length; k++) { - context = new TraversalContext(context); - context.visit(nodes, k, opts, scope, parent); - if (context.didStop) return; - } - - if (context && context.didFlatten) { - parent[key] = _.flatten(parent[key]); - - if (key === "body") { - // we can safely compact this - parent[key] = _.compact(parent[key]); - } - } - } else { - context = new TraversalContext(context); - context.visit(parent, key, opts, scope, parent); - if (context.didStop) return; - } + for (var i = 0; i < parent.length; i++) { + traverseNode(parent[i], opts, scope); } } From f9480b52805a69719cd49150bbc61aaaa7f4d3fd Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Sat, 17 Jan 2015 00:10:57 +0300 Subject: [PATCH 067/129] Avoid closures when traversing --- .../transformation/helpers/name-method.js | 23 ++-- .../helpers/remap-async-to-generator.js | 4 +- lib/6to5/transformation/modules/_default.js | 20 +-- lib/6to5/transformation/modules/system.js | 34 ++--- lib/6to5/transformation/transformer.js | 44 ++++--- .../transformers/_alias-functions.js | 37 +++--- .../transformers/es6-classes.js | 10 +- .../es6-computed-property-names.js | 2 +- .../transformers/es6-constants.js | 13 +- .../transformers/es6-default-parameters.js | 2 +- .../transformers/es6-destructuring.js | 12 +- .../transformation/transformers/es6-for-of.js | 8 +- .../transformers/es6-let-scoping.js | 53 ++++---- .../transformers/es6-modules.js | 4 +- .../es6-property-method-assignment.js | 2 +- .../transformers/es6-rest-parameters.js | 2 +- .../transformation/transformers/es6-spread.js | 6 +- .../transformers/es6-template-literals.js | 2 +- .../transformers/es7-abstract-references.js | 4 +- .../transformers/es7-array-comprehension.js | 6 +- .../transformers/es7-object-spread.js | 2 +- .../optional-async-to-generator.js | 2 +- .../optional-bluebird-coroutines.js | 2 +- .../transformers/optional-core-aliasing.js | 4 +- .../transformers/optional-proto-to-assign.js | 6 +- .../transformers/optional-typeof-symbol.js | 4 +- .../playground-memoization-operator.js | 4 +- .../transformers/playground-method-binding.js | 4 +- .../playground-object-getter-memoization.js | 2 +- lib/6to5/transformation/transformers/react.js | 4 +- .../spec-no-for-in-of-assignment.js | 2 +- .../transformers/spec-setters.js | 2 +- lib/6to5/traverse/index.js | 122 ++++++++++-------- lib/6to5/traverse/scope.js | 17 ++- lib/6to5/types/index.js | 3 +- lib/6to5/util.js | 4 +- 36 files changed, 257 insertions(+), 215 deletions(-) diff --git a/lib/6to5/transformation/helpers/name-method.js b/lib/6to5/transformation/helpers/name-method.js index 00d3c77a08..340bf7e27d 100644 --- a/lib/6to5/transformation/helpers/name-method.js +++ b/lib/6to5/transformation/helpers/name-method.js @@ -9,28 +9,31 @@ module.exports = function (node, file, scope) { var id = t.toIdentifier(key.value); key = t.identifier(id); - var selfReference = false; - var outerDeclar = scope.get(id, true); + var state = { + id: id, + selfReference: false, + outerDeclar: scope.get(id, true), + }; traverse(node, { - enter: function (node, parent, scope) { + enter: function (node, parent, scope, context, state) { // check if this node is an identifier that matches the same as our function id - if (!t.isIdentifier(node, { name: id })) return; + if (!t.isIdentifier(node, { name: state.id })) return; // check if this node is the one referenced if (!t.isReferenced(node, parent)) return; // check that we don't have a local variable declared as that removes the need // for the wrapper - var localDeclar = scope.get(id, true); - if (localDeclar !== outerDeclar) return; + var localDeclar = scope.get(state.id, true); + if (localDeclar !== state.outerDeclar) return; - selfReference = true; - this.stop(); + state.selfReference = true; + context.stop(); } - }, scope); + }, scope, state); - if (selfReference) { + if (state.selfReference) { node.value = util.template("property-method-assignment-wrapper", { FUNCTION: node.value, FUNCTION_ID: key, diff --git a/lib/6to5/transformation/helpers/remap-async-to-generator.js b/lib/6to5/transformation/helpers/remap-async-to-generator.js index 86a2c00ba7..7868e86ec7 100644 --- a/lib/6to5/transformation/helpers/remap-async-to-generator.js +++ b/lib/6to5/transformation/helpers/remap-async-to-generator.js @@ -6,8 +6,8 @@ module.exports = function (node, callId) { node.generator = true; traverse(node, { - enter: function (node) { - if (t.isFunction(node)) this.skip(); + enter: function (node, parent, scope, context) { + if (t.isFunction(node)) context.skip(); if (t.isAwaitExpression(node)) { node.type = "YieldExpression"; diff --git a/lib/6to5/transformation/modules/_default.js b/lib/6to5/transformation/modules/_default.js index 42e377b3a8..68040ac8b8 100644 --- a/lib/6to5/transformation/modules/_default.js +++ b/lib/6to5/transformation/modules/_default.js @@ -20,13 +20,13 @@ DefaultFormatter.prototype.getLocalExports = function () { var localExports = {}; traverse(this.file.ast, { - enter: function (node) { + enter: function (node, parent, scope, context, localExports) { var declar = node && node.declaration; if (t.isExportDeclaration(node) && declar && t.isStatement(declar)) { _.extend(localExports, t.getIds(declar, true)); } } - }); + }, null, localExports); return localExports; }; @@ -35,12 +35,12 @@ DefaultFormatter.prototype.getLocalImports = function () { var localImports = {}; traverse(this.file.ast, { - enter: function (node) { + enter: function (node, parent, scope, context, localImports) { if (t.isImportDeclaration(node)) { _.extend(localImports, t.getIds(node, true)); } } - }); + }, null, localImports); return localImports; }; @@ -62,7 +62,7 @@ DefaultFormatter.prototype.checkCollisions = function () { }; traverse(file.ast, { - enter: function (node) { + enter: function (node, parent, scope, context, check) { if (t.isAssignmentExpression(node)) { var left = node.left; @@ -75,7 +75,7 @@ DefaultFormatter.prototype.checkCollisions = function () { _.each(t.getIds(node, true), check); } } - }); + }, null, check); }; DefaultFormatter.prototype.remapExportAssignment = function (node) { @@ -100,9 +100,9 @@ DefaultFormatter.prototype.remapAssignments = function () { }; traverse(this.file.ast, { - enter: function (node, parent, scope) { + enter: function (node, parent, scope, context, isLocalReference) { if (t.isUpdateExpression(node) && isLocalReference(node.argument, scope)) { - this.skip(); + context.skip(); // expand to long file assignment expression var assign = t.assignmentExpression(node.operator[0] + "=", node.argument, t.literal(1)); @@ -130,11 +130,11 @@ DefaultFormatter.prototype.remapAssignments = function () { } if (t.isAssignmentExpression(node) && isLocalReference(node.left, scope)) { - this.skip(); + context.skip(); return self.remapExportAssignment(node); } } - }); + }, null, isLocalReference); }; DefaultFormatter.prototype.getModuleName = function () { diff --git a/lib/6to5/transformation/modules/system.js b/lib/6to5/transformation/modules/system.js index b70c8423a4..d0650a69c2 100644 --- a/lib/6to5/transformation/modules/system.js +++ b/lib/6to5/transformation/modules/system.js @@ -63,28 +63,31 @@ SystemFormatter.prototype.importSpecifier = function (specifier, node, nodes) { SystemFormatter.prototype.buildRunnerSetters = function (block, hoistDeclarators) { return t.arrayExpression(_.map(this.ids, function (uid, source) { - var nodes = []; + var state = { + nodes: [], + hoistDeclarators: hoistDeclarators + }; traverse(block, { - enter: function (node) { + enter: function (node, parent, scope, context, state) { if (node._importSource === source) { if (t.isVariableDeclaration(node)) { _.each(node.declarations, function (declar) { - hoistDeclarators.push(t.variableDeclarator(declar.id)); - nodes.push(t.expressionStatement( + state.hoistDeclarators.push(t.variableDeclarator(declar.id)); + state.nodes.push(t.expressionStatement( t.assignmentExpression("=", declar.id, declar.init) )); }); } else { - nodes.push(node); + state.nodes.push(node); } - this.remove(); + context.remove(); } } - }); + }, null, state); - return t.functionExpression(null, [uid], t.blockStatement(nodes)); + return t.functionExpression(null, [uid], t.blockStatement(state.nodes)); })); }; @@ -112,10 +115,10 @@ SystemFormatter.prototype.transform = function (ast) { // hoist up all variable declarations traverse(block, { - enter: function (node, parent, scope) { + enter: function (node, parent, scope, context, hoistDeclarators) { if (t.isFunction(node)) { // nothing inside is accessible - return this.skip(); + return context.skip(); } if (t.isVariableDeclaration(node)) { @@ -150,7 +153,8 @@ SystemFormatter.prototype.transform = function (ast) { return nodes; } } - }); + }, null, hoistDeclarators); + if (hoistDeclarators.length) { var hoistDeclar = t.variableDeclaration("var", hoistDeclarators); hoistDeclar._blockHoist = true; @@ -159,15 +163,15 @@ SystemFormatter.prototype.transform = function (ast) { // hoist up function declarations for circular references traverse(block, { - enter: function (node) { - if (t.isFunction(node)) this.skip(); + enter: function (node, parent, scope, context, handlerBody) { + if (t.isFunction(node)) context.skip(); if (t.isFunctionDeclaration(node) || node._blockHoist) { handlerBody.push(node); - this.remove(); + context.remove(); } } - }); + }, null, handlerBody); handlerBody.push(returnStatement); diff --git a/lib/6to5/transformation/transformer.js b/lib/6to5/transformation/transformer.js index f4de701b51..dbcd0f9d10 100644 --- a/lib/6to5/transformation/transformer.js +++ b/lib/6to5/transformation/transformer.js @@ -4,6 +4,25 @@ var traverse = require("../traverse"); var t = require("../types"); var _ = require("lodash"); +function noop() { } + +function enter(node, parent, scope, context, args) { + var fns = args[1][node.type]; + if (!fns) return; + return fns.enter(node, parent, scope, context, args[0]); +} + +function exit(node, parent, scope, context, args) { + var fns = args[1][node.type]; + if (!fns) return; + return fns.exit(node, parent, scope, context, args[0]); +} + +var traverseOpts = { + enter: enter, + exit: exit +}; + function Transformer(key, transformer, opts) { this.manipulateOptions = transformer.manipulateOptions; this.experimental = !!transformer.experimental; @@ -32,6 +51,9 @@ Transformer.prototype.normalise = function (transformer) { if (!_.isObject(fns)) return; + if (!fns.enter) fns.enter = noop; + if (!fns.exit) fns.exit = noop; + transformer[type] = fns; var aliases = t.FLIPPED_ALIAS_KEYS[type]; @@ -54,28 +76,8 @@ Transformer.prototype.astRun = function (file, key) { }; Transformer.prototype.transform = function (file) { - var transformer = this.transformer; - - var build = function (exit) { - return function (node, parent, scope) { - var fns = transformer[node.type]; - if (!fns) return; - - var fn = fns.enter; - if (exit) fn = fns.exit; - if (!fn) return; - - return fn.call(this, node, parent, file, scope); - }; - }; - this.astRun(file, "before"); - - traverse(file.ast, { - enter: build(), - exit: build(true) - }); - + traverse(file.ast, traverseOpts, null, [file, this.transformer]); this.astRun(file, "after"); }; diff --git a/lib/6to5/transformation/transformers/_alias-functions.js b/lib/6to5/transformation/transformers/_alias-functions.js index 5cf9003c84..bc52a45ee8 100644 --- a/lib/6to5/transformation/transformers/_alias-functions.js +++ b/lib/6to5/transformation/transformers/_alias-functions.js @@ -5,22 +5,23 @@ var go = function (getBody, node, file, scope) { var argumentsId; var thisId; - var getArgumentsId = function () { - return argumentsId = argumentsId || file.generateUidIdentifier("arguments", scope); - }; - - var getThisId = function () { - return thisId = thisId || file.generateUidIdentifier("this", scope); + var state = { + getArgumentsId: function () { + return argumentsId = argumentsId || file.generateUidIdentifier("arguments", scope); + }, + getThisId: function () { + return thisId = thisId || file.generateUidIdentifier("this", scope); + } }; // traverse the function and find all alias functions so we can alias // `arguments` and `this` if necessary traverse(node, { - enter: function (node) { + enter: function (node, parent, scope, context, state) { if (!node._aliasFunction) { if (t.isFunction(node)) { // stop traversal of this node as it'll be hit again by this transformer - return this.skip(); + return context.skip(); } else { return; } @@ -28,30 +29,30 @@ var go = function (getBody, node, file, scope) { // traverse all child nodes of this function and find `arguments` and `this` traverse(node, { - enter: function (node, parent) { + enter: function (node, parent, scope, context, state) { if (t.isFunction(node) && !node._aliasFunction) { - return this.skip(); + return context.skip(); } - if (node._ignoreAliasFunctions) return this.skip(); + if (node._ignoreAliasFunctions) return context.skip(); var getId; if (t.isIdentifier(node) && node.name === "arguments") { - getId = getArgumentsId; + getId = state.getArgumentsId; } else if (t.isThisExpression(node)) { - getId = getThisId; + getId = state.getThisId; } else { return; } if (t.isReferenced(node, parent)) return getId(); } - }); + }, null, state); - return this.skip(); + return context.skip(); } - }); + }, null, state); var body; @@ -71,14 +72,14 @@ var go = function (getBody, node, file, scope) { } }; -exports.Program = function (node, parent, file, scope) { +exports.Program = function (node, parent, scope, context, file) { go(function () { return node.body; }, node, file, scope); }; exports.FunctionDeclaration = -exports.FunctionExpression = function (node, parent, file, scope) { +exports.FunctionExpression = function (node, parent, scope, context, file) { go(function () { t.ensureBlock(node); return node.body.body; diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index dc079ca164..18f24c4031 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -3,11 +3,11 @@ var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); -exports.ClassDeclaration = function (node, parent, file, scope) { +exports.ClassDeclaration = function (node, parent, scope, context, file) { return new Class(node, file, scope, true).run(); }; -exports.ClassExpression = function (node, parent, file, scope) { +exports.ClassExpression = function (node, parent, scope, context, file) { if (!node.id) { if (t.isProperty(parent) && parent.value === node && !parent.computed && t.isIdentifier(parent.key)) { // var o = { foo: class {} }; @@ -316,16 +316,16 @@ Class.prototype.replaceSuperReferences = function (methodNode) { function traverseLevel(node, topLevel) { traverse(node, { - enter: function (node, parent) { + enter: function (node, parent, scope, context) { if (t.isFunction(node) && !t.isArrowFunctionExpression(node)) { // we need to call traverseLevel again so we're context aware traverseLevel(node, false); - return this.skip(); + return context.skip(); } if (t.isProperty(node, { method: true }) || t.isMethodDefinition(node)) { // break on object methods - return this.skip(); + return context.skip(); } var getThisReference = function () { diff --git a/lib/6to5/transformation/transformers/es6-computed-property-names.js b/lib/6to5/transformation/transformers/es6-computed-property-names.js index dcfff3b827..3789ca1d23 100644 --- a/lib/6to5/transformation/transformers/es6-computed-property-names.js +++ b/lib/6to5/transformation/transformers/es6-computed-property-names.js @@ -1,6 +1,6 @@ var t = require("../../types"); -exports.ObjectExpression = function (node, parent, file, scope) { +exports.ObjectExpression = function (node, parent, scope, context, file) { var hasComputed = false; for (var i = 0; i < node.properties.length; i++) { diff --git a/lib/6to5/transformation/transformers/es6-constants.js b/lib/6to5/transformation/transformers/es6-constants.js index dc8e0ce49e..096eee9097 100644 --- a/lib/6to5/transformation/transformers/es6-constants.js +++ b/lib/6to5/transformation/transformers/es6-constants.js @@ -6,7 +6,7 @@ exports.Program = exports.BlockStatement = exports.ForInStatement = exports.ForOfStatement = -exports.ForStatement = function (node, parent, file) { +exports.ForStatement = function (node, parent, scope, context, file) { var hasConstants = false; var constants = {}; @@ -69,14 +69,19 @@ exports.ForStatement = function (node, parent, file) { if (!hasConstants) return; + var state = { + check: check, + getIds: getIds + }; + traverse(node, { - enter: function (child, parent, scope) { + enter: function (child, parent, scope, context, state) { if (child._ignoreConstant) return; if (t.isVariableDeclaration(child)) return; if (t.isVariableDeclarator(child) || t.isDeclaration(child) || t.isAssignmentExpression(child)) { - check(parent, getIds(child), scope); + state.check(parent, state.getIds(child), scope); } } - }); + }, null, state); }; diff --git a/lib/6to5/transformation/transformers/es6-default-parameters.js b/lib/6to5/transformation/transformers/es6-default-parameters.js index 0d99b6db8e..4b03d56295 100644 --- a/lib/6to5/transformation/transformers/es6-default-parameters.js +++ b/lib/6to5/transformation/transformers/es6-default-parameters.js @@ -2,7 +2,7 @@ var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); -exports.Function = function (node, parent, file, scope) { +exports.Function = function (node, parent, scope, context, file) { if (!node.defaults || !node.defaults.length) return; t.ensureBlock(node); diff --git a/lib/6to5/transformation/transformers/es6-destructuring.js b/lib/6to5/transformation/transformers/es6-destructuring.js index 22d016b38f..bcd04ba4b2 100644 --- a/lib/6to5/transformation/transformers/es6-destructuring.js +++ b/lib/6to5/transformation/transformers/es6-destructuring.js @@ -148,7 +148,7 @@ var pushPattern = function (opts) { }; exports.ForInStatement = -exports.ForOfStatement = function (node, parent, file, scope) { +exports.ForOfStatement = function (node, parent, scope, context, file) { var declar = node.left; if (!t.isVariableDeclaration(declar)) return; @@ -174,7 +174,7 @@ exports.ForOfStatement = function (node, parent, file, scope) { block.body = nodes.concat(block.body); }; -exports.Function = function (node, parent, file, scope) { +exports.Function = function (node, parent, scope, context, file) { var nodes = []; var hasDestructuring = false; @@ -205,7 +205,7 @@ exports.Function = function (node, parent, file, scope) { block.body = nodes.concat(block.body); }; -exports.CatchClause = function (node, parent, file, scope) { +exports.CatchClause = function (node, parent, scope, context, file) { var pattern = node.param; if (!t.isPattern(pattern)) return; @@ -223,7 +223,7 @@ exports.CatchClause = function (node, parent, file, scope) { node.body.body = nodes.concat(node.body.body); }; -exports.ExpressionStatement = function (node, parent, file, scope) { +exports.ExpressionStatement = function (node, parent, scope, context, file) { var expr = node.expression; if (expr.type !== "AssignmentExpression") return; @@ -245,7 +245,7 @@ exports.ExpressionStatement = function (node, parent, file, scope) { return nodes; }; -exports.AssignmentExpression = function (node, parent, file, scope) { +exports.AssignmentExpression = function (node, parent, scope, context, file) { if (parent.type === "ExpressionStatement") return; if (!t.isPattern(node.left)) return; @@ -270,7 +270,7 @@ exports.AssignmentExpression = function (node, parent, file, scope) { return t.toSequenceExpression(nodes, scope); }; -exports.VariableDeclaration = function (node, parent, file, scope) { +exports.VariableDeclaration = function (node, parent, scope, context, file) { if (t.isForInStatement(parent) || t.isForOfStatement(parent)) return; var nodes = []; diff --git a/lib/6to5/transformation/transformers/es6-for-of.js b/lib/6to5/transformation/transformers/es6-for-of.js index 61357cfbad..9018e995af 100644 --- a/lib/6to5/transformation/transformers/es6-for-of.js +++ b/lib/6to5/transformation/transformers/es6-for-of.js @@ -1,11 +1,11 @@ var util = require("../../util"); var t = require("../../types"); -exports.ForOfStatement = function (node, parent, file, scope) { +exports.ForOfStatement = function (node, parent, scope, context, file) { var callback = spec; if (file.isLoose("forOf")) callback = loose; - var build = callback(node, parent, file, scope); + var build = callback(node, parent, scope, context, file); var declar = build.declar; var loop = build.loop; var block = loop.body; @@ -31,7 +31,7 @@ exports.ForOfStatement = function (node, parent, file, scope) { return loop; }; -var loose = function (node, parent, file, scope) { +var loose = function (node, parent, scope, context, file) { var left = node.left; var declar, id; @@ -63,7 +63,7 @@ var loose = function (node, parent, file, scope) { }; }; -var spec = function (node, parent, file, scope) { +var spec = function (node, parent, scope, context, file) { var left = node.left; var declar; diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index bdce7be036..7c589851cd 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -34,7 +34,7 @@ exports.VariableDeclaration = function (node, parent) { isLet(node, parent); }; -exports.Loop = function (node, parent, file, scope) { +exports.Loop = function (node, parent, scope, context, file) { var init = node.left || node.init; if (isLet(init, node)) { t.ensureBlock(node); @@ -46,7 +46,7 @@ exports.Loop = function (node, parent, file, scope) { node.label = parent.label; } - var letScoping = new LetScoping(node, node.body, parent, file, scope); + var letScoping = new LetScoping(node, node.body, parent, scope, file); letScoping.run(); if (node.label && !t.isLabeledStatement(parent)) { @@ -55,9 +55,9 @@ exports.Loop = function (node, parent, file, scope) { } }; -exports.BlockStatement = function (block, parent, file, scope) { +exports.BlockStatement = function (block, parent, scope, context, file) { if (!t.isLoop(parent)) { - var letScoping = new LetScoping(false, block, parent, file, scope); + var letScoping = new LetScoping(false, block, parent, scope, file); letScoping.run(); } }; @@ -68,11 +68,11 @@ exports.BlockStatement = function (block, parent, file, scope) { * @param {Boolean|Node} loopParent * @param {Node} block * @param {Node} parent - * @param {File} file * @param {Scope} scope + * @param {File} file */ -function LetScoping(loopParent, block, parent, file, scope) { +function LetScoping(loopParent, block, parent, scope, file) { this.loopParent = loopParent; this.parent = parent; this.scope = scope; @@ -167,7 +167,7 @@ LetScoping.prototype.remap = function () { if (!this.info.hasDuplicates) return; - var replace = function (node, parent, scope) { + var replace = function (node, parent, scope, context, replacements) { if (!t.isIdentifier(node)) return; if (!t.isReferenced(node, parent)) return; if (scope && scope.hasOwn(node.name)) return; @@ -176,7 +176,7 @@ LetScoping.prototype.remap = function () { var traverseReplace = function (node, parent) { replace(node, parent); - traverse(node, { enter: replace }); + traverse(node, { enter: replace }, null, replacements); }; var loopParent = this.loopParent; @@ -186,7 +186,7 @@ LetScoping.prototype.remap = function () { traverseReplace(loopParent.update, loopParent); } - traverse(block, { enter: replace }); + traverse(block, { enter: replace }, null, replacements); }; /** @@ -277,11 +277,11 @@ LetScoping.prototype.checkLoop = function () { }; traverse(this.block, { - enter: function (node, parent) { + enter: function (node, parent, scope, context) { var replace; if (t.isFunction(node) || t.isLoop(node)) { - return this.skip(); + return context.skip(); } if (node && !node.label) { @@ -305,7 +305,7 @@ LetScoping.prototype.checkLoop = function () { if (replace) return t.inherits(replace, node); } - }); + }, null, has); return has; }; @@ -316,9 +316,8 @@ LetScoping.prototype.checkLoop = function () { */ LetScoping.prototype.hoistVarDeclarations = function () { - var self = this; traverse(this.block, { - enter: function (node, parent) { + enter: function (node, parent, scope, context, self) { if (t.isForStatement(node)) { if (isVar(node.init, node)) { node.init = t.sequenceExpression(self.pushDeclar(node.init)); @@ -330,10 +329,10 @@ LetScoping.prototype.hoistVarDeclarations = function () { } else if (isVar(node, parent)) { return self.pushDeclar(node).map(t.expressionStatement); } else if (t.isFunction(node)) { - return this.skip(); + return context.skip(); } } - }); + }, null, this); }; /** @@ -359,13 +358,15 @@ LetScoping.prototype.getParams = function (params) { */ LetScoping.prototype.getLetReferences = function () { - var closurify = false; - var self = this; + var state = { + self: this, + closurify: false + }; // traverse through this block, stopping on functions and checking if they // contain any outside let references traverse(this.block, { - enter: function (node, parent, scope) { + enter: function (node, parent, scope, context, state) { if (t.isFunction(node)) { traverse(node, { enter: function (node, parent) { @@ -379,22 +380,22 @@ LetScoping.prototype.getLetReferences = function () { // to our let scope if (scope.hasOwn(node.name, true)) return; - closurify = true; + state.closurify = true; // this key doesn't appear just outside our scope - if (!_.contains(self.info.outsideKeys, node.name)) return; + if (!_.contains(state.self.info.outsideKeys, node.name)) return; // push this badboy - self.letReferences[node.name] = node; + state.self.letReferences[node.name] = node; } - }); + }, null, state); - return this.skip(); + return context.skip(); } } - }); + }, null, state); - return closurify; + return state.closurify; }; /** diff --git a/lib/6to5/transformation/transformers/es6-modules.js b/lib/6to5/transformation/transformers/es6-modules.js index 9d9631095b..a9b60473f2 100644 --- a/lib/6to5/transformation/transformers/es6-modules.js +++ b/lib/6to5/transformation/transformers/es6-modules.js @@ -6,7 +6,7 @@ exports.ast = { } }; -exports.ImportDeclaration = function (node, parent, file) { +exports.ImportDeclaration = function (node, parent, scope, context, file) { var nodes = []; if (node.specifiers.length) { @@ -26,7 +26,7 @@ exports.ImportDeclaration = function (node, parent, file) { return nodes; }; -exports.ExportDeclaration = function (node, parent, file) { +exports.ExportDeclaration = function (node, parent, scope, context, file) { var nodes = []; if (node.declaration) { diff --git a/lib/6to5/transformation/transformers/es6-property-method-assignment.js b/lib/6to5/transformation/transformers/es6-property-method-assignment.js index 30a54102a5..7237ef1c4f 100644 --- a/lib/6to5/transformation/transformers/es6-property-method-assignment.js +++ b/lib/6to5/transformation/transformers/es6-property-method-assignment.js @@ -2,7 +2,7 @@ var nameMethod = require("../helpers/name-method"); var util = require("../../util"); var t = require("../../types"); -exports.Property = function (node, parent, file, scope) { +exports.Property = function (node, parent, scope, context, file) { if (!node.method) return; node.method = false; diff --git a/lib/6to5/transformation/transformers/es6-rest-parameters.js b/lib/6to5/transformation/transformers/es6-rest-parameters.js index 5bfcf88601..f9ec265c9e 100644 --- a/lib/6to5/transformation/transformers/es6-rest-parameters.js +++ b/lib/6to5/transformation/transformers/es6-rest-parameters.js @@ -1,7 +1,7 @@ var util = require("../../util"); var t = require("../../types"); -exports.Function = function (node, parent, file) { +exports.Function = function (node, parent, scope, context, file) { if (!node.rest) return; var rest = node.rest; diff --git a/lib/6to5/transformation/transformers/es6-spread.js b/lib/6to5/transformation/transformers/es6-spread.js index 8547bfa550..4fa64b5e2b 100644 --- a/lib/6to5/transformation/transformers/es6-spread.js +++ b/lib/6to5/transformation/transformers/es6-spread.js @@ -40,7 +40,7 @@ var build = function (props, file) { return nodes; }; -exports.ArrayExpression = function (node, parent, file) { +exports.ArrayExpression = function (node, parent, scope, context, file) { var elements = node.elements; if (!hasSpread(elements)) return; @@ -55,7 +55,7 @@ exports.ArrayExpression = function (node, parent, file) { return t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes); }; -exports.CallExpression = function (node, parent, file, scope) { +exports.CallExpression = function (node, parent, scope, context, file) { var args = node.arguments; if (!hasSpread(args)) return; @@ -95,7 +95,7 @@ exports.CallExpression = function (node, parent, file, scope) { node.arguments.unshift(contextLiteral); }; -exports.NewExpression = function (node, parent, file) { +exports.NewExpression = function (node, parent, scope, context, file) { var args = node.arguments; if (!hasSpread(args)) return; diff --git a/lib/6to5/transformation/transformers/es6-template-literals.js b/lib/6to5/transformation/transformers/es6-template-literals.js index 8ad54914f7..d2f360b104 100644 --- a/lib/6to5/transformation/transformers/es6-template-literals.js +++ b/lib/6to5/transformation/transformers/es6-template-literals.js @@ -4,7 +4,7 @@ var buildBinaryExpression = function (left, right) { return t.binaryExpression("+", left, right); }; -exports.TaggedTemplateExpression = function (node, parent, file) { +exports.TaggedTemplateExpression = function (node, parent, scope, context, file) { var args = []; var quasi = node.quasi; diff --git a/lib/6to5/transformation/transformers/es7-abstract-references.js b/lib/6to5/transformation/transformers/es7-abstract-references.js index 89bc7c05e4..39003b914c 100644 --- a/lib/6to5/transformation/transformers/es7-abstract-references.js +++ b/lib/6to5/transformation/transformers/es7-abstract-references.js @@ -21,7 +21,7 @@ var container = function (parent, call, ret) { } }; -exports.AssignmentExpression = function (node, parent, file, scope) { +exports.AssignmentExpression = function (node, parent, scope, context, file) { var left = node.left; if (!t.isVirtualPropertyExpression(left)) return; @@ -74,7 +74,7 @@ exports.UnaryExpression = function (node, parent) { return container(parent, call, t.literal(true)); }; -exports.CallExpression = function (node, parent, file, scope) { +exports.CallExpression = function (node, parent, scope, context, file) { var callee = node.callee; if (!t.isVirtualPropertyExpression(callee)) return; diff --git a/lib/6to5/transformation/transformers/es7-array-comprehension.js b/lib/6to5/transformation/transformers/es7-array-comprehension.js index 0a18a94e8a..e50b135d0b 100644 --- a/lib/6to5/transformation/transformers/es7-array-comprehension.js +++ b/lib/6to5/transformation/transformers/es7-array-comprehension.js @@ -5,7 +5,7 @@ var t = require("../../types"); exports.experimental = true; -var build = function (node, parent, file, scope) { +var build = function (node, parent, scope, context, file) { var uid = scope.generateUidBasedOnNode(parent, file); var container = util.template("array-comprehension-container", { @@ -34,8 +34,8 @@ var build = function (node, parent, file, scope) { return container; }; -exports.ComprehensionExpression = function (node, parent, file, scope) { +exports.ComprehensionExpression = function (node, parent, scope, context, file) { if (node.generator) return; - return build(node, parent, file, scope); + return build(node, parent, scope, context, file); }; diff --git a/lib/6to5/transformation/transformers/es7-object-spread.js b/lib/6to5/transformation/transformers/es7-object-spread.js index a9ffecaf03..083bbdc166 100644 --- a/lib/6to5/transformation/transformers/es7-object-spread.js +++ b/lib/6to5/transformation/transformers/es7-object-spread.js @@ -4,7 +4,7 @@ var t = require("../../types"); exports.experimental = true; -exports.ObjectExpression = function (node, parent, file) { +exports.ObjectExpression = function (node, parent, scope, context, file) { var hasSpread = false; var i; var prop; diff --git a/lib/6to5/transformation/transformers/optional-async-to-generator.js b/lib/6to5/transformation/transformers/optional-async-to-generator.js index 143445a9c2..d6f952783c 100644 --- a/lib/6to5/transformation/transformers/optional-async-to-generator.js +++ b/lib/6to5/transformation/transformers/optional-async-to-generator.js @@ -5,7 +5,7 @@ exports.optional = true; exports.manipulateOptions = bluebirdCoroutines.manipulateOptions; -exports.Function = function (node, parent, file) { +exports.Function = function (node, parent, scope, context, file) { if (!node.async || node.generator) return; return remapAsyncToGenerator(node, file.addHelper("async-to-generator")); diff --git a/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js b/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js index 5338fa1e1c..73b9db1fd7 100644 --- a/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js +++ b/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js @@ -9,7 +9,7 @@ exports.manipulateOptions = function (opts) { exports.optional = true; -exports.Function = function (node, parent, file) { +exports.Function = function (node, parent, scope, context, file) { if (!node.async || node.generator) return; return remapAsyncToGenerator( diff --git a/lib/6to5/transformation/transformers/optional-core-aliasing.js b/lib/6to5/transformation/transformers/optional-core-aliasing.js index 3610ae8f62..d563bb619a 100644 --- a/lib/6to5/transformation/transformers/optional-core-aliasing.js +++ b/lib/6to5/transformation/transformers/optional-core-aliasing.js @@ -26,7 +26,7 @@ exports.ast = { exit: function (ast, file) { traverse(ast, { - enter: function (node, parent) { + enter: function (node, parent, scope, context) { var prop; if (t.isMemberExpression(node) && t.isReferenced(node, parent)) { @@ -37,7 +37,7 @@ exports.ast = { if (!t.isReferenced(obj, node)) return; if (!node.computed && coreHas(obj) && _.has(core[obj.name], prop.name)) { - this.skip(); + context.skip(); return t.prependToMemberExpression(node, file._coreId); } } else if (t.isIdentifier(node) && !t.isMemberExpression(parent) && t.isReferenced(node, parent) && _.contains(ALIASABLE_CONSTRUCTORS, node.name)) { diff --git a/lib/6to5/transformation/transformers/optional-proto-to-assign.js b/lib/6to5/transformation/transformers/optional-proto-to-assign.js index e3f0a78022..73a9c8167d 100644 --- a/lib/6to5/transformation/transformers/optional-proto-to-assign.js +++ b/lib/6to5/transformation/transformers/optional-proto-to-assign.js @@ -17,7 +17,7 @@ var buildDefaultsCallExpression = function (expr, ref, file) { exports.optional = true; exports.secondPass = true; -exports.AssignmentExpression = function (node, parent, file, scope) { +exports.AssignmentExpression = function (node, parent, scope, context, file) { if (t.isExpressionStatement(parent)) return; if (!isProtoAssignmentExpression(node)) return; @@ -32,7 +32,7 @@ exports.AssignmentExpression = function (node, parent, file, scope) { return t.toSequenceExpression(nodes); }; -exports.ExpressionStatement = function (node, parent, file) { +exports.ExpressionStatement = function (node, parent, scope, context, file) { var expr = node.expression; if (!t.isAssignmentExpression(expr, { operator: "=" })) return; @@ -41,7 +41,7 @@ exports.ExpressionStatement = function (node, parent, file) { } }; -exports.ObjectExpression = function (node, parent, file) { +exports.ObjectExpression = function (node, parent, scope, context, file) { var proto; for (var i = 0; i < node.properties.length; i++) { diff --git a/lib/6to5/transformation/transformers/optional-typeof-symbol.js b/lib/6to5/transformation/transformers/optional-typeof-symbol.js index 36b8458936..1c1aed8cc1 100644 --- a/lib/6to5/transformation/transformers/optional-typeof-symbol.js +++ b/lib/6to5/transformation/transformers/optional-typeof-symbol.js @@ -2,8 +2,8 @@ var t = require("../../types"); exports.optional = true; -exports.UnaryExpression = function (node, parent, file) { - this.skip(); +exports.UnaryExpression = function (node, parent, scope, context, file) { + context.skip(); if (node.operator === "typeof") { var call = t.callExpression(file.addHelper("typeof"), [node.argument]); diff --git a/lib/6to5/transformation/transformers/playground-memoization-operator.js b/lib/6to5/transformation/transformers/playground-memoization-operator.js index 1f5fa4bb9f..1bd6d004ee 100644 --- a/lib/6to5/transformation/transformers/playground-memoization-operator.js +++ b/lib/6to5/transformation/transformers/playground-memoization-operator.js @@ -48,7 +48,7 @@ var buildAssignment = function (expr, obj, prop) { return t.assignmentExpression("=", buildAbsoluteRef(expr.left, obj, prop), expr.right); }; -exports.ExpressionStatement = function (node, parent, file, scope) { +exports.ExpressionStatement = function (node, parent, scope, context, file) { var expr = node.expression; if (!isMemo(expr)) return; @@ -66,7 +66,7 @@ exports.ExpressionStatement = function (node, parent, file, scope) { return nodes; }; -exports.AssignmentExpression = function (node, parent, file, scope) { +exports.AssignmentExpression = function (node, parent, scope, context, file) { if (t.isExpressionStatement(parent)) return; if (!isMemo(node)) return; diff --git a/lib/6to5/transformation/transformers/playground-method-binding.js b/lib/6to5/transformation/transformers/playground-method-binding.js index 6c6bd9e37b..4f3fe3bb51 100644 --- a/lib/6to5/transformation/transformers/playground-method-binding.js +++ b/lib/6to5/transformation/transformers/playground-method-binding.js @@ -1,6 +1,6 @@ var t = require("../../types"); -exports.BindMemberExpression = function (node, parent, file, scope) { +exports.BindMemberExpression = function (node, parent, scope, context, file) { var object = node.object; var prop = node.property; @@ -22,7 +22,7 @@ exports.BindMemberExpression = function (node, parent, file, scope) { } }; -exports.BindFunctionExpression = function (node, parent, file, scope) { +exports.BindFunctionExpression = function (node, parent, scope, context, file) { var buildCall = function (args) { var param = file.generateUidIdentifier("val", scope); return t.functionExpression(null, [param], t.blockStatement([ diff --git a/lib/6to5/transformation/transformers/playground-object-getter-memoization.js b/lib/6to5/transformation/transformers/playground-object-getter-memoization.js index 9c3f0bb6d0..08af63ea57 100644 --- a/lib/6to5/transformation/transformers/playground-object-getter-memoization.js +++ b/lib/6to5/transformation/transformers/playground-object-getter-memoization.js @@ -2,7 +2,7 @@ var traverse = require("../../traverse"); var t = require("../../types"); exports.Property = -exports.MethodDefinition = function (node, parent, file) { +exports.MethodDefinition = function (node, parent, scope, context, file) { if (node.kind !== "memo") return; node.kind = "get"; diff --git a/lib/6to5/transformation/transformers/react.js b/lib/6to5/transformation/transformers/react.js index 41b1ae4e14..79bca15c19 100644 --- a/lib/6to5/transformation/transformers/react.js +++ b/lib/6to5/transformation/transformers/react.js @@ -15,7 +15,7 @@ exports.XJSIdentifier = function (node) { } }; -exports.XJSNamespacedName = function (node, parent, file) { +exports.XJSNamespacedName = function (node, parent, scope, context, file) { throw file.errorWithNode(node, "Namespace tags are not supported. ReactJSX is not XML."); }; @@ -42,7 +42,7 @@ var isTag = function(tagName) { }; exports.XJSOpeningElement = { - exit: function (node, parent, file) { + exit: function (node, parent, scope, context, file) { var reactCompat = file.opts.reactCompat; var tagExpr = node.name; var args = []; diff --git a/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js b/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js index 5af12108f1..0b00e88f9d 100644 --- a/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js +++ b/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js @@ -1,7 +1,7 @@ var t = require("../../types"); exports.ForInStatement = -exports.ForOfStatement = function (node, parent, file) { +exports.ForOfStatement = function (node, parent, scope, context, file) { var left = node.left; if (t.isVariableDeclaration(left)) { var declar = left.declarations[0]; diff --git a/lib/6to5/transformation/transformers/spec-setters.js b/lib/6to5/transformation/transformers/spec-setters.js index 30b404ed1e..54d88827aa 100644 --- a/lib/6to5/transformation/transformers/spec-setters.js +++ b/lib/6to5/transformation/transformers/spec-setters.js @@ -1,5 +1,5 @@ exports.MethodDefinition = -exports.Property = function (node, parent, file) { +exports.Property = function (node, parent, scope, context, file) { if (node.kind === "set" && node.value.params.length !== 1) { throw file.errorWithNode(node.value, "Setters must have only one parameter"); } diff --git a/lib/6to5/traverse/index.js b/lib/6to5/traverse/index.js index 625905944d..7307a7665d 100644 --- a/lib/6to5/traverse/index.js +++ b/lib/6to5/traverse/index.js @@ -1,9 +1,13 @@ module.exports = traverse; +/* jshint maxparams:7 */ + var Scope = require("./scope"); var t = require("../types"); var _ = require("lodash"); +function noop() { } + function TraversalContext() { this.didSkip = false; this.didRemove = false; @@ -35,7 +39,7 @@ TraversalContext.prototype.reset = function () { this.didRemove = false; }; -TraversalContext.prototype.maybeReplace = function (result, obj, key, node) { +function replaceNode(obj, key, node, result) { var isArray = Array.isArray(result); // inherit comments from original node to the first replacement node @@ -44,7 +48,7 @@ TraversalContext.prototype.maybeReplace = function (result, obj, key, node) { if (inheritTo) t.inheritsComments(inheritTo, node); // replace the node - node = obj[key] = result; + obj[key] = result; // we're replacing a statement or block node with an array of statements so we better // ensure that it's a block @@ -53,11 +57,48 @@ TraversalContext.prototype.maybeReplace = function (result, obj, key, node) { } if (isArray) { - this.flatten(); + return true; } +} + +TraversalContext.prototype.enterNode = function (obj, key, node, enter, parent, scope, state) { + var result = enter(node, parent, scope, this, state); + var flatten = false; + + if (result) { + flatten = replaceNode(obj, key, node, result); + node = result; + + if (flatten) { + this.didFlatten = true; + } + } + + if (this.didRemove) { + obj[key] = null; + this.didFlatten = true; + } + + return node; }; -TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent) { +TraversalContext.prototype.exitNode = function (obj, key, node, exit, parent, scope, state) { + var result = exit(node, parent, scope, this, state); + var flatten = false; + + if (result) { + flatten = replaceNode(obj, key, node, result); + node = result; + + if (flatten) { + this.didFlatten = true; + } + } + + return node; +}; + +TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent, state) { this.reset(); var node = obj[key]; @@ -70,48 +111,23 @@ TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent) if (t.isScope(node)) ourScope = new Scope(node, scope); - var result; + node = this.enterNode(obj, key, node, opts.enter, parent, ourScope, state); - // enter - if (opts.enter) { - result = opts.enter.call(this, node, parent, ourScope); + if (this.didSkip) + return this.didStop; - if (result) { - this.maybeReplace(result, obj, key, node); - node = obj[key]; - } - - if (this.didRemove) { - node = obj[key] = null; - this.flatten(); - } - - // stop traversal - if (this.didSkip) - return this.didStop; - } - - // traverse node - traverseNode(node, opts, ourScope); - - // exit - if (opts.exit) { - result = opts.exit.call(this, node, parent, ourScope); - - if (result) { - this.maybeReplace(result, obj, key, node); - } - } + traverseNode(node, opts, ourScope, state); + this.exitNode(obj, key, node, opts.exit, parent, ourScope, state); return this.didStop; }; -TraversalContext.prototype.visit = function (node, key, opts, scope) { +TraversalContext.prototype.visit = function (node, key, opts, scope, state) { var nodes = node[key]; if (!nodes) return; if (!Array.isArray(nodes)) { - return this.visitNode(node, key, opts, scope, node); + return this.visitNode(node, key, opts, scope, node, state); } if (nodes.length === 0) { @@ -119,7 +135,7 @@ TraversalContext.prototype.visit = function (node, key, opts, scope) { } for (var k = 0; k < nodes.length; k++) { - if (nodes[k] && this.visitNode(nodes, k, opts, scope, node)) + if (nodes[k] && this.visitNode(nodes, k, opts, scope, node, state)) return true; } @@ -133,31 +149,33 @@ TraversalContext.prototype.visit = function (node, key, opts, scope) { } }; -function traverseNode(node, opts, scope) { +function traverseNode(node, opts, scope, state) { var keys = t.VISITOR_KEYS[node.type]; if (!keys) return; - opts = opts || {}; var context = new TraversalContext(); - for (var j = 0; j < keys.length; j++) { - if (context.visit(node, keys[j], opts, scope)) + if (context.visit(node, keys[j], opts, scope, state)) return; } } -function traverse(parent, opts, scope) { +function traverse(parent, opts, scope, state) { // falsy node if (!parent) return; + if (!opts) opts = {}; + if (!opts.enter) opts.enter = noop; + if (!opts.exit) opts.exit = noop; + // array of nodes if (!Array.isArray(parent)) { - traverseNode(parent, opts, scope); + traverseNode(parent, opts, scope, state); return; } for (var i = 0; i < parent.length; i++) { - traverseNode(parent[i], opts, scope); + traverseNode(parent[i], opts, scope, state); } } @@ -190,23 +208,25 @@ traverse.removeProperties = function (tree) { traverse.hasType = function (tree, type, blacklistTypes) { blacklistTypes = [].concat(blacklistTypes || []); - var has = false; - // the node we're searching in is blacklisted if (_.contains(blacklistTypes, tree.type)) return false; // the type we're looking for is the same as the passed node if (tree.type === type) return true; + var state = { + has: false + }; + traverse(tree, { blacklist: blacklistTypes, - enter: function (node) { + enter: function (node, parent, scope, context, state) { if (node.type === type) { - has = true; - this.skip(); + state.has = true; + context.skip(); } } - }); + }, null, state); - return has; + return state.has; }; diff --git a/lib/6to5/traverse/scope.js b/lib/6to5/traverse/scope.js index f02803c886..320728c059 100644 --- a/lib/6to5/traverse/scope.js +++ b/lib/6to5/traverse/scope.js @@ -159,8 +159,13 @@ Scope.prototype.getInfo = function () { // Program, Function - var variables if (t.isProgram(block) || t.isFunction(block)) { + var state = { + blockId: block.id, + add: add + }; + traverse(block, { - enter: function (node, parent, scope) { + enter: function (node, parent, scope, context, state) { if (t.isFor(node)) { _.each(FOR_KEYS, function (key) { var declar = node[key]; @@ -170,22 +175,22 @@ Scope.prototype.getInfo = function () { // this block is a function so we'll stop since none of the variables // declared within are accessible - if (t.isFunction(node)) return this.skip(); + if (t.isFunction(node)) return context.skip(); // function identifier doesn't belong to this scope - if (block.id && node === block.id) return; + if (state.blockId && node === state.blockId) return; if (t.isIdentifier(node) && t.isReferenced(node, parent) && !scope.has(node.name)) { - add(node, true); + state.add(node, true); } // we've ran into a declaration! // we'll let the BlockStatement scope deal with `let` declarations unless if (t.isDeclaration(node) && !t.isLet(node)) { - add(node); + state.add(node); } } - }, this); + }, this, state); } // Function - params, rest diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 7984bcfba8..9155d41bd4 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -575,4 +575,5 @@ t.isSpecifierDefault = function (specifier) { return t.isIdentifier(specifier.id) && specifier.id.name === "default"; }; -toFastProperties(t); \ No newline at end of file +toFastProperties(t); +toFastProperties(t.VISITOR_KEYS); \ No newline at end of file diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 66af587e4f..87ce7cebe5 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -142,12 +142,12 @@ exports.template = function (name, nodes, keepExpression) { if (!_.isEmpty(nodes)) { traverse(template, { - enter: function (node) { + enter: function (node, parent, scope, context, nodes) { if (t.isIdentifier(node) && _.has(nodes, node.name)) { return nodes[node.name]; } } - }); + }, null, nodes); } var node = template.body[0]; From dc6a862beca9e5394f36e2f86ca8ebfc5bef473d Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 17 Jan 2015 10:23:53 +1100 Subject: [PATCH 068/129] better clarification of rest parameter size construction comment --- .../transformation/transformers/es6-rest-parameters.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-rest-parameters.js b/lib/6to5/transformation/transformers/es6-rest-parameters.js index 68dfdd0ae8..bf6546a9c1 100644 --- a/lib/6to5/transformation/transformers/es6-rest-parameters.js +++ b/lib/6to5/transformation/transformers/es6-rest-parameters.js @@ -27,9 +27,11 @@ exports.Function = function (node, parent, file) { arrKey = t.binaryExpression("-", key, start); // we need to work out the size of the array that we're - // going to store all the rest parameters in, if there - // are less arguments than params then the array can be - // constructed with <1 which will cause an error + // going to store all the rest parameters + // + // we need to add a check to avoid constructing the array + // with <1 if there are less arguments than params as it'll + // cause an error arrLen = t.conditionalExpression( t.binaryExpression(">", len, start), t.binaryExpression("-", len, start), From 1f9ce96e431cec3749f03825b2a62e7b482b7914 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 17 Jan 2015 10:27:01 +1100 Subject: [PATCH 069/129] fix rest parameter array size error in rest parameter comment --- lib/6to5/transformation/transformers/es6-rest-parameters.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/6to5/transformation/transformers/es6-rest-parameters.js b/lib/6to5/transformation/transformers/es6-rest-parameters.js index bf6546a9c1..d0757690b4 100644 --- a/lib/6to5/transformation/transformers/es6-rest-parameters.js +++ b/lib/6to5/transformation/transformers/es6-rest-parameters.js @@ -30,7 +30,7 @@ exports.Function = function (node, parent, file) { // going to store all the rest parameters // // we need to add a check to avoid constructing the array - // with <1 if there are less arguments than params as it'll + // with <0 if there are less arguments than params as it'll // cause an error arrLen = t.conditionalExpression( t.binaryExpression(">", len, start), From 616640a128a4c6e31b1936dd4fa8dcbe0a088077 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Fri, 16 Jan 2015 01:14:28 -0500 Subject: [PATCH 070/129] Playground Proposal: Mallet operator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The mallet operator is similar to the current memoization operator, except it can be used outside of just objects. In Ruby, it’s almost the same as `a = a || b`. Note that only `nil` and `false` are falsey in Ruby. I’ve defined it as `== null`, though that could be changed to any JS falsey value. --- lib/6to5/transformation/transform.js | 1 + .../playground-mallet-operator.js | 86 +++++++++++++++++++ .../playground/mallet-operator/actual.js | 17 ++++ .../playground/mallet-operator/exec.js | 56 ++++++++++++ .../playground/mallet-operator/expected.js | 29 +++++++ 5 files changed, 189 insertions(+) create mode 100644 lib/6to5/transformation/transformers/playground-mallet-operator.js create mode 100644 test/fixtures/transformation/playground/mallet-operator/actual.js create mode 100644 test/fixtures/transformation/playground/mallet-operator/exec.js create mode 100644 test/fixtures/transformation/playground/mallet-operator/expected.js diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 351fe6e7d8..485ed26959 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -46,6 +46,7 @@ _.each({ specSetters: require("./transformers/spec-setters"), // playground + malletOperator: require("./transformers/playground-mallet-operator"), methodBinding: require("./transformers/playground-method-binding"), memoizationOperator: require("./transformers/playground-memoization-operator"), objectGetterMemoization: require("./transformers/playground-object-getter-memoization"), diff --git a/lib/6to5/transformation/transformers/playground-mallet-operator.js b/lib/6to5/transformation/transformers/playground-mallet-operator.js new file mode 100644 index 0000000000..c0e92bddf3 --- /dev/null +++ b/lib/6to5/transformation/transformers/playground-mallet-operator.js @@ -0,0 +1,86 @@ +var t = require("../../types"); + +var isMallet = function (node) { + var is = t.isAssignmentExpression(node) && node.operator === "||="; + if (is) { + var left = node.left; + if (!t.isMemberExpression(left) && !t.isIdentifier(left)) { + throw new Error("Expected type MemeberExpression or Identifier"); + } + return true; + } +}; + +var getObjRef = function (node, nodes, file, scope) { + var obj = node.object; + if (t.isIdentifier(obj)) return obj; + + var temp = scope.generateUidBasedOnNode(obj, file); + nodes.push(t.variableDeclaration("var", [ + t.variableDeclarator(temp, obj) + ])); + return temp; +}; + +var getPropRef = function (node, nodes, file, scope) { + var prop = node.property; + var key = t.toComputedKey(node, prop); + if (t.isLiteral(key)) return key; + + var temp = scope.generateUidBasedOnNode(prop, file); + nodes.push(t.variableDeclaration("var", [ + t.variableDeclarator(temp, prop) + ])); + return temp; +}; + +var buildAbsoluteRef = function (node, nodes, file, scope) { + if (t.isIdentifier(node)) return node; + + var obj = getObjRef(node, nodes, file, scope); + var prop = getPropRef(node, nodes, file, scope); + + var computed = node.computed || t.isLiteral(prop); + return t.memberExpression(obj, prop, computed); +}; + +var buildIsNull = function (node) { + return t.binaryExpression("==", node, t.literal(null)); +}; + +var buildAssignment = function (left, right) { + return t.assignmentExpression("=", left, right); +}; + +exports.ExpressionStatement = function (node, parent, file, scope) { + var expr = node.expression; + if (!isMallet(expr)) return; + + var nodes = []; + var left = buildAbsoluteRef(expr.left, nodes, file, scope); + + nodes.push(t.ifStatement( + buildIsNull(left), + t.expressionStatement(buildAssignment(left, expr.right)) + )); + + return nodes; +}; + +exports.AssignmentExpression = function (node, parent, file, scope) { + if (t.isExpressionStatement(parent)) return; + if (!isMallet(node)) return; + + var nodes = []; + var left = buildAbsoluteRef(node.left, nodes, file, scope); + + nodes.push(t.logicalExpression( + "&&", + buildIsNull(left), + buildAssignment(left, node.right) + )); + + nodes.push(left); + + return t.toSequenceExpression(nodes, scope); +}; diff --git a/test/fixtures/transformation/playground/mallet-operator/actual.js b/test/fixtures/transformation/playground/mallet-operator/actual.js new file mode 100644 index 0000000000..0c287674e4 --- /dev/null +++ b/test/fixtures/transformation/playground/mallet-operator/actual.js @@ -0,0 +1,17 @@ +obj ||= {}; + +obj.x ||= 2; + +console.log(obj.x ||= 2); + +obj.x.x ||= 2; + +console.log(obj.x.x ||= 2); + +obj[x()] ||= 2; + +console.log(obj[x()] ||= 2); + +obj[y()][x()] ||= 2; + +console.log(obj[y()][x()] ||= 2); diff --git a/test/fixtures/transformation/playground/mallet-operator/exec.js b/test/fixtures/transformation/playground/mallet-operator/exec.js new file mode 100644 index 0000000000..3fccbc42a2 --- /dev/null +++ b/test/fixtures/transformation/playground/mallet-operator/exec.js @@ -0,0 +1,56 @@ +var obj = {}; +obj.x ||= 2; +assert.equal(obj.x, 2); + +obj = {}; +assert.equal(obj.x ||= 2, 2); + +obj = { x: 1 }; +obj.x ||= 2; +assert.equal(obj.x, 1); + +obj = { x: 1 }; +assert.equal(obj.x ||= 2, 1); + +obj = { x: undefined } +obj.x ||= 2; +assert.equal(obj.x, 2); + +obj = { x: undefined } +assert.equal(obj.x ||= 2, 2); + +obj = { x: null } +obj.x ||= 2; +assert.equal(obj.x, 2); + +obj = { x: null } +assert.equal(obj.x ||= 2, 2); + +obj = { x: false } +obj.x ||= 2; +assert.equal(obj.x, false); + +obj = { x: false } +assert.equal(obj.x ||= 2, false); + +obj = undefined; +obj ||= 2; +assert.equal(obj, 2); + +obj = undefined; +assert.equal(obj ||= 2 , 2); + +obj = 1; +obj ||= 2; +assert.equal(obj, 1); + +obj = 1; +assert.equal(obj ||= 2 , 1); + +obj = null; +obj ||= 2; +assert.equal(obj, 2); + +obj = null; +assert.equal(obj ||= 2 , 2); + diff --git a/test/fixtures/transformation/playground/mallet-operator/expected.js b/test/fixtures/transformation/playground/mallet-operator/expected.js new file mode 100644 index 0000000000..efa81dfea9 --- /dev/null +++ b/test/fixtures/transformation/playground/mallet-operator/expected.js @@ -0,0 +1,29 @@ +"use strict"; + +var _obj$x2, _x2, _obj$y2, _x4; +if (obj == null) obj = {}; +if (obj.x == null) obj.x = 2; + + +console.log((obj.x == null && (obj.x = 2), obj.x)); + +var _obj$x = obj.x; +if (_obj$x.x == null) _obj$x.x = 2; + + +console.log((_obj$x2 = obj.x, _obj$x2.x == null && (_obj$x2.x = 2), _obj$x2.x)); + +var _x = x(); + +if (obj[_x] == null) obj[_x] = 2; + + +console.log((_x2 = x(), obj[_x2] == null && (obj[_x2] = 2), obj[_x2])); + +var _obj$y = obj[y()]; +var _x3 = x(); + +if (_obj$y[_x3] == null) _obj$y[_x3] = 2; + + +console.log((_obj$y2 = obj[y()], _x4 = x(), _obj$y2[_x4] == null && (_obj$y2[_x4] = 2), _obj$y2[_x4])); From ac5a6da1faf4c64540ab28911f6ad5cc561b4992 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Fri, 16 Jan 2015 19:25:28 -0500 Subject: [PATCH 071/129] Use all falsey values --- .../playground-mallet-operator.js | 8 ++-- .../playground/mallet-operator/exec.js | 38 ++++++++++++++++++- .../playground/mallet-operator/expected.js | 18 ++++----- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/lib/6to5/transformation/transformers/playground-mallet-operator.js b/lib/6to5/transformation/transformers/playground-mallet-operator.js index c0e92bddf3..3e8fc83bf5 100644 --- a/lib/6to5/transformation/transformers/playground-mallet-operator.js +++ b/lib/6to5/transformation/transformers/playground-mallet-operator.js @@ -44,8 +44,8 @@ var buildAbsoluteRef = function (node, nodes, file, scope) { return t.memberExpression(obj, prop, computed); }; -var buildIsNull = function (node) { - return t.binaryExpression("==", node, t.literal(null)); +var buildIsFalsey = function (node) { + return t.unaryExpression("!", node, true); }; var buildAssignment = function (left, right) { @@ -60,7 +60,7 @@ exports.ExpressionStatement = function (node, parent, file, scope) { var left = buildAbsoluteRef(expr.left, nodes, file, scope); nodes.push(t.ifStatement( - buildIsNull(left), + buildIsFalsey(left), t.expressionStatement(buildAssignment(left, expr.right)) )); @@ -76,7 +76,7 @@ exports.AssignmentExpression = function (node, parent, file, scope) { nodes.push(t.logicalExpression( "&&", - buildIsNull(left), + buildIsFalsey(left), buildAssignment(left, node.right) )); diff --git a/test/fixtures/transformation/playground/mallet-operator/exec.js b/test/fixtures/transformation/playground/mallet-operator/exec.js index 3fccbc42a2..e82d33882e 100644 --- a/test/fixtures/transformation/playground/mallet-operator/exec.js +++ b/test/fixtures/transformation/playground/mallet-operator/exec.js @@ -28,10 +28,24 @@ assert.equal(obj.x ||= 2, 2); obj = { x: false } obj.x ||= 2; -assert.equal(obj.x, false); +assert.equal(obj.x, 2); obj = { x: false } -assert.equal(obj.x ||= 2, false); +assert.equal(obj.x ||= 2, 2); + +obj = { x: '' } +obj.x ||= 2; +assert.equal(obj.x, 2); + +obj = { x: '' } +assert.equal(obj.x ||= 2, 2); + +obj = { x: 0 } +obj.x ||= 2; +assert.equal(obj.x, 2); + +obj = { x: 0 } +assert.equal(obj.x ||= 2, 2); obj = undefined; obj ||= 2; @@ -54,3 +68,23 @@ assert.equal(obj, 2); obj = null; assert.equal(obj ||= 2 , 2); +obj = false; +obj ||= 2; +assert.equal(obj, 2); + +obj = false; +assert.equal(obj ||= 2 , 2); + +obj = ''; +obj ||= 2; +assert.equal(obj, 2); + +obj = ''; +assert.equal(obj ||= 2 , 2); + +obj = 0; +obj ||= 2; +assert.equal(obj, 2); + +obj = 0; +assert.equal(obj ||= 2 , 2); diff --git a/test/fixtures/transformation/playground/mallet-operator/expected.js b/test/fixtures/transformation/playground/mallet-operator/expected.js index efa81dfea9..28b7439356 100644 --- a/test/fixtures/transformation/playground/mallet-operator/expected.js +++ b/test/fixtures/transformation/playground/mallet-operator/expected.js @@ -1,29 +1,29 @@ "use strict"; var _obj$x2, _x2, _obj$y2, _x4; -if (obj == null) obj = {}; -if (obj.x == null) obj.x = 2; +if (!obj) obj = {}; +if (!obj.x) obj.x = 2; -console.log((obj.x == null && (obj.x = 2), obj.x)); +console.log((!obj.x && (obj.x = 2), obj.x)); var _obj$x = obj.x; -if (_obj$x.x == null) _obj$x.x = 2; +if (!_obj$x.x) _obj$x.x = 2; -console.log((_obj$x2 = obj.x, _obj$x2.x == null && (_obj$x2.x = 2), _obj$x2.x)); +console.log((_obj$x2 = obj.x, !_obj$x2.x && (_obj$x2.x = 2), _obj$x2.x)); var _x = x(); -if (obj[_x] == null) obj[_x] = 2; +if (!obj[_x]) obj[_x] = 2; -console.log((_x2 = x(), obj[_x2] == null && (obj[_x2] = 2), obj[_x2])); +console.log((_x2 = x(), !obj[_x2] && (obj[_x2] = 2), obj[_x2])); var _obj$y = obj[y()]; var _x3 = x(); -if (_obj$y[_x3] == null) _obj$y[_x3] = 2; +if (!_obj$y[_x3]) _obj$y[_x3] = 2; -console.log((_obj$y2 = obj[y()], _x4 = x(), _obj$y2[_x4] == null && (_obj$y2[_x4] = 2), _obj$y2[_x4])); +console.log((_obj$y2 = obj[y()], _x4 = x(), !_obj$y2[_x4] && (_obj$y2[_x4] = 2), _obj$y2[_x4])); From ff9511d4357e63e14366ec32c6d7f1b37a4c5602 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Sat, 17 Jan 2015 05:03:23 +0300 Subject: [PATCH 072/129] Proof of concept of how traversal would look like with `state` parameter --- .../transformation/helpers/name-method.js | 36 +++++---- lib/6to5/transformation/modules/_default.js | 70 +++++++++-------- .../transformers/_alias-functions.js | 78 ++++++++++--------- lib/6to5/util.js | 16 ++-- 4 files changed, 105 insertions(+), 95 deletions(-) diff --git a/lib/6to5/transformation/helpers/name-method.js b/lib/6to5/transformation/helpers/name-method.js index 340bf7e27d..36cdbddaea 100644 --- a/lib/6to5/transformation/helpers/name-method.js +++ b/lib/6to5/transformation/helpers/name-method.js @@ -2,6 +2,24 @@ var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); +var traverser = { + enter: function (node, parent, scope, context, state) { + // check if this node is an identifier that matches the same as our function id + if (!t.isIdentifier(node, { name: state.id })) return; + + // check if this node is the one referenced + if (!t.isReferenced(node, parent)) return; + + // check that we don't have a local variable declared as that removes the need + // for the wrapper + var localDeclar = scope.get(state.id, true); + if (localDeclar !== state.outerDeclar) return; + + state.selfReference = true; + context.stop(); + } +}; + module.exports = function (node, file, scope) { var key = t.toComputedKey(node, node.key); if (!t.isLiteral(key)) return node; // we can't set a function id with this @@ -15,23 +33,7 @@ module.exports = function (node, file, scope) { outerDeclar: scope.get(id, true), }; - traverse(node, { - enter: function (node, parent, scope, context, state) { - // check if this node is an identifier that matches the same as our function id - if (!t.isIdentifier(node, { name: state.id })) return; - - // check if this node is the one referenced - if (!t.isReferenced(node, parent)) return; - - // check that we don't have a local variable declared as that removes the need - // for the wrapper - var localDeclar = scope.get(state.id, true); - if (localDeclar !== state.outerDeclar) return; - - state.selfReference = true; - context.stop(); - } - }, scope, state); + traverse(node, traverser, scope, state); if (state.selfReference) { node.value = util.template("property-method-assignment-wrapper", { diff --git a/lib/6to5/transformation/modules/_default.js b/lib/6to5/transformation/modules/_default.js index 68040ac8b8..1485d27f03 100644 --- a/lib/6to5/transformation/modules/_default.js +++ b/lib/6to5/transformation/modules/_default.js @@ -16,35 +16,51 @@ function DefaultFormatter(file) { //this.checkCollisions(); } +var exportsTraverser = { + enter: function (node, parent, scope, context, localExports) { + var declar = node && node.declaration; + if (t.isExportDeclaration(node) && declar && t.isStatement(declar)) { + _.extend(localExports, t.getIds(declar, true)); + } + } +}; + DefaultFormatter.prototype.getLocalExports = function () { var localExports = {}; - - traverse(this.file.ast, { - enter: function (node, parent, scope, context, localExports) { - var declar = node && node.declaration; - if (t.isExportDeclaration(node) && declar && t.isStatement(declar)) { - _.extend(localExports, t.getIds(declar, true)); - } - } - }, null, localExports); - + traverse(this.file.ast, exportsTraverser, null, localExports); return localExports; }; +var importsTraverser = { + enter: function (node, parent, scope, context, localImports) { + if (t.isImportDeclaration(node)) { + _.extend(localImports, t.getIds(node, true)); + } + } +}; + DefaultFormatter.prototype.getLocalImports = function () { var localImports = {}; - - traverse(this.file.ast, { - enter: function (node, parent, scope, context, localImports) { - if (t.isImportDeclaration(node)) { - _.extend(localImports, t.getIds(node, true)); - } - } - }, null, localImports); - + traverse(this.file.ast, importsTraverser, null, localImports); return localImports; }; +var collissionsTraverser = { + enter: function (node, parent, scope, context, check) { + if (t.isAssignmentExpression(node)) { + + var left = node.left; + if (t.isMemberExpression(left)) { + while (left.object) left = left.object; + } + + check(left); + } else if (t.isDeclaration(node)) { + _.each(t.getIds(node, true), check); + } + } +}; + DefaultFormatter.prototype.checkCollisions = function () { // todo: all check export collissions @@ -61,21 +77,7 @@ DefaultFormatter.prototype.checkCollisions = function () { } }; - traverse(file.ast, { - enter: function (node, parent, scope, context, check) { - if (t.isAssignmentExpression(node)) { - - var left = node.left; - if (t.isMemberExpression(left)) { - while (left.object) left = left.object; - } - - check(left); - } else if (t.isDeclaration(node)) { - _.each(t.getIds(node, true), check); - } - } - }, null, check); + traverse(file.ast, collissionsTraverser, null, check); }; DefaultFormatter.prototype.remapExportAssignment = function (node) { diff --git a/lib/6to5/transformation/transformers/_alias-functions.js b/lib/6to5/transformation/transformers/_alias-functions.js index bc52a45ee8..1d2bf585f6 100644 --- a/lib/6to5/transformation/transformers/_alias-functions.js +++ b/lib/6to5/transformation/transformers/_alias-functions.js @@ -1,6 +1,46 @@ var traverse = require("../../traverse"); var t = require("../../types"); +var functionChildrenTraverser = { + enter: function (node, parent, scope, context, state) { + if (t.isFunction(node) && !node._aliasFunction) { + return context.skip(); + } + + if (node._ignoreAliasFunctions) return context.skip(); + + var getId; + + if (t.isIdentifier(node) && node.name === "arguments") { + getId = state.getArgumentsId; + } else if (t.isThisExpression(node)) { + getId = state.getThisId; + } else { + return; + } + + if (t.isReferenced(node, parent)) return getId(); + } +}; + +var functionTraverser = { + enter: function (node, parent, scope, context, state) { + if (!node._aliasFunction) { + if (t.isFunction(node)) { + // stop traversal of this node as it'll be hit again by this transformer + return context.skip(); + } else { + return; + } + } + + // traverse all child nodes of this function and find `arguments` and `this` + traverse(node, functionChildrenTraverser, null, state); + + return context.skip(); + } +}; + var go = function (getBody, node, file, scope) { var argumentsId; var thisId; @@ -16,43 +56,7 @@ var go = function (getBody, node, file, scope) { // traverse the function and find all alias functions so we can alias // `arguments` and `this` if necessary - traverse(node, { - enter: function (node, parent, scope, context, state) { - if (!node._aliasFunction) { - if (t.isFunction(node)) { - // stop traversal of this node as it'll be hit again by this transformer - return context.skip(); - } else { - return; - } - } - - // traverse all child nodes of this function and find `arguments` and `this` - traverse(node, { - enter: function (node, parent, scope, context, state) { - if (t.isFunction(node) && !node._aliasFunction) { - return context.skip(); - } - - if (node._ignoreAliasFunctions) return context.skip(); - - var getId; - - if (t.isIdentifier(node) && node.name === "arguments") { - getId = state.getArgumentsId; - } else if (t.isThisExpression(node)) { - getId = state.getThisId; - } else { - return; - } - - if (t.isReferenced(node, parent)) return getId(); - } - }, null, state); - - return context.skip(); - } - }, null, state); + traverse(node, functionTraverser, null, state); var body; diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 87ce7cebe5..626d06305d 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -129,6 +129,14 @@ exports.buildDefineProperties = function (mutatorMap) { return objExpr; }; +var templateTraverser = { + enter: function (node, parent, scope, context, nodes) { + if (t.isIdentifier(node) && _.has(nodes, node.name)) { + return nodes[node.name]; + } + } +}; + exports.template = function (name, nodes, keepExpression) { var template = exports.templates[name]; if (!template) throw new ReferenceError("unknown template " + name); @@ -141,13 +149,7 @@ exports.template = function (name, nodes, keepExpression) { template = _.cloneDeep(template); if (!_.isEmpty(nodes)) { - traverse(template, { - enter: function (node, parent, scope, context, nodes) { - if (t.isIdentifier(node) && _.has(nodes, node.name)) { - return nodes[node.name]; - } - } - }, null, nodes); + traverse(template, templateTraverser, null, nodes); } var node = template.body[0]; From 4ec701fc44f5fd71c794a24222024c5632cf0cdc Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 17 Jan 2015 17:44:16 +1100 Subject: [PATCH 073/129] ignore variable declarations inside loop head in newline generation - fixes #519 --- lib/6to5/generation/generators/statements.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/6to5/generation/generators/statements.js b/lib/6to5/generation/generators/statements.js index 087f37fbc7..715858e870 100644 --- a/lib/6to5/generation/generators/statements.js +++ b/lib/6to5/generation/generators/statements.js @@ -169,16 +169,18 @@ exports.VariableDeclaration = function (node, print, parent) { var inits = 0; var noInits = 0; - for (var i = 0; i < node.declarations.length; i++) { - if (node.declarations[i].init) { - inits++; - } else { - noInits++; + if (!t.isFor(parent)) { + for (var i = 0; i < node.declarations.length; i++) { + if (node.declarations[i].init) { + inits++; + } else { + noInits++; + } } } var sep = ","; - if (inits > noInits) { // more inits than noinits + if (inits > noInits) { // more inits than no inits so let's add a newline sep += "\n" + util.repeat(node.kind.length + 1); } else { sep += " "; From eb9cd95d5a0f0035f4c91717a872ad58c2f0f307 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 17 Jan 2015 17:54:04 +1100 Subject: [PATCH 074/129] update for head tests --- .../es6-for-of-loose/identifier/expected.js | 5 +---- .../transformation/es6-for-of-loose/let/expected.js | 5 +---- .../es6-for-of-loose/multiple/expected.js | 10 ++-------- .../transformation/es6-for-of-loose/var/expected.js | 5 +---- .../es6-rest-parameters/arrow-functions/expected.js | 4 +--- .../es6-rest-parameters/multiple/expected.js | 8 ++------ .../es6-rest-parameters/single/expected.js | 8 ++------ 7 files changed, 10 insertions(+), 35 deletions(-) diff --git a/test/fixtures/transformation/es6-for-of-loose/identifier/expected.js b/test/fixtures/transformation/es6-for-of-loose/identifier/expected.js index 4a7ea3a6a5..cb83442b64 100644 --- a/test/fixtures/transformation/es6-for-of-loose/identifier/expected.js +++ b/test/fixtures/transformation/es6-for-of-loose/identifier/expected.js @@ -1,9 +1,6 @@ "use strict"; -for (var _iterator = arr, - _isArray = Array.isArray(_iterator), - _i = 0, - _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { +for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { if (_isArray) { if (_i >= _iterator.length) break; i = _iterator[_i++]; diff --git a/test/fixtures/transformation/es6-for-of-loose/let/expected.js b/test/fixtures/transformation/es6-for-of-loose/let/expected.js index 94848fe0c1..261cb6bf7b 100644 --- a/test/fixtures/transformation/es6-for-of-loose/let/expected.js +++ b/test/fixtures/transformation/es6-for-of-loose/let/expected.js @@ -1,9 +1,6 @@ "use strict"; -for (var _iterator = arr, - _isArray = Array.isArray(_iterator), - _i = 0, - _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { +for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var i = undefined; if (_isArray) { if (_i >= _iterator.length) break; diff --git a/test/fixtures/transformation/es6-for-of-loose/multiple/expected.js b/test/fixtures/transformation/es6-for-of-loose/multiple/expected.js index a20dee268b..46d8e3f0aa 100644 --- a/test/fixtures/transformation/es6-for-of-loose/multiple/expected.js +++ b/test/fixtures/transformation/es6-for-of-loose/multiple/expected.js @@ -1,9 +1,6 @@ "use strict"; -for (var _iterator = arr, - _isArray = Array.isArray(_iterator), - _i = 0, - _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { +for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var i; if (_isArray) { if (_i >= _iterator.length) break; @@ -15,10 +12,7 @@ for (var _iterator = arr, } } -for (var _iterator2 = numbers, - _isArray2 = Array.isArray(_iterator2), - _i2 = 0, - _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { +for (var _iterator2 = numbers, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { var i; if (_isArray2) { if (_i2 >= _iterator2.length) break; diff --git a/test/fixtures/transformation/es6-for-of-loose/var/expected.js b/test/fixtures/transformation/es6-for-of-loose/var/expected.js index 3695bf3454..d5dd9060d5 100644 --- a/test/fixtures/transformation/es6-for-of-loose/var/expected.js +++ b/test/fixtures/transformation/es6-for-of-loose/var/expected.js @@ -1,9 +1,6 @@ "use strict"; -for (var _iterator = arr, - _isArray = Array.isArray(_iterator), - _i = 0, - _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { +for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var i; if (_isArray) { if (_i >= _iterator.length) break; diff --git a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js index fa875fd363..4b72d8d4cf 100644 --- a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js @@ -1,9 +1,7 @@ "use strict"; var concat = function () { - for (var _len = arguments.length, - arrs = Array(_len), - _key = 0; _key < _len; _key++) { + for (var _len = arguments.length, arrs = Array(_len), _key = 0; _key < _len; _key++) { arrs[_key] = arguments[_key]; } }; diff --git a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js index dc6e082ce6..2967aef64a 100644 --- a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js @@ -1,17 +1,13 @@ "use strict"; var t = function (f) { - for (var _len = arguments.length, - items = Array(_len > 1 ? _len - 1 : 0), - _key = 1; _key < _len; _key++) { + for (var _len = arguments.length, items = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { items[_key - 1] = arguments[_key]; } }; function t(f) { - for (var _len2 = arguments.length, - items = Array(_len2 > 1 ? _len2 - 1 : 0), - _key2 = 1; _key2 < _len2; _key2++) { + for (var _len2 = arguments.length, items = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { items[_key2 - 1] = arguments[_key2]; } } diff --git a/test/fixtures/transformation/es6-rest-parameters/single/expected.js b/test/fixtures/transformation/es6-rest-parameters/single/expected.js index d6f9e1a5fe..580c7706dd 100644 --- a/test/fixtures/transformation/es6-rest-parameters/single/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/single/expected.js @@ -1,17 +1,13 @@ "use strict"; var t = function () { - for (var _len = arguments.length, - items = Array(_len), - _key = 0; _key < _len; _key++) { + for (var _len = arguments.length, items = Array(_len), _key = 0; _key < _len; _key++) { items[_key] = arguments[_key]; } }; function t() { - for (var _len2 = arguments.length, - items = Array(_len2), - _key2 = 0; _key2 < _len2; _key2++) { + for (var _len2 = arguments.length, items = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { items[_key2] = arguments[_key2]; } } From 8919873ea19e2e73871f7b870431e9784906e4a5 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 17 Jan 2015 18:26:14 +1100 Subject: [PATCH 075/129] clean up react/jsx transformer --- lib/6to5/transformation/transformers/react.js | 183 ++++++++++-------- 1 file changed, 100 insertions(+), 83 deletions(-) diff --git a/lib/6to5/transformation/transformers/react.js b/lib/6to5/transformation/transformers/react.js index 41b1ae4e14..4dc53db287 100644 --- a/lib/6to5/transformation/transformers/react.js +++ b/lib/6to5/transformation/transformers/react.js @@ -37,8 +37,8 @@ exports.XJSAttribute = { } }; -var isTag = function(tagName) { - return (/^[a-z]|\-/).test(tagName); +var isCompatTag = function(tagName) { + return /^[a-z]|\-/.test(tagName); }; exports.XJSOpeningElement = { @@ -55,7 +55,7 @@ exports.XJSOpeningElement = { } if (!reactCompat) { - if (tagName && isTag(tagName)) { + if (tagName && isCompatTag(tagName)) { args.push(t.literal(tagName)); } else { args.push(tagExpr); @@ -64,48 +64,7 @@ exports.XJSOpeningElement = { var attribs = node.attributes; if (attribs.length) { - var _props = []; - var objs = []; - - // so basically in order to support spread elements we - // loop over all the attributes, breaking on spreads - // we then push a new object containing all prior attributes - // to an array for later processing - - var pushProps = function () { - if (!_props.length) return; - - objs.push(t.objectExpression(_props)); - _props = []; - }; - - while (attribs.length) { - var prop = attribs.shift(); - if (t.isXJSSpreadAttribute(prop)) { - pushProps(); - objs.push(prop.argument); - } else { - _props.push(prop); - } - } - - pushProps(); - - if (objs.length === 1) { - // only one object - attribs = objs[0]; - } else { - // looks like we have multiple objects - if (!t.isObjectExpression(objs[0])) { - objs.unshift(t.objectExpression([])); - } - - // spread it - attribs = t.callExpression( - t.memberExpression(t.identifier("React"), t.identifier("__spread")), - objs - ); - } + attribs = buildXJSOpeningElementAttributes(attribs); } else { attribs = t.literal(null); } @@ -113,7 +72,7 @@ exports.XJSOpeningElement = { args.push(attribs); if (reactCompat) { - if (tagName && isTag(tagName)) { + if (tagName && isCompatTag(tagName)) { return t.callExpression( t.memberExpression( t.memberExpression(t.identifier("React"), t.identifier("DOM")), @@ -131,6 +90,55 @@ exports.XJSOpeningElement = { } }; +/** + * The logic for this is quite terse. It's because we need to + * support spread elements. We loop over all attributes, + * breaking on spreads, we then push a new object containg + * all prior attributes to an array for later processing. + */ + +var buildXJSOpeningElementAttributes = function (attribs) { + var _props = []; + var objs = []; + + var pushProps = function () { + if (!_props.length) return; + + objs.push(t.objectExpression(_props)); + _props = []; + }; + + while (attribs.length) { + var prop = attribs.shift(); + if (t.isXJSSpreadAttribute(prop)) { + pushProps(); + objs.push(prop.argument); + } else { + _props.push(prop); + } + } + + pushProps(); + + if (objs.length === 1) { + // only one object + attribs = objs[0]; + } else { + // looks like we have multiple objects + if (!t.isObjectExpression(objs[0])) { + objs.unshift(t.objectExpression([])); + } + + // spread it + attribs = t.callExpression( + t.memberExpression(t.identifier("React"), t.identifier("__spread")), + objs + ); + } + + return attribs; +}; + exports.XJSElement = { exit: function (node) { var callExpr = node.openingElement; @@ -139,32 +147,7 @@ exports.XJSElement = { var child = node.children[i]; if (t.isLiteral(child) && _.isString(child.value)) { - var lines = child.value.split(/\r\n|\n|\r/); - - for (var i2 = 0; i2 < lines.length; i2++) { - var line = lines[i2]; - - var isFirstLine = i2 === 0; - var isLastLine = i2 === lines.length - 1; - - // replace rendered whitespace tabs with spaces - var trimmedLine = line.replace(/\t/g, " "); - - // trim whitespace touching a newline - if (!isFirstLine) { - trimmedLine = trimmedLine.replace(/^[ ]+/, ""); - } - - // trim whitespace touching an endline - if (!isLastLine) { - trimmedLine = trimmedLine.replace(/[ ]+$/, ""); - } - - if (trimmedLine) { - callExpr.arguments.push(t.literal(trimmedLine)); - } - } - + cleanXJSElementLiteralChild(child, callExpr.arguments); continue; } else if (t.isXJSEmptyExpression(child)) { continue; @@ -181,31 +164,65 @@ exports.XJSElement = { } }; +var cleanXJSElementLiteralChild = function (child, args) { + var lines = child.value.split(/\r\n|\n|\r/); + + for (var i = 0; i < lines.length; i++) { + var line = lines[i]; + + var isFirstLine = i === 0; + var isLastLine = i === lines.length - 1; + + // replace rendered whitespace tabs with spaces + var trimmedLine = line.replace(/\t/g, " "); + + // trim whitespace touching a newline + if (!isFirstLine) { + trimmedLine = trimmedLine.replace(/^[ ]+/, ""); + } + + // trim whitespace touching an endline + if (!isLastLine) { + trimmedLine = trimmedLine.replace(/[ ]+$/, ""); + } + + if (trimmedLine) { + args.push(t.literal(trimmedLine)); + } + } +}; + // display names -var addDisplayName = function (id, call) { - if (!call || !t.isCallExpression(call)) return; +var isCreateClass = function (call) { + if (!call || !t.isCallExpression(call)) return false; var callee = call.callee; - if (!t.isMemberExpression(callee)) return; + if (!t.isMemberExpression(callee)) return false; - // not React + // not React call member object var obj = callee.object; - if (!t.isIdentifier(obj, { name: "React" })) return; + if (!t.isIdentifier(obj, { name: "React" })) return false; - // not createClass + // not createClass call member property var prop = callee.property; - if (!t.isIdentifier(prop, { name: "createClass" })) return; + if (!t.isIdentifier(prop, { name: "createClass" })) return false; - // no arguments + // no call arguments var args = call.arguments; - if (args.length !== 1) return; + if (args.length !== 1) return false; - // not an object + // first call arg is not an object var first = args[0]; if (!t.isObjectExpression(first)) return; - var props = first.properties; + return true; +}; + +var addDisplayName = function (id, call) { + if (!isCreateClass(call)) return; + + var props = call.arguments[0].properties; var safe = true; for (var i = 0; i < props.length; i++) { From 3eb4d5b46619eab58225580d59ada17c0b20d889 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 17 Jan 2015 18:53:40 +1100 Subject: [PATCH 076/129] fix up styling of #518 --- lib/6to5/to-fast-properties.js | 1 + lib/6to5/types/index.js | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/6to5/to-fast-properties.js b/lib/6to5/to-fast-properties.js index 341a451d03..a7499fdff5 100644 --- a/lib/6to5/to-fast-properties.js +++ b/lib/6to5/to-fast-properties.js @@ -5,6 +5,7 @@ * Use %HasFastProperties(obj) and --allow-natives-syntax to check whether * a particular object already has fast properties. */ + module.exports = function toFastProperties(obj) { /*jshint -W027*/ function f() {} diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 9155d41bd4..fb56afd02d 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -1,6 +1,6 @@ +var toFastProperties = require("../to-fast-properties"); var esutils = require("esutils"); var _ = require("lodash"); -var toFastProperties = require("../to-fast-properties"); var t = exports; @@ -15,6 +15,7 @@ t.NATIVE_TYPE_NAMES = ["Array", "Object", "Number", "Boolean", "Date", "Array", * @param {String} type * @param {Boolean?} skipAliasCheck */ + function registerType(type, skipAliasCheck) { var is = t["is" + type] = function (node, opts) { return t.is(type, node, opts, skipAliasCheck); @@ -65,6 +66,7 @@ _.each(t.FLIPPED_ALIAS_KEYS, function (types, type) { * @param {Boolean?} skipAliasCheck * @returns {Boolean} isOfType */ + t.is = function (type, node, opts, skipAliasCheck) { if (!node) return; @@ -545,10 +547,10 @@ t.inheritsComments = function (child, parent) { */ t.inherits = function (child, parent) { - child.loc = parent.loc; - child.end = parent.end; child.range = parent.range; child.start = parent.start; + child.loc = parent.loc; + child.end = parent.end; t.inheritsComments(child, parent); return child; }; @@ -576,4 +578,4 @@ t.isSpecifierDefault = function (specifier) { }; toFastProperties(t); -toFastProperties(t.VISITOR_KEYS); \ No newline at end of file +toFastProperties(t.VISITOR_KEYS); From 9098852897ccf97c4c4faff5c9f5ceb096023626 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 17 Jan 2015 18:53:49 +1100 Subject: [PATCH 077/129] abstract out classes super replace --- .../transformation/helpers/replace-supers.js | 244 ++++++++++++++++++ .../transformers/es6-classes.js | 204 +-------------- 2 files changed, 251 insertions(+), 197 deletions(-) create mode 100644 lib/6to5/transformation/helpers/replace-supers.js diff --git a/lib/6to5/transformation/helpers/replace-supers.js b/lib/6to5/transformation/helpers/replace-supers.js new file mode 100644 index 0000000000..4ca7009be4 --- /dev/null +++ b/lib/6to5/transformation/helpers/replace-supers.js @@ -0,0 +1,244 @@ +module.exports = ReplaceSupers; + +var traverse = require("../../traverse"); +var t = require("../../types"); + +/** + * Description + * + * @param {Object} methodNode + * @param {Object} className + * @param {Object} superName + * @param {Boolean} isLoose + * @param {File} file + */ + +function ReplaceSupers(methodNode, className, superName, isLoose, file) { + this.topLevelThisReference = null; + this.methodNode = methodNode; + this.className = className; + this.superName = superName; + this.isLoose = isLoose; + this.file = file; +} + +/** + * Gets a node representing the super class value of the named property. + * + * @example + * + * _get(Object.getPrototypeOf(CLASS.prototype), "METHOD", this) + * + * @param {Node} property + * @param {boolean} isStatic + * @param {boolean} isComputed + * + * @returns {Node} + */ + +ReplaceSupers.prototype.superProperty = function (property, isStatic, isComputed, thisExpression) { + return t.callExpression( + this.file.addHelper("get"), + [ + t.callExpression( + t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")), + [ + isStatic ? this.className : t.memberExpression(this.className, t.identifier("prototype")) + ] + ), + isComputed ? property : t.literal(property.name), + thisExpression + ] + ); +}; + +/** + * Description + * + * @param {Object} node + * @param {Object} id + * @param {Object} parent + * @returns {Object} + */ + +ReplaceSupers.prototype.looseSuperProperty = function (methodNode, id, parent) { + var methodName = methodNode.key; + var superName = this.superName || t.identifier("Function"); + + if (parent.property === id) { + return; + } else if (t.isCallExpression(parent, { callee: id })) { + // super(); -> ClassName.prototype.MethodName.call(this); + parent.arguments.unshift(t.thisExpression()); + + if (methodName.name === "constructor") { + // constructor() { super(); } + return t.memberExpression(superName, t.identifier("call")); + } else { + id = superName; + + // foo() { super(); } + if (!methodNode.static) { + id = t.memberExpression(id, t.identifier("prototype")); + } + + id = t.memberExpression(id, methodName, methodNode.computed); + return t.memberExpression(id, t.identifier("call")); + } + } else if (t.isMemberExpression(parent) && !methodNode.static) { + // super.test -> ClassName.prototype.test + return t.memberExpression(superName, t.identifier("prototype")); + } else { + return superName; + } +}; + +/** + * Description + */ + +ReplaceSupers.prototype.replace = function () { + this.traverseLevel(this.methodNode.value, true); +}; + +/** + * Description + * + * @param {Object} node + * @param {Boolean} topLevel + */ + +ReplaceSupers.prototype.traverseLevel = function (node, topLevel) { + var self = this; + + traverse(node, { + enter: function (node, parent, scope, context) { + if (t.isFunction(node) && !t.isArrowFunctionExpression(node)) { + // we need to call traverseLevel again so we're context aware + self.traverseLevel(node, false); + return context.skip(); + } + + if (t.isProperty(node, { method: true }) || t.isMethodDefinition(node)) { + // break on object methods + return context.skip(); + } + + var getThisReference = function () { + if (topLevel) { + // top level so `this` is the instance + return t.thisExpression(); + } else { + // not in the top level so we need to create a reference + return self.getThisReference(); + } + }; + + var callback = self.specHandle; + if (self.isLoose) callback = self.looseHandle; + return callback.call(self, getThisReference, node, parent); + } + }); +}; + +/** + * Description + */ + +ReplaceSupers.prototype.getThisReference = function () { + if (this.topLevelThisReference) { + return this.topLevelThisReference; + } else { + var ref = this.topLevelThisReference = this.file.generateUidIdentifier("this"); + this.methodNode.body.body.unshift(t.variableDeclaration("var", [ + t.variableDeclarator(this.topLevelThisReference, t.thisExpression()) + ])); + return ref; + } +}; + +/** + * Description + * + * @param {Function} getThisReference + * @param {Object} node + * @param {Object} parent + */ + +ReplaceSupers.prototype.looseHandle = function (getThisReference, node, parent) { + if (t.isIdentifier(node, { name: "super" })) { + return this.looseSuperProperty(this.methodNode, node, parent); + } else if (t.isCallExpression(node)) { + var callee = node.callee; + if (!t.isMemberExpression(callee)) return; + if (callee.object.name !== "super") return; + + // super.test(); -> ClassName.prototype.MethodName.call(this); + t.appendToMemberExpression(callee, t.identifier("call")); + node.arguments.unshift(getThisReference()); + } +}; + +/** + * Description + * + * @param {Function} getThisReference + * @param {Object} node + * @param {Object} parent + */ + +ReplaceSupers.prototype.specHandle = function (getThisReference, node, parent) { + var methodNode = this.methodNode; + var property; + var computed; + var args; + + if (t.isIdentifier(node, { name: "super" })) { + if (!(t.isMemberExpression(parent) && !parent.computed && parent.property === node)) { + throw this.file.errorWithNode(node, "illegal use of bare super"); + } + } else if (t.isCallExpression(node)) { + var callee = node.callee; + if (t.isIdentifier(callee, { name: "super" })) { + // super(); -> _get(Object.getPrototypeOf(ClassName), "MethodName", this).call(this); + property = methodNode.key; + computed = methodNode.computed; + args = node.arguments; + } else { + if (!t.isMemberExpression(callee)) return; + if (callee.object.name !== "super") return; + + // super.test(); -> _get(Object.getPrototypeOf(ClassName.prototype), "test", this).call(this); + property = callee.property; + computed = callee.computed; + args = node.arguments; + } + } else if (t.isMemberExpression(node)) { + if (!t.isIdentifier(node.object, { name: "super" })) return; + + // super.name; -> _get(Object.getPrototypeOf(ClassName.prototype), "name", this); + property = node.property; + computed = node.computed; + } + + if (!property) return; + + var thisReference = getThisReference(); + var superProperty = this.superProperty(property, methodNode.static, computed, thisReference); + if (args) { + if (args.length === 1 && t.isSpreadElement(args[0])) { + // super(...arguments); + return t.callExpression( + t.memberExpression(superProperty, t.identifier("apply")), + [thisReference, args[0].argument] + ); + } else { + return t.callExpression( + t.memberExpression(superProperty, t.identifier("call")), + [thisReference].concat(args) + ); + } + } else { + return superProperty; + } +}; diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 18f24c4031..1935260324 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -1,7 +1,8 @@ -var nameMethod = require("../helpers/name-method"); -var traverse = require("../../traverse"); -var util = require("../../util"); -var t = require("../../types"); +var ReplaceSupers = require("../helpers/replace-supers"); +var nameMethod = require("../helpers/name-method"); +var traverse = require("../../traverse"); +var util = require("../../util"); +var t = require("../../types"); exports.ClassDeclaration = function (node, parent, scope, context, file) { return new Class(node, file, scope, true).run(); @@ -135,7 +136,8 @@ Class.prototype.buildBody = function () { for (var i = 0; i < classBody.length; i++) { var node = classBody[i]; if (t.isMethodDefinition(node)) { - this.replaceSuperReferences(node); + var replaceSupers = new ReplaceSupers(node, this.className, this.superName, this.isLoose, this.file); + replaceSupers.replace(); if (node.key.name === "constructor") { this.pushConstructor(node); @@ -223,198 +225,6 @@ Class.prototype.pushMethod = function (node) { util.pushMutatorMap(mutatorMap, methodName, kind, node.computed, node); }; -/** - * Gets a node representing the super class value of the named property. - * - * @example - * - * _get(Object.getPrototypeOf(CLASS.prototype), "METHOD", this) - * - * @param {Node} property - * @param {boolean} isStatic - * @param {boolean} isComputed - * - * @returns {Node} - */ - -Class.prototype.superProperty = function (property, isStatic, isComputed, thisExpression) { - return t.callExpression( - this.file.addHelper("get"), - [ - t.callExpression( - t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")), - [ - isStatic ? this.className : t.memberExpression(this.className, t.identifier("prototype")) - ] - ), - isComputed ? property : t.literal(property.name), - thisExpression - ] - ); -}; - -/** - * Description - * - * @param {Object} node - * @param {Object} id - * @param {Object} parent - * @returns {Object} - */ - -Class.prototype.looseSuperProperty = function (methodNode, id, parent) { - var methodName = methodNode.key; - var superName = this.superName || t.identifier("Function"); - - if (parent.property === id) { - return; - } else if (t.isCallExpression(parent, { callee: id })) { - // super(); -> ClassName.prototype.MethodName.call(this); - parent.arguments.unshift(t.thisExpression()); - - if (methodName.name === "constructor") { - // constructor() { super(); } - return t.memberExpression(superName, t.identifier("call")); - } else { - id = superName; - - // foo() { super(); } - if (!methodNode.static) { - id = t.memberExpression(id, t.identifier("prototype")); - } - - id = t.memberExpression(id, methodName, methodNode.computed); - return t.memberExpression(id, t.identifier("call")); - } - } else if (t.isMemberExpression(parent) && !methodNode.static) { - // super.test -> ClassName.prototype.test - return t.memberExpression(superName, t.identifier("prototype")); - } else { - return superName; - } -}; - -/** - * Replace all `super` references with a reference to our `superClass`. - * - * @param {Node} methodNode MethodDefinition - */ - -Class.prototype.replaceSuperReferences = function (methodNode) { - var method = methodNode.value; - var self = this; - - var topLevelThisReference; - - traverseLevel(method, true); - - if (topLevelThisReference) { - method.body.body.unshift(t.variableDeclaration("var", [ - t.variableDeclarator(topLevelThisReference, t.thisExpression()) - ])); - } - - function traverseLevel(node, topLevel) { - traverse(node, { - enter: function (node, parent, scope, context) { - if (t.isFunction(node) && !t.isArrowFunctionExpression(node)) { - // we need to call traverseLevel again so we're context aware - traverseLevel(node, false); - return context.skip(); - } - - if (t.isProperty(node, { method: true }) || t.isMethodDefinition(node)) { - // break on object methods - return context.skip(); - } - - var getThisReference = function () { - if (topLevel) { - // top level so `this` is the instance - return t.thisExpression(); - } else { - // not in the top level so we need to create a reference - return topLevelThisReference = topLevelThisReference || self.file.generateUidIdentifier("this"); - } - }; - - var callback = specHandle; - if (self.isLoose) callback = looseHandle; - return callback(getThisReference, node, parent); - } - }); - } - - function looseHandle(getThisReference, node, parent) { - if (t.isIdentifier(node, { name: "super" })) { - return self.looseSuperProperty(methodNode, node, parent); - } else if (t.isCallExpression(node)) { - var callee = node.callee; - if (!t.isMemberExpression(callee)) return; - if (callee.object.name !== "super") return; - - // super.test(); -> ClassName.prototype.MethodName.call(this); - t.appendToMemberExpression(callee, t.identifier("call")); - node.arguments.unshift(getThisReference()); - } - } - - function specHandle(getThisReference, node, parent) { - var property; - var computed; - var args; - - if (t.isIdentifier(node, { name: "super" })) { - if (!(t.isMemberExpression(parent) && !parent.computed && parent.property === node)) { - throw self.file.errorWithNode(node, "illegal use of bare super"); - } - } else if (t.isCallExpression(node)) { - var callee = node.callee; - if (t.isIdentifier(callee, { name: "super" })) { - // super(); -> _get(Object.getPrototypeOf(ClassName), "MethodName", this).call(this); - property = methodNode.key; - computed = methodNode.computed; - args = node.arguments; - } else { - if (!t.isMemberExpression(callee)) return; - if (callee.object.name !== "super") return; - - // super.test(); -> _get(Object.getPrototypeOf(ClassName.prototype), "test", this).call(this); - property = callee.property; - computed = callee.computed; - args = node.arguments; - } - } else if (t.isMemberExpression(node)) { - if (!t.isIdentifier(node.object, { name: "super" })) return; - - // super.name; -> _get(Object.getPrototypeOf(ClassName.prototype), "name", this); - property = node.property; - computed = node.computed; - } - - if (!property) return; - - var thisReference = getThisReference(); - var superProperty = self.superProperty(property, methodNode.static, computed, thisReference); - if (args) { - if (args.length === 1 && t.isSpreadElement(args[0])) { - // super(...arguments); - return t.callExpression( - t.memberExpression(superProperty, t.identifier("apply")), - [thisReference, args[0].argument] - ); - } else { - return t.callExpression( - t.memberExpression(superProperty, t.identifier("call")), - [thisReference].concat(args) - ); - } - } else { - return superProperty; - } - } -}; - /** * Replace the constructor body of our class. * From b54800234f321a8b27fb37b1e604f3229e73fcc6 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 17 Jan 2015 22:56:49 +1100 Subject: [PATCH 078/129] clean up conditional assignment operator transformers --- ...itional-assignment-operator-transformer.js | 75 +++++++++++++ .../transformation/helpers/explode-unary.js | 0 .../playground-mallet-operator.js | 96 +++-------------- .../playground-memoization-operator.js | 100 +++--------------- .../playground/mallet-operator/actual.js | 17 --- .../playground/mallet-operator/exec.js | 16 +++ .../playground/mallet-operator/expected.js | 29 ----- .../memoization-assignment-operator/actual.js | 13 --- .../memoization-assignment-operator/exec.js | 16 +++ .../expected.js | 27 ----- 10 files changed, 137 insertions(+), 252 deletions(-) create mode 100644 lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js create mode 100644 lib/6to5/transformation/helpers/explode-unary.js delete mode 100644 test/fixtures/transformation/playground/mallet-operator/actual.js delete mode 100644 test/fixtures/transformation/playground/mallet-operator/expected.js delete mode 100644 test/fixtures/transformation/playground/memoization-assignment-operator/actual.js delete mode 100644 test/fixtures/transformation/playground/memoization-assignment-operator/expected.js diff --git a/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js b/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js new file mode 100644 index 0000000000..a3bad77c73 --- /dev/null +++ b/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js @@ -0,0 +1,75 @@ +var t = require("../../types"); + +module.exports = function (opts) { + var getObjRef = function (node, nodes, file, scope) { + var obj = node.object; + var temp = scope.generateUidBasedOnNode(obj, file); + nodes.push(t.variableDeclaration("var", [ + t.variableDeclarator(temp, obj) + ])); + return temp; + }; + + var getPropRef = function (node, nodes, file, scope) { + var prop = node.property; + var key = t.toComputedKey(node, prop); + if (t.isLiteral(key)) return key; + + var temp = scope.generateUidBasedOnNode(prop, file); + nodes.push(t.variableDeclaration("var", [ + t.variableDeclarator(temp, prop) + ])); + return temp; + }; + + var buildAbsoluteRef = function (node, nodes, file, scope) { + if (t.isIdentifier(node)) return node; + + var obj = getObjRef(node, nodes, file, scope); + var prop = getPropRef(node, nodes, file, scope); + + var computed = node.computed || t.isLiteral(prop); + return t.memberExpression(obj, prop, computed); + }; + + var buildAssignment = function (left, right) { + return t.assignmentExpression("=", left, right); + }; + + var exports = {}; + + exports.ExpressionStatement = function (node, parent, scope, context, file) { + var expr = node.expression; + if (!opts.is(expr)) return; + + var nodes = []; + var left = buildAbsoluteRef(expr.left, nodes, file, scope); + + nodes.push(t.ifStatement( + opts.build(left, file), + t.expressionStatement(buildAssignment(left, expr.right)) + )); + + return nodes; + }; + + exports.AssignmentExpression = function (node, parent, scope, context, file) { + if (t.isExpressionStatement(parent)) return; + if (!opts.is(node)) return; + + var nodes = []; + var left = buildAbsoluteRef(node.left, nodes, file, scope); + + nodes.push(t.logicalExpression( + "&&", + opts.build(left, file), + buildAssignment(left, node.right) + )); + + nodes.push(left); + + return t.toSequenceExpression(nodes, scope); + }; + + return exports; +}; diff --git a/lib/6to5/transformation/helpers/explode-unary.js b/lib/6to5/transformation/helpers/explode-unary.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/6to5/transformation/transformers/playground-mallet-operator.js b/lib/6to5/transformation/transformers/playground-mallet-operator.js index 3e8fc83bf5..16333c82e5 100644 --- a/lib/6to5/transformation/transformers/playground-mallet-operator.js +++ b/lib/6to5/transformation/transformers/playground-mallet-operator.js @@ -1,86 +1,18 @@ var t = require("../../types"); -var isMallet = function (node) { - var is = t.isAssignmentExpression(node) && node.operator === "||="; - if (is) { - var left = node.left; - if (!t.isMemberExpression(left) && !t.isIdentifier(left)) { - throw new Error("Expected type MemeberExpression or Identifier"); +module.exports = require("../helpers/build-conditional-assignment-operator-transformer")({ + is: function (node) { + var is = t.isAssignmentExpression(node) && node.operator === "||="; + if (is) { + var left = node.left; + if (!t.isMemberExpression(left) && !t.isIdentifier(left)) { + throw new Error("Expected type MemeberExpression or Identifier"); + } + return true; } - return true; + }, + + build: function (node) { + return t.unaryExpression("!", node, true); } -}; - -var getObjRef = function (node, nodes, file, scope) { - var obj = node.object; - if (t.isIdentifier(obj)) return obj; - - var temp = scope.generateUidBasedOnNode(obj, file); - nodes.push(t.variableDeclaration("var", [ - t.variableDeclarator(temp, obj) - ])); - return temp; -}; - -var getPropRef = function (node, nodes, file, scope) { - var prop = node.property; - var key = t.toComputedKey(node, prop); - if (t.isLiteral(key)) return key; - - var temp = scope.generateUidBasedOnNode(prop, file); - nodes.push(t.variableDeclaration("var", [ - t.variableDeclarator(temp, prop) - ])); - return temp; -}; - -var buildAbsoluteRef = function (node, nodes, file, scope) { - if (t.isIdentifier(node)) return node; - - var obj = getObjRef(node, nodes, file, scope); - var prop = getPropRef(node, nodes, file, scope); - - var computed = node.computed || t.isLiteral(prop); - return t.memberExpression(obj, prop, computed); -}; - -var buildIsFalsey = function (node) { - return t.unaryExpression("!", node, true); -}; - -var buildAssignment = function (left, right) { - return t.assignmentExpression("=", left, right); -}; - -exports.ExpressionStatement = function (node, parent, file, scope) { - var expr = node.expression; - if (!isMallet(expr)) return; - - var nodes = []; - var left = buildAbsoluteRef(expr.left, nodes, file, scope); - - nodes.push(t.ifStatement( - buildIsFalsey(left), - t.expressionStatement(buildAssignment(left, expr.right)) - )); - - return nodes; -}; - -exports.AssignmentExpression = function (node, parent, file, scope) { - if (t.isExpressionStatement(parent)) return; - if (!isMallet(node)) return; - - var nodes = []; - var left = buildAbsoluteRef(node.left, nodes, file, scope); - - nodes.push(t.logicalExpression( - "&&", - buildIsFalsey(left), - buildAssignment(left, node.right) - )); - - nodes.push(left); - - return t.toSequenceExpression(nodes, scope); -}; +}); diff --git a/lib/6to5/transformation/transformers/playground-memoization-operator.js b/lib/6to5/transformation/transformers/playground-memoization-operator.js index 1bd6d004ee..06c5f32405 100644 --- a/lib/6to5/transformation/transformers/playground-memoization-operator.js +++ b/lib/6to5/transformation/transformers/playground-memoization-operator.js @@ -1,88 +1,20 @@ var t = require("../../types"); -var isMemo = function (node) { - var is = t.isAssignmentExpression(node) && node.operator === "?="; - if (is) t.assertMemberExpression(node.left); - return is; -}; +module.exports = require("../helpers/build-conditional-assignment-operator-transformer")({ + is: function (node) { + var is = t.isAssignmentExpression(node) && node.operator === "?="; + if (is) t.assertMemberExpression(node.left); + return is; + }, -var getPropRef = function (nodes, member, file, scope) { - var prop = member.property; - var key = t.toComputedKey(member, member.property); - if (t.isLiteral(key)) { - return key; - } else { - var temp = scope.generateUidBasedOnNode(prop, file); - nodes.push(t.variableDeclaration("var", [ - t.variableDeclarator(temp, prop) - ])); - return temp; + build: function (node, file) { + return t.unaryExpression( + "!", + t.callExpression( + t.memberExpression(file.addHelper("has-own"), t.identifier("call")), + [node.object, node.property] + ), + true + ); } -}; - -var getObjRef = function (nodes, obj, file, scope) { - var temp = scope.generateUidBasedOnNode(obj, file); - nodes.push(t.variableDeclaration("var", [ - t.variableDeclarator(temp, obj) - ])); - return temp; -}; - -var buildHasOwn = function (obj, prop, file) { - return t.unaryExpression( - "!", - t.callExpression( - t.memberExpression(file.addHelper("has-own"), t.identifier("call")), - [obj, prop] - ), - true - ); -}; - -var buildAbsoluteRef = function (left, obj, prop) { - var computed = left.computed || t.isLiteral(prop); - return t.memberExpression(obj, prop, computed); -}; - -var buildAssignment = function (expr, obj, prop) { - return t.assignmentExpression("=", buildAbsoluteRef(expr.left, obj, prop), expr.right); -}; - -exports.ExpressionStatement = function (node, parent, scope, context, file) { - var expr = node.expression; - if (!isMemo(expr)) return; - - var nodes = []; - - var left = expr.left; - var obj = getObjRef(nodes, left.object, file, scope); - var prop = getPropRef(nodes, left, file, scope); - - nodes.push(t.ifStatement( - buildHasOwn(obj, prop, file), - t.expressionStatement(buildAssignment(expr, obj, prop)) - )); - - return nodes; -}; - -exports.AssignmentExpression = function (node, parent, scope, context, file) { - if (t.isExpressionStatement(parent)) return; - if (!isMemo(node)) return; - - var nodes = []; - - var left = node.left; - var obj = getObjRef(nodes, left.object, file, scope); - var prop = getPropRef(nodes, left, file, scope); - - nodes.push(t.logicalExpression( - "&&", - buildHasOwn(obj, prop, file), - buildAssignment(node, obj, prop) - )); - - nodes.push(buildAbsoluteRef(left, obj, prop)); - - return t.toSequenceExpression(nodes, scope); -}; +}); diff --git a/test/fixtures/transformation/playground/mallet-operator/actual.js b/test/fixtures/transformation/playground/mallet-operator/actual.js deleted file mode 100644 index 0c287674e4..0000000000 --- a/test/fixtures/transformation/playground/mallet-operator/actual.js +++ /dev/null @@ -1,17 +0,0 @@ -obj ||= {}; - -obj.x ||= 2; - -console.log(obj.x ||= 2); - -obj.x.x ||= 2; - -console.log(obj.x.x ||= 2); - -obj[x()] ||= 2; - -console.log(obj[x()] ||= 2); - -obj[y()][x()] ||= 2; - -console.log(obj[y()][x()] ||= 2); diff --git a/test/fixtures/transformation/playground/mallet-operator/exec.js b/test/fixtures/transformation/playground/mallet-operator/exec.js index e82d33882e..b784b844bd 100644 --- a/test/fixtures/transformation/playground/mallet-operator/exec.js +++ b/test/fixtures/transformation/playground/mallet-operator/exec.js @@ -88,3 +88,19 @@ assert.equal(obj, 2); obj = 0; assert.equal(obj ||= 2 , 2); + +var calls = 0; +var q = { q: 3 }; +var o = { + get p() { + calls++; + return q; + } +}; + +o.p.q ||= 2; +assert.equal(1, calls); +o.p.f ||= 2; +assert.equal(2, calls); +assert.equal(3, o.p.q); +assert.equal(2, o.p.f); diff --git a/test/fixtures/transformation/playground/mallet-operator/expected.js b/test/fixtures/transformation/playground/mallet-operator/expected.js deleted file mode 100644 index 28b7439356..0000000000 --- a/test/fixtures/transformation/playground/mallet-operator/expected.js +++ /dev/null @@ -1,29 +0,0 @@ -"use strict"; - -var _obj$x2, _x2, _obj$y2, _x4; -if (!obj) obj = {}; -if (!obj.x) obj.x = 2; - - -console.log((!obj.x && (obj.x = 2), obj.x)); - -var _obj$x = obj.x; -if (!_obj$x.x) _obj$x.x = 2; - - -console.log((_obj$x2 = obj.x, !_obj$x2.x && (_obj$x2.x = 2), _obj$x2.x)); - -var _x = x(); - -if (!obj[_x]) obj[_x] = 2; - - -console.log((_x2 = x(), !obj[_x2] && (obj[_x2] = 2), obj[_x2])); - -var _obj$y = obj[y()]; -var _x3 = x(); - -if (!_obj$y[_x3]) _obj$y[_x3] = 2; - - -console.log((_obj$y2 = obj[y()], _x4 = x(), !_obj$y2[_x4] && (_obj$y2[_x4] = 2), _obj$y2[_x4])); diff --git a/test/fixtures/transformation/playground/memoization-assignment-operator/actual.js b/test/fixtures/transformation/playground/memoization-assignment-operator/actual.js deleted file mode 100644 index b2d1521e2c..0000000000 --- a/test/fixtures/transformation/playground/memoization-assignment-operator/actual.js +++ /dev/null @@ -1,13 +0,0 @@ -var obj = {}; - -obj.x ?= 2; - -console.log(obj.x ?= 2); - -obj[x()] ?= 2; - -console.log(obj[x()] ?= 2); - -obj[y()][x()] ?= 2; - -console.log(obj[y()][x()] ?= 2); diff --git a/test/fixtures/transformation/playground/memoization-assignment-operator/exec.js b/test/fixtures/transformation/playground/memoization-assignment-operator/exec.js index 3e528c641d..716aca45a7 100644 --- a/test/fixtures/transformation/playground/memoization-assignment-operator/exec.js +++ b/test/fixtures/transformation/playground/memoization-assignment-operator/exec.js @@ -18,3 +18,19 @@ assert.equal(obj.x, undefined); obj = { x: undefined } assert.equal(obj.x ?= 2, undefined); + +var calls = 0; +var q = { q: 3 }; +var o = { + get p() { + calls++; + return q; + } +}; + +o.p.q ?= 2; +assert.equal(1, calls); +o.p.f ?= 2; +assert.equal(2, calls); +assert.equal(3, o.p.q); +assert.equal(2, o.p.f); diff --git a/test/fixtures/transformation/playground/memoization-assignment-operator/expected.js b/test/fixtures/transformation/playground/memoization-assignment-operator/expected.js deleted file mode 100644 index fbb64ac4f8..0000000000 --- a/test/fixtures/transformation/playground/memoization-assignment-operator/expected.js +++ /dev/null @@ -1,27 +0,0 @@ -"use strict"; - -var _obj2, _obj4, _x2, _obj$y2, _x4; -var _hasOwn = Object.prototype.hasOwnProperty; -var obj = {}; - -var _obj = obj; -if (!_hasOwn.call(_obj, "x")) _obj.x = 2; - - -console.log((_obj2 = obj, !_hasOwn.call(_obj2, "x") && (_obj2.x = 2), _obj2.x)); - -var _obj3 = obj; -var _x = x(); - -if (!_hasOwn.call(_obj3, _x)) _obj3[_x] = 2; - - -console.log((_obj4 = obj, _x2 = x(), !_hasOwn.call(_obj4, _x2) && (_obj4[_x2] = 2), _obj4[_x2])); - -var _obj$y = obj[y()]; -var _x3 = x(); - -if (!_hasOwn.call(_obj$y, _x3)) _obj$y[_x3] = 2; - - -console.log((_obj$y2 = obj[y()], _x4 = x(), !_hasOwn.call(_obj$y2, _x4) && (_obj$y2[_x4] = 2), _obj$y2[_x4])); From 67029ac8e85d35173fdef3d0b66f9ff54608e702 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 00:37:51 +1100 Subject: [PATCH 079/129] better helpers for assignment operators --- ...-binary-assignment-operator-transformer.js | 43 +++++++++++++ ...itional-assignment-operator-transformer.js | 61 +++++-------------- .../helpers/explode-assignable-expression.js | 49 +++++++++++++++ .../transformation/helpers/explode-unary.js | 0 .../es7-exponentiation-operator.js | 20 +++--- .../playground-mallet-operator.js | 9 +-- .../playground-memoization-operator.js | 5 +- 7 files changed, 123 insertions(+), 64 deletions(-) create mode 100644 lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js create mode 100644 lib/6to5/transformation/helpers/explode-assignable-expression.js delete mode 100644 lib/6to5/transformation/helpers/explode-unary.js diff --git a/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js b/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js new file mode 100644 index 0000000000..59e9f53107 --- /dev/null +++ b/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js @@ -0,0 +1,43 @@ +var explode = require("./explode-assignable-expression"); +var t = require("../../types"); + +module.exports = function (exports, opts) { + var isAssignment = function (node) { + return node.operator === opts.operator + "="; + }; + + var buildAssignment = function (left, right) { + return t.assignmentExpression("=", left, right); + }; + + exports.ExpressionStatement = function (node, parent, scope, context, file) { + var expr = node.expression; + if (!isAssignment(expr)) return; + + var nodes = []; + var exploded = explode(expr.left, nodes, file, scope); + + nodes.push(t.expressionStatement( + buildAssignment(exploded.ref, opts.build(exploded.uid, expr.right)) + )); + + return nodes; + }; + + exports.AssignmentExpression = function (node, parent, scope, context, file) { + if (t.isExpressionStatement(parent)) return; + if (!isAssignment(node)) return; + + var nodes = []; + var exploded = explode(node.left, nodes, file, scope); + nodes.push(opts.build(exploded.uid, node.right)); + nodes.push(exploded.ref); + + return t.toSequenceExpression(nodes, scope); + }; + + exports.BinaryExpression = function (node) { + if (node.operator !== opts.operator) return; + return opts.build(node.left, node.right); + }; +}; diff --git a/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js b/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js index a3bad77c73..154588db85 100644 --- a/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js +++ b/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js @@ -1,53 +1,22 @@ -var t = require("../../types"); - -module.exports = function (opts) { - var getObjRef = function (node, nodes, file, scope) { - var obj = node.object; - var temp = scope.generateUidBasedOnNode(obj, file); - nodes.push(t.variableDeclaration("var", [ - t.variableDeclarator(temp, obj) - ])); - return temp; - }; - - var getPropRef = function (node, nodes, file, scope) { - var prop = node.property; - var key = t.toComputedKey(node, prop); - if (t.isLiteral(key)) return key; - - var temp = scope.generateUidBasedOnNode(prop, file); - nodes.push(t.variableDeclaration("var", [ - t.variableDeclarator(temp, prop) - ])); - return temp; - }; - - var buildAbsoluteRef = function (node, nodes, file, scope) { - if (t.isIdentifier(node)) return node; - - var obj = getObjRef(node, nodes, file, scope); - var prop = getPropRef(node, nodes, file, scope); - - var computed = node.computed || t.isLiteral(prop); - return t.memberExpression(obj, prop, computed); - }; +var explode = require("./explode-assignable-expression"); +var t = require("../../types"); +module.exports = function (exports, opts) { var buildAssignment = function (left, right) { return t.assignmentExpression("=", left, right); }; - var exports = {}; - exports.ExpressionStatement = function (node, parent, scope, context, file) { var expr = node.expression; - if (!opts.is(expr)) return; + if (!opts.is(expr, file)) return; var nodes = []; - var left = buildAbsoluteRef(expr.left, nodes, file, scope); + + var exploded = explode(expr.left, nodes, file, scope); nodes.push(t.ifStatement( - opts.build(left, file), - t.expressionStatement(buildAssignment(left, expr.right)) + opts.build(exploded.uid, file), + t.expressionStatement(buildAssignment(exploded.ref, expr.right)) )); return nodes; @@ -55,21 +24,19 @@ module.exports = function (opts) { exports.AssignmentExpression = function (node, parent, scope, context, file) { if (t.isExpressionStatement(parent)) return; - if (!opts.is(node)) return; + if (!opts.is(node, file)) return; - var nodes = []; - var left = buildAbsoluteRef(node.left, nodes, file, scope); + var nodes = []; + var exploded = explode(node.left, nodes, file, scope); nodes.push(t.logicalExpression( "&&", - opts.build(left, file), - buildAssignment(left, node.right) + opts.build(exploded.uid, file), + buildAssignment(exploded.ref, node.right) )); - nodes.push(left); + nodes.push(exploded.ref); return t.toSequenceExpression(nodes, scope); }; - - return exports; }; diff --git a/lib/6to5/transformation/helpers/explode-assignable-expression.js b/lib/6to5/transformation/helpers/explode-assignable-expression.js new file mode 100644 index 0000000000..8b0e47d879 --- /dev/null +++ b/lib/6to5/transformation/helpers/explode-assignable-expression.js @@ -0,0 +1,49 @@ +var t = require("../../types"); + +var getObjRef = function (node, nodes, file, scope) { + var ref; + if (t.isIdentifier(node)) { + ref = node; + } else if (t.isMemberExpression(node)) { + ref = node.object; + } else { + throw new Error("We can't explode this node type " + node.type); + } + + var temp = scope.generateUidBasedOnNode(ref, file); + nodes.push(t.variableDeclaration("var", [ + t.variableDeclarator(temp, ref) + ])); + return temp; +}; + +var getPropRef = function (node, nodes, file, scope) { + var prop = node.property; + var key = t.toComputedKey(node, prop); + if (t.isLiteral(key)) return key; + + var temp = scope.generateUidBasedOnNode(prop, file); + nodes.push(t.variableDeclaration("var", [ + t.variableDeclarator(temp, prop) + ])); + return temp; +}; + +module.exports = function (node, nodes, file, scope) { + var obj = getObjRef(node, nodes, file, scope); + var ref, uid; + + if (t.isIdentifier(node)) { + ref = node; + uid = obj; + } else { + var prop = getPropRef(node, nodes, file, scope); + var computed = node.computed || t.isLiteral(prop); + uid = ref = t.memberExpression(obj, prop, computed); + } + + return { + uid: uid, + ref: ref + }; +}; diff --git a/lib/6to5/transformation/helpers/explode-unary.js b/lib/6to5/transformation/helpers/explode-unary.js deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/lib/6to5/transformation/transformers/es7-exponentiation-operator.js b/lib/6to5/transformation/transformers/es7-exponentiation-operator.js index f5825967ee..1310700c18 100644 --- a/lib/6to5/transformation/transformers/es7-exponentiation-operator.js +++ b/lib/6to5/transformation/transformers/es7-exponentiation-operator.js @@ -2,17 +2,15 @@ exports.experimental = true; -var t = require("../../types"); -var pow = t.memberExpression(t.identifier("Math"), t.identifier("pow")); +var build = require("../helpers/build-binary-assignment-operator-transformer"); +var t = require("../../types"); -exports.AssignmentExpression = function (node) { - if (node.operator !== "**=") return; - node.operator = "="; - node.right = t.callExpression(pow, [node.left, node.right]); -}; +var MATH_POW = t.memberExpression(t.identifier("Math"), t.identifier("pow")); -exports.BinaryExpression = function (node) { - if (node.operator !== "**") return; +build(exports, { + operator: "**", - return t.callExpression(pow, [node.left, node.right]); -}; + build: function (left, right) { + return t.callExpression(MATH_POW, [left, right]); + } +}); diff --git a/lib/6to5/transformation/transformers/playground-mallet-operator.js b/lib/6to5/transformation/transformers/playground-mallet-operator.js index 16333c82e5..559cbde1ed 100644 --- a/lib/6to5/transformation/transformers/playground-mallet-operator.js +++ b/lib/6to5/transformation/transformers/playground-mallet-operator.js @@ -1,12 +1,13 @@ -var t = require("../../types"); +var build = require("../helpers/build-conditional-assignment-operator-transformer"); +var t = require("../../types"); -module.exports = require("../helpers/build-conditional-assignment-operator-transformer")({ - is: function (node) { +build(exports, { + is: function (node, file) { var is = t.isAssignmentExpression(node) && node.operator === "||="; if (is) { var left = node.left; if (!t.isMemberExpression(left) && !t.isIdentifier(left)) { - throw new Error("Expected type MemeberExpression or Identifier"); + throw file.errorWithNode(left, "Expected type MemeberExpression or Identifier"); } return true; } diff --git a/lib/6to5/transformation/transformers/playground-memoization-operator.js b/lib/6to5/transformation/transformers/playground-memoization-operator.js index 06c5f32405..822692bcee 100644 --- a/lib/6to5/transformation/transformers/playground-memoization-operator.js +++ b/lib/6to5/transformation/transformers/playground-memoization-operator.js @@ -1,6 +1,7 @@ -var t = require("../../types"); +var build = require("../helpers/build-conditional-assignment-operator-transformer"); +var t = require("../../types"); -module.exports = require("../helpers/build-conditional-assignment-operator-transformer")({ +build(exports, { is: function (node) { var is = t.isAssignmentExpression(node) && node.operator === "?="; if (is) t.assertMemberExpression(node.left); From f298cd3f0f1d8a5208241e808b4b8e81217fd46a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 01:15:35 +1100 Subject: [PATCH 080/129] allow single idents in binary assignment operator transformer --- .../build-binary-assignment-operator-transformer.js | 2 +- .../helpers/explode-assignable-expression.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js b/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js index 59e9f53107..96743c8bdc 100644 --- a/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js +++ b/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js @@ -15,7 +15,7 @@ module.exports = function (exports, opts) { if (!isAssignment(expr)) return; var nodes = []; - var exploded = explode(expr.left, nodes, file, scope); + var exploded = explode(expr.left, nodes, file, scope, true); nodes.push(t.expressionStatement( buildAssignment(exploded.ref, opts.build(exploded.uid, expr.right)) diff --git a/lib/6to5/transformation/helpers/explode-assignable-expression.js b/lib/6to5/transformation/helpers/explode-assignable-expression.js index 8b0e47d879..eecc696b78 100644 --- a/lib/6to5/transformation/helpers/explode-assignable-expression.js +++ b/lib/6to5/transformation/helpers/explode-assignable-expression.js @@ -29,8 +29,14 @@ var getPropRef = function (node, nodes, file, scope) { return temp; }; -module.exports = function (node, nodes, file, scope) { - var obj = getObjRef(node, nodes, file, scope); +module.exports = function (node, nodes, file, scope, allowedSingleIdent) { + var obj; + if (t.isIdentifier(node) && allowedSingleIdent) { + obj = node; + } else { + obj = getObjRef(node, nodes, file, scope); + } + var ref, uid; if (t.isIdentifier(node)) { From 529544ce0ea4017586baf57a1cc4b2dda3c51432 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 01:16:09 +1100 Subject: [PATCH 081/129] use generateUidIdentifier instead of generateUid --- lib/6to5/transformation/transformers/es6-destructuring.js | 5 ++--- lib/6to5/transformation/transformers/es6-let-scoping.js | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-destructuring.js b/lib/6to5/transformation/transformers/es6-destructuring.js index bcd04ba4b2..8b1f5ae882 100644 --- a/lib/6to5/transformation/transformers/es6-destructuring.js +++ b/lib/6to5/transformation/transformers/es6-destructuring.js @@ -249,10 +249,9 @@ exports.AssignmentExpression = function (node, parent, scope, context, file) { if (parent.type === "ExpressionStatement") return; if (!t.isPattern(node.left)) return; - var tempName = file.generateUid("temp", scope); - var ref = t.identifier(tempName); + var ref = file.generateUidIdentifier("temp", scope); scope.push({ - key: tempName, + key: ref.name, id: ref }); diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index 7c589851cd..d6ccf9078f 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -226,7 +226,7 @@ LetScoping.prototype.getInfo = function () { if (has && has !== id) { // there's a variable with this exact name in an upper scope so we need // to generate a new name - opts.duplicates[key] = id.name = file.generateUid(key, scope); + opts.duplicates[key] = id.name = file.generateUidIdentifier(key, scope).name; opts.hasDuplicates = true; } }; From 621e66e9ac1038df5666e7922c65b6970a6b99d3 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 02:35:08 +1100 Subject: [PATCH 082/129] add private declaration and comprehension expression support to scope tracking and id building --- lib/6to5/traverse/scope.js | 8 ++++++-- lib/6to5/types/alias-keys.json | 3 ++- lib/6to5/types/index.js | 5 ++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/6to5/traverse/scope.js b/lib/6to5/traverse/scope.js index 320728c059..c77c75dcb3 100644 --- a/lib/6to5/traverse/scope.js +++ b/lib/6to5/traverse/scope.js @@ -137,8 +137,6 @@ Scope.prototype.getInfo = function () { var node = block[key]; if (t.isLet(node)) add(node); }); - - block = block.body; } // Program, BlockStatement - let variables @@ -156,6 +154,12 @@ Scope.prototype.getInfo = function () { add(block.param); } + // ComprehensionExpression - blocks + + if (t.isComprehensionExpression(block)) { + add(block); + } + // Program, Function - var variables if (t.isProgram(block) || t.isFunction(block)) { diff --git a/lib/6to5/types/alias-keys.json b/lib/6to5/types/alias-keys.json index e07ecc429d..cc80d7290c 100644 --- a/lib/6to5/types/alias-keys.json +++ b/lib/6to5/types/alias-keys.json @@ -16,6 +16,7 @@ "VariableDeclaration": ["Statement", "Declaration"], "ExportDeclaration": ["Statement", "Declaration"], "ImportDeclaration": ["Statement", "Declaration"], + "PrivateDeclaration": ["Statement", "Declaration"], "ArrowFunctionExpression": ["Scope", "Function", "Expression"], "FunctionDeclaration": ["Statement", "Declaration", "Scope", "Function"], @@ -52,7 +53,7 @@ "BindFunctionExpression": ["Expression"], "BindMemberExpression": ["Expression"], "CallExpression": ["Expression"], - "ComprehensionExpression": ["Expression"], + "ComprehensionExpression": ["Expression", "Scope"], "ConditionalExpression": ["Expression"], "Identifier": ["Expression"], "Literal": ["Expression"], diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index fb56afd02d..85c89cb427 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -472,10 +472,13 @@ t.getIds.nodes = { ClassDeclaration: ["id"], MemeberExpression: ["object"], SpreadElement: ["argument"], - Property: ["value"] + Property: ["value"], + ComprehensionBlock: ["left"] }; t.getIds.arrays = { + PrivateDeclaration: ["declarations"], + ComprehensionExpression: ["blocks"], ExportDeclaration: ["specifiers", "declaration"], ImportDeclaration: ["specifiers"], VariableDeclaration: ["declarations"], From d0dc972840fb22716f39986df9f827b9dd1c14b7 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 02:35:19 +1100 Subject: [PATCH 083/129] fix up formatting in traverse --- lib/6to5/traverse/index.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/6to5/traverse/index.js b/lib/6to5/traverse/index.js index 7307a7665d..a9c5c381b3 100644 --- a/lib/6to5/traverse/index.js +++ b/lib/6to5/traverse/index.js @@ -104,17 +104,18 @@ TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent, var node = obj[key]; // type is blacklisted - if (opts.blacklist && opts.blacklist.indexOf(node.type) > -1) - return; + if (opts.blacklist && opts.blacklist.indexOf(node.type) > -1) return; var ourScope = scope; - if (t.isScope(node)) + if (t.isScope(node)) { ourScope = new Scope(node, scope); + } node = this.enterNode(obj, key, node, opts.enter, parent, ourScope, state); - if (this.didSkip) + if (this.didSkip) { return this.didStop; + } traverseNode(node, opts, ourScope, state); this.exitNode(obj, key, node, opts.exit, parent, ourScope, state); @@ -155,8 +156,9 @@ function traverseNode(node, opts, scope, state) { var context = new TraversalContext(); for (var j = 0; j < keys.length; j++) { - if (context.visit(node, keys[j], opts, scope, state)) + if (context.visit(node, keys[j], opts, scope, state)) { return; + } } } @@ -169,13 +171,12 @@ function traverse(parent, opts, scope, state) { if (!opts.exit) opts.exit = noop; // array of nodes - if (!Array.isArray(parent)) { + if (Array.isArray(parent)) { + for (var i = 0; i < parent.length; i++) { + traverseNode(parent[i], opts, scope, state); + } + } else { traverseNode(parent, opts, scope, state); - return; - } - - for (var i = 0; i < parent.length; i++) { - traverseNode(parent[i], opts, scope, state); } } From 41949fd58b2441642010de5a298c3111840c5c93 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 02:36:37 +1100 Subject: [PATCH 084/129] push uids to scope tracking - fixes #515 --- lib/6to5/file.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/6to5/file.js b/lib/6to5/file.js index fa2967e3d2..68cf3ce5e5 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -325,7 +325,10 @@ File.prototype.generateUid = function (name, scope) { }; File.prototype.generateUidIdentifier = function (name, scope) { - return t.identifier(this.generateUid(name, scope)); + scope = scope || this.scope; + var id = t.identifier(this.generateUid(name, scope)); + scope.add(id); + return id; }; File.prototype._generateUid = function (name) { From e7d4642d4863d74f8589a1dd643bfe737bbdcd4d Mon Sep 17 00:00:00 2001 From: "Fabio M. Costa" Date: Sat, 17 Jan 2015 14:19:54 -0800 Subject: [PATCH 085/129] Fixes lint warnings that are blocking test run --- lib/6to5/transformation/transformers/es6-classes.js | 1 - lib/6to5/transformation/transformers/react.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 1935260324..b68159d242 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -1,6 +1,5 @@ var ReplaceSupers = require("../helpers/replace-supers"); var nameMethod = require("../helpers/name-method"); -var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/react.js b/lib/6to5/transformation/transformers/react.js index 76592d97c2..a1ad20e23b 100644 --- a/lib/6to5/transformation/transformers/react.js +++ b/lib/6to5/transformation/transformers/react.js @@ -226,7 +226,7 @@ var addDisplayName = function (id, call) { var safe = true; for (var i = 0; i < props.length; i++) { - prop = props[i]; + var prop = props[i]; if (t.isIdentifier(prop.key, { name: "displayName" })) { safe = false; break; From 95d9f596687cd3fb4ce4d78dc6c7606128b74209 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 12:12:12 +1100 Subject: [PATCH 086/129] add loose mode to spread and destructuring --- .../transformers/es6-destructuring.js | 17 +++++++---- .../transformation/transformers/es6-spread.js | 8 +++-- .../es6-destructuring-loose/array/actual.js | 1 + .../es6-destructuring-loose/array/expected.js | 10 +++++++ .../assignment-expression/actual.js | 1 + .../assignment-expression/expected.js | 4 +++ .../assignment-statement/actual.js | 1 + .../assignment-statement/expected.js | 7 +++++ .../es6-destructuring-loose/empty/actual.js | 1 + .../es6-destructuring-loose/empty/expected.js | 10 +++++++ .../es7-object-rest/actual.js | 3 ++ .../es7-object-rest/expected.js | 23 ++++++++++++++ .../es7-object-rest/options.json | 3 ++ .../es6-destructuring-loose/for-in/actual.js | 3 ++ .../for-in/expected.js | 8 +++++ .../es6-destructuring-loose/for-of/actual.js | 3 ++ .../for-of/expected.js | 9 ++++++ .../member-expression/actual.js | 1 + .../member-expression/expected.js | 7 +++++ .../es6-destructuring-loose/mixed/actual.js | 1 + .../es6-destructuring-loose/mixed/expected.js | 8 +++++ .../multiple/actual.js | 2 ++ .../multiple/expected.js | 5 ++++ .../object-advanced/actual.js | 1 + .../object-advanced/expected.js | 6 ++++ .../object-basic/actual.js | 1 + .../object-basic/expected.js | 4 +++ .../es6-destructuring-loose/options.json | 3 ++ .../parameters/actual.js | 15 ++++++++++ .../parameters/expected.js | 30 +++++++++++++++++++ .../es6-destructuring-loose/spread/actual.js | 5 ++++ .../spread/expected.js | 12 ++++++++ .../es6-destructuring/parameters/expected.js | 16 +++++----- .../exec-block-scoped-2/exec.js | 12 ++++++++ .../arguments-array/actual.js | 9 ++++++ .../arguments-array/expected.js | 11 +++++++ .../arguments-concat/actual.js | 9 ++++++ .../arguments-concat/expected.js | 11 +++++++ .../es6-spread-loose/arguments/actual.js | 9 ++++++ .../es6-spread-loose/arguments/expected.js | 11 +++++++ .../array-literal-first/actual.js | 1 + .../array-literal-first/expected.js | 3 ++ .../array-literal-middle/actual.js | 1 + .../array-literal-middle/expected.js | 3 ++ .../array-literal-multiple/actual.js | 1 + .../array-literal-multiple/expected.js | 3 ++ .../es6-spread-loose/array-literals/actual.js | 1 + .../array-literals/expected.js | 3 ++ .../actual.js | 1 + .../expected.js | 4 +++ .../actual.js | 1 + .../expected.js | 4 +++ .../actual.js | 2 ++ .../expected.js | 5 ++++ .../actual.js | 2 ++ .../expected.js | 5 ++++ .../method-call-array-literal/actual.js | 1 + .../method-call-array-literal/expected.js | 3 ++ .../method-call-first/actual.js | 1 + .../method-call-first/expected.js | 3 ++ .../method-call-middle/actual.js | 1 + .../method-call-middle/expected.js | 3 ++ .../method-call-multiple-args/actual.js | 1 + .../method-call-multiple-args/expected.js | 3 ++ .../method-call-multiple/actual.js | 1 + .../method-call-multiple/expected.js | 3 ++ .../method-call-single-arg/actual.js | 1 + .../method-call-single-arg/expected.js | 3 ++ .../es6-spread-loose/new-expression/actual.js | 2 ++ .../new-expression/expected.js | 12 ++++++++ .../es6-spread-loose/options.json | 3 ++ .../es6-spread-loose/single/actual.js | 1 + .../es6-spread-loose/single/expected.js | 3 ++ .../arguments/actual.js | 5 ++++ .../arguments/expected.js | 17 +++++++++++ .../array-expression-single-if/actual.js | 1 + .../array-expression-single-if/expected.js | 14 +++++++++ .../array-expression-single/actual.js | 1 + .../array-expression-single/expected.js | 12 ++++++++ .../multiple-if/actual.js | 1 + .../multiple-if/expected.js | 17 +++++++++++ .../multiple/actual.js | 1 + .../multiple/expected.js | 15 ++++++++++ .../options.json | 4 +++ .../single-if/actual.js | 1 + .../single-if/expected.js | 14 +++++++++ .../single/actual.js | 1 + .../single/expected.js | 12 ++++++++ .../this/actual.js | 5 ++++ .../this/expected.js | 17 +++++++++++ .../options.json | 4 +++ .../simple/exec.js | 6 ++++ 92 files changed, 519 insertions(+), 15 deletions(-) create mode 100644 test/fixtures/transformation/es6-destructuring-loose/array/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/array/expected.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/assignment-expression/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/assignment-expression/expected.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/assignment-statement/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/assignment-statement/expected.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/empty/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/empty/expected.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/expected.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/options.json create mode 100644 test/fixtures/transformation/es6-destructuring-loose/for-in/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/for-in/expected.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/for-of/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/for-of/expected.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/member-expression/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/member-expression/expected.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/mixed/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/mixed/expected.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/multiple/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/multiple/expected.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/object-advanced/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/object-advanced/expected.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/object-basic/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/object-basic/expected.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/options.json create mode 100644 test/fixtures/transformation/es6-destructuring-loose/parameters/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/parameters/expected.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/spread/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring-loose/spread/expected.js create mode 100644 test/fixtures/transformation/es6-let-scoping/exec-block-scoped-2/exec.js create mode 100644 test/fixtures/transformation/es6-spread-loose/arguments-array/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/arguments-array/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/arguments-concat/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/arguments-concat/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/arguments/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/arguments/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/array-literal-first/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/array-literal-first/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/array-literal-middle/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/array-literal-middle/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/array-literal-multiple/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/array-literal-multiple/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/array-literals/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/array-literals/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-array-literal/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-array-literal/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-first/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-first/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-middle/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-middle/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-multiple/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-multiple/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-single-arg/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-single-arg/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/new-expression/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/new-expression/expected.js create mode 100644 test/fixtures/transformation/es6-spread-loose/options.json create mode 100644 test/fixtures/transformation/es6-spread-loose/single/actual.js create mode 100644 test/fixtures/transformation/es6-spread-loose/single/expected.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/arguments/actual.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/arguments/expected.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/actual.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/expected.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/actual.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/expected.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/actual.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/expected.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/multiple/actual.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/multiple/expected.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/options.json create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/single-if/actual.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/single-if/expected.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/single/actual.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/single/expected.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/this/actual.js create mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/this/expected.js create mode 100644 test/fixtures/transformation/es7-generator-comprehension-loose/options.json create mode 100644 test/fixtures/transformation/es7-generator-comprehension-loose/simple/exec.js diff --git a/lib/6to5/transformation/transformers/es6-destructuring.js b/lib/6to5/transformation/transformers/es6-destructuring.js index 8b1f5ae882..33929faadd 100644 --- a/lib/6to5/transformation/transformers/es6-destructuring.js +++ b/lib/6to5/transformation/transformers/es6-destructuring.js @@ -7,7 +7,7 @@ var buildVariableAssign = function (opts, id, init) { if (t.isMemberExpression(id)) op = "="; if (op) { - return t.expressionStatement(t.assignmentExpression("=", id, init)); + return t.expressionStatement(t.assignmentExpression(op, id, init)); } else { return t.variableDeclaration(opts.kind, [ t.variableDeclarator(id, init) @@ -95,7 +95,12 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) { } } - var toArray = opts.file.toArray(parentId, !hasSpreadElement && pattern.elements.length); + var isLoose = opts.file.isLoose("destructuring"); + var toArray = parentId; + + if (!isLoose) { + toArray = opts.file.toArray(parentId, !hasSpreadElement && pattern.elements.length); + } var _parentId = opts.scope.generateUidBasedOnNode(parentId, opts.file); nodes.push(t.variableDeclaration("var", [ @@ -107,12 +112,14 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) { var elem = pattern.elements[i]; if (!elem) continue; - i = +i; - var newPatternId; if (t.isSpreadElement(elem)) { - newPatternId = opts.file.toArray(parentId); + newPatternId = parentId; + + if (!isLoose) { + newPatternId = opts.file.toArray(parentId); + } if (i > 0) { newPatternId = t.callExpression(t.memberExpression(newPatternId, t.identifier("slice")), [t.literal(i)]); diff --git a/lib/6to5/transformation/transformers/es6-spread.js b/lib/6to5/transformation/transformers/es6-spread.js index 4fa64b5e2b..2b9865fce5 100644 --- a/lib/6to5/transformation/transformers/es6-spread.js +++ b/lib/6to5/transformation/transformers/es6-spread.js @@ -2,7 +2,11 @@ var t = require("../../types"); var _ = require("lodash"); var getSpreadLiteral = function (spread, file) { - return file.toArray(spread.argument); + if (file.isLoose("spread")) { + return spread.argument; + } else { + return file.toArray(spread.argument); + } }; var hasSpread = function (nodes) { @@ -64,7 +68,7 @@ exports.CallExpression = function (node, parent, scope, context, file) { node.arguments = []; var nodes; - if (args.length === 1 && args[0].argument.name === 'arguments') { + if (args.length === 1 && args[0].argument.name === "arguments") { nodes = [args[0].argument]; } else { nodes = build(args, file); diff --git a/test/fixtures/transformation/es6-destructuring-loose/array/actual.js b/test/fixtures/transformation/es6-destructuring-loose/array/actual.js new file mode 100644 index 0000000000..10aa8799ed --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/array/actual.js @@ -0,0 +1 @@ +var [a, [b], [c], d] = ["hello", [", ", "junk"], ["world"]]; diff --git a/test/fixtures/transformation/es6-destructuring-loose/array/expected.js b/test/fixtures/transformation/es6-destructuring-loose/array/expected.js new file mode 100644 index 0000000000..2b7403faed --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/array/expected.js @@ -0,0 +1,10 @@ +"use strict"; + +var _ref = ["hello", [", ", "junk"], ["world"]]; + +var a = _ref[0]; +var _ref$1 = _ref[1]; +var b = _ref$1[0]; +var _ref$2 = _ref[2]; +var c = _ref$2[0]; +var d = _ref[3]; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/assignment-expression/actual.js b/test/fixtures/transformation/es6-destructuring-loose/assignment-expression/actual.js new file mode 100644 index 0000000000..2ebf93ff16 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/assignment-expression/actual.js @@ -0,0 +1 @@ +console.log([x] = [123]); diff --git a/test/fixtures/transformation/es6-destructuring-loose/assignment-expression/expected.js b/test/fixtures/transformation/es6-destructuring-loose/assignment-expression/expected.js new file mode 100644 index 0000000000..b7721a94bf --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/assignment-expression/expected.js @@ -0,0 +1,4 @@ +"use strict"; + +var _temp, _temp2; +console.log((_temp = [123], _temp2 = _temp, x = _temp2[0], _temp)); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/assignment-statement/actual.js b/test/fixtures/transformation/es6-destructuring-loose/assignment-statement/actual.js new file mode 100644 index 0000000000..d14ce7e47d --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/assignment-statement/actual.js @@ -0,0 +1 @@ +[a, b] = f(); diff --git a/test/fixtures/transformation/es6-destructuring-loose/assignment-statement/expected.js b/test/fixtures/transformation/es6-destructuring-loose/assignment-statement/expected.js new file mode 100644 index 0000000000..e1feb2d7fe --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/assignment-statement/expected.js @@ -0,0 +1,7 @@ +"use strict"; + +var _ref = f(); + +var _ref2 = _ref; +a = _ref2[0]; +b = _ref2[1]; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/empty/actual.js b/test/fixtures/transformation/es6-destructuring-loose/empty/actual.js new file mode 100644 index 0000000000..a70e49fdd6 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/empty/actual.js @@ -0,0 +1 @@ +var [, a, [b], [c], d] = ["foo", "hello", [", ", "junk"], ["world"]]; diff --git a/test/fixtures/transformation/es6-destructuring-loose/empty/expected.js b/test/fixtures/transformation/es6-destructuring-loose/empty/expected.js new file mode 100644 index 0000000000..1bdbe4fc65 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/empty/expected.js @@ -0,0 +1,10 @@ +"use strict"; + +var _ref = ["foo", "hello", [", ", "junk"], ["world"]]; + +var a = _ref[1]; +var _ref$2 = _ref[2]; +var b = _ref$2[0]; +var _ref$3 = _ref[3]; +var c = _ref$3[0]; +var d = _ref[4]; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/actual.js b/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/actual.js new file mode 100644 index 0000000000..b4877d6e45 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/actual.js @@ -0,0 +1,3 @@ +var { ...x } = z; +var { x, ...y } = z; +(function({ x, ...y }) { }) diff --git a/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/expected.js b/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/expected.js new file mode 100644 index 0000000000..dbb29cb23d --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/expected.js @@ -0,0 +1,23 @@ +"use strict"; + +var _objectWithoutProperties = function (obj, keys) { + var target = {}; + + for (var i in obj) { + if (keys.indexOf(i) >= 0) continue; + if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; + target[i] = obj[i]; + } + + return target; +}; + +var x = _objectWithoutProperties(z, []); + +var x = z.x; +var y = _objectWithoutProperties(z, ["x"]); + +(function (_ref) { + var x = _ref.x; + var y = _objectWithoutProperties(_ref, ["x"]); +}); diff --git a/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/options.json b/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/options.json new file mode 100644 index 0000000000..252f473a73 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/options.json @@ -0,0 +1,3 @@ +{ + "experimental": true +} diff --git a/test/fixtures/transformation/es6-destructuring-loose/for-in/actual.js b/test/fixtures/transformation/es6-destructuring-loose/for-in/actual.js new file mode 100644 index 0000000000..87fe7e7dde --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/for-in/actual.js @@ -0,0 +1,3 @@ +for (var [name, value] in obj) { + print("Name: " + name + ", Value: " + value); +} diff --git a/test/fixtures/transformation/es6-destructuring-loose/for-in/expected.js b/test/fixtures/transformation/es6-destructuring-loose/for-in/expected.js new file mode 100644 index 0000000000..787fa1df42 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/for-in/expected.js @@ -0,0 +1,8 @@ +"use strict"; + +for (var _ref in obj) { + var _ref2 = _ref; + var name = _ref2[0]; + var value = _ref2[1]; + print("Name: " + name + ", Value: " + value); +} \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/for-of/actual.js b/test/fixtures/transformation/es6-destructuring-loose/for-of/actual.js new file mode 100644 index 0000000000..5873553d65 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/for-of/actual.js @@ -0,0 +1,3 @@ +for (var [ name, before, after ] of this.test.expectation.registers) { + +} diff --git a/test/fixtures/transformation/es6-destructuring-loose/for-of/expected.js b/test/fixtures/transformation/es6-destructuring-loose/for-of/expected.js new file mode 100644 index 0000000000..314c03654c --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/for-of/expected.js @@ -0,0 +1,9 @@ +"use strict"; + +for (var _iterator = this.test.expectation.registers[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { + var _ref = _step.value; + var _ref2 = _ref; + var name = _ref2[0]; + var before = _ref2[1]; + var after = _ref2[2]; +} \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/member-expression/actual.js b/test/fixtures/transformation/es6-destructuring-loose/member-expression/actual.js new file mode 100644 index 0000000000..bd5e42d7d1 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/member-expression/actual.js @@ -0,0 +1 @@ +[this.foo, this.bar] = [1, 2]; diff --git a/test/fixtures/transformation/es6-destructuring-loose/member-expression/expected.js b/test/fixtures/transformation/es6-destructuring-loose/member-expression/expected.js new file mode 100644 index 0000000000..7e7353b88f --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/member-expression/expected.js @@ -0,0 +1,7 @@ +"use strict"; + +var _ref = [1, 2]; + +var _ref2 = _ref; +this.foo = _ref2[0]; +this.bar = _ref2[1]; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/mixed/actual.js b/test/fixtures/transformation/es6-destructuring-loose/mixed/actual.js new file mode 100644 index 0000000000..79ac2cb22f --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/mixed/actual.js @@ -0,0 +1 @@ +var {topLeft: [x1, y1], bottomRight: [x2, y2] } = rect; diff --git a/test/fixtures/transformation/es6-destructuring-loose/mixed/expected.js b/test/fixtures/transformation/es6-destructuring-loose/mixed/expected.js new file mode 100644 index 0000000000..7f44ca9a37 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/mixed/expected.js @@ -0,0 +1,8 @@ +"use strict"; + +var _rect$topLeft = rect.topLeft; +var x1 = _rect$topLeft[0]; +var y1 = _rect$topLeft[1]; +var _rect$bottomRight = rect.bottomRight; +var x2 = _rect$bottomRight[0]; +var y2 = _rect$bottomRight[1]; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/multiple/actual.js b/test/fixtures/transformation/es6-destructuring-loose/multiple/actual.js new file mode 100644 index 0000000000..09249c483c --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/multiple/actual.js @@ -0,0 +1,2 @@ +var { x, y } = coords, + foo = "bar"; diff --git a/test/fixtures/transformation/es6-destructuring-loose/multiple/expected.js b/test/fixtures/transformation/es6-destructuring-loose/multiple/expected.js new file mode 100644 index 0000000000..c782335937 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/multiple/expected.js @@ -0,0 +1,5 @@ +"use strict"; + +var x = coords.x; +var y = coords.y; +var foo = "bar"; diff --git a/test/fixtures/transformation/es6-destructuring-loose/object-advanced/actual.js b/test/fixtures/transformation/es6-destructuring-loose/object-advanced/actual.js new file mode 100644 index 0000000000..c4b85492cf --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/object-advanced/actual.js @@ -0,0 +1 @@ +var {topLeft: {x: x1, y: y1}, bottomRight: {x: x2, y: y2}} = rect; diff --git a/test/fixtures/transformation/es6-destructuring-loose/object-advanced/expected.js b/test/fixtures/transformation/es6-destructuring-loose/object-advanced/expected.js new file mode 100644 index 0000000000..d3ee04ea05 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/object-advanced/expected.js @@ -0,0 +1,6 @@ +"use strict"; + +var x1 = rect.topLeft.x; +var y1 = rect.topLeft.y; +var x2 = rect.bottomRight.x; +var y2 = rect.bottomRight.y; diff --git a/test/fixtures/transformation/es6-destructuring-loose/object-basic/actual.js b/test/fixtures/transformation/es6-destructuring-loose/object-basic/actual.js new file mode 100644 index 0000000000..dae014575e --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/object-basic/actual.js @@ -0,0 +1 @@ +var { x, y } = coords; diff --git a/test/fixtures/transformation/es6-destructuring-loose/object-basic/expected.js b/test/fixtures/transformation/es6-destructuring-loose/object-basic/expected.js new file mode 100644 index 0000000000..97601e8b4e --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/object-basic/expected.js @@ -0,0 +1,4 @@ +"use strict"; + +var x = coords.x; +var y = coords.y; diff --git a/test/fixtures/transformation/es6-destructuring-loose/options.json b/test/fixtures/transformation/es6-destructuring-loose/options.json new file mode 100644 index 0000000000..77a73984e1 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/options.json @@ -0,0 +1,3 @@ +{ + "loose": ["destructuring"] +} diff --git a/test/fixtures/transformation/es6-destructuring-loose/parameters/actual.js b/test/fixtures/transformation/es6-destructuring-loose/parameters/actual.js new file mode 100644 index 0000000000..896a7ce416 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/parameters/actual.js @@ -0,0 +1,15 @@ +function somethingAdvanced({topLeft: {x: x1, y: y1}, bottomRight: {x: x2, y: y2}}){ + +} + +function unpackObject({title: title, author: author}) { + return title + ' ' + author; +} + +console.log(unpackObject({title: 'title', author: 'author'})); + +var unpackArray = function ([a, b, c], [x, y, z]) { + return a+b+c; +}; + +console.log(unpackArray(['hello', ', ', 'world'], [1, 2, 3])); diff --git a/test/fixtures/transformation/es6-destructuring-loose/parameters/expected.js b/test/fixtures/transformation/es6-destructuring-loose/parameters/expected.js new file mode 100644 index 0000000000..3fd491d5b0 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/parameters/expected.js @@ -0,0 +1,30 @@ +"use strict"; + +function somethingAdvanced(_ref) { + var x1 = _ref.topLeft.x; + var y1 = _ref.topLeft.y; + var x2 = _ref.bottomRight.x; + var y2 = _ref.bottomRight.y; +} + +function unpackObject(_ref2) { + var title = _ref2.title; + var author = _ref2.author; + return title + " " + author; +} + +console.log(unpackObject({ title: "title", author: "author" })); + +var unpackArray = function (_ref3, _ref4) { + var _ref32 = _ref3; + var a = _ref32[0]; + var b = _ref32[1]; + var c = _ref32[2]; + var _ref42 = _ref4; + var x = _ref42[0]; + var y = _ref42[1]; + var z = _ref42[2]; + return a + b + c; +}; + +console.log(unpackArray(["hello", ", ", "world"], [1, 2, 3])); diff --git a/test/fixtures/transformation/es6-destructuring-loose/spread/actual.js b/test/fixtures/transformation/es6-destructuring-loose/spread/actual.js new file mode 100644 index 0000000000..0ef8f028ed --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/spread/actual.js @@ -0,0 +1,5 @@ +var isSorted = ([x, y, ...wow]) => { + if (!zs.length) return true + if (y > x) return isSorted(zs) + return false +}; diff --git a/test/fixtures/transformation/es6-destructuring-loose/spread/expected.js b/test/fixtures/transformation/es6-destructuring-loose/spread/expected.js new file mode 100644 index 0000000000..03400c9581 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring-loose/spread/expected.js @@ -0,0 +1,12 @@ +"use strict"; + +var isSorted = function (_ref) { + var _ref2 = _ref; + var x = _ref2[0]; + var y = _ref2[1]; + var wow = _ref2.slice(2); + + if (!zs.length) return true; + if (y > x) return isSorted(zs); + return false; +}; diff --git a/test/fixtures/transformation/es6-destructuring/parameters/expected.js b/test/fixtures/transformation/es6-destructuring/parameters/expected.js index a6a1d3dfe6..830eb15edc 100644 --- a/test/fixtures/transformation/es6-destructuring/parameters/expected.js +++ b/test/fixtures/transformation/es6-destructuring/parameters/expected.js @@ -32,16 +32,16 @@ function unpackObject(_ref2) { console.log(unpackObject({ title: "title", author: "author" })); var unpackArray = function (_ref3, _ref4) { - var _ref3 = _slicedToArray(_ref3, 3); + var _ref32 = _slicedToArray(_ref3, 3); - var a = _ref3[0]; - var b = _ref3[1]; - var c = _ref3[2]; - var _ref4 = _slicedToArray(_ref4, 3); + var a = _ref32[0]; + var b = _ref32[1]; + var c = _ref32[2]; + var _ref42 = _slicedToArray(_ref4, 3); - var x = _ref4[0]; - var y = _ref4[1]; - var z = _ref4[2]; + var x = _ref42[0]; + var y = _ref42[1]; + var z = _ref42[2]; return a + b + c; }; diff --git a/test/fixtures/transformation/es6-let-scoping/exec-block-scoped-2/exec.js b/test/fixtures/transformation/es6-let-scoping/exec-block-scoped-2/exec.js new file mode 100644 index 0000000000..e98bf47f5e --- /dev/null +++ b/test/fixtures/transformation/es6-let-scoping/exec-block-scoped-2/exec.js @@ -0,0 +1,12 @@ +assert.equal(() => { + let sum = 0; + let a = 0; + { + let a = 10; + for (let i = 0; i < a; i++) { + let a = 1; + sum += (() => a)(); + } + } + return sum; +}(), 10); diff --git a/test/fixtures/transformation/es6-spread-loose/arguments-array/actual.js b/test/fixtures/transformation/es6-spread-loose/arguments-array/actual.js new file mode 100644 index 0000000000..e18c362b1b --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/arguments-array/actual.js @@ -0,0 +1,9 @@ +function foo() { + return bar([...arguments]); +} + +function bar(one, two, three) { + return [one, two, three]; +} + +foo("foo", "bar"); diff --git a/test/fixtures/transformation/es6-spread-loose/arguments-array/expected.js b/test/fixtures/transformation/es6-spread-loose/arguments-array/expected.js new file mode 100644 index 0000000000..92e9e6ff46 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/arguments-array/expected.js @@ -0,0 +1,11 @@ +"use strict"; + +function foo() { + return bar([].concat(arguments)); +} + +function bar(one, two, three) { + return [one, two, three]; +} + +foo("foo", "bar"); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-spread-loose/arguments-concat/actual.js b/test/fixtures/transformation/es6-spread-loose/arguments-concat/actual.js new file mode 100644 index 0000000000..a6fdb21bef --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/arguments-concat/actual.js @@ -0,0 +1,9 @@ +function foo() { + return bar("test", ...arguments); +} + +function bar(one, two, three) { + return [one, two, three]; +} + +foo("foo", "bar"); diff --git a/test/fixtures/transformation/es6-spread-loose/arguments-concat/expected.js b/test/fixtures/transformation/es6-spread-loose/arguments-concat/expected.js new file mode 100644 index 0000000000..b0bbaef08f --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/arguments-concat/expected.js @@ -0,0 +1,11 @@ +"use strict"; + +function foo() { + return bar.apply(undefined, ["test"].concat(arguments)); +} + +function bar(one, two, three) { + return [one, two, three]; +} + +foo("foo", "bar"); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-spread-loose/arguments/actual.js b/test/fixtures/transformation/es6-spread-loose/arguments/actual.js new file mode 100644 index 0000000000..476da431e8 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/arguments/actual.js @@ -0,0 +1,9 @@ +function foo() { + return bar(...arguments); +} + +function bar(one, two, three) { + return [one, two, three]; +} + +foo("foo", "bar"); diff --git a/test/fixtures/transformation/es6-spread-loose/arguments/expected.js b/test/fixtures/transformation/es6-spread-loose/arguments/expected.js new file mode 100644 index 0000000000..52a18f07e7 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/arguments/expected.js @@ -0,0 +1,11 @@ +"use strict"; + +function foo() { + return bar.apply(undefined, arguments); +} + +function bar(one, two, three) { + return [one, two, three]; +} + +foo("foo", "bar"); diff --git a/test/fixtures/transformation/es6-spread-loose/array-literal-first/actual.js b/test/fixtures/transformation/es6-spread-loose/array-literal-first/actual.js new file mode 100644 index 0000000000..3857ab97d4 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/array-literal-first/actual.js @@ -0,0 +1 @@ +var lyrics = [...parts, "head", "and", "toes"]; diff --git a/test/fixtures/transformation/es6-spread-loose/array-literal-first/expected.js b/test/fixtures/transformation/es6-spread-loose/array-literal-first/expected.js new file mode 100644 index 0000000000..3d1401d537 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/array-literal-first/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +var lyrics = [].concat(parts, ["head", "and", "toes"]); diff --git a/test/fixtures/transformation/es6-spread-loose/array-literal-middle/actual.js b/test/fixtures/transformation/es6-spread-loose/array-literal-middle/actual.js new file mode 100644 index 0000000000..9b354ad7cf --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/array-literal-middle/actual.js @@ -0,0 +1 @@ +var a = [b, ...c, d]; diff --git a/test/fixtures/transformation/es6-spread-loose/array-literal-middle/expected.js b/test/fixtures/transformation/es6-spread-loose/array-literal-middle/expected.js new file mode 100644 index 0000000000..cc40455aeb --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/array-literal-middle/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +var a = [b].concat(c, [d]); diff --git a/test/fixtures/transformation/es6-spread-loose/array-literal-multiple/actual.js b/test/fixtures/transformation/es6-spread-loose/array-literal-multiple/actual.js new file mode 100644 index 0000000000..e65edc597a --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/array-literal-multiple/actual.js @@ -0,0 +1 @@ +var a = [b, ...c, d, e, ...f]; diff --git a/test/fixtures/transformation/es6-spread-loose/array-literal-multiple/expected.js b/test/fixtures/transformation/es6-spread-loose/array-literal-multiple/expected.js new file mode 100644 index 0000000000..5b55e67414 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/array-literal-multiple/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +var a = [b].concat(c, [d, e], f); diff --git a/test/fixtures/transformation/es6-spread-loose/array-literals/actual.js b/test/fixtures/transformation/es6-spread-loose/array-literals/actual.js new file mode 100644 index 0000000000..5ebb80da66 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/array-literals/actual.js @@ -0,0 +1 @@ +var lyrics = ["head", "and", "toes", ...parts]; diff --git a/test/fixtures/transformation/es6-spread-loose/array-literals/expected.js b/test/fixtures/transformation/es6-spread-loose/array-literals/expected.js new file mode 100644 index 0000000000..4b402a9818 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/array-literals/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +var lyrics = ["head", "and", "toes"].concat(parts); diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/actual.js b/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/actual.js new file mode 100644 index 0000000000..305586e2a6 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/actual.js @@ -0,0 +1 @@ +obj[method](foo, bar, ...args); diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/expected.js b/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/expected.js new file mode 100644 index 0000000000..6303149d3c --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/expected.js @@ -0,0 +1,4 @@ +"use strict"; + +var _obj; +(_obj = obj)[method].apply(_obj, [foo, bar].concat(args)); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/actual.js b/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/actual.js new file mode 100644 index 0000000000..65a21cba1e --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/actual.js @@ -0,0 +1 @@ +obj[method](...args); diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/expected.js b/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/expected.js new file mode 100644 index 0000000000..786a1efbd7 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/expected.js @@ -0,0 +1,4 @@ +"use strict"; + +var _obj; +(_obj = obj)[method].apply(_obj, args); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/actual.js b/test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/actual.js new file mode 100644 index 0000000000..7dc7d142a1 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/actual.js @@ -0,0 +1,2 @@ +foob.add(foo, bar, ...numbers); +foob.test.add(foo, bar, ...numbers); diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/expected.js b/test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/expected.js new file mode 100644 index 0000000000..5c9d6466ef --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/expected.js @@ -0,0 +1,5 @@ +"use strict"; + +var _foob, _foob$test; +(_foob = foob).add.apply(_foob, [foo, bar].concat(numbers)); +(_foob$test = foob.test).add.apply(_foob$test, [foo, bar].concat(numbers)); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/actual.js b/test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/actual.js new file mode 100644 index 0000000000..ef061e17a8 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/actual.js @@ -0,0 +1,2 @@ +foob.add(...numbers); +foob.test.add(...numbers); diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/expected.js b/test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/expected.js new file mode 100644 index 0000000000..f03c36ac76 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/expected.js @@ -0,0 +1,5 @@ +"use strict"; + +var _foob, _foob$test; +(_foob = foob).add.apply(_foob, numbers); +(_foob$test = foob.test).add.apply(_foob$test, numbers); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-array-literal/actual.js b/test/fixtures/transformation/es6-spread-loose/method-call-array-literal/actual.js new file mode 100644 index 0000000000..2d1a0bb716 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/method-call-array-literal/actual.js @@ -0,0 +1 @@ +f(...[1, 2, 3]); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-array-literal/expected.js b/test/fixtures/transformation/es6-spread-loose/method-call-array-literal/expected.js new file mode 100644 index 0000000000..a370047756 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/method-call-array-literal/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +f.apply(undefined, [1, 2, 3]); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-first/actual.js b/test/fixtures/transformation/es6-spread-loose/method-call-first/actual.js new file mode 100644 index 0000000000..0d63c0ea63 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/method-call-first/actual.js @@ -0,0 +1 @@ +add(...numbers, foo, bar); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-first/expected.js b/test/fixtures/transformation/es6-spread-loose/method-call-first/expected.js new file mode 100644 index 0000000000..4700d54131 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/method-call-first/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +add.apply(undefined, numbers.concat([foo, bar])); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-middle/actual.js b/test/fixtures/transformation/es6-spread-loose/method-call-middle/actual.js new file mode 100644 index 0000000000..b04a3f0223 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/method-call-middle/actual.js @@ -0,0 +1 @@ +add(foo, ...numbers, bar); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-middle/expected.js b/test/fixtures/transformation/es6-spread-loose/method-call-middle/expected.js new file mode 100644 index 0000000000..74f6a108ac --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/method-call-middle/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +add.apply(undefined, [foo].concat(numbers, [bar])); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/actual.js b/test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/actual.js new file mode 100644 index 0000000000..0a2421b7b4 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/actual.js @@ -0,0 +1 @@ +add(foo, bar, ...numbers); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/expected.js b/test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/expected.js new file mode 100644 index 0000000000..e543eca3db --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +add.apply(undefined, [foo, bar].concat(numbers)); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-multiple/actual.js b/test/fixtures/transformation/es6-spread-loose/method-call-multiple/actual.js new file mode 100644 index 0000000000..c06ed01cfb --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/method-call-multiple/actual.js @@ -0,0 +1 @@ +add(foo, ...numbers, bar, what, ...test); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-multiple/expected.js b/test/fixtures/transformation/es6-spread-loose/method-call-multiple/expected.js new file mode 100644 index 0000000000..046ac08426 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/method-call-multiple/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +add.apply(undefined, [foo].concat(numbers, [bar, what], test)); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-single-arg/actual.js b/test/fixtures/transformation/es6-spread-loose/method-call-single-arg/actual.js new file mode 100644 index 0000000000..a24aa1b09c --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/method-call-single-arg/actual.js @@ -0,0 +1 @@ +add(...numbers); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-single-arg/expected.js b/test/fixtures/transformation/es6-spread-loose/method-call-single-arg/expected.js new file mode 100644 index 0000000000..ef2f902647 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/method-call-single-arg/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +add.apply(undefined, numbers); diff --git a/test/fixtures/transformation/es6-spread-loose/new-expression/actual.js b/test/fixtures/transformation/es6-spread-loose/new-expression/actual.js new file mode 100644 index 0000000000..72800dd42b --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/new-expression/actual.js @@ -0,0 +1,2 @@ +new Numbers(...nums); +new Numbers(1, ...nums); diff --git a/test/fixtures/transformation/es6-spread-loose/new-expression/expected.js b/test/fixtures/transformation/es6-spread-loose/new-expression/expected.js new file mode 100644 index 0000000000..5677c95bec --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/new-expression/expected.js @@ -0,0 +1,12 @@ +"use strict"; + +var _applyConstructor = function (Constructor, args) { + var instance = Object.create(Constructor.prototype); + + var result = Constructor.apply(instance, args); + + return result != null && (typeof result == "object" || typeof result == "function") ? result : instance; +}; + +_applyConstructor(Numbers, nums); +_applyConstructor(Numbers, [1].concat(nums)); diff --git a/test/fixtures/transformation/es6-spread-loose/options.json b/test/fixtures/transformation/es6-spread-loose/options.json new file mode 100644 index 0000000000..536a7d71d2 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/options.json @@ -0,0 +1,3 @@ +{ + "loose": "spread" +} diff --git a/test/fixtures/transformation/es6-spread-loose/single/actual.js b/test/fixtures/transformation/es6-spread-loose/single/actual.js new file mode 100644 index 0000000000..6541257708 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/single/actual.js @@ -0,0 +1 @@ +[...foo]; diff --git a/test/fixtures/transformation/es6-spread-loose/single/expected.js b/test/fixtures/transformation/es6-spread-loose/single/expected.js new file mode 100644 index 0000000000..0d7e55ac18 --- /dev/null +++ b/test/fixtures/transformation/es6-spread-loose/single/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +[].concat(foo); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/arguments/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/arguments/actual.js new file mode 100644 index 0000000000..95f9a4df57 --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/arguments/actual.js @@ -0,0 +1,5 @@ +function add() { + return [for (i of [1, 2, 3]) i * arguments[0]]; +} + +add(5); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/arguments/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/arguments/expected.js new file mode 100644 index 0000000000..39115d87d3 --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/arguments/expected.js @@ -0,0 +1,17 @@ +"use strict"; + +function add() { + var _arguments = arguments; + return (function () { + var _ref = []; + + for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { + var i = _step.value; + _ref.push(i * _arguments[0]); + } + + return _ref; + })(); +} + +add(5); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/actual.js new file mode 100644 index 0000000000..4882336b9f --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/actual.js @@ -0,0 +1 @@ +var arr = [for (i of [1, 2, 3]) if (i > 1) i * i]; diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/expected.js new file mode 100644 index 0000000000..0f3f625138 --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/expected.js @@ -0,0 +1,14 @@ +"use strict"; + +var arr = (function () { + var _arr = []; + + for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { + var i = _step.value; + if (i > 1) { + _arr.push(i * i); + } + } + + return _arr; +})(); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/actual.js new file mode 100644 index 0000000000..2087ae351b --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/actual.js @@ -0,0 +1 @@ +var arr = [for (i of [1, 2, 3]) i * i]; diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/expected.js new file mode 100644 index 0000000000..c3ae8d9432 --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/expected.js @@ -0,0 +1,12 @@ +"use strict"; + +var arr = (function () { + var _arr = []; + + for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { + var i = _step.value; + _arr.push(i * i); + } + + return _arr; +})(); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/actual.js new file mode 100644 index 0000000000..754bb7e894 --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/actual.js @@ -0,0 +1 @@ +var seattlers = [for (customers of countries) for (c of customers) if (c.city == "Seattle") { name: c.name, age: c.age }]; diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/expected.js new file mode 100644 index 0000000000..6b486b45f5 --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/expected.js @@ -0,0 +1,17 @@ +"use strict"; + +var seattlers = (function () { + var _seattlers = []; + + for (var _iterator = countries[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { + var customers = _step.value; + for (var _iterator2 = customers[Symbol.iterator](), _step2; !(_step2 = _iterator2.next()).done;) { + var c = _step2.value; + if (c.city == "Seattle") { + _seattlers.push({ name: c.name, age: c.age }); + } + } + } + + return _seattlers; +})(); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/multiple/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/multiple/actual.js new file mode 100644 index 0000000000..f0aaa63c27 --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/multiple/actual.js @@ -0,0 +1 @@ +var arr = [for (x of "abcdefgh".split("")) for (y of "12345678".split("")) x + y]; diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/multiple/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/multiple/expected.js new file mode 100644 index 0000000000..05f2c6e76d --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/multiple/expected.js @@ -0,0 +1,15 @@ +"use strict"; + +var arr = (function () { + var _arr = []; + + for (var _iterator = "abcdefgh".split("")[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { + var x = _step.value; + for (var _iterator2 = "12345678".split("")[Symbol.iterator](), _step2; !(_step2 = _iterator2.next()).done;) { + var y = _step2.value; + _arr.push(x + y); + } + } + + return _arr; +})(); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/options.json b/test/fixtures/transformation/es7-array-comprehension-loose/options.json new file mode 100644 index 0000000000..6e5e1c6b65 --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/options.json @@ -0,0 +1,4 @@ +{ + "experimental": true, + "loose": "arrayComprehension" +} diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/single-if/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/single-if/actual.js new file mode 100644 index 0000000000..e2cf129386 --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/single-if/actual.js @@ -0,0 +1 @@ +var arr = [for (i of nums) if (i > 1) i * i]; diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/single-if/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/single-if/expected.js new file mode 100644 index 0000000000..6e95f9cadc --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/single-if/expected.js @@ -0,0 +1,14 @@ +"use strict"; + +var arr = (function () { + var _arr = []; + + for (var _iterator = nums[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { + var i = _step.value; + if (i > 1) { + _arr.push(i * i); + } + } + + return _arr; +})(); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/single/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/single/actual.js new file mode 100644 index 0000000000..9dda19048b --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/single/actual.js @@ -0,0 +1 @@ +var arr = [for (i of nums) i * i]; diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/single/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/single/expected.js new file mode 100644 index 0000000000..88e01e350a --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/single/expected.js @@ -0,0 +1,12 @@ +"use strict"; + +var arr = (function () { + var _arr = []; + + for (var _iterator = nums[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { + var i = _step.value; + _arr.push(i * i); + } + + return _arr; +})(); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/this/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/this/actual.js new file mode 100644 index 0000000000..355ed48188 --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/this/actual.js @@ -0,0 +1,5 @@ +function add() { + return [for (i of [1, 2, 3]) i * this.multiplier]; +} + +add.call({ multiplier: 5 }); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/this/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/this/expected.js new file mode 100644 index 0000000000..5645b53dcc --- /dev/null +++ b/test/fixtures/transformation/es7-array-comprehension-loose/this/expected.js @@ -0,0 +1,17 @@ +"use strict"; + +function add() { + var _this = this; + return (function () { + var _ref = []; + + for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { + var i = _step.value; + _ref.push(i * _this.multiplier); + } + + return _ref; + })(); +} + +add.call({ multiplier: 5 }); diff --git a/test/fixtures/transformation/es7-generator-comprehension-loose/options.json b/test/fixtures/transformation/es7-generator-comprehension-loose/options.json new file mode 100644 index 0000000000..4fdef2f3ff --- /dev/null +++ b/test/fixtures/transformation/es7-generator-comprehension-loose/options.json @@ -0,0 +1,4 @@ +{ + "experimental": true, + "loose": "generatorComprehension" +} diff --git a/test/fixtures/transformation/es7-generator-comprehension-loose/simple/exec.js b/test/fixtures/transformation/es7-generator-comprehension-loose/simple/exec.js new file mode 100644 index 0000000000..dd7ef4da0a --- /dev/null +++ b/test/fixtures/transformation/es7-generator-comprehension-loose/simple/exec.js @@ -0,0 +1,6 @@ +var nums = [1, 2, 3, 4, 5, 6]; +var multiples = (for (i of nums) if (i % 2) i * i); +assert.equal(multiples.next().value, 1); +assert.equal(multiples.next().value, 9); +assert.equal(multiples.next().value, 25); +assert.ok(multiples.next().done); From f4b2768194222ec877561b5b16a07947c353f24f Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 12:12:32 +1100 Subject: [PATCH 087/129] add todo note to duplicate expression --- .../helpers/build-conditional-assignment-operator-transformer.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js b/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js index 154588db85..adb097052f 100644 --- a/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js +++ b/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js @@ -35,6 +35,7 @@ module.exports = function (exports, opts) { buildAssignment(exploded.ref, node.right) )); + // todo: duplicate expression node nodes.push(exploded.ref); return t.toSequenceExpression(nodes, scope); From a0f605bbab910a53255e1e4410104d57b54de54a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 12:12:58 +1100 Subject: [PATCH 088/129] add way to automatically write expected tests --- test/_transformation-helper.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/_transformation-helper.js b/test/_transformation-helper.js index db754ac9ec..c036440f9e 100644 --- a/test/_transformation-helper.js +++ b/test/_transformation-helper.js @@ -94,7 +94,12 @@ var run = function (task, done) { checkAst(result); actualCode = result.code; - chai.expect(actualCode).to.be.equal(expectCode, actual.loc + " !== " + expect.loc); + try { + chai.expect(actualCode).to.be.equal(expectCode, actual.loc + " !== " + expect.loc); + } catch (err) { + //require("fs").writeFileSync(expect.loc, actualCode); + throw err; + } } if (task.sourceMap) { From c210d6455786cab28a7ada5a74302dcdb4ef5cdc Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 12:13:08 +1100 Subject: [PATCH 089/129] add loose all option --- lib/6to5/file.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 68cf3ce5e5..926cf0e427 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -78,6 +78,10 @@ File.normaliseOptions = function (opts) { opts.optional = util.arrayify(opts.optional); opts.loose = util.arrayify(opts.loose); + if (_.contains(opts.loose, "all")) { + opts.loose = Object.keys(transform.transformers); + } + // todo: remove in 3.0.0 _.each({ fastForOf: "forOf", From ddd59b94b030098936a9f6ae504fb3a77507691b Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 12:15:31 +1100 Subject: [PATCH 090/129] upgrade acorn-6to5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 63d222e27d..5c0c46e586 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "test": "make test" }, "dependencies": { - "acorn-6to5": "0.11.1-16", + "acorn-6to5": "0.11.1-17", "ast-types": "~0.6.1", "chokidar": "0.11.1", "commander": "2.5.0", From 320c78b8150368a164fa175b9f4577a87d2be069 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 12:15:50 +1100 Subject: [PATCH 091/129] make let scoping collision detection much more reliable --- .../transformers/es6-let-scoping.js | 50 ++++++++++++------- .../private/expected.js | 14 +++--- .../array-expression-single-if/expected.js | 6 +-- .../multiple-if/expected.js | 6 +-- .../single-if/expected.js | 6 +-- .../comprehensive/exec.js | 21 ++++++++ 6 files changed, 69 insertions(+), 34 deletions(-) create mode 100644 test/fixtures/transformation/es7-exponentian-operator/comprehensive/exec.js diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index d6ccf9078f..65f8cf9d9c 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -162,21 +162,32 @@ LetScoping.prototype.noClosure = function () { */ LetScoping.prototype.remap = function () { - var replacements = this.info.duplicates; - var block = this.block; + var duplicates = this.info.duplicates; + var block = this.block; + var scope = this.scope; if (!this.info.hasDuplicates) return; - var replace = function (node, parent, scope, context, replacements) { + var replace = function (node, parent, scope, context, duplicates) { if (!t.isIdentifier(node)) return; if (!t.isReferenced(node, parent)) return; - if (scope && scope.hasOwn(node.name)) return; - node.name = replacements[node.name] || node.name; + + var duplicate = duplicates[node.name]; + if (!duplicate) return; + + var own = scope.get(node.name, true); + if (own === duplicate.node) { + node.name = duplicate.uid; + } else { + // scope already has it's own declaration that doesn't + // match the one we have a stored replacement for + context.skip(); + } }; var traverseReplace = function (node, parent) { replace(node, parent); - traverse(node, { enter: replace }, null, replacements); + traverse(node, { enter: replace }, scope, duplicates); }; var loopParent = this.loopParent; @@ -186,7 +197,7 @@ LetScoping.prototype.remap = function () { traverseReplace(loopParent.update, loopParent); } - traverse(block, { enter: replace }, null, replacements); + traverse(block, { enter: replace }, scope, duplicates); }; /** @@ -221,12 +232,14 @@ LetScoping.prototype.getInfo = function () { }; var duplicates = function (id, key) { - var has = scope.parentGet(key); - - if (has && has !== id) { - // there's a variable with this exact name in an upper scope so we need - // to generate a new name - opts.duplicates[key] = id.name = file.generateUidIdentifier(key, scope).name; + // there's a variable with this exact name in an upper scope so we need + // to generate a new name + if (scope.parentHas(key, true)) { + var duplicate = opts.duplicates[key] = { + uid: file.generateUidIdentifier(key, scope).name, + node: id + }; + id.name = duplicate.uid; opts.hasDuplicates = true; } }; @@ -305,7 +318,7 @@ LetScoping.prototype.checkLoop = function () { if (replace) return t.inherits(replace, node); } - }, null, has); + }, this.scope, has); return has; }; @@ -332,7 +345,7 @@ LetScoping.prototype.hoistVarDeclarations = function () { return context.skip(); } } - }, null, this); + }, this.scope, this); }; /** @@ -347,7 +360,8 @@ LetScoping.prototype.getParams = function (params) { var info = this.info; params = _.cloneDeep(params); _.each(params, function (param) { - param.name = info.duplicates[param.name] || param.name; + var duplicate = info.duplicates[param.name]; + if (duplicate) param.name = duplicate.uid; }); return params; }; @@ -388,12 +402,12 @@ LetScoping.prototype.getLetReferences = function () { // push this badboy state.self.letReferences[node.name] = node; } - }, null, state); + }, scope, state); return context.skip(); } } - }, null, state); + }, this.scope, state); return state.closurify; }; diff --git a/test/fixtures/transformation/es7-abstract-references/private/expected.js b/test/fixtures/transformation/es7-abstract-references/private/expected.js index 1af25539ca..d79155a923 100644 --- a/test/fixtures/transformation/es7-abstract-references/private/expected.js +++ b/test/fixtures/transformation/es7-abstract-references/private/expected.js @@ -4,19 +4,19 @@ var A = new WeakMap(); var B = new WeakMap(), C = new WeakMap(); var D = (function () { - var F = new WeakMap(), - G = new WeakMap(); - var E = new WeakMap(); + var _F = new WeakMap(), + _G = new WeakMap(); + var _E = new WeakMap(); function D() {} return D; })(); var H = (function () { - var J = new WeakMap(), - K = new WeakMap(); - var I = new WeakMap(); + var _J = new WeakMap(), + _K = new WeakMap(); + var _I = new WeakMap(); function H() {} return H; -})(); +})(); \ No newline at end of file diff --git a/test/fixtures/transformation/es7-array-comprehension/array-expression-single-if/expected.js b/test/fixtures/transformation/es7-array-comprehension/array-expression-single-if/expected.js index 37496b18dc..0f3f625138 100644 --- a/test/fixtures/transformation/es7-array-comprehension/array-expression-single-if/expected.js +++ b/test/fixtures/transformation/es7-array-comprehension/array-expression-single-if/expected.js @@ -4,9 +4,9 @@ var arr = (function () { var _arr = []; for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { - var _i = _step.value; - if (_i > 1) { - _arr.push(_i * _i); + var i = _step.value; + if (i > 1) { + _arr.push(i * i); } } diff --git a/test/fixtures/transformation/es7-array-comprehension/multiple-if/expected.js b/test/fixtures/transformation/es7-array-comprehension/multiple-if/expected.js index c5ba3b5331..6b486b45f5 100644 --- a/test/fixtures/transformation/es7-array-comprehension/multiple-if/expected.js +++ b/test/fixtures/transformation/es7-array-comprehension/multiple-if/expected.js @@ -6,9 +6,9 @@ var seattlers = (function () { for (var _iterator = countries[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { var customers = _step.value; for (var _iterator2 = customers[Symbol.iterator](), _step2; !(_step2 = _iterator2.next()).done;) { - var _c = _step2.value; - if (_c.city == "Seattle") { - _seattlers.push({ name: _c.name, age: _c.age }); + var c = _step2.value; + if (c.city == "Seattle") { + _seattlers.push({ name: c.name, age: c.age }); } } } diff --git a/test/fixtures/transformation/es7-array-comprehension/single-if/expected.js b/test/fixtures/transformation/es7-array-comprehension/single-if/expected.js index 25915fc7f0..6e95f9cadc 100644 --- a/test/fixtures/transformation/es7-array-comprehension/single-if/expected.js +++ b/test/fixtures/transformation/es7-array-comprehension/single-if/expected.js @@ -4,9 +4,9 @@ var arr = (function () { var _arr = []; for (var _iterator = nums[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { - var _i = _step.value; - if (_i > 1) { - _arr.push(_i * _i); + var i = _step.value; + if (i > 1) { + _arr.push(i * i); } } diff --git a/test/fixtures/transformation/es7-exponentian-operator/comprehensive/exec.js b/test/fixtures/transformation/es7-exponentian-operator/comprehensive/exec.js new file mode 100644 index 0000000000..5a7ac52277 --- /dev/null +++ b/test/fixtures/transformation/es7-exponentian-operator/comprehensive/exec.js @@ -0,0 +1,21 @@ +assert.equal(8, 2 ** 3); +assert.equal(24, 3 * 2 ** 3); +var x = 2; +assert.equal(8, 2 ** ++x); +assert.equal(1, (2 ** -1) * 2); + +var calls = 0; +var q = {q: 3}; +var o = { + get p() { + calls++; + return q; + } +}; + +o.p.q **= 2; +assert.equal(1, calls); +assert.equal(9, o.p.q); + +assert.equal(512, 2 ** (3 ** 2)); +assert.equal(512, 2 ** 3 ** 2); From 93d99077e5306c493eb662d30a85208193b39751 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 12:52:16 +1100 Subject: [PATCH 092/129] Revert "add loose mode to spread and destructuring" This reverts commit 95d9f596687cd3fb4ce4d78dc6c7606128b74209. --- .../transformers/es6-destructuring.js | 17 ++++------- .../transformation/transformers/es6-spread.js | 8 ++--- .../es6-destructuring-loose/array/actual.js | 1 - .../es6-destructuring-loose/array/expected.js | 10 ------- .../assignment-expression/actual.js | 1 - .../assignment-expression/expected.js | 4 --- .../assignment-statement/actual.js | 1 - .../assignment-statement/expected.js | 7 ----- .../es6-destructuring-loose/empty/actual.js | 1 - .../es6-destructuring-loose/empty/expected.js | 10 ------- .../es7-object-rest/actual.js | 3 -- .../es7-object-rest/expected.js | 23 -------------- .../es7-object-rest/options.json | 3 -- .../es6-destructuring-loose/for-in/actual.js | 3 -- .../for-in/expected.js | 8 ----- .../es6-destructuring-loose/for-of/actual.js | 3 -- .../for-of/expected.js | 9 ------ .../member-expression/actual.js | 1 - .../member-expression/expected.js | 7 ----- .../es6-destructuring-loose/mixed/actual.js | 1 - .../es6-destructuring-loose/mixed/expected.js | 8 ----- .../multiple/actual.js | 2 -- .../multiple/expected.js | 5 ---- .../object-advanced/actual.js | 1 - .../object-advanced/expected.js | 6 ---- .../object-basic/actual.js | 1 - .../object-basic/expected.js | 4 --- .../es6-destructuring-loose/options.json | 3 -- .../parameters/actual.js | 15 ---------- .../parameters/expected.js | 30 ------------------- .../es6-destructuring-loose/spread/actual.js | 5 ---- .../spread/expected.js | 12 -------- .../es6-destructuring/parameters/expected.js | 16 +++++----- .../exec-block-scoped-2/exec.js | 12 -------- .../arguments-array/actual.js | 9 ------ .../arguments-array/expected.js | 11 ------- .../arguments-concat/actual.js | 9 ------ .../arguments-concat/expected.js | 11 ------- .../es6-spread-loose/arguments/actual.js | 9 ------ .../es6-spread-loose/arguments/expected.js | 11 ------- .../array-literal-first/actual.js | 1 - .../array-literal-first/expected.js | 3 -- .../array-literal-middle/actual.js | 1 - .../array-literal-middle/expected.js | 3 -- .../array-literal-multiple/actual.js | 1 - .../array-literal-multiple/expected.js | 3 -- .../es6-spread-loose/array-literals/actual.js | 1 - .../array-literals/expected.js | 3 -- .../actual.js | 1 - .../expected.js | 4 --- .../actual.js | 1 - .../expected.js | 4 --- .../actual.js | 2 -- .../expected.js | 5 ---- .../actual.js | 2 -- .../expected.js | 5 ---- .../method-call-array-literal/actual.js | 1 - .../method-call-array-literal/expected.js | 3 -- .../method-call-first/actual.js | 1 - .../method-call-first/expected.js | 3 -- .../method-call-middle/actual.js | 1 - .../method-call-middle/expected.js | 3 -- .../method-call-multiple-args/actual.js | 1 - .../method-call-multiple-args/expected.js | 3 -- .../method-call-multiple/actual.js | 1 - .../method-call-multiple/expected.js | 3 -- .../method-call-single-arg/actual.js | 1 - .../method-call-single-arg/expected.js | 3 -- .../es6-spread-loose/new-expression/actual.js | 2 -- .../new-expression/expected.js | 12 -------- .../es6-spread-loose/options.json | 3 -- .../es6-spread-loose/single/actual.js | 1 - .../es6-spread-loose/single/expected.js | 3 -- .../arguments/actual.js | 5 ---- .../arguments/expected.js | 17 ----------- .../array-expression-single-if/actual.js | 1 - .../array-expression-single-if/expected.js | 14 --------- .../array-expression-single/actual.js | 1 - .../array-expression-single/expected.js | 12 -------- .../multiple-if/actual.js | 1 - .../multiple-if/expected.js | 17 ----------- .../multiple/actual.js | 1 - .../multiple/expected.js | 15 ---------- .../options.json | 4 --- .../single-if/actual.js | 1 - .../single-if/expected.js | 14 --------- .../single/actual.js | 1 - .../single/expected.js | 12 -------- .../this/actual.js | 5 ---- .../this/expected.js | 17 ----------- .../options.json | 4 --- .../simple/exec.js | 6 ---- 92 files changed, 15 insertions(+), 519 deletions(-) delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/array/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/array/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/assignment-expression/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/assignment-expression/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/assignment-statement/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/assignment-statement/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/empty/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/empty/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/options.json delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/for-in/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/for-in/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/for-of/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/for-of/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/member-expression/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/member-expression/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/mixed/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/mixed/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/multiple/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/multiple/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/object-advanced/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/object-advanced/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/object-basic/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/object-basic/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/options.json delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/parameters/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/parameters/expected.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/spread/actual.js delete mode 100644 test/fixtures/transformation/es6-destructuring-loose/spread/expected.js delete mode 100644 test/fixtures/transformation/es6-let-scoping/exec-block-scoped-2/exec.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/arguments-array/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/arguments-array/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/arguments-concat/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/arguments-concat/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/arguments/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/arguments/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/array-literal-first/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/array-literal-first/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/array-literal-middle/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/array-literal-middle/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/array-literal-multiple/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/array-literal-multiple/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/array-literals/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/array-literals/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-array-literal/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-array-literal/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-first/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-first/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-middle/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-middle/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-multiple/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-multiple/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-single-arg/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/method-call-single-arg/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/new-expression/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/new-expression/expected.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/options.json delete mode 100644 test/fixtures/transformation/es6-spread-loose/single/actual.js delete mode 100644 test/fixtures/transformation/es6-spread-loose/single/expected.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/arguments/actual.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/arguments/expected.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/actual.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/expected.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/actual.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/expected.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/actual.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/expected.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/multiple/actual.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/multiple/expected.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/options.json delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/single-if/actual.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/single-if/expected.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/single/actual.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/single/expected.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/this/actual.js delete mode 100644 test/fixtures/transformation/es7-array-comprehension-loose/this/expected.js delete mode 100644 test/fixtures/transformation/es7-generator-comprehension-loose/options.json delete mode 100644 test/fixtures/transformation/es7-generator-comprehension-loose/simple/exec.js diff --git a/lib/6to5/transformation/transformers/es6-destructuring.js b/lib/6to5/transformation/transformers/es6-destructuring.js index 33929faadd..8b1f5ae882 100644 --- a/lib/6to5/transformation/transformers/es6-destructuring.js +++ b/lib/6to5/transformation/transformers/es6-destructuring.js @@ -7,7 +7,7 @@ var buildVariableAssign = function (opts, id, init) { if (t.isMemberExpression(id)) op = "="; if (op) { - return t.expressionStatement(t.assignmentExpression(op, id, init)); + return t.expressionStatement(t.assignmentExpression("=", id, init)); } else { return t.variableDeclaration(opts.kind, [ t.variableDeclarator(id, init) @@ -95,12 +95,7 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) { } } - var isLoose = opts.file.isLoose("destructuring"); - var toArray = parentId; - - if (!isLoose) { - toArray = opts.file.toArray(parentId, !hasSpreadElement && pattern.elements.length); - } + var toArray = opts.file.toArray(parentId, !hasSpreadElement && pattern.elements.length); var _parentId = opts.scope.generateUidBasedOnNode(parentId, opts.file); nodes.push(t.variableDeclaration("var", [ @@ -112,14 +107,12 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) { var elem = pattern.elements[i]; if (!elem) continue; + i = +i; + var newPatternId; if (t.isSpreadElement(elem)) { - newPatternId = parentId; - - if (!isLoose) { - newPatternId = opts.file.toArray(parentId); - } + newPatternId = opts.file.toArray(parentId); if (i > 0) { newPatternId = t.callExpression(t.memberExpression(newPatternId, t.identifier("slice")), [t.literal(i)]); diff --git a/lib/6to5/transformation/transformers/es6-spread.js b/lib/6to5/transformation/transformers/es6-spread.js index 2b9865fce5..4fa64b5e2b 100644 --- a/lib/6to5/transformation/transformers/es6-spread.js +++ b/lib/6to5/transformation/transformers/es6-spread.js @@ -2,11 +2,7 @@ var t = require("../../types"); var _ = require("lodash"); var getSpreadLiteral = function (spread, file) { - if (file.isLoose("spread")) { - return spread.argument; - } else { - return file.toArray(spread.argument); - } + return file.toArray(spread.argument); }; var hasSpread = function (nodes) { @@ -68,7 +64,7 @@ exports.CallExpression = function (node, parent, scope, context, file) { node.arguments = []; var nodes; - if (args.length === 1 && args[0].argument.name === "arguments") { + if (args.length === 1 && args[0].argument.name === 'arguments') { nodes = [args[0].argument]; } else { nodes = build(args, file); diff --git a/test/fixtures/transformation/es6-destructuring-loose/array/actual.js b/test/fixtures/transformation/es6-destructuring-loose/array/actual.js deleted file mode 100644 index 10aa8799ed..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/array/actual.js +++ /dev/null @@ -1 +0,0 @@ -var [a, [b], [c], d] = ["hello", [", ", "junk"], ["world"]]; diff --git a/test/fixtures/transformation/es6-destructuring-loose/array/expected.js b/test/fixtures/transformation/es6-destructuring-loose/array/expected.js deleted file mode 100644 index 2b7403faed..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/array/expected.js +++ /dev/null @@ -1,10 +0,0 @@ -"use strict"; - -var _ref = ["hello", [", ", "junk"], ["world"]]; - -var a = _ref[0]; -var _ref$1 = _ref[1]; -var b = _ref$1[0]; -var _ref$2 = _ref[2]; -var c = _ref$2[0]; -var d = _ref[3]; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/assignment-expression/actual.js b/test/fixtures/transformation/es6-destructuring-loose/assignment-expression/actual.js deleted file mode 100644 index 2ebf93ff16..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/assignment-expression/actual.js +++ /dev/null @@ -1 +0,0 @@ -console.log([x] = [123]); diff --git a/test/fixtures/transformation/es6-destructuring-loose/assignment-expression/expected.js b/test/fixtures/transformation/es6-destructuring-loose/assignment-expression/expected.js deleted file mode 100644 index b7721a94bf..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/assignment-expression/expected.js +++ /dev/null @@ -1,4 +0,0 @@ -"use strict"; - -var _temp, _temp2; -console.log((_temp = [123], _temp2 = _temp, x = _temp2[0], _temp)); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/assignment-statement/actual.js b/test/fixtures/transformation/es6-destructuring-loose/assignment-statement/actual.js deleted file mode 100644 index d14ce7e47d..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/assignment-statement/actual.js +++ /dev/null @@ -1 +0,0 @@ -[a, b] = f(); diff --git a/test/fixtures/transformation/es6-destructuring-loose/assignment-statement/expected.js b/test/fixtures/transformation/es6-destructuring-loose/assignment-statement/expected.js deleted file mode 100644 index e1feb2d7fe..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/assignment-statement/expected.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; - -var _ref = f(); - -var _ref2 = _ref; -a = _ref2[0]; -b = _ref2[1]; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/empty/actual.js b/test/fixtures/transformation/es6-destructuring-loose/empty/actual.js deleted file mode 100644 index a70e49fdd6..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/empty/actual.js +++ /dev/null @@ -1 +0,0 @@ -var [, a, [b], [c], d] = ["foo", "hello", [", ", "junk"], ["world"]]; diff --git a/test/fixtures/transformation/es6-destructuring-loose/empty/expected.js b/test/fixtures/transformation/es6-destructuring-loose/empty/expected.js deleted file mode 100644 index 1bdbe4fc65..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/empty/expected.js +++ /dev/null @@ -1,10 +0,0 @@ -"use strict"; - -var _ref = ["foo", "hello", [", ", "junk"], ["world"]]; - -var a = _ref[1]; -var _ref$2 = _ref[2]; -var b = _ref$2[0]; -var _ref$3 = _ref[3]; -var c = _ref$3[0]; -var d = _ref[4]; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/actual.js b/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/actual.js deleted file mode 100644 index b4877d6e45..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/actual.js +++ /dev/null @@ -1,3 +0,0 @@ -var { ...x } = z; -var { x, ...y } = z; -(function({ x, ...y }) { }) diff --git a/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/expected.js b/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/expected.js deleted file mode 100644 index dbb29cb23d..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/expected.js +++ /dev/null @@ -1,23 +0,0 @@ -"use strict"; - -var _objectWithoutProperties = function (obj, keys) { - var target = {}; - - for (var i in obj) { - if (keys.indexOf(i) >= 0) continue; - if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; - target[i] = obj[i]; - } - - return target; -}; - -var x = _objectWithoutProperties(z, []); - -var x = z.x; -var y = _objectWithoutProperties(z, ["x"]); - -(function (_ref) { - var x = _ref.x; - var y = _objectWithoutProperties(_ref, ["x"]); -}); diff --git a/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/options.json b/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/options.json deleted file mode 100644 index 252f473a73..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/es7-object-rest/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "experimental": true -} diff --git a/test/fixtures/transformation/es6-destructuring-loose/for-in/actual.js b/test/fixtures/transformation/es6-destructuring-loose/for-in/actual.js deleted file mode 100644 index 87fe7e7dde..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/for-in/actual.js +++ /dev/null @@ -1,3 +0,0 @@ -for (var [name, value] in obj) { - print("Name: " + name + ", Value: " + value); -} diff --git a/test/fixtures/transformation/es6-destructuring-loose/for-in/expected.js b/test/fixtures/transformation/es6-destructuring-loose/for-in/expected.js deleted file mode 100644 index 787fa1df42..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/for-in/expected.js +++ /dev/null @@ -1,8 +0,0 @@ -"use strict"; - -for (var _ref in obj) { - var _ref2 = _ref; - var name = _ref2[0]; - var value = _ref2[1]; - print("Name: " + name + ", Value: " + value); -} \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/for-of/actual.js b/test/fixtures/transformation/es6-destructuring-loose/for-of/actual.js deleted file mode 100644 index 5873553d65..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/for-of/actual.js +++ /dev/null @@ -1,3 +0,0 @@ -for (var [ name, before, after ] of this.test.expectation.registers) { - -} diff --git a/test/fixtures/transformation/es6-destructuring-loose/for-of/expected.js b/test/fixtures/transformation/es6-destructuring-loose/for-of/expected.js deleted file mode 100644 index 314c03654c..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/for-of/expected.js +++ /dev/null @@ -1,9 +0,0 @@ -"use strict"; - -for (var _iterator = this.test.expectation.registers[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { - var _ref = _step.value; - var _ref2 = _ref; - var name = _ref2[0]; - var before = _ref2[1]; - var after = _ref2[2]; -} \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/member-expression/actual.js b/test/fixtures/transformation/es6-destructuring-loose/member-expression/actual.js deleted file mode 100644 index bd5e42d7d1..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/member-expression/actual.js +++ /dev/null @@ -1 +0,0 @@ -[this.foo, this.bar] = [1, 2]; diff --git a/test/fixtures/transformation/es6-destructuring-loose/member-expression/expected.js b/test/fixtures/transformation/es6-destructuring-loose/member-expression/expected.js deleted file mode 100644 index 7e7353b88f..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/member-expression/expected.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; - -var _ref = [1, 2]; - -var _ref2 = _ref; -this.foo = _ref2[0]; -this.bar = _ref2[1]; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/mixed/actual.js b/test/fixtures/transformation/es6-destructuring-loose/mixed/actual.js deleted file mode 100644 index 79ac2cb22f..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/mixed/actual.js +++ /dev/null @@ -1 +0,0 @@ -var {topLeft: [x1, y1], bottomRight: [x2, y2] } = rect; diff --git a/test/fixtures/transformation/es6-destructuring-loose/mixed/expected.js b/test/fixtures/transformation/es6-destructuring-loose/mixed/expected.js deleted file mode 100644 index 7f44ca9a37..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/mixed/expected.js +++ /dev/null @@ -1,8 +0,0 @@ -"use strict"; - -var _rect$topLeft = rect.topLeft; -var x1 = _rect$topLeft[0]; -var y1 = _rect$topLeft[1]; -var _rect$bottomRight = rect.bottomRight; -var x2 = _rect$bottomRight[0]; -var y2 = _rect$bottomRight[1]; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-destructuring-loose/multiple/actual.js b/test/fixtures/transformation/es6-destructuring-loose/multiple/actual.js deleted file mode 100644 index 09249c483c..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/multiple/actual.js +++ /dev/null @@ -1,2 +0,0 @@ -var { x, y } = coords, - foo = "bar"; diff --git a/test/fixtures/transformation/es6-destructuring-loose/multiple/expected.js b/test/fixtures/transformation/es6-destructuring-loose/multiple/expected.js deleted file mode 100644 index c782335937..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/multiple/expected.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict"; - -var x = coords.x; -var y = coords.y; -var foo = "bar"; diff --git a/test/fixtures/transformation/es6-destructuring-loose/object-advanced/actual.js b/test/fixtures/transformation/es6-destructuring-loose/object-advanced/actual.js deleted file mode 100644 index c4b85492cf..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/object-advanced/actual.js +++ /dev/null @@ -1 +0,0 @@ -var {topLeft: {x: x1, y: y1}, bottomRight: {x: x2, y: y2}} = rect; diff --git a/test/fixtures/transformation/es6-destructuring-loose/object-advanced/expected.js b/test/fixtures/transformation/es6-destructuring-loose/object-advanced/expected.js deleted file mode 100644 index d3ee04ea05..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/object-advanced/expected.js +++ /dev/null @@ -1,6 +0,0 @@ -"use strict"; - -var x1 = rect.topLeft.x; -var y1 = rect.topLeft.y; -var x2 = rect.bottomRight.x; -var y2 = rect.bottomRight.y; diff --git a/test/fixtures/transformation/es6-destructuring-loose/object-basic/actual.js b/test/fixtures/transformation/es6-destructuring-loose/object-basic/actual.js deleted file mode 100644 index dae014575e..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/object-basic/actual.js +++ /dev/null @@ -1 +0,0 @@ -var { x, y } = coords; diff --git a/test/fixtures/transformation/es6-destructuring-loose/object-basic/expected.js b/test/fixtures/transformation/es6-destructuring-loose/object-basic/expected.js deleted file mode 100644 index 97601e8b4e..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/object-basic/expected.js +++ /dev/null @@ -1,4 +0,0 @@ -"use strict"; - -var x = coords.x; -var y = coords.y; diff --git a/test/fixtures/transformation/es6-destructuring-loose/options.json b/test/fixtures/transformation/es6-destructuring-loose/options.json deleted file mode 100644 index 77a73984e1..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "loose": ["destructuring"] -} diff --git a/test/fixtures/transformation/es6-destructuring-loose/parameters/actual.js b/test/fixtures/transformation/es6-destructuring-loose/parameters/actual.js deleted file mode 100644 index 896a7ce416..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/parameters/actual.js +++ /dev/null @@ -1,15 +0,0 @@ -function somethingAdvanced({topLeft: {x: x1, y: y1}, bottomRight: {x: x2, y: y2}}){ - -} - -function unpackObject({title: title, author: author}) { - return title + ' ' + author; -} - -console.log(unpackObject({title: 'title', author: 'author'})); - -var unpackArray = function ([a, b, c], [x, y, z]) { - return a+b+c; -}; - -console.log(unpackArray(['hello', ', ', 'world'], [1, 2, 3])); diff --git a/test/fixtures/transformation/es6-destructuring-loose/parameters/expected.js b/test/fixtures/transformation/es6-destructuring-loose/parameters/expected.js deleted file mode 100644 index 3fd491d5b0..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/parameters/expected.js +++ /dev/null @@ -1,30 +0,0 @@ -"use strict"; - -function somethingAdvanced(_ref) { - var x1 = _ref.topLeft.x; - var y1 = _ref.topLeft.y; - var x2 = _ref.bottomRight.x; - var y2 = _ref.bottomRight.y; -} - -function unpackObject(_ref2) { - var title = _ref2.title; - var author = _ref2.author; - return title + " " + author; -} - -console.log(unpackObject({ title: "title", author: "author" })); - -var unpackArray = function (_ref3, _ref4) { - var _ref32 = _ref3; - var a = _ref32[0]; - var b = _ref32[1]; - var c = _ref32[2]; - var _ref42 = _ref4; - var x = _ref42[0]; - var y = _ref42[1]; - var z = _ref42[2]; - return a + b + c; -}; - -console.log(unpackArray(["hello", ", ", "world"], [1, 2, 3])); diff --git a/test/fixtures/transformation/es6-destructuring-loose/spread/actual.js b/test/fixtures/transformation/es6-destructuring-loose/spread/actual.js deleted file mode 100644 index 0ef8f028ed..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/spread/actual.js +++ /dev/null @@ -1,5 +0,0 @@ -var isSorted = ([x, y, ...wow]) => { - if (!zs.length) return true - if (y > x) return isSorted(zs) - return false -}; diff --git a/test/fixtures/transformation/es6-destructuring-loose/spread/expected.js b/test/fixtures/transformation/es6-destructuring-loose/spread/expected.js deleted file mode 100644 index 03400c9581..0000000000 --- a/test/fixtures/transformation/es6-destructuring-loose/spread/expected.js +++ /dev/null @@ -1,12 +0,0 @@ -"use strict"; - -var isSorted = function (_ref) { - var _ref2 = _ref; - var x = _ref2[0]; - var y = _ref2[1]; - var wow = _ref2.slice(2); - - if (!zs.length) return true; - if (y > x) return isSorted(zs); - return false; -}; diff --git a/test/fixtures/transformation/es6-destructuring/parameters/expected.js b/test/fixtures/transformation/es6-destructuring/parameters/expected.js index 830eb15edc..a6a1d3dfe6 100644 --- a/test/fixtures/transformation/es6-destructuring/parameters/expected.js +++ b/test/fixtures/transformation/es6-destructuring/parameters/expected.js @@ -32,16 +32,16 @@ function unpackObject(_ref2) { console.log(unpackObject({ title: "title", author: "author" })); var unpackArray = function (_ref3, _ref4) { - var _ref32 = _slicedToArray(_ref3, 3); + var _ref3 = _slicedToArray(_ref3, 3); - var a = _ref32[0]; - var b = _ref32[1]; - var c = _ref32[2]; - var _ref42 = _slicedToArray(_ref4, 3); + var a = _ref3[0]; + var b = _ref3[1]; + var c = _ref3[2]; + var _ref4 = _slicedToArray(_ref4, 3); - var x = _ref42[0]; - var y = _ref42[1]; - var z = _ref42[2]; + var x = _ref4[0]; + var y = _ref4[1]; + var z = _ref4[2]; return a + b + c; }; diff --git a/test/fixtures/transformation/es6-let-scoping/exec-block-scoped-2/exec.js b/test/fixtures/transformation/es6-let-scoping/exec-block-scoped-2/exec.js deleted file mode 100644 index e98bf47f5e..0000000000 --- a/test/fixtures/transformation/es6-let-scoping/exec-block-scoped-2/exec.js +++ /dev/null @@ -1,12 +0,0 @@ -assert.equal(() => { - let sum = 0; - let a = 0; - { - let a = 10; - for (let i = 0; i < a; i++) { - let a = 1; - sum += (() => a)(); - } - } - return sum; -}(), 10); diff --git a/test/fixtures/transformation/es6-spread-loose/arguments-array/actual.js b/test/fixtures/transformation/es6-spread-loose/arguments-array/actual.js deleted file mode 100644 index e18c362b1b..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/arguments-array/actual.js +++ /dev/null @@ -1,9 +0,0 @@ -function foo() { - return bar([...arguments]); -} - -function bar(one, two, three) { - return [one, two, three]; -} - -foo("foo", "bar"); diff --git a/test/fixtures/transformation/es6-spread-loose/arguments-array/expected.js b/test/fixtures/transformation/es6-spread-loose/arguments-array/expected.js deleted file mode 100644 index 92e9e6ff46..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/arguments-array/expected.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict"; - -function foo() { - return bar([].concat(arguments)); -} - -function bar(one, two, three) { - return [one, two, three]; -} - -foo("foo", "bar"); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-spread-loose/arguments-concat/actual.js b/test/fixtures/transformation/es6-spread-loose/arguments-concat/actual.js deleted file mode 100644 index a6fdb21bef..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/arguments-concat/actual.js +++ /dev/null @@ -1,9 +0,0 @@ -function foo() { - return bar("test", ...arguments); -} - -function bar(one, two, three) { - return [one, two, three]; -} - -foo("foo", "bar"); diff --git a/test/fixtures/transformation/es6-spread-loose/arguments-concat/expected.js b/test/fixtures/transformation/es6-spread-loose/arguments-concat/expected.js deleted file mode 100644 index b0bbaef08f..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/arguments-concat/expected.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict"; - -function foo() { - return bar.apply(undefined, ["test"].concat(arguments)); -} - -function bar(one, two, three) { - return [one, two, three]; -} - -foo("foo", "bar"); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-spread-loose/arguments/actual.js b/test/fixtures/transformation/es6-spread-loose/arguments/actual.js deleted file mode 100644 index 476da431e8..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/arguments/actual.js +++ /dev/null @@ -1,9 +0,0 @@ -function foo() { - return bar(...arguments); -} - -function bar(one, two, three) { - return [one, two, three]; -} - -foo("foo", "bar"); diff --git a/test/fixtures/transformation/es6-spread-loose/arguments/expected.js b/test/fixtures/transformation/es6-spread-loose/arguments/expected.js deleted file mode 100644 index 52a18f07e7..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/arguments/expected.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict"; - -function foo() { - return bar.apply(undefined, arguments); -} - -function bar(one, two, three) { - return [one, two, three]; -} - -foo("foo", "bar"); diff --git a/test/fixtures/transformation/es6-spread-loose/array-literal-first/actual.js b/test/fixtures/transformation/es6-spread-loose/array-literal-first/actual.js deleted file mode 100644 index 3857ab97d4..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/array-literal-first/actual.js +++ /dev/null @@ -1 +0,0 @@ -var lyrics = [...parts, "head", "and", "toes"]; diff --git a/test/fixtures/transformation/es6-spread-loose/array-literal-first/expected.js b/test/fixtures/transformation/es6-spread-loose/array-literal-first/expected.js deleted file mode 100644 index 3d1401d537..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/array-literal-first/expected.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; - -var lyrics = [].concat(parts, ["head", "and", "toes"]); diff --git a/test/fixtures/transformation/es6-spread-loose/array-literal-middle/actual.js b/test/fixtures/transformation/es6-spread-loose/array-literal-middle/actual.js deleted file mode 100644 index 9b354ad7cf..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/array-literal-middle/actual.js +++ /dev/null @@ -1 +0,0 @@ -var a = [b, ...c, d]; diff --git a/test/fixtures/transformation/es6-spread-loose/array-literal-middle/expected.js b/test/fixtures/transformation/es6-spread-loose/array-literal-middle/expected.js deleted file mode 100644 index cc40455aeb..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/array-literal-middle/expected.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; - -var a = [b].concat(c, [d]); diff --git a/test/fixtures/transformation/es6-spread-loose/array-literal-multiple/actual.js b/test/fixtures/transformation/es6-spread-loose/array-literal-multiple/actual.js deleted file mode 100644 index e65edc597a..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/array-literal-multiple/actual.js +++ /dev/null @@ -1 +0,0 @@ -var a = [b, ...c, d, e, ...f]; diff --git a/test/fixtures/transformation/es6-spread-loose/array-literal-multiple/expected.js b/test/fixtures/transformation/es6-spread-loose/array-literal-multiple/expected.js deleted file mode 100644 index 5b55e67414..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/array-literal-multiple/expected.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; - -var a = [b].concat(c, [d, e], f); diff --git a/test/fixtures/transformation/es6-spread-loose/array-literals/actual.js b/test/fixtures/transformation/es6-spread-loose/array-literals/actual.js deleted file mode 100644 index 5ebb80da66..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/array-literals/actual.js +++ /dev/null @@ -1 +0,0 @@ -var lyrics = ["head", "and", "toes", ...parts]; diff --git a/test/fixtures/transformation/es6-spread-loose/array-literals/expected.js b/test/fixtures/transformation/es6-spread-loose/array-literals/expected.js deleted file mode 100644 index 4b402a9818..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/array-literals/expected.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; - -var lyrics = ["head", "and", "toes"].concat(parts); diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/actual.js b/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/actual.js deleted file mode 100644 index 305586e2a6..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/actual.js +++ /dev/null @@ -1 +0,0 @@ -obj[method](foo, bar, ...args); diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/expected.js b/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/expected.js deleted file mode 100644 index 6303149d3c..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-multiple-args/expected.js +++ /dev/null @@ -1,4 +0,0 @@ -"use strict"; - -var _obj; -(_obj = obj)[method].apply(_obj, [foo, bar].concat(args)); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/actual.js b/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/actual.js deleted file mode 100644 index 65a21cba1e..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/actual.js +++ /dev/null @@ -1 +0,0 @@ -obj[method](...args); diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/expected.js b/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/expected.js deleted file mode 100644 index 786a1efbd7..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/contexted-computed-method-call-single-arg/expected.js +++ /dev/null @@ -1,4 +0,0 @@ -"use strict"; - -var _obj; -(_obj = obj)[method].apply(_obj, args); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/actual.js b/test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/actual.js deleted file mode 100644 index 7dc7d142a1..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/actual.js +++ /dev/null @@ -1,2 +0,0 @@ -foob.add(foo, bar, ...numbers); -foob.test.add(foo, bar, ...numbers); diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/expected.js b/test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/expected.js deleted file mode 100644 index 5c9d6466ef..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/contexted-method-call-multiple-args/expected.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict"; - -var _foob, _foob$test; -(_foob = foob).add.apply(_foob, [foo, bar].concat(numbers)); -(_foob$test = foob.test).add.apply(_foob$test, [foo, bar].concat(numbers)); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/actual.js b/test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/actual.js deleted file mode 100644 index ef061e17a8..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/actual.js +++ /dev/null @@ -1,2 +0,0 @@ -foob.add(...numbers); -foob.test.add(...numbers); diff --git a/test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/expected.js b/test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/expected.js deleted file mode 100644 index f03c36ac76..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/contexted-method-call-single-arg/expected.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict"; - -var _foob, _foob$test; -(_foob = foob).add.apply(_foob, numbers); -(_foob$test = foob.test).add.apply(_foob$test, numbers); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-array-literal/actual.js b/test/fixtures/transformation/es6-spread-loose/method-call-array-literal/actual.js deleted file mode 100644 index 2d1a0bb716..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/method-call-array-literal/actual.js +++ /dev/null @@ -1 +0,0 @@ -f(...[1, 2, 3]); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-array-literal/expected.js b/test/fixtures/transformation/es6-spread-loose/method-call-array-literal/expected.js deleted file mode 100644 index a370047756..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/method-call-array-literal/expected.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; - -f.apply(undefined, [1, 2, 3]); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-first/actual.js b/test/fixtures/transformation/es6-spread-loose/method-call-first/actual.js deleted file mode 100644 index 0d63c0ea63..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/method-call-first/actual.js +++ /dev/null @@ -1 +0,0 @@ -add(...numbers, foo, bar); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-first/expected.js b/test/fixtures/transformation/es6-spread-loose/method-call-first/expected.js deleted file mode 100644 index 4700d54131..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/method-call-first/expected.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; - -add.apply(undefined, numbers.concat([foo, bar])); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-middle/actual.js b/test/fixtures/transformation/es6-spread-loose/method-call-middle/actual.js deleted file mode 100644 index b04a3f0223..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/method-call-middle/actual.js +++ /dev/null @@ -1 +0,0 @@ -add(foo, ...numbers, bar); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-middle/expected.js b/test/fixtures/transformation/es6-spread-loose/method-call-middle/expected.js deleted file mode 100644 index 74f6a108ac..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/method-call-middle/expected.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; - -add.apply(undefined, [foo].concat(numbers, [bar])); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/actual.js b/test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/actual.js deleted file mode 100644 index 0a2421b7b4..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/actual.js +++ /dev/null @@ -1 +0,0 @@ -add(foo, bar, ...numbers); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/expected.js b/test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/expected.js deleted file mode 100644 index e543eca3db..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/method-call-multiple-args/expected.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; - -add.apply(undefined, [foo, bar].concat(numbers)); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-multiple/actual.js b/test/fixtures/transformation/es6-spread-loose/method-call-multiple/actual.js deleted file mode 100644 index c06ed01cfb..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/method-call-multiple/actual.js +++ /dev/null @@ -1 +0,0 @@ -add(foo, ...numbers, bar, what, ...test); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-multiple/expected.js b/test/fixtures/transformation/es6-spread-loose/method-call-multiple/expected.js deleted file mode 100644 index 046ac08426..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/method-call-multiple/expected.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; - -add.apply(undefined, [foo].concat(numbers, [bar, what], test)); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-single-arg/actual.js b/test/fixtures/transformation/es6-spread-loose/method-call-single-arg/actual.js deleted file mode 100644 index a24aa1b09c..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/method-call-single-arg/actual.js +++ /dev/null @@ -1 +0,0 @@ -add(...numbers); diff --git a/test/fixtures/transformation/es6-spread-loose/method-call-single-arg/expected.js b/test/fixtures/transformation/es6-spread-loose/method-call-single-arg/expected.js deleted file mode 100644 index ef2f902647..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/method-call-single-arg/expected.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; - -add.apply(undefined, numbers); diff --git a/test/fixtures/transformation/es6-spread-loose/new-expression/actual.js b/test/fixtures/transformation/es6-spread-loose/new-expression/actual.js deleted file mode 100644 index 72800dd42b..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/new-expression/actual.js +++ /dev/null @@ -1,2 +0,0 @@ -new Numbers(...nums); -new Numbers(1, ...nums); diff --git a/test/fixtures/transformation/es6-spread-loose/new-expression/expected.js b/test/fixtures/transformation/es6-spread-loose/new-expression/expected.js deleted file mode 100644 index 5677c95bec..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/new-expression/expected.js +++ /dev/null @@ -1,12 +0,0 @@ -"use strict"; - -var _applyConstructor = function (Constructor, args) { - var instance = Object.create(Constructor.prototype); - - var result = Constructor.apply(instance, args); - - return result != null && (typeof result == "object" || typeof result == "function") ? result : instance; -}; - -_applyConstructor(Numbers, nums); -_applyConstructor(Numbers, [1].concat(nums)); diff --git a/test/fixtures/transformation/es6-spread-loose/options.json b/test/fixtures/transformation/es6-spread-loose/options.json deleted file mode 100644 index 536a7d71d2..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "loose": "spread" -} diff --git a/test/fixtures/transformation/es6-spread-loose/single/actual.js b/test/fixtures/transformation/es6-spread-loose/single/actual.js deleted file mode 100644 index 6541257708..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/single/actual.js +++ /dev/null @@ -1 +0,0 @@ -[...foo]; diff --git a/test/fixtures/transformation/es6-spread-loose/single/expected.js b/test/fixtures/transformation/es6-spread-loose/single/expected.js deleted file mode 100644 index 0d7e55ac18..0000000000 --- a/test/fixtures/transformation/es6-spread-loose/single/expected.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; - -[].concat(foo); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/arguments/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/arguments/actual.js deleted file mode 100644 index 95f9a4df57..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/arguments/actual.js +++ /dev/null @@ -1,5 +0,0 @@ -function add() { - return [for (i of [1, 2, 3]) i * arguments[0]]; -} - -add(5); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/arguments/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/arguments/expected.js deleted file mode 100644 index 39115d87d3..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/arguments/expected.js +++ /dev/null @@ -1,17 +0,0 @@ -"use strict"; - -function add() { - var _arguments = arguments; - return (function () { - var _ref = []; - - for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { - var i = _step.value; - _ref.push(i * _arguments[0]); - } - - return _ref; - })(); -} - -add(5); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/actual.js deleted file mode 100644 index 4882336b9f..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/actual.js +++ /dev/null @@ -1 +0,0 @@ -var arr = [for (i of [1, 2, 3]) if (i > 1) i * i]; diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/expected.js deleted file mode 100644 index 0f3f625138..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single-if/expected.js +++ /dev/null @@ -1,14 +0,0 @@ -"use strict"; - -var arr = (function () { - var _arr = []; - - for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { - var i = _step.value; - if (i > 1) { - _arr.push(i * i); - } - } - - return _arr; -})(); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/actual.js deleted file mode 100644 index 2087ae351b..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/actual.js +++ /dev/null @@ -1 +0,0 @@ -var arr = [for (i of [1, 2, 3]) i * i]; diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/expected.js deleted file mode 100644 index c3ae8d9432..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/array-expression-single/expected.js +++ /dev/null @@ -1,12 +0,0 @@ -"use strict"; - -var arr = (function () { - var _arr = []; - - for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { - var i = _step.value; - _arr.push(i * i); - } - - return _arr; -})(); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/actual.js deleted file mode 100644 index 754bb7e894..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/actual.js +++ /dev/null @@ -1 +0,0 @@ -var seattlers = [for (customers of countries) for (c of customers) if (c.city == "Seattle") { name: c.name, age: c.age }]; diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/expected.js deleted file mode 100644 index 6b486b45f5..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/multiple-if/expected.js +++ /dev/null @@ -1,17 +0,0 @@ -"use strict"; - -var seattlers = (function () { - var _seattlers = []; - - for (var _iterator = countries[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { - var customers = _step.value; - for (var _iterator2 = customers[Symbol.iterator](), _step2; !(_step2 = _iterator2.next()).done;) { - var c = _step2.value; - if (c.city == "Seattle") { - _seattlers.push({ name: c.name, age: c.age }); - } - } - } - - return _seattlers; -})(); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/multiple/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/multiple/actual.js deleted file mode 100644 index f0aaa63c27..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/multiple/actual.js +++ /dev/null @@ -1 +0,0 @@ -var arr = [for (x of "abcdefgh".split("")) for (y of "12345678".split("")) x + y]; diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/multiple/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/multiple/expected.js deleted file mode 100644 index 05f2c6e76d..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/multiple/expected.js +++ /dev/null @@ -1,15 +0,0 @@ -"use strict"; - -var arr = (function () { - var _arr = []; - - for (var _iterator = "abcdefgh".split("")[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { - var x = _step.value; - for (var _iterator2 = "12345678".split("")[Symbol.iterator](), _step2; !(_step2 = _iterator2.next()).done;) { - var y = _step2.value; - _arr.push(x + y); - } - } - - return _arr; -})(); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/options.json b/test/fixtures/transformation/es7-array-comprehension-loose/options.json deleted file mode 100644 index 6e5e1c6b65..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/options.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "experimental": true, - "loose": "arrayComprehension" -} diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/single-if/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/single-if/actual.js deleted file mode 100644 index e2cf129386..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/single-if/actual.js +++ /dev/null @@ -1 +0,0 @@ -var arr = [for (i of nums) if (i > 1) i * i]; diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/single-if/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/single-if/expected.js deleted file mode 100644 index 6e95f9cadc..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/single-if/expected.js +++ /dev/null @@ -1,14 +0,0 @@ -"use strict"; - -var arr = (function () { - var _arr = []; - - for (var _iterator = nums[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { - var i = _step.value; - if (i > 1) { - _arr.push(i * i); - } - } - - return _arr; -})(); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/single/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/single/actual.js deleted file mode 100644 index 9dda19048b..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/single/actual.js +++ /dev/null @@ -1 +0,0 @@ -var arr = [for (i of nums) i * i]; diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/single/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/single/expected.js deleted file mode 100644 index 88e01e350a..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/single/expected.js +++ /dev/null @@ -1,12 +0,0 @@ -"use strict"; - -var arr = (function () { - var _arr = []; - - for (var _iterator = nums[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { - var i = _step.value; - _arr.push(i * i); - } - - return _arr; -})(); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/this/actual.js b/test/fixtures/transformation/es7-array-comprehension-loose/this/actual.js deleted file mode 100644 index 355ed48188..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/this/actual.js +++ /dev/null @@ -1,5 +0,0 @@ -function add() { - return [for (i of [1, 2, 3]) i * this.multiplier]; -} - -add.call({ multiplier: 5 }); diff --git a/test/fixtures/transformation/es7-array-comprehension-loose/this/expected.js b/test/fixtures/transformation/es7-array-comprehension-loose/this/expected.js deleted file mode 100644 index 5645b53dcc..0000000000 --- a/test/fixtures/transformation/es7-array-comprehension-loose/this/expected.js +++ /dev/null @@ -1,17 +0,0 @@ -"use strict"; - -function add() { - var _this = this; - return (function () { - var _ref = []; - - for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { - var i = _step.value; - _ref.push(i * _this.multiplier); - } - - return _ref; - })(); -} - -add.call({ multiplier: 5 }); diff --git a/test/fixtures/transformation/es7-generator-comprehension-loose/options.json b/test/fixtures/transformation/es7-generator-comprehension-loose/options.json deleted file mode 100644 index 4fdef2f3ff..0000000000 --- a/test/fixtures/transformation/es7-generator-comprehension-loose/options.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "experimental": true, - "loose": "generatorComprehension" -} diff --git a/test/fixtures/transformation/es7-generator-comprehension-loose/simple/exec.js b/test/fixtures/transformation/es7-generator-comprehension-loose/simple/exec.js deleted file mode 100644 index dd7ef4da0a..0000000000 --- a/test/fixtures/transformation/es7-generator-comprehension-loose/simple/exec.js +++ /dev/null @@ -1,6 +0,0 @@ -var nums = [1, 2, 3, 4, 5, 6]; -var multiples = (for (i of nums) if (i % 2) i * i); -assert.equal(multiples.next().value, 1); -assert.equal(multiples.next().value, 9); -assert.equal(multiples.next().value, 25); -assert.ok(multiples.next().done); From 436c488ee31fa7082f0ecdd7d0c72b2a669250a5 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 18:22:37 +1100 Subject: [PATCH 093/129] revamp let scoping transformer - closes #510 --- .../transformers/es6-let-scoping.js | 335 ++++++++---------- .../exec-block-scoped-2/exec.js | 12 + 2 files changed, 151 insertions(+), 196 deletions(-) create mode 100644 test/fixtures/transformation/es6-let-scoping/exec-block-scoped-2/exec.js diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index 65f8cf9d9c..8675611244 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -1,4 +1,5 @@ var traverse = require("../../traverse"); +var Scope = require("../../traverse/scope"); var util = require("../../util"); var t = require("../../types"); var _ = require("lodash"); @@ -79,8 +80,9 @@ function LetScoping(loopParent, block, parent, scope, file) { this.block = block; this.file = file; - this.letReferences = {}; - this.body = []; + this.outsideLetReferences = {}; + this.letReferences = {}; + this.body = []; } /** @@ -89,27 +91,91 @@ function LetScoping(loopParent, block, parent, scope, file) { LetScoping.prototype.run = function () { var block = this.block; - if (block._letDone) return; block._letDone = true; - this.info = this.getInfo(); - - // remap all let references that exist in upper scopes to their uid - this.remap(); - // this is a block within a `Function` so we can safely leave it be - if (t.isFunction(this.parent)) return this.noClosure(); + if (t.isFunction(this.parent)) return; - // this block has no let references so let's clean up - if (!this.info.keys.length) return this.noClosure(); + var needsClosure = this.getLetReferences(); + if (needsClosure) { + this.needsClosure(); + } else { + this.remap(); + } +}; - // returns whether or not there are any outside let references within any - // functions - var referencesInClosure = this.getLetReferences(); +/** + * Description + */ - // no need for a closure so let's clean up - if (!referencesInClosure) return this.noClosure(); +LetScoping.prototype.remap = function () { + var letRefs = this.letReferences; + var scope = this.scope; + var file = this.file; + + // alright, so since we aren't wrapping this block in a closure + // we have to check if any of our let variables collide with + // those in upper scopes and then if they do, generate a uid + // for them and replace all references with it + var remaps = {}; + + for (var key in letRefs) { + // just an Identifier node we collected in `getLetReferences` + // this is the defining identifier of a declaration + var ref = letRefs[key]; + + if (scope.parentHas(key)) { + var uid = file.generateUidIdentifier(ref.name, scope).name; + ref.name = uid; + + remaps[key] = { + node: ref, + uid: uid + }; + } + } + + // + + var replace = function (node, parent, scope, context, remaps) { + if (!t.isIdentifier(node)) return; + if (!t.isReferenced(node, parent)) return; + + var remap = remaps[node.name]; + if (!remap) return; + + var own = scope.get(node.name, true); + if (own === remap.node) { + node.name = remap.uid; + } else { + // scope already has it's own declaration that doesn't + // match the one we have a stored replacement for + if (context) context.skip(); + } + }; + + var traverseReplace = function (node, parent) { + replace(node, parent, scope, null, remaps); + traverse(node, { enter: replace }, scope, remaps); + }; + + var loopParent = this.loopParent; + if (loopParent) { + traverseReplace(loopParent.right, loopParent); + traverseReplace(loopParent.test, loopParent); + traverseReplace(loopParent.update, loopParent); + } + + traverse(this.block, { enter: replace }, scope, remaps); +}; + +/** + * Description + */ + +LetScoping.prototype.needsClosure = function () { + var block = this.block; // if we're inside of a for loop then we search to see if there are any // `break`s, `continue`s, `return`s etc @@ -118,22 +184,16 @@ LetScoping.prototype.run = function () { // hoist var references to retain scope this.hoistVarDeclarations(); - // set let references to plain var references - standardiseLets(this.info.declarators); - - // turn letReferences into an array - var letReferences = _.values(this.letReferences); + // turn outsideLetReferences into an array + var params = _.values(this.outsideLetReferences); // build the closure that we're going to wrap the block with - var fn = t.functionExpression(null, letReferences, t.blockStatement(block.body)); + var fn = t.functionExpression(null, params, t.blockStatement(block.body)); fn._aliasFunction = true; // replace the current block body with the one we're going to build block.body = this.body; - // change upper scope references with their uid if they have one - var params = this.getParams(letReferences); - // build a call and a unique id that we can assign the return value to var call = t.callExpression(fn, params); var ret = this.file.generateUidIdentifier("ret", this.scope); @@ -147,130 +207,77 @@ LetScoping.prototype.run = function () { this.build(ret, call); }; -/** - * There are no let references accessed within a closure so we can just turn the - * lets into vars. - */ - -LetScoping.prototype.noClosure = function () { - standardiseLets(this.info.declarators); -}; - -/** - * Traverse through block and replace all references that exist in a higher - * scope to their uids. - */ - -LetScoping.prototype.remap = function () { - var duplicates = this.info.duplicates; - var block = this.block; - var scope = this.scope; - - if (!this.info.hasDuplicates) return; - - var replace = function (node, parent, scope, context, duplicates) { - if (!t.isIdentifier(node)) return; - if (!t.isReferenced(node, parent)) return; - - var duplicate = duplicates[node.name]; - if (!duplicate) return; - - var own = scope.get(node.name, true); - if (own === duplicate.node) { - node.name = duplicate.uid; - } else { - // scope already has it's own declaration that doesn't - // match the one we have a stored replacement for - context.skip(); - } - }; - - var traverseReplace = function (node, parent) { - replace(node, parent); - traverse(node, { enter: replace }, scope, duplicates); - }; - - var loopParent = this.loopParent; - if (loopParent) { - traverseReplace(loopParent.right, loopParent); - traverseReplace(loopParent.test, loopParent); - traverseReplace(loopParent.update, loopParent); - } - - traverse(block, { enter: replace }, scope, duplicates); -}; - /** * Description - * - * @returns {Object} */ -LetScoping.prototype.getInfo = function () { +LetScoping.prototype.getLetReferences = function () { var block = this.block; - var scope = this.scope; - var file = this.file; - - var opts = { - // array of `Identifier` names of let variables that appear lexically out of - // this scope but should be accessible - eg. `ForOfStatement`.left - outsideKeys: [], - - // array of let `VariableDeclarator`s that are a part of this block - declarators: block._letDeclars || [], - - // object of duplicate ids and their aliases - if there's an `Identifier` - // name that's used in an upper scope we generate a unique id and replace - // all references with it - duplicates: {}, - - hasDuplicates: false, - - // array of `Identifier` names of let variables that are accessible within - // the current block - keys: [] - }; - - var duplicates = function (id, key) { - // there's a variable with this exact name in an upper scope so we need - // to generate a new name - if (scope.parentHas(key, true)) { - var duplicate = opts.duplicates[key] = { - uid: file.generateUidIdentifier(key, scope).name, - node: id - }; - id.name = duplicate.uid; - opts.hasDuplicates = true; - } - }; + var declarators = block._letDeclars || []; var declar; - for (var i = 0; i < opts.declarators.length; i++) { - declar = opts.declarators[i]; - - var keys = t.getIds(declar, true); - _.each(keys, duplicates); - keys = Object.keys(keys); - - opts.outsideKeys = opts.outsideKeys.concat(keys); - opts.keys = opts.keys.concat(keys); + // + for (var i = 0; i < declarators.length; i++) { + declar = declarators[i]; + _.extend(this.outsideLetReferences, t.getIds(declar, true)); } + // if (block.body) { for (i = 0; i < block.body.length; i++) { declar = block.body[i]; - if (!isLet(declar, block)) continue; - - var declars = t.getIds(declar, true); - for (var key in declars) { - duplicates(declars[key], key); - opts.keys.push(key); + if (isLet(declar, block)) { + declarators = declarators.concat(declar.declarations); } } } - return opts; + // + for (var i = 0; i < declarators.length; i++) { + var declar = declarators[i]; + var keys = t.getIds(declar, true); + _.extend(this.letReferences, keys); + } + + // set let references to plain var references + standardiseLets(declarators); + + var state = { + letReferences: this.letReferences, + closurify: false + }; + + // traverse through this block, stopping on functions and checking if they + // contain any local let references + traverse(this.block, { + enter: function (node, parent, scope, context, state) { + if (t.isFunction(node)) { + traverse(node, { + enter: function (node, parent) { + // not an identifier so we have no use + if (!t.isIdentifier(node)) return; + + // not a direct reference + if (!t.isReferenced(node, parent)) return; + + // this scope has a variable with the same name so it couldn't belong + // to our let scope + if (scope.hasOwn(node.name, true)) return; + + // not a part of our scope + if (!state.letReferences[node.name]) return; + + state.closurify = true; + } + }, scope, state); + + return context.skip(); + } + } + }, this.scope, state); + + return state.closurify; }; /** @@ -348,70 +355,6 @@ LetScoping.prototype.hoistVarDeclarations = function () { }, this.scope, this); }; -/** - * Build up a parameter list that we'll call our closure wrapper with, replacing - * all duplicate ids with their uid. - * - * @param {Array} params - * @returns {Array} - */ - -LetScoping.prototype.getParams = function (params) { - var info = this.info; - params = _.cloneDeep(params); - _.each(params, function (param) { - var duplicate = info.duplicates[param.name]; - if (duplicate) param.name = duplicate.uid; - }); - return params; -}; - -/** - * Get all let references within this block. Stopping whenever we reach another - * block. - */ - -LetScoping.prototype.getLetReferences = function () { - var state = { - self: this, - closurify: false - }; - - // traverse through this block, stopping on functions and checking if they - // contain any outside let references - traverse(this.block, { - enter: function (node, parent, scope, context, state) { - if (t.isFunction(node)) { - traverse(node, { - enter: function (node, parent) { - // not an identifier so we have no use - if (!t.isIdentifier(node)) return; - - // not a direct reference - if (!t.isReferenced(node, parent)) return; - - // this scope has a variable with the same name so it couldn't belong - // to our let scope - if (scope.hasOwn(node.name, true)) return; - - state.closurify = true; - - // this key doesn't appear just outside our scope - if (!_.contains(state.self.info.outsideKeys, node.name)) return; - - // push this badboy - state.self.letReferences[node.name] = node; - } - }, scope, state); - - return context.skip(); - } - } - }, this.scope, state); - - return state.closurify; -}; - /** * Turn a `VariableDeclaration` into an array of `AssignmentExpressions` with * their declarations hoisted to before the closure wrapper. diff --git a/test/fixtures/transformation/es6-let-scoping/exec-block-scoped-2/exec.js b/test/fixtures/transformation/es6-let-scoping/exec-block-scoped-2/exec.js new file mode 100644 index 0000000000..e98bf47f5e --- /dev/null +++ b/test/fixtures/transformation/es6-let-scoping/exec-block-scoped-2/exec.js @@ -0,0 +1,12 @@ +assert.equal(() => { + let sum = 0; + let a = 0; + { + let a = 10; + for (let i = 0; i < a; i++) { + let a = 1; + sum += (() => a)(); + } + } + return sum; +}(), 10); From 26395a86fa008ebb5bcdff84bdcaf37d74655929 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 18:23:03 +1100 Subject: [PATCH 094/129] add block scoped functions - fixes #514 --- lib/6to5/transformation/transform.js | 1 + .../spec-block-scoped-functions.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 lib/6to5/transformation/transformers/spec-block-scoped-functions.js diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 485ed26959..838ee5a90d 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -44,6 +44,7 @@ transform.moduleFormatters = { _.each({ specNoForInOfAssignment: require("./transformers/spec-no-for-in-of-assignment"), specSetters: require("./transformers/spec-setters"), + specBlockScopedFunctions: require("./transformers/spec-block-scoped-functions"), // playground malletOperator: require("./transformers/playground-mallet-operator"), diff --git a/lib/6to5/transformation/transformers/spec-block-scoped-functions.js b/lib/6to5/transformation/transformers/spec-block-scoped-functions.js new file mode 100644 index 0000000000..814937fd31 --- /dev/null +++ b/lib/6to5/transformation/transformers/spec-block-scoped-functions.js @@ -0,0 +1,19 @@ +var t = require("../../types"); + +exports.FunctionDeclaration = function (node, parent) { + if (t.isProgram(parent) || t.isExportDeclaration(parent)) { + return; + } + + var declar = t.variableDeclaration("let", [ + t.variableDeclarator(node.id, t.toExpression(node)) + ]); + + // hoist it up above everything else + declar._blockHoist = 2; + + // todo: name this + node.id = null; + + return declar; +}; From 74f5a73d31c28bf85d0c5169934583cd6b8f80b8 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 18:23:14 +1100 Subject: [PATCH 095/129] update traceur --- vendor/traceur | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/traceur b/vendor/traceur index 0c3dad3ae2..ed432fb5d4 160000 --- a/vendor/traceur +++ b/vendor/traceur @@ -1 +1 @@ -Subproject commit 0c3dad3ae217139b1a6b4b986cef57669e7b49c6 +Subproject commit ed432fb5d4e7575b05b54f68226eede97509c05e From d360bd5bb71ff5d6b608fa311b77486264c342be Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 18:23:27 +1100 Subject: [PATCH 096/129] update misc tests --- .../es6-destructuring/parameters/expected.js | 16 ++++++++-------- .../es7-abstract-references/private/expected.js | 14 +++++++------- .../comprehensive/exec.js | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/test/fixtures/transformation/es6-destructuring/parameters/expected.js b/test/fixtures/transformation/es6-destructuring/parameters/expected.js index a6a1d3dfe6..830eb15edc 100644 --- a/test/fixtures/transformation/es6-destructuring/parameters/expected.js +++ b/test/fixtures/transformation/es6-destructuring/parameters/expected.js @@ -32,16 +32,16 @@ function unpackObject(_ref2) { console.log(unpackObject({ title: "title", author: "author" })); var unpackArray = function (_ref3, _ref4) { - var _ref3 = _slicedToArray(_ref3, 3); + var _ref32 = _slicedToArray(_ref3, 3); - var a = _ref3[0]; - var b = _ref3[1]; - var c = _ref3[2]; - var _ref4 = _slicedToArray(_ref4, 3); + var a = _ref32[0]; + var b = _ref32[1]; + var c = _ref32[2]; + var _ref42 = _slicedToArray(_ref4, 3); - var x = _ref4[0]; - var y = _ref4[1]; - var z = _ref4[2]; + var x = _ref42[0]; + var y = _ref42[1]; + var z = _ref42[2]; return a + b + c; }; diff --git a/test/fixtures/transformation/es7-abstract-references/private/expected.js b/test/fixtures/transformation/es7-abstract-references/private/expected.js index d79155a923..1af25539ca 100644 --- a/test/fixtures/transformation/es7-abstract-references/private/expected.js +++ b/test/fixtures/transformation/es7-abstract-references/private/expected.js @@ -4,19 +4,19 @@ var A = new WeakMap(); var B = new WeakMap(), C = new WeakMap(); var D = (function () { - var _F = new WeakMap(), - _G = new WeakMap(); - var _E = new WeakMap(); + var F = new WeakMap(), + G = new WeakMap(); + var E = new WeakMap(); function D() {} return D; })(); var H = (function () { - var _J = new WeakMap(), - _K = new WeakMap(); - var _I = new WeakMap(); + var J = new WeakMap(), + K = new WeakMap(); + var I = new WeakMap(); function H() {} return H; -})(); \ No newline at end of file +})(); diff --git a/test/fixtures/transformation/es7-exponentian-operator/comprehensive/exec.js b/test/fixtures/transformation/es7-exponentian-operator/comprehensive/exec.js index 5a7ac52277..10dda4ed30 100644 --- a/test/fixtures/transformation/es7-exponentian-operator/comprehensive/exec.js +++ b/test/fixtures/transformation/es7-exponentian-operator/comprehensive/exec.js @@ -2,7 +2,7 @@ assert.equal(8, 2 ** 3); assert.equal(24, 3 * 2 ** 3); var x = 2; assert.equal(8, 2 ** ++x); -assert.equal(1, (2 ** -1) * 2); +assert.equal(1, 2 ** -1 * 2); var calls = 0; var q = {q: 3}; From 774cb66d9bdd0e6d74c43a280da0a06c5ab1691c Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 18:23:37 +1100 Subject: [PATCH 097/129] add isBlockedScoped types helper --- lib/6to5/traverse/scope.js | 6 +++--- lib/6to5/types/index.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/6to5/traverse/scope.js b/lib/6to5/traverse/scope.js index c77c75dcb3..e532848246 100644 --- a/lib/6to5/traverse/scope.js +++ b/lib/6to5/traverse/scope.js @@ -135,7 +135,7 @@ Scope.prototype.getInfo = function () { if (t.isFor(block)) { _.each(FOR_KEYS, function (key) { var node = block[key]; - if (t.isLet(node)) add(node); + if (t.isBlockScoped(node)) add(node); }); } @@ -144,7 +144,7 @@ Scope.prototype.getInfo = function () { if (t.isBlockStatement(block) || t.isProgram(block)) { _.each(block.body, function (node) { // check for non-var `VariableDeclaration`s - if (t.isLet(node)) add(node); + if (t.isBlockScoped(node)) add(node); }); } @@ -190,7 +190,7 @@ Scope.prototype.getInfo = function () { // we've ran into a declaration! // we'll let the BlockStatement scope deal with `let` declarations unless - if (t.isDeclaration(node) && !t.isLet(node)) { + if (t.isDeclaration(node) && !t.isBlockScoped(node)) { state.add(node); } } diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 85c89cb427..ff8af91e5e 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -497,6 +497,17 @@ t.isLet = function (node) { return t.isVariableDeclaration(node) && (node.kind !== "var" || node._let); }; +/** + * Description + * + * @param {Object} node + * @returns {Boolean} + */ + +t.isBlockScoped = function (node) { + return t.isFunctionDeclaration(node) || t.isLet(node); +}; + /** * Description * From 309d19960f6159c0ab597d8981155f63924fc525 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 18:23:46 +1100 Subject: [PATCH 098/129] better traversal context variable names --- lib/6to5/traverse/index.js | 42 ++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/lib/6to5/traverse/index.js b/lib/6to5/traverse/index.js index a9c5c381b3..2e54a47505 100644 --- a/lib/6to5/traverse/index.js +++ b/lib/6to5/traverse/index.js @@ -9,34 +9,34 @@ var _ = require("lodash"); function noop() { } function TraversalContext() { - this.didSkip = false; - this.didRemove = false; - this.didStop = false; - this.didFlatten = false; + this.shouldFlatten = false; + this.shouldRemove = false; + this.shouldSkip = false; + this.shouldStop = false; } TraversalContext.prototype.flatten = function () { - this.didFlatten = true; + this.shouldFlatten = true; }; TraversalContext.prototype.remove = function () { - this.didRemove = true; - this.didSkip = true; + this.shouldRemove = true; + this.shouldSkip = true; }; TraversalContext.prototype.skip = function () { - this.didSkip = true; + this.shouldSkip = true; }; TraversalContext.prototype.stop = function () { - this.didStop = true; - this.didSkip = true; + this.shouldStop = true; + this.shouldSkip = true; }; TraversalContext.prototype.reset = function () { - this.didSkip = false; - this.didStop = false; - this.didRemove = false; + this.shouldRemove = false; + this.shouldSkip = false; + this.shouldStop = false; }; function replaceNode(obj, key, node, result) { @@ -70,13 +70,13 @@ TraversalContext.prototype.enterNode = function (obj, key, node, enter, parent, node = result; if (flatten) { - this.didFlatten = true; + this.shouldFlatten = true; } } - if (this.didRemove) { + if (this.shouldRemove) { obj[key] = null; - this.didFlatten = true; + this.shouldFlatten = true; } return node; @@ -91,7 +91,7 @@ TraversalContext.prototype.exitNode = function (obj, key, node, exit, parent, sc node = result; if (flatten) { - this.didFlatten = true; + this.shouldFlatten = true; } } @@ -113,14 +113,12 @@ TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent, node = this.enterNode(obj, key, node, opts.enter, parent, ourScope, state); - if (this.didSkip) { - return this.didStop; - } + if (this.shouldSkip) return this.shouldStop; traverseNode(node, opts, ourScope, state); this.exitNode(obj, key, node, opts.exit, parent, ourScope, state); - return this.didStop; + return this.shouldStop; }; TraversalContext.prototype.visit = function (node, key, opts, scope, state) { @@ -140,7 +138,7 @@ TraversalContext.prototype.visit = function (node, key, opts, scope, state) { return true; } - if (this.didFlatten) { + if (this.shouldFlatten) { node[key] = _.flatten(node[key]); if (key === "body") { From c2b20b18b3eddaf154cdb132368fb3c51ff9255d Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 18:24:05 +1100 Subject: [PATCH 099/129] make name method helper export a property method instead of a default --- lib/6to5/transformation/helpers/name-method.js | 2 +- lib/6to5/transformation/transformers/es6-classes.js | 2 +- .../transformers/es6-property-method-assignment.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/6to5/transformation/helpers/name-method.js b/lib/6to5/transformation/helpers/name-method.js index 36cdbddaea..14db11b3e9 100644 --- a/lib/6to5/transformation/helpers/name-method.js +++ b/lib/6to5/transformation/helpers/name-method.js @@ -20,7 +20,7 @@ var traverser = { } }; -module.exports = function (node, file, scope) { +exports.property = function (node, file, scope) { var key = t.toComputedKey(node, node.key); if (!t.isLiteral(key)) return node; // we can't set a function id with this diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index b68159d242..432b0ff2ea 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -195,7 +195,7 @@ Class.prototype.pushMethod = function (node) { var kind = node.kind; if (kind === "") { - nameMethod(node, this.file, this.scope); + nameMethod.property(node, this.file, this.scope); if (this.isLoose) { // use assignments instead of define properties for loose classes diff --git a/lib/6to5/transformation/transformers/es6-property-method-assignment.js b/lib/6to5/transformation/transformers/es6-property-method-assignment.js index 7237ef1c4f..776b6172cc 100644 --- a/lib/6to5/transformation/transformers/es6-property-method-assignment.js +++ b/lib/6to5/transformation/transformers/es6-property-method-assignment.js @@ -7,7 +7,7 @@ exports.Property = function (node, parent, scope, context, file) { node.method = false; - nameMethod(node, file, scope); + nameMethod.property(node, file, scope); }; exports.ObjectExpression = function (node) { From 810c97d7abcce73f57d1bcb940e55f82eb47a9d4 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 18:24:16 +1100 Subject: [PATCH 100/129] actually make destructuring use the provided operator --- lib/6to5/transformation/transformers/es6-destructuring.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/6to5/transformation/transformers/es6-destructuring.js b/lib/6to5/transformation/transformers/es6-destructuring.js index 8b1f5ae882..910eed82a8 100644 --- a/lib/6to5/transformation/transformers/es6-destructuring.js +++ b/lib/6to5/transformation/transformers/es6-destructuring.js @@ -7,7 +7,7 @@ var buildVariableAssign = function (opts, id, init) { if (t.isMemberExpression(id)) op = "="; if (op) { - return t.expressionStatement(t.assignmentExpression("=", id, init)); + return t.expressionStatement(t.assignmentExpression(op, id, init)); } else { return t.variableDeclaration(opts.kind, [ t.variableDeclarator(id, init) From 62c168b13c18a26e5ef15fa3448eb7099a92d2a1 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 18:41:24 +1100 Subject: [PATCH 101/129] fix linting errors --- lib/6to5/transformation/transformers/es6-let-scoping.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index 8675611244..8533f9d1d5 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -1,5 +1,4 @@ var traverse = require("../../traverse"); -var Scope = require("../../traverse/scope"); var util = require("../../util"); var t = require("../../types"); var _ = require("lodash"); @@ -234,8 +233,8 @@ LetScoping.prototype.getLetReferences = function () { } // - for (var i = 0; i < declarators.length; i++) { - var declar = declarators[i]; + for (i = 0; i < declarators.length; i++) { + declar = declarators[i]; var keys = t.getIds(declar, true); _.extend(this.letReferences, keys); } From 36a933a004ed969a42ae3d8e962d5b506c136c1b Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 18:44:08 +1100 Subject: [PATCH 102/129] add strict mode to every file --- lib/6to5/browser.js | 2 ++ lib/6to5/file.js | 2 ++ lib/6to5/generation/buffer.js | 2 ++ lib/6to5/generation/generator.js | 2 ++ lib/6to5/generation/generators/base.js | 2 ++ lib/6to5/generation/generators/classes.js | 2 ++ lib/6to5/generation/generators/comprehensions.js | 2 ++ lib/6to5/generation/generators/expressions.js | 2 ++ lib/6to5/generation/generators/flow.js | 2 ++ lib/6to5/generation/generators/jsx.js | 2 ++ lib/6to5/generation/generators/methods.js | 2 ++ lib/6to5/generation/generators/modules.js | 2 ++ lib/6to5/generation/generators/playground.js | 2 ++ lib/6to5/generation/generators/statements.js | 2 ++ lib/6to5/generation/generators/template-literals.js | 2 ++ lib/6to5/generation/generators/types.js | 2 ++ lib/6to5/generation/node/index.js | 2 ++ lib/6to5/generation/node/parentheses.js | 2 ++ lib/6to5/generation/node/whitespace.js | 2 ++ lib/6to5/generation/position.js | 2 ++ lib/6to5/generation/source-map.js | 2 ++ lib/6to5/generation/whitespace.js | 4 +++- lib/6to5/index.js | 2 ++ lib/6to5/patch.js | 2 ++ lib/6to5/polyfill.js | 2 ++ lib/6to5/register-browser.js | 2 ++ lib/6to5/register.js | 2 ++ lib/6to5/runtime-generator.js | 2 ++ lib/6to5/to-fast-properties.js | 2 ++ .../helpers/build-binary-assignment-operator-transformer.js | 2 ++ lib/6to5/transformation/helpers/build-comprehension.js | 2 ++ .../build-conditional-assignment-operator-transformer.js | 2 ++ .../transformation/helpers/explode-assignable-expression.js | 2 ++ lib/6to5/transformation/helpers/name-method.js | 2 ++ lib/6to5/transformation/helpers/remap-async-to-generator.js | 2 ++ lib/6to5/transformation/helpers/replace-supers.js | 2 ++ lib/6to5/transformation/helpers/use-strict.js | 2 ++ lib/6to5/transformation/modules/_default.js | 2 ++ lib/6to5/transformation/modules/_strict.js | 2 ++ lib/6to5/transformation/modules/amd-strict.js | 2 ++ lib/6to5/transformation/modules/amd.js | 2 ++ lib/6to5/transformation/modules/common-strict.js | 2 ++ lib/6to5/transformation/modules/common.js | 2 ++ lib/6to5/transformation/modules/ignore.js | 2 ++ lib/6to5/transformation/modules/system.js | 2 ++ lib/6to5/transformation/modules/umd-strict.js | 2 ++ lib/6to5/transformation/modules/umd.js | 2 ++ lib/6to5/transformation/transform.js | 2 ++ lib/6to5/transformation/transformer.js | 2 ++ lib/6to5/transformation/transformers/_alias-functions.js | 2 ++ lib/6to5/transformation/transformers/_block-hoist.js | 2 ++ lib/6to5/transformation/transformers/_declarations.js | 2 ++ lib/6to5/transformation/transformers/_module-formatter.js | 2 ++ lib/6to5/transformation/transformers/es6-arrow-functions.js | 2 ++ lib/6to5/transformation/transformers/es6-classes.js | 2 ++ .../transformers/es6-computed-property-names.js | 2 ++ lib/6to5/transformation/transformers/es6-constants.js | 2 ++ .../transformation/transformers/es6-default-parameters.js | 2 ++ lib/6to5/transformation/transformers/es6-destructuring.js | 2 ++ lib/6to5/transformation/transformers/es6-for-of.js | 2 ++ lib/6to5/transformation/transformers/es6-generators.js | 2 ++ lib/6to5/transformation/transformers/es6-let-scoping.js | 2 ++ lib/6to5/transformation/transformers/es6-modules.js | 2 ++ .../transformers/es6-property-method-assignment.js | 2 ++ .../transformers/es6-property-name-shorthand.js | 2 ++ lib/6to5/transformation/transformers/es6-rest-parameters.js | 2 ++ lib/6to5/transformation/transformers/es6-spread.js | 2 ++ lib/6to5/transformation/transformers/es6-template-literals.js | 2 ++ lib/6to5/transformation/transformers/es6-unicode-regex.js | 2 ++ .../transformation/transformers/es7-abstract-references.js | 2 ++ .../transformation/transformers/es7-array-comprehension.js | 2 ++ .../transformers/es7-exponentiation-operator.js | 2 ++ .../transformers/es7-generator-comprehension.js | 2 ++ lib/6to5/transformation/transformers/es7-object-spread.js | 2 ++ .../transformers/optional-async-to-generator.js | 2 ++ .../transformers/optional-bluebird-coroutines.js | 2 ++ .../transformation/transformers/optional-core-aliasing.js | 2 ++ .../transformation/transformers/optional-proto-to-assign.js | 2 ++ .../transformation/transformers/optional-typeof-symbol.js | 2 ++ .../transformation/transformers/optional-undefined-to-void.js | 2 ++ .../transformation/transformers/playground-mallet-operator.js | 2 ++ .../transformers/playground-memoization-operator.js | 2 ++ .../transformation/transformers/playground-method-binding.js | 2 ++ .../transformers/playground-object-getter-memoization.js | 2 ++ lib/6to5/transformation/transformers/react.js | 2 ++ .../transformers/spec-block-scoped-functions.js | 2 ++ .../transformers/spec-member-expression-literals.js | 2 ++ .../transformers/spec-no-for-in-of-assignment.js | 2 ++ .../transformation/transformers/spec-property-literals.js | 2 ++ lib/6to5/transformation/transformers/spec-setters.js | 2 ++ lib/6to5/transformation/transformers/use-strict.js | 2 ++ lib/6to5/traverse/index.js | 2 ++ lib/6to5/traverse/scope.js | 2 ++ lib/6to5/types/index.js | 2 ++ lib/6to5/util.js | 2 ++ 95 files changed, 191 insertions(+), 1 deletion(-) diff --git a/lib/6to5/browser.js b/lib/6to5/browser.js index ccf6d54ed4..18a7fd0f28 100644 --- a/lib/6to5/browser.js +++ b/lib/6to5/browser.js @@ -1,3 +1,5 @@ +"use strict"; + var transform = module.exports = require("./transformation/transform"); transform.version = require("../../package").version; diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 926cf0e427..f9a6eb7d5e 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = File; var SHEBANG_REGEX = /^\#\!.*/; diff --git a/lib/6to5/generation/buffer.js b/lib/6to5/generation/buffer.js index 2fadf5db33..26e6bcc9aa 100644 --- a/lib/6to5/generation/buffer.js +++ b/lib/6to5/generation/buffer.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = Buffer; var util = require("../util"); diff --git a/lib/6to5/generation/generator.js b/lib/6to5/generation/generator.js index 39f92e1491..778a590e74 100644 --- a/lib/6to5/generation/generator.js +++ b/lib/6to5/generation/generator.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = function (ast, opts, code) { var gen = new CodeGenerator(ast, opts, code); return gen.generate(); diff --git a/lib/6to5/generation/generators/base.js b/lib/6to5/generation/generators/base.js index 5d7eb6f412..a9e8bd51af 100644 --- a/lib/6to5/generation/generators/base.js +++ b/lib/6to5/generation/generators/base.js @@ -1,3 +1,5 @@ +"use strict"; + exports.File = function (node, print) { print(node.program); }; diff --git a/lib/6to5/generation/generators/classes.js b/lib/6to5/generation/generators/classes.js index f7df307f62..df4ecd557a 100644 --- a/lib/6to5/generation/generators/classes.js +++ b/lib/6to5/generation/generators/classes.js @@ -1,3 +1,5 @@ +"use strict"; + exports.ClassExpression = exports.ClassDeclaration = function (node, print) { this.push("class"); diff --git a/lib/6to5/generation/generators/comprehensions.js b/lib/6to5/generation/generators/comprehensions.js index 1fe81bbbe4..feeef3c2de 100644 --- a/lib/6to5/generation/generators/comprehensions.js +++ b/lib/6to5/generation/generators/comprehensions.js @@ -1,3 +1,5 @@ +"use strict"; + exports.ComprehensionBlock = function (node, print) { this.keyword("for"); this.push("("); diff --git a/lib/6to5/generation/generators/expressions.js b/lib/6to5/generation/generators/expressions.js index ebe3fd9b7c..5989e8f8d0 100644 --- a/lib/6to5/generation/generators/expressions.js +++ b/lib/6to5/generation/generators/expressions.js @@ -1,3 +1,5 @@ +"use strict"; + var util = require("../../util"); var t = require("../../types"); var _ = require("lodash"); diff --git a/lib/6to5/generation/generators/flow.js b/lib/6to5/generation/generators/flow.js index 0e5c3c0cc2..cc3d481d1a 100644 --- a/lib/6to5/generation/generators/flow.js +++ b/lib/6to5/generation/generators/flow.js @@ -1,3 +1,5 @@ +"use strict"; + exports.AnyTypeAnnotation = exports.ArrayTypeAnnotation = exports.BooleanTypeAnnotation = diff --git a/lib/6to5/generation/generators/jsx.js b/lib/6to5/generation/generators/jsx.js index 5460660515..d372cbc74b 100644 --- a/lib/6to5/generation/generators/jsx.js +++ b/lib/6to5/generation/generators/jsx.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); var _ = require("lodash"); diff --git a/lib/6to5/generation/generators/methods.js b/lib/6to5/generation/generators/methods.js index e147b20a06..40a4e52b89 100644 --- a/lib/6to5/generation/generators/methods.js +++ b/lib/6to5/generation/generators/methods.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); exports._params = function (node, print) { diff --git a/lib/6to5/generation/generators/modules.js b/lib/6to5/generation/generators/modules.js index 16f6a01d83..6e8a587efb 100644 --- a/lib/6to5/generation/generators/modules.js +++ b/lib/6to5/generation/generators/modules.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); var _ = require("lodash"); diff --git a/lib/6to5/generation/generators/playground.js b/lib/6to5/generation/generators/playground.js index b97e2d6184..d11aa04ee6 100644 --- a/lib/6to5/generation/generators/playground.js +++ b/lib/6to5/generation/generators/playground.js @@ -1,3 +1,5 @@ +"use strict"; + var _ = require("lodash"); _.each(["BindMemberExpression", "BindFunctionExpression"], function (type) { diff --git a/lib/6to5/generation/generators/statements.js b/lib/6to5/generation/generators/statements.js index 715858e870..6cc66dd255 100644 --- a/lib/6to5/generation/generators/statements.js +++ b/lib/6to5/generation/generators/statements.js @@ -1,3 +1,5 @@ +"use strict"; + var util = require("../../util"); var t = require("../../types"); diff --git a/lib/6to5/generation/generators/template-literals.js b/lib/6to5/generation/generators/template-literals.js index b723a2b600..4bda97505f 100644 --- a/lib/6to5/generation/generators/template-literals.js +++ b/lib/6to5/generation/generators/template-literals.js @@ -1,3 +1,5 @@ +"use strict"; + var _ = require("lodash"); exports.TaggedTemplateExpression = function (node, print) { diff --git a/lib/6to5/generation/generators/types.js b/lib/6to5/generation/generators/types.js index 4a58375a8d..405fe31720 100644 --- a/lib/6to5/generation/generators/types.js +++ b/lib/6to5/generation/generators/types.js @@ -1,3 +1,5 @@ +"use strict"; + var _ = require("lodash"); exports.Identifier = function (node) { diff --git a/lib/6to5/generation/node/index.js b/lib/6to5/generation/node/index.js index 179b3015d7..ba6cc61f78 100644 --- a/lib/6to5/generation/node/index.js +++ b/lib/6to5/generation/node/index.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = Node; var whitespace = require("./whitespace"); diff --git a/lib/6to5/generation/node/parentheses.js b/lib/6to5/generation/node/parentheses.js index 5518086e27..391f8ffc5c 100644 --- a/lib/6to5/generation/node/parentheses.js +++ b/lib/6to5/generation/node/parentheses.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); var _ = require("lodash"); diff --git a/lib/6to5/generation/node/whitespace.js b/lib/6to5/generation/node/whitespace.js index d1defd1cfe..8deca5a4b2 100644 --- a/lib/6to5/generation/node/whitespace.js +++ b/lib/6to5/generation/node/whitespace.js @@ -1,3 +1,5 @@ +"use strict"; + var _ = require("lodash"); var t = require("../../types"); diff --git a/lib/6to5/generation/position.js b/lib/6to5/generation/position.js index cae67ca356..df85e0a332 100644 --- a/lib/6to5/generation/position.js +++ b/lib/6to5/generation/position.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = Position; function Position() { diff --git a/lib/6to5/generation/source-map.js b/lib/6to5/generation/source-map.js index 34f5d9d0cd..2dc922f024 100644 --- a/lib/6to5/generation/source-map.js +++ b/lib/6to5/generation/source-map.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = SourceMap; var sourceMap = require("source-map"); diff --git a/lib/6to5/generation/whitespace.js b/lib/6to5/generation/whitespace.js index aa0993bc2a..30855472d4 100644 --- a/lib/6to5/generation/whitespace.js +++ b/lib/6to5/generation/whitespace.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = Whitespace; var _ = require("lodash"); @@ -106,4 +108,4 @@ Whitespace.prototype.getNewlinesBetween = function (startToken, endToken) { } return lines; -}; \ No newline at end of file +}; diff --git a/lib/6to5/index.js b/lib/6to5/index.js index 7e3bac2922..79a33a1f0b 100644 --- a/lib/6to5/index.js +++ b/lib/6to5/index.js @@ -1,3 +1,5 @@ +"use strict"; + var transform = require("./transformation/transform"); var util = require("./util"); var fs = require("fs"); diff --git a/lib/6to5/patch.js b/lib/6to5/patch.js index 76e65bd38f..ac1ff2670b 100644 --- a/lib/6to5/patch.js +++ b/lib/6to5/patch.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("./types"); var _ = require("lodash"); diff --git a/lib/6to5/polyfill.js b/lib/6to5/polyfill.js index c8d5124f2b..7a893bd69c 100644 --- a/lib/6to5/polyfill.js +++ b/lib/6to5/polyfill.js @@ -1,2 +1,4 @@ +"use strict"; + require("core-js/shim"); require("regenerator/runtime"); diff --git a/lib/6to5/register-browser.js b/lib/6to5/register-browser.js index 7f7ab7a552..bd18a5fbc9 100644 --- a/lib/6to5/register-browser.js +++ b/lib/6to5/register-browser.js @@ -1,3 +1,5 @@ +"use strict"; + // required to safely use 6to5/register within a browserify codebase module.exports = function () {}; diff --git a/lib/6to5/register.js b/lib/6to5/register.js index 8b4d99d915..6d1b317bd8 100644 --- a/lib/6to5/register.js +++ b/lib/6to5/register.js @@ -1,3 +1,5 @@ +"use strict"; + require("./polyfill"); var sourceMapSupport = require("source-map-support"); diff --git a/lib/6to5/runtime-generator.js b/lib/6to5/runtime-generator.js index 8ea86c5c99..fa44638215 100644 --- a/lib/6to5/runtime-generator.js +++ b/lib/6to5/runtime-generator.js @@ -1,3 +1,5 @@ +"use strict"; + var generator = require("./generation/generator"); var util = require("./util"); var File = require("./file"); diff --git a/lib/6to5/to-fast-properties.js b/lib/6to5/to-fast-properties.js index a7499fdff5..c82e9cbe4b 100644 --- a/lib/6to5/to-fast-properties.js +++ b/lib/6to5/to-fast-properties.js @@ -1,3 +1,5 @@ +"use strict"; + /** * A trick from Bluebird to force V8 to use fast properties for an object. * Read more: http://stackoverflow.com/questions/24987896/ diff --git a/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js b/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js index 96743c8bdc..5822026ec6 100644 --- a/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js +++ b/lib/6to5/transformation/helpers/build-binary-assignment-operator-transformer.js @@ -1,3 +1,5 @@ +"use strict"; + var explode = require("./explode-assignable-expression"); var t = require("../../types"); diff --git a/lib/6to5/transformation/helpers/build-comprehension.js b/lib/6to5/transformation/helpers/build-comprehension.js index 9ba979caef..d390b7afde 100644 --- a/lib/6to5/transformation/helpers/build-comprehension.js +++ b/lib/6to5/transformation/helpers/build-comprehension.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); module.exports = function build(node, buildBody) { diff --git a/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js b/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js index adb097052f..9137b31b23 100644 --- a/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js +++ b/lib/6to5/transformation/helpers/build-conditional-assignment-operator-transformer.js @@ -1,3 +1,5 @@ +"use strict"; + var explode = require("./explode-assignable-expression"); var t = require("../../types"); diff --git a/lib/6to5/transformation/helpers/explode-assignable-expression.js b/lib/6to5/transformation/helpers/explode-assignable-expression.js index eecc696b78..9412f85390 100644 --- a/lib/6to5/transformation/helpers/explode-assignable-expression.js +++ b/lib/6to5/transformation/helpers/explode-assignable-expression.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); var getObjRef = function (node, nodes, file, scope) { diff --git a/lib/6to5/transformation/helpers/name-method.js b/lib/6to5/transformation/helpers/name-method.js index 14db11b3e9..34ea75fbf1 100644 --- a/lib/6to5/transformation/helpers/name-method.js +++ b/lib/6to5/transformation/helpers/name-method.js @@ -1,3 +1,5 @@ +"use strict"; + var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); diff --git a/lib/6to5/transformation/helpers/remap-async-to-generator.js b/lib/6to5/transformation/helpers/remap-async-to-generator.js index 7868e86ec7..7426862f71 100644 --- a/lib/6to5/transformation/helpers/remap-async-to-generator.js +++ b/lib/6to5/transformation/helpers/remap-async-to-generator.js @@ -1,3 +1,5 @@ +"use strict"; + var traverse = require("../../traverse"); var t = require("../../types"); diff --git a/lib/6to5/transformation/helpers/replace-supers.js b/lib/6to5/transformation/helpers/replace-supers.js index 4ca7009be4..4ec70cf6b5 100644 --- a/lib/6to5/transformation/helpers/replace-supers.js +++ b/lib/6to5/transformation/helpers/replace-supers.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = ReplaceSupers; var traverse = require("../../traverse"); diff --git a/lib/6to5/transformation/helpers/use-strict.js b/lib/6to5/transformation/helpers/use-strict.js index 6ef8e3baf0..7709528a41 100644 --- a/lib/6to5/transformation/helpers/use-strict.js +++ b/lib/6to5/transformation/helpers/use-strict.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); exports.has = function (node) { diff --git a/lib/6to5/transformation/modules/_default.js b/lib/6to5/transformation/modules/_default.js index 1485d27f03..989d7b3f94 100644 --- a/lib/6to5/transformation/modules/_default.js +++ b/lib/6to5/transformation/modules/_default.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = DefaultFormatter; var traverse = require("../../traverse"); diff --git a/lib/6to5/transformation/modules/_strict.js b/lib/6to5/transformation/modules/_strict.js index 2531d64422..160f30b547 100644 --- a/lib/6to5/transformation/modules/_strict.js +++ b/lib/6to5/transformation/modules/_strict.js @@ -1,3 +1,5 @@ +"use strict"; + var util = require("../../util"); module.exports = function (Parent) { diff --git a/lib/6to5/transformation/modules/amd-strict.js b/lib/6to5/transformation/modules/amd-strict.js index 725d67325d..a055b94763 100644 --- a/lib/6to5/transformation/modules/amd-strict.js +++ b/lib/6to5/transformation/modules/amd-strict.js @@ -1 +1,3 @@ +"use strict"; + module.exports = require("./_strict")(require("./amd")); diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index f0aedd7e67..3940c9d18d 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = AMDFormatter; var DefaultFormatter = require("./_default"); diff --git a/lib/6to5/transformation/modules/common-strict.js b/lib/6to5/transformation/modules/common-strict.js index 54608375de..bb7b5c944c 100644 --- a/lib/6to5/transformation/modules/common-strict.js +++ b/lib/6to5/transformation/modules/common-strict.js @@ -1 +1,3 @@ +"use strict"; + module.exports = require("./_strict")(require("./common")); diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index f62cb03883..8e6044f2e9 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = CommonJSFormatter; var DefaultFormatter = require("./_default"); diff --git a/lib/6to5/transformation/modules/ignore.js b/lib/6to5/transformation/modules/ignore.js index 3753440eed..64c902afe7 100644 --- a/lib/6to5/transformation/modules/ignore.js +++ b/lib/6to5/transformation/modules/ignore.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = IgnoreFormatter; var t = require("../../types"); diff --git a/lib/6to5/transformation/modules/system.js b/lib/6to5/transformation/modules/system.js index d0650a69c2..85f64662a9 100644 --- a/lib/6to5/transformation/modules/system.js +++ b/lib/6to5/transformation/modules/system.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = SystemFormatter; var AMDFormatter = require("./amd"); diff --git a/lib/6to5/transformation/modules/umd-strict.js b/lib/6to5/transformation/modules/umd-strict.js index c14d6e4076..6104513e81 100644 --- a/lib/6to5/transformation/modules/umd-strict.js +++ b/lib/6to5/transformation/modules/umd-strict.js @@ -1 +1,3 @@ +"use strict"; + module.exports = require("./_strict")(require("./umd")); diff --git a/lib/6to5/transformation/modules/umd.js b/lib/6to5/transformation/modules/umd.js index d0122f4f68..f5c068bb64 100644 --- a/lib/6to5/transformation/modules/umd.js +++ b/lib/6to5/transformation/modules/umd.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = UMDFormatter; var AMDFormatter = require("./amd"); diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 838ee5a90d..2a5cbb26a2 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = transform; var Transformer = require("./transformer"); diff --git a/lib/6to5/transformation/transformer.js b/lib/6to5/transformation/transformer.js index dbcd0f9d10..d0a4e0dd0a 100644 --- a/lib/6to5/transformation/transformer.js +++ b/lib/6to5/transformation/transformer.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = Transformer; var traverse = require("../traverse"); diff --git a/lib/6to5/transformation/transformers/_alias-functions.js b/lib/6to5/transformation/transformers/_alias-functions.js index 1d2bf585f6..836bf8f362 100644 --- a/lib/6to5/transformation/transformers/_alias-functions.js +++ b/lib/6to5/transformation/transformers/_alias-functions.js @@ -1,3 +1,5 @@ +"use strict"; + var traverse = require("../../traverse"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/_block-hoist.js b/lib/6to5/transformation/transformers/_block-hoist.js index cc17209797..6e1ab91033 100644 --- a/lib/6to5/transformation/transformers/_block-hoist.js +++ b/lib/6to5/transformation/transformers/_block-hoist.js @@ -1,3 +1,5 @@ +"use strict"; + var useStrict = require("../helpers/use-strict"); var _ = require("lodash"); diff --git a/lib/6to5/transformation/transformers/_declarations.js b/lib/6to5/transformation/transformers/_declarations.js index 61852930b6..07974b32a2 100644 --- a/lib/6to5/transformation/transformers/_declarations.js +++ b/lib/6to5/transformation/transformers/_declarations.js @@ -1,3 +1,5 @@ +"use strict"; + var useStrict = require("../helpers/use-strict"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/_module-formatter.js b/lib/6to5/transformation/transformers/_module-formatter.js index fb7c7d4a90..503543171e 100644 --- a/lib/6to5/transformation/transformers/_module-formatter.js +++ b/lib/6to5/transformation/transformers/_module-formatter.js @@ -1,3 +1,5 @@ +"use strict"; + var transform = require("../transform"); exports.ast = { diff --git a/lib/6to5/transformation/transformers/es6-arrow-functions.js b/lib/6to5/transformation/transformers/es6-arrow-functions.js index 40f9ab9906..e822b42707 100644 --- a/lib/6to5/transformation/transformers/es6-arrow-functions.js +++ b/lib/6to5/transformation/transformers/es6-arrow-functions.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); exports.ArrowFunctionExpression = function (node) { diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 432b0ff2ea..e320d4634e 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -1,3 +1,5 @@ +"use strict"; + var ReplaceSupers = require("../helpers/replace-supers"); var nameMethod = require("../helpers/name-method"); var util = require("../../util"); diff --git a/lib/6to5/transformation/transformers/es6-computed-property-names.js b/lib/6to5/transformation/transformers/es6-computed-property-names.js index 3789ca1d23..9d08092830 100644 --- a/lib/6to5/transformation/transformers/es6-computed-property-names.js +++ b/lib/6to5/transformation/transformers/es6-computed-property-names.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); exports.ObjectExpression = function (node, parent, scope, context, file) { diff --git a/lib/6to5/transformation/transformers/es6-constants.js b/lib/6to5/transformation/transformers/es6-constants.js index 096eee9097..7be895f0f2 100644 --- a/lib/6to5/transformation/transformers/es6-constants.js +++ b/lib/6to5/transformation/transformers/es6-constants.js @@ -1,3 +1,5 @@ +"use strict"; + var traverse = require("../../traverse"); var t = require("../../types"); var _ = require("lodash"); diff --git a/lib/6to5/transformation/transformers/es6-default-parameters.js b/lib/6to5/transformation/transformers/es6-default-parameters.js index 4b03d56295..408c1a7e63 100644 --- a/lib/6to5/transformation/transformers/es6-default-parameters.js +++ b/lib/6to5/transformation/transformers/es6-default-parameters.js @@ -1,3 +1,5 @@ +"use strict"; + var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/es6-destructuring.js b/lib/6to5/transformation/transformers/es6-destructuring.js index 910eed82a8..3a3e27c4b5 100644 --- a/lib/6to5/transformation/transformers/es6-destructuring.js +++ b/lib/6to5/transformation/transformers/es6-destructuring.js @@ -1,3 +1,5 @@ +"use strict"; + // TODO: Clean up var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/es6-for-of.js b/lib/6to5/transformation/transformers/es6-for-of.js index 9018e995af..3eed21d15d 100644 --- a/lib/6to5/transformation/transformers/es6-for-of.js +++ b/lib/6to5/transformation/transformers/es6-for-of.js @@ -1,3 +1,5 @@ +"use strict"; + var util = require("../../util"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/es6-generators.js b/lib/6to5/transformation/transformers/es6-generators.js index caeeec5ef6..6d8124b709 100644 --- a/lib/6to5/transformation/transformers/es6-generators.js +++ b/lib/6to5/transformation/transformers/es6-generators.js @@ -1,3 +1,5 @@ +"use strict"; + var regenerator = require("regenerator"); exports.ast = { diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index 8533f9d1d5..14849d6c7c 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -1,3 +1,5 @@ +"use strict"; + var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/es6-modules.js b/lib/6to5/transformation/transformers/es6-modules.js index a9b60473f2..b59b191323 100644 --- a/lib/6to5/transformation/transformers/es6-modules.js +++ b/lib/6to5/transformation/transformers/es6-modules.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); exports.ast = { diff --git a/lib/6to5/transformation/transformers/es6-property-method-assignment.js b/lib/6to5/transformation/transformers/es6-property-method-assignment.js index 776b6172cc..4a740e2f83 100644 --- a/lib/6to5/transformation/transformers/es6-property-method-assignment.js +++ b/lib/6to5/transformation/transformers/es6-property-method-assignment.js @@ -1,3 +1,5 @@ +"use strict"; + var nameMethod = require("../helpers/name-method"); var util = require("../../util"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/es6-property-name-shorthand.js b/lib/6to5/transformation/transformers/es6-property-name-shorthand.js index 8614d69c84..006fc9325b 100644 --- a/lib/6to5/transformation/transformers/es6-property-name-shorthand.js +++ b/lib/6to5/transformation/transformers/es6-property-name-shorthand.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); var _ = require("lodash"); diff --git a/lib/6to5/transformation/transformers/es6-rest-parameters.js b/lib/6to5/transformation/transformers/es6-rest-parameters.js index c618394c51..89bafc063a 100644 --- a/lib/6to5/transformation/transformers/es6-rest-parameters.js +++ b/lib/6to5/transformation/transformers/es6-rest-parameters.js @@ -1,3 +1,5 @@ +"use strict"; + var util = require("../../util"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/es6-spread.js b/lib/6to5/transformation/transformers/es6-spread.js index 4fa64b5e2b..fff975918c 100644 --- a/lib/6to5/transformation/transformers/es6-spread.js +++ b/lib/6to5/transformation/transformers/es6-spread.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); var _ = require("lodash"); diff --git a/lib/6to5/transformation/transformers/es6-template-literals.js b/lib/6to5/transformation/transformers/es6-template-literals.js index d2f360b104..682fbbe85b 100644 --- a/lib/6to5/transformation/transformers/es6-template-literals.js +++ b/lib/6to5/transformation/transformers/es6-template-literals.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); var buildBinaryExpression = function (left, right) { diff --git a/lib/6to5/transformation/transformers/es6-unicode-regex.js b/lib/6to5/transformation/transformers/es6-unicode-regex.js index d85bdf4e3b..1c1cf36089 100644 --- a/lib/6to5/transformation/transformers/es6-unicode-regex.js +++ b/lib/6to5/transformation/transformers/es6-unicode-regex.js @@ -1,3 +1,5 @@ +"use strict"; + var rewritePattern = require("regexpu/rewrite-pattern"); var _ = require("lodash"); diff --git a/lib/6to5/transformation/transformers/es7-abstract-references.js b/lib/6to5/transformation/transformers/es7-abstract-references.js index 39003b914c..f6c7eefb7e 100644 --- a/lib/6to5/transformation/transformers/es7-abstract-references.js +++ b/lib/6to5/transformation/transformers/es7-abstract-references.js @@ -1,3 +1,5 @@ +"use strict"; + // https://github.com/zenparsing/es-abstract-refs var util = require("../../util"); diff --git a/lib/6to5/transformation/transformers/es7-array-comprehension.js b/lib/6to5/transformation/transformers/es7-array-comprehension.js index e50b135d0b..46b537f5f9 100644 --- a/lib/6to5/transformation/transformers/es7-array-comprehension.js +++ b/lib/6to5/transformation/transformers/es7-array-comprehension.js @@ -1,3 +1,5 @@ +"use strict"; + var buildComprehension = require("../helpers/build-comprehension"); var traverse = require("../../traverse"); var util = require("../../util"); diff --git a/lib/6to5/transformation/transformers/es7-exponentiation-operator.js b/lib/6to5/transformation/transformers/es7-exponentiation-operator.js index 1310700c18..9ef16713d8 100644 --- a/lib/6to5/transformation/transformers/es7-exponentiation-operator.js +++ b/lib/6to5/transformation/transformers/es7-exponentiation-operator.js @@ -1,3 +1,5 @@ +"use strict"; + // https://github.com/rwaldron/exponentiation-operator exports.experimental = true; diff --git a/lib/6to5/transformation/transformers/es7-generator-comprehension.js b/lib/6to5/transformation/transformers/es7-generator-comprehension.js index 84d75634a1..cd229e5eb3 100644 --- a/lib/6to5/transformation/transformers/es7-generator-comprehension.js +++ b/lib/6to5/transformation/transformers/es7-generator-comprehension.js @@ -1,3 +1,5 @@ +"use strict"; + var buildComprehension = require("../helpers/build-comprehension"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/es7-object-spread.js b/lib/6to5/transformation/transformers/es7-object-spread.js index 083bbdc166..87b32bff53 100644 --- a/lib/6to5/transformation/transformers/es7-object-spread.js +++ b/lib/6to5/transformation/transformers/es7-object-spread.js @@ -1,3 +1,5 @@ +"use strict"; + // https://github.com/sebmarkbage/ecmascript-rest-spread var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/optional-async-to-generator.js b/lib/6to5/transformation/transformers/optional-async-to-generator.js index d6f952783c..04f68f8cd9 100644 --- a/lib/6to5/transformation/transformers/optional-async-to-generator.js +++ b/lib/6to5/transformation/transformers/optional-async-to-generator.js @@ -1,3 +1,5 @@ +"use strict"; + var remapAsyncToGenerator = require("../helpers/remap-async-to-generator"); var bluebirdCoroutines = require("./optional-bluebird-coroutines"); diff --git a/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js b/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js index 73b9db1fd7..1aa2a24863 100644 --- a/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js +++ b/lib/6to5/transformation/transformers/optional-bluebird-coroutines.js @@ -1,3 +1,5 @@ +"use strict"; + var remapAsyncToGenerator = require("../helpers/remap-async-to-generator"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/optional-core-aliasing.js b/lib/6to5/transformation/transformers/optional-core-aliasing.js index d563bb619a..c0db8bd9bd 100644 --- a/lib/6to5/transformation/transformers/optional-core-aliasing.js +++ b/lib/6to5/transformation/transformers/optional-core-aliasing.js @@ -1,3 +1,5 @@ +"use strict"; + var traverse = require("../../traverse"); var util = require("../../util"); var core = require("core-js/library"); diff --git a/lib/6to5/transformation/transformers/optional-proto-to-assign.js b/lib/6to5/transformation/transformers/optional-proto-to-assign.js index 73a9c8167d..1533480b76 100644 --- a/lib/6to5/transformation/transformers/optional-proto-to-assign.js +++ b/lib/6to5/transformation/transformers/optional-proto-to-assign.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); var _ = require("lodash"); diff --git a/lib/6to5/transformation/transformers/optional-typeof-symbol.js b/lib/6to5/transformation/transformers/optional-typeof-symbol.js index 1c1aed8cc1..a3d938ccf8 100644 --- a/lib/6to5/transformation/transformers/optional-typeof-symbol.js +++ b/lib/6to5/transformation/transformers/optional-typeof-symbol.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); exports.optional = true; diff --git a/lib/6to5/transformation/transformers/optional-undefined-to-void.js b/lib/6to5/transformation/transformers/optional-undefined-to-void.js index 19875cd90a..f233becf74 100644 --- a/lib/6to5/transformation/transformers/optional-undefined-to-void.js +++ b/lib/6to5/transformation/transformers/optional-undefined-to-void.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); exports.optional = true; diff --git a/lib/6to5/transformation/transformers/playground-mallet-operator.js b/lib/6to5/transformation/transformers/playground-mallet-operator.js index 559cbde1ed..5bd48d6046 100644 --- a/lib/6to5/transformation/transformers/playground-mallet-operator.js +++ b/lib/6to5/transformation/transformers/playground-mallet-operator.js @@ -1,3 +1,5 @@ +"use strict"; + var build = require("../helpers/build-conditional-assignment-operator-transformer"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/playground-memoization-operator.js b/lib/6to5/transformation/transformers/playground-memoization-operator.js index 822692bcee..0f41ffb3c6 100644 --- a/lib/6to5/transformation/transformers/playground-memoization-operator.js +++ b/lib/6to5/transformation/transformers/playground-memoization-operator.js @@ -1,3 +1,5 @@ +"use strict"; + var build = require("../helpers/build-conditional-assignment-operator-transformer"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/playground-method-binding.js b/lib/6to5/transformation/transformers/playground-method-binding.js index 4f3fe3bb51..69e901f628 100644 --- a/lib/6to5/transformation/transformers/playground-method-binding.js +++ b/lib/6to5/transformation/transformers/playground-method-binding.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); exports.BindMemberExpression = function (node, parent, scope, context, file) { diff --git a/lib/6to5/transformation/transformers/playground-object-getter-memoization.js b/lib/6to5/transformation/transformers/playground-object-getter-memoization.js index 08af63ea57..d5acc71105 100644 --- a/lib/6to5/transformation/transformers/playground-object-getter-memoization.js +++ b/lib/6to5/transformation/transformers/playground-object-getter-memoization.js @@ -1,3 +1,5 @@ +"use strict"; + var traverse = require("../../traverse"); var t = require("../../types"); diff --git a/lib/6to5/transformation/transformers/react.js b/lib/6to5/transformation/transformers/react.js index a1ad20e23b..f906cead68 100644 --- a/lib/6to5/transformation/transformers/react.js +++ b/lib/6to5/transformation/transformers/react.js @@ -1,3 +1,5 @@ +"use strict"; + // Based upon the excellent jsx-transpiler by Ingvar Stepanyan (RReverser) // https://github.com/RReverser/jsx-transpiler diff --git a/lib/6to5/transformation/transformers/spec-block-scoped-functions.js b/lib/6to5/transformation/transformers/spec-block-scoped-functions.js index 814937fd31..3d8d3449c8 100644 --- a/lib/6to5/transformation/transformers/spec-block-scoped-functions.js +++ b/lib/6to5/transformation/transformers/spec-block-scoped-functions.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); exports.FunctionDeclaration = function (node, parent) { diff --git a/lib/6to5/transformation/transformers/spec-member-expression-literals.js b/lib/6to5/transformation/transformers/spec-member-expression-literals.js index 6803865073..d4c318a996 100644 --- a/lib/6to5/transformation/transformers/spec-member-expression-literals.js +++ b/lib/6to5/transformation/transformers/spec-member-expression-literals.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); exports.MemberExpression = function (node) { diff --git a/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js b/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js index 0b00e88f9d..01de184aaa 100644 --- a/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js +++ b/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); exports.ForInStatement = diff --git a/lib/6to5/transformation/transformers/spec-property-literals.js b/lib/6to5/transformation/transformers/spec-property-literals.js index f750fa7527..3473aa9f26 100644 --- a/lib/6to5/transformation/transformers/spec-property-literals.js +++ b/lib/6to5/transformation/transformers/spec-property-literals.js @@ -1,3 +1,5 @@ +"use strict"; + var t = require("../../types"); exports.Property = function (node) { diff --git a/lib/6to5/transformation/transformers/spec-setters.js b/lib/6to5/transformation/transformers/spec-setters.js index 54d88827aa..e104022bbc 100644 --- a/lib/6to5/transformation/transformers/spec-setters.js +++ b/lib/6to5/transformation/transformers/spec-setters.js @@ -1,3 +1,5 @@ +"use strict"; + exports.MethodDefinition = exports.Property = function (node, parent, scope, context, file) { if (node.kind === "set" && node.value.params.length !== 1) { diff --git a/lib/6to5/transformation/transformers/use-strict.js b/lib/6to5/transformation/transformers/use-strict.js index 41eec48c14..e8b813d253 100644 --- a/lib/6to5/transformation/transformers/use-strict.js +++ b/lib/6to5/transformation/transformers/use-strict.js @@ -1,3 +1,5 @@ +"use strict"; + var useStrict = require("../helpers/use-strict"); var t = require("../../types"); diff --git a/lib/6to5/traverse/index.js b/lib/6to5/traverse/index.js index 2e54a47505..3c88178c1b 100644 --- a/lib/6to5/traverse/index.js +++ b/lib/6to5/traverse/index.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = traverse; /* jshint maxparams:7 */ diff --git a/lib/6to5/traverse/scope.js b/lib/6to5/traverse/scope.js index e532848246..f100615f12 100644 --- a/lib/6to5/traverse/scope.js +++ b/lib/6to5/traverse/scope.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = Scope; var traverse = require("./index"); diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index ff8af91e5e..31c783dc1f 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -1,3 +1,5 @@ +"use strict"; + var toFastProperties = require("../to-fast-properties"); var esutils = require("esutils"); var _ = require("lodash"); diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 626d06305d..40a2c23b97 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -1,3 +1,5 @@ +"use strict"; + require("./patch"); var estraverse = require("estraverse"); From 2534f7f2a00f88bde8b59601c903d558c57a0193 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 18:50:58 +1100 Subject: [PATCH 103/129] upgrade dependencies --- CHANGELOG.md | 19 +++++++++++++++++++ package.json | 28 ++++++++++++++-------------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af7f76644c..1052a028f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,25 @@ _Note: Gaps between patch versions are faulty/broken releases._ +## 2.13.0 + + * **Internal** + * Upgrade `acorn-6to5`. + * Upgrade `chokidar` to `0.12.6`. + * Upgrade `commander` to `2.6.0`. + * Upgrade `core-js` to `0.4.5`. + * Upgrade `estraverse` to `1.9.1`. + * Upgrade `jshint` to `2.5.11`. + * Upgrade `regenerator` to `0.8.9`. + * Upgrade `regexpu` to `1.0.0`. + * Upgrade `source-map` to `0.1.43`. + * Upgrade `source-map-support` to `0.2.9`. + * Upgrade `browserify` to `8.1.1`. + * Upgrade `chai` to `1.10.0`. + * Upgrade `istanbul` to `0.3.5`. + * Upgrade `mocha` to `2.1.0`. + * Upgrade `uglify-js` to `2.4.16`. + ## 2.12.6 * **Bug Fix** diff --git a/package.json b/package.json index 5c0c46e586..f1365a7d64 100644 --- a/package.json +++ b/package.json @@ -35,32 +35,32 @@ "dependencies": { "acorn-6to5": "0.11.1-17", "ast-types": "~0.6.1", - "chokidar": "0.11.1", - "commander": "2.5.0", - "core-js": "0.4.4", - "estraverse": "1.8.0", + "chokidar": "0.12.6", + "commander": "2.6.0", + "core-js": "0.4.5", + "estraverse": "1.9.1", "esutils": "1.1.6", "esvalid": "1.1.0", "fs-readdir-recursive": "0.1.0", - "jshint": "2.5.10", + "jshint": "2.5.11", "lodash": "2.4.1", "output-file-sync": "^1.1.0", "private": "0.1.6", - "regenerator": "0.8.3", - "regexpu": "0.3.0", + "regenerator": "0.8.9", + "regexpu": "1.0.0", "roadrunner": "1.0.4", - "source-map": "0.1.40", - "source-map-support": "0.2.8" + "source-map": "0.1.43", + "source-map-support": "0.2.9" }, "devDependencies": { - "browserify": "6.3.2", - "chai": "1.9.2", - "istanbul": "0.3.2", + "browserify": "8.1.1", + "chai": "1.10.0", + "istanbul": "0.3.5", "jshint-stylish": "1.0.0", "matcha": "0.6.0", - "mocha": "1.21.4", + "mocha": "2.1.0", "rimraf": "2.2.8", - "uglify-js": "2.4.15" + "uglify-js": "2.4.16" }, "optionalDependencies": { "kexec": "0.2.0" From 9c9a9b032526aca28902e01e984fa51417eede44 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 18:51:11 +1100 Subject: [PATCH 104/129] make regenerator test future-proof --- .../include-regenerator/actual.js | 1 - .../include-regenerator/exec.js | 1 + .../include-regenerator/expected.js | 400 ------------------ 3 files changed, 1 insertion(+), 401 deletions(-) delete mode 100644 test/fixtures/transformation/es6-generators/include-regenerator/actual.js create mode 100644 test/fixtures/transformation/es6-generators/include-regenerator/exec.js delete mode 100644 test/fixtures/transformation/es6-generators/include-regenerator/expected.js diff --git a/test/fixtures/transformation/es6-generators/include-regenerator/actual.js b/test/fixtures/transformation/es6-generators/include-regenerator/actual.js deleted file mode 100644 index d82da75f43..0000000000 --- a/test/fixtures/transformation/es6-generators/include-regenerator/actual.js +++ /dev/null @@ -1 +0,0 @@ -function *x() {} diff --git a/test/fixtures/transformation/es6-generators/include-regenerator/exec.js b/test/fixtures/transformation/es6-generators/include-regenerator/exec.js new file mode 100644 index 0000000000..cea5770203 --- /dev/null +++ b/test/fixtures/transformation/es6-generators/include-regenerator/exec.js @@ -0,0 +1 @@ +assert.ok(typeof regeneratorRuntime !== "undefined"); diff --git a/test/fixtures/transformation/es6-generators/include-regenerator/expected.js b/test/fixtures/transformation/es6-generators/include-regenerator/expected.js deleted file mode 100644 index 3b1d9b0960..0000000000 --- a/test/fixtures/transformation/es6-generators/include-regenerator/expected.js +++ /dev/null @@ -1,400 +0,0 @@ -"use strict"; - -!(function () { - var hasOwn = Object.prototype.hasOwnProperty; - var undefined; - var iteratorSymbol = typeof Symbol === "function" && Symbol.iterator || "@@iterator"; - if (typeof regeneratorRuntime === "object") { - return; - } - var runtime = regeneratorRuntime = typeof exports === "undefined" ? {} : exports; - function wrap(innerFn, outerFn, self, tryList) { - return new Generator(innerFn, outerFn, self || null, tryList || []); - } - - runtime.wrap = wrap; - var GenStateSuspendedStart = "suspendedStart"; - - var GenStateSuspendedYield = "suspendedYield"; - - var GenStateExecuting = "executing"; - - var GenStateCompleted = "completed"; - - var ContinueSentinel = {}; - - function GeneratorFunction() {} - - function GeneratorFunctionPrototype() {} - - var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype; - GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype; - GeneratorFunctionPrototype.constructor = GeneratorFunction; - GeneratorFunction.displayName = "GeneratorFunction"; - runtime.isGeneratorFunction = function (genFun) { - var ctor = typeof genFun === "function" && genFun.constructor; - return ctor ? ctor === GeneratorFunction || (ctor.displayName || ctor.name) === "GeneratorFunction" : false; - }; - - runtime.mark = function (genFun) { - genFun.__proto__ = GeneratorFunctionPrototype; - genFun.prototype = Object.create(Gp); - return genFun; - }; - - runtime.async = function (innerFn, outerFn, self, tryList) { - return new Promise(function (resolve, reject) { - var generator = wrap(innerFn, outerFn, self, tryList); - - var callNext = step.bind(generator.next); - - var callThrow = step.bind(generator["throw"]); - - function step(arg) { - try { - var info = this(arg); - - var value = info.value; - } catch (error) { - return reject(error); - } - if (info.done) { - resolve(value); - } else { - Promise.resolve(value).then(callNext, callThrow); - } - } - - callNext(); - }); - }; - - function Generator(innerFn, outerFn, self, tryList) { - var generator = outerFn ? Object.create(outerFn.prototype) : this; - var context = new Context(tryList); - var state = GenStateSuspendedStart; - function invoke(method, arg) { - if (state === GenStateExecuting) { - throw new Error("Generator is already running"); - } - if (state === GenStateCompleted) { - return doneResult(); - } - while (true) { - var delegate = context.delegate; - if (delegate) { - try { - var info = delegate.iterator[method](arg); - - method = "next"; - arg = undefined; - } catch (uncaught) { - context.delegate = null; - method = "throw"; - arg = uncaught; - continue; - } - if (info.done) { - context[delegate.resultName] = info.value; - context.next = delegate.nextLoc; - } else { - state = GenStateSuspendedYield; - return info; - } - context.delegate = null; - } - if (method === "next") { - if (state === GenStateSuspendedStart && typeof arg !== "undefined") { - throw new TypeError("attempt to send " + JSON.stringify(arg) + " to newborn generator"); - } - if (state === GenStateSuspendedYield) { - context.sent = arg; - } else { - delete context.sent; - } - } else if (method === "throw") { - if (state === GenStateSuspendedStart) { - state = GenStateCompleted; - throw arg; - } - if (context.dispatchException(arg)) { - method = "next"; - arg = undefined; - } - } else if (method === "return") { - context.abrupt("return", arg); - } - state = GenStateExecuting; - try { - var value = innerFn.call(self, context); - - state = context.done ? GenStateCompleted : GenStateSuspendedYield; - var info = { - value: value, - done: context.done - }; - - if (value === ContinueSentinel) { - if (context.delegate && method === "next") { - arg = undefined; - } - } else { - return info; - } - } catch (thrown) { - state = GenStateCompleted; - if (method === "next") { - context.dispatchException(thrown); - } else { - arg = thrown; - } - } - } - } - - generator.next = invoke.bind(generator, "next"); - generator["throw"] = invoke.bind(generator, "throw"); - generator["return"] = invoke.bind(generator, "return"); - return generator; - } - - Gp[iteratorSymbol] = function () { - return this; - }; - - Gp.toString = function () { - return "[object Generator]"; - }; - - function pushTryEntry(triple) { - var entry = { - tryLoc: triple[0] - }; - - if (1 in triple) { - entry.catchLoc = triple[1]; - } - if (2 in triple) { - entry.finallyLoc = triple[2]; - } - this.tryEntries.push(entry); - } - - function resetTryEntry(entry, i) { - var record = entry.completion || {}; - record.type = i === 0 ? "normal" : "return"; - delete record.arg; - entry.completion = record; - } - - function Context(tryList) { - this.tryEntries = [{ - tryLoc: "root" - }]; - tryList.forEach(pushTryEntry, this); - - this.reset(); - } - - runtime.keys = function (object) { - var keys = []; - - for (var key in object) { - keys.push(key); - } - - keys.reverse(); - - return function next() { - while (keys.length) { - var key = keys.pop(); - - if (key in object) { - next.value = key; - next.done = false; - return next; - } - } - next.done = true; - return next; - }; - }; - - function values(iterable) { - if (iterable) { - var iteratorMethod = iterable[iteratorSymbol]; - if (iteratorMethod) { - return iteratorMethod.call(iterable); - } - if (typeof iterable.next === "function") { - return iterable; - } - if (!isNaN(iterable.length)) { - var i = -1; - function next() { - while (++i < iterable.length) { - if (hasOwn.call(iterable, i)) { - next.value = iterable[i]; - next.done = false; - return next; - } - } - next.value = undefined; - next.done = true; - return next; - } - - return next.next = next; - } - } - return { - next: doneResult - }; - } - - runtime.values = values; - function doneResult() { - return { - value: undefined, - done: true - }; - } - - Context.prototype = { - constructor: Context, - reset: function () { - this.prev = 0; - this.next = 0; - this.sent = undefined; - this.done = false; - this.delegate = null; - this.tryEntries.forEach(resetTryEntry); - - for (var tempIndex = 0, tempName; hasOwn.call(this, tempName = "t" + tempIndex) || tempIndex < 20; ++tempIndex) { - this[tempName] = null; - } - }, - stop: function () { - this.done = true; - var rootEntry = this.tryEntries[0]; - var rootRecord = rootEntry.completion; - if (rootRecord.type === "throw") { - throw rootRecord.arg; - } - return this.rval; - }, - dispatchException: function (exception) { - if (this.done) { - throw exception; - } - var context = this; - function handle(loc, caught) { - record.type = "throw"; - record.arg = exception; - context.next = loc; - return !!caught; - } - - for (var i = this.tryEntries.length - 1; i >= 0; --i) { - var entry = this.tryEntries[i]; - var record = entry.completion; - if (entry.tryLoc === "root") { - return handle("end"); - } - if (entry.tryLoc <= this.prev) { - var hasCatch = hasOwn.call(entry, "catchLoc"); - - var hasFinally = hasOwn.call(entry, "finallyLoc"); - - if (hasCatch && hasFinally) { - if (this.prev < entry.catchLoc) { - return handle(entry.catchLoc, true); - } else if (this.prev < entry.finallyLoc) { - return handle(entry.finallyLoc); - } - } else if (hasCatch) { - if (this.prev < entry.catchLoc) { - return handle(entry.catchLoc, true); - } - } else if (hasFinally) { - if (this.prev < entry.finallyLoc) { - return handle(entry.finallyLoc); - } - } else { - throw new Error("try statement without catch or finally"); - } - } - } - }, - _findFinallyEntry: function (finallyLoc) { - for (var i = this.tryEntries.length - 1; i >= 0; --i) { - var entry = this.tryEntries[i]; - if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && (entry.finallyLoc === finallyLoc || this.prev < entry.finallyLoc)) { - return entry; - } - } - }, - abrupt: function (type, arg) { - var entry = this._findFinallyEntry(); - - var record = entry ? entry.completion : {}; - record.type = type; - record.arg = arg; - if (entry) { - this.next = entry.finallyLoc; - } else { - this.complete(record); - } - return ContinueSentinel; - }, - complete: function (record) { - if (record.type === "throw") { - throw record.arg; - } - if (record.type === "break" || record.type === "continue") { - this.next = record.arg; - } else if (record.type === "return") { - this.rval = record.arg; - this.next = "end"; - } - return ContinueSentinel; - }, - finish: function (finallyLoc) { - var entry = this._findFinallyEntry(finallyLoc); - - return this.complete(entry.completion); - }, - "catch": function (tryLoc) { - for (var i = this.tryEntries.length - 1; i >= 0; --i) { - var entry = this.tryEntries[i]; - if (entry.tryLoc === tryLoc) { - var record = entry.completion; - if (record.type === "throw") { - var thrown = record.arg; - resetTryEntry(entry, i); - } - return thrown; - } - } - - throw new Error("illegal catch attempt"); - }, - delegateYield: function (iterable, resultName, nextLoc) { - this.delegate = { - iterator: values(iterable), - resultName: resultName, - nextLoc: nextLoc - }; - return ContinueSentinel; - } - }; -})(); -var x = regeneratorRuntime.mark(function x() { - return regeneratorRuntime.wrap(function x$(context$1$0) { - while (1) switch (context$1$0.prev = context$1$0.next) { - case 0: - case "end": - return context$1$0.stop(); - } - }, x, this); -}); From 5a44793dcbf07cbb0554210f224acdb7350595bd Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 19:24:49 +1100 Subject: [PATCH 105/129] upgrade acorn-6to5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f1365a7d64..69049d6c7a 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "test": "make test" }, "dependencies": { - "acorn-6to5": "0.11.1-17", + "acorn-6to5": "0.11.1-18", "ast-types": "~0.6.1", "chokidar": "0.12.6", "commander": "2.6.0", From 622da331a604cd79761fa19bdcf1b9ffb0f09ca5 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 19:24:54 +1100 Subject: [PATCH 106/129] add 2.13.0 changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1052a028f2..16a6517e92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,14 @@ _Note: Gaps between patch versions are faulty/broken releases._ ## 2.13.0 + * **New Feature** + * Block scoped functions. + * Add `--loose all` option. + * Add playground mallet operator thanks to [@jridgewell](https://github.com/jridgewell). + * **Bug Fix** + * Rewrote large parts of the let scoping transformer. Now passes all known tests. * **Internal** + * Even more optimisations thanks to the awesome [@gaearon](https://github.com/gaearon). * Upgrade `acorn-6to5`. * Upgrade `chokidar` to `0.12.6`. * Upgrade `commander` to `2.6.0`. From dd256dc3f805f5acddbd8d5fe9dee024eaee5bbd Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 19:26:15 +1100 Subject: [PATCH 107/129] add acorn-6to5 changes to 2.13.0 changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16a6517e92..121e17d9af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ _Note: Gaps between patch versions are faulty/broken releases._ * **Internal** * Even more optimisations thanks to the awesome [@gaearon](https://github.com/gaearon). * Upgrade `acorn-6to5`. + * Fixes exponentation operator thanks to [@charliesome](https://github.com/charliesome). + * Fix flow optional parameters with types. * Upgrade `chokidar` to `0.12.6`. * Upgrade `commander` to `2.6.0`. * Upgrade `core-js` to `0.4.5`. From c6be5cccd7d64529e7445628cdc09e3393bd50a9 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 19:29:18 +1100 Subject: [PATCH 108/129] update bin/6to5/file to new source-map api --- bin/6to5/file.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/6to5/file.js b/bin/6to5/file.js index a06bf304ea..aeaf4001eb 100644 --- a/bin/6to5/file.js +++ b/bin/6to5/file.js @@ -28,7 +28,7 @@ module.exports = function (commander, filenames) { map.setSourceContent(filename, result.actual); consumer.eachMapping(function (mapping) { - map._mappings.push({ + map._mappings.add({ generatedLine: mapping.generatedLine + offset, generatedColumn: mapping.generatedColumn, originalLine: mapping.originalLine, From ecfd5fadc6f3159ead7425af61a6158c2a0481a2 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 19:31:07 +1100 Subject: [PATCH 109/129] v2.13.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 69049d6c7a..c65bb29de5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "6to5", "description": "Turn ES6 code into readable vanilla ES5 with source maps", - "version": "2.12.6", + "version": "2.13.0", "author": "Sebastian McKenzie ", "homepage": "https://6to5.org/", "repository": "6to5/6to5", From 7c4701716c2b228e372a3b8cee7d64322242efc9 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 21:26:02 +1100 Subject: [PATCH 110/129] implement block scoping TDZ --- .../transformers/es6-let-scoping.js | 49 +++++++++++++++++-- .../temporal-dead-zone/actual.js | 2 + .../temporal-dead-zone/options.json | 3 ++ 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/transformation/es6-let-scoping/temporal-dead-zone/actual.js create mode 100644 test/fixtures/transformation/es6-let-scoping/temporal-dead-zone/options.json diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index 14849d6c7c..d48e3fcf74 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -57,6 +57,7 @@ exports.Loop = function (node, parent, scope, context, file) { } }; +exports.Program = exports.BlockStatement = function (block, parent, scope, context, file) { if (!t.isLoop(parent)) { var letScoping = new LetScoping(false, block, parent, scope, file); @@ -95,10 +96,12 @@ LetScoping.prototype.run = function () { if (block._letDone) return; block._letDone = true; - // this is a block within a `Function` so we can safely leave it be - if (t.isFunction(this.parent)) return; - var needsClosure = this.getLetReferences(); + this.checkTDZ(); + + // this is a block within a `Function` so we can safely leave it be + if (t.isFunction(this.parent) || t.isProgram(this.block)) return; + if (needsClosure) { this.needsClosure(); } else { @@ -106,6 +109,46 @@ LetScoping.prototype.run = function () { } }; +/** + * Description + */ + +LetScoping.prototype.checkTDZ = function () { + var state = { + letRefs: this.letReferences, + file: this.file + }; + + traverse(this.block, { + enter: function (node, parent, scope, context, state) { + if (!t.isIdentifier(node)) return; + if (!t.isReferenced(node, parent)) return; + + var declared = state.letRefs[node.name]; + if (!declared) return; + + // declared node is different in this scope + if (scope.get(node.name, true) !== declared) return; + + var declaredLoc = declared.loc.start; + var referenceLoc = node.loc.start; + + // does this reference appear on a line before the declaration? + var before = referenceLoc.line < declaredLoc.line; + + if (referenceLoc.line === declaredLoc.line) { + // this reference appears on the same line + // check it appears before the declaration + before = referenceLoc.col < declaredLoc.col; + } + + if (before) { + throw state.file.errorWithNode(node, "Temporal dead zone - accessing a variable before it's initialized"); + } + } + }, this.scope, state); +}; + /** * Description */ diff --git a/test/fixtures/transformation/es6-let-scoping/temporal-dead-zone/actual.js b/test/fixtures/transformation/es6-let-scoping/temporal-dead-zone/actual.js new file mode 100644 index 0000000000..34a3ccd844 --- /dev/null +++ b/test/fixtures/transformation/es6-let-scoping/temporal-dead-zone/actual.js @@ -0,0 +1,2 @@ +qux; +let qux = 456; diff --git a/test/fixtures/transformation/es6-let-scoping/temporal-dead-zone/options.json b/test/fixtures/transformation/es6-let-scoping/temporal-dead-zone/options.json new file mode 100644 index 0000000000..faba38c879 --- /dev/null +++ b/test/fixtures/transformation/es6-let-scoping/temporal-dead-zone/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Temporal dead zone - accessing a variable before it's initialized" +} From 6a884c58a7cff83ae6d68a138310ba11e2a72cf3 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 21:28:34 +1100 Subject: [PATCH 111/129] add 2.13.1 changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 121e17d9af..04a7f6cb16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ _Note: Gaps between patch versions are faulty/broken releases._ +## 2.13.1 + + * **New Feature** + * Temporal dead zone for block binding. + ## 2.13.0 * **New Feature** From a80945cfb4199e998efbe3432ceba184ec90a403 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 21:33:22 +1100 Subject: [PATCH 112/129] ignore function declarations in TDZ detection --- .../transformation/transformers/es6-let-scoping.js | 12 +++++++----- .../transformers/spec-block-scoped-functions.js | 3 +++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index d48e3fcf74..ebff0352b3 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -130,16 +130,18 @@ LetScoping.prototype.checkTDZ = function () { // declared node is different in this scope if (scope.get(node.name, true) !== declared) return; - var declaredLoc = declared.loc.start; - var referenceLoc = node.loc.start; + var declaredLoc = declared.loc; + var referenceLoc = node.loc; + + if (!declaredLoc || !referenceLoc) return; // does this reference appear on a line before the declaration? - var before = referenceLoc.line < declaredLoc.line; + var before = referenceLoc.start.line < declaredLoc.start.line; - if (referenceLoc.line === declaredLoc.line) { + if (referenceLoc.start.line === declaredLoc.start.line) { // this reference appears on the same line // check it appears before the declaration - before = referenceLoc.col < declaredLoc.col; + before = referenceLoc.start.col < declaredLoc.start.col; } if (before) { diff --git a/lib/6to5/transformation/transformers/spec-block-scoped-functions.js b/lib/6to5/transformation/transformers/spec-block-scoped-functions.js index 3d8d3449c8..087b881d9f 100644 --- a/lib/6to5/transformation/transformers/spec-block-scoped-functions.js +++ b/lib/6to5/transformation/transformers/spec-block-scoped-functions.js @@ -7,6 +7,9 @@ exports.FunctionDeclaration = function (node, parent) { return; } + // this is to avoid triggering the TDZ detection + node.id.loc = null; + var declar = t.variableDeclaration("let", [ t.variableDeclarator(node.id, t.toExpression(node)) ]); From 4844882f5e5d79facbcb254e9467c712f45e793a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 21:37:37 +1100 Subject: [PATCH 113/129] break let scoping transformer if there are no block scoped references --- .../transformation/transformers/es6-let-scoping.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index ebff0352b3..5a9c829b31 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -83,6 +83,7 @@ function LetScoping(loopParent, block, parent, scope, file) { this.file = file; this.outsideLetReferences = {}; + this.hasLetReferences = false; this.letReferences = {}; this.body = []; } @@ -99,9 +100,12 @@ LetScoping.prototype.run = function () { var needsClosure = this.getLetReferences(); this.checkTDZ(); - // this is a block within a `Function` so we can safely leave it be + // this is a block within a `Function/Program` so we can safely leave it be if (t.isFunction(this.parent) || t.isProgram(this.block)) return; + // we can skip everything + if (!this.hasLetReferences) return; + if (needsClosure) { this.needsClosure(); } else { @@ -284,8 +288,12 @@ LetScoping.prototype.getLetReferences = function () { declar = declarators[i]; var keys = t.getIds(declar, true); _.extend(this.letReferences, keys); + this.hasLetReferences = true; } + // no let references so we can just quit + if (!this.hasLetReferences) return; + // set let references to plain var references standardiseLets(declarators); From d877a04397525fc66942cb028de8a5db961878b9 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 21:40:21 +1100 Subject: [PATCH 114/129] v2.13.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c65bb29de5..75d438cb68 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "6to5", "description": "Turn ES6 code into readable vanilla ES5 with source maps", - "version": "2.13.0", + "version": "2.13.1", "author": "Sebastian McKenzie ", "homepage": "https://6to5.org/", "repository": "6to5/6to5", From 24d6c3f48842f4636cfabf062eba8113cf799e17 Mon Sep 17 00:00:00 2001 From: Ryunosuke SATO Date: Sun, 18 Jan 2015 20:16:32 +0900 Subject: [PATCH 115/129] Remove unused local variable `hasOptional` is assigned but unused from anywhere. --- bin/6to5/index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/bin/6to5/index.js b/bin/6to5/index.js index 955671555d..6b1019ba4e 100755 --- a/bin/6to5/index.js +++ b/bin/6to5/index.js @@ -34,13 +34,10 @@ commander.on("--help", function(){ console.log(" " + title + ":"); console.log(); - var hasOptional = true; - _.each(_.keys(obj).sort(), function (key) { if (key[0] === "_") return; if (obj[key].optional) { - hasOptional = true; key = "[" + key + "]"; } From 8fc7af5480bb4f190cc3c295f292ffa4694a1ff3 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 19 Jan 2015 08:35:01 +1100 Subject: [PATCH 116/129] properly reference method body - fixes #530 --- lib/6to5/transformation/helpers/replace-supers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/6to5/transformation/helpers/replace-supers.js b/lib/6to5/transformation/helpers/replace-supers.js index 4ec70cf6b5..2403e878cb 100644 --- a/lib/6to5/transformation/helpers/replace-supers.js +++ b/lib/6to5/transformation/helpers/replace-supers.js @@ -152,7 +152,7 @@ ReplaceSupers.prototype.getThisReference = function () { return this.topLevelThisReference; } else { var ref = this.topLevelThisReference = this.file.generateUidIdentifier("this"); - this.methodNode.body.body.unshift(t.variableDeclaration("var", [ + this.methodNode.value.body.body.unshift(t.variableDeclaration("var", [ t.variableDeclarator(this.topLevelThisReference, t.thisExpression()) ])); return ref; From ca41612109883b2bfd3979cc9afa63d372805c03 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 19 Jan 2015 08:53:42 +1100 Subject: [PATCH 117/129] use process.stdin.write instead of console.log to avoid console.log sprintf - fixes #527 --- bin/6to5/file.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/6to5/file.js b/bin/6to5/file.js index aeaf4001eb..6a1dd9b7fc 100644 --- a/bin/6to5/file.js +++ b/bin/6to5/file.js @@ -63,7 +63,7 @@ module.exports = function (commander, filenames) { fs.writeFileSync(commander.outFile, result.code); } else { - console.log(result.code); + process.stdout.write(result.code + "\n"); } }; From 16b7ff972c5fa7b2937f64b1708204aabfd3f00a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 19 Jan 2015 08:54:04 +1100 Subject: [PATCH 118/129] split up tdz into an optional transformer until it has a better implementation - fixes #527 --- lib/6to5/transformation/transform.js | 1 + .../transformers/es6-let-scoping.js | 47 +------------------ .../optional-block-scoping-tdz.js | 47 +++++++++++++++++++ .../temporal-dead-zone/options.json | 3 +- 4 files changed, 52 insertions(+), 46 deletions(-) create mode 100644 lib/6to5/transformation/transformers/optional-block-scoping-tdz.js diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 2a5cbb26a2..e0238cc5f0 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -79,6 +79,7 @@ _.each({ constants: require("./transformers/es6-constants"), letScoping: require("./transformers/es6-let-scoping"), + blockScopingTDZ: require("./transformers/optional-block-scoping-tdz"), _blockHoist: require("./transformers/_block-hoist"), diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index 5a9c829b31..b7d7f5fbb2 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -84,7 +84,7 @@ function LetScoping(loopParent, block, parent, scope, file) { this.outsideLetReferences = {}; this.hasLetReferences = false; - this.letReferences = {}; + this.letReferences = block._letReferences = {}; this.body = []; } @@ -98,7 +98,6 @@ LetScoping.prototype.run = function () { block._letDone = true; var needsClosure = this.getLetReferences(); - this.checkTDZ(); // this is a block within a `Function/Program` so we can safely leave it be if (t.isFunction(this.parent) || t.isProgram(this.block)) return; @@ -113,48 +112,6 @@ LetScoping.prototype.run = function () { } }; -/** - * Description - */ - -LetScoping.prototype.checkTDZ = function () { - var state = { - letRefs: this.letReferences, - file: this.file - }; - - traverse(this.block, { - enter: function (node, parent, scope, context, state) { - if (!t.isIdentifier(node)) return; - if (!t.isReferenced(node, parent)) return; - - var declared = state.letRefs[node.name]; - if (!declared) return; - - // declared node is different in this scope - if (scope.get(node.name, true) !== declared) return; - - var declaredLoc = declared.loc; - var referenceLoc = node.loc; - - if (!declaredLoc || !referenceLoc) return; - - // does this reference appear on a line before the declaration? - var before = referenceLoc.start.line < declaredLoc.start.line; - - if (referenceLoc.start.line === declaredLoc.start.line) { - // this reference appears on the same line - // check it appears before the declaration - before = referenceLoc.start.col < declaredLoc.start.col; - } - - if (before) { - throw state.file.errorWithNode(node, "Temporal dead zone - accessing a variable before it's initialized"); - } - } - }, this.scope, state); -}; - /** * Description */ @@ -179,7 +136,7 @@ LetScoping.prototype.remap = function () { var uid = file.generateUidIdentifier(ref.name, scope).name; ref.name = uid; - remaps[key] = { + remaps[key] = remaps[uid] = { node: ref, uid: uid }; diff --git a/lib/6to5/transformation/transformers/optional-block-scoping-tdz.js b/lib/6to5/transformation/transformers/optional-block-scoping-tdz.js new file mode 100644 index 0000000000..22a9474197 --- /dev/null +++ b/lib/6to5/transformation/transformers/optional-block-scoping-tdz.js @@ -0,0 +1,47 @@ +var traverse = require("../../traverse"); +var t = require("../../types"); + +exports.optional = true; + +exports.Loop +exports.Program = +exports.BlockStatement = function (node, parent, scope, context, file) { + var letRefs = node._letReferences; + if (!letRefs) return; + + var state = { + letRefs: letRefs, + file: file + }; + + traverse(node, { + enter: function (node, parent, scope, context, state) { + if (!t.isIdentifier(node)) return; + if (!t.isReferenced(node, parent)) return; + + var declared = state.letRefs[node.name]; + if (!declared) return; + + // declared node is different in this scope + if (scope.get(node.name, true) !== declared) return; + + var declaredLoc = declared.loc; + var referenceLoc = node.loc; + + if (!declaredLoc || !referenceLoc) return; + + // does this reference appear on a line before the declaration? + var before = referenceLoc.start.line < declaredLoc.start.line; + + if (referenceLoc.start.line === declaredLoc.start.line) { + // this reference appears on the same line + // check it appears before the declaration + before = referenceLoc.start.col < declaredLoc.start.col; + } + + if (before) { + throw state.file.errorWithNode(node, "Temporal dead zone - accessing a variable before it's initialized"); + } + } + }, scope, state); +}; diff --git a/test/fixtures/transformation/es6-let-scoping/temporal-dead-zone/options.json b/test/fixtures/transformation/es6-let-scoping/temporal-dead-zone/options.json index faba38c879..bcd3d8f6e2 100644 --- a/test/fixtures/transformation/es6-let-scoping/temporal-dead-zone/options.json +++ b/test/fixtures/transformation/es6-let-scoping/temporal-dead-zone/options.json @@ -1,3 +1,4 @@ { - "throws": "Temporal dead zone - accessing a variable before it's initialized" + "throws": "Temporal dead zone - accessing a variable before it's initialized", + "optional": ["blockScopingTDZ"] } From 8d8dd5fa3731a1f8cc844430e14b7f5a5db8a485 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 19 Jan 2015 08:54:26 +1100 Subject: [PATCH 119/129] add let scoping transformer rename note --- NOTES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NOTES.md b/NOTES.md index a973eaafdf..7219db2712 100644 --- a/NOTES.md +++ b/NOTES.md @@ -11,3 +11,4 @@ * Add autoindentation. * Move `super` transformation from classes into a separate transformer that also supports object expressions. * Remove fast transformer backwards compatibility. + * Rename let scoping transformer to block scoping. From 0b44137d1f019c9a5a62aa7c1f051d99a6e1f427 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 19 Jan 2015 08:56:58 +1100 Subject: [PATCH 120/129] exclude coverage folder from npm --- .npmignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.npmignore b/.npmignore index d8e51136b0..efc55d3d15 100644 --- a/.npmignore +++ b/.npmignore @@ -10,3 +10,4 @@ dist tests.json CHANGELOG.md .package.json +coverage From 2d41b09c3b44eb5c323d19b30d69092b589dd77b Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 19 Jan 2015 08:57:04 +1100 Subject: [PATCH 121/129] add 2.12.3 changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04a7f6cb16..c7b5694e2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,14 @@ _Note: Gaps between patch versions are faulty/broken releases._ +## 2.13.2 + + * **Bug Fix** + * Fix `super` inside of nested functions. + * **Internal** + * Move let scoping TDZ into a separate transformer until it's more solid. + * Use `process.stdin.write` instead of `console.log` in `bin` to avoid sprintfification. + ## 2.13.1 * **New Feature** From b63accca252b8e174230a4329f0be4cbe7c493cc Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 19 Jan 2015 08:57:43 +1100 Subject: [PATCH 122/129] remove --mangle sort from uglify since it's causing issues in safari/ios --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1acfc5281f..5ceff1b52e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ BROWSERIFY_CMD = node_modules/browserify/bin/cmd.js ISTANBUL_CMD = node_modules/istanbul/lib/cli.js cover -UGLIFY_CMD = node_modules/uglify-js/bin/uglifyjs --mangle sort +UGLIFY_CMD = node_modules/uglify-js/bin/uglifyjs +#UGLIFY_CMD = node_modules/uglify-js/bin/uglifyjs --mangle sort JSHINT_CMD = node_modules/jshint/bin/jshint MOCHA_CMD = node_modules/mocha/bin/_mocha From b719eaf6ab5563cc1d266a4e10c5f0bec43e7fcb Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 19 Jan 2015 08:58:11 +1100 Subject: [PATCH 123/129] add missing semicolon --- .../transformation/transformers/optional-block-scoping-tdz.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/6to5/transformation/transformers/optional-block-scoping-tdz.js b/lib/6to5/transformation/transformers/optional-block-scoping-tdz.js index 22a9474197..972cca4567 100644 --- a/lib/6to5/transformation/transformers/optional-block-scoping-tdz.js +++ b/lib/6to5/transformation/transformers/optional-block-scoping-tdz.js @@ -3,7 +3,7 @@ var t = require("../../types"); exports.optional = true; -exports.Loop +exports.Loop = exports.Program = exports.BlockStatement = function (node, parent, scope, context, file) { var letRefs = node._letReferences; From 078b09676fe7b38e125b8b54fab0e3c45c6044d4 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 19 Jan 2015 09:00:15 +1100 Subject: [PATCH 124/129] v2.13.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 75d438cb68..d4798a487d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "6to5", "description": "Turn ES6 code into readable vanilla ES5 with source maps", - "version": "2.13.1", + "version": "2.13.2", "author": "Sebastian McKenzie ", "homepage": "https://6to5.org/", "repository": "6to5/6to5", From 6d67105babad296496b53c4e46582f0d2bc27b2f Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 19 Jan 2015 09:02:51 +1100 Subject: [PATCH 125/129] add coverage and vendor to npmignore --- .npmignore | 1 + CHANGELOG.md | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/.npmignore b/.npmignore index efc55d3d15..97d91a42da 100644 --- a/.npmignore +++ b/.npmignore @@ -11,3 +11,4 @@ tests.json CHANGELOG.md .package.json coverage +vendor diff --git a/CHANGELOG.md b/CHANGELOG.md index c7b5694e2a..90dc942ac5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ _Note: Gaps between patch versions are faulty/broken releases._ +## 2.13.3 + + * **Internal** + * Add `coverage` and `vendor` to npmignore. + ## 2.13.2 * **Bug Fix** From 857abf5024f82dedc580243e46abdbca85c28694 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 19 Jan 2015 09:05:37 +1100 Subject: [PATCH 126/129] v2.13.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d4798a487d..0bcce27ea4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "6to5", "description": "Turn ES6 code into readable vanilla ES5 with source maps", - "version": "2.13.2", + "version": "2.13.3", "author": "Sebastian McKenzie ", "homepage": "https://6to5.org/", "repository": "6to5/6to5", From 4a1addc558db97d3b25e70c4fdfef5fe9a2cbf65 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 19 Jan 2015 22:09:05 +1100 Subject: [PATCH 127/129] better whitespace for VariableDeclarations --- lib/6to5/generation/generators/statements.js | 11 +++++------ .../expected.js | 6 ++++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/6to5/generation/generators/statements.js b/lib/6to5/generation/generators/statements.js index 6cc66dd255..473ce5a18b 100644 --- a/lib/6to5/generation/generators/statements.js +++ b/lib/6to5/generation/generators/statements.js @@ -169,20 +169,19 @@ exports.DebuggerStatement = function () { exports.VariableDeclaration = function (node, print, parent) { this.push(node.kind + " "); - var inits = 0; - var noInits = 0; + var hasInits = false; + // don't add whitespace to loop heads if (!t.isFor(parent)) { for (var i = 0; i < node.declarations.length; i++) { if (node.declarations[i].init) { - inits++; - } else { - noInits++; + // has an init so let's split it up over multiple lines + hasInits = true; } } } var sep = ","; - if (inits > noInits) { // more inits than no inits so let's add a newline + if (hasInits) { sep += "\n" + util.repeat(node.kind.length + 1); } else { sep += " "; diff --git a/test/fixtures/generation/types/VariableDeclaration-VariableDeclarator/expected.js b/test/fixtures/generation/types/VariableDeclaration-VariableDeclarator/expected.js index 86c14e5b1f..487fff2ae2 100644 --- a/test/fixtures/generation/types/VariableDeclaration-VariableDeclarator/expected.js +++ b/test/fixtures/generation/types/VariableDeclaration-VariableDeclarator/expected.js @@ -5,8 +5,10 @@ let foo = "foo"; var foo = "bar"; const foo = "foo"; -let foo, bar = "bar"; -var foo, bar = "bar"; +let foo, + bar = "bar"; +var foo, + bar = "bar"; let foo = "foo", bar = "bar"; From da16bf1e427efc7ce8503a1f0e27fa23d5a6f04e Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 20 Jan 2015 00:17:11 +1100 Subject: [PATCH 128/129] add t.isReferencedIdentifier --- .../transformers/es6-default-parameters.js | 2 +- .../transformation/transformers/es6-let-scoping.js | 8 ++------ .../transformers/optional-block-scoping-tdz.js | 3 +-- .../transformers/optional-core-aliasing.js | 2 +- lib/6to5/types/index.js | 12 ++++++++++++ 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-default-parameters.js b/lib/6to5/transformation/transformers/es6-default-parameters.js index 408c1a7e63..af1b163e5b 100644 --- a/lib/6to5/transformation/transformers/es6-default-parameters.js +++ b/lib/6to5/transformation/transformers/es6-default-parameters.js @@ -17,7 +17,7 @@ exports.Function = function (node, parent, scope, context, file) { var checkTDZ = function (ids) { var check = function (node, parent) { - if (!t.isIdentifier(node) || !t.isReferenced(node, parent)) return; + if (!t.isReferencedIdentifier(node, parent)) return; if (ids.indexOf(node.name) >= 0) { throw file.errorWithNode(node, "Temporal dead zone - accessing a variable before it's initialized"); diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index b7d7f5fbb2..4d0f13910e 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -146,8 +146,7 @@ LetScoping.prototype.remap = function () { // var replace = function (node, parent, scope, context, remaps) { - if (!t.isIdentifier(node)) return; - if (!t.isReferenced(node, parent)) return; + if (!t.isReferencedIdentifier(node, parent)) return; var remap = remaps[node.name]; if (!remap) return; @@ -266,11 +265,8 @@ LetScoping.prototype.getLetReferences = function () { if (t.isFunction(node)) { traverse(node, { enter: function (node, parent) { - // not an identifier so we have no use - if (!t.isIdentifier(node)) return; - // not a direct reference - if (!t.isReferenced(node, parent)) return; + if (!t.isReferencedIdentifier(node, parent)) return; // this scope has a variable with the same name so it couldn't belong // to our let scope diff --git a/lib/6to5/transformation/transformers/optional-block-scoping-tdz.js b/lib/6to5/transformation/transformers/optional-block-scoping-tdz.js index 972cca4567..27472f8bdf 100644 --- a/lib/6to5/transformation/transformers/optional-block-scoping-tdz.js +++ b/lib/6to5/transformation/transformers/optional-block-scoping-tdz.js @@ -16,8 +16,7 @@ exports.BlockStatement = function (node, parent, scope, context, file) { traverse(node, { enter: function (node, parent, scope, context, state) { - if (!t.isIdentifier(node)) return; - if (!t.isReferenced(node, parent)) return; + if (!t.isReferencedIdentifier(node, parent)) return; var declared = state.letRefs[node.name]; if (!declared) return; diff --git a/lib/6to5/transformation/transformers/optional-core-aliasing.js b/lib/6to5/transformation/transformers/optional-core-aliasing.js index c0db8bd9bd..90bb4621a7 100644 --- a/lib/6to5/transformation/transformers/optional-core-aliasing.js +++ b/lib/6to5/transformation/transformers/optional-core-aliasing.js @@ -42,7 +42,7 @@ exports.ast = { context.skip(); return t.prependToMemberExpression(node, file._coreId); } - } else if (t.isIdentifier(node) && !t.isMemberExpression(parent) && t.isReferenced(node, parent) && _.contains(ALIASABLE_CONSTRUCTORS, node.name)) { + } else if (t.isReferencedIdentifier(node, parent) && !t.isMemberExpression(parent) && _.contains(ALIASABLE_CONSTRUCTORS, node.name)) { // Symbol() -> _core.Symbol(); new Promise -> new _core.Promise return t.memberExpression(file._coreId, node); } else if (t.isCallExpression(node)) { diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 31c783dc1f..8bc6cb5d75 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -263,6 +263,18 @@ t.isReferenced = function (node, parent) { return false; }; +/** + * Description + * + * @param {Object} node + * @param {Object} parent + * @returns {Boolean} + */ + +t.isReferencedIdentifier = function (node, parent) { + return t.isIdentifier(node) && t.isReferenced(node, parent); +}; + /** * Description * From f322252c362212a0095dfea00041d56a53f6e2ef Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 20 Jan 2015 00:17:22 +1100 Subject: [PATCH 129/129] add optional undeclared variable check transformer --- lib/6to5/transformation/transform.js | 1 + .../transformers/optional-undeclared-variable-check.js | 9 +++++++++ .../optional-undeclared-variable-check/declared/exec.js | 5 +++++ .../optional-undeclared-variable-check/options.json | 3 +++ .../undeclared/exec.js | 1 + .../undeclared/options.json | 3 +++ 6 files changed, 22 insertions(+) create mode 100644 lib/6to5/transformation/transformers/optional-undeclared-variable-check.js create mode 100644 test/fixtures/transformation/optional-undeclared-variable-check/declared/exec.js create mode 100644 test/fixtures/transformation/optional-undeclared-variable-check/options.json create mode 100644 test/fixtures/transformation/optional-undeclared-variable-check/undeclared/exec.js create mode 100644 test/fixtures/transformation/optional-undeclared-variable-check/undeclared/options.json diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index e0238cc5f0..a98b0fe820 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -98,6 +98,7 @@ _.each({ typeofSymbol: require("./transformers/optional-typeof-symbol"), coreAliasing: require("./transformers/optional-core-aliasing"), undefinedToVoid: require("./transformers/optional-undefined-to-void"), + undeclaredVariableCheck: require("./transformers/optional-undeclared-variable-check"), // spec specPropertyLiterals: require("./transformers/spec-property-literals"), diff --git a/lib/6to5/transformation/transformers/optional-undeclared-variable-check.js b/lib/6to5/transformation/transformers/optional-undeclared-variable-check.js new file mode 100644 index 0000000000..6640c0217f --- /dev/null +++ b/lib/6to5/transformation/transformers/optional-undeclared-variable-check.js @@ -0,0 +1,9 @@ +var t = require("../../types"); + +exports.optional = true; + +exports.Identifier = function (node, parent, scope, context, file) { + if (!scope.has(node.name, true)) { + throw file.errorWithNode(node, "Reference to undeclared variable"); + } +}; diff --git a/test/fixtures/transformation/optional-undeclared-variable-check/declared/exec.js b/test/fixtures/transformation/optional-undeclared-variable-check/declared/exec.js new file mode 100644 index 0000000000..e2721feb4a --- /dev/null +++ b/test/fixtures/transformation/optional-undeclared-variable-check/declared/exec.js @@ -0,0 +1,5 @@ +function foo() { + +} + +foo(); diff --git a/test/fixtures/transformation/optional-undeclared-variable-check/options.json b/test/fixtures/transformation/optional-undeclared-variable-check/options.json new file mode 100644 index 0000000000..535d39109b --- /dev/null +++ b/test/fixtures/transformation/optional-undeclared-variable-check/options.json @@ -0,0 +1,3 @@ +{ + "optional": ["undeclaredVariableCheck"] +} diff --git a/test/fixtures/transformation/optional-undeclared-variable-check/undeclared/exec.js b/test/fixtures/transformation/optional-undeclared-variable-check/undeclared/exec.js new file mode 100644 index 0000000000..a280f9a5cc --- /dev/null +++ b/test/fixtures/transformation/optional-undeclared-variable-check/undeclared/exec.js @@ -0,0 +1 @@ +foo(); diff --git a/test/fixtures/transformation/optional-undeclared-variable-check/undeclared/options.json b/test/fixtures/transformation/optional-undeclared-variable-check/undeclared/options.json new file mode 100644 index 0000000000..a661ffc703 --- /dev/null +++ b/test/fixtures/transformation/optional-undeclared-variable-check/undeclared/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Reference to undeclared variable" +}