Code coverage report for 6to5/lib/6to5/generation/node/parentheses.js

Statements: 88.24% (60 / 68)      Branches: 86.84% (66 / 76)      Functions: 100% (12 / 12)      Lines: 88.24% (60 / 68)      Ignored: none     

All files » 6to5/lib/6to5/generation/node/ » parentheses.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 1661 1   1   1                         11 24       1 58   1       1 358         358         358     1 795       795 2     793       793 239 239   239 239   239 40     199 4         1 580   24 4       20 3         1 32               32 6         26     1 13                 1 10     1 243     1   753 2       751 9       742 130       1   761       761 6     755       755       755 52     703    
var t = require("../../types");
var _ = require("lodash");
 
var PRECEDENCE = {};
 
_.each([
  ["||"],
  ["&&"],
  ["|"],
  ["^"],
  ["&"],
  ["==", "===", "!=", "!=="],
  ["<", ">", "<=", ">=", "in", "instanceof"],
  [">>", "<<", ">>>"],
  ["+", "-"],
  ["*", "/", "%"],
  ["**"]
], function (tier, i) {
  _.each(tier, function (op) {
    PRECEDENCE[op] = i;
  });
});
 
exports.UpdateExpression = function (node, parent) {
  if (t.isMemberExpression(parent) && parent.object === node) {
    // (foo++).test()
    return true;
  }
};
 
exports.ObjectExpression = function (node, parent) {
  Iif (t.isExpressionStatement(parent)) {
    // ({ foo: "bar" });
    return true;
  }
 
  Iif (t.isMemberExpression(parent) && parent.object === node) {
    // ({ foo: "bar" }).foo
    return true;
  }
 
  return false;
};
 
exports.Binary = function (node, parent) {
  Iif ((t.isCallExpression(parent) || t.isNewExpression(parent)) && parent.callee === node) {
    return true;
  }
 
  if (t.isUnaryLike(parent)) {
    return true;
  }
 
  Iif (t.isMemberExpression(parent) && parent.object === node) {
    return true;
  }
 
  if (t.isBinary(parent)) {
    var parentOp  = parent.operator;
    var parentPos = PRECEDENCE[parentOp];
 
    var nodeOp = node.operator;
    var nodePos = PRECEDENCE[nodeOp];
 
    if (parentPos > nodePos) {
      return true;
    }
 
    if (parentPos === nodePos && parent.right === node) {
      return true;
    }
  }
};
 
exports.BinaryExpression = function (node, parent) {
  if (node.operator === "in") {
    // var i = (1 in []);
    if (t.isVariableDeclarator(parent)) {
      return true;
    }
 
    // for ((1 in []);;);
    if (t.isFor(parent)) {
      return true;
    }
  }
};
 
exports.SequenceExpression = function (node, parent) {
  Iif (t.isForStatement(parent)) {
    // Although parentheses wouldn't hurt around sequence
    // expressions in the head of for loops, traditional style
    // dictates that e.g. i++, j++ should not be wrapped with
    // parentheses.
    return false;
  }
 
  if (t.isExpressionStatement(parent) && parent.expression === node) {
    return false;
  }
 
  // Otherwise err on the side of overparenthesization, adding
  // explicit exceptions above if this proves overzealous.
  return true;
};
 
exports.YieldExpression = function (node, parent) {
  return t.isBinary(parent) ||
         t.isUnaryLike(parent) ||
         t.isCallExpression(parent) ||
         t.isMemberExpression(parent) ||
         t.isNewExpression(parent) ||
         t.isConditionalExpression(parent) ||
         t.isYieldExpression(parent);
};
 
exports.ClassExpression = function (node, parent) {
  return t.isExpressionStatement(parent);
};
 
exports.UnaryLike = function (node, parent) {
  return t.isMemberExpression(parent) && parent.object === node;
};
 
exports.FunctionExpression = function (node, parent) {
  // function () {};
  if (t.isExpressionStatement(parent)) {
    return true;
  }
 
  // (function test() {}).name;
  if (t.isMemberExpression(parent) && parent.object === node) {
    return true;
  }
 
  // (function () {})();
  if (t.isCallExpression(parent) && parent.callee === node) {
    return true;
  }
};
 
exports.AssignmentExpression =
exports.ConditionalExpression = function (node, parent) {
  Iif (t.isUnaryLike(parent)) {
    return true;
  }
 
  if (t.isBinary(parent)) {
    return true;
  }
 
  Iif (t.isCallExpression(parent) && parent.callee === node) {
    return true;
  }
 
  Iif (t.isConditionalExpression(parent) && parent.test === node) {
    return true;
  }
 
  if (t.isMemberExpression(parent) && parent.object === node) {
    return true;
  }
 
  return false;
};