Code coverage report for 6to5/lib/6to5/transformation/transformers/es6-property-method-assignment.js

Statements: 100% (39 / 39)      Branches: 100% (18 / 18)      Functions: 100% (4 / 4)      Lines: 100% (33 / 33)      Ignored: none     

All files » 6to5/lib/6to5/transformation/transformers/ » es6-property-method-assignment.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 701 1 1   1 443   12   12 12   10 10   10 10   10     69     16       5 5   4 4       10 4             6       1 233 233   233 426 10 10 10   416       233   6          
var traverse = require("../../traverse");
var util     = require("../../util");
var t        = require("../../types");
 
exports.Property = function (node, parent, file, scope) {
  if (!node.method) return;
 
  node.method = false;
 
  var key = t.toComputedKey(node, node.key);
  if (!t.isLiteral(key)) return; // we can't set a function id with this
 
  var id = t.toIdentifier(key.value);
  key = t.identifier(id);
 
  var selfReference = false;
  var outerDeclar = scope.get(id, true);
 
  traverse(node, {
    enter: function (node, parent, scope) {
      // check if this node is an identifier that matches the same as our function id
      if (!t.isIdentifier(node, { name: id })) return;
 
      // check if this node is the one referenced
      if (!t.isReferenced(node, parent)) return;
 
      // check that we don't have a local variable declared as that removes the need
      // for the wrapper
      var localDeclar = scope.get(id, true);
      if (localDeclar !== outerDeclar) return;
 
      selfReference = true;
      this.stop();
    }
  }, scope);
 
  if (selfReference) {
    node.value = util.template("property-method-assignment-wrapper", {
      FUNCTION: node.value,
      FUNCTION_ID: key,
      FUNCTION_KEY: file.generateUidIdentifier(id, scope),
      WRAPPER_KEY: file.generateUidIdentifier(id + "Wrapper", scope)
    });
  } else {
    node.value.id = key;
  }
};
 
exports.ObjectExpression = function (node) {
  var mutatorMap = {};
  var hasAny = false;
 
  node.properties = node.properties.filter(function (prop) {
    if (prop.kind === "get" || prop.kind === "set") {
      hasAny = true;
      util.pushMutatorMap(mutatorMap, prop.key, prop.kind, prop.computed, prop.value);
      return false;
    } else {
      return true;
    }
  });
 
  if (!hasAny) return;
 
  return t.callExpression(
    t.memberExpression(t.identifier("Object"), t.identifier("defineProperties")),
    [node, util.buildDefineProperties(mutatorMap)]
  );
};