Code coverage report for lib/reporters/console-reporter.js

Statements: 92.68% (38 / 41)      Branches: 70% (7 / 10)      Functions: 80% (4 / 5)      Lines: 92.68% (38 / 41)      Ignored: none     

All files » lib/reporters/ » console-reporter.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 132 133 134 135 136 137    1 1 1 1             1         5         5               1               1         1         1               1   2 2   2 2 2 2   2     2       2           1                     1   4 4   4 2   2   2   2 2 4       2 1       4   4               1                       1  
'use strict';
 
var merge = require('merge');
var chalk = require('chalk');
var errorFactory = require('../util/error-factory');
var AbstractReporter = require('./abstract-reporter');
 
/**
 * @param {Object=} options
 * @extends {AbstractReporter}
 * @constructor
 */
var ConsoleReporter = function(options)
{
  /**
   * @type {boolean}
   */
  this.failed = false;
 
  /**
   * @type {*}
   */
  this.options = merge({}, ConsoleReporter.DEFAULT_OPTIONS, options);
};
 
/**
 * Inherit from AbstractReporter.
 *
 * @type {AbstractReporter}
 */
ConsoleReporter.prototype = new AbstractReporter();
 
/**
 * Correctly set the constructor, which will have been
 * set to AbstractReporter.
 *
 * @type {ConsoleReporter}
 */
ConsoleReporter.prototype.constructor = ConsoleReporter;
 
/**
 * @type {string}
 */
ConsoleReporter.REPORTER_NAME = 'console';
 
/**
 * @type {*}
 */
ConsoleReporter.DEFAULT_OPTIONS = {
  fail: false
};
 
/**
 * @param {File} file
 * @return {Array}
 */
ConsoleReporter.createErrorOutput = function(file)
{
  var output = [];
  var results = file.gjslint.results;
 
  for (var i = 0, n = results.errors.length; i < n; i += 1) {
    var error = results.errors[i];
    var line = chalk.gray('line ' + error.line);
    var description = chalk.blue(error.description);
 
    output.push('  ' + line + ': ' + description);
  }
 
  output.push(
    chalk.bold.yellow('  Total errors in file: ' + results.total + '\n')
  );
 
  return output;
};
 
/**
 * @type {function}
 */
ConsoleReporter.prototype.createError = errorFactory('gulp-gjslint');
 
/**
 * Output linting result to the console for a single file.
 *
 * TODO: Check why options aren't being used.
 *
 * @param {File} file
 * @param {string} encoding
 * @param {Function} callback
 */
ConsoleReporter.prototype.processFile = function(file, encoding, callback)
{
  var options = merge({}, this.options);
  var errorOutput;
 
  if (file.gjslint && file.gjslint.success === false) {
    this.failed = true;
 
    errorOutput = ConsoleReporter.createErrorOutput(file);
 
    console.log(chalk.bold.bgBlue.white(file.path));
 
    Eif (errorOutput.length) {
      errorOutput.forEach(function(e) {
        console.log(e);
      });
    }
 
    if (this.options.fail === true) {
      this.stream.emit('error', this.createError('Linting failed'));
    }
  }
 
  this.stream.push(file);
 
  callback();
};
 
/**
 * Output failure message after the errors have been shown.
 *
 * @param {function} callback
 */
ConsoleReporter.prototype.flush = function(callback)
{
  if (this.failed === true) {
    console.log(chalk.bold.red('gjslint failed'));
  }
 
  callback();
};
 
/**
 * @type {ConsoleReporter}
 */
module.exports = ConsoleReporter;