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

Sort shorthand across multi-properties #1732

Merged
merged 3 commits into from
Oct 24, 2024

Conversation

kylorhall-atlassian
Copy link
Collaborator

What is this change?

Making sure sorting works across mutli-property classes.

Example: this {-webkit-text-decoration-color:initial;text-decoration-color:initial} currently blocks all sorting above or below it, effectively creating a new "sorting bucket" because it always returns 0 as a or b.

This doesn't get sorted:

._y44v1tgl{animation:kbayob8 5s ease infinite}
._syaz1qtk{color:var(--ds-link)}
._1jmq18uv{-webkit-text-decoration-color:initial;text-decoration-color:initial}
._zulph461{gap:var(--ds-space-050)}

Added a test to show how this works now!

PR checklist

I have...

  • Updated or added applicable tests
  • Not updated the documentation in website/

Example: this `{-webkit-text-decoration-color:initial;text-decoration-color:initial}` blocks all sorting above/below it, effectively creating a new "sorting bucket" because it always returns `0` as `a` or `b`.
```css
._y44v1tgl{animation:kbayob8 5s ease infinite}
._syaz1qtk{color:var(--ds-link)}
._1jmq18uv{-webkit-text-decoration-color:initial;text-decoration-color:initial}
._zulph461{gap:var(--ds-space-050)}
```

Added a test to replicate and fix the issue.
Copy link

changeset-bot bot commented Oct 23, 2024

🦋 Changeset detected

Latest commit: cccb610

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@compiled/css Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

netlify bot commented Oct 23, 2024

Deploy Preview for compiled-css-in-js canceled.

Name Link
🔨 Latest commit cccb610
🔍 Latest deploy log https://app.netlify.com/sites/compiled-css-in-js/deploys/67196426e5758b000929e108

Copy link
Collaborator Author

@kylorhall-atlassian kylorhall-atlassian Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this file gets regenerated quite often, I'm not really sure why—if this is developer error or our deps are so prone to changing…? Making CI protect this in #1733 (nevermind, I gave up)

Comment on lines -377 to +497
// WARNING: This is technically wrong as `.a { … }` is not sorted as we expect;
// it _should_ be 'abcdef' not 'eabcdf'.
//
// We are ok with this because we expect atomicifyRules to run before this plugin,
// so each class will never have more than one property.
expect(actual).toMatchInlineSnapshot(`
".e { border-top: none; }
"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 Guess it wasn't what I thought and this was a bug actually!

if (node.nodes.length === 1 && nodeIsDeclaration(node.nodes[0])) {
return node.nodes[0];
}
// Return the first node that is a declaration, if we find one
Copy link
Collaborator Author

@kylorhall-atlassian kylorhall-atlassian Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may not be correct at all, eg. if Compiled ever tries to sort this, it's kind of uncharted territory…global will come second here 🥴

.global {padding-left: 8px;all: unset}
._abcd1234{padding: 8px}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could technically do a deep recursive sort somehow, eg. compare the scores of all children vs. another child, but I'm expecting in real-world Compiled, the above is not a legitimate scenario and instead using the first property's "score" is fine.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if I learnt anything from this, it's that it's safer to ask devs not to mix shorthand and longhand properties at all, than to try to work with it 😅

._18s8paju{margin:var(--ds-space-150,9pt) auto 28px auto}
._y44v1syn{animation:kbpspdk 5s ease infinite}
._y44vbxa2{animation:k1qo9wnt 5s ease infinite}
._bfhk29zg, ._irr329zg:hover{background-color:var(--ds-background-selected,#deebff)}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect sorting pseudos after non pseudos won't (and can't) work for this line?

(Not sure how we would fix this short of splitting up the declaration...)

Copy link
Collaborator Author

@kylorhall-atlassian kylorhall-atlassian Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm actually confused what we'd do here…it's unrelated to this, but I guess you would have to split this into two… 😢

I think the impact of this is pretty minor, but not nothing!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeahhh i'm ok with tackling stuff like ._bfhk29zg, ._irr329zg:hover{background-color:var(--ds-background-selected,#deebff)} being out of scope, but best to keep in the back of our minds in case we ever figure out why this happens

._apju8stv:hover .linkText{-webkit-text-decoration-line:underline;text-decoration-line:underline}
._ku3unqa1:hover .linkText{-webkit-text-decoration-style:solid;text-decoration-style:solid}
._pwyo12x7{padding-inline-end:var(--ds-space-075,6px)}
._1b7p1epz{padding-left:var(--ds-space-1000,5pc)}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does padding-left and padding-right come before background

Copy link
Collaborator Author

@kylorhall-atlassian kylorhall-atlassian Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's …:active{background} isn't it?

._j6xt1fef:active{background:var(--_hcgrh3)}
._1owrotz2:active, ._jzfzotz2:focus, ._1mq6otz2:hover{padding:0 var(--ds-space-050,4px)}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah you're right yeah, this looks fine

const actual = transform(outdent`
._zulph461{gap:3px}
._bfhk1ayf{background-color:red}
._1jmq18uv{-webkit-text-decoration-color:initial;text-decoration-color:initial}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah is this from autoprefixer? I guess the 1 property per class name assumption is incorrect after all

sorry for introducing the bug 😔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, 99% sure it was there before because I just return 0'd arrays (and this is an array of properties)

@dddlr dddlr merged commit 124243c into master Oct 24, 2024
13 checks passed
@dddlr dddlr deleted the kylorhall/sort-shorthand-multi-properties branch October 24, 2024 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants