Code coverage report for 6to5/transformation/transformers/es7-abstract-references.js

Statements: 86.27% (44 / 51)      Branches: 73.08% (19 / 26)      Functions: 100% (7 / 7)      Lines: 87.23% (41 / 47)      Ignored: none     

All files » 6to5/transformation/transformers/ » es7-abstract-references.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    1 1   1 4   2   2 2     2   2 2       1 294 294   2 2     2   1         2                     2           2             2     1 53 53 2   2         2     1 1094 1094   2 2         2         2   2           2       1 1           1 6 9      
// https://github.com/zenparsing/es-abstract-refs
 
var util = require("../../util");
var t    = require("../../types");
 
var container = function (parent, call, ret) {
  if (t.isExpressionStatement(parent)) {
    // we don't need to worry about return values
    return call;
  } else {
    var exprs = [];
    Iif (t.isSequenceExpression(call)) {
      exprs = call.expressions;
    } else {
      exprs.push(call);
    }
    exprs.push(ret);
    return t.sequenceExpression(exprs);
  }
};
 
exports.AssignmentExpression = function (node, parent, file, scope) {
  var left = node.left;
  if (!t.isVirtualPropertyExpression(left)) return;
 
  var value = node.right;
  var temp;
 
  // we need to return `node.right`
  if (!t.isExpressionStatement(parent)) {
    // `node.right` isn't a simple identifier so we need to reference it
    Iif (t.isDynamic(value)) {
      temp = value = scope.generateTemp(file);
    }
  }
 
  Iif (node.operator !== "=") {
    value = t.binaryExpression(
      node.operator[0],
      util.template("abstract-expression-get", {
        PROPERTY: node.property,
        OBJECT:   node.object
      }),
      value
    );
  }
 
  var call = util.template("abstract-expression-set", {
    PROPERTY: left.property,
    OBJECT:   left.object,
    VALUE:    value
  });
 
  Iif (temp) {
    call = t.sequenceExpression([
      t.assignmentExpression("=", temp, node.right),
      call
    ]);
  }
 
  return container(parent, call, value);
};
 
exports.UnaryExpression = function (node, parent) {
  var arg = node.argument;
  if (!t.isVirtualPropertyExpression(arg)) return;
  Iif (node.operator !== "delete") return;
 
  var call = util.template("abstract-expression-delete", {
    PROPERTY: arg.property,
    OBJECT:   arg.object
  });
 
  return container(parent, call, t.literal(true));
};
 
exports.CallExpression = function (node, parent, file, scope) {
  var callee = node.callee;
  if (!t.isVirtualPropertyExpression(callee)) return;
 
  var temp;
  Iif (t.isDynamic(callee.object)) {
    // we need to save `callee.object` so we can call it again
    temp = scope.generateTemp(file);
  }
 
  var call = util.template("abstract-expression-call", {
    PROPERTY: callee.property,
    OBJECT:   temp || callee.object
  });
 
  call.arguments = call.arguments.concat(node.arguments);
 
  Iif (temp) {
    return t.sequenceExpression([
      t.assignmentExpression("=", temp, callee.object),
      call
    ]);
  } else {
    return call;
  }
};
 
exports.VirtualPropertyExpression = function (node) {
  return util.template("abstract-expression-get", {
    PROPERTY: node.property,
    OBJECT:   node.object
  });
};
 
exports.PrivateDeclaration = function (node) {
  return t.variableDeclaration("const", node.declarations.map(function (id) {
    return t.variableDeclarator(id, t.newExpression(t.identifier("WeakMap"), []));
  }));
};