add precedence to generated node generation

This commit is contained in:
Sebastian McKenzie 2014-11-08 12:00:26 +11:00
parent d0a33ab933
commit 4cb90cb1e0

View File

@ -8,6 +8,8 @@ function Node(node, parent) {
this.node = node;
}
//
Node.whitespace = {
FunctionExpression: 1,
FunctionStatement: 1,
@ -27,6 +29,29 @@ _.each(Node.whitespace, function (amounts, type) {
Node.whitespace[type] = amounts;
});
//
Node.PRECEDENCE = {};
_.each([
["||"],
["&&"],
["|"],
["^"],
["&"],
["==", "===", "!=", "!=="],
["<", ">", "<=", ">=", "in", "instanceof"],
[">>", "<<", ">>>"],
["+", "-"],
["*", "/", "%"]
], function (tier, i) {
_.each(tier, function (op) {
Node.PRECEDENCE[op] = i;
});
});
//
Node.prototype.needsWhitespace = function (type) {
var node = this.node;
if (!node) return 0;
@ -109,10 +134,10 @@ Node.prototype.needsParans = function () {
if (t.isBinary(parent)) {
var parentOp = parent.operator;
var parentPos = t.PRECEDENCE[parentOp];
var parentPos = Node.PRECEDENCE[parentOp];
var nodeOp = node.operator;
var nodePos = t.PRECEDENCE[nodeOp];
var nodePos = Node.PRECEDENCE[nodeOp];
if (parentPos > nodePos) {
return true;