-
-
Notifications
You must be signed in to change notification settings - Fork 955
fix(transformer-attributify-jsx): falls back to regular expression when babel parse error #5032
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
Conversation
…en babel parse error
✅ Deploy Preview for unocss ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this 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 |
Copilot
AI
Dec 30, 2025
There was a problem hiding this comment.
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.
| 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. | |
| */ |
| try { | ||
| await attributifyJsxBabelResolver(params) | ||
| } | ||
| catch { |
Copilot
AI
Dec 30, 2025
There was a problem hiding this comment.
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.
| 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, | |
| ) |
packages-presets/transformer-attributify-jsx/src/resolver/regex.ts
Outdated
Show resolved
Hide resolved
packages-presets/transformer-attributify-jsx/src/resolver/regex.ts
Outdated
Show resolved
Hide resolved
| 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 |
Copilot
AI
Dec 30, 2025
There was a problem hiding this comment.
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.
| 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 |
|
commit: |
…x.ts Co-authored-by: Copilot <[email protected]>
…x.ts Co-authored-by: Copilot <[email protected]>
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.