-
Notifications
You must be signed in to change notification settings - Fork 134
/
format.test.ts
341 lines (317 loc) · 11.3 KB
/
format.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import { describe, test } from 'vitest'
import type { TestEntry } from './utils.js'
import { format, no, t, yes } from './utils.js'
let html: TestEntry[] = [
t`<div class="${yes}"></div>`,
t`<!-- <div class="${no}"></div> -->`,
t`<div class="${no} {{ 'p-0 sm:p-0 m-0' }}"></div>`,
t`<div not-class="${no}"></div>`,
['<div class=" sm:p-0 p-0 "></div>', '<div class="p-0 sm:p-0"></div>'],
t`<div class></div>`,
t`<div class=""></div>`,
// Ensure duplicate classes are removed
['<div class="sm:p-0 p-0 p-0"></div>', '<div class="p-0 sm:p-0"></div>'],
// Duplicates are not removed for unknown classes
[
'<div class="idonotexist sm:p-0 p-0 idonotexist p-0 idonotexist"></div>',
'<div class="idonotexist idonotexist idonotexist p-0 sm:p-0"></div>',
],
// Ensure duplicate can be kept
[
'<div class="sm:p-0 p-0 p-0"></div>',
'<div class="p-0 p-0 sm:p-0"></div>',
{
tailwindPreserveDuplicates: true,
},
],
]
let css: TestEntry[] = [
t`@apply ${yes};`,
t`/* @apply ${no}; */`,
t`@not-apply ${no};`,
[
'@apply sm:p-0\n p-0;',
'@apply p-0\n sm:p-0;',
{ tailwindPreserveWhitespace: true },
],
]
let javascript: TestEntry[] = [
t`;<div class="${yes}" />`,
t`;<div ns:class="${no}" />`,
t`/* <div class="${no}" /> */`,
t`// <div class="${no}" />`,
t`;<div not-class="${no}" />`,
t`;<div class={\`${yes}\`} />`,
t`;<div class={\`${yes} \${'${yes}'} \${'${yes}' ? '${yes}' : '${yes}'}\`} />`,
t`;<div class={'${yes}'} />`,
t`;<div class={'${yes}' + '${yes}'} />`,
t`;<div class={'${yes}' ? '${yes}' + '${yes}' : '${yes}'} />`,
t`;<div class={clsx('${yes}', ['${yes}'])} />`,
t`;<div class={clsx({ '${yes}': '${yes}' })} />`,
t`;<div class={{ '${yes}': '${yes}' }['${yes}']} />`,
t`;<div class />`,
t`;<div class="" />`,
[
`;<div class={\`sm:block inline flex\${someVar}\`} />`,
`;<div class={\`inline sm:block flex\${someVar}\`} />`,
],
[
`;<div class={\`\${someVar}sm:block md:inline flex\`} />`,
`;<div class={\`\${someVar}sm:block flex md:inline\`} />`,
],
[
`;<div class={\`sm:p-0 p-0 \${someVar}sm:block md:inline flex\`} />`,
`;<div class={\`p-0 sm:p-0 \${someVar}sm:block flex md:inline\`} />`,
],
[
`;<div class="block px-1\u3000py-2" />`,
`;<div class="px-1\u3000py-2 block" />`,
],
// Whitespace is normalized and duplicates are removed
[
';<div class=" m-0 sm:p-0 p-0 " />',
';<div class="m-0 p-0 sm:p-0" />',
],
[
";<div class={' m-0 sm:p-0 p-0 '} />",
";<div class={'m-0 p-0 sm:p-0'} />",
],
[';<div class={` sm:p-0\n p-0 `} />', ';<div class={`p-0 sm:p-0`} />'],
[';<div class="flex flex" />', ';<div class="flex" />'],
[';<div class={` flex flex `} />', ';<div class={`flex`} />'],
[
';<div class={` flex flex flex${someVar}block block`} />',
';<div class={`flex flex${someVar}block block`} />',
],
[
';<div class={`flex ` + `text-red-500`} />',
';<div class={`flex ` + `text-red-500`} />',
],
[
';<div class={`flex ` + ` ` + `text-red-500`} />',
';<div class={`flex ` + ` ` + `text-red-500`} />',
],
t`;<div class={"before:content-['\\\\2248']"} />`,
t`;<div class={\`before:content-['\\\\2248']\`} />`,
t`;<div class="before:content-['\\\\2248']" />`,
[
`;<div class={'object-cover' + (standalone ? ' aspect-square w-full' : ' min-h-0 grow basis-0')}></div>`,
`;<div class={'object-cover' + (standalone ? ' aspect-square w-full' : ' min-h-0 grow basis-0')}></div>`,
],
]
javascript = javascript.concat(
javascript.map((test) => [
test[0].replace(/class/g, 'className'),
test[1].replace(/class/g, 'className'),
test[2],
]),
)
let vue: TestEntry[] = [
...html,
t`<div :class="'${yes}'"></div>`,
t`<!-- <div :class="'${no}'"></div> -->`,
t`<div :class></div>`,
t`<div :class=""></div>`,
t`<div :class="'${yes}' + '${yes}'"></div>`,
t`<div :class="['${yes}', '${yes}']"></div>`,
t`<div :class="[cond ? '${yes}' : '${yes}']"></div>`,
t`<div :class="[someVar ?? '${yes}']"></div>`,
t`<div :class="{ '${yes}': true }"></div>`,
t`<div :class="clsx('${yes}')"></div>`,
t`<div :class="\`${yes}\`"></div>`,
t`<div :class="\`${yes} \${someVar}\`"></div>`,
t`<div :class="someVar! ? \`${yes}\` : \`${yes}\`"></div>`, // ts
t`<div :class="someVar ? someFunc(someVar as string) + '${yes}' : ''"></div>`, // ts
[
`<div :class="\`sm:block inline flex\${someVar}\`"></div>`,
`<div :class="\`inline sm:block flex\${someVar}\`"></div>`,
],
[
`<div :class="\`\${someVar}sm:block md:inline flex\`"></div>`,
`<div :class="\`\${someVar}sm:block flex md:inline\`"></div>`,
],
[
`<div :class="\`sm:p-0 p-0 \${someVar}sm:block md:inline flex\`"></div>`,
`<div :class="\`p-0 sm:p-0 \${someVar}sm:block flex md:inline\`"></div>`,
],
[`<div :class="' flex flex '"></div>`, `<div :class="'flex'"></div>`],
[`<div :class="\` flex flex \`"></div>`, `<div :class="\`flex\`"></div>`],
[
`<div :class="' flex ' + ' underline '"></div>`,
`<div :class="'flex ' + ' underline'"></div>`,
],
[
`<div :class="' sm:p-5 ' + ' flex ' + ' underline ' + ' sm:m-5 '"></div>`,
`<div :class="'sm:p-5 ' + ' flex' + ' underline' + ' sm:m-5'"></div>`,
],
[
`<div :class="'before:content-[\\'\\\\2248\\']'" />`,
`<div :class="'before:content-[\\'\\\\2248\\']'" />`,
],
]
let glimmer: TestEntry[] = [
t`<div class='${yes}'></div>`,
t`<!-- <div class='${no}'></div> -->`,
t`<div class='${yes} {{"${yes}"}}'></div>`,
t`<div class='${yes} {{"${yes}"}} ${yes}'></div>`,
t`<div class='${yes} {{"${yes}"}} {{if someVar "${yes}" "${yes}"}}'></div>`,
t`<div class='${yes} {{"${yes}"}} {{if someVar "${yes}" "${yes}"}}' {{if someVar "attr='${no}'" "attr='${no}'"}}></div>`,
[
`<div class='md:inline flex sm:block{{someVar}}'></div>`,
`<div class='flex md:inline sm:block{{someVar}}'></div>`,
],
[
`<div class='sm:p-0 p-0 {{someVar}}sm:block md:inline flex'></div>`,
`<div class='p-0 sm:p-0 {{someVar}}sm:block flex md:inline'></div>`,
],
t`<div not-class='${no}'></div>`,
["<div class=' sm:p-0 p-0 '></div>", "<div class='p-0 sm:p-0'></div>"],
t`<div class></div>`,
t`<div class=''></div>`,
t`{{link 'Some page' href=person.url class='${no}'}}`,
t`<div class='{{if @isTrue (concat "border-l-4 border-" @borderColor)}}'></div>`,
[
`<div class='{{if @isTrue (concat "border-opacity-30 border-l-4 border-" @borderColor)}}'></div>`,
`<div class='{{if @isTrue (concat "border-l-4 border-opacity-30 border-" @borderColor)}}'></div>`,
],
[
`<div class='{{if @isTrue (concat "border-l-4 border " @borderColor)}}'></div>`,
`<div class='{{if @isTrue (concat "border border-l-4 " @borderColor)}}'></div>`,
],
[
`<div class='{{if @isTrue (nope "border-l-4 border-" @borderColor)}}'></div>`,
`<div class='{{if @isTrue (nope "border- border-l-4" @borderColor)}}'></div>`,
],
[`<div class='flex flex '></div>`, `<div class='flex'></div>`],
[
`<div class='sm:p-0 p-0 p-0 {{someVar}}sm:block flex md:inline flex '></div>`,
`<div class='p-0 sm:p-0 {{someVar}}sm:block flex md:inline'></div>`,
],
]
let tests: Record<string, TestEntry[]> = {
html,
glimmer,
lwc: html,
vue: [
...vue,
t`<div :class="\`${yes} \${someVar} ${yes} \${'${yes}'}\`"></div>`,
],
angular: [
...html,
t`<div [ngClass]="'${yes}'"></div>`,
t`<!-- <div [ngClass]="'${no}'"></div> -->`,
t`<div [ngClass]></div>`,
t`<div [ngClass]=""></div>`,
t`<div [ngClass]="'${yes}' + '${yes}'"></div>`,
t`<div [ngClass]="['${yes}', '${yes}']"></div>`,
t`<div [ngClass]="[cond ? '${yes}' : '${yes}']"></div>`,
t`<div [ngClass]="[someVar ?? '${yes}']"></div>`,
t`<div [ngClass]="{ '${yes}': true }"></div>`,
t`<div [ngClass]="clsx('${yes}')"></div>`,
t`<div [ngClass]="{ '${yes}': (some.thing | urlPipe: { option: true } | async), '${yes}': true }"></div>`,
t`<div [ngClass]="{ '${yes}': foo && bar?.['baz'] }" class="${yes}"></div>`,
[
`<div [ngClass]="' flex ' + ' underline ' + ' block '"></div>`,
`<div [ngClass]="'flex ' + ' underline' + ' block'"></div>`,
],
// TODO: Enable this test — it causes console noise but not a failure
// t`<div [ngClass]="{ '${no}': foo && definitely&a:syntax*error }" class="${yes}"></div>`,
],
css: [...css, t`@apply ${yes} !important;`],
scss: [
...css,
t`@apply ${yes} #{!important};`,
t`@apply ${yes} #{'!important'};`,
t`@apply ${yes} #{"!important"};`,
// These shouldn't ever be used but they are valid
// syntax so we might as well not break them
t`@apply ${yes} #{""!important""};`,
t`@apply ${yes} #{'''!important'''};`,
t`@apply ${yes} #{"'"'"!important"'"'"};`,
],
less: [...css, t`@apply ${yes} !important;`],
babel: javascript,
typescript: javascript,
'babel-ts': javascript,
flow: javascript,
'babel-flow': javascript,
acorn: javascript,
meriyah: javascript,
mdx: javascript
.filter((test) => {
return !/^\/\*/.test(test[0]) && !/^\/\*/.test(test[1])
})
.map((test) => [
test[0].replace(/^;/, ''),
test[1].replace(/^;/, ''),
test[2],
]),
}
describe('parsers', () => {
for (let parser in tests) {
test(parser, async ({ expect }) => {
for (let [input, expected, options] of tests[parser]) {
expect(await format(input, { ...options, parser })).toEqual(expected)
}
})
}
})
describe('other', () => {
test('non-tailwind classes', async ({ expect }) => {
expect(
await format('<div class="sm:lowercase uppercase potato text-sm"></div>'),
).toEqual('<div class="potato text-sm uppercase sm:lowercase"></div>')
})
test('parasite utilities', async ({ expect }) => {
expect(
await format(
'<div class="group peer unknown-class p-0 container"></div>',
),
).toEqual('<div class="unknown-class group peer container p-0"></div>')
})
})
describe('whitespace', () => {
test('class lists containing interpolation are ignored', async ({
expect,
}) => {
let result = await format('<div class="{{ this is ignored }}"></div>')
expect(result).toEqual('<div class="{{ this is ignored }}"></div>')
})
test('whitespace can be preserved around classes', async ({ expect }) => {
let result = await format(
`;<div className={' underline text-red-500 flex '}></div>`,
{
parser: 'babel',
tailwindPreserveWhitespace: true,
},
)
expect(result).toEqual(
`;<div className={' flex text-red-500 underline '}></div>`,
)
})
test('whitespace can be collapsed around classes', async ({ expect }) => {
let result = await format(
'<div class=" underline text-red-500 flex "></div>',
)
expect(result).toEqual('<div class="flex text-red-500 underline"></div>')
})
test('whitespace is collapsed but not trimmed when ignored', async ({
expect,
}) => {
let result = await format(
';<div className={`underline text-red-500 ${foo}-bar flex`}></div>',
{
parser: 'babel',
},
)
expect(result).toEqual(
';<div className={`text-red-500 underline ${foo}-bar flex`}></div>',
)
})
test('duplicate classes are dropped', async ({ expect }) => {
let result = await format(
'<div class="underline line-through underline flex"></div>',
)
expect(result).toEqual('<div class="flex underline line-through"></div>')
})
})