forked from vitejs/vite-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
159 lines (150 loc) · 4.4 KB
/
utils.ts
File metadata and controls
159 lines (150 loc) · 4.4 KB
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
import { tinyassert } from '@hiogawa/utils'
import type { ExportDefaultDeclaration } from 'estree'
import type { Identifier, Node, Pattern, Program } from 'estree'
export function hasDirective(
body: Program['body'],
directive: string,
): boolean {
return !!body.find(
(stmt) =>
stmt.type === 'ExpressionStatement' &&
stmt.expression.type === 'Literal' &&
typeof stmt.expression.value === 'string' &&
stmt.expression.value === directive,
)
}
export function getExportNames(
ast: Program,
options: {
ignoreExportAllDeclaration?: boolean
},
): {
exportNames: string[]
} {
const exportNames: string[] = []
for (const node of ast.body) {
if (node.type === 'ExportNamedDeclaration') {
if (node.declaration) {
if (
node.declaration.type === 'FunctionDeclaration' ||
node.declaration.type === 'ClassDeclaration'
) {
/**
* export function foo() {}
*/
exportNames.push(node.declaration.id.name)
} else if (node.declaration.type === 'VariableDeclaration') {
/**
* export const foo = 1, bar = 2
*/
for (const decl of node.declaration.declarations) {
exportNames.push(...extractNames(decl.id))
}
} else {
node.declaration satisfies never
}
} else {
/**
* export { foo, bar as car } from './foo'
* export { foo, bar as car }
*/
for (const spec of node.specifiers) {
tinyassert(spec.exported.type === 'Identifier')
exportNames.push(spec.exported.name)
}
}
}
/**
* export * from './foo'
*/
if (
!options.ignoreExportAllDeclaration &&
node.type === 'ExportAllDeclaration'
) {
throw new Error('unsupported ExportAllDeclaration')
}
/**
* export default function foo() {}
* export default class Foo {}
* export default () => {}
*/
if (node.type === 'ExportDefaultDeclaration') {
exportNames.push('default')
}
}
return { exportNames }
}
// Copied from periscopic `extract_names` / `extract_identifiers`
export function extractNames(param: Pattern): string[] {
return extractIdentifiers(param).map((n) => n.name)
}
// Copied from periscopic and intentionally broader than this repo's current
// declaration-oriented use cases.
//
// ESTree's `Pattern` type also covers assignment targets, where
// `MemberExpression` can appear (for example `({ x: obj.y } = value)`), so this
// helper preserves periscopic's behavior of reducing `a.b.c` to the base
// identifier `a`.
//
// In this repo, current callers use it only for declaration/binding positions
// (`VariableDeclarator.id`, function params, catch params), where
// `MemberExpression` should not appear for valid input. That branch is kept for
// compatibility with the original helper rather than because current
// declaration use cases require it.
export function extractIdentifiers(
param: Pattern,
nodes: Identifier[] = [],
): Identifier[] {
switch (param.type) {
case 'Identifier':
nodes.push(param)
break
case 'MemberExpression': {
let obj = param as any
while (obj.type === 'MemberExpression') {
obj = obj.object
}
nodes.push(obj)
break
}
case 'ObjectPattern':
for (const prop of param.properties) {
extractIdentifiers(
prop.type === 'RestElement' ? prop : prop.value,
nodes,
)
}
break
case 'ArrayPattern':
for (const el of param.elements) {
if (el) extractIdentifiers(el, nodes)
}
break
case 'RestElement':
extractIdentifiers(param.argument, nodes)
break
case 'AssignmentPattern':
extractIdentifiers(param.left, nodes)
break
}
return nodes
}
export function validateNonAsyncFunction(
opts: { rejectNonAsyncFunction?: boolean },
// export default function/class can be unnamed
node: Node | ExportDefaultDeclaration['declaration'],
): void {
if (!opts.rejectNonAsyncFunction) return
if (
node.type === 'ClassDeclaration' ||
node.type === 'ClassExpression' ||
((node.type === 'FunctionDeclaration' ||
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression') &&
!node.async)
) {
throw Object.assign(new Error(`unsupported non async function`), {
pos: node.start,
})
}
}