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 rules for no-color-css-vars #61

Closed
wants to merge 9 commits into from
Closed

Conversation

langermank
Copy link
Contributor

@langermank langermank commented Jun 23, 2023

As we begin testing new CSS color variable names, I'd like to try and pair down the surface area as much as possible. There are many (~200) cases in github/github where teams are referencing color CSS variables within the sx prop and other styled-system props, and I think it makes sense to try and enforce primitives the way Primer React consumes them currently.

This test should check for the sx prop or color related styled-system props such as color or backgroundColor, and if it finds a CSS variable in the provided JSON object suggest a fix.

I hand wrote the JSON object because the names are so different, it would be impossible to infer when to camelCase. I opted to handle the most common colors and will likely need to hand edit some component specific issues I found in github/github.

I also plan to followup with another plugin that will actually allow the use of CSS variables in some cases, but it will do an autofix with the new color variable the same way we do in stylelint.

Closes https://github.com/github/primer/issues/2347

@changeset-bot
Copy link

changeset-bot bot commented Jun 23, 2023

⚠️ No Changeset found

Latest commit: 108b8b5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@@ -0,0 +1,75 @@
{
"--color-canvas-default-transparent": "canvasDefaultTransparent",
Copy link
Member

Choose a reason for hiding this comment

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

How are we generating this map?

Is there a risk of this falling out of sync with primitives?

src/rules/__tests__/no-color-css-vars.test.js Show resolved Hide resolved
@siddharthkp
Copy link
Member

siddharthkp commented Jun 27, 2023

@langermank Found it!

It was context.sourceCode.getText, pushed a few commits that handle more use cases (see tests)

@@ -25,12 +25,12 @@
"@changesets/cli": "^2.16.0",
"@github/prettier-config": "0.0.4",
"@primer/primitives": "^5.1.0",
"eslint": "^8.0.1",
"eslint": "^8.42.0",
Copy link
Member

@siddharthkp siddharthkp Jun 27, 2023

Choose a reason for hiding this comment

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

to use new features: sourceCode.getText and sourceCode.getScope

This is compatible with version in dotcom, github/github/ui/packages/eslintrc/package.json#L19

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you!!! 🎉 you're the best

Copy link
Member

@broccolinisoup broccolinisoup left a comment

Choose a reason for hiding this comment

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

meta: {
type: 'suggestion',
hasSuggestions: true,
fixable: 'code',
Copy link
Member

Choose a reason for hiding this comment

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

So cool 🔥

},
{
code: `<Box sx={{boxShadow: '0 0 0 2px var(--color-canvas-subtle)'}} />`,
output: `<Box sx={{boxShadow: '0 0 0 2px canvas.subtle'}} />`,
Copy link
Contributor

@colebemis colebemis Jun 29, 2023

Choose a reason for hiding this comment

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

boxShadow doesn't support referencing variables like this. Instead, you have to pass a function like this:

boxShadow: theme => `0 0 0 2px ${theme.colors.canvas.subtle}`

Example: https://github.com/primer/react/blob/650a46d5dbf10500d4d9af508c71744665f1bd0d/src/PageLayout/PageLayout.tsx#L141

It might be tricky to update the plugin to support this, so let's check if this is a common pattern before investing the time in updating the plugin.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you! That was a real example from dotcom I grabbed. I'll see if there's more than one, though.

@joshblack joshblack removed their request for review July 6, 2023 15:52
@langermank
Copy link
Contributor Author

Closing in favor of #81

@langermank langermank closed this Oct 2, 2023
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.

4 participants