Code coverage report for 6to5/lib/6to5/transformation/transformers/es6-computed-property-names.js

Statements: 98.04% (50 / 51)      Branches: 96.43% (27 / 28)      Functions: 100% (1 / 1)      Lines: 100% (47 / 47)      Ignored: none     

All files » 6to5/lib/6to5/transformation/transformers/ » es6-computed-property-names.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 1001   1 251 251 251 251   251 414 414     251   24   24 24 24   24       24 32 32   32   32 6           24 24   24 32   32 26     32 3 3             24 32 32   29 29   29   2           27     29         24 21   21 19 19           5       5   5    
var t = require("../../types");
 
exports.ObjectExpression = function (node, parent, file, scope) {
  var hasComputed = false;
  var prop;
  var key;
  var i;
 
  for (i in node.properties) {
    hasComputed = t.isProperty(node.properties[i], { computed: true, kind: "init" });
    if (hasComputed) break;
  }
 
  if (!hasComputed) return;
 
  var objId = scope.generateUidBasedOnNode(parent, file);
 
  var body = [];
  var container = t.functionExpression(null, [], t.blockStatement(body));
  container._aliasFunction = true;
 
  var props = node.properties;
 
  // normalise key
 
  for (i in props) {
    prop = props[i];
    Iif (prop.kind !== "init") continue;
 
    key = prop.key;
 
    if (!prop.computed && t.isIdentifier(key)) {
      prop.key = t.literal(key.name);
    }
  }
 
  // add all non-computed properties and `__proto__` properties to the initializer
 
  var initProps = [];
  var broken = false;
 
  for (i in props) {
    prop = props[i];
 
    if (prop.computed) {
      broken = true;
    }
 
    if (prop.kind !== "init" || !broken || t.isLiteral(t.toComputedKey(prop, prop.key), { value: "__proto__" })) {
      initProps.push(prop);
      props[i] = null;
    }
  }
 
  // add a simple assignment for all Symbol member expressions due to symbol polyfill limitations
  // otherwise use Object.defineProperty
 
  for (i in props) {
    prop = props[i];
    if (!prop) continue;
 
    key = prop.key;
    var bodyNode;
 
    if (prop.computed && t.isMemberExpression(key) && t.isIdentifier(key.object, { name: "Symbol" })) {
      // { [Symbol.iterator]: "foo" }
      bodyNode = t.assignmentExpression(
        "=",
        t.memberExpression(objId, key, true),
        prop.value
      );
    } else {
      bodyNode = t.callExpression(file.addHelper("define-property"), [objId, key, prop.value]);
    }
 
    body.push(t.expressionStatement(bodyNode));
  }
 
  // only one node and it's a Object.defineProperty that returns the object
 
  if (body.length === 1) {
    var first = body[0].expression;
 
    if (t.isCallExpression(first)) {
      first.arguments[0] = t.objectExpression(initProps);
      return first;
    }
  }
 
  //
 
  body.unshift(t.variableDeclaration("var", [
    t.variableDeclarator(objId, t.objectExpression(initProps))
  ]));
 
  body.push(t.returnStatement(objId));
 
  return t.callExpression(container, []);
};