Code coverage report for 6to5/lib/6to5/transformation/transformers/es6-destructuring.js

Statements: 88.76% (158 / 178)      Branches: 82.72% (67 / 81)      Functions: 92.31% (12 / 13)      Lines: 89.44% (144 / 161)      Ignored: none     

All files » 6to5/lib/6to5/transformation/transformers/ » es6-destructuring.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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336    1   1 67 67   67 5   62           1 81 16 65 23 42     42       1                                   1 16 30 30   3 3 5   5 2   2 2 2   2   3   3 3   27   27 27   27 6   21           1 23   23   23 23 49 2 2       23   23 23     23   23 49 49   48   48   48 2   2 1     2   46     48       1 22 22 22 22 22   22 3   3       3     22     1   867 867   651 651   2 2       2   2           2   2 2     1 15481   15481   15481 6972   7 7   7                 7     15481   6   6 6     1 2764 2764                               1 62489 62489   17459   2   2 2       2           2     1 18267 806   1 1 1         1 1   1           1   1     1 23211   22560 22560 22560   22560 22560 23191 23191 15 15     22560   15 16   16 16 16                 16 15   15     1     1       15                                 15    
// TODO: Clean up
 
var t = require("../../types");
 
var buildVariableAssign = function (opts, id, init) {
  var op = opts.operator;
  if (t.isMemberExpression(id)) op = "=";
 
  if (op) {
    return t.expressionStatement(t.assignmentExpression("=", id, init));
  } else {
    return t.variableDeclaration(opts.kind, [
      t.variableDeclarator(id, init)
    ]);
  }
};
 
var push = function (opts, nodes, elem, parentId) {
  if (t.isObjectPattern(elem)) {
    pushObjectPattern(opts, nodes, elem, parentId);
  } else if (t.isArrayPattern(elem)) {
    pushArrayPattern(opts, nodes, elem, parentId);
  } else Iif (t.isAssignmentPattern(elem)) {
    pushAssignmentPattern(opts, nodes, elem, parentId);
  } else {
    nodes.push(buildVariableAssign(opts, elem, parentId));
  }
};
 
var pushAssignmentPattern = function (opts, nodes, pattern, parentId) {
  var tempParentId = opts.scope.generateUidBasedOnNode(parentId, opts.file);
 
  nodes.push(t.variableDeclaration("var", [
    t.variableDeclarator(tempParentId, parentId)
  ]));
 
  nodes.push(buildVariableAssign(
    opts,
    pattern.left,
    t.conditionalExpression(
      t.binaryExpression("===", tempParentId, t.identifier("undefined")),
      pattern.right,
      tempParentId
    )
  ));
};
 
var pushObjectPattern = function (opts, nodes, pattern, parentId) {
  for (var i in pattern.properties) {
    var prop = pattern.properties[i];
    if (t.isSpreadProperty(prop)) {
        // get all the keys that appear in this object before the current spread
        var keys = [];
        for (var i2 in pattern.properties) {
          var prop2 = pattern.properties[i2];
 
          if (i2 >= i) break;
          Iif (t.isSpreadProperty(prop2)) continue;
 
          var key = prop2.key;
          Eif (t.isIdentifier(key)) {
            key = t.literal(prop2.key.name);
          }
          keys.push(key);
        }
        keys = t.arrayExpression(keys);
 
        var value = t.callExpression(opts.file.addHelper("object-without-properties"), [parentId, keys]);
        nodes.push(buildVariableAssign(opts, prop.argument, value));
    } else {
      Iif (t.isLiteral(prop.key)) prop.computed = true;
 
      var pattern2   = prop.value;
      var patternId2 = t.memberExpression(parentId, prop.key, prop.computed);
 
      if (t.isPattern(pattern2)) {
        push(opts, nodes, pattern2, patternId2);
      } else {
        nodes.push(buildVariableAssign(opts, pattern2, patternId2));
      }
    }
  }
};
 
var pushArrayPattern = function (opts, nodes, pattern, parentId) {
  Iif (!pattern.elements) return;
 
  var i;
 
  var hasSpreadElement = false;
  for (i in pattern.elements) {
    if (t.isSpreadElement(pattern.elements[i])) {
      hasSpreadElement = true;
      break;
    }
  }
 
  var toArray = opts.file.toArray(parentId, !hasSpreadElement && pattern.elements.length);
 
  var _parentId = opts.scope.generateUidBasedOnNode(parentId, opts.file);
  nodes.push(t.variableDeclaration("var", [
    t.variableDeclarator(_parentId, toArray)
  ]));
  parentId = _parentId;
 
  for (i in pattern.elements) {
    var elem = pattern.elements[i];
    if (!elem) continue;
 
    i = +i;
 
    var newPatternId;
 
    if (t.isSpreadElement(elem)) {
      newPatternId = opts.file.toArray(parentId);
 
      if (i > 0) {
        newPatternId = t.callExpression(t.memberExpression(newPatternId, t.identifier("slice")), [t.literal(i)]);
      }
 
      elem = elem.argument;
    } else {
      newPatternId = t.memberExpression(parentId, t.literal(i), true);
    }
 
    push(opts, nodes, elem, newPatternId);
  }
};
 
var pushPattern = function (opts) {
  var nodes = opts.nodes;
  var pattern = opts.pattern;
  var parentId = opts.id;
  var file = opts.file;
  var scope = opts.scope;
 
  if (!t.isArrayExpression(parentId) && !t.isMemberExpression(parentId) && !t.isIdentifier(parentId)) {
    var key = scope.generateUidBasedOnNode(parentId, file);
 
    nodes.push(t.variableDeclaration("var", [
      t.variableDeclarator(key, parentId)
    ]));
 
    parentId = key;
  }
 
  push(opts, nodes, pattern, parentId);
};
 
exports.ForInStatement =
exports.ForOfStatement = function (node, parent, file, scope) {
  var declar = node.left;
  if (!t.isVariableDeclaration(declar)) return;
 
  var pattern = declar.declarations[0].id;
  if (!t.isPattern(pattern)) return;
 
  var key = file.generateUidIdentifier("ref", scope);
  node.left = t.variableDeclaration(declar.kind, [
    t.variableDeclarator(key, null)
  ]);
 
  var nodes = [];
 
  push({
    kind: declar.kind,
    file: file,
    scope: scope
  }, nodes, pattern, key);
 
  t.ensureBlock(node);
 
  var block = node.body;
  block.body = nodes.concat(block.body);
};
 
exports.Function = function (node, parent, file, scope) {
  var nodes = [];
 
  var hasDestructuring = false;
 
  node.params = node.params.map(function (pattern) {
    if (!t.isPattern(pattern)) return pattern;
 
    hasDestructuring = true;
    var parentId = file.generateUidIdentifier("ref", scope);
 
    pushPattern({
      kind:    "var",
      nodes:   nodes,
      pattern: pattern,
      id:      parentId,
      file:    file,
      scope:   scope
    });
 
    return parentId;
  });
 
  if (!hasDestructuring) return;
 
  t.ensureBlock(node);
 
  var block = node.body;
  block.body = nodes.concat(block.body);
};
 
exports.CatchClause = function (node, parent, file, scope) {
  var pattern = node.param;
  Eif (!t.isPattern(pattern)) return;
 
  var ref = file.generateUidIdentifier("ref", scope);
  node.param = ref;
 
  var nodes = [];
 
  push({
    kind: "var",
    file: file,
    scope: scope
  }, nodes, pattern, ref);
 
  node.body.body = nodes.concat(node.body.body);
};
 
exports.ExpressionStatement = function (node, parent, file, scope) {
  var expr = node.expression;
  if (expr.type !== "AssignmentExpression") return;
 
  if (!t.isPattern(expr.left)) return;
 
  var nodes = [];
 
  var ref = file.generateUidIdentifier("ref", scope);
  nodes.push(t.variableDeclaration("var", [
    t.variableDeclarator(ref, expr.right)
  ]));
 
  push({
    operator: expr.operator,
    file: file,
    scope: scope
  }, nodes, expr.left, ref);
 
  return nodes;
};
 
exports.AssignmentExpression = function (node, parent, file, scope) {
  if (parent.type === "ExpressionStatement") return;
  if (!t.isPattern(node.left)) return;
 
  var tempName = file.generateUid("temp", scope);
  var ref = t.identifier(tempName);
  scope.push({
    key: tempName,
    id: ref
  });
 
  var nodes = [];
  nodes.push(t.assignmentExpression("=", ref, node.right));
 
  push({
    operator: node.operator,
    file: file,
    scope: scope
  }, nodes, node.left, ref);
 
  nodes.push(ref);
 
  return t.toSequenceExpression(nodes, scope);
};
 
exports.VariableDeclaration = function (node, parent, file, scope) {
  if (t.isForInStatement(parent) || t.isForOfStatement(parent)) return;
 
  var nodes = [];
  var i;
  var declar;
 
  var hasPattern = false;
  for (i in node.declarations) {
    declar = node.declarations[i];
    if (t.isPattern(declar.id)) {
      hasPattern = true;
      break;
    }
  }
  if (!hasPattern) return;
 
  for (i in node.declarations) {
    declar = node.declarations[i];
 
    var patternId = declar.init;
    var pattern   = declar.id;
    var opts = {
      kind:    node.kind,
      nodes:   nodes,
      pattern: pattern,
      id:      patternId,
      file:    file,
      scope:   scope
    };
 
    if (t.isPattern(pattern) && patternId) {
      pushPattern(opts);
 
      if (+i !== node.declarations.length - 1) {
        // we aren't the last declarator so let's just make the
        // last transformed node inherit from us
        t.inherits(nodes[nodes.length - 1], declar);
      }
    } else {
      nodes.push(t.inherits(buildVariableAssign(opts, declar.id, declar.init), declar));
    }
  }
 
  Iif (!t.isProgram(parent) && !t.isBlockStatement(parent)) {
    declar = null;
 
    for (i in nodes) {
      node = nodes[i];
      declar = declar || t.variableDeclaration(node.kind, []);
 
      if (!t.isVariableDeclaration(node) && declar.kind !== node.kind) {
        throw file.errorWithNode(node, "Cannot use this node within the current parent");
      }
 
      declar.declarations = declar.declarations.concat(node.declarations);
    }
 
    return declar;
  }
 
  return nodes;
};