1 | ;
|
2 |
|
3 | const validateOptions = require('./utils/validateOptions');
|
4 | const { isRegExp, isString } = require('./utils/validateTypes');
|
5 |
|
6 | /**
|
7 | * @typedef {import('stylelint').PostcssResult} PostcssResult
|
8 | * @typedef {import('stylelint').DisableOptions} DisableOptions
|
9 | * @typedef {import('stylelint').DisablePropertyName} DisablePropertyName
|
10 | * @typedef {import('stylelint').StylelintPostcssResult} StylelintPostcssResult
|
11 | */
|
12 |
|
13 | /**
|
14 | * Validates that the stylelint config for `result` has a valid disable field
|
15 | * named `field`, and returns the result in normalized form as well as a
|
16 | * `StylelintPostcssResult` for convenience.
|
17 | *
|
18 | * Returns `null` if no disables should be reported, and automatically reports
|
19 | * an invalid configuration. If this returns non-`null`, it guarantees that
|
20 | * `result._postcssResult` is defined as well.
|
21 | *
|
22 | * @param {PostcssResult | undefined} result
|
23 | * @param {DisablePropertyName} field
|
24 | * @return {[boolean, Required<DisableOptions>, StylelintPostcssResult] | null}
|
25 | */
|
26 | module.exports = function (result, field) {
|
27 | // Files with `CssSyntaxError`s don't have `_postcssResult`s.
|
28 | if (!result) return null;
|
29 |
|
30 | const stylelintResult = result.stylelint;
|
31 |
|
32 | // Files with linting errors may not have configs associated with them.
|
33 | if (!stylelintResult.config) return null;
|
34 |
|
35 | const rawSettings = stylelintResult.config[field];
|
36 |
|
37 | /** @type {boolean} */
|
38 | let enabled;
|
39 | /** @type {DisableOptions} */
|
40 | let options;
|
41 |
|
42 | if (Array.isArray(rawSettings)) {
|
43 | enabled = rawSettings[0];
|
44 | options = rawSettings[1] || {};
|
45 | } else {
|
46 | enabled = rawSettings || false;
|
47 | options = {};
|
48 | }
|
49 |
|
50 | const validOptions = validateOptions(
|
51 | result,
|
52 | field,
|
53 | {
|
54 | actual: enabled,
|
55 | possible: [true, false],
|
56 | },
|
57 | {
|
58 | actual: options,
|
59 | possible: {
|
60 | except: [isString, isRegExp],
|
61 | },
|
62 | },
|
63 | );
|
64 |
|
65 | if (!validOptions) return null;
|
66 |
|
67 | // If the check is disabled with no exceptions, there's no reason to run
|
68 | // it at all.
|
69 | if (!enabled && !options.except) return null;
|
70 |
|
71 | return [
|
72 | enabled,
|
73 | {
|
74 | except: options.except || [],
|
75 | severity: options.severity || stylelintResult.config.defaultSeverity || 'error',
|
76 | },
|
77 | stylelintResult,
|
78 | ];
|
79 | };
|