remove isDynamic checks and always execute those expressions **once**

This commit is contained in:
Sebastian McKenzie 2015-01-03 21:48:06 +11:00
parent 5a622ac9c7
commit d945379b5b
24 changed files with 238 additions and 164 deletions

View File

@ -89,7 +89,7 @@ Class.prototype.run = function () {
this.closure = true;
// so we're only evaluating it once
var superRef = t.getUid(superName, this.file);
var superRef = this.scope.generateUidBasedOnNode(superName, this.file);
body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(superRef, superName)
]));

View File

@ -1,6 +1,6 @@
var t = require("../../types");
exports.ObjectExpression = function (node, parent, file) {
exports.ObjectExpression = function (node, parent, file, scope) {
var hasComputed = false;
var prop;
var key;
@ -13,7 +13,7 @@ exports.ObjectExpression = function (node, parent, file) {
if (!hasComputed) return;
var objId = t.getUid(parent, file);
var objId = scope.generateUidBasedOnNode(parent, file);
var body = [];
var container = t.functionExpression(null, [], t.blockStatement(body));

View File

@ -75,7 +75,7 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) {
var toArray = opts.file.toArray(parentId, !hasSpreadElement && pattern.elements.length);
var _parentId = t.getUid(parentId, opts.file, opts.scope);
var _parentId = opts.scope.generateUidBasedOnNode(parentId, opts.file);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(_parentId, toArray)
]));
@ -113,7 +113,7 @@ var pushPattern = function (opts) {
var scope = opts.scope;
if (!t.isMemberExpression(parentId) && !t.isIdentifier(parentId)) {
var key = t.getUid(parentId, file, scope);
var key = scope.generateUidBasedOnNode(parentId, file);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(key, parentId)

View File

@ -7,7 +7,7 @@ exports.Property = function (node, parent, file, scope) {
node.method = false;
var key = t.toComputedPropertyKey(node);
var key = t.toComputedKey(node, node.key);
if (!t.isLiteral(key)) return; // we can't set a function id with this
var id = t.toIdentifier(key.value);
@ -46,7 +46,7 @@ exports.Property = function (node, parent, file, scope) {
}
};
exports.ObjectExpression = function (node, parent, file) {
exports.ObjectExpression = function (node, parent, file, scope) {
var mutatorMap = {};
var hasAny = false;
@ -63,7 +63,7 @@ exports.ObjectExpression = function (node, parent, file) {
if (!hasAny) return;
if (node.properties.length) {
var objId = t.getUid(parent, file);
var objId = scope.generateUidBasedOnNode(parent, file);
return util.template("object-define-properties-closure", {
KEY: objId,

View File

@ -77,16 +77,13 @@ exports.CallExpression = function (node, parent, file, scope) {
}
var callee = node.callee;
var temp;
if (t.isMemberExpression(callee)) {
contextLiteral = callee.object;
if (t.isDynamic(contextLiteral)) {
temp = contextLiteral = scope.generateTemp(file);
var temp = scope.generateTempBasedOnNode(callee.object, file);
if (temp) {
callee.object = t.assignmentExpression("=", temp, callee.object);
contextLiteral = temp;
}
t.appendToMemberExpression(callee, t.identifier("apply"));
} else {
node.callee = t.memberExpression(node.callee, t.identifier("apply"));

View File

@ -30,10 +30,8 @@ exports.AssignmentExpression = function (node, parent, file, scope) {
// we need to return `node.right`
if (!t.isExpressionStatement(parent)) {
// `node.right` isn't a simple identifier so we need to reference it
if (t.isDynamic(value)) {
temp = value = scope.generateTemp(file);
}
temp = scope.generateTempBasedOnNode(node.right, file);
if (temp) value = temp;
}
if (node.operator !== "=") {
@ -80,11 +78,7 @@ exports.CallExpression = function (node, parent, file, scope) {
var callee = node.callee;
if (!t.isVirtualPropertyExpression(callee)) return;
var temp;
if (t.isDynamic(callee.object)) {
// we need to save `callee.object` so we can call it again
temp = scope.generateTemp(file);
}
var temp = scope.generateTempBasedOnNode(callee.object, file);
var call = util.template("abstract-expression-call", {
PROPERTY: callee.property,

View File

@ -5,7 +5,7 @@ var t = require("../../types");
exports.experimental = true;
var build = function (node, parent, file, scope) {
var uid = t.getUid(parent, file, scope);
var uid = scope.generateUidBasedOnNode(parent, file);
var container = util.template("array-comprehension-container", {
KEY: uid

View File

@ -10,7 +10,7 @@ var getPropRef = function (nodes, prop, file, scope) {
if (t.isIdentifier(prop)) {
return t.literal(prop.name);
} else {
var temp = t.getUid(prop, file, scope);
var temp = scope.generateUidBasedOnNode(prop, file);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(temp, prop)
]));
@ -19,15 +19,11 @@ var getPropRef = function (nodes, prop, file, scope) {
};
var getObjRef = function (nodes, obj, file, scope) {
if (t.isDynamic(obj)) {
var temp = t.getUid(obj, file, scope);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(temp, obj)
]));
return temp;
} else {
return obj;
}
var temp = scope.generateUidBasedOnNode(obj, file);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(temp, obj)
]));
return temp;
};
var buildHasOwn = function (obj, prop, file) {

View File

@ -5,10 +5,8 @@ exports.BindMemberExpression = function (node, parent, file, scope) {
var object = node.object;
var prop = node.property;
var temp;
if (t.isDynamic(object)) {
temp = object = scope.generateTemp(file);
}
var temp = scope.generateTempBasedOnNode(node.object, file);
if (temp) object = temp;
var call = t.callExpression(
t.memberExpression(t.memberExpression(object, prop), t.identifier("bind")),
@ -33,16 +31,12 @@ exports.BindFunctionExpression = function (node, parent, file, scope) {
]));
};
if (_.find(node.arguments, t.isDynamic)) {
var temp = scope.generateTemp(file, "args");
var temp = scope.generateTemp(file, "args");
return t.sequenceExpression([
t.assignmentExpression("=", temp, t.arrayExpression(node.arguments)),
buildCall(node.arguments.map(function (node, i) {
return t.memberExpression(temp, t.literal(i), true);
}))
]);
} else {
return buildCall(node.arguments);
}
return t.sequenceExpression([
t.assignmentExpression("=", temp, t.arrayExpression(node.arguments)),
buildCall(node.arguments.map(function (node, i) {
return t.memberExpression(temp, t.literal(i), true);
}))
]);
};

View File

@ -31,6 +31,13 @@ Scope.add = function (node, references) {
_.defaults(references, t.getIds(node, true));
};
/**
* Description
*
* @param {File} file
* @param {String} [name="temp"]
*/
Scope.prototype.generateTemp = function (file, name) {
var id = file.generateUidIdentifier(name || "temp", this);
this.push({
@ -40,6 +47,83 @@ Scope.prototype.generateTemp = function (file, name) {
return id;
};
/*
* Description
*
* @param {Object} parent
* @param {File} file
* @param {Scope} scope
* @returns {Object}
*/
Scope.prototype.generateUidBasedOnNode = function (parent, file) {
var node = parent;
if (t.isAssignmentExpression(parent)) {
node = parent.left;
} else if (t.isVariableDeclarator(parent)) {
node = parent.id;
}
var id = "ref";
if (t.isProperty(node)) {
node = node.key;
}
if (t.isIdentifier(node)) {
id = node.name;
} else if (t.isLiteral(node)) {
id = node.value;
} else if (t.isMemberExpression(node)) {
var parts = [];
var add = function (node) {
if (t.isMemberExpression(node)) {
add(node.object);
add(node.property);
} else if (t.isIdentifier(node)) {
parts.push(node.name);
} else if (t.isLiteral(node)) {
parts.push(node.value);
}
};
add(node);
id = parts.join("$");
}
id = id.replace(/^_/, "");
return file.generateUidIdentifier(id, this);
};
/**
* Description
*
* @param {Object} node
* @param {File} file
* @returns {Object}
*/
Scope.prototype.generateTempBasedOnNode = function (node, file) {
if (!t.isIdentifier(node) && !t.isMemberExpression(node)) {
throw new TypeError("Invalid node type " + JSON.stringify(node.type) + " passed to Scope.prototype.generateTempBasedOnNode");
}
if (t.isIdentifier(node) && this.has(node.name, true)) {
return null;
}
var id = this.generateUidBasedOnNode(node, file);
this.push({
key: id.name,
id: id
});
return id;
};
Scope.prototype.getInfo = function () {
var block = this.block;
if (block._scopeInfo) return block._scopeInfo;
@ -123,6 +207,12 @@ Scope.prototype.getInfo = function () {
return info;
};
/**
* Description
*
* @param {Object} opts
*/
Scope.prototype.push = function (opts) {
var block = this.block;
@ -143,33 +233,84 @@ Scope.prototype.push = function (opts) {
}
};
/**
* Description
*
* @param {Object} node
*/
Scope.prototype.add = function (node) {
Scope.add(node, this.references);
};
/**
* Description
*
* @param {String} [id]
* @param {Boolean} [decl]
*/
Scope.prototype.get = function (id, decl) {
return id && (this.getOwn(id, decl) || this.parentGet(id, decl));
};
/**
* Description
*
* @param {String} [id]
* @param {Boolean} [decl]
*/
Scope.prototype.getOwn = function (id, decl) {
var refs = this.references;
if (decl) refs = this.declarations;
return _.has(refs, id) && refs[id];
};
/**
* Description
*
* @param {String} [id]
* @param {Boolean} [decl]
*/
Scope.prototype.parentGet = function (id, decl) {
return this.parent && this.parent.get(id, decl);
};
/**
* Description
*
* @param {String} [id]
* @param {Boolean} [decl]
* @returns {Boolean}
*/
Scope.prototype.has = function (id, decl) {
return (id && (this.hasOwn(id, decl) || this.parentHas(id, decl))) ||
_.contains(Scope.defaultDeclarations, id);
};
/**
* Description
*
* @param {String} [id]
* @param {Boolean} [decl]
* @returns {Boolean}
*/
Scope.prototype.hasOwn = function (id, decl) {
return !!this.getOwn(id, decl);
};
/**
* Description
*
* @param {String} [id]
* @param {Boolean} [decl]
* @returns {Boolean}
*/
Scope.prototype.parentHas = function (id, decl) {
return this.parent && this.parent.has(id, decl);
};

View File

@ -71,9 +71,8 @@ _.each(t.FLIPPED_ALIAS_KEYS, function (types, type) {
* @returns {Object}
*/
t.toComputedPropertyKey = function (prop) {
var key = prop.key;
if (!prop.computed) {
t.toComputedKey = function (node, key) {
if (!node.computed) {
if (t.isIdentifier(key)) key = t.literal(key.name);
}
return key;
@ -126,6 +125,10 @@ t.toSequenceExpression = function (nodes, scope) {
}
});
if (exprs.length === 1) {
return exprs[0]
}
return t.sequenceExpression(exprs);
};
@ -174,25 +177,6 @@ t.prependToMemberExpression = function (member, append) {
return member;
};
/*
* Description
*
* @param {Object} node
* @returns {Boolean}
*/
t.isDynamic = function (node) {
if (t.isExpressionStatement(node)) {
return t.isDynamic(node.expression);
} else if (t.isIdentifier(node) || t.isLiteral(node) || t.isThisExpression(node)) {
return false;
} else if (t.isMemberExpression(node)) {
return t.isDynamic(node.object) || t.isDynamic(node.property);
} else {
return true;
}
};
/**
* Description
*
@ -341,58 +325,6 @@ t.toBlock = function (node, parent) {
return t.blockStatement(node);
};
/*
* Description
*
* @param {Object} parent
* @param {File} file
* @param {Scope} scope
* @returns {Object}
*/
t.getUid = function (parent, file, scope) {
var node = parent;
if (t.isAssignmentExpression(parent)) {
node = parent.left;
} else if (t.isVariableDeclarator(parent)) {
node = parent.id;
}
var id = "ref";
if (t.isProperty(node)) {
node = node.key;
}
if (t.isIdentifier(node)) {
id = node.name;
} else if (t.isLiteral(node)) {
id = node.value;
} else if (t.isMemberExpression(node)) {
var parts = [];
var add = function (node) {
if (t.isMemberExpression(node)) {
add(node.object);
add(node.property);
} else if (t.isIdentifier(node)) {
parts.push(node.name);
} else if (t.isLiteral(node)) {
parts.push(node.value);
}
};
add(node);
id = parts.join("$");
}
id = id.replace(/^_/, "");
return file.generateUidIdentifier(id, scope);
};
/**
* Description
*

View File

@ -16,30 +16,33 @@ var _inherits = function (child, parent) {
var Test = (function () {
var _Foo = Foo;
var Test = function Test() {
var _Foo$prototype$test, _Foo$prototype$test2;
woops["super"].test();
_Foo.call(this);
_Foo.prototype.test.call(this);
foob(_Foo);
_Foo.call.apply(_Foo, [this].concat(_slice.call(arguments)));
_Foo.call.apply(_Foo, [this, "test"].concat(_slice.call(arguments)));
_Foo.call.apply(null, [this].concat(_slice.call(arguments)));
_Foo.call.apply(null, [this, "test"].concat(_slice.call(arguments)));
_Foo.prototype.test.call.apply(_Foo.prototype.test, [this].concat(_slice.call(arguments)));
_Foo.prototype.test.call.apply(_Foo.prototype.test, [this, "test"].concat(_slice.call(arguments)));
(_Foo$prototype$test = _Foo.prototype.test).call.apply(_Foo$prototype$test, [this].concat(_slice.call(arguments)));
(_Foo$prototype$test2 = _Foo.prototype.test).call.apply(_Foo$prototype$test2, [this, "test"].concat(_slice.call(arguments)));
};
_inherits(Test, _Foo);
Test.prototype.test = function () {
var _Foo$prototype$test3, _Foo$prototype$test4;
_Foo.prototype.test.call(this);
_Foo.prototype.test.call.apply(_Foo.prototype.test, [this].concat(_slice.call(arguments)));
_Foo.prototype.test.call.apply(_Foo.prototype.test, [this, "test"].concat(_slice.call(arguments)));
(_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 () {
var _Foo$foo, _Foo$foo2;
_Foo.foo.call(this);
_Foo.foo.call.apply(_Foo.foo, [this].concat(_slice.call(arguments)));
_Foo.foo.call.apply(_Foo.foo, [this, "test"].concat(_slice.call(arguments)));
(_Foo$foo = _Foo.foo).call.apply(_Foo$foo, [this].concat(_slice.call(arguments)));
(_Foo$foo2 = _Foo.foo).call.apply(_Foo$foo2, [this, "test"].concat(_slice.call(arguments)));
};
return Test;

View File

@ -1,7 +1,8 @@
"use strict";
var _obj;
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
};
obj[method].apply(obj, [foo, bar].concat(_toArray(args)));
(_obj = obj)[method].apply(_obj, [foo, bar].concat(_toArray(args)));

View File

@ -1,7 +1,8 @@
"use strict";
var _obj;
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
};
obj[method].apply(obj, _toArray(args));
(_obj = obj)[method].apply(_obj, _toArray(args));

View File

@ -1,8 +1,9 @@
"use strict";
var _foob, _foob$test;
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
};
foob.add.apply(foob, [foo, bar].concat(_toArray(numbers)));
foob.test.add.apply(foob.test, [foo, bar].concat(_toArray(numbers)));
(_foob = foob).add.apply(_foob, [foo, bar].concat(_toArray(numbers)));
(_foob$test = foob.test).add.apply(_foob$test, [foo, bar].concat(_toArray(numbers)));

View File

@ -1,8 +1,9 @@
"use strict";
var _foob, _foob$test;
var _toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
};
foob.add.apply(foob, _toArray(numbers));
foob.test.add.apply(foob.test, _toArray(numbers));
(_foob = foob).add.apply(_foob, _toArray(numbers));
(_foob$test = foob.test).add.apply(_foob$test, _toArray(numbers));

View File

@ -1,2 +1,6 @@
foo::bar();
foo::bar("arg");
var test = "test";
test::bar();
test::bar("arg");

View File

@ -1,4 +1,9 @@
"use strict";
bar[Symbol.referenceGet](foo).call(foo);
bar[Symbol.referenceGet](foo).call(foo, "arg");
var _foo, _foo2;
_foo = foo, bar[Symbol.referenceGet](_foo).call(_foo);
_foo2 = foo, bar[Symbol.referenceGet](_foo2).call(_foo2, "arg");
var test = "test";
bar[Symbol.referenceGet](test).call(test);
bar[Symbol.referenceGet](test).call(test, "arg");

View File

@ -1,3 +1,3 @@
delete foo::bar;
if (delete foo::bar);
if (delete foo::bar) {}

View File

@ -2,4 +2,4 @@
bar[Symbol.referenceDelete](foo);
if ((bar[Symbol.referenceDelete](foo), true)) ;
if ((bar[Symbol.referenceDelete](foo), true)) {}

View File

@ -1,2 +1,3 @@
var baz = "foo";
foo::bar = baz;
if (foo::bar = baz);
if (foo::bar = baz) {}

View File

@ -1,4 +1,5 @@
"use strict";
var baz = "foo";
bar[Symbol.referenceSet](foo, baz);
if ((bar[Symbol.referenceSet](foo, baz), baz)) ;
if ((bar[Symbol.referenceSet](foo, baz), baz)) {}

View File

@ -1,25 +1,27 @@
"use strict";
var _ref2, _obj2, _ref4;
var _obj2, _obj4, _ref2, _obj6, _ref4;
var _hasOwn = Object.prototype.hasOwnProperty;
var obj = {};
if (!_hasOwn.call(obj, "x")) obj.x = 2;
var _obj = obj;
if (!_hasOwn.call(_obj, "x")) _obj.x = 2;
console.log((!_hasOwn.call(obj, "x") && (obj.x = 2), obj.x));
console.log((_obj2 = obj, !_hasOwn.call(_obj2, "x") && (_obj2.x = 2), _obj2.x));
var _obj3 = obj;
var _ref = x();
if (!_hasOwn.call(obj, _ref)) obj[_ref] = 2;
if (!_hasOwn.call(_obj3, _ref)) _obj3[_ref] = 2;
console.log((_ref2 = x(), !_hasOwn.call(obj, _ref2) && (obj[_ref2] = 2), obj[_ref2]));
console.log((_obj4 = obj, _ref2 = x(), !_hasOwn.call(_obj4, _ref2) && (_obj4[_ref2] = 2), _obj4[_ref2]));
var _obj = obj[y()];
var _obj5 = obj[y()];
var _ref3 = x();
if (!_hasOwn.call(_obj, _ref3)) _obj[_ref3] = 2;
if (!_hasOwn.call(_obj5, _ref3)) _obj5[_ref3] = 2;
console.log((_obj2 = obj[y()], _ref4 = x(), !_hasOwn.call(_obj2, _ref4) && (_obj2[_ref4] = 2), _obj2[_ref4]));
console.log((_obj6 = obj[y()], _ref4 = x(), !_hasOwn.call(_obj6, _ref4) && (_obj6[_ref4] = 2), _obj6[_ref4]));

View File

@ -1,22 +1,22 @@
"use strict";
var _temp, _args;
var fn = obj.method.bind(obj);
var fn = obj.method.bind(obj, "foob");
var fn = obj[foo].method.bind(obj[foo]);
var fn = obj.foo.method.bind(obj.foo);
var fn = (_temp = obj[foo()], _temp.method.bind(_temp));
var _obj, _obj2, _obj$foo, _obj$foo2, _obj3, _args, _args2, _args3;
var fn = (_obj = obj, _obj.method.bind(_obj));
var fn = (_obj2 = obj, _obj2.method.bind(_obj2, "foob"));
var fn = (_obj$foo = obj[foo], _obj$foo.method.bind(_obj$foo));
var fn = (_obj$foo2 = obj.foo, _obj$foo2.method.bind(_obj$foo2));
var fn = (_obj3 = obj[foo()], _obj3.method.bind(_obj3));
["foo", "bar"].map(function (_val) {
["foo", "bar"].map((_args = [], function (_val) {
return _val.toUpperCase();
});
[1.1234, 23.53245, 3].map(function (_val2) {
return _val2.toFixed(2);
});
}));
[1.1234, 23.53245, 3].map((_args2 = [2], function (_val2) {
return _val2.toFixed(_args2[0]);
}));
var get = function () {
return 2;
};
[1.1234, 23.53245, 3].map((_args = [get()], function (_val3) {
return _val3.toFixed(_args[0]);
[1.1234, 23.53245, 3].map((_args3 = [get()], function (_val3) {
return _val3.toFixed(_args3[0]);
}));