Code coverage report for 6to5/lib/6to5/generation/generators/types.js

Statements: 100% (59 / 59)      Branches: 96.3% (26 / 27)      Functions: 100% (9 / 9)      Lines: 100% (56 / 56)      Ignored: none     

All files » 6to5/lib/6to5/generation/generators/ » types.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 92 93 94 95 96 97 98 99 100 1011   1 276875     1   7 7     1 3 3 3     1   10454   10454 7303 7303   7303   7303 7303   3151       1 14712 11   14701 11 11 11   14690 14690     14687 14687       1   3244 3244 3244   3244   3244 8367           224   8143 8143 8143       3244     1 170613 170613   170613 69023     69023 702     69023 101590 98861 2729 1214 1515 1515      
var _ = require("lodash");
 
exports.Identifier = function (node) {
  this.push(node.name);
};
 
exports.SpreadElement =
exports.SpreadProperty = function (node, print) {
  this.push("...");
  print(node.argument);
};
 
exports.VirtualPropertyExpression = function (node, print) {
  print(node.object);
  this.push("::");
  print(node.property);
};
 
exports.ObjectExpression =
exports.ObjectPattern = function (node, print) {
  var props = node.properties;
 
  if (props.length) {
    this.push("{");
    this.space();
 
    print.join(props, { separator: ", ", indent: true });
 
    this.space();
    this.push("}");
  } else {
    this.push("{}");
  }
};
 
exports.Property = function (node, print) {
  if (node.method || node.kind === "get" || node.kind === "set") {
    this._method(node, print);
  } else {
    if (node.computed) {
      this.push("[");
      print(node.key);
      this.push("]");
    } else {
      print(node.key);
      if (node.shorthand) return;
    }
 
    this.push(": ");
    print(node.value);
  }
};
 
exports.ArrayExpression =
exports.ArrayPattern = function (node, print) {
  var elems = node.elements;
  var self  = this;
  var len   = elems.length;
 
  this.push("[");
 
  _.each(elems, function (elem, i) {
    if (!elem) {
      // If the array expression ends with a hole, that hole
      // will be ignored by the interpreter, but if it ends with
      // two (or more) holes, we need to write out two (or more)
      // commas so that the resulting code is interpreted with
      // both (all) of the holes.
      self.push(",");
    } else {
      if (i > 0) self.push(" ");
      print(elem);
      if (i < len - 1) self.push(",");
    }
  });
 
  this.push("]");
};
 
exports.Literal = function (node) {
  var val  = node.value;
  var type = typeof val;
 
  if (type === "string") {
    val = JSON.stringify(val);
 
    // escape illegal js but valid json unicode characters
    val = val.replace(/[\u000A\u000D\u2028\u2029]/g, function (c) {
      return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4);
    });
 
    this.push(val);
  } else if (type === "boolean" || type === "number") {
    this.push(JSON.stringify(val));
  } else if (node.regex) {
    this.push("/" + node.regex.pattern + "/" + node.regex.flags);
  } else Eif (val === null) {
    this.push("null");
  }
};