/
ast.ts
176 lines (159 loc) · 4.83 KB
/
ast.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
import { walkIdentifiers } from '@vue/compiler-sfc'
import { isFunctionType, isLiteralType, resolveObjectKey } from 'ast-kit'
import type { MagicString } from 'magic-string-ast'
import type * as t from '@babel/types'
export function checkInvalidScopeReference(
node: t.Node | undefined,
method: string,
setupBindings: string[],
) {
if (!node) return
walkIdentifiers(node, (id) => {
if (setupBindings.includes(id.name))
throw new SyntaxError(
`\`${method}()\` in <script setup> cannot reference locally ` +
`declared variables (${id.name}) because it will be hoisted outside of the ` +
`setup() function.`,
)
})
}
export function isStaticExpression(
node: t.Node,
options: Partial<
Record<
'object' | 'fn' | 'objectMethod' | 'array' | 'unary' | 'regex',
boolean
> & {
magicComment?: string
}
> = {},
): boolean {
const { magicComment, fn, object, objectMethod, array, unary, regex } =
options
// magic comment
if (
magicComment &&
node.leadingComments?.some(
(comment) => comment.value.trim() === magicComment,
)
)
return true
else if (fn && isFunctionType(node)) return true
switch (node.type) {
case 'UnaryExpression': // !true
return !!unary && isStaticExpression(node.argument, options)
case 'LogicalExpression': // 1 > 2
case 'BinaryExpression': // 1 + 2
return (
isStaticExpression(node.left, options) &&
isStaticExpression(node.right, options)
)
case 'ConditionalExpression': // 1 ? 2 : 3
return (
isStaticExpression(node.test, options) &&
isStaticExpression(node.consequent, options) &&
isStaticExpression(node.alternate, options)
)
case 'SequenceExpression': // (1, 2)
case 'TemplateLiteral': // `123`
return node.expressions.every((expr) => isStaticExpression(expr, options))
case 'ArrayExpression': // [1, 2]
return (
!!array &&
node.elements.every(
(element) => element && isStaticExpression(element, options),
)
)
case 'ObjectExpression': // { foo: 1 }
return (
!!object &&
node.properties.every((prop) => {
if (prop.type === 'SpreadElement') {
return (
prop.argument.type === 'ObjectExpression' &&
isStaticExpression(prop.argument, options)
)
} else if (!isLiteralType(prop.key) && prop.computed) {
return false
} else if (
prop.type === 'ObjectProperty' &&
!isStaticExpression(prop.value, options)
) {
return false
}
if (prop.type === 'ObjectMethod' && !objectMethod) {
return false
}
return true
})
)
case 'ParenthesizedExpression': // (1)
case 'TSNonNullExpression': // 1!
case 'TSAsExpression': // 1 as number
case 'TSTypeAssertion': // (<number>2)
case 'TSSatisfiesExpression': // 1 satisfies number
return isStaticExpression(node.expression, options)
case 'RegExpLiteral':
return !!regex
}
if (isLiteralType(node)) return true
return false
}
export function isStaticObjectKey(node: t.ObjectExpression): boolean {
return node.properties.every((prop) => {
if (prop.type === 'SpreadElement') {
return (
prop.argument.type === 'ObjectExpression' &&
isStaticObjectKey(prop.argument)
)
}
return !prop.computed || isLiteralType(prop.key)
})
}
/**
* @param node must be a static expression, SpreadElement is not supported
*/
export function resolveObjectExpression(node: t.ObjectExpression) {
const maps: Record<string | number, t.ObjectMethod | t.ObjectProperty> =
Object.create(null)
for (const property of node.properties) {
if (property.type === 'SpreadElement') {
if (property.argument.type !== 'ObjectExpression')
// not supported
return undefined
Object.assign(maps, resolveObjectExpression(property.argument)!)
} else {
const key = resolveObjectKey(property)
maps[key] = property
}
}
return maps
}
const importedMap = new WeakMap<MagicString, Set<string>>()
export const HELPER_PREFIX = '__MACROS_'
export function importHelperFn(
s: MagicString,
offset: number,
local: string,
from = 'vue',
isDefault = false,
) {
const imported = isDefault ? 'default' : local
const cacheKey = `${from}@${imported}`
if (!importedMap.get(s)?.has(cacheKey)) {
s.appendLeft(
offset,
`\nimport ${
isDefault
? HELPER_PREFIX + local
: `{ ${imported} as ${HELPER_PREFIX + local} }`
} from ${JSON.stringify(from)};`,
)
if (!importedMap.has(s)) {
importedMap.set(s, new Set([cacheKey]))
} else {
importedMap.get(s)!.add(cacheKey)
}
}
return `${HELPER_PREFIX}${local}`
}