Compare commits

...

6 Commits

Author SHA1 Message Date
Sebastian McKenzie
005f5b2174 v2.0.4 2014-12-28 11:06:04 +11:00
Sebastian McKenzie
8551c94fa0 fix linting errors 2014-12-28 11:05:27 +11:00
Sebastian McKenzie
c509d06bc2 vaoid being greedy when destructuring array iterables 2014-12-28 11:04:00 +11:00
Sebastian McKenzie
f21743012b v2.0.3 2014-12-28 09:37:32 +11:00
Sebastian McKenzie
22dc33f93c remove unused variable 2014-12-28 09:36:29 +11:00
Sebastian McKenzie
6df6652629 better circular references in amd/umd/system module formatter 2014-12-28 09:35:47 +11:00
23 changed files with 268 additions and 83 deletions

View File

@@ -2,6 +2,15 @@
Gaps between patch versions are faulty/broken releases.
## 2.0.4
* Avoid being greedy when destructuring array iterables.
## 2.0.3
* Hoist function declarations in system module formatter for circular references.
* Hoist default function declarations in umd and amd module formatters for circular references.
## 2.0.2
* Inherit comments in `for-of` transformer.

View File

@@ -22,6 +22,7 @@ File.declarations = [
"tagged-template-literal",
"interop-require",
"to-array",
"sliced-to-array",
"object-without-properties",
"has-own",
"slice"
@@ -82,13 +83,19 @@ File.normaliseOptions = function (opts) {
return opts;
};
File.prototype.toArray = function (node) {
File.prototype.toArray = function (node, i) {
if (t.isArrayExpression(node)) {
return node;
} else if (t.isIdentifier(node) && node.name === "arguments") {
return t.callExpression(t.memberExpression(this.addDeclaration("slice"), t.identifier("call")), [node]);
} else {
return t.callExpression(this.addDeclaration("to-array"), [node]);
var declarationName = "to-array";
var args = [node];
if (i) {
args.push(t.literal(i));
declarationName = "sliced-to-array";
}
return t.callExpression(this.addDeclaration(declarationName), args);
}
};

View File

@@ -0,0 +1,12 @@
(function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else {
var _arr = [];
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
_arr.push(_step.value);
if (i && _arr.length === i) break;
}
return _arr;
}
});

View File

@@ -169,30 +169,36 @@ DefaultFormatter.prototype._exportsAssign = function (id, init) {
DefaultFormatter.prototype.exportDeclaration = function (node, nodes) {
var declar = node.declaration;
var id = declar.id;
if (node.default) {
nodes.push(
this._exportsAssign(t.identifier("default"), this._pushStatement(declar, nodes))
);
} else {
var assign;
id = t.identifier("default");
}
if (t.isVariableDeclaration(declar)) {
for (var i in declar.declarations) {
var decl = declar.declarations[i];
var assign;
decl.init = this._exportsAssign(decl.id, decl.init).expression;
if (t.isVariableDeclaration(declar)) {
for (var i in declar.declarations) {
var decl = declar.declarations[i];
var newDeclar = t.variableDeclaration(declar.kind, [decl]);
if (i === "0") t.inherits(newDeclar, declar);
nodes.push(newDeclar);
}
} else {
assign = this._exportsAssign(declar.id, declar.id);
decl.init = this._exportsAssign(decl.id, decl.init).expression;
nodes.push(t.toStatement(declar));
nodes.push(assign);
this._hoistExport(declar, assign);
var newDeclar = t.variableDeclaration(declar.kind, [decl]);
if (i === "0") t.inherits(newDeclar, declar);
nodes.push(newDeclar);
}
} else {
var ref = declar;
if (t.isFunctionDeclaration(declar) || t.isClassDeclaration(declar)) {
ref = declar.id;
nodes.push(declar);
}
assign = this._exportsAssign(id, ref);
nodes.push(assign);
this._hoistExport(declar, assign);
}
};

View File

@@ -2,6 +2,7 @@ module.exports = SystemFormatter;
var DefaultFormatter = require("./_default");
var AMDFormatter = require("./amd");
var traverse = require("../../traverse");
var util = require("../../util");
var t = require("../../types");
var _ = require("lodash");
@@ -74,12 +75,28 @@ SystemFormatter.prototype.transform = function (ast) {
EXECUTE: t.functionExpression(null, [], t.blockStatement(program.body))
}, true);
var handlerBody = runner.expression.arguments[2].body.body;
var returnStatement = handlerBody.pop();
// hoist up function declarations for circular references
traverse(program, {
enter: function (node) {
if (t.isFunction(node)) this.stop();
if (t.isFunctionDeclaration(node) || node._blockHoist) {
handlerBody.push(node);
this.remove();
}
}
});
if (!_.isEmpty(this.ids)) {
var handlerBody = runner.expression.arguments[2].body.body;
handlerBody.unshift(t.variableDeclaration("var", _.map(this.ids, function (uid) {
handlerBody.push(t.variableDeclaration("var", _.map(this.ids, function (uid) {
return t.variableDeclarator(uid);
})));
}
handlerBody.push(returnStatement);
program.body = [runner];
};

View File

@@ -61,13 +61,27 @@ var pushObjectPattern = function (opts, nodes, pattern, parentId) {
};
var pushArrayPattern = function (opts, nodes, pattern, parentId) {
if (!pattern.elements) return;
var i;
var hasSpreadElement = false;
for (i in pattern.elements) {
if (t.isSpreadElement(pattern.elements[i])) {
hasSpreadElement = true;
break;
}
}
var toArray = opts.file.toArray(parentId, !hasSpreadElement && pattern.elements.length);
var _parentId = opts.file.generateUidIdentifier("ref", opts.scope);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(_parentId, opts.file.toArray(parentId))
t.variableDeclarator(_parentId, toArray)
]));
parentId = _parentId;
for (var i in pattern.elements) {
for (i in pattern.elements) {
var elem = pattern.elements[i];
if (!elem) continue;

View File

@@ -50,11 +50,16 @@ function traverse(parent, opts, scope) {
}
};
var stop = false;
var stopped = false;
var removed = false;
var context = {
stop: function () {
stop = true;
stopped = true;
},
remove: function () {
stopped = removed = true;
}
};
@@ -67,8 +72,13 @@ function traverse(parent, opts, scope) {
var result = opts.enter.call(context, node, parent, ourScope);
maybeReplace(result);
if (removed) {
delete obj[key];
updated = true;
}
// stop iteration
if (stop || result === false) return;
if (stopped || result === false) return;
}
// traverse node

View File

@@ -1,7 +1,7 @@
{
"name": "6to5",
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
"version": "2.0.2",
"version": "2.0.4",
"author": "Sebastian McKenzie <sebmck@gmail.com>",
"homepage": "https://github.com/6to5/6to5",
"repository": {

View File

@@ -1,18 +1,29 @@
"use strict";
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
var _slicedToArray = function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else {
var _arr = [];
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
_arr.push(_step.value);
if (i && _arr.length === i) break;
}
return _arr;
}
};
var _ref = [1, 2];
var _ref2 = _toArray(_ref);
var _ref2 = _slicedToArray(_ref, 2);
var a = _ref2[0];
var b = _ref2[1];
var _ref3 = [3, 4];
var _ref4 = _toArray(_ref3);
var _ref4 = _slicedToArray(_ref3, 2);
var c = _ref4[0];
var d = _ref4[1];

View File

@@ -1,18 +1,29 @@
"use strict";
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
var _slicedToArray = function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else {
var _arr = [];
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
_arr.push(_step.value);
if (i && _arr.length === i) break;
}
return _arr;
}
};
var _ref = ["hello", [", ", "junk"], ["world"]];
var _ref2 = _toArray(_ref);
var _ref2 = _slicedToArray(_ref, 4);
var a = _ref2[0];
var _ref3 = _toArray(_ref2[1]);
var _ref3 = _slicedToArray(_ref2[1], 1);
var b = _ref3[0];
var _ref4 = _toArray(_ref2[2]);
var _ref4 = _slicedToArray(_ref2[2], 1);
var c = _ref4[0];
var d = _ref2[3];

View File

@@ -1,8 +1,19 @@
"use strict";
var _temp, _ref;
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
var _slicedToArray = function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else {
var _arr = [];
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
_arr.push(_step.value);
if (i && _arr.length === i) break;
}
return _arr;
}
};
console.log((_temp = [123], _ref = _toArray(_temp), x = _ref[0], _temp));
console.log((_temp = [123], _ref = _slicedToArray(_temp, 1), x = _ref[0], _temp));

View File

@@ -1,12 +1,23 @@
"use strict";
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
var _slicedToArray = function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else {
var _arr = [];
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
_arr.push(_step.value);
if (i && _arr.length === i) break;
}
return _arr;
}
};
var _ref = f();
var _ref2 = _toArray(_ref);
var _ref2 = _slicedToArray(_ref, 2);
a = _ref2[0];
b = _ref2[1];

View File

@@ -1,18 +1,29 @@
"use strict";
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
var _slicedToArray = function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else {
var _arr = [];
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
_arr.push(_step.value);
if (i && _arr.length === i) break;
}
return _arr;
}
};
var _ref = ["foo", "hello", [", ", "junk"], ["world"]];
var _ref2 = _toArray(_ref);
var _ref2 = _slicedToArray(_ref, 5);
var a = _ref2[1];
var _ref3 = _toArray(_ref2[2]);
var _ref3 = _slicedToArray(_ref2[2], 1);
var b = _ref3[0];
var _ref4 = _toArray(_ref2[3]);
var _ref4 = _slicedToArray(_ref2[3], 1);
var c = _ref4[0];
var d = _ref2[4];

View File

@@ -1,11 +1,22 @@
"use strict";
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
var _slicedToArray = function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else {
var _arr = [];
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
_arr.push(_step.value);
if (i && _arr.length === i) break;
}
return _arr;
}
};
for (var _ref in obj) {
var _ref2 = _toArray(_ref);
var _ref2 = _slicedToArray(_ref, 2);
var name = _ref2[0];
var value = _ref2[1];

View File

@@ -1,12 +1,23 @@
"use strict";
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
var _slicedToArray = function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else {
var _arr = [];
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
_arr.push(_step.value);
if (i && _arr.length === i) break;
}
return _arr;
}
};
for (var _iterator = this.test.expectation.registers[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
var _ref = _step.value;
var _ref2 = _toArray(_ref);
var _ref2 = _slicedToArray(_ref, 3);
var name = _ref2[0];
var before = _ref2[1];

View File

@@ -1,12 +1,23 @@
"use strict";
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
var _slicedToArray = function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else {
var _arr = [];
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
_arr.push(_step.value);
if (i && _arr.length === i) break;
}
return _arr;
}
};
var _ref = [1, 2];
var _ref2 = _toArray(_ref);
var _ref2 = _slicedToArray(_ref, 2);
this.foo = _ref2[0];
this.bar = _ref2[1];

View File

@@ -1,14 +1,25 @@
"use strict";
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
var _slicedToArray = function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else {
var _arr = [];
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
_arr.push(_step.value);
if (i && _arr.length === i) break;
}
return _arr;
}
};
var _ref = _toArray(rect.topLeft);
var _ref = _slicedToArray(rect.topLeft, 2);
var x1 = _ref[0];
var y1 = _ref[1];
var _ref2 = _toArray(rect.bottomRight);
var _ref2 = _slicedToArray(rect.bottomRight, 2);
var x2 = _ref2[0];
var y2 = _ref2[1];

View File

@@ -1,7 +1,18 @@
"use strict";
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
var _slicedToArray = function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else {
var _arr = [];
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
_arr.push(_step.value);
if (i && _arr.length === i) break;
}
return _arr;
}
};
function somethingAdvanced(_ref) {
@@ -20,12 +31,12 @@ function unpackObject(_ref2) {
console.log(unpackObject({ title: "title", author: "author" }));
var unpackArray = function (_ref3, _ref5) {
var _ref4 = _toArray(_ref3);
var _ref4 = _slicedToArray(_ref3, 3);
var a = _ref4[0];
var b = _ref4[1];
var c = _ref4[2];
var _ref6 = _toArray(_ref5);
var _ref6 = _slicedToArray(_ref5, 3);
var x = _ref6[0];
var y = _ref6[1];

View File

@@ -1,6 +1,7 @@
define(["exports"], function (exports) {
"use strict";
exports["default"] = foo;
exports["default"] = 42;
exports["default"] = {};
exports["default"] = [];
@@ -10,7 +11,6 @@ define(["exports"], function (exports) {
exports["default"] = function () {};
function foo() {}
exports["default"] = foo;
var Foo = function Foo() {};
exports["default"] = Foo;

View File

@@ -1,4 +1,7 @@
System.register("es6-modules-system/exports-default/expected", [], function (_export) {
_export("default", foo);
function foo() {}
return {
setters: [],
execute: function () {
@@ -16,12 +19,9 @@ System.register("es6-modules-system/exports-default/expected", [], function (_ex
_export("default", function () {});
function foo() {}
_export("default", foo);
var Foo = function Foo() {};
_export("default", Foo);
}
};
});
});

View File

@@ -1,21 +1,21 @@
System.register("es6-modules-system/exports-variable/expected", [], function (_export) {
_export("foo7", foo7);
function foo7() {}
return {
setters: [],
execute: function () {
"use strict";
_export("foo7", foo7);
var foo = _export("foo", 1);
var foo2 = _export("foo2", function () {});
var foo3 = _export("foo3", undefined);
var foo4 = _export("foo4", 2);
var foo5 = _export("foo5", undefined);
var foo6 = _export("foo6", 3);
function foo7() {}
var foo8 = function foo8() {};
_export("foo8", foo8);
}
};
});
});

View File

@@ -1,4 +1,10 @@
System.register("es6-modules-system/hoist-function-exports/expected", ["./evens"], function (_export) {
_export("nextOdd", nextOdd);
function nextOdd(n) {
return isEven(n) ? n + 1 : n + 2;
}
var _evens;
return {
setters: [function (m) {
@@ -7,13 +13,7 @@ System.register("es6-modules-system/hoist-function-exports/expected", ["./evens"
execute: function () {
"use strict";
_export("nextOdd", nextOdd);
var isEven = _evens.isEven;
function nextOdd(n) {
return isEven(n) ? n + 1 : n + 2;
}
var isOdd = _export("isOdd", (function (isEven) {
return function (n) {
return !isEven(n);
@@ -21,4 +21,4 @@ System.register("es6-modules-system/hoist-function-exports/expected", ["./evens"
})(isEven));
}
};
});
});

View File

@@ -7,6 +7,7 @@
})(function (exports) {
"use strict";
exports["default"] = foo;
exports["default"] = 42;
exports["default"] = {};
exports["default"] = [];
@@ -16,7 +17,6 @@
exports["default"] = function () {};
function foo() {}
exports["default"] = foo;
var Foo = function Foo() {};
exports["default"] = Foo;