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 stylelint-disable commands support to autofix #2643

Open
dan-gamble opened this issue Jun 15, 2017 · 24 comments
Open

Add stylelint-disable commands support to autofix #2643

dan-gamble opened this issue Jun 15, 2017 · 24 comments
Labels
help wanted is likely non-trival and help is wanted priority: high is impactful on users status: ready to implement is ready to be worked on by someone type: enhancement a new feature that isn't related to rules

Comments

@dan-gamble
Copy link
Contributor

Describe the issue. Is it a bug or a feature request (new rule, new option, etc.)?

I have a file that has /* stylelint-disable */ /* stylelint-enable */ wrapped around it but when using --fix it tries to format this file and breaks it quite bad.

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

--fix

What CSS is needed to reproduce this issue?

Before
After

A few of the issues:

  • Double ;; on L38
  • No ; on L92 (I imagine this is where the ;; comes from

What stylelint configuration is needed to reproduce this issue?

Config

The config is pretty huge but i don't think it's the problem here

Which version of stylelint are you using?

7.11.0

How are you running stylelint: CLI, PostCSS plugin, Node API?

CLI through lint-staged

@alexander-akait alexander-akait added status: ready to implement is ready to be worked on by someone type: bug a problem with a feature or rule labels Jun 15, 2017
@alexander-akait
Copy link
Member

I think it is bug, should be respected by default.

@dan-gamble
Copy link
Contributor Author

I think it also ignore ignoreFiles too fwiw.

@hudochenkov
Copy link
Member

I know why stylelint-disable doesn't work with --fix. stylelint creates disabledRanges while processing style sheet. While linting, stylelint doesn't care about this ranges. They only matter, when stylelint generates report. It compares each violation and deletes violations within disabledRange.

I have no idea what to do with --fix and disabled ranges yet :(

@alexander-akait
Copy link
Member

@hudochenkov maybe create util isStylelintDisable and check on this before fix in rule?

@jeddy3 jeddy3 added type: enhancement a new feature that isn't related to rules and removed type: bug a problem with a feature or rule labels Jun 20, 2017
@jeddy3 jeddy3 changed the title Have autofix respect stylelint-disable Autofix should respect stylelint-disable commands Jun 20, 2017
@hudochenkov
Copy link
Member

I came up to another problem. Disabled ranges assigned before file linted/fixed. For rules which altered lines (change empty lines, add line endings), disabled ranges might be incorrect after few fixes. Even after fixes within one rule.

This problem affects every rule. Because if user has rule with fixing which changes lines, then every rule after this rule will have incorrect disabled ranges.

@jeddy3
Copy link
Member

jeddy3 commented Jun 20, 2017

@hudochenkov Thanks for digging deeper. I suspect there are going to be some significant hurdles to overcome with this issue. As such, I suggest we push on with 8.0.0 and revisit this later. The autofix feature is marked as experimental and support for disable-ranges can be seen as a feature we'd like add, rather than a showstopping bug.

@hudochenkov
Copy link
Member

I suspect there are going to be some significant hurdles to overcome with this issue. As such, I suggest we push on with 8.0.0 and revisit this later.

Agree. We should not close this issue, to not forget about this problem.

The autofix feature is marked as experimental and support for disable-ranges can be seen as a feature we'd like add, rather than a showstopping bug.

I think we should add a note somewhere about this caveat. And suggested strategy: if user is using stylelint --fix and stylelint-disable* comments, then stylelint should be run twice. First run there might be false violations (or ignored violations), because of shifted disable ranges. On second run all results would be correct. Also worth adding, that this strategy is for correct violation reports. --fix will fix everything regardless disabled ranges.

@alexander-akait
Copy link
Member

@hudochenkov can we learn how it does eslint?

@hudochenkov
Copy link
Member

@evilebottnawi of course we can. But I have a feeling that it won't help us, because of differences in architecture and approaches. Also I remember something from discussions, when we were discussing how to do autofixing. ESLint make multiple runs until everything is fixed and violations verified. And it also has some ranges for autofixing or something, that recalculated constantly. I might be mistaken about the last one.

@zkuzmic
Copy link

zkuzmic commented Sep 25, 2018

Any updates on this? Are you still looking for some help here?

@jeddy3
Copy link
Member

jeddy3 commented Sep 27, 2018

Are you still looking for some help here?

@zkuzmic Yes, definitely. The issue has been labeled as "help wanted" for a while, but I don't think anyone has had time to pick it up as it's quite gnarly. Is it something you can help with?

@zkuzmic
Copy link

zkuzmic commented Sep 27, 2018

I'd be happy to look into it at least!

chmanie pushed a commit to JoinColony/colonyDapp that referenced this issue Oct 18, 2018
Stylelint also fix-breaks calc() functions as it removes units of values that need it (see stylelint/stylelint#3037). It also auto-fixes it (and hence breaks it then): stylelint/stylelint#2643, so we have to disable the whole rule for now. In order to be able to add comments to the stylelint config I moved it to a .js file.
@SharakPL
Copy link

SharakPL commented Jul 28, 2021

Since 13.2.0 stylelint no longer attempts to autofix sources which contain /* stylelint-disable */ comments, making the autofix feature safer to use.

However, the underlying issue still remains.

@stof's suggestion sounds viable:

instead of mutating the AST immediately, they would report with their fixer included, and the runner would then call that callback after checking the disabled ranges.

I believe the next step is a proof-of-concept (PoC) pull request showing this working with a rule or two. Once we are happy with the concept we can rally to migrate the 81 autofixable rules in stylelint to report with their fixer included.

If anyone has time, please consider contributing this PoC.

It works but it should also fix styles before /* stylelint-disable */ and after /* stylelint-enable */. For now it's just linting 😓

stylelint-disable-fix-bug

@stof
Copy link
Contributor

stof commented Nov 23, 2021

It works but it should also fix styles before /* stylelint-disable */ and after /* stylelint-enable */. For now it's just linting

Respecting these comments is precisely what requires building the new suggested API to provides a fixer callback instead of running the fixing logic directly in the rule, so that the fixing logic could be run only when rules are not ignored.

Copy link
Contributor

This issue is older than one month. Please ask before opening a pull request, as it may no longer be relevant.

@github-actions github-actions bot added status: ask to implement ask before implementing as may no longer be relevant and removed status: ready to implement is ready to be worked on by someone labels Jan 22, 2024
@Mouvedia
Copy link
Contributor

instead of mutating the AST immediately, they would report with their fixer included, and the runner would then call that callback after checking the disabled ranges.

I believe the next step is a proof-of-concept (PoC) pull request showing this working with a rule or two. Once we are happy with the concept we can rally to migrate the 81 autofixable rules in stylelint to report with their fixer included.

If anyone has time, please consider contributing this PoC.

Iv refactored a rule to do just that.
@jeddy3 before I go further with the next step which is

and the runner would then call that callback after checking the disabled ranges

is this issue ready to implement?

@Mouvedia
Copy link
Contributor

@ybiquitous is this issue ready to be implemented?

@ybiquitous
Copy link
Member

Yes, I think creating a PoC has no problem. We could evaluate this feature by the PoC.

@Miofly
Copy link

Miofly commented Apr 28, 2024

how to resolve this problem

@Mouvedia
Copy link
Contributor

Mouvedia commented May 1, 2024

Yes, I think creating a PoC has no problem. We could evaluate this feature by the PoC.

Sorry to be insistent, but I won't make the same mistake thrice.
Is this ready to implement?

@ybiquitous ybiquitous added status: ready to implement is ready to be worked on by someone and removed status: ask to implement ask before implementing as may no longer be relevant labels May 1, 2024
@ybiquitous
Copy link
Member

Yes, this is ready to implement. To clarify this problem, I just created a reproducible demo:

a {
	/* stylelint-disable */
	width: 0px;
	/* stylelint-enable */
	height: 0px;
/*           ^^ this "px" unit should be removed by autofix, but it isn't actually.
 */
}
// .stylelintrc.json
{
  "rules": {
    "length-zero-no-unit": true
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted is likely non-trival and help is wanted priority: high is impactful on users status: ready to implement is ready to be worked on by someone type: enhancement a new feature that isn't related to rules
Development

No branches or pull requests