Skip to content

[Emotion] Add eslint rule for CSS logical properties#6125

Merged
cee-chen merged 34 commits intoelastic:mainfrom
cee-chen:more-logical-properties
Aug 22, 2022
Merged

[Emotion] Add eslint rule for CSS logical properties#6125
cee-chen merged 34 commits intoelastic:mainfrom
cee-chen:more-logical-properties

Conversation

@cee-chen
Copy link
Copy Markdown
Contributor

@cee-chen cee-chen commented Aug 11, 2022

Summary

This PR adds a new local eslint rule that looks for CSS properties that should instead use logical properties and flags them.

Because @chandlerprall is the bomb, it also includes an auto --fix option 😎 (although generally we prefer to use our logicalCSS utils instead of the plain logical CSS)

screencap

Checklist

  • Checked in mobile
  • Checked in Chrome, Safari, Edge, and Firefox

- [ ] Checked in both light and dark modes
- [ ] Props have proper autodocs and playground toggles
- [ ] Added documentation
- [ ] Checked Code Sandbox works for any docs examples
- [ ] Added or updated jest and cypress tests
- [ ] Checked for breaking changes and labeled appropriately
- [ ] Checked for accessibility including keyboard-only and screenreader modes
- [ ] Updated the Figma library counterpart
- [ ] A changelog entry exists and is marked appropriately

@cee-chen cee-chen added the skip-changelog Use on PRs to skip changelog requirement (Don't delete - used for automation) label Aug 11, 2022
@kibanamachine
Copy link
Copy Markdown

Preview documentation changes for this PR: https://eui.elastic.co/pr_6125/

@thompsongl
Copy link
Copy Markdown
Contributor

do wonder if we could make a homebrew eslint rule that checks the contents of css string literals for the properties in logicals.ts

Seems like a neat idea to me!

@cee-chen
Copy link
Copy Markdown
Contributor Author

cee-chen commented Aug 12, 2022

I spent my spacetime Friday figuring out how to write an eslint rule and was able to do so! hooray! 438b861

It could maybe use a little more refinement in flagging the exact location of the property instead of the entire CSS string, but it got a little gnarly due to the string literal usage, so maybe another day 😬

The bad news is that CI will now fail because there's a bunch of property fixes I did in #6124 and not this PR, so now #6124 needs to get in first 😅

Overall though very excited for this plugin and not having to try to remember it in my own conversions or search for it in code review! 🎉

@cee-chen cee-chen requested a review from chandlerprall August 12, 2022 20:22
@kibanamachine
Copy link
Copy Markdown

Preview documentation changes for this PR: https://eui.elastic.co/pr_6125/

@cee-chen cee-chen force-pushed the more-logical-properties branch from e5879b2 to b9f52cf Compare August 16, 2022 16:58
@cee-chen cee-chen changed the title [Emotion] Convert some missed properties to logical properties [Emotion] Add eslint rule for CSS logical properties Aug 16, 2022
@kibanamachine
Copy link
Copy Markdown

Preview documentation changes for this PR: https://eui.elastic.co/pr_6125/

@cee-chen cee-chen force-pushed the more-logical-properties branch 2 times, most recently from 5d1fbb2 to 12f2324 Compare August 17, 2022 01:40
- throwing as a result of our new `resolveJsonModule: true`
- importing the map directly with a regex for newlines produces less false positives for non-logical properties
- it's already a new logical property and doesn't need to be converted
+ add separate message for values vs properties - this might get used more for other future properties
- switching to a exec allows us to get the start index of the match, so that we can more accurately return the node location and also use a fixer
- (opinionated) using a single regex vs multiple loops also simplifes logic somewhat, even if the regex itself is annoying
- use regex capture groups for readability
- via the maps we've set up
- vs highlighting the entire css`` block
- yay for even more gnarly regex utils
- instead of manual media query
note: loading_logo.styles already had so many static logical properties i just continued using them for in-file consistency
@cee-chen cee-chen force-pushed the more-logical-properties branch from 12f2324 to a0b5625 Compare August 17, 2022 01:53
@cee-chen
Copy link
Copy Markdown
Contributor Author

Alrighty, this PR now has a way fancier eslint rule 😅 PR description has been updated with screenshots!

I very strongly recommend following along by commit if possible, regexes are a gnarly beast, even more so when using regex utils sourced from stack overflow 🤪

@kibanamachine
Copy link
Copy Markdown

Preview documentation changes for this PR: https://eui.elastic.co/pr_6125/

- since we have several Emotion utilities that pass back raw ``s instead of css``
@cee-chen cee-chen force-pushed the more-logical-properties branch from 902b0a9 to cfb35a3 Compare August 18, 2022 18:14
Comment on lines +38 to +41
TemplateLiteral(node) {
const templateContents = node.quasis || [];
templateContents.forEach((quasi) => {
const stringLiteral = quasi?.value?.raw;
Copy link
Copy Markdown
Contributor Author

@cee-chen cee-chen Aug 18, 2022

Choose a reason for hiding this comment

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

Quick comment/note on 172b3b8 and the change from linting css literals to all `` literals:

If this more aggressive lint rule becomes an issue in the future (either from a dev experience or performance POV), we should consider reverting the change and imposing the following limits for it:

  • lint all css templates, anywhere
  • lint all template literals but only within **/*.styles.ts or src/global_styling files

Copy link
Copy Markdown
Contributor Author

@cee-chen cee-chen Aug 18, 2022

Choose a reason for hiding this comment

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

Also, per Chandler's suggestion to benchmark performance impact on CI/yarn lint runs:

Control (random unrelated PR from 3 days ago without this lint rule)

Linting just css templates

52min

Linting all template literals:

51min

Conclusion: no significant perf impact

@kibanamachine
Copy link
Copy Markdown

Preview documentation changes for this PR: https://eui.elastic.co/pr_6125/

@cee-chen cee-chen force-pushed the more-logical-properties branch from cfb35a3 to abaeaa4 Compare August 18, 2022 19:04
@kibanamachine
Copy link
Copy Markdown

Preview documentation changes for this PR: https://eui.elastic.co/pr_6125/

Copy link
Copy Markdown
Contributor

@chandlerprall chandlerprall left a comment

Choose a reason for hiding this comment

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

Exciting! Thanks for taking the time to make this work with --fix as well. I can't find a case where this breaks or otherwise doesn't perform as expected.

@cee-chen cee-chen enabled auto-merge (squash) August 22, 2022 20:39
@kibanamachine
Copy link
Copy Markdown

Preview documentation changes for this PR: https://eui.elastic.co/pr_6125/

@cee-chen cee-chen merged commit 4c285cd into elastic:main Aug 22, 2022
@cee-chen cee-chen deleted the more-logical-properties branch August 22, 2022 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-changelog Use on PRs to skip changelog requirement (Don't delete - used for automation)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants