/
hoistStatic.spec.ts
219 lines (206 loc) 路 5.41 KB
/
hoistStatic.spec.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
import { BindingTypes } from '@vue/compiler-core'
import { SFCScriptCompileOptions } from '../../src'
import { compileSFCScript, assertCode } from '../utils'
describe('sfc hoist static', () => {
function compile(src: string, options?: Partial<SFCScriptCompileOptions>) {
return compileSFCScript(src, {
inlineTemplate: true,
hoistStatic: true,
...options
})
}
test('should hoist literal value', () => {
const code = `
const string = 'default value'
const number = 123
const boolean = false
const nil = null
const bigint = 100n
const template = \`str\`
`.trim()
const { content, bindings } = compile(`
<script setup>
${code}
</script>
`)
// should hoist to first line
expect(content.startsWith(code)).toBe(true)
expect(bindings).toStrictEqual({
string: BindingTypes.LITERAL_CONST,
number: BindingTypes.LITERAL_CONST,
boolean: BindingTypes.LITERAL_CONST,
nil: BindingTypes.LITERAL_CONST,
bigint: BindingTypes.LITERAL_CONST,
template: BindingTypes.LITERAL_CONST
})
assertCode(content)
})
test('should hoist expressions', () => {
const code = `
const unary = !false
const binary = 1 + 2
const conditional = 1 ? 2 : 3
const sequence = (1, true, 'foo', 1)
`.trim()
const { content, bindings } = compile(`
<script setup>
${code}
</script>
`)
// should hoist to first line
expect(content.startsWith(code)).toBe(true)
expect(bindings).toStrictEqual({
binary: BindingTypes.LITERAL_CONST,
conditional: BindingTypes.LITERAL_CONST,
unary: BindingTypes.LITERAL_CONST,
sequence: BindingTypes.LITERAL_CONST
})
assertCode(content)
})
test('should hoist w/ defineProps/Emits', () => {
const hoistCode = `const defaultValue = 'default value'`
const { content, bindings } = compile(`
<script setup>
${hoistCode}
defineProps({
foo: {
default: defaultValue
}
})
</script>
`)
// should hoist to first line
expect(content.startsWith(hoistCode)).toBe(true)
expect(bindings).toStrictEqual({
foo: BindingTypes.PROPS,
defaultValue: BindingTypes.LITERAL_CONST
})
assertCode(content)
})
test('should not hoist a variable', () => {
const code = `
let KEY1 = 'default value'
var KEY2 = 123
const regex = /.*/g
const undef = undefined
`.trim()
const { content, bindings } = compile(`
<script setup>
${code}
</script>
`)
expect(bindings).toStrictEqual({
KEY1: BindingTypes.SETUP_LET,
KEY2: BindingTypes.SETUP_LET,
regex: BindingTypes.SETUP_CONST,
undef: BindingTypes.SETUP_MAYBE_REF
})
expect(content).toMatch(`setup(__props) {\n\n ${code}`)
assertCode(content)
})
test('should not hoist a constant initialized to a reference value', () => {
const code = `
const KEY1 = Boolean
const KEY2 = [Boolean]
const KEY3 = [getCurrentInstance()]
let i = 0;
const KEY4 = (i++, 'foo')
enum KEY5 {
FOO = 1,
BAR = getCurrentInstance(),
}
const KEY6 = \`template\${i}\`
`.trim()
const { content, bindings } = compile(`
<script setup lang="ts">
${code}
</script>
`)
expect(bindings).toStrictEqual({
KEY1: BindingTypes.SETUP_MAYBE_REF,
KEY2: BindingTypes.SETUP_CONST,
KEY3: BindingTypes.SETUP_CONST,
KEY4: BindingTypes.SETUP_CONST,
KEY5: BindingTypes.SETUP_CONST,
KEY6: BindingTypes.SETUP_CONST,
i: BindingTypes.SETUP_LET
})
expect(content).toMatch(`setup(__props) {\n\n ${code}`)
assertCode(content)
})
test('should not hoist a object or array', () => {
const code = `
const obj = { foo: 'bar' }
const arr = [1, 2, 3]
`.trim()
const { content, bindings } = compile(`
<script setup>
${code}
</script>
`)
expect(bindings).toStrictEqual({
arr: BindingTypes.SETUP_CONST,
obj: BindingTypes.SETUP_CONST
})
expect(content).toMatch(`setup(__props) {\n\n ${code}`)
assertCode(content)
})
test('should not hoist a function or class', () => {
const code = `
const fn = () => {}
function fn2() {}
class Foo {}
`.trim()
const { content, bindings } = compile(`
<script setup>
${code}
</script>
`)
expect(bindings).toStrictEqual({
Foo: BindingTypes.SETUP_CONST,
fn: BindingTypes.SETUP_CONST,
fn2: BindingTypes.SETUP_CONST
})
expect(content).toMatch(`setup(__props) {\n\n ${code}`)
assertCode(content)
})
test('should enable when only script setup', () => {
const { content, bindings } = compile(`
<script>
const foo = 'bar'
</script>
<script setup>
const foo = 'bar'
</script>
`)
expect(bindings).toStrictEqual({
foo: BindingTypes.SETUP_CONST
})
assertCode(content)
})
test('should not hoist when disabled', () => {
const { content, bindings } = compile(
`
<script setup>
const foo = 'bar'
</script>
`,
{ hoistStatic: false }
)
expect(bindings).toStrictEqual({
foo: BindingTypes.SETUP_CONST
})
assertCode(content)
})
test('template binding access in inline mode', () => {
const { content } = compile(
`
<script setup>
const foo = 'bar'
</script>
<template>{{ foo }}</template>
`
)
expect(content).toMatch('_toDisplayString(foo)')
})
})