Code coverage report for 6to5/types/index.js

Statements: 95.68% (133 / 139)      Branches: 94.12% (80 / 85)      Functions: 96.43% (27 / 28)      Lines: 95.42% (125 / 131)      Ignored: none     

All files » 6to5/types/ » index.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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 2701   1   1 85 3854 3854               1   1 74 453170     74         1   1 75 10526 10526 10526 18248   10526           1   1   1 37 54 54       1 10   10 681138     10         1 2050     1       1 98311   98311 3593 825 679         98311         1   7571     7271   6732     6732     6732     6732   1840     1 300     299     299     299 139     299     1 88     1 40 23     17 17   17 6 6 11 6 6     17 2     17 7 7           10   10     1 88 67     21 21 19 19           21     21     1 5837   5837 5837   5837 7911 7911 7910   7886 7886   7886 5776 2110 1069 1041 1022       5837 5837     1                         1               1 3871     1 196     1           1 177 177 177     1 113 113     1 55 55 55 55 55 55     1 141    
var _ = require("lodash");
 
var t = exports;
 
var addAssert = function (type, is) {
  t["assert" + type] = function (node, opts) {
    opts = opts || {};
    Iif (!is(node, opts)) {
      throw new Error("Expected type " + JSON.stringify(type) + " with option " + JSON.stringify(opts));
    }
  };
};
 
//
 
t.VISITOR_KEYS = require("./visitor-keys");
 
_.each(t.VISITOR_KEYS, function (keys, type) {
  var is = t["is" + type] = function (node, opts) {
    return node && node.type === type && t.shallowEqual(node, opts);
  };
 
  addAssert(type, is);
});
 
//
 
t.BUILDER_KEYS = _.defaults(require("./builder-keys"), t.VISITOR_KEYS);
 
_.each(t.BUILDER_KEYS, function (keys, type) {
  t[type[0].toLowerCase() + type.slice(1)] = function () {
    var args = arguments;
    var node = { type: type };
    _.each(keys, function (key, i) {
      node[key] = args[i];
    });
    return node;
  };
});
 
//
 
t.ALIAS_KEYS = require("./alias-keys");
 
var _aliases = {};
 
_.each(t.ALIAS_KEYS, function (aliases, type) {
  _.each(aliases, function (alias) {
    var types = _aliases[alias] = _aliases[alias] || [];
    types.push(type);
  });
});
 
_.each(_aliases, function (types, type) {
  t[type.toUpperCase() + "_TYPES"] = types;
 
  var is = t["is" + type] = function (node, opts) {
    return node && _.contains(types, node.type) && t.shallowEqual(node, opts);
  };
 
  addAssert(type, is);
});
 
//
 
t.isExpression = function (node) {
  return !t.isStatement(node);
};
 
addAssert("Expression", t.isExpression);
 
//
 
t.shallowEqual = function (actual, expected) {
  var same = true;
 
  if (expected) {
    _.each(expected, function (val, key) {
      if (actual[key] !== val) {
        return same = false;
      }
    });
  }
 
  return same;
};
 
//
 
t.isReferenced = function (node, parent) {
  // we're a property key so we aren't referenced
  if (t.isProperty(parent) && parent.key === node) return false;
 
  // we're a variable declarator id so we aren't referenced
  if (t.isVariableDeclarator(parent) && parent.id === node) return false;
 
  var isMemberExpression = t.isMemberExpression(parent);
 
  // 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;
};
 
t.toIdentifier = function (name) {
  if (t.isIdentifier(name)) return name.name;
 
  // replace all non-valid identifiers with dashes
  name = name.replace(/[^a-zA-Z0-9]/g, "-");
 
  // remove all dashes and numbers from start of name
  name = name.replace(/^[-0-9]+/, "");
 
  // camel case
  name = name.replace(/[-_\s]+(.)?/g, function (match, c) {
    return c ? c.toUpperCase() : "";
  });
 
  return name;
};
 
t.ensureBlock = function (node) {
  node.body = t.toBlock(node.body, node);
};
 
t.toStatement = function (node, ignore) {
  if (t.isStatement(node)) {
    return node;
  }
 
  var mustHaveId = false;
  var newType;
 
  if (t.isClass(node)) {
    mustHaveId = true;
    newType = "ClassDeclaration";
  } else if (t.isFunction(node)) {
    mustHaveId = true;
    newType = "FunctionDeclaration";
  }
 
  if (mustHaveId && !node.id) {
    newType = false;
  }
 
  if (!newType) {
    Eif (ignore) {
      return false;
    } else {
      throw new Error("cannot turn " + node.type + " to a statement");
    }
  }
 
  node.type = newType;
 
  return node;
};
 
t.toBlock = function (node, parent) {
  if (t.isBlockStatement(node)) {
    return node;
  }
 
  Eif (!_.isArray(node)) {
    if (!t.isStatement(node)) {
      Eif (t.isFunction(parent)) {
        node = t.returnStatement(node);
      } else {
        node = t.expressionStatement(node);
      }
    }
 
    node = [node];
  }
 
  return t.blockStatement(node);
};
 
t.getIds = function (node, map, ignoreTypes) {
  ignoreTypes = ignoreTypes || [];
 
  var search = [].concat(node);
  var ids    = {};
 
  while (search.length) {
    var id = search.shift();
    if (!id) continue;
    if (_.contains(ignoreTypes, id.type)) continue;
 
    var nodeKey = t.getIds.nodes[id.type];
    var arrKey  = t.getIds.arrays[id.type];
 
    if (t.isIdentifier(id)) {
      ids[id.name] = id;
    } else if (nodeKey) {
      Eif (id[nodeKey]) search.push(id[nodeKey]);
    } else if (arrKey) {
      search = search.concat(id[arrKey] || []);
    }
  }
 
  if (!map) ids = _.keys(ids);
  return ids;
};
 
t.getIds.nodes = {
  AssignmentExpression: "left",
  ImportSpecifier: "id",
  ExportSpecifier: "id",
  VariableDeclarator: "id",
  FunctionDeclaration: "id",
  ClassDeclaration: "id",
  ParenthesizedExpression: "expression",
  MemeberExpression: "object",
  SpreadElement: "argument",
  Property: "value"
};
 
t.getIds.arrays = {
  ExportDeclaration: "specifiers",
  ImportDeclaration: "specifiers",
  VariableDeclaration: "declarations",
  ArrayPattern: "elements",
  ObjectPattern: "properties"
};
 
t.isLet = function (node) {
  return t.isVariableDeclaration(node) && (node.kind !== "var" || node._let);
};
 
t.isVar = function (node) {
  return t.isVariableDeclaration(node, { kind: "var" }) && !node._let;
};
 
t.removeComments = function (child) {
  delete child.leadingComments;
  delete child.trailingComments;
  return child;
};
 
t.inheritsComments = function (child, parent) {
  child.leadingComments  = _.compact([].concat(child.leadingComments, parent.leadingComments));
  child.trailingComments = _.compact([].concat(child.trailingComments, parent.trailingComments));
  return child;
};
 
t.removeComments = function (node) {
  delete node.leadingComments;
  delete node.trailingComments;
};
 
t.inherits = function (child, parent) {
  child.loc   = parent.loc;
  child.end   = parent.end;
  child.range = parent.range;
  child.start = parent.start;
  t.inheritsComments(child, parent);
  return child;
};
 
t.getSpecifierName = function (specifier) {
  return specifier.name || specifier.id;
};