-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathpreprocess-warnings.mjs
90 lines (76 loc) · 1.79 KB
/
preprocess-warnings.mjs
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
/**
* @see https://github.com/stylelint/stylelint/blob/cfe2bcff69a4c6ffb2839552a1fec36cf4971895/lib/formatters/preprocessWarnings.mjs
*
* @license MIT https://github.com/stylelint/stylelint/blob/main/LICENSE
*/
/** @import {LintResult} from 'stylelint' */
/** @typedef {LintResult['parseErrors'][0]} ParseError */
/** @typedef {LintResult['warnings'][0]} Warning */
/** @typedef {Warning['severity']} Severity */
/**
* Preprocess warnings in a given lint result.
* Note that this function has a side-effect.
*
* @param {LintResult} result
* @returns {LintResult}
*/
export default function preprocessWarnings(result) {
for (const error of result.parseErrors || []) {
result.warnings.push(parseErrorToWarning(error));
}
for (const warning of result.warnings) {
warning.severity = normalizeSeverity(warning);
}
result.warnings.sort(byLocationOrder);
return result;
}
/**
* @param {ParseError} error
* @returns {Warning}
*/
function parseErrorToWarning(error) {
return {
line: error.line,
column: error.column,
rule: error.stylelintType,
severity: 'error',
text: `${error.text} (${error.stylelintType})`,
};
}
/**
* @param {Warning} warning
* @returns {Severity}
*/
function normalizeSeverity(warning) {
// NOTE: Plugins may add a warning without severity, for example,
// by directly using the PostCSS `Result#warn()` API.
return warning.severity || 'error';
}
/**
* @param {Warning} a
* @param {Warning} b
* @returns {number}
*/
function byLocationOrder(a, b) {
// positionless first
if (!a.line && b.line) {
return -1;
}
// positionless first
if (a.line && !b.line) {
return 1;
}
if (a.line < b.line) {
return -1;
}
if (a.line > b.line) {
return 1;
}
if (a.column < b.column) {
return -1;
}
if (a.column > b.column) {
return 1;
}
return 0;
}