@sanity/codegen 3.93.1-next.6.2e3d18b278 → 3.93.1-next.60.e1dbdccb42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +7 -9
- package/src/_exports/index.ts +0 -8
- package/src/getBabelConfig.ts +0 -39
- package/src/readConfig.ts +0 -38
- package/src/readSchema.ts +0 -15
- package/src/safeParseQuery.ts +0 -39
- package/src/typescript/expressionResolvers.ts +0 -513
- package/src/typescript/findQueriesInPath.ts +0 -79
- package/src/typescript/findQueriesInSource.ts +0 -232
- package/src/typescript/moduleResolver.ts +0 -41
- package/src/typescript/parseSource.ts +0 -41
- package/src/typescript/registerBabel.ts +0 -16
- package/src/typescript/typeGenerator.ts +0 -374
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/codegen",
|
|
3
|
-
"version": "3.93.1-next.
|
|
3
|
+
"version": "3.93.1-next.60.e1dbdccb42",
|
|
4
4
|
"description": "Codegen toolkit for Sanity.io",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -37,8 +37,6 @@
|
|
|
37
37
|
"types": "./lib/index.d.ts",
|
|
38
38
|
"files": [
|
|
39
39
|
"lib",
|
|
40
|
-
"src",
|
|
41
|
-
"!**/__tests__/**",
|
|
42
40
|
"babel.config.json"
|
|
43
41
|
],
|
|
44
42
|
"scripts": {
|
|
@@ -61,16 +59,16 @@
|
|
|
61
59
|
"@babel/types": "^7.27.6",
|
|
62
60
|
"debug": "^4.3.4",
|
|
63
61
|
"globby": "^11.1.0",
|
|
64
|
-
"groq": "3.93.1-next.
|
|
65
|
-
"groq-js": "^1.17.
|
|
62
|
+
"groq": "3.93.1-next.60.e1dbdccb42",
|
|
63
|
+
"groq-js": "^1.17.1",
|
|
66
64
|
"json5": "^2.2.3",
|
|
67
65
|
"tsconfig-paths": "^4.2.0",
|
|
68
66
|
"zod": "^3.22.4"
|
|
69
67
|
},
|
|
70
68
|
"devDependencies": {
|
|
71
|
-
"@repo/eslint-config": "3.
|
|
72
|
-
"@repo/package.config": "3.
|
|
73
|
-
"@repo/test-config": "3.
|
|
69
|
+
"@repo/eslint-config": "3.94.0",
|
|
70
|
+
"@repo/package.config": "3.94.0",
|
|
71
|
+
"@repo/test-config": "3.94.0",
|
|
74
72
|
"@types/babel__core": "^7.20.5",
|
|
75
73
|
"@types/babel__generator": "^7.6.8",
|
|
76
74
|
"@types/babel__register": "^7.17.3",
|
|
@@ -85,5 +83,5 @@
|
|
|
85
83
|
"publishConfig": {
|
|
86
84
|
"access": "public"
|
|
87
85
|
},
|
|
88
|
-
"gitHead": "
|
|
86
|
+
"gitHead": "e1dbdccb42e5551644aa17861f557ea475d0cceb"
|
|
89
87
|
}
|
package/src/_exports/index.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export {type CodegenConfig, readConfig} from '../readConfig'
|
|
2
|
-
export {readSchema} from '../readSchema'
|
|
3
|
-
export {safeParseQuery} from '../safeParseQuery'
|
|
4
|
-
export {findQueriesInPath} from '../typescript/findQueriesInPath'
|
|
5
|
-
export {findQueriesInSource} from '../typescript/findQueriesInSource'
|
|
6
|
-
export {getResolver} from '../typescript/moduleResolver'
|
|
7
|
-
export {registerBabel} from '../typescript/registerBabel'
|
|
8
|
-
export {TypeGenerator} from '../typescript/typeGenerator'
|
package/src/getBabelConfig.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import {existsSync} from 'node:fs'
|
|
2
|
-
import {join, resolve} from 'node:path'
|
|
3
|
-
|
|
4
|
-
import {type TransformOptions} from '@babel/core'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Because of bundlers and compilers, knowing the exact path the babel configuration will be
|
|
8
|
-
* located at post - build is not always trivial. We traverse from the current directory upwards
|
|
9
|
-
* until we find the first `babel.config.json` and use that path.
|
|
10
|
-
*
|
|
11
|
-
* @param path - The path to start looking for the babel configuration
|
|
12
|
-
* @returns The path to the `babel.config.json` file
|
|
13
|
-
* @internal
|
|
14
|
-
*/
|
|
15
|
-
export function findBabelConfig(path: string): string {
|
|
16
|
-
const configPath = join(path, 'babel.config.json')
|
|
17
|
-
if (existsSync(configPath)) {
|
|
18
|
-
return configPath
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const parent = resolve(join(path, '..'))
|
|
22
|
-
if (parent && parent !== path) {
|
|
23
|
-
return findBabelConfig(parent)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
throw new Error('Could not find `babel.config.json` in @sanity/codegen')
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Get the default babel configuration for `@sanity/codegen`
|
|
31
|
-
*
|
|
32
|
-
* @param path - The path to start looking for the babel configuration. Defaults to `__dirname`
|
|
33
|
-
* @returns A babel configuration object
|
|
34
|
-
* @internal
|
|
35
|
-
*/
|
|
36
|
-
export function getBabelConfig(path?: string): TransformOptions {
|
|
37
|
-
const configPath = findBabelConfig(path || __dirname)
|
|
38
|
-
return {extends: configPath}
|
|
39
|
-
}
|
package/src/readConfig.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import {readFile} from 'node:fs/promises'
|
|
2
|
-
|
|
3
|
-
import * as json5 from 'json5'
|
|
4
|
-
import * as z from 'zod'
|
|
5
|
-
|
|
6
|
-
export const configDefintion = z.object({
|
|
7
|
-
path: z
|
|
8
|
-
.string()
|
|
9
|
-
.or(z.array(z.string()))
|
|
10
|
-
.default([
|
|
11
|
-
'./src/**/*.{ts,tsx,js,jsx,mjs,cjs,astro}',
|
|
12
|
-
'./app/**/*.{ts,tsx,js,jsx,mjs,cjs}',
|
|
13
|
-
'./sanity/**/*.{ts,tsx,js,jsx,mjs,cjs}',
|
|
14
|
-
]),
|
|
15
|
-
schema: z.string().default('./schema.json'),
|
|
16
|
-
generates: z.string().default('./sanity.types.ts'),
|
|
17
|
-
formatGeneratedCode: z.boolean().default(true),
|
|
18
|
-
overloadClientMethods: z.boolean().default(true),
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
export type CodegenConfig = z.infer<typeof configDefintion>
|
|
22
|
-
|
|
23
|
-
export async function readConfig(path: string): Promise<CodegenConfig> {
|
|
24
|
-
try {
|
|
25
|
-
const content = await readFile(path, 'utf-8')
|
|
26
|
-
const json = json5.parse(content)
|
|
27
|
-
return configDefintion.parseAsync(json)
|
|
28
|
-
} catch (error) {
|
|
29
|
-
if (error instanceof z.ZodError) {
|
|
30
|
-
throw new Error(`Error in config file\n ${error.errors.map((err) => err.message).join('\n')}`)
|
|
31
|
-
}
|
|
32
|
-
if (typeof error === 'object' && error !== null && 'code' in error && error.code === 'ENOENT') {
|
|
33
|
-
return configDefintion.parse({})
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
throw error
|
|
37
|
-
}
|
|
38
|
-
}
|
package/src/readSchema.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import {readFile} from 'node:fs/promises'
|
|
2
|
-
|
|
3
|
-
import {type SchemaType} from 'groq-js'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Read a schema from a given path
|
|
7
|
-
* @param path - The path to the schema
|
|
8
|
-
* @returns The schema
|
|
9
|
-
* @internal
|
|
10
|
-
* @beta
|
|
11
|
-
**/
|
|
12
|
-
export async function readSchema(path: string): Promise<SchemaType> {
|
|
13
|
-
const content = await readFile(path, 'utf-8')
|
|
14
|
-
return JSON.parse(content) // todo: ZOD validation?
|
|
15
|
-
}
|
package/src/safeParseQuery.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import {parse} from 'groq-js'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* safeParseQuery parses a GROQ query string, but first attempts to extract any parameters used in slices. This method is _only_
|
|
5
|
-
* intended for use in type generation where we don't actually execute the parsed AST on a dataset, and should not be used elsewhere.
|
|
6
|
-
* @internal
|
|
7
|
-
*/
|
|
8
|
-
export function safeParseQuery(query: string) {
|
|
9
|
-
const params: Record<string, unknown> = {}
|
|
10
|
-
|
|
11
|
-
for (const param of extractSliceParams(query)) {
|
|
12
|
-
params[param] = 0 // we don't care about the value, just the type
|
|
13
|
-
}
|
|
14
|
-
return parse(query, {params})
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Finds occurences of `[($start|{number})..($end|{number})]` in a query string and returns the start and end values, and return
|
|
19
|
-
* the names of the start and end variables.
|
|
20
|
-
* @internal
|
|
21
|
-
*/
|
|
22
|
-
export function* extractSliceParams(query: string): Generator<string> {
|
|
23
|
-
const sliceRegex = /\[(\$(\w+)|\d)\.\.\.?(\$(\w+)|\d)\]/g
|
|
24
|
-
const matches = query.matchAll(sliceRegex)
|
|
25
|
-
if (!matches) {
|
|
26
|
-
return
|
|
27
|
-
}
|
|
28
|
-
const params = new Set<string>()
|
|
29
|
-
for (const match of matches) {
|
|
30
|
-
const start = match[1] === `$${match[2]}` ? match[2] : null
|
|
31
|
-
if (start !== null) {
|
|
32
|
-
yield start
|
|
33
|
-
}
|
|
34
|
-
const end = match[3] === `$${match[4]}` ? match[4] : null
|
|
35
|
-
if (end !== null) {
|
|
36
|
-
yield end
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
@@ -1,513 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs'
|
|
2
|
-
import path from 'node:path'
|
|
3
|
-
|
|
4
|
-
import {type TransformOptions} from '@babel/core'
|
|
5
|
-
import traverse, {Scope} from '@babel/traverse'
|
|
6
|
-
import * as babelTypes from '@babel/types'
|
|
7
|
-
import createDebug from 'debug'
|
|
8
|
-
|
|
9
|
-
import {parseSourceFile} from './parseSource'
|
|
10
|
-
|
|
11
|
-
const debug = createDebug('sanity:codegen:findQueries:debug')
|
|
12
|
-
|
|
13
|
-
type resolveExpressionReturnType = string
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* NamedQueryResult is a result of a named query
|
|
17
|
-
*/
|
|
18
|
-
export interface NamedQueryResult {
|
|
19
|
-
/** name is the name of the query */
|
|
20
|
-
name: string
|
|
21
|
-
/** result is a groq query */
|
|
22
|
-
result: resolveExpressionReturnType
|
|
23
|
-
|
|
24
|
-
/** location is the location of the query in the source */
|
|
25
|
-
location: {
|
|
26
|
-
start?: {
|
|
27
|
-
line: number
|
|
28
|
-
column: number
|
|
29
|
-
index: number
|
|
30
|
-
}
|
|
31
|
-
end?: {
|
|
32
|
-
line: number
|
|
33
|
-
column: number
|
|
34
|
-
index: number
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const TAGGED_TEMPLATE_ALLOW_LIST = ['groq']
|
|
40
|
-
const FUNCTION_WRAPPER_ALLOW_LIST = ['defineQuery']
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* resolveExpression takes a node and returns the resolved value of the expression.
|
|
44
|
-
* @beta
|
|
45
|
-
* @internal
|
|
46
|
-
*/
|
|
47
|
-
export function resolveExpression({
|
|
48
|
-
node,
|
|
49
|
-
file,
|
|
50
|
-
scope,
|
|
51
|
-
filename,
|
|
52
|
-
resolver,
|
|
53
|
-
babelConfig,
|
|
54
|
-
params = [],
|
|
55
|
-
fnArguments = [],
|
|
56
|
-
}: {
|
|
57
|
-
node: babelTypes.Node
|
|
58
|
-
file: babelTypes.File
|
|
59
|
-
scope: Scope
|
|
60
|
-
filename: string
|
|
61
|
-
resolver: NodeJS.RequireResolve
|
|
62
|
-
babelConfig: TransformOptions
|
|
63
|
-
params?: babelTypes.Node[]
|
|
64
|
-
fnArguments?: babelTypes.Node[]
|
|
65
|
-
}): resolveExpressionReturnType {
|
|
66
|
-
debug(
|
|
67
|
-
`Resolving node ${node.type} in ${filename}:${node.loc?.start.line}:${node.loc?.start.column}`,
|
|
68
|
-
)
|
|
69
|
-
if (
|
|
70
|
-
babelTypes.isTaggedTemplateExpression(node) &&
|
|
71
|
-
babelTypes.isIdentifier(node.tag) &&
|
|
72
|
-
TAGGED_TEMPLATE_ALLOW_LIST.includes(node.tag.name)
|
|
73
|
-
) {
|
|
74
|
-
return resolveExpression({
|
|
75
|
-
node: node.quasi,
|
|
76
|
-
scope,
|
|
77
|
-
filename,
|
|
78
|
-
file,
|
|
79
|
-
resolver,
|
|
80
|
-
params,
|
|
81
|
-
babelConfig,
|
|
82
|
-
fnArguments,
|
|
83
|
-
})
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (babelTypes.isTemplateLiteral(node)) {
|
|
87
|
-
const resolvedExpressions = node.expressions.map((expression) =>
|
|
88
|
-
resolveExpression({
|
|
89
|
-
node: expression,
|
|
90
|
-
scope,
|
|
91
|
-
filename,
|
|
92
|
-
file,
|
|
93
|
-
resolver,
|
|
94
|
-
params,
|
|
95
|
-
babelConfig,
|
|
96
|
-
fnArguments,
|
|
97
|
-
}),
|
|
98
|
-
)
|
|
99
|
-
return node.quasis
|
|
100
|
-
.map((quasi, idx) => {
|
|
101
|
-
return (quasi.value.cooked || '') + (resolvedExpressions[idx] || '')
|
|
102
|
-
})
|
|
103
|
-
.join('')
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (babelTypes.isLiteral(node)) {
|
|
107
|
-
if (node.type === 'NullLiteral' || node.type === 'RegExpLiteral') {
|
|
108
|
-
throw new Error(`Unsupported literal type: ${node.type}`)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return node.value.toString()
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (babelTypes.isIdentifier(node)) {
|
|
115
|
-
return resolveIdentifier({
|
|
116
|
-
node,
|
|
117
|
-
scope,
|
|
118
|
-
filename,
|
|
119
|
-
file,
|
|
120
|
-
resolver,
|
|
121
|
-
fnArguments,
|
|
122
|
-
babelConfig,
|
|
123
|
-
params,
|
|
124
|
-
})
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (babelTypes.isVariableDeclarator(node)) {
|
|
128
|
-
const init = node.init ?? (babelTypes.isAssignmentPattern(node.id) && node.id.right)
|
|
129
|
-
if (!init) {
|
|
130
|
-
throw new Error(`Unsupported variable declarator`)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return resolveExpression({
|
|
134
|
-
node: init,
|
|
135
|
-
fnArguments,
|
|
136
|
-
scope,
|
|
137
|
-
filename,
|
|
138
|
-
file,
|
|
139
|
-
babelConfig,
|
|
140
|
-
resolver,
|
|
141
|
-
})
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (
|
|
145
|
-
babelTypes.isCallExpression(node) &&
|
|
146
|
-
babelTypes.isIdentifier(node.callee) &&
|
|
147
|
-
FUNCTION_WRAPPER_ALLOW_LIST.includes(node.callee.name)
|
|
148
|
-
) {
|
|
149
|
-
return resolveExpression({
|
|
150
|
-
node: node.arguments[0],
|
|
151
|
-
scope,
|
|
152
|
-
filename,
|
|
153
|
-
file,
|
|
154
|
-
resolver,
|
|
155
|
-
babelConfig,
|
|
156
|
-
params,
|
|
157
|
-
})
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (babelTypes.isCallExpression(node)) {
|
|
161
|
-
return resolveCallExpression({
|
|
162
|
-
node,
|
|
163
|
-
scope,
|
|
164
|
-
filename,
|
|
165
|
-
file,
|
|
166
|
-
resolver,
|
|
167
|
-
babelConfig,
|
|
168
|
-
params,
|
|
169
|
-
fnArguments,
|
|
170
|
-
})
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (
|
|
174
|
-
babelTypes.isArrowFunctionExpression(node) ||
|
|
175
|
-
babelTypes.isFunctionDeclaration(node) ||
|
|
176
|
-
babelTypes.isFunctionExpression(node)
|
|
177
|
-
) {
|
|
178
|
-
const newScope = new Scope(scope.path, scope)
|
|
179
|
-
|
|
180
|
-
params.forEach((param, i) => {
|
|
181
|
-
newScope.push({
|
|
182
|
-
id: param as babelTypes.LVal,
|
|
183
|
-
init: fnArguments[i] as babelTypes.Expression | undefined,
|
|
184
|
-
})
|
|
185
|
-
})
|
|
186
|
-
|
|
187
|
-
return resolveExpression({
|
|
188
|
-
node: node.body,
|
|
189
|
-
params: node.params,
|
|
190
|
-
fnArguments,
|
|
191
|
-
scope: newScope,
|
|
192
|
-
filename,
|
|
193
|
-
file,
|
|
194
|
-
babelConfig,
|
|
195
|
-
resolver,
|
|
196
|
-
})
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (babelTypes.isNewExpression(node)) {
|
|
200
|
-
return resolveExpression({
|
|
201
|
-
node: node.callee,
|
|
202
|
-
scope,
|
|
203
|
-
filename,
|
|
204
|
-
file,
|
|
205
|
-
babelConfig,
|
|
206
|
-
resolver,
|
|
207
|
-
})
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
if (babelTypes.isImportDefaultSpecifier(node) || babelTypes.isImportSpecifier(node)) {
|
|
211
|
-
return resolveImportSpecifier({node, file, scope, filename, fnArguments, resolver, babelConfig})
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
if (babelTypes.isAssignmentPattern(node)) {
|
|
215
|
-
return resolveExpression({
|
|
216
|
-
node: node.right,
|
|
217
|
-
scope,
|
|
218
|
-
filename,
|
|
219
|
-
file,
|
|
220
|
-
resolver,
|
|
221
|
-
params,
|
|
222
|
-
babelConfig,
|
|
223
|
-
fnArguments,
|
|
224
|
-
})
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
throw new Error(
|
|
228
|
-
`Unsupported expression type: ${node.type} in ${filename}:${node.loc?.start.line}:${node.loc?.start.column}`,
|
|
229
|
-
)
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
function resolveIdentifier({
|
|
233
|
-
node,
|
|
234
|
-
scope,
|
|
235
|
-
filename,
|
|
236
|
-
file,
|
|
237
|
-
resolver,
|
|
238
|
-
babelConfig,
|
|
239
|
-
fnArguments,
|
|
240
|
-
params,
|
|
241
|
-
}: {
|
|
242
|
-
node: babelTypes.Identifier
|
|
243
|
-
file: babelTypes.File
|
|
244
|
-
scope: Scope
|
|
245
|
-
filename: string
|
|
246
|
-
resolver: NodeJS.RequireResolve
|
|
247
|
-
babelConfig: TransformOptions
|
|
248
|
-
fnArguments: babelTypes.Node[]
|
|
249
|
-
params: babelTypes.Node[]
|
|
250
|
-
}): resolveExpressionReturnType {
|
|
251
|
-
const paramIndex = params.findIndex(
|
|
252
|
-
(param) =>
|
|
253
|
-
(babelTypes.isIdentifier(param) && node.name === param.name) ||
|
|
254
|
-
(babelTypes.isAssignmentPattern(param) &&
|
|
255
|
-
babelTypes.isIdentifier(param.left) &&
|
|
256
|
-
node.name === param.left.name),
|
|
257
|
-
)
|
|
258
|
-
let argument = fnArguments[paramIndex]
|
|
259
|
-
if (!argument && paramIndex >= 0 && babelTypes.isAssignmentPattern(params[paramIndex])) {
|
|
260
|
-
argument = params[paramIndex].right
|
|
261
|
-
}
|
|
262
|
-
if (argument && babelTypes.isLiteral(argument)) {
|
|
263
|
-
return resolveExpression({
|
|
264
|
-
node: argument,
|
|
265
|
-
scope,
|
|
266
|
-
filename,
|
|
267
|
-
file,
|
|
268
|
-
resolver,
|
|
269
|
-
params,
|
|
270
|
-
babelConfig,
|
|
271
|
-
fnArguments,
|
|
272
|
-
})
|
|
273
|
-
}
|
|
274
|
-
const binding = scope.getBinding(node.name)
|
|
275
|
-
if (binding) {
|
|
276
|
-
if (babelTypes.isIdentifier(binding.path.node)) {
|
|
277
|
-
const isSame = binding.path.node.name === node.name
|
|
278
|
-
if (isSame) {
|
|
279
|
-
throw new Error(
|
|
280
|
-
`Could not resolve same identifier "${node.name}" in "${filename}:${node.loc?.start.line}:${node.loc?.start.column}"`,
|
|
281
|
-
)
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
return resolveExpression({
|
|
285
|
-
node: binding.path.node,
|
|
286
|
-
params,
|
|
287
|
-
fnArguments,
|
|
288
|
-
scope,
|
|
289
|
-
filename,
|
|
290
|
-
babelConfig,
|
|
291
|
-
file,
|
|
292
|
-
resolver,
|
|
293
|
-
})
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
throw new Error(
|
|
297
|
-
`Could not find binding for node "${node.name}" in ${filename}:${node.loc?.start.line}:${node.loc?.start.column}`,
|
|
298
|
-
)
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
function resolveCallExpression({
|
|
302
|
-
node,
|
|
303
|
-
scope,
|
|
304
|
-
filename,
|
|
305
|
-
file,
|
|
306
|
-
resolver,
|
|
307
|
-
babelConfig,
|
|
308
|
-
params,
|
|
309
|
-
}: {
|
|
310
|
-
node: babelTypes.CallExpression
|
|
311
|
-
file: babelTypes.File
|
|
312
|
-
scope: Scope
|
|
313
|
-
filename: string
|
|
314
|
-
resolver: NodeJS.RequireResolve
|
|
315
|
-
babelConfig: TransformOptions
|
|
316
|
-
fnArguments: babelTypes.Node[]
|
|
317
|
-
params: babelTypes.Node[]
|
|
318
|
-
}): resolveExpressionReturnType {
|
|
319
|
-
const {callee} = node
|
|
320
|
-
return resolveExpression({
|
|
321
|
-
node: callee,
|
|
322
|
-
scope,
|
|
323
|
-
filename,
|
|
324
|
-
file,
|
|
325
|
-
resolver,
|
|
326
|
-
babelConfig,
|
|
327
|
-
params,
|
|
328
|
-
fnArguments: node.arguments,
|
|
329
|
-
})
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
function resolveImportSpecifier({
|
|
333
|
-
node,
|
|
334
|
-
file,
|
|
335
|
-
filename,
|
|
336
|
-
fnArguments,
|
|
337
|
-
resolver,
|
|
338
|
-
babelConfig,
|
|
339
|
-
}: {
|
|
340
|
-
node: babelTypes.ImportDefaultSpecifier | babelTypes.ImportSpecifier | babelTypes.ExportSpecifier
|
|
341
|
-
file: babelTypes.File
|
|
342
|
-
scope: Scope
|
|
343
|
-
filename: string
|
|
344
|
-
fnArguments: babelTypes.Node[]
|
|
345
|
-
resolver: NodeJS.RequireResolve
|
|
346
|
-
babelConfig: TransformOptions
|
|
347
|
-
}): resolveExpressionReturnType {
|
|
348
|
-
let importDeclaration: babelTypes.ImportDeclaration | undefined
|
|
349
|
-
traverse(file, {
|
|
350
|
-
ImportDeclaration(n) {
|
|
351
|
-
if (!babelTypes.isImportDeclaration(n.node)) {
|
|
352
|
-
return
|
|
353
|
-
}
|
|
354
|
-
for (const specifier of n.node.specifiers) {
|
|
355
|
-
if (babelTypes.isImportDefaultSpecifier(specifier)) {
|
|
356
|
-
if (specifier.local.loc?.identifierName === node.local.name) {
|
|
357
|
-
importDeclaration = n.node
|
|
358
|
-
break
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
if (specifier.local.name === node.local.name) {
|
|
362
|
-
importDeclaration = n.node
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
},
|
|
366
|
-
})
|
|
367
|
-
|
|
368
|
-
if (!importDeclaration) {
|
|
369
|
-
throw new Error(`Could not find import declaration for ${node.local.name}`)
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
const importName = node.local.name // the name of the variable to import
|
|
373
|
-
const importFileName = importDeclaration.source.value // the file to import from
|
|
374
|
-
|
|
375
|
-
const importPath =
|
|
376
|
-
importFileName.startsWith('./') || importFileName.startsWith('../')
|
|
377
|
-
? path.resolve(path.dirname(filename), importFileName)
|
|
378
|
-
: importFileName
|
|
379
|
-
|
|
380
|
-
const resolvedFile = resolver(importPath)
|
|
381
|
-
const source = fs.readFileSync(resolvedFile)
|
|
382
|
-
const tree = parseSourceFile(source.toString(), resolvedFile, babelConfig)
|
|
383
|
-
|
|
384
|
-
let newScope: Scope | undefined
|
|
385
|
-
traverse(tree, {
|
|
386
|
-
Program(p) {
|
|
387
|
-
newScope = p.scope
|
|
388
|
-
},
|
|
389
|
-
})
|
|
390
|
-
if (!newScope) {
|
|
391
|
-
throw new Error(`Could not find scope for ${filename}`)
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
const binding = newScope.getBinding(importName)
|
|
395
|
-
if (binding) {
|
|
396
|
-
return resolveExpression({
|
|
397
|
-
node: binding.path.node,
|
|
398
|
-
file: tree,
|
|
399
|
-
scope: newScope,
|
|
400
|
-
fnArguments,
|
|
401
|
-
babelConfig,
|
|
402
|
-
filename: resolvedFile,
|
|
403
|
-
resolver,
|
|
404
|
-
})
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
// It's not a global binding, but it might be a named export
|
|
408
|
-
let namedExport: babelTypes.ExportNamedDeclaration | undefined
|
|
409
|
-
let newImportName: string | undefined
|
|
410
|
-
traverse(tree, {
|
|
411
|
-
ExportDeclaration(p) {
|
|
412
|
-
if (p.node.type === 'ExportNamedDeclaration') {
|
|
413
|
-
for (const specifier of p.node.specifiers) {
|
|
414
|
-
if (
|
|
415
|
-
specifier.type === 'ExportSpecifier' &&
|
|
416
|
-
specifier.exported.type === 'Identifier' &&
|
|
417
|
-
specifier.exported.name === importName
|
|
418
|
-
) {
|
|
419
|
-
namedExport = p.node
|
|
420
|
-
newImportName = specifier.exported.name
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
},
|
|
425
|
-
})
|
|
426
|
-
|
|
427
|
-
if (namedExport && newImportName) {
|
|
428
|
-
return resolveExportSpecifier({
|
|
429
|
-
node: namedExport,
|
|
430
|
-
importName: newImportName,
|
|
431
|
-
filename: resolvedFile,
|
|
432
|
-
fnArguments,
|
|
433
|
-
resolver,
|
|
434
|
-
babelConfig,
|
|
435
|
-
})
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
let result: resolveExpressionReturnType | undefined
|
|
439
|
-
traverse(tree, {
|
|
440
|
-
ExportDeclaration(p) {
|
|
441
|
-
if (p.node.type === 'ExportAllDeclaration') {
|
|
442
|
-
try {
|
|
443
|
-
result = resolveExportSpecifier({
|
|
444
|
-
node: p.node,
|
|
445
|
-
importName,
|
|
446
|
-
filename: resolvedFile,
|
|
447
|
-
fnArguments,
|
|
448
|
-
resolver,
|
|
449
|
-
babelConfig,
|
|
450
|
-
})
|
|
451
|
-
} catch (e) {
|
|
452
|
-
if (e.cause !== `noBinding:${importName}`) throw e
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
},
|
|
456
|
-
})
|
|
457
|
-
if (result) return result
|
|
458
|
-
|
|
459
|
-
throw new Error(`Could not find binding for import "${importName}" in ${importFileName}`)
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
function resolveExportSpecifier({
|
|
463
|
-
node,
|
|
464
|
-
importName,
|
|
465
|
-
filename,
|
|
466
|
-
fnArguments,
|
|
467
|
-
babelConfig,
|
|
468
|
-
resolver,
|
|
469
|
-
}: {
|
|
470
|
-
node: babelTypes.ExportNamedDeclaration | babelTypes.ExportAllDeclaration
|
|
471
|
-
importName: string
|
|
472
|
-
filename: string
|
|
473
|
-
fnArguments: babelTypes.Node[]
|
|
474
|
-
babelConfig: TransformOptions
|
|
475
|
-
resolver: NodeJS.RequireResolve
|
|
476
|
-
}): resolveExpressionReturnType {
|
|
477
|
-
if (!node.source) {
|
|
478
|
-
throw new Error(`Could not find source for export "${importName}" in ${filename}`)
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
const importFileName = node.source.value
|
|
482
|
-
const importPath = path.resolve(path.dirname(filename), importFileName)
|
|
483
|
-
const resolvedFile = resolver(importPath)
|
|
484
|
-
const source = fs.readFileSync(resolvedFile)
|
|
485
|
-
const tree = parseSourceFile(source.toString(), resolvedFile, babelConfig)
|
|
486
|
-
|
|
487
|
-
let newScope: Scope | undefined
|
|
488
|
-
traverse(tree, {
|
|
489
|
-
Program(p) {
|
|
490
|
-
newScope = p.scope
|
|
491
|
-
},
|
|
492
|
-
})
|
|
493
|
-
if (!newScope) {
|
|
494
|
-
throw new Error(`Could not find scope for ${filename}`)
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
const binding = newScope.getBinding(importName)
|
|
498
|
-
if (binding) {
|
|
499
|
-
return resolveExpression({
|
|
500
|
-
node: binding.path.node,
|
|
501
|
-
file: tree,
|
|
502
|
-
scope: newScope,
|
|
503
|
-
filename: importFileName,
|
|
504
|
-
babelConfig,
|
|
505
|
-
resolver,
|
|
506
|
-
fnArguments,
|
|
507
|
-
})
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
throw new Error(`Could not find binding for export "${importName}" in ${importFileName}`, {
|
|
511
|
-
cause: `noBinding:${importName}`,
|
|
512
|
-
})
|
|
513
|
-
}
|