Code coverage report for 6to5/transformation/transformers/es6-default-parameters.js

Statements: 97.5% (39 / 40)      Branches: 90.91% (20 / 22)      Functions: 100% (6 / 6)      Lines: 97.22% (35 / 36)      Ignored: none     

All files » 6to5/transformation/transformers/ » es6-default-parameters.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 711 1 1 1   1 369 7   7 11     7   7 10   8       8 10 11   4 2     2 2       10 8       6 6         5   5 8   6           5 1 1   1   1   4     5    
var traverse = require("../../traverse");
var util     = require("../../util");
var t        = require("../../types");
var _        = require("lodash");
 
exports.Function = function (node, parent, file, scope) {
  if (!node.defaults || !node.defaults.length) return;
  t.ensureBlock(node);
 
  var ids = node.params.map(function (param) {
    return t.getIds(param);
  });
 
  var closure = false;
 
  _.each(node.defaults, function (def, i) {
    if (!def) return;
 
    var param = node.params[i];
 
    // temporal dead zone check - here we prevent accessing of params that
    // are to the right - ie. uninitialized parameters
    _.each(ids.slice(i), function (ids) {
      var check = function (node, parent) {
        if (!t.isIdentifier(node) || !t.isReferenced(node, parent)) return;
 
        if (_.contains(ids, node.name)) {
          throw file.errorWithNode(node, "Temporal dead zone - accessing a variable before it's initialized");
        }
 
        Eif (scope.has(node.name)) {
          closure = true;
        }
      };
 
      check(def, node);
      traverse(def, check);
    });
 
    // we're accessing a variable that's already defined within this function
    var has = scope.get(param.name);
    Iif (has && !_.contains(node.params, has)) {
      closure = true;
    }
  });
 
  var body = [];
 
  _.each(node.defaults, function (def, i) {
    if (!def) return;
 
    body.push(util.template("if-undefined-set-to", {
      VARIABLE: node.params[i],
      DEFAULT:  def
    }, true));
  });
 
  if (closure) {
    var container = t.functionExpression(null, [], node.body, node.generator);
    container._aliasFunction = true;
 
    body.push(t.returnStatement(t.callExpression(container, [])));
 
    node.body = t.blockStatement(body);
  } else {
    node.body.body = body.concat(node.body.body);
  }
 
  node.defaults = [];
};