Code coverage report for lib/GulpGjslint.js

Statements: 87.8% (36 / 41)      Branches: 81.82% (9 / 11)      Functions: 100% (5 / 5)      Lines: 87.8% (36 / 41)      Ignored: none     

All files » lib/ » GulpGjslint.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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161            3 3 3 3 3 3           3     12             12     12             12         12           3         3             3                       3   8       8 4         4 4     8                 3       5   5                 5   5   4                                       4   4   4     5           3   1   1           3  
/*
 * gulp-gjslint
 */
 
'use strict';
 
var GulpGjslint;
var gjslint = require('closure-linter-wrapper').gjslint;
var PluginError = require('gulp-util').PluginError;
var merge = require('merge');
var through = require('through2');
var errorFactory = require('./util/error-factory');
 
/**
 * @param {Object=} options
 * @constructor
 */
GulpGjslint = function(options)
{
  // Set options to empty object if none were specified
  options = options || {};
 
  /**
   * Merge options with the default options.
   *
   * @type {*|exports}
   */
  this.options = merge({}, GulpGjslint.DEFAULT_OPTIONS, options);
 
  // Force reporter to be null as reporting is handled separately
  this.options.reporter = GulpGjslint.DEFAULT_OPTIONS.reporter;
 
  /**
   * Initialise stream property.
   *
   * @type {null}
   */
  this.stream = null;
 
  /**
   * @type {Function}
   */
  this.createError = new errorFactory(GulpGjslint.PLUGIN_NAME);
};
 
/**
 * @type {string}
 */
GulpGjslint.PLUGIN_NAME = 'gulp-gjslint';
 
/**
 * @type {Object}
 */
GulpGjslint.DEFAULT_OPTIONS = {
  reporter: null
};
 
/**
 * @type {*}
 */
GulpGjslint.reporter = require(__dirname + '/reporters');
 
/**
 * Adds linting result data to a File object.
 *
 * This data can be used by a reporter after the stream
 * has finished.
 *
 * @param {File} file
 * @param {Object=} errors
 * @return {File}
 */
GulpGjslint.prototype.parseResults = function(file, errors)
{
  file.gjslint = {
    success: !errors
  };
 
  if (errors) {
    file.gjslint.results = errors.info;
 
    // gjslint returns an array of files,
    // but we only processed one, so just
    // return that one.
    file.gjslint.results.errors = file.gjslint.results.fails[0].errors;
    delete file.gjslint.results.fails;
  }
 
  return file;
};
 
/**
 * @param {File} file
 * @param {String} encoding
 * @param {Function} callback
 * @return {Function=}
 */
GulpGjslint.prototype.processFile = function(file, encoding, callback)
{
  // Get copy of options, so that any modifications
  // will be for this file only.
  var options = merge({}, this.options);
 
  Iif (file.isStream()) {
    this.stream.emit(
      'error',
      this.createError('Streaming is not supported')
    );
 
    return callback();
  }
 
  options.src = [file.path];
 
  gjslint(options, function(err) {
    // Check gjslint didn't blow up
    Iif (err && (err.code !== 0 && err.code !== 2)) {
      /*
       Exit codes:
       0: Linting success
       1: Python not found
       2: Linting failed
       3: Parsing failed
       4: gjslint exception
       */
 
      var errorMessage;
 
      errorMessage = 'gjslint crashed whilst parsing: ' + file.path +
       '\nReason: ' + err.description +
        '\n\n' + err.info;
 
      this.stream.emit('error', this.createError(errorMessage));
    }
 
    // Store result data on the file object
    file = this.parseResults(file, err);
 
    this.stream.push(file);
 
    callback();
  }.bind(this));
 
  return null;
};
 
/**
 * @return {Stream=}
 */
GulpGjslint.prototype.run = function()
{
  this.stream = through.obj(this.processFile.bind(this));
 
  return this.stream;
};
 
/**
 * @type {GulpGjslint}
 */
module.exports = GulpGjslint;