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

refactor(preset-attributify): improve ternary extraction #1921

Merged
merged 1 commit into from Nov 29, 2022

Conversation

sibbng
Copy link
Member

@sibbng sibbng commented Nov 26, 2022

Fixes #1915

It seems we already support this in Vue templates. This PR adds support for ternary operators in JSX and eliminates some false positives of previous ternary extraction logic.

@sibbng sibbng requested a review from antfu as a code owner November 26, 2022 16:17
@netlify
Copy link

netlify bot commented Nov 26, 2022

Deploy Preview for unocss ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 8287f08
🔍 Latest deploy log https://app.netlify.com/sites/unocss/deploys/63823c2ee7f6e00008da1288
😎 Deploy Preview https://deploy-preview-1921--unocss.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@zyyv
Copy link
Member

zyyv commented Nov 26, 2022

@sibbng Can you check your pr with this playground

@zyyv
Copy link
Member

zyyv commented Nov 26, 2022

In #1915 , I am more inclined that this is a user's usage rules problem.

@sibbng
Copy link
Member Author

sibbng commented Nov 26, 2022

@sibbng Can you check your pr with this playground

posTest is not a valid rule. So it's not picked up by any attributify selector.

In #1915 , I am more inclined that this is a user's usage rules problem.

No, it's not. We already have a test for extracting ternaries from vue templates. This PR adds support for JSX.

@zyyv
Copy link
Member

zyyv commented Nov 26, 2022

We already have a test for extracting ternaries from vue templates

Can you guide me where is the test code?

image

Following this image, this token was not generated because it has no corresponding rule or variant

@sibbng
Copy link
Member Author

sibbng commented Nov 26, 2022

Can you guide me where is the test code?

test/preset-attributify.test.ts:22

@zyyv
Copy link
Member

zyyv commented Nov 26, 2022

Can you guide me where is the test code?

test/preset-attributify.test.ts:22

:font="foo > bar ? 'mono' : 'sans'"

About this line, you can see UnoCSS extracted results:

"[font~=\\"foo\\"]",
"[font~=\\">\\"]",
"[font~=\\"bar\\"]",
"[font~=\\"?\\"]",
"[font~=\\"mono\\"]",
"[font~=\\":\\"]",
"[font~=\\"sans\\"]",

Only font-mono and font-sans can be parsed, because we have its rules.
[font~=\\"mono\\"]{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,\\"Liberation Mono\\",\\"Courier New\\",monospace;}
[font~=\\"sans\\"]{font-family:ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,\\"Segoe UI\\",Roboto,\\"Helvetica Neue\\",Arial,\\"Noto Sans\\",sans-serif,\\"Apple Color Emoji\\",\\"Segoe UI Emoji\\",\\"Segoe UI Symbol\\",\\"Noto Color Emoji\\";}


In #1915, We don't have any rule or variant about pos

@sibbng
Copy link
Member Author

sibbng commented Nov 26, 2022

In #1915, We don't have any rule or variant about pos

pos is shorthand for position

export const positions: Rule[] = [
[/^(?:position-|pos-)?(relative|absolute|fixed|sticky)$/, ([, v]) => ({ position: v })],
[/^(?:position-|pos-)([-\w]+)$/, ([, v]) => globalKeywords.includes(v) ? { position: v } : undefined],
[/^(?:position-|pos-)?(static)$/, ([, v]) => ({ position: v })],
]

About this line, you can see UnoCSS extracted results:

This is exactly what I called false positives. In this case, they will not make into output since there are no matching rules as you said. But, what if the variables we used in ternaries are has matching rules? e.g:

<div
  :text="xl ? '5xl' : 'base'"
/>

As you can see from the output an unnecessary selector [text~="xl"] is generated in main branch.

This PR makes sure, only the quoted strings in the true or false side of the ternary are extracted. Both for vue and JSX.

@zyyv
Copy link
Member

zyyv commented Nov 27, 2022

I'm so sorry, and thanks for you answer, you are right. I never use pos this shorthand, i forgot it. 😅

@pawover
Copy link

pawover commented Nov 29, 2022

Thanks for this PR, are there any plans for new releases in the near future?

@antfu antfu merged commit daba01c into unocss:main Nov 29, 2022
sibbng added a commit that referenced this pull request Jan 8, 2023
antfu pushed a commit that referenced this pull request Jan 9, 2023
praburangki pushed a commit to praburangki/unocss that referenced this pull request Jan 12, 2023
praburangki pushed a commit to praburangki/unocss that referenced this pull request Jan 12, 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.

about dynamic attributify mode
4 participants