123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 'use strict';
- const optionsMatches = require('./utils/optionsMatches');
- const putIfAbsent = require('./utils/putIfAbsent');
- const validateDisableSettings = require('./validateDisableSettings');
- module.exports = function needlessDisables(results) {
- for (const result of results) {
- const settings = validateDisableSettings(result._postcssResult, 'reportNeedlessDisables');
- if (!settings) continue;
- const [enabled, options, stylelintResult] = settings;
- const rangeData = stylelintResult.disabledRanges;
- if (!rangeData) continue;
- const disabledWarnings = stylelintResult.disabledWarnings || [];
-
-
-
-
-
-
- const usefulDisables = new Map();
- for (const warning of disabledWarnings) {
- const rule = warning.rule;
- const ruleRanges = rangeData[rule];
- if (ruleRanges) {
- for (const range of ruleRanges) {
- if (isWarningInRange(warning, range)) {
- putIfAbsent(usefulDisables, range.comment, () => new Set()).add(rule);
- }
- }
- }
- for (const range of rangeData.all || []) {
- if (isWarningInRange(warning, range)) {
- putIfAbsent(usefulDisables, range.comment, () => new Set()).add(rule);
- }
- }
- }
- const allRangeComments = new Set((rangeData.all || []).map((range) => range.comment));
- for (const [rule, ranges] of Object.entries(rangeData)) {
- for (const range of ranges) {
- if (rule !== 'all' && allRangeComments.has(range.comment)) continue;
- if (enabled === optionsMatches(options, 'except', rule)) continue;
- const useful = usefulDisables.get(range.comment) || new Set();
-
-
-
- if (rule === 'all' ? useful.size !== 0 : useful.has(rule)) continue;
-
-
- if (!range.comment.source || !range.comment.source.start) continue;
- result.warnings.push({
- text: `Needless disable for "${rule}"`,
- rule: '--report-needless-disables',
- line: range.comment.source.start.line,
- column: range.comment.source.start.column,
- endLine: range.comment.source.end && range.comment.source.end.line,
- endColumn: range.comment.source.end && range.comment.source.end.column,
- severity: options.severity,
- });
- }
- }
- }
- };
- function isWarningInRange(warning, range) {
- const line = warning.line;
-
- return (
- range.start <= line &&
- ((range.end !== undefined && range.end >= line) || range.end === undefined)
- );
- }
|