-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
prepareReturnValue.js
75 lines (61 loc) · 2 KB
/
prepareReturnValue.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
'use strict';
const descriptionlessDisables = require('./descriptionlessDisables');
const invalidScopeDisables = require('./invalidScopeDisables');
const needlessDisables = require('./needlessDisables');
const reportDisables = require('./reportDisables');
/** @typedef {import('stylelint').Formatter} Formatter */
/** @typedef {import('stylelint').LintResult} StylelintResult */
/** @typedef {import('stylelint').LinterOptions["maxWarnings"]} maxWarnings */
/** @typedef {import('stylelint').LinterResult} LinterResult */
/**
* @param {StylelintResult[]} stylelintResults
* @param {maxWarnings} maxWarnings
* @param {Formatter} formatter
* @param {string} cwd
*
* @returns {LinterResult}
*/
module.exports = function prepareReturnValue(stylelintResults, maxWarnings, formatter, cwd) {
reportDisables(stylelintResults);
needlessDisables(stylelintResults);
invalidScopeDisables(stylelintResults);
descriptionlessDisables(stylelintResults);
let errored = false;
for (const result of stylelintResults) {
if (
result.errored ||
result.parseErrors.length > 0 ||
result.warnings.some((warning) => warning.severity === 'error')
) {
errored = true;
result.errored = true;
}
}
/** @type {LinterResult} */
const returnValue = {
cwd,
errored,
results: [],
output: '',
reportedDisables: [],
ruleMetadata: getRuleMetadata(stylelintResults),
};
if (maxWarnings !== undefined) {
const foundWarnings = stylelintResults.reduce((count, file) => count + file.warnings.length, 0);
if (foundWarnings > maxWarnings) {
returnValue.maxWarningsExceeded = { maxWarnings, foundWarnings };
}
}
returnValue.output = formatter(stylelintResults, returnValue);
returnValue.results = stylelintResults;
return returnValue;
};
/**
* @param {StylelintResult[]} lintResults
*/
function getRuleMetadata(lintResults) {
const [lintResult] = lintResults;
if (lintResult === undefined) return {};
if (lintResult._postcssResult === undefined) return {};
return lintResult._postcssResult.stylelint.ruleMetadata;
}