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

Statements: 94.17% (113 / 120)      Branches: 86.21% (50 / 58)      Functions: 93.75% (15 / 16)      Lines: 93.58% (102 / 109)      Ignored: none     

All files » 6to5/transformation/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 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 2281 1 1   1 57 4   53           1 69 13 56 19 37 2   35       1 13 25 25   25 6   19         1 19 19         19   19 42   41   41 2       2 1     2   39     41       1 18 18 18 18 18   18 8   8       8     18     1   40 40   37 37   2 2       2   2           2   2 2     1 358   358   358 178   6 6   6                 6     358   5   5 5     1 814 814   191   2   2 2       2           2     1 226 33 1     1 524   487   487 487 497 12 12     487   12 13 13 13 12                 1       12                               12      
var util = require("../../util");
var t    = require("../../types");
var _    = require("lodash");
 
var buildVariableAssign = function (kind, id, init) {
  if (kind === false) {
    return t.expressionStatement(t.assignmentExpression("=", id, init));
  } else {
    return t.variableDeclaration(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 if (t.isMemberExpression(elem)) {
    nodes.push(buildVariableAssign(false, elem, parentId));
  } else {
    nodes.push(buildVariableAssign(opts.kind, elem, parentId));
  }
};
 
var pushObjectPattern = function (opts, nodes, pattern, parentId) {
  _.each(pattern.properties, function (prop) {
    var pattern2   = prop.value;
    var patternId2 = t.memberExpression(parentId, prop.key);
 
    if (t.isPattern(pattern2)) {
      push(opts, nodes, pattern2, patternId2);
    } else {
      nodes.push(buildVariableAssign(opts.kind, pattern2, patternId2));
    }
  });
};
 
var pushArrayPattern = function (opts, nodes, pattern, parentId) {
  var _parentId = opts.file.generateUidIdentifier("ref", opts.scope);
  nodes.push(t.variableDeclaration("var", [
    t.variableDeclarator(_parentId, util.template("array-from", {
      VALUE: parentId
    }))
  ]));
  parentId = _parentId;
 
  _.each(pattern.elements, function (elem, i) {
    if (!elem) return;
 
    var newPatternId;
 
    if (t.isSpreadElement(elem)) {
      newPatternId = util.template("array-from", {
        VALUE: 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.isMemberExpression(parentId) && !t.isIdentifier(parentId)) {
    var key = file.generateUidIdentifier("ref", scope);
 
    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.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({
    kind: false,
    file: file,
    scope: scope
  }, nodes, expr.left, ref);
 
  return nodes;
};
 
exports.AssignmentExpression = function (node, parent, file) {
  if (parent.type === "ExpressionStatement") return;
  if (!t.isPattern(node.left)) return;
  throw file.errorWithNode(node, "AssignmentExpression destructuring outside of a ExpressionStatement is forbidden due to current 6to5 limitations");
};
 
exports.VariableDeclaration = function (node, parent, file, scope) {
  if (t.isForInStatement(parent) || t.isForOfStatement(parent)) return;
 
  var nodes = [];
 
  var hasPattern = false;
  _.each(node.declarations, function (declar) {
    if (t.isPattern(declar.id)) {
      hasPattern = true;
      return false;
    }
  });
  if (!hasPattern) return;
 
  _.each(node.declarations, function (declar) {
    var patternId = declar.init;
    var pattern   = declar.id;
    if (t.isPattern(pattern) && patternId) {
      pushPattern({
        kind:    node.kind,
        nodes:   nodes,
        pattern: pattern,
        id:      patternId,
        file:    file,
        scope:   scope
      });
    } else {
      nodes.push(buildVariableAssign(node.kind, declar.id, declar.init));
    }
  });
 
  Iif (!t.isProgram(parent) && !t.isBlockStatement(parent)) {
    var declar;
 
    _.each(nodes, function (node) {
      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;
};