forked from adrianpietka/jshint-html-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.js
executable file
·127 lines (106 loc) · 3.84 KB
/
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
'use strict';
module.exports = {
reporter: function (result) {
var fs = require('fs');
var path = require('path');
var templates = {
body: '',
item: '',
itemHeader: '',
noItems: '',
summary: ''
};
var numberOfFailures = {
failures: 0,
errors: 0,
warnings: 0
};
function init() {
loadTemplates();
calculateNumberOfFailures();
process.stdout.write(getRenderedHTML());
}
function calculateNumberOfFailures() {
numberOfFailures.failures = result.length;
result.forEach(function (element) {
if (isError(element.error.code)) {
numberOfFailures.errors += 1;
} else {
numberOfFailures.warnings += 1;
}
});
}
function isError(errorCode) {
return errorCode && errorCode[0] === 'E';
}
function loadTemplates() {
var templatePath = path.join(__dirname) + '/templates/bootstrap/';
for (var template in templates) {
templates[template] = fs.readFileSync(templatePath + template + '.html').toString();
}
}
function escapeHtml(string) {
if (!string) {
return string;
}
return ("" + string)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/@/g, '@')
.replace(/\$/g, '$')
.replace(/\(/g, '(')
.replace(/\)/g, ')')
.replace(/\{/g, '{')
.replace(/\}/g, '}')
.replace(/\[/g, '[')
.replace(/\]/g, ']')
.replace(/\+/g, '+')
.replace(/=/g, '=')
.replace(/`/g, '`')
.replace(/\,/g, ',')
.replace(/\!/g, '!')
.replace(/%/g, '%');
}
function prepareContent() {
var content = '';
var previousFile = '';
if (result.length === 0) {
return templates.noItems;
}
result.forEach(function (element) {
var file = element.file;
var error = element.error;
if (previousFile !== file) {
previousFile = file;
content += templates.itemHeader.replace('{file}', escapeHtml(file));
}
content += templates.item
.replace('{class}', isError(error.code) ? 'danger' : 'warning')
.replace('{code}', escapeHtml(error.code))
.replace('{line}', escapeHtml(error.line))
.replace('{character}', escapeHtml(error.character))
.replace('{evidence}', escapeHtml(error.evidence))
.replace('{reason}', escapeHtml(error.reason));
});
return content;
}
function prepareSummary() {
if (!numberOfFailures.failures) {
return '';
}
return templates.summary
.replace('{failures}', escapeHtml(numberOfFailures.failures))
.replace('{errors}', escapeHtml(numberOfFailures.errors))
.replace('{warnings}', escapeHtml(numberOfFailures.warnings));
}
function getRenderedHTML() {
return templates.body
.replace('{content}', prepareContent())
.replace('{summary}', prepareSummary());
}
init();
}
};