remove block binding because the current implementation is flaky and will have to be rewritten from scratch without the issue of wrapping functions

This commit is contained in:
Sebastian McKenzie 2014-10-12 16:18:18 +11:00
parent 81924aee09
commit 52d4d73f0b
43 changed files with 54 additions and 453 deletions

View File

@ -49,7 +49,6 @@ It's as easy as:
- [Array comprehension](FEATURES.md#array-comprehension)
- [Arrow functions](FEATURES.md#arrow-functions)
- [Block binding](FEATURES.md#block-binding)
- [Classes](FEATURES.md#classes)
- [Computed property names](FEATURES.md#computed-property-names)
- [Constants](FEATURES.md#constants)
@ -67,6 +66,7 @@ It's as easy as:
To be implemented:
- [Block binding](FEATURES.md#block-binding)
- [Generators](FEATURES.md#generators)
## Usage
@ -340,7 +340,7 @@ better suited if you'd like a full ES6 environment with polyfills and all.
| Source maps | ✓ | ✓ | ✓ | | ✓ | ✓ |
| No compiler global pollution | ✓ | | ✓ | | ✓ | ✓ |
| Arrow functions | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Block binding | | ✓ | | | ✓ | |
| Block binding | | ✓ | | | ✓ | |
| Classes | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Computed property names | ✓ | ✓ | ✓ | ✓ | ✓ | |
| Constants | ✓ | ✓ | | | ✓ | |

View File

@ -1,145 +0,0 @@
var traverse = require("../traverse");
var util = require("../util");
var b = require("ast-types").builders;
var _ = require("lodash");
var blockTypes = traverse.FUNCTION_TYPES.concat(["BlockStatement"]);
var isLet = function (node) {
if (node && node.type === "VariableDeclaration" && node.kind === "let") {
node.kind = "var";
node._ignoreBlockBindingHoist = true;
return true;
}
};
var hasLet = function (nodes) {
var has = false;
_.each(nodes, function (node) {
if (isLet(node)) has = true;
});
return has;
};
exports.Program = function (node, parent, opts, generateUid) {
if (hasLet(node.body)) node.body = buildNode(node.body, generateUid).node;
};
exports.BlockStatement = function (node, parent, opts, generateUid) {
if (!hasLet(node.body)) return;
// ignore if we're the body of a closure already
if (_.contains(traverse.FUNCTION_TYPES, parent.type)) return;
var body = node.body;
var built = buildNode(node.body, generateUid);
node.body = built.node;
traverse(built.body, function (node) {
if (node.type === "ContinueStatement") {
return b.returnStatement(null);
}
}, blockTypes);
if (traverse.hasType(body, "BreakStatement", blockTypes)) {
var id = b.identifier(generateUid("break"));
node.body.unshift(b.variableDeclaration("var", [
b.variableDeclarator(id, b.literal(false))
]));
traverse(built.body, function (node) {
if (node.type === "BreakStatement") {
return b.returnStatement(b.assignmentExpression("=", id, b.literal(true)));
}
}, blockTypes);
node.body.push(b.ifStatement(id, b.breakStatement()));
}
};
var buildForStatement = function (key) {
return function (node, parent, opts, generateUid) {
if (isLet(node[key])) {
if (parent.type === "LabeledStatement") {
throw util.errorWithNode(parent, "Label statements not supported with block binding yet.");
}
return buildNode(node, generateUid).node;
}
};
};
exports.ForOfStatement = exports.ForInStatement = buildForStatement("left");
exports.ForStatement = buildForStatement("init");
var buildNode = function (node, generateUid) {
var nodes = [];
// hoist normal variable declarations
node = [].concat(node);
node = node.map(function (node) {
if (node._ignoreBlockBindingHoist) return node;
if (node.type === "VariableDeclaration" && node.kind === "var") {
var declars = node.declarations.map(function (declar) {
return b.variableDeclarator(declar.id, null);
});
nodes.push(b.variableDeclaration("var", declars));
return _.compact(node.declarations.map(function (declar) {
if (!declar.init) return;
return util.template("assign", {
VALUE: declar.init,
KEY: declar.id
}, true);
}));
} else if (node.type === "ForInStatement" && node.left.type === "VariableDeclaration" && !node.left._ignoreBlockBindingHoist) {
var id = node.left.declarations[0].id;
node.left = id;
nodes.push(util.template("variable-declare", {
KEY: id
}));
}
return node;
});
//
var argumentsId = util.aliasArguments(generateUid, node);
if (argumentsId) {
nodes.push(b.variableDeclaration("var", [
b.variableDeclarator(argumentsId, b.identifier("arguments"))
]));
}
//
var block = b.blockStatement([]);
block.body = node;
var func = b.functionExpression(null, [], block, false);
//
var templateName = "function-call";
if (traverse.hasType(node, "ThisExpression")) templateName += "-this";
if (traverse.hasType(node, "ReturnStatement", traverse.FUNCTION_TYPES)) templateName += "-return";
nodes.push(util.template(templateName, {
FUNCTION: func
}, true));
return {
node: nodes,
body: block
};
};

View File

@ -1,2 +1 @@
let MULTIPLER = 5;
arr.map(x => x * MULTIPLIER);

View File

@ -1,4 +1 @@
(function() {
var MULTIPLER = 5;
arr.map(x => x * MULTIPLIER);
})();
arr.map(x => x * MULTIPLIER);

View File

@ -1,6 +0,0 @@
(function () {
if (true) {
let a = arguments[0];
console.log(a);
}
})(1);

View File

@ -1,9 +0,0 @@
(function () {
if (true) {
var _arguments = arguments;
(function () {
var a = _arguments[0];
console.log(a);
})();
}
})(1);

View File

@ -1,5 +0,0 @@
{
let val = 1;
let multiplier = 5;
console.log(val * multiplier);
}

View File

@ -1,7 +0,0 @@
{
(function () {
var val = 1;
var multiplier = 5;
console.log(val * multiplier);
}());
}

View File

@ -1,4 +0,0 @@
{
let val = 1;
console.log(val * 2);
}

View File

@ -1,6 +0,0 @@
{
(function () {
var val = 1;
console.log(val * 2);
}());
}

View File

@ -1,4 +0,0 @@
(function () {
let i = 5;
console.log(i);
}());

View File

@ -1,4 +0,0 @@
(function () {
var i = 5;
console.log(i);
}());

View File

@ -1,10 +0,0 @@
for (var i in arr) {
let val = arr[i];
for (i in arr) {
let val2 = arr[i];
break;
}
break;
}

View File

@ -1,16 +0,0 @@
for (var i in arr) {
var _break = false;
(function () {
var val = arr[i];
for (i in arr) {
var _break2 = false;
(function () {
var val2 = arr[i];
return _break2 = true;
}());
if (_break2) break;
}
return _break = true;
}());
if (_break) break;
}

View File

@ -1,5 +0,0 @@
for (let i in arr) {
let val = arr[i];
console.log(val * 2);
break;
}

View File

@ -1,12 +0,0 @@
(function () {
for (var i in arr) {
var _break = false;
(function () {
var val = arr[i];
console.log(val * 2);
return _break = true;
}());
if (_break) break;
}
}());

View File

@ -1,5 +0,0 @@
for (let i in arr) {
let val = arr[i];
console.log(val * 2);
continue;
}

View File

@ -1,9 +0,0 @@
(function () {
for (var i in arr) {
(function () {
var val = arr[i];
console.log(val * 2);
return;
}());
}
}());

View File

@ -1,9 +0,0 @@
for (var i in arr) {
let val = arr[i];
console.log(val * 2);
for (i in arr) {
let x = arr[i];
console.log(x * 2);
}
}

View File

@ -1,12 +0,0 @@
for (var i in arr) {
(function () {
var val = arr[i];
console.log(val * 2);
for (i in arr) {
(function () {
var x = arr[i];
console.log(x * 2);
}());
}
}());
}

View File

@ -1,4 +0,0 @@
for (let i in arr) {
let val = arr[i];
console.log(val * 2);
}

View File

@ -1,8 +0,0 @@
(function () {
for (var i in arr) {
(function () {
var val = arr[i];
console.log(val * 2);
}());
}
}());

View File

@ -1,3 +0,0 @@
for (let i = 0; i < 9; i++) {
console.log(i);
}

View File

@ -1,5 +0,0 @@
(function () {
for (var i = 0; i < 9; i++) {
console.log(i);
}
}());

View File

@ -1,2 +0,0 @@
let MULTIPLIER = 5;
var foo = "bar", bar = "foo";

View File

@ -1,6 +0,0 @@
var foo, bar;
(function () {
var MULTIPLIER = 5;
foo = "bar";
bar = "foo";
})();

View File

@ -1,2 +0,0 @@
let MULTIPLIER = 5;
var foo = "bar";

View File

@ -1,5 +0,0 @@
var foo;
(function () {
var MULTIPLIER = 5;
foo = "bar";
})();

View File

@ -1,4 +0,0 @@
test:
for (let i in test) {
}

View File

@ -1,3 +0,0 @@
{
"throws": "Label statements not supported with block binding yet."
}

View File

@ -1 +0,0 @@
let arr = [1, 2, 3];

View File

@ -1,7 +0,0 @@
(function () {
var arr = [
1,
2,
3
];
})();

View File

@ -1,6 +0,0 @@
let _message = message();
console.log( _message );
function message () {
return 'hello';
}

View File

@ -1,8 +0,0 @@
(function () {
var _message = message();
console.log(_message);
function message () {
return 'hello';
}
})();

View File

@ -1,16 +0,0 @@
function student () {
let isStudent = true;
return Object.freeze({
isStudent
});
}
function student () {
let isStudent = true;
while (true) {
let test = "foo";
return Object.freeze({
isStudent
});
}
}

View File

@ -1,14 +0,0 @@
function student() {
var isStudent = true;
return Object.freeze({ isStudent: isStudent });
}
function student() {
var isStudent = true;
while (true) {
return function () {
var test = 'foo';
return Object.freeze({ isStudent: isStudent });
}();
}
}

View File

@ -1,5 +0,0 @@
var arr = [1, 2, 3];
for (let i in arr) {
let val = arr[i];
console.log(val * this.multiplier);
}

View File

@ -1,13 +0,0 @@
var arr = [
1,
2,
3
];
(function () {
for (var i in arr) {
(function () {
var val = arr[i];
console.log(val * this.multiplier);
}.call(this));
}
}.call(this));

View File

@ -1,8 +1,4 @@
(function () {
for (var i in arr) {
(function () {
var MULTIPLIER = 5;
console.log(arr[i] * MULTIPLIER);
}());
}
}());
for (let i in arr) {
let MULTIPLIER = 5;
console.log(arr[i] * MULTIPLIER);
}

View File

@ -1,23 +1,21 @@
(function () {
var _ref = [1, 2];
var a = _ref[0];
var b = _ref[1];
var _ref = [1, 2];
let a = _ref[0];
let b = _ref[1];
var _ref2 = [3, 4];
var c = _ref2[0];
var d = _ref2[1];
var _ref2 = [3, 4];
let c = _ref2[0];
let d = _ref2[1];
var _ref3 = {
e: 5,
f: 6
};
var e = _ref3.e;
var f = _ref3.f;
var _ref3 = {
e: 5,
f: 6
};
let e = _ref3.e;
let f = _ref3.f;
var _ref4 = {
a: 7,
b: 8
};
var g = _ref4.a;
var h = _ref4.b;
})();
var _ref4 = {
a: 7,
b: 8
};
let g = _ref4.a;
let h = _ref4.b;

View File

@ -1,7 +1,5 @@
var i;
(function () {
var MULTIPLIER = 5;
for (i in arr) {
console.log(arr[i] * MULTIPLIER);
}
}());
let MULTIPLIER = 5;
for (var i in arr) {
console.log(arr[i] * MULTIPLIER);
}

View File

@ -1,5 +1,3 @@
(function () {
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
var i = _step.value;
}
}());
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
let i = _step.value;
}

View File

@ -1,32 +1,27 @@
var foo;
var foo2;
exports.foo7 = foo7;
var foo = 1;
exports.foo = foo;
var foo2 = function () {};
exports.foo2 = foo2;
var foo3;
var foo8;
(function () {
exports.foo7 = foo7;
exports.foo3 = foo3;
foo = 1;
exports.foo = foo;
let foo4 = 2;
exports.foo4 = foo4;
foo2 = function () {};
exports.foo2 = foo2;
let foo5;
exports.foo5 = foo5;
exports.foo3 = foo3;
let foo6 = 3;
exports.foo6 = foo6;
var foo4 = 2;
exports.foo4 = foo4;
function foo7 () {}
var foo5;
exports.foo5 = foo5;
var foo6 = 3;
exports.foo6 = foo6;
function foo7 () {}
foo8 = function () {
var foo8 = function foo8() {};
return foo8;
}();
exports.foo8 = foo8;
}());
var foo8 = function () {
var foo8 = function foo8() {};
return foo8;
}();
exports.foo8 = foo8;