Code coverage report for 6to5/types.js

Statements: 91.11% (82 / 90)      Branches: 86.27% (88 / 102)      Functions: 91.67% (11 / 12)      Lines: 90.7% (78 / 86)      Ignored: none     

All files » 6to5/ » types.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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 2021 1 1   1 1 1 1   1           1 12544     1 12503     1 6139     1 592     1   159   155     155     155     155   14     1 56 56   19 19 19     19     1 96     1 27       27     1     12078 41     12037 243       243 2     241       241 63 63 63 63   63 9     54 2         12024 2         1     1 1               12022 7                         12015 2     12013 2     12011 218       218       218       218       218 14       11997 79     11918     1       1   1                       10 23        
var b = require("./builders");
var n = require("acorn-ast-types").namedTypes;
var _ = require("lodash");
 
exports.FUNCTION_TYPES = ["ArrowFunctionExpression", "FunctionDeclaration", "FunctionExpression"];
exports.PATTERN_TYPES  = ["ArrayPattern", "ObjectPattern"];
exports.BINARY_TYPES   = ["BinaryExpression", "LogicalExpression"];
exports.UNARY_TYPES    = ["UnaryExpression", "SpreadElement", "SpreadProperty"];
 
exports.aliases = {
  ArrowFunctionExpression: ["Function"],
  FunctionDeclaration:     ["Function"],
  FunctionExpression:      ["Function"]
};
 
exports.isUnaryLike = function (node) {
  return _.contains(exports.UNARY_TYPES, node.type);
};
 
exports.isBinary = function (node) {
  return _.contains(exports.BINARY_TYPES, node.type);
};
 
exports.isFunction = function (node) {
  return _.contains(exports.FUNCTION_TYPES, node.type);
};
 
exports.isPattern = function (node) {
  return _.contains(exports.PATTERN_TYPES, node.type);
};
 
exports.isReferenced = function (node, parent) {
  // we're a property key so we aren't referenced
  if (parent.type === "Property" && parent.key === node) return false;
 
  var isMemberExpression = parent.type === "MemberExpression";
 
  // we're in a member expression and we're the computed property so we're referenced
  var isComputedProperty = isMemberExpression && parent.property === node && parent.computed;
 
  // we're in a member expression and we're the object so we're referenced
  var isObject = isMemberExpression && parent.object === node;
 
  // we are referenced
  if (!isMemberExpression || isComputedProperty || isObject) return true;
 
  return false;
};
 
exports.ensureBlock = function (node) {
  var block = node.body;
  if (block.type === "BlockStatement") return;
 
  Eif (!_.isArray(block)) {
    Eif (!n.Statement.check(block)) block = b.returnStatement(block);
    block = [block];
  }
 
  node.body = b.blockStatement(block);
};
 
exports.getSpecifierName = function (specifier) {
  return specifier.name || specifier.id;
};
 
exports.ensureExpressionType = function (node) {
  node.type = {
    FunctionDeclaration: "FunctionExpression",
    ClassDeclaration: "ClassExpression"
  }[node.type] || node.type;
  return node;
};
 
exports.needsParans = function (node, parent) {
  //if (!n.Expression.check(node)) return false;
 
  if (exports.isUnaryLike(node)) {
    return parent.type === "MemberExpression" && parent.object === node;
  }
 
  if (exports.isBinary(node)) {
    Iif (parent.type === "CallExpression" && parent.callee === node) {
      return true;
    }
 
    if (exports.isUnaryLike(parent)) {
      return true;
    }
 
    Iif (parent.type === "MemberExpression" && parent.object === node) {
      return true;
    }
 
    if (exports.isBinary(parent)) {
      var po = parent.operator;
      var pp = PRECEDENCE[po];
      var no = node.operator;
      var np = PRECEDENCE[no];
 
      if (pp > np) {
        return true;
      }
 
      if (pp === np && parent.right === node) {
        return true;
      }
    }
  }
 
  if (node.type === "SequenceExpression") {
    if (parent.type === "ForStatement") {
      // 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;
    }
 
    Eif (parent.type === "ExpressionStatement" && parent.expression === node) {
      return false;
    }
 
    // Otherwise err on the side of overparenthesization, adding
    // explicit exceptions above if this proves overzealous.
    return true;
  }
 
  if (node.type === "YieldExpression") {
    return exports.isBinary(parent)
        || exports.isUnaryLike(parent)
        || parent.type === "CallExpression"
        || parent.type === "MemberExpression"
        || parent.type === "NewExpression"
        || parent.type === "ConditionalExpression"
        || parent.type === "YieldExpression";
  }
 
  //if (parent.type === "NewExpression" && parent.callee === node) {
  //  return containsCallExpression(node);
  //}
 
  if (node.type === "Literal" && _.isNumber(node.value) && parent.type === "MemberExpression" && parent.object === node) {
    return true;
  }
 
  if (node.type === "CallExpression" && parent.type === "NewExpression") {
    return true;
  }
 
  if (node.type === "AssignmentExpression" || node.type === "ConditionalExpression") {
    Iif (exports.isUnaryLike(parent)) {
      return true;
    }
 
    Iif (exports.isBinary(parent)) {
      return true;
    }
 
    Iif (parent.type === "CallExpression" && parent.callee === node) {
      return true;
    }
 
    Iif (parent.type === "ConditionalExpression" && parent.test === node) {
      return true;
    }
 
    if (parent.type === "MemberExpression" && parent.object === node) {
      return true;
    }
  }
 
  if (node.type === "FunctionExpression" && parent.type === "CallExpression" && parent.callee === node) {
    return true;
  }
 
  return false;
};
 
exports.canBeFirstInStatement = function (node) {
  return node.type !== "FunctionExpression" && node.type !== "ObjectExpression";
};
 
var PRECEDENCE = {};
 
_.each([
  ["||"],
  ["&&"],
  ["|"],
  ["^"],
  ["&"],
  ["==", "===", "!=", "!=="],
  ["<", ">", "<=", ">=", "in", "instanceof"],
  [">>", "<<", ">>>"],
  ["+", "-"],
  ["*", "/", "%"]
], function(tier, i) {
  _.each(tier, function(op) {
    PRECEDENCE[op] = i;
  });
});