This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
checkstyle-reporter.js
66 lines (57 loc) · 1.7 KB
/
checkstyle-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
'use strict';
var fmt = require('util').format;
// ==== begin attribution =====================================================
// The pairs object and encode function are borrowed from:
// github.com/jshint/jshint src/reporters/checkstyle.js
// Author: Boy Baukema, http://github.com/relaxnow
// License MIT
// This code should be replaced in a future commit of checkstyle-reporter.js
var pairs = {
'&': '&',
'"': '"',
'\'': ''',
'<': '<',
'>': '>'
};
function encode(s) {
for (var r in pairs) {
if (typeof s !== 'undefined') {
s = s.replace(new RegExp(r, 'g'), pairs[r]);
}
}
return s || '';
}
// ==== end attribution =======================================================
function printCheckstyle(fileMessages) {
fileMessages = fileMessages.filter(removeEmpty);
function removeEmpty(fileMessage) {
return fileMessage.errors.length > 0;
}
return [
'<?xml version="1.0" encoding="utf-8"?>',
'<checkstyle version="4.3">',
fileMessages.map(printFile).join('\n'),
'</checkstyle>'
].join('\n') + '\n';
}
function printFile(message) {
return fmt('\t<file name="%s">\n%s\n\t</file>',
encode(message.file),
printErrors(message)
);
}
function printErrors(message) {
return message.errors.map(printError).join('\n');
}
function printError(message) {
return fmt(
'\t\t<error line="%d" column="%d" severity="%s" ' +
'message="%s" source="%s" />',
message.line,
message.column,
message.type,
encode(message.message),
encode([message.linter, message.rule].join('.'))
);
}
module.exports = printCheckstyle;