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

Statements: 100% (42 / 42)      Branches: 100% (20 / 20)      Functions: 100% (4 / 4)      Lines: 100% (36 / 36)      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 70 71 72 73 74 75 76 77 78 79 80 81 82 831 1 1   1 14523   12   12 12   10 10   10 10   10     69     16       5 5   4 4       10 4             6       1 10456 10456   10456 14783 285 285 285   14498       10456   119 2   2                 117            
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, parent, file, scope) {
  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;
 
  if (node.properties.length) {
    var objId = scope.generateUidBasedOnNode(parent, file);
 
    return util.template("object-define-properties-closure", {
      KEY: objId,
      OBJECT: node,
      CONTENT: util.template("object-define-properties", {
        OBJECT: objId,
        PROPS:  util.buildDefineProperties(mutatorMap)
      })
    });
  } else {
    return util.template("object-define-properties", {
      OBJECT: node,
      PROPS:  util.buildDefineProperties(mutatorMap)
    });
  }
};