Skip to content
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

Merged

Conversation

vankop
Copy link
Member

@vankop vankop commented Jul 30, 2019

Which issue, if any, is this issue related to?

closes #2291

Is there anything in the PR that needs further explanation?

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:

/* stylelint-disable not-in-config */ a {} /* stylelint-enable */
/* stylelint-disable */ b {} /* stylelint-enable */

and vice verca

/* stylelint-disable */ b {} /* stylelint-enable */
/* stylelint-disable not-in-config */ a {} /* stylelint-enable */

In both cases rule not-in-config will be disabled twice, so invalidScopeDisables will report twice as well. I decided to enhance disableRange with 2 flags strictStart: boolean and strictEnd?: boolean, that work as describe below:

for each rule in stylelint-disable* <rule> strictStart will be true
for each rule in stylelint-enable* <rule> strictEnd will be true
for each rule in stylelint-disable* strictStart will be false and for all rule will be true
for each rule in stylelint-enable* strictEnd will be false and for all rule will be true

/* stylelint-disable not-in-config */ a {} /* stylelint-enable */ (1)
/* stylelint-disable */ b {} /* stylelint-enable */ (2)

will result in

{
    all: [{start: 2, end: 2, strictStart: true, strictEnd: true}],
    "not-in-config": [{start: 1, end: 1, strictStart: true, strictEnd: false}, {start: 2, end: 2, strictStart: false, strictEnd: false}]
}

I updated disableRanges tests, so you can find more examples there.

@vankop
Copy link
Member Author

vankop commented Jul 30, 2019

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

Copy link
Member

@alexander-akait alexander-akait left a 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

@vankop
Copy link
Member Author

vankop commented Jul 30, 2019

CLI should support this option too

Thanks, forgot about cli description.

@hudochenkov
Copy link
Member

hudochenkov commented Jul 31, 2019

Error might be caused by #4174 change. And/or this change #4151.

@vankop vankop mentioned this pull request Sep 3, 2019
6 tasks
Copy link
Member

@hudochenkov hudochenkov left a 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);
Copy link
Member Author

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);
Copy link
Member Author

@vankop vankop Sep 4, 2019

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
Copy link
Member Author

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(
Copy link
Member Author

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");
Copy link
Member Author

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)) {
Copy link
Member Author

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) {
Copy link
Member Author

@vankop vankop Sep 4, 2019

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({
Copy link
Member Author

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

@vankop
Copy link
Member Author

vankop commented Sep 4, 2019

@hudochenkov I have clarified some stuff

Copy link
Member

@ntwb ntwb left a 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

@hudochenkov hudochenkov merged commit 862b44f into stylelint:master Sep 9, 2019
@hudochenkov
Copy link
Member

  • Added: --reportInvalidScopeDisables CLI flag (#4181).

@vankop vankop deleted the add-report-invalid-scope-disables branch September 9, 2019 08:52
vankop added a commit that referenced this pull request Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Add reportInvalidScopeDisables flag
4 participants