New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add report invalid scope disables #4181
Add report invalid scope disables #4181
Conversation
Actually PR looks pretty big (and it will be bigger since need to add more tests), if we ok to split it in different tickets I can do it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CLI should support this option too
/cc @stylelint/core maybe we can merge all invalid comment in one option for CLI for better DX
Thanks, forgot about cli description. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks too complex for me to understand completely :) Tests are passing.
const strict = disabledRuleName === ALL_RULES; | ||
|
||
startDisabledRange(line, disabledRuleName, strict); | ||
endDisabledRange(line, disabledRuleName, strict); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
regarding to strict
flag definition - if ruleName
is all
, then strictStart
/strictEnd
true, otherwise false
startDisabledRange(line, ruleName); | ||
endDisabledRange(line, ruleName); | ||
startDisabledRange(line, ruleName, true); | ||
endDisabledRange(line, ruleName, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
provided ruleName
is not all
, e.g.:
/* stylelint-disable-line my-rule */
so strictStart
/strictEnd
true
startDisabledRange( | ||
comment.source.start.line, | ||
ruleName, | ||
ruleName === ALL_RULES |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
disabling all
rule, so strictStart
true only for all
rules
@@ -116,7 +140,11 @@ module.exports = function( | |||
|
|||
Object.keys(disabledRanges).forEach(ruleName => { | |||
if (!_.get(_.last(disabledRanges[ruleName]), "end")) { | |||
endDisabledRange(comment.source.end.line, ruleName); | |||
endDisabledRange( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for end
const report = []; | ||
const usedRules = new Set(Object.keys(config.rules || {})); | ||
|
||
usedRules.add("all"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setup all used rules Set
(include all
rule)
const disabledRules = Object.keys(rangeData); | ||
|
||
disabledRules.forEach(rule => { | ||
if (usedRules.has(rule)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
skip creating warning for rule that exists in config (include all
rule)
} | ||
|
||
rangeData[rule].forEach((range /*: unusedRangeT */) => { | ||
if (!range.strictStart && !range.strictEnd) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if current range is not strict from start or end, then skip it, e.g:
/* stylelint-disable */ a { color: red} /* stylelint-enable */
/* stylelint-disable color-hex */ a { color: red} /* stylelint-enable */
current code will skip creating warning for color-hex
on line 1
return; | ||
} | ||
|
||
sourceReport.ranges.push({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
creating warning using report-needless-disable
warnings format
@hudochenkov I have clarified some stuff |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see no issues
|
closes #2291
Naive idea of collecting rules that don't exist within the configuration object is pretty straight forward - find all disabled rules that don't exist in configuration using info in
disableRanges
object. However, I faced with problem in tests:and vice verca
In both cases rule
not-in-config
will be disabled twice, soinvalidScopeDisables
will report twice as well. I decided to enhancedisableRange
with 2 flagsstrictStart: boolean
andstrictEnd?: boolean
, that work as describe below:for each rule in
stylelint-disable* <rule>
strictStart
will be truefor each rule in
stylelint-enable* <rule>
strictEnd
will be truefor each rule in
stylelint-disable*
strictStart
will be false and forall
rule will be truefor each rule in
stylelint-enable*
strictEnd
will be false and forall
rule will be truewill result in
I updated
disableRanges
tests, so you can find more examples there.