all files / htmlcs/lib/ reporter.js

100% Statements 33/33
100% Branches 14/14
100% Functions 9/9
100% Lines 33/33
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 162                        4672×   4672× 4672× 4672× 4672×                 103× 103×                 4560×                                               311×               311× 298× 298× 20×       291×   291×                                                                             302×             302×     302×                 118× 435×     118×               10×      
/**
 * @file class Reporter
 * @author nighca<nighca@live.cn>
 */
 
var util = require('./util');
 
/**
 * class Reporter
 *
 * @constructor
 * @param {Object} options - options for reporter
 */
function Reporter(options) {
    options = options || {};
 
    this._options = options;
    this._results = options.results || [];
    this._rule = options.rule || null;
    this._cfg = options.cfg || null;
}
 
/**
 * Config reporter with config info.
 *
 * @param {Object} cfg - reporter config info
 * @return {Reporter} new bound reporter
 */
Reporter.prototype.config = function (cfg) {
    this._cfg = cfg;
    return this;
};
 
/**
 * Create a new reporter bound with given rule (use as default rule).
 *
 * @param {string} rule - name of rule
 * @return {Reporter} new bound reporter
 */
Reporter.prototype.bindRule = function (rule) {
    return new Reporter({
        results: this._results,
        rule: rule,
        cfg: this._cfg
    });
};
 
/**
 * The report item.
 *
 * @typedef {Object} Report
 * @property {string} type - typeof the message, one of "info", "warn", "error"
 * @property {number} pos - position index (in code content) of report
 * @property {string} code - code of the report
 * @property {string} message - message of the report
 * @property {string} rule - name of the report's rule
 */
 
/**
 * Save given report.
 *
 * @param {Object} item - given report item
 * @return {Reporter} reporter itself
 */
Reporter.prototype.report = function (item) {
    item = util.extend({
        type: 'WARN',
        pos: 0,
        code: null,
        message: '',
        rule: this._rule
    }, item);
 
    if (this._cfg) {
        var cfg = util.getInlineCfgByIndex(item.rule, item.pos, this._cfg, 'enable');
        if (cfg === 'disable') {
            return this;
        }
    }
 
    this._results.push(item);
 
    return this;
};
 
/**
 * Report with type "info"
 *
 * @name Reporter.prototype.info
 * @type Function
 * @param {number} pos - position index in code content
 * @param {string} code - code of the report
 * @param {string} message - message of the report
 * @param {string} rule - name of the report's rule
 * @return {Reporter} reporter itself
 */
 
 /**
 * Report with type "warn"
 *
 * @name Reporter.prototype.warn
 * @type Function
 * @param {number} pos - position index in code content
 * @param {string} code - code of the report
 * @param {string} message - message of the report
 * @param {string} rule - name of the report's rule
 * @return {Reporter} reporter itself
 */
 
 /**
 * Report with type "error"
 *
 * @name Reporter.prototype.error
 * @type Function
 * @param {number} pos - position index in code content
 * @param {string} code - code of the report
 * @param {string} message - message of the report
 * @param {string} rule - name of the report's rule
 * @return {Reporter} reporter itself
 */
 
['info', 'warn', 'error'].forEach(function (type) {
    Reporter.prototype[type] = function (pos, code, message, rule) {
        var item = {
            type: type.toUpperCase(),
            pos: pos,
            code: code,
            message: message
        };
 
        if (rule) {
            item.rule = rule;
        }
 
        return this.report(item);
    };
});
 
/**
 * Get result report list.
 *
 * @return {Array.Report} report list
 */
Reporter.prototype.result = function () {
    this._results.sort(function (a, b) {
        return a.pos - b.pos;
    });
 
    return this._results;
};
 
/**
 * Get result report num.
 *
 * @return {number} report num
 */
Reporter.prototype.num = function () {
    return this._results.length;
};
 
module.exports = Reporter;