Code coverage report for 6to5/transformation/transformers/es6-generators/emit/explode-expressions.js

Statements: 82.5% (66 / 80)      Branches: 68.18% (15 / 22)      Functions: 84.21% (16 / 19)      Lines: 82.5% (66 / 80)      Ignored: none     

All files » 6to5/transformation/transformers/es6-generators/emit/ » explode-expressions.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                    1 1 1   1 13     1 1             1 8 8               8 1           8     11         1 1     2         1 2   6                 1 3   7         1 2 2 2   2 4 2   2       2     1                                               1 2 2 2 2   2   2 2     2 2   2 2   2   2     1                   1 8             1 7             1               1 231 231 231   231 20   20               20   20     211 211 211   211    
/**
 * Copyright (c) 2014, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
 * additional grant of patent rights can be found in the PATENTS file in
 * the same directory.
 */
 
var assert = require("assert");
var loc    = require("../util").loc;
var t      = require("../../../../types");
 
exports.ParenthesizedExpression = function (expr, path, explodeViaTempVar, finish) {
  return finish(this.explodeExpression(path.get("expression")));
};
 
exports.MemberExpression = function (expr, path, explodeViaTempVar, finish) {
  return finish(t.memberExpression(
    this.explodeExpression(path.get("object")),
    expr.computed ? explodeViaTempVar(null, path.get("property")) : expr.property,
    expr.computed
  ));
};
 
exports.CallExpression = function (expr, path, explodeViaTempVar, finish) {
  var oldCalleePath = path.get("callee");
  var newCallee = this.explodeExpression(oldCalleePath);
 
  // If the callee was not previously a MemberExpression, then the
  // CallExpression was "unqualified," meaning its `this` object should
  // be the global object. If the exploded expression has become a
  // MemberExpression, then we need to force it to be unqualified by
  // using the (0, object.property)(...) trick; otherwise, it will
  // receive the object of the MemberExpression as its `this` object.
  if (!t.isMemberExpression(oldCalleePath.node) && t.isMemberExpression(newCallee)) {
    newCallee = t.sequenceExpression([
      t.literal(0),
      newCallee
    ]);
  }
 
  return finish(t.callExpression(
    newCallee,
    path.get("arguments").map(function (argPath) {
      return explodeViaTempVar(null, argPath);
    })
  ));
};
 
exports.NewExpression = function (expr, path, explodeViaTempVar, finish) {
  return finish(t.newExpression(
    explodeViaTempVar(null, path.get("callee")),
    path.get("arguments").map(function (argPath) {
      return explodeViaTempVar(null, argPath);
    })
  ));
};
 
exports.ObjectExpression = function (expr, path, explodeViaTempVar, finish) {
  return finish(t.objectExpression(
    path.get("properties").map(function (propPath) {
      return t.property(
        propPath.value.kind,
        propPath.value.key,
        explodeViaTempVar(null, propPath.get("value"))
      );
    })
  ));
};
 
exports.ArrayExpression = function (expr, path, explodeViaTempVar, finish) {
  return finish(t.arrayExpression(
    path.get("elements").map(function (elemPath) {
      return explodeViaTempVar(null, elemPath);
    })
  ));
};
 
exports.SequenceExpression = function (expr, path, explodeViaTempVar, finish, ignoreResult) {
  var lastIndex = expr.expressions.length - 1;
  var self = this;
  var result;
 
  path.get("expressions").each(function (exprPath) {
    if (exprPath.name === lastIndex) {
      result = self.explodeExpression(exprPath, ignoreResult);
    } else {
      self.explodeExpression(exprPath, true);
    }
  });
 
  return result;
};
 
exports.LogicalExpression = function (expr, path, explodeViaTempVar, finish, ignoreResult) {
  var after = loc();
  var result;
 
  if (!ignoreResult) {
    result = this.makeTempVar();
  }
 
  var left = explodeViaTempVar(result, path.get("left"));
 
  if (expr.operator === "&&") {
    this.jumpIfNot(left, after);
  } else {
    assert.strictEqual(expr.operator, "||");
    this.jumpIf(left, after);
  }
 
  explodeViaTempVar(result, path.get("right"), ignoreResult);
 
  this.mark(after);
 
  return result;
};
 
exports.ConditionalExpression = function (expr, path, explodeViaTempVar, finish, ignoreResult) {
  var elseLoc = loc();
  var after = loc();
  var test = this.explodeExpression(path.get("test"));
  var result;
 
  this.jumpIfNot(test, elseLoc);
 
  Eif (!ignoreResult) {
    result = this.makeTempVar();
  }
 
  explodeViaTempVar(result, path.get("consequent"), ignoreResult);
  this.jump(after);
 
  this.mark(elseLoc);
  explodeViaTempVar(result, path.get("alternate"), ignoreResult);
 
  this.mark(after);
 
  return result;
};
 
exports.UnaryExpression = function (expr, path, explodeViaTempVar, finish) {
  return finish(t.unaryExpression(
    expr.operator,
    // Can't (and don't need to) break up the syntax of the argument.
    // Think about delete a[b].
    this.explodeExpression(path.get("argument")),
    !!expr.prefix
  ));
};
 
exports.BinaryExpression = function (expr, path, explodeViaTempVar, finish) {
  return finish(t.binaryExpression(
    expr.operator,
    explodeViaTempVar(null, path.get("left")),
    explodeViaTempVar(null, path.get("right"))
  ));
};
 
exports.AssignmentExpression = function (expr, path, explodeViaTempVar, finish) {
  return finish(t.assignmentExpression(
    expr.operator,
    this.explodeExpression(path.get("left")),
    this.explodeExpression(path.get("right"))
  ));
};
 
exports.UpdateExpression = function (expr, path, explodeViaTempVar, finish) {
  return finish(t.updateExpression(
    expr.operator,
    this.explodeExpression(path.get("argument")),
    expr.prefix
  ));
};
 
exports.YieldExpression = function (expr, path) {
  var after = loc();
  var arg = expr.argument && this.explodeExpression(path.get("argument"));
  var result;
 
  if (arg && expr.delegate) {
    result = this.makeTempVar();
 
    this.emit(t.returnStatement(t.callExpression(
      this.contextProperty("delegateYield"), [
        arg,
        t.literal(result.property.name),
        after
      ]
    )));
 
    this.mark(after);
 
    return result;
  }
 
  this.emitAssign(this.contextProperty("next"), after);
  this.emit(t.returnStatement(arg || null));
  this.mark(after);
 
  return this.contextProperty("sent");
};