@fabrika/check 0.0.0-stage → 0.0.1
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 +48 -5
- package/src/ast.ts +268 -0
- package/src/bun-plugin.ts +63 -0
- package/src/check.ts +183 -0
- package/src/cli.ts +88 -0
- package/src/contract.ts +254 -0
- package/src/endpoint-fixtures.ts +221 -0
- package/src/finding.ts +88 -0
- package/src/graph.ts +206 -0
- package/src/index.ts +15 -0
- package/src/inputs.ts +88 -0
- package/src/jsonc.ts +63 -0
- package/src/layout.ts +115 -0
- package/src/limits.ts +15 -0
- package/src/packed.ts +66 -0
- package/src/reader/problems.ts +11 -0
- package/src/reader/schema.ts +551 -0
- package/src/reader/surface.ts +1087 -0
- package/src/reader/tables.ts +116 -0
- package/src/reader/value.ts +644 -0
- package/src/references.ts +95 -0
- package/src/resolution.ts +178 -0
- package/src/rules/contract.ts +78 -0
- package/src/rules/crontab.ts +147 -0
- package/src/rules/framework.ts +177 -0
- package/src/rules/identity.ts +80 -0
- package/src/rules/isolation.ts +79 -0
- package/src/rules/json.ts +28 -0
- package/src/rules/literal.ts +736 -0
- package/src/rules/migrations.ts +87 -0
- package/src/rules/modules.ts +226 -0
- package/src/rules/packages.ts +168 -0
- package/src/rules/runtime.ts +507 -0
- package/src/rules/structure.ts +299 -0
- package/src/rules/tests.ts +208 -0
- package/src/rules/types.ts +62 -0
- package/src/scaffold-sample.ts +108 -0
- package/src/scaffold.ts +605 -0
- package/src/scope.ts +108 -0
- package/src/slice.ts +339 -0
- package/src/toml.ts +159 -0
- package/src/trust.ts +189 -0
- package/src/version.ts +4 -0
- package/README.md +0 -3
|
@@ -0,0 +1,736 @@
|
|
|
1
|
+
import { type AstNode, child, children, identifierName, isConstAssertion, type ParsedModule, staticKey, staticString, stringField } from '../ast'
|
|
2
|
+
import { type Finding, type Report, reporter } from '../finding'
|
|
3
|
+
import type { Binding, ModuleGraph } from '../graph'
|
|
4
|
+
import { isRelativeSpecifier } from '../layout'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* What a call root builds. `surface` is the parameter of a `defineContract` callback (`c`, `l`); `app` is
|
|
8
|
+
* `defineApp`, `kit` a const it returned, `shell` the parameter of `app.contract`'s callback and `endpoints` that of
|
|
9
|
+
* a group's, a link's or an export's; `attachment` is the output `attachment({ … })`.
|
|
10
|
+
*/
|
|
11
|
+
export type BuilderKind = 'zod' | 'contract' | 'vocabulary' | 'tables' | 'surface' | 'app' | 'kit' | 'shell' | 'endpoints' | 'attachment'
|
|
12
|
+
|
|
13
|
+
/** The only imported names a literal module may use: the builders the platform reads without executing them. */
|
|
14
|
+
export const BUILDER_IMPORTS: Readonly<Record<string, Readonly<Record<string, BuilderKind>>>> = {
|
|
15
|
+
'zod/v4': { z: 'zod', '*': 'zod' },
|
|
16
|
+
'@fabrika/app/contract': { defineContract: 'contract', defineVocabulary: 'vocabulary', defineApp: 'app', attachment: 'attachment' },
|
|
17
|
+
'@fabrika/db/tables': { defineTables: 'tables' },
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The shell graph is what the SPA bundles: `@fabrika/db`'s root also exports the Bun-only data layer, so the tables
|
|
22
|
+
* come from its browser-safe entry.
|
|
23
|
+
*/
|
|
24
|
+
const SERVER_ENTRY_OF: Readonly<Record<string, string>> = { '@fabrika/db': '@fabrika/db/tables' }
|
|
25
|
+
|
|
26
|
+
const SURFACE_BUILDERS = new Set(['rpc', 'query', 'mutation', 'link', 'spa', 'health', 'form', 'webhook', 'files', 'cron', 'export'])
|
|
27
|
+
/** The methods of the kit `defineApp` returns. */
|
|
28
|
+
export const KIT_BUILDERS: ReadonlySet<string> = new Set(['group', 'contract', 'cron', 'webhook', 'link', 'export'])
|
|
29
|
+
/** The builders of `app.contract`'s callback: the mounts that take no handler, and `rpc`, which mounts groups. */
|
|
30
|
+
export const SHELL_BUILDERS: ReadonlySet<string> = new Set(['rpc', 'spa', 'health', 'form', 'files'])
|
|
31
|
+
/** The builders of a group's, a link's and an export's callback. */
|
|
32
|
+
export const ENDPOINT_BUILDERS: ReadonlySet<string> = new Set(['query', 'mutation'])
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Where a kit method takes a builder callback, and the kit its parameter denotes: a group's endpoints, the contract's
|
|
36
|
+
* mounts, a link's and an export's `procedures` (`options: true`).
|
|
37
|
+
*/
|
|
38
|
+
export const KIT_CALLBACKS: Readonly<Record<string, { readonly position: number; readonly kind: BuilderKind; readonly options: boolean }>> = {
|
|
39
|
+
group: { position: 1, kind: 'endpoints', options: false },
|
|
40
|
+
contract: { position: 0, kind: 'shell', options: false },
|
|
41
|
+
link: { position: 1, kind: 'endpoints', options: true },
|
|
42
|
+
export: { position: 0, kind: 'endpoints', options: true },
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Whether `init` is `defineApp(…)`, `defineApp` imported from `@fabrika/app/contract` into `module`. */
|
|
46
|
+
export const isAppCall = (graph: ModuleGraph, module: ParsedModule, init: AstNode): boolean => {
|
|
47
|
+
const { root, links } = decompose(init)
|
|
48
|
+
const name = identifierName(root)
|
|
49
|
+
const imported = name === undefined ? undefined : graph.index(module).imports.get(name)
|
|
50
|
+
return links.length === 1 && links[0]?.kind === 'call' && imported !== undefined
|
|
51
|
+
&& BUILDER_IMPORTS[imported.specifier]?.[imported.imported] === 'app'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Whether `name` in `module` denotes a const `defineApp(…)` returned: the application's kit. */
|
|
55
|
+
export const isKitName = (graph: ModuleGraph, module: ParsedModule, name: string): boolean => {
|
|
56
|
+
const binding = graph.binding(module, name)
|
|
57
|
+
return binding.kind === 'const' && binding.init !== undefined && isAppCall(graph, binding.module, binding.init)
|
|
58
|
+
}
|
|
59
|
+
/** The schema subset the static contract reader (B1) understands; `packages/check/CLAUDE.md` lists it. */
|
|
60
|
+
const ZOD_FACTORIES = new Set([
|
|
61
|
+
'strictObject',
|
|
62
|
+
'string',
|
|
63
|
+
'number',
|
|
64
|
+
'int',
|
|
65
|
+
'boolean',
|
|
66
|
+
'null',
|
|
67
|
+
'literal',
|
|
68
|
+
'enum',
|
|
69
|
+
'array',
|
|
70
|
+
'tuple',
|
|
71
|
+
'union',
|
|
72
|
+
'discriminatedUnion',
|
|
73
|
+
'record',
|
|
74
|
+
'optional',
|
|
75
|
+
'nullable',
|
|
76
|
+
'uuid',
|
|
77
|
+
'email',
|
|
78
|
+
'url',
|
|
79
|
+
])
|
|
80
|
+
const ZOD_ISO_FORMATS = new Set(['date', 'datetime', 'time', 'duration'])
|
|
81
|
+
const ZOD_METHODS = new Set([
|
|
82
|
+
'min',
|
|
83
|
+
'max',
|
|
84
|
+
'length',
|
|
85
|
+
'nonempty',
|
|
86
|
+
'email',
|
|
87
|
+
'uuid',
|
|
88
|
+
'url',
|
|
89
|
+
'startsWith',
|
|
90
|
+
'endsWith',
|
|
91
|
+
'includes',
|
|
92
|
+
'int',
|
|
93
|
+
'positive',
|
|
94
|
+
'negative',
|
|
95
|
+
'nonnegative',
|
|
96
|
+
'nonpositive',
|
|
97
|
+
'gt',
|
|
98
|
+
'gte',
|
|
99
|
+
'lt',
|
|
100
|
+
'lte',
|
|
101
|
+
'multipleOf',
|
|
102
|
+
'regex',
|
|
103
|
+
'optional',
|
|
104
|
+
'nullable',
|
|
105
|
+
'nullish',
|
|
106
|
+
'default',
|
|
107
|
+
'describe',
|
|
108
|
+
'array',
|
|
109
|
+
'or',
|
|
110
|
+
'extend',
|
|
111
|
+
'pick',
|
|
112
|
+
'omit',
|
|
113
|
+
'partial',
|
|
114
|
+
])
|
|
115
|
+
/** Named apart because an agent reaches for them first: each runs application code or admits undeclared keys. */
|
|
116
|
+
const ZOD_UNSERIALIZABLE = new Set([
|
|
117
|
+
'transform',
|
|
118
|
+
'refine',
|
|
119
|
+
'superRefine',
|
|
120
|
+
'preprocess',
|
|
121
|
+
'overwrite',
|
|
122
|
+
'check',
|
|
123
|
+
'pipe',
|
|
124
|
+
'catch',
|
|
125
|
+
'custom',
|
|
126
|
+
'lazy',
|
|
127
|
+
'function',
|
|
128
|
+
'instanceof',
|
|
129
|
+
'codec',
|
|
130
|
+
'passthrough',
|
|
131
|
+
'loose',
|
|
132
|
+
'looseObject',
|
|
133
|
+
'catchall',
|
|
134
|
+
])
|
|
135
|
+
|
|
136
|
+
const ALLOWED_VALUE = 'object and array literals, strings, numbers, booleans, null, the c.* and z.* builders, and const imports of such values'
|
|
137
|
+
const ALLOWED_SCHEMA = 'a schema of the supported z subset (packages/check/CLAUDE.md): strict objects, no transforms, refinements or pipes'
|
|
138
|
+
const ALLOWED_CALLBACK =
|
|
139
|
+
'a function only as a builder callback: defineContract(…, (c) => ({ … })), app.contract((c) => ({ … })), app.group(…, (r) => ({ … })), procedures: (l) => ({ … }) of a link or an export, and the handler of .handle(…)'
|
|
140
|
+
const ALLOWED_IMPORT =
|
|
141
|
+
'z from zod/v4, defineContract, defineVocabulary or defineApp from @fabrika/app/contract, defineTables from @fabrika/db/tables, or a literal const of the application'
|
|
142
|
+
|
|
143
|
+
export type Link = { readonly kind: 'member'; readonly name: string | undefined; readonly node: AstNode } | {
|
|
144
|
+
readonly kind: 'call'
|
|
145
|
+
readonly node: AstNode
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** `z['string']()` names the same builder, but a key read through a const is no longer a name the reader sees. */
|
|
149
|
+
export const isComputedMember = (link: Link): boolean => link.kind === 'member' && link.node['computed'] === true
|
|
150
|
+
|
|
151
|
+
/** `z.string().min(1)` → root `z`, links `.string` `()` `.min` `(1)`. */
|
|
152
|
+
export const decompose = (node: AstNode): { readonly root: AstNode; readonly links: readonly Link[] } => {
|
|
153
|
+
const links: Link[] = []
|
|
154
|
+
let current = node
|
|
155
|
+
for (;;) {
|
|
156
|
+
const inner = child(current, current.type === 'CallExpression' ? 'callee' : 'object')
|
|
157
|
+
if (inner === undefined || (current.type !== 'CallExpression' && current.type !== 'MemberExpression')) break
|
|
158
|
+
links.unshift(
|
|
159
|
+
current.type === 'CallExpression' ? { kind: 'call', node: current } : { kind: 'member', name: staticKey(current, 'property'), node: current },
|
|
160
|
+
)
|
|
161
|
+
current = inner
|
|
162
|
+
}
|
|
163
|
+
return { root: current, links }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
interface Problem {
|
|
167
|
+
readonly node: AstNode
|
|
168
|
+
readonly message: string
|
|
169
|
+
readonly allowed: string
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const memberProblem = (link: Link | undefined, allowed: ReadonlySet<string>, where: string, fallback: AstNode): Problem | undefined => {
|
|
173
|
+
if (link?.kind !== 'member') return { node: link?.node ?? fallback, message: `${where} must name a builder`, allowed: ALLOWED_SCHEMA }
|
|
174
|
+
const name = link.name
|
|
175
|
+
if (name === undefined) return { node: link.node, message: 'computed builder name', allowed: ALLOWED_SCHEMA }
|
|
176
|
+
if (name === 'object') {
|
|
177
|
+
return { node: link.node, message: 'z.object() strips undeclared keys instead of refusing them', allowed: 'z.strictObject({ … })' }
|
|
178
|
+
}
|
|
179
|
+
if (ZOD_UNSERIALIZABLE.has(name)) return { node: link.node, message: `'.${name}()' makes the schema unserializable`, allowed: ALLOWED_SCHEMA }
|
|
180
|
+
if (!allowed.has(name)) return { node: link.node, message: `'${name}' is not in the supported schema subset`, allowed: ALLOWED_SCHEMA }
|
|
181
|
+
return undefined
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const calledProblem = (link: Link | undefined, previous: Link): Problem | undefined =>
|
|
185
|
+
link?.kind === 'call' ? undefined : { node: previous.node, message: 'a schema builder that is not called', allowed: ALLOWED_SCHEMA }
|
|
186
|
+
|
|
187
|
+
/** `.<method>(…)` links from `start`, each a supported schema method, each called. */
|
|
188
|
+
const schemaMethodsProblem = (root: AstNode, links: readonly Link[], start: number): Problem | undefined => {
|
|
189
|
+
for (let index = start; index < links.length; index += 2) {
|
|
190
|
+
const method = links[index]
|
|
191
|
+
const problem = memberProblem(method, ZOD_METHODS, 'a schema method', root)
|
|
192
|
+
if (problem !== undefined || method === undefined) return problem
|
|
193
|
+
const called = calledProblem(links[index + 1], method)
|
|
194
|
+
if (called !== undefined) return called
|
|
195
|
+
}
|
|
196
|
+
return undefined
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const zodChainProblem = (root: AstNode, links: readonly Link[]): Problem | undefined => {
|
|
200
|
+
const first = links[0]
|
|
201
|
+
let next = 2
|
|
202
|
+
if (first?.kind === 'member' && first.name === 'iso') {
|
|
203
|
+
const format = links[1]
|
|
204
|
+
const problem = memberProblem(format, ZOD_ISO_FORMATS, 'z.iso', first.node)
|
|
205
|
+
if (problem !== undefined || format === undefined) return problem
|
|
206
|
+
next = 3
|
|
207
|
+
const called = calledProblem(links[2], format)
|
|
208
|
+
if (called !== undefined) return called
|
|
209
|
+
} else {
|
|
210
|
+
const problem = memberProblem(first, ZOD_FACTORIES, 'z', root)
|
|
211
|
+
if (problem !== undefined || first === undefined) return problem
|
|
212
|
+
const called = calledProblem(links[1], first)
|
|
213
|
+
if (called !== undefined) return called
|
|
214
|
+
}
|
|
215
|
+
return schemaMethodsProblem(root, links, next) ?? stringOrderProblem(links, next)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const STRING_FACTORIES = new Set(['string', 'uuid', 'email', 'url', 'iso'])
|
|
219
|
+
const FORMATS = new Set(['uuid', 'email', 'url', 'iso'])
|
|
220
|
+
const AFFIXES = new Set(['startsWith', 'endsWith', 'includes'])
|
|
221
|
+
const STRING_CHECKS = new Set(['min', 'max', 'length', 'nonempty', 'regex', ...AFFIXES])
|
|
222
|
+
/** Methods after which the chain no longer checks the string itself. */
|
|
223
|
+
const WRAPPERS = new Set(['optional', 'nullable', 'nullish', 'default', 'array', 'or'])
|
|
224
|
+
|
|
225
|
+
export interface StringStepProblem {
|
|
226
|
+
/** The step that makes the chain order-dependent: 0 is the factory, then each method in turn. */
|
|
227
|
+
readonly index: number
|
|
228
|
+
readonly message: string
|
|
229
|
+
readonly allowed: string
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* The artifact keeps a string's checks unordered (`@fabrika/app`'s schema-shape.ts), so the chain may not depend on
|
|
234
|
+
* their order: `z.url()` rewrites the value the checks after it see, and an affix overlaps a format. `steps` is the
|
|
235
|
+
* factory (`iso` for `z.iso.*`) and then the methods, also across a schema const and the methods applied to it.
|
|
236
|
+
*/
|
|
237
|
+
export const stringStepProblem = (steps: readonly string[]): StringStepProblem | undefined => {
|
|
238
|
+
const [factory] = steps
|
|
239
|
+
if (factory === undefined || !STRING_FACTORIES.has(factory)) return undefined
|
|
240
|
+
let formats = FORMATS.has(factory) ? 1 : 0
|
|
241
|
+
let url = factory === 'url'
|
|
242
|
+
let affix = false
|
|
243
|
+
let checks = false
|
|
244
|
+
for (const [index, name] of steps.entries()) {
|
|
245
|
+
if (index === 0) continue
|
|
246
|
+
if (WRAPPERS.has(name)) return undefined
|
|
247
|
+
formats += FORMATS.has(name) ? 1 : 0
|
|
248
|
+
url ||= name === 'url'
|
|
249
|
+
affix ||= AFFIXES.has(name)
|
|
250
|
+
checks ||= STRING_CHECKS.has(name)
|
|
251
|
+
if (formats > 1) return { index, message: 'a string with two formats', allowed: 'one format per string' }
|
|
252
|
+
if (url && checks) {
|
|
253
|
+
return { index, message: 'z.url() rewrites the value, so another string check depends on its order', allowed: 'z.url() with no other check' }
|
|
254
|
+
}
|
|
255
|
+
if (formats > 0 && affix) {
|
|
256
|
+
return { index, message: '.startsWith(), .endsWith() and .includes() do not combine with a format', allowed: 'one or the other' }
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return undefined
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const stringOrderProblem = (links: readonly Link[], firstMethod: number): Problem | undefined => {
|
|
263
|
+
const [factory] = links
|
|
264
|
+
if (factory?.kind !== 'member' || factory.name === undefined) return undefined
|
|
265
|
+
const steps = [factory.name]
|
|
266
|
+
const nodes = [factory.node]
|
|
267
|
+
for (let index = firstMethod; index < links.length; index += 2) {
|
|
268
|
+
const method = links[index]
|
|
269
|
+
if (method?.kind !== 'member' || method.name === undefined) break
|
|
270
|
+
steps.push(method.name)
|
|
271
|
+
nodes.push(method.node)
|
|
272
|
+
}
|
|
273
|
+
const problem = stringStepProblem(steps)
|
|
274
|
+
const node = problem === undefined ? undefined : nodes[problem.index]
|
|
275
|
+
return problem === undefined || node === undefined ? undefined : { node, message: problem.message, allowed: problem.allowed }
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** `<root>.<method>(…)`, one member and one call, with `method` one of `allowed`. */
|
|
279
|
+
const methodCallProblem = (
|
|
280
|
+
root: AstNode,
|
|
281
|
+
links: readonly Link[],
|
|
282
|
+
allowed: ReadonlySet<string>,
|
|
283
|
+
prefix: string,
|
|
284
|
+
message: string,
|
|
285
|
+
): Problem | undefined => {
|
|
286
|
+
const [method, call] = links
|
|
287
|
+
const name = method?.kind === 'member' ? method.name : undefined
|
|
288
|
+
if (name === undefined || !allowed.has(name) || call?.kind !== 'call' || links.length !== 2) {
|
|
289
|
+
return { node: method?.node ?? root, message, allowed: `one of ${prefix}.${[...allowed].join(`, ${prefix}.`)}(…)` }
|
|
290
|
+
}
|
|
291
|
+
return undefined
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const shapeProblem = (kind: BuilderKind, root: AstNode, links: readonly Link[]): Problem | undefined => {
|
|
295
|
+
const calls = links.filter((link) => link.kind === 'call').length
|
|
296
|
+
switch (kind) {
|
|
297
|
+
case 'zod':
|
|
298
|
+
return zodChainProblem(root, links)
|
|
299
|
+
case 'surface':
|
|
300
|
+
return methodCallProblem(root, links, SURFACE_BUILDERS, 'c', 'not a call of a contract builder')
|
|
301
|
+
case 'kit':
|
|
302
|
+
return methodCallProblem(root, links, KIT_BUILDERS, identifierName(root) ?? 'app', 'not a call of a builder of the kit')
|
|
303
|
+
case 'shell':
|
|
304
|
+
return methodCallProblem(root, links, SHELL_BUILDERS, identifierName(root) ?? 'c', 'not a call of a contract builder')
|
|
305
|
+
case 'endpoints':
|
|
306
|
+
return methodCallProblem(root, links, ENDPOINT_BUILDERS, identifierName(root) ?? 'r', 'not a call of an endpoint builder')
|
|
307
|
+
case 'app':
|
|
308
|
+
return links.length === 1 && calls === 1
|
|
309
|
+
? undefined
|
|
310
|
+
: { node: root, message: 'defineApp used other than as one call', allowed: 'defineApp(vocabulary, tables, { imports })' }
|
|
311
|
+
case 'contract':
|
|
312
|
+
return links.length === 1 && calls === 1
|
|
313
|
+
? undefined
|
|
314
|
+
: { node: root, message: 'defineContract used other than as one call', allowed: 'defineContract(vocabulary, (c) => ({ … }))' }
|
|
315
|
+
case 'vocabulary':
|
|
316
|
+
return links.length === 1 && calls === 1
|
|
317
|
+
? undefined
|
|
318
|
+
: { node: root, message: 'defineVocabulary used other than as one call', allowed: 'defineVocabulary({ app, dimensions, actions, roles })' }
|
|
319
|
+
case 'attachment':
|
|
320
|
+
return links.length === 1 && calls === 1
|
|
321
|
+
? undefined
|
|
322
|
+
: { node: root, message: 'attachment used other than as one call', allowed: "attachment({ format: 'csv', filename, row, maxBytes })" }
|
|
323
|
+
case 'tables':
|
|
324
|
+
return calls === links.length && (calls === 1 || calls === 2)
|
|
325
|
+
? undefined
|
|
326
|
+
: {
|
|
327
|
+
node: root,
|
|
328
|
+
message: 'defineTables used other than as its call',
|
|
329
|
+
allowed: 'defineTables(vocabulary, columns)({ … })',
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const offset = (node: AstNode | undefined): number | undefined => (typeof node?.['start'] === 'number' ? node['start'] : undefined)
|
|
335
|
+
|
|
336
|
+
/** Whether `reference` stands before `declaration` in the source: evaluating it would read the const uninitialized. */
|
|
337
|
+
const isBefore = (reference: AstNode, declaration: AstNode | undefined): boolean => {
|
|
338
|
+
const at = offset(reference)
|
|
339
|
+
const declared = offset(declaration)
|
|
340
|
+
return at !== undefined && declared !== undefined && at < declared
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
interface ConstInfo {
|
|
344
|
+
readonly node: AstNode
|
|
345
|
+
/** The initializer itself has no finding. */
|
|
346
|
+
readonly clean: boolean
|
|
347
|
+
/** Top-level names the initializer refers to: consts of this module and imports. */
|
|
348
|
+
readonly dependencies: readonly string[]
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
interface LiteralSummary {
|
|
352
|
+
readonly module: ParsedModule
|
|
353
|
+
readonly consts: ReadonlyMap<string, ConstInfo>
|
|
354
|
+
readonly report: Report
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const readModule = (module: ParsedModule, graph: ModuleGraph, findings: Finding[]): LiteralSummary => {
|
|
358
|
+
const report = reporter(module.file, findings)
|
|
359
|
+
const index = graph.index(module)
|
|
360
|
+
const builders = new Map<string, BuilderKind>()
|
|
361
|
+
for (const binding of index.imports.values()) {
|
|
362
|
+
const kind = BUILDER_IMPORTS[binding.specifier]?.[binding.imported]
|
|
363
|
+
if (kind !== undefined) builders.set(binding.local, kind)
|
|
364
|
+
}
|
|
365
|
+
const consts = new Map<string, ConstInfo>()
|
|
366
|
+
let reported = 0
|
|
367
|
+
const fail = (node: AstNode, message: string, allowed: string = ALLOWED_VALUE) => {
|
|
368
|
+
reported++
|
|
369
|
+
report(node, 'literal', message, allowed)
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
type Scope = ReadonlyMap<string, BuilderKind>
|
|
373
|
+
|
|
374
|
+
const checkIdentifier = (node: AstNode, scope: Scope, dependencies: string[]): void => {
|
|
375
|
+
const name = identifierName(node) ?? ''
|
|
376
|
+
if (scope.has(name) || builders.has(name) || isKitName(graph, module, name)) {
|
|
377
|
+
return fail(node, `'${name}' used as a value`, `${name}.<builder>(…) calls only`)
|
|
378
|
+
}
|
|
379
|
+
if (index.consts.has(name) && isBefore(node, index.consts.get(name))) {
|
|
380
|
+
return fail(node, `'${name}' is used before its declaration, where the module cannot read it yet`, 'declare a const above its first use')
|
|
381
|
+
}
|
|
382
|
+
if (index.consts.has(name) || index.imports.has(name)) {
|
|
383
|
+
dependencies.push(name)
|
|
384
|
+
return
|
|
385
|
+
}
|
|
386
|
+
if (index.others.has(name)) return fail(node, `'${name}' is not a const`, 'a top-level const in this file, or a named import of one')
|
|
387
|
+
fail(node, `unknown identifier '${name}'`, 'a top-level const in this file, or a named import of one')
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const checkBuilderCallback = (node: AstNode, scope: Scope, dependencies: string[], kit: BuilderKind = 'surface'): void => {
|
|
391
|
+
const parameters = children(node, 'params')
|
|
392
|
+
const parameter = parameters.length === 1 ? identifierName(parameters[0] ?? undefined) : undefined
|
|
393
|
+
const body = child(node, 'body')
|
|
394
|
+
if (node.type !== 'ArrowFunctionExpression') {
|
|
395
|
+
return fail(node, 'the builder callback is not written here', 'an inline arrow function: (c) => ({ … })')
|
|
396
|
+
}
|
|
397
|
+
if (parameter === undefined || body === undefined || body.type === 'BlockStatement' || node['async'] === true) {
|
|
398
|
+
return fail(node, 'a builder callback of another shape', 'one parameter and an expression body: (c) => ({ … })')
|
|
399
|
+
}
|
|
400
|
+
checkExpression(body, new Map([...scope, [parameter, kit]]), dependencies)
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* A callback, or options whose `procedures` is one, are the documented builder-callback positions, each with the kit
|
|
405
|
+
* its parameter denotes; `pattern` is the argument of `.regex()`.
|
|
406
|
+
*/
|
|
407
|
+
type Role = { readonly callback: BuilderKind } | { readonly procedures: BuilderKind } | 'pattern' | 'value'
|
|
408
|
+
|
|
409
|
+
const checkArgument = (argument: AstNode, scope: Scope, dependencies: string[], role: Role): void => {
|
|
410
|
+
if (argument.type === 'SpreadElement') return fail(argument, 'spread', 'the arguments written out')
|
|
411
|
+
if (typeof role === 'object' && 'callback' in role) return checkBuilderCallback(argument, scope, dependencies, role.callback)
|
|
412
|
+
if (typeof role === 'object' && argument.type === 'ObjectExpression') {
|
|
413
|
+
return checkObject(argument, scope, dependencies, 'procedures', role.procedures)
|
|
414
|
+
}
|
|
415
|
+
if (role === 'pattern' && argument.type === 'Literal' && argument['regex'] !== undefined) return
|
|
416
|
+
checkExpression(argument, scope, dependencies)
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const roleOf = (kind: BuilderKind, builder: string | undefined, method: string | undefined, position: number): Role => {
|
|
420
|
+
if (kind === 'contract' && position === 1) return { callback: 'surface' }
|
|
421
|
+
if (kind === 'surface' && builder === 'link' && position === 1) return { procedures: 'surface' }
|
|
422
|
+
if (kind === 'zod' && method === 'regex' && position === 0) return 'pattern'
|
|
423
|
+
const callback = kind === 'kit' && builder !== undefined && Object.hasOwn(KIT_CALLBACKS, builder) ? KIT_CALLBACKS[builder] : undefined
|
|
424
|
+
if (callback !== undefined && callback.position === position) {
|
|
425
|
+
return callback.options ? { procedures: callback.kind } : { callback: callback.kind }
|
|
426
|
+
}
|
|
427
|
+
return 'value'
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* `Status.optional()`: a schema method on a const of this module or an imported one. Whether the const holds a schema
|
|
432
|
+
* is the reader's to decide; here it is a name the literal rule follows like any other.
|
|
433
|
+
*/
|
|
434
|
+
const isSchemaModifier = (name: string, links: readonly Link[]): boolean =>
|
|
435
|
+
(index.consts.has(name) || index.imports.has(name)) && links[0]?.kind === 'member' && links[0].name !== undefined
|
|
436
|
+
|
|
437
|
+
const checkCall = (node: AstNode, scope: Scope, dependencies: string[]): void => {
|
|
438
|
+
const { root, links } = decompose(node)
|
|
439
|
+
const rootName = identifierName(root)
|
|
440
|
+
const kind = rootName === undefined
|
|
441
|
+
? undefined
|
|
442
|
+
: (scope.get(rootName) ?? builders.get(rootName) ?? (isKitName(graph, module, rootName) ? 'kit' : undefined))
|
|
443
|
+
if (kind === undefined && rootName !== undefined && isSchemaModifier(rootName, links)) {
|
|
444
|
+
const problem = schemaMethodsProblem(root, links, 0)
|
|
445
|
+
if (problem !== undefined) return fail(problem.node, problem.message, problem.allowed)
|
|
446
|
+
checkIdentifier(root, scope, dependencies)
|
|
447
|
+
links.forEach((link) => {
|
|
448
|
+
if (link.kind !== 'call') return
|
|
449
|
+
if (link.node['optional'] === true) fail(link.node, 'optional call', 'a plain call')
|
|
450
|
+
for (const argument of children(link.node, 'arguments')) {
|
|
451
|
+
if (argument !== null) checkArgument(argument, scope, dependencies, 'value')
|
|
452
|
+
}
|
|
453
|
+
})
|
|
454
|
+
return
|
|
455
|
+
}
|
|
456
|
+
if (kind === undefined) {
|
|
457
|
+
return fail(
|
|
458
|
+
node,
|
|
459
|
+
'call of a function that is not c.*, z.*, define* or the kit of defineApp',
|
|
460
|
+
'the value the function returns, written as a literal',
|
|
461
|
+
)
|
|
462
|
+
}
|
|
463
|
+
const computed = links.find(isComputedMember)
|
|
464
|
+
if (computed !== undefined) return fail(computed.node, 'a computed builder member', `${rootName}.<builder>(…) written with a dot`)
|
|
465
|
+
const problem = shapeProblem(kind, root, links)
|
|
466
|
+
if (problem !== undefined) return fail(problem.node, problem.message, problem.allowed)
|
|
467
|
+
const builder = links[0]?.kind === 'member' ? links[0].name : undefined
|
|
468
|
+
links.forEach((link, index) => {
|
|
469
|
+
if (link.kind !== 'call') return
|
|
470
|
+
if (link.node['optional'] === true) fail(link.node, 'optional call', 'a plain call')
|
|
471
|
+
const previous = links[index - 1]
|
|
472
|
+
const method = previous?.kind === 'member' ? previous.name : undefined
|
|
473
|
+
children(link.node, 'arguments').forEach((argument, position) => {
|
|
474
|
+
if (argument !== null) checkArgument(argument, scope, dependencies, roleOf(kind, builder, method, position))
|
|
475
|
+
})
|
|
476
|
+
})
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
const checkObject = (node: AstNode, scope: Scope, dependencies: string[], callbackKey?: string, callbackKit?: BuilderKind): void => {
|
|
480
|
+
for (const property of children(node, 'properties')) {
|
|
481
|
+
if (property === null) continue
|
|
482
|
+
if (property.type === 'SpreadElement') {
|
|
483
|
+
fail(property, 'spread', 'the entries written out')
|
|
484
|
+
continue
|
|
485
|
+
}
|
|
486
|
+
if (property['computed'] === true) {
|
|
487
|
+
fail(property, 'computed key', 'a plain or quoted key')
|
|
488
|
+
continue
|
|
489
|
+
}
|
|
490
|
+
if (property['kind'] !== 'init' || property['method'] === true) {
|
|
491
|
+
fail(property, 'method or accessor', 'a property with a literal value')
|
|
492
|
+
continue
|
|
493
|
+
}
|
|
494
|
+
if (staticKey(property, 'key') === '__proto__') {
|
|
495
|
+
fail(property, 'a __proto__ key sets the prototype instead of a field', 'another key name')
|
|
496
|
+
continue
|
|
497
|
+
}
|
|
498
|
+
const value = child(property, 'value')
|
|
499
|
+
if (value === undefined) continue
|
|
500
|
+
if (callbackKey !== undefined && staticKey(property, 'key') === callbackKey) checkBuilderCallback(value, scope, dependencies, callbackKit)
|
|
501
|
+
else checkExpression(value, scope, dependencies)
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const checkExpression = (node: AstNode, scope: Scope, dependencies: string[]): void => {
|
|
506
|
+
switch (node.type) {
|
|
507
|
+
case 'Literal':
|
|
508
|
+
if (node['regex'] !== undefined) return fail(node, 'regular expression literal outside .regex()', 'z.string().regex(/…/)')
|
|
509
|
+
if (node['bigint'] !== undefined) return fail(node, 'bigint literal', 'a number or a string')
|
|
510
|
+
return
|
|
511
|
+
case 'TemplateLiteral':
|
|
512
|
+
if (children(node, 'expressions').length > 0) return fail(node, 'template literal with ${…}', 'a plain string')
|
|
513
|
+
return
|
|
514
|
+
case 'UnaryExpression': {
|
|
515
|
+
const argument = child(node, 'argument')
|
|
516
|
+
if (node['operator'] === '-' && argument?.type === 'Literal' && typeof argument['value'] === 'number') return
|
|
517
|
+
return fail(node, `unary '${String(node['operator'])}'`, 'the resulting value written as a literal')
|
|
518
|
+
}
|
|
519
|
+
case 'ParenthesizedExpression':
|
|
520
|
+
case 'TSSatisfiesExpression': {
|
|
521
|
+
const inner = child(node, 'expression')
|
|
522
|
+
if (inner !== undefined) checkExpression(inner, scope, dependencies)
|
|
523
|
+
return
|
|
524
|
+
}
|
|
525
|
+
case 'TSAsExpression': {
|
|
526
|
+
const inner = child(node, 'expression')
|
|
527
|
+
if (!isConstAssertion(node)) return fail(node, 'type assertion `as T`', '`as const` or `satisfies T`')
|
|
528
|
+
if (inner !== undefined) checkExpression(inner, scope, dependencies)
|
|
529
|
+
return
|
|
530
|
+
}
|
|
531
|
+
case 'ArrayExpression':
|
|
532
|
+
for (const element of children(node, 'elements')) {
|
|
533
|
+
if (element === null) fail(node, 'array hole', 'every element written out')
|
|
534
|
+
else if (element.type === 'SpreadElement') fail(element, 'spread', 'the elements written out')
|
|
535
|
+
else checkExpression(element, scope, dependencies)
|
|
536
|
+
}
|
|
537
|
+
return
|
|
538
|
+
case 'ObjectExpression':
|
|
539
|
+
return checkObject(node, scope, dependencies)
|
|
540
|
+
case 'Identifier':
|
|
541
|
+
return checkIdentifier(node, scope, dependencies)
|
|
542
|
+
case 'ArrowFunctionExpression':
|
|
543
|
+
case 'FunctionExpression':
|
|
544
|
+
return fail(node, 'function', ALLOWED_CALLBACK)
|
|
545
|
+
case 'CallExpression':
|
|
546
|
+
return checkCall(node, scope, dependencies)
|
|
547
|
+
case 'BinaryExpression':
|
|
548
|
+
case 'LogicalExpression':
|
|
549
|
+
return fail(node, `operator '${String(node['operator'])}'`, 'the resulting value written as a literal')
|
|
550
|
+
default:
|
|
551
|
+
return fail(node, `${node.type} is not a literal`, ALLOWED_VALUE)
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
const checkInitializer = (name: string, node: AstNode, init: AstNode): void => {
|
|
556
|
+
const before = reported
|
|
557
|
+
const dependencies: string[] = []
|
|
558
|
+
checkExpression(init, new Map(), dependencies)
|
|
559
|
+
consts.set(name, { node, clean: reported === before, dependencies })
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
for (const statement of children(module.program, 'body')) {
|
|
563
|
+
if (statement === null) continue
|
|
564
|
+
if (statement.type === 'ImportDeclaration') {
|
|
565
|
+
if (children(statement, 'specifiers').length === 0) {
|
|
566
|
+
fail(statement, `side-effect import of '${staticString(child(statement, 'source')) ?? ''}'`, 'named imports of literal consts')
|
|
567
|
+
}
|
|
568
|
+
continue
|
|
569
|
+
}
|
|
570
|
+
const declaration = statement.type === 'ExportNamedDeclaration' ? (child(statement, 'declaration') ?? statement) : statement
|
|
571
|
+
if (declaration.type === 'TSTypeAliasDeclaration' || declaration.type === 'TSInterfaceDeclaration') continue
|
|
572
|
+
if (declaration.type === 'ExportNamedDeclaration') {
|
|
573
|
+
if (child(declaration, 'source') !== undefined) {
|
|
574
|
+
fail(declaration, 're-export', 'import the const, then export it')
|
|
575
|
+
continue
|
|
576
|
+
}
|
|
577
|
+
for (const item of children(declaration, 'specifiers')) {
|
|
578
|
+
const local = item ? identifierName(child(item, 'local')) : undefined
|
|
579
|
+
if (item !== null && local !== undefined && !index.consts.has(local) && !index.imports.has(local)) {
|
|
580
|
+
fail(item, `exports '${local}', which is not a const of this module`, 'export a top-level const')
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
continue
|
|
584
|
+
}
|
|
585
|
+
if (declaration.type === 'ExportDefaultDeclaration') {
|
|
586
|
+
const exported = child(declaration, 'declaration')
|
|
587
|
+
if (exported !== undefined) checkInitializer('default', declaration, exported)
|
|
588
|
+
continue
|
|
589
|
+
}
|
|
590
|
+
if (declaration.type === 'VariableDeclaration') {
|
|
591
|
+
const kind = stringField(declaration, 'kind') ?? ''
|
|
592
|
+
if (kind !== 'const') {
|
|
593
|
+
fail(declaration, `'${kind}' declaration`, 'a top-level const')
|
|
594
|
+
continue
|
|
595
|
+
}
|
|
596
|
+
for (const declarator of children(declaration, 'declarations')) {
|
|
597
|
+
if (declarator === null) continue
|
|
598
|
+
const name = identifierName(child(declarator, 'id'))
|
|
599
|
+
const init = child(declarator, 'init')
|
|
600
|
+
if (name === undefined || init === undefined) fail(declarator, 'destructuring or a missing initializer', 'const <name> = <literal>')
|
|
601
|
+
else checkInitializer(name, declarator, init)
|
|
602
|
+
}
|
|
603
|
+
continue
|
|
604
|
+
}
|
|
605
|
+
fail(statement, `top-level ${declaration.type}`, 'only imports, types and const declarations of literals; move code out of the contract graph')
|
|
606
|
+
}
|
|
607
|
+
return { module, consts, report }
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* The contract, its schema modules and the table policies are literals (D5), so that the platform can read them from a
|
|
612
|
+
* commit without executing them. `entries` are the graph's roots; relative value imports pull further modules in.
|
|
613
|
+
*/
|
|
614
|
+
export const checkLiteralGraph = (entries: readonly string[], graph: ModuleGraph): Finding[] => {
|
|
615
|
+
const findings: Finding[] = []
|
|
616
|
+
const summaries = new Map<string, LiteralSummary>()
|
|
617
|
+
const queue = entries.map((entry) => graph.module(entry)).filter((module) => module !== undefined)
|
|
618
|
+
for (let module = queue.shift(); module !== undefined; module = queue.shift()) {
|
|
619
|
+
if (summaries.has(module.file)) continue
|
|
620
|
+
summaries.set(module.file, readModule(module, graph, findings))
|
|
621
|
+
for (const binding of graph.index(module).imports.values()) {
|
|
622
|
+
const target = graph.resolve(module.file, binding.specifier)
|
|
623
|
+
if (target !== undefined) queue.push(target)
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
reportImportCycles(summaries, graph)
|
|
627
|
+
|
|
628
|
+
const states = new Map<string, 'visiting' | boolean>()
|
|
629
|
+
const cycles = new Set<string>()
|
|
630
|
+
/** Whether a binding is a literal all the way down; a const defined through itself is reported at the const re-entered. */
|
|
631
|
+
const isLiteral = (binding: Binding): boolean => {
|
|
632
|
+
if (binding.kind === 'package') return BUILDER_IMPORTS[binding.specifier]?.[binding.imported] !== undefined
|
|
633
|
+
if (binding.kind !== 'const' && binding.kind !== 'default') return false
|
|
634
|
+
const name = binding.kind === 'const' ? binding.name : 'default'
|
|
635
|
+
const summary = summaries.get(binding.module.file)
|
|
636
|
+
const info = summary?.consts.get(name)
|
|
637
|
+
if (summary === undefined || info === undefined || !info.clean) return false
|
|
638
|
+
const key = `${binding.module.file}#${name}`
|
|
639
|
+
const state = states.get(key)
|
|
640
|
+
if (state === 'visiting') {
|
|
641
|
+
if (!cycles.has(key)) {
|
|
642
|
+
summary.report(info.node, 'literal', `'${name}' is defined through itself`, 'a const whose value is written out as a literal')
|
|
643
|
+
}
|
|
644
|
+
cycles.add(key)
|
|
645
|
+
return false
|
|
646
|
+
}
|
|
647
|
+
if (state !== undefined) return state
|
|
648
|
+
states.set(key, 'visiting')
|
|
649
|
+
const literal = info.dependencies.every((dependency) => isLiteral(graph.binding(binding.module, dependency)))
|
|
650
|
+
states.set(key, literal)
|
|
651
|
+
return literal
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/** A const whose own initializer was refused: the refusal stands at the offending expression, not again at each import. */
|
|
655
|
+
const refusedInPlace = (binding: Binding): boolean => {
|
|
656
|
+
if (binding.kind !== 'const' && binding.kind !== 'default') return false
|
|
657
|
+
const info = summaries.get(binding.module.file)?.consts.get(binding.kind === 'const' ? binding.name : 'default')
|
|
658
|
+
return info !== undefined && !info.clean
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
for (const summary of summaries.values()) {
|
|
662
|
+
for (const name of summary.consts.keys()) {
|
|
663
|
+
isLiteral(name === 'default' ? graph.exported(summary.module, 'default') : graph.binding(summary.module, name))
|
|
664
|
+
}
|
|
665
|
+
for (const imported of graph.index(summary.module).imports.values()) {
|
|
666
|
+
if (!isRelativeSpecifier(imported.specifier)) {
|
|
667
|
+
const browserSafe = SERVER_ENTRY_OF[imported.specifier]
|
|
668
|
+
if (browserSafe !== undefined && BUILDER_IMPORTS[browserSafe]?.[imported.imported] !== undefined) {
|
|
669
|
+
summary.report(
|
|
670
|
+
imported.node,
|
|
671
|
+
'literal',
|
|
672
|
+
`'${imported.imported}' from '${imported.specifier}', whose root also loads the server runtime the SPA bundles with the contract`,
|
|
673
|
+
`import { ${imported.imported} } from '${browserSafe}'`,
|
|
674
|
+
)
|
|
675
|
+
} else if (BUILDER_IMPORTS[imported.specifier]?.[imported.imported] === undefined) {
|
|
676
|
+
summary.report(imported.node, 'literal', `value import of '${imported.imported}' from the package '${imported.specifier}'`, ALLOWED_IMPORT)
|
|
677
|
+
}
|
|
678
|
+
continue
|
|
679
|
+
}
|
|
680
|
+
if (imported.imported === '*') {
|
|
681
|
+
summary.report(imported.node, 'literal', `namespace import of '${imported.specifier}'`, 'named imports of literal consts')
|
|
682
|
+
continue
|
|
683
|
+
}
|
|
684
|
+
const resolution = graph.resolution(summary.module.file, imported.specifier)
|
|
685
|
+
if (resolution.kind === 'ambiguous' || resolution.kind === 'directory') {
|
|
686
|
+
const what = resolution.kind === 'ambiguous'
|
|
687
|
+
? `resolves to each of ${resolution.paths.join(', ')}`
|
|
688
|
+
: `is a directory import (${resolution.paths.join(', ')})`
|
|
689
|
+
summary.report(
|
|
690
|
+
imported.node,
|
|
691
|
+
'literal',
|
|
692
|
+
`'${imported.specifier}' ${what}, which the platform does not resolve as Bun does`,
|
|
693
|
+
'a relative import that names the module file, extension included',
|
|
694
|
+
)
|
|
695
|
+
continue
|
|
696
|
+
}
|
|
697
|
+
const binding = graph.binding(summary.module, imported.local)
|
|
698
|
+
if (binding.kind === 'unresolved') summary.report(imported.node, 'literal', binding.reason, 'a named import of a const the module exports')
|
|
699
|
+
else if (!isLiteral(binding) && !refusedInPlace(binding)) {
|
|
700
|
+
summary.report(
|
|
701
|
+
imported.node,
|
|
702
|
+
'literal',
|
|
703
|
+
`'${imported.imported}' from '${imported.specifier}' is not a literal const`,
|
|
704
|
+
'an import of a const whose value is a literal',
|
|
705
|
+
)
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
return findings
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/** An import cycle among literal modules: one of them is evaluated while the other's consts are still uninitialized. */
|
|
713
|
+
const reportImportCycles = (summaries: ReadonlyMap<string, LiteralSummary>, graph: ModuleGraph): void => {
|
|
714
|
+
const state = new Map<string, 'visiting' | 'done'>()
|
|
715
|
+
const visit = (summary: LiteralSummary): void => {
|
|
716
|
+
state.set(summary.module.file, 'visiting')
|
|
717
|
+
for (const imported of graph.index(summary.module).imports.values()) {
|
|
718
|
+
const target = graph.resolve(summary.module.file, imported.specifier)
|
|
719
|
+
const next = target === undefined ? undefined : summaries.get(target.file)
|
|
720
|
+
if (next === undefined) continue
|
|
721
|
+
const seen = state.get(next.module.file)
|
|
722
|
+
if (seen === 'visiting') {
|
|
723
|
+
summary.report(
|
|
724
|
+
imported.node,
|
|
725
|
+
'literal',
|
|
726
|
+
`'${imported.specifier}' closes an import cycle`,
|
|
727
|
+
'a module graph without cycles: move the shared consts into one module',
|
|
728
|
+
)
|
|
729
|
+
} else if (seen === undefined) visit(next)
|
|
730
|
+
}
|
|
731
|
+
state.set(summary.module.file, 'done')
|
|
732
|
+
}
|
|
733
|
+
for (const summary of summaries.values()) {
|
|
734
|
+
if (!state.has(summary.module.file)) visit(summary)
|
|
735
|
+
}
|
|
736
|
+
}
|