-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
131 lines (117 loc) · 2.99 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
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
/* @flow */
const crypto = require('crypto');
/*::
type FlowReport = {
flowVersion: string,
errors: ErrorReport[],
};
type ErrorReport = {
kind: 'infer' | 'parse' | string,
level: 'error' | 'warning',
suppressions: any[],
message: Message[],
extra?: ExtraReport[],
};
type ExtraReport = {
message: Message[],
children?: ExtraReport[],
}
type Message = BlameMessage | CommentMessage;
type BlameMessage = {
type: 'Blame',
descr: string,
context: string,
line: number,
endline: number,
path: string,
start: number,
end: number,
};
type CommentMessage = {
type: 'Comment',
descr: string,
context: null,
line: number,
endline: number,
path: string,
start: number,
end: number,
}
*/
function errorToString(error /*: ErrorReport */) /*: string */ {
const title = error.message.map(message => message.descr).join(' ');
let message = `Error: ${title}
${error.message.map(messageToString).join('\n\n')}`;
if (error.extra) {
message += '\n\nExtra information - ';
message += error.extra.map(extraToString).join('\n');
}
return message;
}
function extraToString(extra /*: ExtraReport */) /*: string */ {
let message = extra.message.map(messageToString).join('\n\n');
if (extra.children) {
return message + '\n\n' + extra.children.map(extraToString).join('\n\n');
}
return message;
}
function messageToString(message /*: Message */) /*: string */ {
if (message.context !== null) {
return blameToString(message);
}
return commentToString(message);
}
function blameToString(blame /*: BlameMessage */) /*: string */ {
let location = '';
if ((blame.context, blame.end - blame.start + 1 > 0)) {
location = '^'.repeat(blame.end - blame.start + 1);
}
return (
blame.context + '\n' + ' '.repeat(Math.max(0, blame.start - 1)) + location + ' ' + blame.descr
);
}
function commentToString(comment /*: CommentMessage */) /*: string */ {
return comment.descr;
}
module.exports = function flowJUnitTransformer(input /*: FlowReport */) {
const errors = input.errors.map(error => {
const context = error.message[0].context;
const title = error.message.map(message => message.descr).join(' ');
const message = errorToString(error);
const hash = crypto
.createHash('sha256')
.update(message)
.digest('hex')
.substr(0, 16);
return `
<testcase
time="0"
classname="org.flow.${error.level.toLowerCase()}.${hash}"
id="flow-error"
name="org.flow.${error.level.toLowerCase()}.${hash}"
>
<failure
type="${error.level.toUpperCase()}"
message="${title}"
>
<![CDATA[
${message}
]]>
</failure>
</testcase>`;
});
return `<?xml version="1.0" encoding="UTF-8" ?>
<testsuites id="flow">
<testsuite
package="org.flow"
tests="${errors.length}"
time="0"
skipped="0"
errors="${errors.length}"
id="flow-suite"
name="Flow type check"
>${errors.join('')}
</testsuite>
</testsuites>
`;
};