invalidScopeDisables.js 1.6 KB

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