-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
codegen.ts
377 lines (350 loc) · 11.9 KB
/
codegen.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// @TODO remove @ts-ignore
import { VNode, vModelText, vModelCheckbox, vModelSelect, vModelRadio, vModelDynamic, Text } from 'vue'
import { pascalCase, camelCase } from 'change-case'
import { createAutoBuildingObject, indent, serializeJs, voidElements } from '@histoire/shared'
import type { Variant } from '@histoire/shared'
export async function generateSourceCode (variant: Variant) {
const vnode = variant.slots().default?.({ state: variant.state ?? {} }) ?? []
const list = Array.isArray(vnode) ? vnode : [vnode]
const lines: string[] = []
for (const n in list) {
const vnode = list[n]
lines.push(...(await printVNode(vnode, variant.state?._hPropState?.[n])).lines)
}
return lines.join('\n')
}
async function printVNode (vnode: VNode, propsOverrides: Record<string, any> = null): Promise<{ lines: string[], isText?: boolean }> {
if (vnode.type === Text) {
return {
// @ts-ignore
lines: [vnode.children],
isText: true,
}
}
const lines: string[] = []
if (typeof vnode.type === 'object' || typeof vnode.type === 'string') {
// Wait for async component
// @ts-ignore
if (vnode.type?.__asyncLoader && !vnode.type.__asyncResolved) {
// @ts-ignore
await vnode.type.__asyncLoader()
}
const attrs: string[][] = []
let multilineAttrs = false
const skipProps: string[] = [
'key',
]
// Directives
function genDirective (dirName: string, dir, valueCode: string = null) {
let modifiers = ''
for (const key in dir.modifiers) {
if (dir.modifiers[key]) {
modifiers += `.${key}`
}
}
let arg = ''
if (dir.arg) {
arg = `:${dir.arg}`
}
if (valueCode) {
// Cleanup render code
valueCode = valueCode.replace(/^\$(setup|props|data)\./g, '')
}
const valueLines = valueCode ? [valueCode] : serializeAndCleanJs(dir.value)
const attr: string[] = []
const dirAttr = `v-${dirName}${arg}${modifiers}="`
if (valueLines.length > 1) {
attr.push(`${dirAttr}${valueLines[0]}`)
attr.push(...valueLines.slice(1, valueLines.length - 1))
attr.push(`${valueLines[valueLines.length - 1]}"`)
multilineAttrs = true
} else {
attr.push(`${dirAttr}${valueLines[0] ?? ''}"`)
}
attrs.push(attr)
}
if (vnode.dirs) {
for (const dir of vnode.dirs) {
// Vmodel
if (dir.dir === vModelText || dir.dir === vModelSelect || dir.dir === vModelRadio || dir.dir === vModelCheckbox || dir.dir === vModelDynamic) {
const listenerKeys = [`onUpdate:${dir.arg ?? 'modelValue'}`, `onUpdate:${camelCase(dir.arg ?? 'modelValue')}`]
const listenerKey = listenerKeys.find(key => vnode.props[key])
const listener = vnode.props[listenerKey]
let valueCode: string = null
if (listener) {
skipProps.push(listenerKey)
const listenerSource = listener.toString()
const result = /\(\$event\) => (.*?) = \$event/.exec(listenerSource)
if (result) {
valueCode = result[1]
}
}
genDirective('model', dir, valueCode)
// @ts-ignore
} else if (dir.instance._ || dir.instance.$) {
// @ts-ignore
const target = dir.instance.$ ?? dir.instance._
let dirName: string
for (const directives of [target.directives, target.appContext.directives]) {
for (const key in directives) {
if (directives[key] === dir.dir) {
dirName = key
break
}
}
if (dirName) break
}
// Check in setup state
if (!dirName) {
for (const key in target.setupState) {
if (target.setupState[key] === dir.dir) {
dirName = key.replace(/^v(\w)/, (_, p1) => p1.toLowerCase())
break
}
}
}
if (dirName) {
genDirective(dirName, dir)
}
}
}
}
// Attributes
function addAttr (prop: string, value: any) {
// @ts-ignore
if (typeof value !== 'string' || vnode.dynamicProps?.includes(prop)) {
let directive = ':'
if (prop.startsWith('on')) {
directive = '@'
}
const arg = directive === '@' ? `${prop[2].toLowerCase()}${prop.slice(3)}` : prop
// v-model on component
const vmodelListeners = [`onUpdate:${prop}`, `onUpdate:${camelCase(prop)}`]
// @ts-ignore
const vmodelListener = vmodelListeners.find(key => vnode.dynamicProps?.includes(key) || (vnode.props && key in vnode.props))
if (directive === ':' && vmodelListener) {
// Listener
skipProps.push(vmodelListener)
const listener = vnode.props[vmodelListener]
const listenerSource = listener.toString()
let valueCode: string
const result = /\(\$event\) => (.*?) = \$event/.exec(listenerSource)
if (result) {
valueCode = result[1]
}
// Modifiers
const modifiersKey = `${prop === 'modelValue' ? 'model' : prop}Modifiers`
const modifiers = vnode.props[modifiersKey] ?? {}
skipProps.push(modifiersKey)
// Directive
genDirective('model', {
arg: prop === 'modelValue' ? null : prop,
modifiers,
value,
}, valueCode)
return
}
if (typeof value === 'undefined') {
return
}
let serialized: string[]
if (typeof value === 'string' && value.startsWith('{{') && value.endsWith('}}')) {
// It was formatted from auto building object (slot props)
serialized = cleanupExpression(value.substring(2, value.length - 2).trim()).split('\n')
} else if (typeof value === 'function') {
let code = cleanupExpression(value.toString().replace(/'/g, '\\\'').replace(/"/g, '\''))
const testResult = /function ([^\s]+)\(/.exec(code)
if (testResult) {
// Function name only
serialized = [testResult[1]]
} else {
if (code.startsWith('($event) => ')) {
// Remove unnecessary `($event) => `
code = code.substring('($event) => '.length)
}
serialized = code.split('\n')
}
} else {
serialized = serializeAndCleanJs(value)
}
if (serialized.length > 1) {
multilineAttrs = true
const indented: string[] = [`${directive}${arg}="${serialized[0]}`]
indented.push(...serialized.slice(1, serialized.length - 1))
indented.push(`${serialized[serialized.length - 1]}"`)
attrs.push(indented)
} else {
attrs.push([`${directive}${arg}="${serialized[0]}"`])
}
// @ts-ignore
} else if (vnode.type?.props?.[prop]?.type === Boolean) {
attrs.push([prop])
} else {
attrs.push([`${prop}="${value}"`])
}
}
for (const prop in vnode.props) {
if (skipProps.includes(prop) || (propsOverrides && prop in propsOverrides)) {
continue
}
const value = vnode.props[prop]
addAttr(prop, value)
}
if (propsOverrides) {
for (const prop in propsOverrides) {
addAttr(prop, propsOverrides[prop])
}
}
if (attrs.length > 1) {
multilineAttrs = true
}
// Tags
const tagName = getTagName(vnode)
// Children
let isChildText = false
const childLines: string[] = []
if (typeof vnode.children === 'string') {
if (tagName === 'pre') {
childLines.push(vnode.children)
} else {
childLines.push(...vnode.children.split('\n'))
}
isChildText = true
} else if (Array.isArray(vnode.children)) {
let isAllChildText
for (const child of vnode.children) {
// @ts-ignore
const result = await printVNode(child)
if (result.isText) {
if (isAllChildText === undefined) {
isAllChildText = true
}
const text = result.lines[0]
if (!childLines.length || /^\s/.test(text)) {
childLines.push(text.trim())
} else {
childLines[childLines.length - 1] += text
}
} else {
if (isAllChildText === undefined) {
isAllChildText = false
}
childLines.push(...result.lines)
}
}
if (isAllChildText !== undefined) {
isChildText = isAllChildText
}
}
// Slots
if (vnode.children && typeof vnode.children === 'object' && !Array.isArray(vnode.children)) {
for (const key in vnode.children) {
if (typeof vnode.children[key] === 'function') {
const autoObject = createAutoBuildingObject(key => `{{ ${key} }}`, (target, p) => {
// Vue 3
if (p === '__v_isRef') {
return () => false
}
})
// @ts-ignore
const children = vnode.children[key](autoObject.proxy)
const slotLines: string[] = []
for (const child of children) {
slotLines.push(...(await printVNode(child)).lines)
}
const slotProps = Object.keys(autoObject.cache)
if (slotProps.length) {
childLines.push(`<template #${key}="{ ${slotProps.join(', ')} }">`)
childLines.push(...indent(slotLines))
childLines.push('</template>')
} else if (key === 'default') {
childLines.push(...slotLines)
} else {
childLines.push(`<template #${key}>`)
childLines.push(...indent(slotLines))
childLines.push(`</template>`)
}
}
}
}
// Template
const tag = [`<${tagName}`]
if (multilineAttrs) {
for (const attrLines of attrs) {
tag.push(...indent(attrLines))
}
if (childLines.length > 0) {
tag.push('>')
}
} else {
if (attrs.length === 1) {
tag[0] += ` ${attrs[0]}`
}
if (childLines.length > 0) {
tag[0] += '>'
}
}
const isVoid = voidElements.includes(tagName.toLowerCase())
if (childLines.length > 0) {
if (childLines.length === 1 && tag.length === 1 && !attrs.length && isChildText) {
lines.push(`${tag[0]}${childLines[0]}</${tagName}>`)
} else {
lines.push(...tag)
lines.push(...indent(childLines))
lines.push(`</${tagName}>`)
}
} else if (tag.length > 1) {
lines.push(...tag)
lines.push(isVoid ? '>' : '/>')
} else {
lines.push(`${tag[0]}${isVoid ? '' : ' /'}>`)
}
} else if (vnode?.shapeFlag & 1 << 4) {
// @ts-ignore
for (const child of vnode.children) {
lines.push(...(await printVNode(child)).lines)
}
}
return {
lines,
}
}
export function getTagName (vnode: VNode) {
if (typeof vnode.type === 'string') {
return vnode.type
// @ts-ignore
} else if (vnode.type?.__asyncResolved) {
// @ts-ignore
const asyncComp = vnode.type?.__asyncResolved
return asyncComp.name ?? getNameFromFile(asyncComp.__file)
// @ts-ignore
} else if (vnode.type?.name) {
// @ts-ignore
return vnode.type.name
// @ts-ignore
} else if (vnode.type?.__file) {
// @ts-ignore
return getNameFromFile(vnode.type.__file)
}
return 'Anonymous'
}
function getNameFromFile (file: string) {
const parts = /([^/]+)\.vue$/.exec(file)
if (parts) {
return pascalCase(parts[1])
}
return 'Anonymous'
}
function serializeAndCleanJs (value: any) {
const isAutoBuildingObject = !!value?.__autoBuildingObject
const result = serializeJs(value)
if (isAutoBuildingObject) {
// @ts-ignore
return [cleanupExpression(result.__autoBuildingObjectGetKey)]
} else {
return cleanupExpression(result).split('\n')
}
}
function cleanupExpression (expr: string) {
return expr.replace(/\$setup\./g, '')
}