UNPKG

1.98 kBJavaScriptView Raw
1'use strict';
2
3const descriptionlessDisables = require('./descriptionlessDisables');
4const invalidScopeDisables = require('./invalidScopeDisables');
5const needlessDisables = require('./needlessDisables');
6const reportDisables = require('./reportDisables');
7
8/** @typedef {import('stylelint').Formatter} Formatter */
9/** @typedef {import('stylelint').LintResult} StylelintResult */
10/** @typedef {import('stylelint').LinterOptions["maxWarnings"]} maxWarnings */
11/** @typedef {import('stylelint').LinterResult} LinterResult */
12
13/**
14 * @param {StylelintResult[]} stylelintResults
15 * @param {maxWarnings} maxWarnings
16 * @param {Formatter} formatter
17 * @param {string} cwd
18 *
19 * @returns {LinterResult}
20 */
21module.exports = function prepareReturnValue(stylelintResults, maxWarnings, formatter, cwd) {
22 reportDisables(stylelintResults);
23 needlessDisables(stylelintResults);
24 invalidScopeDisables(stylelintResults);
25 descriptionlessDisables(stylelintResults);
26
27 const errored = stylelintResults.some(
28 (result) =>
29 result.errored ||
30 result.parseErrors.length > 0 ||
31 result.warnings.some((warning) => warning.severity === 'error'),
32 );
33
34 /** @type {LinterResult} */
35 const returnValue = {
36 cwd,
37 errored,
38 results: [],
39 output: '',
40 reportedDisables: [],
41 ruleMetadata: getRuleMetadata(stylelintResults),
42 };
43
44 if (maxWarnings !== undefined) {
45 const foundWarnings = stylelintResults.reduce((count, file) => count + file.warnings.length, 0);
46
47 if (foundWarnings > maxWarnings) {
48 returnValue.maxWarningsExceeded = { maxWarnings, foundWarnings };
49 }
50 }
51
52 returnValue.output = formatter(stylelintResults, returnValue);
53 returnValue.results = stylelintResults;
54
55 return returnValue;
56};
57
58/**
59 * @param {StylelintResult[]} lintResults
60 */
61function getRuleMetadata(lintResults) {
62 const [lintResult] = lintResults;
63
64 if (lintResult === undefined) return {};
65
66 if (lintResult._postcssResult === undefined) return {};
67
68 return lintResult._postcssResult.stylelint.ruleMetadata;
69}