-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeedbackMessage.js
80 lines (65 loc) · 2.59 KB
/
feedbackMessage.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
const buildFeedbackMessage = (stylelintOutcomes, root) => {
const { issues, errorCount, warningCount } = extractStylelintIssues(stylelintOutcomes);
const { errorIssues, warningIssues } = splitIssues(issues, root);
let feedbackMessage = [];
feedbackMessage.push(feedbackTitleMessage('erro', errorCount));
feedbackMessage = addIssueLineMessage(feedbackMessage, errorIssues, true);
feedbackMessage.push(feedbackTitleMessage('aviso', warningCount));
feedbackMessage = addIssueLineMessage(feedbackMessage, warningIssues);
return feedbackMessage.join('\n');
};
const extractStylelintIssues = (stylelintOutcomes) => {
return stylelintOutcomes.reduce((total, currentValue) => {
const { source, warnings } = currentValue;
let issues = {
source,
error: [],
warning: [],
};
warnings.forEach(({ line, column, severity, text }) => (
issues[severity].push({ line, column, text })
));
return {
issues: total.issues.concat(issues),
errorCount: total.errorCount + issues.error.length,
warningCount: total.warningCount + issues.warning.length,
};
}, { issues: [], errorCount: 0, warningCount: 0 });
};
const splitIssues = (issues, root) => {
return issues.reduce((total, currentValue) => {
const { source, error, warning } = currentValue;
const errorIssues = buildIssuesMessage(source, error, root);
const warningIssues = buildIssuesMessage(source, warning, root);
return {
errorIssues: total.errorIssues.concat(...errorIssues),
warningIssues: total.warningIssues.concat(...warningIssues),
};
}, { errorIssues: [], warningIssues: [] });
};
const buildIssuesMessage = (source, issues, root) => {
const messages = [];
if (issues.length > 0) {
messages.push(buildFileSection(source, root));
issues.map(({ line, text }) => messages.push(buildDetailedMessage(line, text)));
}
return [...messages];
};
const buildFileSection = (filePath, root) => {
const relativePathFile = filePath.replace(root, '');
return `#### Arquivo \`${relativePathFile}\`\n`;
};
const buildDetailedMessage = (line, message) => `- Linha **${line}**: ${message}`;
const feedbackTitleMessage = (issueType, count) => {
if (count === 0) return `### Nenhum ${issueType} encontrado.`;
if (count === 1) return `### Foi encontrado 1 ${issueType}.\n`;
return `### Foram encontrados ${count} ${issueType}s.\n`;
};
const addIssueLineMessage = (messages, issues, newline = false) => {
if (issues.length > 0) {
messages.push(...issues);
if (newline) messages.push('');
}
return [...messages];
};
module.exports = buildFeedbackMessage;