UNPKG

1.52 kBJavaScriptView Raw
1'use strict';
2
3const optionsMatches = require('./utils/optionsMatches');
4const validateDisableSettings = require('./validateDisableSettings');
5
6/** @typedef {import('stylelint').RangeType} RangeType */
7
8/**
9 * @param {import('stylelint').LintResult[]} results
10 */
11module.exports = function invalidScopeDisables(results) {
12 for (const result of results) {
13 const settings = validateDisableSettings(result._postcssResult, 'reportInvalidScopeDisables');
14
15 if (!settings) continue;
16
17 const [enabled, options, stylelintResult] = settings;
18
19 const configRules = (stylelintResult.config || {}).rules || {};
20
21 const usedRules = new Set(Object.keys(configRules));
22
23 usedRules.add('all');
24
25 const rangeData = stylelintResult.disabledRanges;
26 const disabledRules = Object.keys(rangeData);
27
28 for (const rule of disabledRules) {
29 if (usedRules.has(rule)) continue;
30
31 if (enabled === optionsMatches(options, 'except', rule)) continue;
32
33 for (const range of rangeData[rule]) {
34 if (!range.strictStart && !range.strictEnd) continue;
35
36 // If the comment doesn't have a location, we can't report a useful error.
37 // In practice we expect all comments to have locations, though.
38 if (!range.comment.source || !range.comment.source.start) continue;
39
40 result.warnings.push({
41 text: `Rule "${rule}" isn't enabled`,
42 rule: '--report-invalid-scope-disables',
43 line: range.comment.source.start.line,
44 column: range.comment.source.start.column,
45 severity: options.severity,
46 });
47 }
48 }
49 }
50};