Skip to content

Commit 5908c78

Browse files
authored
fix(transformer-attributify-jsx): transform error when html element is like a atomic class (#3147)
1 parent 4fd8353 commit 5908c78

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

packages/transformer-attributify-jsx/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface TransformerAttributifyJsxOptions {
3838
}
3939

4040
const elementRE = /(<\w[\w:\.$-]*\s)([\s\S]*?)(?=>[\s\S]?<\/[\s\w:\.$-]*>|\/>)/g
41-
const attributeRE = /(?<!<)([a-zA-Z()#][\[?a-zA-Z0-9-_:()#%\]?]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/g
41+
const attributeRE = /(?<!<\/?)([a-zA-Z()#][\[?a-zA-Z0-9-_:()#%\]?]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/g
4242
const valuedAttributeRE = /((?!\d|-{2}|-\d)[a-zA-Z0-9\u00A0-\uFFFF-_:!%-.~<]+)=(?:["]([^"]*)["]|[']([^']*)[']|[{]((?:[`(](?:[^`)]*)[`)]|[^}])+)[}])/gms
4343

4444
export default function transformerAttributifyJsx(options: TransformerAttributifyJsxOptions = {}): SourceCodeTransformer {

test/transformer-attributify-jsx.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ const originalCode = `
4343
</div>
4444
`.trim()
4545

46+
const tagCouldBeAttrCode = `
47+
<div>
48+
<b text-red>Test</b>
49+
<h1 text-red>Test</h1>
50+
<h2 text-red>Test</h2>
51+
<h3 text-red>Test</h3>
52+
<h4 text-red>Test</h4>
53+
<h5 text-red>Test</h5>
54+
<h6 text-red>Test</h6>
55+
</div>
56+
`.trim()
57+
4658
describe('transformerAttributifyJsx', () => {
4759
const uno = createGenerator({
4860
presets: [
@@ -146,6 +158,23 @@ describe('transformerAttributifyJsx', () => {
146158
expect(codeToString).not.toMatch(`${rule}=""`)
147159
})
148160
})
161+
162+
test('if class-like tag do not cause error', async () => {
163+
const code = new MagicString(tagCouldBeAttrCode)
164+
await transformerAttributifyJsx().transform(code, 'app.tsx', { uno, tokens: new Set() } as any)
165+
166+
expect(code.toString()).toMatchInlineSnapshot(`
167+
"<div>
168+
<b text-red=\\"\\">Test</b>
169+
<h1 text-red=\\"\\">Test</h1>
170+
<h2 text-red=\\"\\">Test</h2>
171+
<h3 text-red=\\"\\">Test</h3>
172+
<h4 text-red=\\"\\">Test</h4>
173+
<h5 text-red=\\"\\">Test</h5>
174+
<h6 text-red=\\"\\">Test</h6>
175+
</div>"
176+
`)
177+
})
149178
})
150179

151180
describe('transformerAttributifyJsxBabel', () => {
@@ -237,4 +266,21 @@ describe('transformerAttributifyJsxBabel', () => {
237266
expect(codeToString).not.toMatch(`${rule}=""`)
238267
})
239268
})
269+
270+
test('if class-like tag do not cause error', async () => {
271+
const code = new MagicString(tagCouldBeAttrCode)
272+
await transformerAttributifyJsx().transform(code, 'app.tsx', { uno, tokens: new Set() } as any)
273+
274+
expect(code.toString()).toMatchInlineSnapshot(`
275+
"<div>
276+
<b text-red=\\"\\">Test</b>
277+
<h1 text-red=\\"\\">Test</h1>
278+
<h2 text-red=\\"\\">Test</h2>
279+
<h3 text-red=\\"\\">Test</h3>
280+
<h4 text-red=\\"\\">Test</h4>
281+
<h5 text-red=\\"\\">Test</h5>
282+
<h6 text-red=\\"\\">Test</h6>
283+
</div>"
284+
`)
285+
})
240286
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy