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

Statements: 98.04% (50 / 51)      Branches: 90.91% (20 / 22)      Functions: 100% (4 / 4)      Lines: 97.92% (47 / 48)      Ignored: none     

All files » 6to5/lib/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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 921 1 1   1 508 18   18 26     18 18 18   18 23 24   4 2     2 2       23 21     18 25 25   20       20 20 23       18 18         16   16 16   16   16 23 23 5 5     18                 16   16 1 1   1   1   15     16    
var traverse = require("../../traverse");
var util     = require("../../util");
var t        = require("../../types");
 
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 iife = false;
  var i;
  var def;
 
  var checkTDZ = function (ids) {
    var check = function (node, parent) {
      if (!t.isIdentifier(node) || !t.isReferenced(node, parent)) return;
 
      if (ids.indexOf(node.name) >= 0) {
        throw file.errorWithNode(node, "Temporal dead zone - accessing a variable before it's initialized");
      }
 
      Eif (scope.has(node.name, true)) {
        iife = true;
      }
    };
 
    check(def, node);
    traverse(def, { enter: check });
  };
 
  for (i in node.defaults) {
    def = node.defaults[i];
    if (!def) continue;
 
    var param = node.params[i];
 
    // temporal dead zone check - here we prevent accessing of params that
    // are to the right - ie. uninitialized parameters
    var rightIds = ids.slice(i);
    for (i in rightIds) {
      checkTDZ(rightIds[i]);
    }
 
    // we're accessing a variable that's already defined within this function
    var has = scope.get(param.name);
    Iif (has && node.params.indexOf(has) < 0) {
      iife = true;
    }
  }
 
  var body = [];
 
  var argsIdentifier = t.identifier("arguments");
  argsIdentifier._ignoreAliasFunctions = true;
 
  var lastNonDefaultParam = 0;
 
  for (i in node.defaults) {
    def = node.defaults[i];
    if (!def) {
      lastNonDefaultParam = +i + 1;
      continue;
    }
 
    body.push(util.template("default-parameter", {
      VARIABLE_NAME: node.params[i],
      DEFAULT_VALUE: def,
      ARGUMENT_KEY:  t.literal(+i),
      ARGUMENTS:     argsIdentifier
    }, true));
  }
 
  // we need to cut off all trailing default parameters
  node.params = node.params.slice(0, lastNonDefaultParam);
 
  if (iife) {
    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 = [];
};