-
Notifications
You must be signed in to change notification settings - Fork 0
/
jscs-json-reporter.js
94 lines (82 loc) · 2.72 KB
/
jscs-json-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
var fs = require('fs');
var path = require('path');
/**
* @param {Array.<Errors>} errorsCollection
*/
module.exports = function (errorsCollection) {
'use strict';
var config = this.options;
var reporter = {
/**
* @description An errors object that will be converted to JSON string.
*/
errorsObj: {
totalCount: 0,
errors: []
},
/**
* @description Create report and output to file.
* @this reporter
*/
report: function () {
errorsCollection.forEach(this.processCollection, this);
this.outputToFile(this.errorsObj);
},
/**
* @description Iterates over each errors collection, updates total count and populates `errors` array.
* @param {Object} errors Errors collection
* @this reporter
*/
processCollection: function (errors) {
if (!errors.isEmpty()) {
this.errorsObj.totalCount += errors.getErrorCount();
errors.getErrorList().forEach(function (error) {
this.errorsObj.errors.push(this.mapError(error, errors));
}, this);
}
},
/**
*
* @param {Object} error JSCS error object
* @param {Object} errors Errors collection for a certain rule
* @returns {Object} Mapped error object
* @this reporter
*/
mapError: function (error, errors) {
return {
message: error.message,
filename: error.filename,
line: error.line,
column: error.column,
rule: error.rule,
explainedError: errors.explainError(error, false)
};
},
/**
* @description Outputs results to file
* @this reporter
*/
outputToFile: function () {
var outputPath,
data = JSON.stringify(this.errorsObj, null, 4);
if (this.isReporterOutputSet()) {
console.log(data);
} else {
outputPath = path.resolve(process.cwd(), 'jscs-report.json');
fs.writeFileSync(outputPath, data);
console.log('>> jscs report written to', outputPath);
}
},
/**
* @description Checks whether reporter output path is set in `config`.
* `reporterOutput` property can only be set using `grunt-jscs`.
* @returns {Boolean}
* @this reporter
*/
isReporterOutputSet: function () {
return !!(config && config.reporterOutput);
}
};
reporter.report();
};
module.exports.path = __dirname;