-
-
Notifications
You must be signed in to change notification settings - Fork 779
/
transformer-attributify-jsx.test.ts
240 lines (225 loc) · 9.08 KB
/
transformer-attributify-jsx.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import MagicString from 'magic-string'
import presetAttributify from '@unocss/preset-attributify'
import presetUno from '@unocss/preset-uno'
import { createGenerator } from '@unocss/core'
import { describe, expect, test } from 'vitest'
import transformerAttributifyJsx from '../packages/transformer-attributify-jsx/src'
import transformerAttributifyJsxBabel from '../packages/transformer-attributify-jsx-babel/src'
const originalCode = `
<div h-full text-center flex select-none className={red ? 'text-red': 'text-green'}>
<input value={ target ? '10px' : '20px'} style={{ height: '100px' }} />
<div ma>
<div text-5xl fw100 animate-bounce-alt animate-count-infinite animate-duration-1s key={index}>
unocss
</div>
<div op30 text-lg fw300 m1 className={hidden && 'op0'}>
The instant on-demand Atomic CSS engine.
</div>
<div m2 flex justify-center text-2xl op30 hover:op80 hover:text-2xl>
<a
i-carbon-logo-github
text-inherit
href="https://github.com/unocss/unocss"
target="_blank"
></a>
<router-link to={\`/path/\${1}\`}/>
</div>
</div>
<section
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
mr-10
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
></section>
<div
label={
<b>1</b>
}
></div>
<div absolute bottom-5 right-0 left-0 text-center op30 fw300>
on-demand · instant · fully customizable
</div>
<div components={<div absolute bottom-5></div>}></div>
</div>
`.trim()
describe('transformerAttributifyJsx', () => {
const uno = createGenerator({
presets: [
presetUno(),
presetAttributify(),
],
})
test('transform', async () => {
const code = new MagicString(originalCode)
await transformerAttributifyJsx().transform(code, 'app.tsx', { uno, tokens: new Set() } as any)
expect(code.toString()).toMatchInlineSnapshot(`
"<div h-full=\\"\\" text-center=\\"\\" flex=\\"\\" select-none=\\"\\" className={red ? 'text-red': 'text-green'}>
<input value={ target ? '10px' : '20px'} style={{ height: '100px' }} />
<div ma=\\"\\">
<div text-5xl=\\"\\" fw100=\\"\\" animate-bounce-alt=\\"\\" animate-count-infinite=\\"\\" animate-duration-1s=\\"\\" key={index}>
unocss
</div>
<div op30=\\"\\" text-lg=\\"\\" fw300=\\"\\" m1=\\"\\" className={hidden && 'op0'}>
The instant on-demand Atomic CSS engine.
</div>
<div m2=\\"\\" flex=\\"\\" justify-center=\\"\\" text-2xl=\\"\\" op30=\\"\\" hover-op80=\\"\\" hover-text-2xl=\\"\\">
<a
i-carbon-logo-github
text-inherit=\\"\\"
href=\\"https://github.com/unocss/unocss\\"
target=\\"_blank\\"
></a>
<router-link to={\`/path/\${1}\`}/>
</div>
</div>
<section
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
mr-10=\\"\\"
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
></section>
<div
label={
<b>1</b>
}
></div>
<div absolute=\\"\\" bottom-5=\\"\\" right-0=\\"\\" left-0=\\"\\" text-center=\\"\\" op30=\\"\\" fw300=\\"\\">
on-demand · instant · fully customizable
</div>
<div components={<div absolute bottom-5></div>}></div>
</div>"
`)
})
test('blocklist', async () => {
const code = new MagicString(originalCode)
const blocklist: (string | RegExp)[] = ['flex', 'absolute']
await transformerAttributifyJsx({
blocklist,
}).transform(code, 'app.jsx', { uno, tokens: new Set() } as any)
expect(code.toString()).toMatchInlineSnapshot(`
"<div h-full=\\"\\" text-center=\\"\\" flex select-none=\\"\\" className={red ? 'text-red': 'text-green'}>
<input value={ target ? '10px' : '20px'} style={{ height: '100px' }} />
<div ma=\\"\\">
<div text-5xl=\\"\\" fw100=\\"\\" animate-bounce-alt=\\"\\" animate-count-infinite=\\"\\" animate-duration-1s=\\"\\" key={index}>
unocss
</div>
<div op30=\\"\\" text-lg=\\"\\" fw300=\\"\\" m1=\\"\\" className={hidden && 'op0'}>
The instant on-demand Atomic CSS engine.
</div>
<div m2=\\"\\" flex justify-center=\\"\\" text-2xl=\\"\\" op30=\\"\\" hover-op80=\\"\\" hover-text-2xl=\\"\\">
<a
i-carbon-logo-github
text-inherit=\\"\\"
href=\\"https://github.com/unocss/unocss\\"
target=\\"_blank\\"
></a>
<router-link to={\`/path/\${1}\`}/>
</div>
</div>
<section
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
mr-10=\\"\\"
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
></section>
<div
label={
<b>1</b>
}
></div>
<div absolute bottom-5=\\"\\" right-0=\\"\\" left-0=\\"\\" text-center=\\"\\" op30=\\"\\" fw300=\\"\\">
on-demand · instant · fully customizable
</div>
<div components={<div absolute bottom-5></div>}></div>
</div>"
`)
const codeToString = code.toString()
blocklist.forEach((rule) => {
if (rule instanceof RegExp)
expect(new RegExp(`${rule.source}=""`).test(codeToString)).not.true
else
expect(codeToString).not.toMatch(`${rule}=""`)
})
})
})
describe('transformerAttributifyJsxBabel', () => {
const uno = createGenerator({
presets: [
presetUno(),
presetAttributify(),
],
})
test('transform', async () => {
const code = new MagicString(originalCode)
await transformerAttributifyJsxBabel().transform(code, 'app.tsx', { uno, tokens: new Set() } as any)
expect(code.toString()).toMatchInlineSnapshot(`
"<div h-full=\\"\\" text-center=\\"\\" flex=\\"\\" select-none=\\"\\" className={red ? 'text-red' : 'text-green'}>
<input value={target ? '10px' : '20px'} style={{
height: '100px'
}} />
<div ma=\\"\\">
<div text-5xl=\\"\\" fw100=\\"\\" animate-bounce-alt=\\"\\" animate-count-infinite=\\"\\" animate-duration-1s=\\"\\" key={index}>
unocss
</div>
<div op30=\\"\\" text-lg=\\"\\" fw300=\\"\\" m1=\\"\\" className={hidden && 'op0'}>
The instant on-demand Atomic CSS engine.
</div>
<div m2=\\"\\" flex=\\"\\" justify-center=\\"\\" text-2xl=\\"\\" op30=\\"\\" hover:op80=\\"\\" hover:text-2xl=\\"\\">
<a i-carbon-logo-github text-inherit=\\"\\" href=\\"https://github.com/unocss/unocss\\" target=\\"_blank\\"></a>
<router-link to={\`/path/\${1}\`} />
</div>
</div>
<section className={cn({
'c-red': variable > 0
}, 'mr-10')} mr-10=\\"\\" className={cn({
'c-red': variable > 0
}, 'mr-10')}></section>
<div label={<b>1</b>}></div>
<div absolute=\\"\\" bottom-5=\\"\\" right-0=\\"\\" left-0=\\"\\" text-center=\\"\\" op30=\\"\\" fw300=\\"\\">
on-demand · instant · fully customizable
</div>
<div components={<div absolute=\\"\\" bottom-5=\\"\\"></div>}></div>
</div>;"
`)
})
test('blocklist', async () => {
const code = new MagicString(originalCode)
const blocklist: (string | RegExp)[] = ['flex', 'absolute']
await transformerAttributifyJsxBabel({
blocklist,
}).transform(code, 'app.jsx', { uno, tokens: new Set() } as any)
expect(code.toString()).toMatchInlineSnapshot(`
"<div h-full=\\"\\" text-center=\\"\\" flex select-none=\\"\\" className={red ? 'text-red' : 'text-green'}>
<input value={target ? '10px' : '20px'} style={{
height: '100px'
}} />
<div ma=\\"\\">
<div text-5xl=\\"\\" fw100=\\"\\" animate-bounce-alt=\\"\\" animate-count-infinite=\\"\\" animate-duration-1s=\\"\\" key={index}>
unocss
</div>
<div op30=\\"\\" text-lg=\\"\\" fw300=\\"\\" m1=\\"\\" className={hidden && 'op0'}>
The instant on-demand Atomic CSS engine.
</div>
<div m2=\\"\\" flex justify-center=\\"\\" text-2xl=\\"\\" op30=\\"\\" hover:op80=\\"\\" hover:text-2xl=\\"\\">
<a i-carbon-logo-github text-inherit=\\"\\" href=\\"https://github.com/unocss/unocss\\" target=\\"_blank\\"></a>
<router-link to={\`/path/\${1}\`} />
</div>
</div>
<section className={cn({
'c-red': variable > 0
}, 'mr-10')} mr-10=\\"\\" className={cn({
'c-red': variable > 0
}, 'mr-10')}></section>
<div label={<b>1</b>}></div>
<div absolute bottom-5=\\"\\" right-0=\\"\\" left-0=\\"\\" text-center=\\"\\" op30=\\"\\" fw300=\\"\\">
on-demand · instant · fully customizable
</div>
<div components={<div absolute bottom-5=\\"\\"></div>}></div>
</div>;"
`)
const codeToString = code.toString()
blocklist.forEach((rule) => {
if (rule instanceof RegExp)
expect(new RegExp(`${rule.source}=""`).test(codeToString)).not.true
else
expect(codeToString).not.toMatch(`${rule}=""`)
})
})
})