-
-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix report flags not reporting on subsequent runs when cache is used (#…
…7483) The bug's reason is that the `stylelintError`/`stylelintWarning` properties in a lint result object are not set when `styelint-disable` comment problems are reported. Also, this change refactors the code around `styelint-disable` comments to make it more readable and prevent possible bugs. The new code uses [`Result#warn()`](https://postcss.org/api/#result-warn) in PostCSS for a comment node, like reporting a normal rule problem.
- Loading branch information
1 parent
f02d168
commit 382961f
Showing
18 changed files
with
411 additions
and
400 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"stylelint": patch | ||
--- | ||
|
||
Fixed: report flags not reporting on subsequent runs when cache is used |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,49 @@ | ||
import optionsMatches from './utils/optionsMatches.mjs'; | ||
import reportCommentProblem from './utils/reportCommentProblem.mjs'; | ||
import validateDisableSettings from './validateDisableSettings.mjs'; | ||
|
||
/** @typedef {import('postcss').Comment} PostcssComment */ | ||
/** @typedef {import('stylelint').DisableReportRange} DisableReportRange */ | ||
/** @typedef {import('stylelint').DisableOptionsReport} StylelintDisableOptionsReport */ | ||
|
||
/** | ||
* @param {import('stylelint').LintResult[]} results | ||
* @param {import('stylelint').PostcssResult} postcssResult | ||
* @returns {void} | ||
*/ | ||
export default function descriptionlessDisables(results) { | ||
for (const result of results) { | ||
const settings = validateDisableSettings( | ||
result._postcssResult, | ||
'reportDescriptionlessDisables', | ||
); | ||
|
||
if (!settings) continue; | ||
|
||
const [enabled, options, stylelintResult] = settings; | ||
|
||
/** @type {Set<PostcssComment>} */ | ||
const alreadyReported = new Set(); | ||
|
||
for (const [rule, ruleRanges] of Object.entries(stylelintResult.disabledRanges)) { | ||
for (const range of ruleRanges) { | ||
if (range.description) continue; | ||
|
||
if (alreadyReported.has(range.comment)) continue; | ||
|
||
if (enabled === optionsMatches(options, 'except', rule)) { | ||
// An 'all' rule will get copied for each individual rule. If the | ||
// configuration is `[false, {except: ['specific-rule']}]`, we | ||
// don't want to report the copies that match except, so we record | ||
// the comment as already reported. | ||
if (!enabled && rule === 'all') alreadyReported.add(range.comment); | ||
|
||
continue; | ||
} | ||
|
||
alreadyReported.add(range.comment); | ||
|
||
// If the comment doesn't have a location, we can't report a useful error. | ||
// In practice we expect all comments to have locations, though. | ||
if (!range.comment.source || !range.comment.source.start) continue; | ||
|
||
result.warnings.push({ | ||
text: `Disable for "${rule}" is missing a description`, | ||
rule: '--report-descriptionless-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, | ||
}); | ||
export default function descriptionlessDisables(postcssResult) { | ||
const [enabled, options] = validateDisableSettings( | ||
postcssResult, | ||
'reportDescriptionlessDisables', | ||
); | ||
|
||
if (!options) return; | ||
|
||
/** @type {Set<import('postcss').Comment>} */ | ||
const alreadyReported = new Set(); | ||
|
||
for (const [rule, ruleRanges] of Object.entries(postcssResult.stylelint.disabledRanges)) { | ||
for (const range of ruleRanges) { | ||
if (range.description) continue; | ||
|
||
const commentNode = range.comment; | ||
|
||
if (alreadyReported.has(commentNode)) continue; | ||
|
||
if (enabled === optionsMatches(options, 'except', rule)) { | ||
// An 'all' rule will get copied for each individual rule. If the | ||
// configuration is `[false, {except: ['specific-rule']}]`, we | ||
// don't want to report the copies that match except, so we record | ||
// the comment as already reported. | ||
if (!enabled && rule === 'all') alreadyReported.add(commentNode); | ||
|
||
continue; | ||
} | ||
|
||
alreadyReported.add(commentNode); | ||
|
||
reportCommentProblem({ | ||
rule: '--report-descriptionless-disables', | ||
message: `Disable for "${rule}" is missing a description`, | ||
severity: options.severity, | ||
commentNode, | ||
postcssResult, | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,39 @@ | ||
import optionsMatches from './utils/optionsMatches.mjs'; | ||
import reportCommentProblem from './utils/reportCommentProblem.mjs'; | ||
import validateDisableSettings from './validateDisableSettings.mjs'; | ||
|
||
/** | ||
* @param {import('stylelint').LintResult[]} results | ||
* @param {import('stylelint').PostcssResult} postcssResult | ||
* @returns {void} | ||
*/ | ||
export default function invalidScopeDisables(results) { | ||
for (const result of results) { | ||
const settings = validateDisableSettings(result._postcssResult, 'reportInvalidScopeDisables'); | ||
export default function invalidScopeDisables(postcssResult) { | ||
const [enabled, options] = validateDisableSettings(postcssResult, 'reportInvalidScopeDisables'); | ||
|
||
if (!settings) continue; | ||
if (!options) return; | ||
|
||
const [enabled, options, stylelintResult] = settings; | ||
const configRules = postcssResult.stylelint.config?.rules; | ||
|
||
const configRules = (stylelintResult.config || {}).rules || {}; | ||
if (!configRules) return; | ||
|
||
const usedRules = new Set(Object.keys(configRules)); | ||
const usedRules = new Set(Object.keys(configRules)); | ||
|
||
usedRules.add('all'); | ||
usedRules.add('all'); | ||
|
||
for (const [rule, ruleRanges] of Object.entries(stylelintResult.disabledRanges)) { | ||
if (usedRules.has(rule)) continue; | ||
for (const [rule, ruleRanges] of Object.entries(postcssResult.stylelint.disabledRanges)) { | ||
if (usedRules.has(rule)) continue; | ||
|
||
if (enabled === optionsMatches(options, 'except', rule)) continue; | ||
if (enabled === optionsMatches(options, 'except', rule)) continue; | ||
|
||
for (const range of ruleRanges) { | ||
if (!range.strictStart && !range.strictEnd) continue; | ||
for (const range of ruleRanges) { | ||
if (!range.strictStart && !range.strictEnd) continue; | ||
|
||
// If the comment doesn't have a location, we can't report a useful error. | ||
// In practice we expect all comments to have locations, though. | ||
if (!range.comment.source || !range.comment.source.start) continue; | ||
|
||
result.warnings.push({ | ||
text: `Rule "${rule}" isn't enabled`, | ||
rule: '--report-invalid-scope-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, | ||
}); | ||
} | ||
reportCommentProblem({ | ||
rule: '--report-invalid-scope-disables', | ||
message: `Rule "${rule}" isn't enabled`, | ||
severity: options.severity, | ||
commentNode: range.comment, | ||
postcssResult, | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.