Code coverage report for 6to5/transformers/destructuring.js

Statements: 98.9% (90 / 91)      Branches: 92.5% (37 / 40)      Functions: 100% (14 / 14)      Lines: 100% (82 / 82)      Ignored: none     

All files » 6to5/transformers/ » 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 1541 1 1   1 36 2   34           1 20 10 10 10       1 10 20 20   20 6   14         1 10 23   23   23 21   2         1 9 1   1         1     9     1   17 17   14 14   2 2       2   2   2   2 2     1     143 143   143   143 56   4 4 4 4     143 3 3     1 186 186   80   1   1 1       1   1     1 167   158   158 158 159 5 5     158   5 6 6 6 5   1       5    
var util = require("../util");
var b    = require("ast-types").builders;
var _    = require("lodash");
 
var buildVariableAssign = function (kind, id, init) {
  if (kind === false) {
    return b.expressionStatement(b.assignmentExpression("=", id, init));
  } else {
    return b.variableDeclaration(kind, [
      b.variableDeclarator(id, init)
    ]);
  }
};
 
var push = function (kind, nodes, pattern, parentId) {
  if (pattern.type === "ObjectPattern") {
    pushObjectPattern(kind, nodes, pattern, parentId);
  } else Eif (pattern.type === "ArrayPattern") {
    pushArrayPattern(kind, nodes, pattern, parentId);
  }
};
 
var pushObjectPattern = function (kind, nodes, pattern, parentId) {
  _.each(pattern.properties, function (prop) {
    var pattern2   = prop.value;
    var patternId2 = b.memberExpression(parentId, prop.key, false);
 
    if (util.isPattern(pattern2)) {
      push(kind, nodes, pattern2, patternId2);
    } else {
      nodes.push(buildVariableAssign(kind, pattern2, patternId2));
    }
  });
};
 
var pushArrayPattern = function (kind, nodes, pattern, parentId) {
  _.each(pattern.elements, function (elem, i) {
    Iif (!elem) return;
 
    var newPatternId = b.memberExpression(parentId, b.literal(i), true);
 
    if (elem.type === "Identifier") {
      nodes.push(buildVariableAssign(kind, elem, newPatternId));
    } else {
      push(kind, nodes, elem, newPatternId);
    }
  });
};
 
var pushPattern = function (kind, nodes, pattern, parentId, generateUid) {
  if (parentId.type !== "MemberExpression" && parentId.type !== "Identifier") {
    var key = generateUid("ref");
 
    nodes.push(util.template("variable-assign", {
      KEY: key,
      VALUE: parentId
    }, true));
 
    parentId = b.identifier(key);
  }
 
  push(kind, nodes, pattern, parentId);
};
 
exports.ForInStatement =
exports.ForOfStatement = function (node, parent, opts, generateUid) {
  var declar = node.left;
  if (declar.type !== "VariableDeclaration") return;
 
  var pattern = declar.declarations[0].id;
  if (!util.isPattern(pattern)) return;
 
  var key = b.identifier(generateUid("ref"));
  node.left = b.variableDeclaration(declar.kind, [
    b.variableDeclarator(key, null)
  ]);
 
  var nodes = [];
 
  push(declar.kind, nodes, pattern, key);
 
  util.ensureBlock(node);
 
  var block = node.body;
  block.body = nodes.concat(block.body);
};
 
exports.ArrowFunctionExpression =
exports.FunctionDeclaration =
exports.FunctionExpression = function (node, parent, opts, generateUid) {
  var block = node.body;
  var nodes = [];
 
  var hasDestructuring = false;
 
  node.params = node.params.map(function (pattern) {
    if (!util.isPattern(pattern)) return pattern;
 
    hasDestructuring = true;
    var parentId = b.identifier(generateUid("ref"));
    pushPattern("var", nodes, pattern, parentId, generateUid);
    return parentId;
  });
 
  if (!hasDestructuring) return;
  util.ensureBlock(node);
  block.body = nodes.concat(block.body || []);
};
 
exports.ExpressionStatement = function (node, parent, opts, generateUid) {
  var expr = node.expression;
  if (expr.type !== "AssignmentExpression") return;
 
  if (!util.isPattern(expr.left)) return;
 
  var nodes = [];
 
  var ref = b.identifier(generateUid("ref"));
  nodes.push(b.variableDeclaration("var", [
    b.variableDeclarator(ref, expr.right)
  ]));
 
  push(false, nodes, expr.left, ref);
 
  return nodes;
};
 
exports.VariableDeclaration = function (node, parent, opts, generateUid) {
  if (parent.type === "ForInStatement") return;
 
  var nodes = [];
 
  var hasPattern = false;
  _.each(node.declarations, function (declar) {
    if (util.isPattern(declar.id)) {
      hasPattern = true;
      return false;
    }
  });
  if (!hasPattern) return;
 
  _.each(node.declarations, function (declar) {
    var patternId = declar.init;
    var pattern   = declar.id;
    if (util.isPattern(pattern) && patternId) {
      pushPattern(node.kind, nodes, pattern, patternId, generateUid);
    } else {
      nodes.push(buildVariableAssign(node.kind, declar.id, declar.init));
    }
  });
 
  return nodes;
};