Code coverage report for 6to5/lib/6to5/generation/buffer.js

Statements: 96.2% (76 / 79)      Branches: 89.19% (33 / 37)      Functions: 100% (17 / 17)      Lines: 98.59% (70 / 71)      Ignored: none     

All files » 6to5/lib/6to5/generation/ » buffer.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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 1321   1 1   1 12210 12210 12210 12210     1 18572     1 942960     942960       1 11901     1 60263     1 60263     1 100820     1 23     1 52823 52823     1 40589 40589     1 72785 67266       1 457624   60410 60410     1 871246   871246 52823 52823     871246 467222 467222   228180 298274   228180     404024   404024     404024   404024     1 1446370   855733     855733     855733     1446370     1 1926331 1926331     1 934444 934444     1 1440679 1440679 1440679   1440679 73130   1367549      
module.exports = Buffer;
 
var util = require("../util");
var _    = require("lodash");
 
function Buffer(position, format) {
  this.position = position;
  this._indent  = format.indent.base;
  this.format   = format;
  this.buf      = "";
}
 
Buffer.prototype.get = function () {
  return util.trimRight(this.buf);
};
 
Buffer.prototype.getIndent = function () {
  Iif (this.format.compact) {
    return "";
  } else {
    return util.repeat(this._indent, this.format.indent.style);
  }
};
 
Buffer.prototype.indentSize = function () {
  return this.getIndent().length;
};
 
Buffer.prototype.indent = function () {
  this._indent++;
};
 
Buffer.prototype.dedent = function () {
  this._indent--;
};
 
Buffer.prototype.semicolon = function () {
  this.push(";");
};
 
Buffer.prototype.ensureSemicolon = function () {
  Eif (!this.isLast(";")) this.semicolon();
};
 
Buffer.prototype.rightBrace = function () {
  this.newline(true);
  this.push("}");
};
 
Buffer.prototype.keyword = function (name) {
  this.push(name);
  this.push(" ");
};
 
Buffer.prototype.space = function () {
  if (this.buf && !this.isLast([" ", "\n"])) {
    this.push(" ");
  }
};
 
Buffer.prototype.removeLast = function (cha) {
  if (!this.isLast(cha)) return;
 
  this.buf = this.buf.substr(0, this.buf.length - 1);
  this.position.unshift(cha);
};
 
Buffer.prototype.newline = function (i, removeLast) {
  Iif (this.format.compact) return;
 
  if (_.isBoolean(i)) {
    removeLast = i;
    i = null;
  }
 
  if (_.isNumber(i)) {
    if (this.endsWith("{\n")) i--;
    if (this.endsWith(util.repeat(i, "\n"))) return;
 
    for (var j = 0; j < i; j++) {
      this.newline(null, removeLast);
    }
    return;
  }
 
  if (removeLast && this.isLast("\n")) this.removeLast("\n");
 
  this.removeLast(" ");
 
  // remove whitespace if last character was a newline
  this.buf = this.buf.replace(/\n +$/, "\n");
 
  this._push("\n");
};
 
Buffer.prototype.push = function (str, noIndent) {
  if (this._indent && !noIndent && str !== "\n") {
    // we have an indent level and we aren't pushing a newline
    var indent = this.getIndent();
 
    // replace all newlines with newlines with the indentation
    str = str.replace(/\n/g, "\n" + indent);
 
    // we've got a newline before us so prepend on the indentation
    if (this.isLast("\n")) str = indent + str;
  }
 
  this._push(str);
};
 
Buffer.prototype._push = function (str) {
  this.position.push(str);
  this.buf += str;
};
 
Buffer.prototype.endsWith = function (str) {
  var d = this.buf.length - str.length;
  return d >= 0 && this.buf.lastIndexOf(str) === d;
};
 
Buffer.prototype.isLast = function (cha, trimRight) {
  var buf = this.buf;
  Iif (trimRight) buf = util.trimRight(buf);
  var last = buf[buf.length - 1];
 
  if (Array.isArray(cha)) {
    return _.contains(cha, last);
  } else {
    return cha === last;
  }
};