UNPKG

2.14 kBJavaScriptView Raw
1'use strict';
2
3/** @typedef {import('stylelint').RangeType} RangeType */
4/** @typedef {import('stylelint').DisableReportRange} DisabledRange */
5/** @typedef {import('stylelint').LintResult} StylelintResult */
6/** @typedef {import('stylelint').ConfigRuleSettings<any, Object>} StylelintConfigRuleSettings */
7
8/**
9 * Returns a report describing which `results` (if any) contain disabled ranges
10 * for rules that disallow disables via `reportDisables: true`.
11 *
12 * @param {StylelintResult[]} results
13 */
14module.exports = function (results) {
15 for (const result of results) {
16 // File with `CssSyntaxError` don't have `_postcssResult`s.
17 if (!result._postcssResult) {
18 continue;
19 }
20
21 /** @type {{[ruleName: string]: Array<RangeType>}} */
22 const rangeData = result._postcssResult.stylelint.disabledRanges;
23
24 if (!rangeData) continue;
25
26 const config = result._postcssResult.stylelint.config;
27
28 if (!config || !config.rules) continue;
29
30 // If no rules actually disallow disables, don't bother looking for ranges
31 // that correspond to disabled rules.
32 if (!Object.values(config.rules).some((rule) => reportDisablesForRule(rule))) {
33 continue;
34 }
35
36 for (const [rule, ranges] of Object.entries(rangeData)) {
37 for (const range of ranges) {
38 if (!reportDisablesForRule(config.rules[rule] || [])) continue;
39
40 // If the comment doesn't have a location, we can't report a useful error.
41 // In practice we expect all comments to have locations, though.
42 if (!range.comment.source || !range.comment.source.start) continue;
43
44 result.warnings.push({
45 text: `Rule "${rule}" may not be disabled`,
46 rule: 'reportDisables',
47 line: range.comment.source.start.line,
48 column: range.comment.source.start.column,
49 endLine: range.comment.source.end && range.comment.source.end.line,
50 endColumn: range.comment.source.end && range.comment.source.end.column,
51 severity: 'error',
52 });
53 }
54 }
55 }
56};
57
58/**
59 * @param {StylelintConfigRuleSettings} options
60 * @return {boolean}
61 */
62function reportDisablesForRule(options) {
63 if (!options || !options[1]) return false;
64
65 return Boolean(options[1].reportDisables);
66}