-
-
Notifications
You must be signed in to change notification settings - Fork 788
/
processExpressions.ts
134 lines (121 loc) · 4.2 KB
/
processExpressions.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
import type { UnoGenerator } from '@unocss/core'
import type { TransformClassesOptions } from '../types'
import type { ProcessResult } from './processClasses'
import { sortClassesIntoCategories } from './sortClassesIntoCategories'
import { shortcutName, unoMock } from './unoMock'
const expressionsRE = /\S*{[^{}]+?}\S*/g // { foo ? 'mt-1' : 'mt-2'}, \S* handles expressions as partial class name as in bg-{color}-100
const classesRE = /(["'\`])([\S\s]*?)\1/g // 'mt-1 mr-1'
export async function processExpressions(
body: string,
options: TransformClassesOptions,
uno: UnoGenerator,
filename: string,
) {
const rulesToGenerate: ProcessResult['rulesToGenerate'] = {}
const updatedExpressions: string[] = []
let restOfBody = body
const expressions = [...body.matchAll(expressionsRE)]
for (let [expression] of expressions) {
restOfBody = restOfBody.replace(expression, '').trim()
const classes = [...expression.matchAll(classesRE)]
for (const [withQuotes, quoteMark, withoutQuotes] of classes) {
const { rulesToGenerate: rulesFromExpression, ignore } = await sortClassesIntoCategories(withoutQuotes, options, uno, filename)
Object.assign(rulesToGenerate, rulesFromExpression)
const updatedClasses = Object.keys(rulesFromExpression)
.concat(ignore)
.join(' ')
expression = expression.replace(withQuotes, quoteMark + updatedClasses + quoteMark)
}
updatedExpressions.push(expression)
}
return { rulesToGenerate, updatedExpressions, restOfBody }
}
if (import.meta.vitest) {
const { describe, expect, it } = import.meta.vitest
describe('processExpressions', () => {
describe('everything', () => {
const body = `font-bold {bar ? 'text-red-600' : 'text-green-600 ${shortcutName} text-lg boo'} underline foo {baz ? 'italic ' : ''}`
it('combined', async () => {
expect(await processExpressions(body, {}, unoMock, 'Foo.svelte')).toMatchInlineSnapshot(`
{
"restOfBody": "font-bold underline foo",
"rulesToGenerate": {
"uno-br1nw8": [
"italic",
],
"uno-ffvc5a": [
"text-red-600",
],
"uno-oaete6": [
"text-green-600",
"my-shortcut",
"text-lg",
],
},
"updatedExpressions": [
"{bar ? 'uno-ffvc5a' : 'uno-oaete6 boo'}",
"{baz ? 'uno-br1nw8' : ''}",
],
}
`)
})
it('uncombined', async () => {
expect(await processExpressions(body, { combine: false }, unoMock, 'Foo.svelte')).toMatchInlineSnapshot(`
{
"restOfBody": "font-bold underline foo",
"rulesToGenerate": {
"_italic_7dkb0w": [
"italic",
],
"_my-shortcut_7dkb0w": [
"my-shortcut",
],
"_text-green-600_7dkb0w": [
"text-green-600",
],
"_text-lg_7dkb0w": [
"text-lg",
],
"_text-red-600_7dkb0w": [
"text-red-600",
],
},
"updatedExpressions": [
"{bar ? '_text-red-600_7dkb0w' : '_text-green-600_7dkb0w _my-shortcut_7dkb0w _text-lg_7dkb0w boo'}",
"{baz ? '_italic_7dkb0w' : ''}",
],
}
`)
})
})
it('handles expression as only part of a class', async () => {
const body = 'mr-1 pr{os}e bg-{color}'
expect(await processExpressions(body, {}, unoMock, 'Foo.svelte')).toMatchInlineSnapshot(`
{
"restOfBody": "mr-1",
"rulesToGenerate": {},
"updatedExpressions": [
"pr{os}e",
"bg-{color}",
],
}
`)
})
it('handles empty string in expression', async () => {
const body = 'font-bold {grid ? \'\' : \'mr-1\'}'
expect(await processExpressions(body, {}, unoMock, 'Foo.svelte')).toMatchInlineSnapshot(`
{
"restOfBody": "font-bold",
"rulesToGenerate": {
"uno-76ckap": [
"mr-1",
],
},
"updatedExpressions": [
"{grid ? '' : 'uno-76ckap'}",
],
}
`)
})
})
}