Code coverage report for 6to5/generation/whitespace.js

Statements: 100% (34 / 34)      Branches: 100% (8 / 8)      Functions: 100% (6 / 6)      Lines: 100% (34 / 34)      Ignored: none     

All files » 6to5/generation/ » whitespace.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 591   1   1 315 315     1 1139 1139 1139   1139   32932 1139 1139 1139       1139     1 1139 1139 1139   1139   42995 1139 1139 1139       1139     1 2278 2278   2278   2278 1931 1254 1254       2278    
module.exports = Whitespace;
 
var _ = require("lodash");
 
function Whitespace(tokens, comments) {
  this.tokens = _.sortBy(tokens.concat(comments), "start");
  this.used   = [];
}
 
Whitespace.prototype.getNewlinesBefore = function (node) {
  var startToken;
  var endToken;
  var tokens = this.tokens;
 
  _.each(tokens, function (token, i) {
    // this is the token this node starts with
    if (node.start === token.start) {
      startToken = tokens[i - 1];
      endToken = token;
      return false;
    }
  });
 
  return this.getNewlinesBetween(startToken, endToken);
};
 
Whitespace.prototype.getNewlinesAfter = function (node) {
  var startToken;
  var endToken;
  var tokens = this.tokens;
 
  _.each(tokens, function (token, i) {
    // this is the token this node ends with
    if (node.end === token.end) {
      startToken = token;
      endToken = tokens[i + 1];
      return false;
    }
  });
 
  return this.getNewlinesBetween(startToken, endToken);
};
 
Whitespace.prototype.getNewlinesBetween = function (startToken, endToken) {
  var start = startToken ? startToken.loc.end.line : 1;
  var end   = endToken.loc.start.line;
 
  var lines = 0;
 
  for (var line = start; line < end; line++) {
    if (!_.contains(this.used, line)) {
      this.used.push(line);
      lines++;
    }
  }
 
  return lines;
};