Skip to content

Conversation

@zyyv
Copy link
Member

@zyyv zyyv commented Dec 30, 2025

close #4999, close #5013, related #4807

Babel is used for parsing first, and if a parsing error occurs, it falls back to using regular expression parsing.

Copilot AI review requested due to automatic review settings December 30, 2025 09:32
@zyyv zyyv requested a review from antfu as a code owner December 30, 2025 09:32
@netlify
Copy link

netlify bot commented Dec 30, 2025

Deploy Preview for unocss ready!

Name Link
🔨 Latest commit 62699cf
🔍 Latest deploy log https://app.netlify.com/projects/unocss/deploys/69539eddbd2b710008db53cf
😎 Deploy Preview https://deploy-preview-5032--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 project configuration.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements a fallback mechanism for the attributify-jsx transformer to use regular expression-based parsing when Babel parsing fails. The main code is refactored into two separate resolver modules: a Babel-based resolver (primary) and a regex-based resolver (fallback).

  • Extracts Babel parsing logic into a dedicated resolver module
  • Adds a new regex-based resolver as a fallback mechanism
  • Implements try-catch logic to gracefully fall back from Babel to regex parsing

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
packages-presets/transformer-attributify-jsx/src/index.ts Refactors transform logic to use resolver modules; adds AttributifyResolverParams interface and implements try-catch fallback pattern
packages-presets/transformer-attributify-jsx/src/resolver/babel.ts New file containing the extracted Babel-based JSX attribute resolver with error handling
packages-presets/transformer-attributify-jsx/src/resolver/regex.ts New file containing regex-based JSX attribute resolver for fallback scenarios when Babel parsing fails

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

export interface AttributifyResolverParams {
code: MagicString
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

The 'id' parameter is added to the AttributifyResolverParams interface but is not used in either the babel or regex resolver implementations. Consider removing it from the interface if it's not needed, or document why it's included for future use.

Suggested change
code: MagicString
code: MagicString
/**
* Identifier of the current module/file.
*
* Exposed for resolver implementations that may need access to the
* original source identifier (e.g. for context-aware transforms or
* debugging), even though the built-in resolvers may not currently use it.
*/

Copilot uses AI. Check for mistakes.
try {
await attributifyJsxBabelResolver(params)
}
catch {
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

The generic error catch block silently falls back to the regex resolver without any logging or indication of why the Babel parser failed. This makes debugging difficult when parsing errors occur. Consider logging the error or at least documenting this silent fallback behavior.

Suggested change
catch {
catch (error) {
// Babel-based resolver failed; falling back to regex resolver for this file.
// This is expected for some inputs but is logged to aid debugging parser issues.
// eslint-disable-next-line no-console
console.warn(
`@unocss/transformer-attributify-jsx: Babel resolver failed for "${id}", falling back to regex resolver:`,
error,
)

Copilot uses AI. Check for mistakes.
Comment on lines +26 to +29
if (valuedAttributeRE.test(attrAttributePart))
attrAttributePart.replace(valuedAttributeRE, (m: string) => ' '.repeat(m.length))

const pre = temp.slice(0, preLastModifierIndex) + ' '.repeat(_item.index + _item[0].indexOf(_item[2]) - preLastModifierIndex) + attrAttributePart
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

The result of the replace operation is not being assigned. This line should be const replacedPart = attrAttributePart.replace(...) and then used, or the replace should modify attrAttributePart by reassigning it. Currently, the replace result is discarded, making this operation ineffective.

Suggested change
if (valuedAttributeRE.test(attrAttributePart))
attrAttributePart.replace(valuedAttributeRE, (m: string) => ' '.repeat(m.length))
const pre = temp.slice(0, preLastModifierIndex) + ' '.repeat(_item.index + _item[0].indexOf(_item[2]) - preLastModifierIndex) + attrAttributePart
let processedAttrAttributePart = attrAttributePart
if (valuedAttributeRE.test(attrAttributePart))
processedAttrAttributePart = attrAttributePart.replace(valuedAttributeRE, (m: string) => ' '.repeat(m.length))
const pre = temp.slice(0, preLastModifierIndex) + ' '.repeat(_item.index + _item[0].indexOf(_item[2]) - preLastModifierIndex) + processedAttrAttributePart

Copilot uses AI. Check for mistakes.
@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 30, 2025

Open in StackBlitz

commit: 62699cf

@zyyv zyyv merged commit 38cc8ae into main Dec 30, 2025
12 checks passed
@zyyv zyyv deleted the fallback-jsx-regex branch December 30, 2025 09:50
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.

Attributify-JSX breaks Astro+MDX

2 participants