invalidScopeDisables.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. const rangeData = stylelintResult.disabledRanges;
  17. const disabledRules = Object.keys(rangeData);
  18. for (const rule of disabledRules) {
  19. if (usedRules.has(rule)) continue;
  20. if (enabled === optionsMatches(options, 'except', rule)) continue;
  21. for (const range of rangeData[rule]) {
  22. if (!range.strictStart && !range.strictEnd) continue;
  23. // If the comment doesn't have a location, we can't report a useful error.
  24. // In practice we expect all comments to have locations, though.
  25. if (!range.comment.source || !range.comment.source.start) continue;
  26. result.warnings.push({
  27. text: `Rule "${rule}" isn't enabled`,
  28. rule: '--report-invalid-scope-disables',
  29. line: range.comment.source.start.line,
  30. column: range.comment.source.start.column,
  31. severity: options.severity,
  32. });
  33. }
  34. }
  35. }
  36. };