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

Fix declaration-block-no-redundant-longhand-properties autofix for transition #6812

Closed
jathak opened this issue Apr 25, 2023 · 3 comments · Fixed by #6815
Closed

Fix declaration-block-no-redundant-longhand-properties autofix for transition #6812

jathak opened this issue Apr 25, 2023 · 3 comments · Fixed by #6815
Labels
status: wip is being worked on by someone type: bug a problem with a feature or rule

Comments

@jathak
Copy link
Contributor

jathak commented Apr 25, 2023

What steps are needed to reproduce the bug?

Given code like:

a {
  transition-delay: 500ms;
  transition-duration: 250ms;
  transition-timing-function: ease-in-out;
  transition-property: transform, visibility;
}

What Stylelint configuration is needed to reproduce the bug?

{
  "rules": {
    "declaration-block-no-redundant-longhand-properties": true
  }
}

How did you run Stylelint?

https://stylelint.io/demo/#N4Igxg9gJgpiBcICGACYAdAdilAXATkpgM4CWupEmAtLADZICe8KArAAzsC2xA3FjgJEyFKrQCuhUZhYAmDj37Y8hEuUo0KXUpgDm1AGbjMYaSxhJiMajuoRxuJYNUiN1AA74I7mPlzMVYQMIfC4AGhQAN1IyACNSOnJGJQBfEDCQAwSYADkkLjhEGAAPfPc6GAA6MGJidPAqLN0EEAxldBAS3BhMKGIOlgBtARwUDuJ-CsTMXGpITCbqCaIoJHwoDpGAXSw0jPmmgDEQriRcFoArYip62Hc6xDacccmYadwBsZAGbomOsJGL0YUx0swOpH0y16aw2CC+PxgfxAuxAKSAA

Which version of Stylelint are you using?

15.6.0

What did you expect to happen?

No problems to be reported, or, if a declaration-block-no-redundant-longhand-properties error is reported, the autofix should be

a {
  transition: transform 250ms ease-in-out 500ms, visibility 250ms ease-in-out 500ms;
}

What actually happened?

An error of Expected shorthand property "transition" with an incorrect suggested autofix of:

a {
  transition: transform, visibility 250ms ease-in-out 500ms;
}

Does the bug relate to non-standard syntax?

No

Proposal to fix the bug

IMO, this error shouldn't be reported at all for transitions with multiple properties (as reported in #4074), but if it is considered an error, then the autofix at least needs to be changed.

@ybiquitous
Copy link
Member

@jathak Thanks for the report with a reproducible demo. I confirm the incorrect behavior.

Using ignoreShorthands option enables us to avoid this problem, although it's just a workaround. For example:

{
  "rules": {
    "declaration-block-no-redundant-longhand-properties": [true, {"ignoreShorthands": ["transition"]}]
  }
}

However, we probably need to provide a custom resolver for the transition shorthand like grid-template to improve user-friendliness:

const customResolvers = new Map([
[
'grid-template',
(decls) => {
const areas = decls.get('grid-template-areas')?.value.trim();
const columns = decls.get('grid-template-columns')?.value.trim();
const rows = decls.get('grid-template-rows')?.value.trim();
if (!(areas && columns && rows)) return;
const splitAreas = [...areas.matchAll(/"[^"]+"/g)].map((x) => x[0]);
const splitRows = rows.split(' ');
if (splitAreas.length === 0 || splitRows.length === 0) return;
if (splitAreas.length !== splitRows.length) return;
const zipped = splitAreas.map((area, i) => `${area} ${splitRows[i]}`).join(' ');
return `${zipped} / ${columns}`;
},
],
]);

I've labeled the issue as ready to implement. Please consider contributing if you have time.

There are steps on how to fix a bug in a rule in the Developer guide.

@ybiquitous ybiquitous added status: ready to implement is ready to be worked on by someone type: bug a problem with a feature or rule labels Apr 26, 2023
@ybiquitous ybiquitous changed the title Incorrect autofix for declaration-block-no-redundant-longhand-properties transition with multiple properties Fix declaration-block-no-redundant-longhand-properties autofix for transition Apr 26, 2023
@mattxwang
Copy link
Member

😓 sorry for the issue with this autofix (I initially contributed it). Have just submitted #6815 which should directly resolve the reported issue + better follow the CSS spec.

@ybiquitous ybiquitous added status: wip is being worked on by someone and removed status: ready to implement is ready to be worked on by someone labels Apr 26, 2023
@mattxwang
Copy link
Member

Ah, also - one other approach to consider is that we could create a new transition for each transition-property. This has the pro of being more (subjectively) readable, as discussed in #4074 and the original issue; the con is that we're potentially adding multiple nodes to the AST. I initially went ahead with the current approach since it's the easiest to implement; but, if we think the other approach is better, I'm happy to rework #6815 to make it behave that way instead!

mattxwang added a commit that referenced this issue Apr 27, 2023
…`transition` (#6815)

Closes #6812.

This one required some non-trivial CSS spec reading, hence the complicated-ish behaviour!


---------

Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: wip is being worked on by someone type: bug a problem with a feature or rule
Development

Successfully merging a pull request may close this issue.

3 participants