forked from davidtheclark/stylelint-checkstyle-formatter
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
48 lines (42 loc) · 1.16 KB
/
index.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
const xmlBuilder = require('xmlbuilder');
function parseFailedCase(testCase, source) {
const {
rule,
severity,
text,
line,
column,
} = testCase;
return {
'@name': rule,
failure: {
'@type': severity,
'@message': text,
'#text': `On line ${line}, column ${column} in ${source}`,
},
};
}
function parseSuite(testSuite) {
const suiteName = testSuite.source;
const failuresCount = testSuite.warnings.length;
const testCases = testSuite.errored
? testSuite.warnings.map((testCase) => parseFailedCase(testCase, suiteName))
: { '@name': 'stylelint.passed' };
return {
testsuite: {
'@name': suiteName,
'@failures': failuresCount,
'@errors': failuresCount,
'@tests': failuresCount || '1',
testcase: testCases,
},
};
}
module.exports = (stylelintResults) => {
const xmlRoot = xmlBuilder.create('testsuites', { encoding: 'utf-8' })
.att('package', 'stylelint.rules');
const testSuites = stylelintResults.map((testSuite) => parseSuite(testSuite));
return testSuites.length > 0
? xmlRoot.element(testSuites).end({ pretty: true })
: xmlRoot.end({ pretty: true });
};