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

Statements: 100% (28 / 28)      Branches: 90% (9 / 10)      Functions: 100% (3 / 3)      Lines: 100% (25 / 25)      Ignored: none     

All files » 6to5/transformers/ » 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 471 1 1   1 23 23   23   23 30 7 7 7 7   23       23   5 5   5       5 5 4   1     5 7           5    
var traverse = require("../traverse");
var util     = require("../util");
var _        = require("lodash");
 
exports.ObjectExpression = function (node) {
  var hasComputed = false;
  var hasThis     = false;
 
  var computed = [];
 
  node.properties = node.properties.filter(function (prop) {
    if (prop.computed) {
      hasComputed = true;
      computed.unshift(prop);
      Eif (!hasThis) hasThis = traverse.hasType(prop, "ThisExpression");
      return false;
    } else {
      return true;
    }
  });
 
  if (!hasComputed) return;
 
  var templateName = "function-return-obj";
  if (hasThis) templateName += "-this";
 
  var container = util.template(templateName, {
    OBJECT: node
  });
 
  var containerBody;
  if (templateName === "function-return-obj") {
    containerBody = container.callee.body.body;
  } else {
    containerBody = container.callee.object.body.body;
  }
 
  _.each(computed, function (prop) {
    containerBody.unshift(util.template("obj-key-set", {
      KEY: prop.key,
      VALUE: prop.value
    }, true));
  });
 
  return container;
};