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

Discard invalid classes such as bg-red-[#000] #13970

Merged
merged 8 commits into from
Jul 10, 2024

Conversation

RobinMalfait
Copy link
Member

This PR improves the candidate parser by making sure that invalid candidates are discarded earlier.

When using a utility such as bg-red-500, then we have to find the actual plugin by finding the root. In this case we will try:

  • root: bg-red-500, value: null <- No match yet
  • root: bg-red, value: 500 <- No match yet
  • root: bg, value: red-500 <- Bingo, bg is a known root

However, if you are using arbitrary values such as bg-[#000], then we know that everything before the -[#000] will be the root of the plugin. In this case we can do a direct lookup for bg.

Before this change, this wasn't the case (in v4), which means that we could write bg-red-[#000] and it parsed just fine.

This PR also adds a test for a case that wasn't covered by any tests, which is when you use a candidate such as bg-. This parsed as a root of "bg" and a value of "".

When using arbitrary values such as `bg-[#0088cc]` then we know that
everything before the `-[` part is the `root` of the utility. This also
means that this should exist in the design system as-is.

If we use `findRoot` instead, it means that `bg-red-[#0088cc]` would
also just parse fine and we don't want.
It's not necessary to track state in a state object. Let's use the
variables directly.
The value doesn't change anymore, which means we can discard early and
don't need to start creating candidate objects.
@RobinMalfait RobinMalfait changed the title Discard more invalid candidates Discard invalid candidates such as bg-red-[#000] Jul 8, 2024
@RobinMalfait RobinMalfait changed the title Discard invalid candidates such as bg-red-[#000] Discard invalid classes such as bg-red-[#000] Jul 8, 2024
@RobinMalfait RobinMalfait added the v4 label Jul 9, 2024
@RobinMalfait RobinMalfait merged commit 8e97a48 into next Jul 10, 2024
1 check passed
@RobinMalfait RobinMalfait deleted the fix/discard-invalid-candidates branch July 10, 2024 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants