@fabrika/check 0.0.0-stage → 0.4.0
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,644 @@
|
|
|
1
|
+
// Reads the literal subset of `rules/literal.ts` into values, without executing the application: names resolve through
|
|
2
|
+
// `ModuleGraph`, `z` chains build real zod schemas, `c.*` calls stay unapplied for `surface.ts`.
|
|
3
|
+
|
|
4
|
+
import type { z } from 'zod/v4'
|
|
5
|
+
import { type AstNode, child, children, identifierName, isConstAssertion, type ParsedModule, staticKey } from '../ast'
|
|
6
|
+
import type { Located, ModuleGraph } from '../graph'
|
|
7
|
+
import { CONTRACT_LIMITS } from '../limits'
|
|
8
|
+
import {
|
|
9
|
+
BUILDER_IMPORTS,
|
|
10
|
+
type BuilderKind,
|
|
11
|
+
decompose,
|
|
12
|
+
isComputedMember,
|
|
13
|
+
isKitName,
|
|
14
|
+
KIT_BUILDERS,
|
|
15
|
+
KIT_CALLBACKS,
|
|
16
|
+
type Link,
|
|
17
|
+
stringStepProblem,
|
|
18
|
+
} from '../rules/literal'
|
|
19
|
+
import type { ReaderProblems } from './problems'
|
|
20
|
+
import { buildSchema } from './schema'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The builder kit a call belongs to: `c` of `defineContract` and `l` of its link's `procedures`; `app`, the kit
|
|
24
|
+
* `defineApp` returned; `c` of `app.contract` (`shell`) and `r` of a group, a link or an export (`endpoints`).
|
|
25
|
+
*/
|
|
26
|
+
export type Kit = 'surface' | 'link' | 'app' | 'shell' | 'endpoints'
|
|
27
|
+
|
|
28
|
+
/** The parameter name a kit is written with, in messages. */
|
|
29
|
+
export const kitName = (kit: Kit): string => (kit === 'link' ? 'l' : kit === 'app' ? 'app' : kit === 'endpoints' ? 'r' : 'c')
|
|
30
|
+
|
|
31
|
+
export type LiteralValue =
|
|
32
|
+
| { readonly kind: 'string'; readonly value: string; readonly at: Located }
|
|
33
|
+
| { readonly kind: 'number'; readonly value: number; readonly at: Located }
|
|
34
|
+
| { readonly kind: 'boolean'; readonly value: boolean; readonly at: Located }
|
|
35
|
+
| { readonly kind: 'null'; readonly at: Located }
|
|
36
|
+
| { readonly kind: 'regex'; readonly value: RegExp; readonly at: Located }
|
|
37
|
+
| { readonly kind: 'array'; readonly items: readonly LiteralValue[]; readonly at: Located }
|
|
38
|
+
/** Entries in source order; a repeated key keeps its first position and its last value, as in an object literal. */
|
|
39
|
+
| { readonly kind: 'object'; readonly entries: ReadonlyMap<string, LiteralValue>; readonly at: Located }
|
|
40
|
+
/** `nodes` counts the schema with every shared part expanded, as the artifact will hold it; `depth` its nesting. */
|
|
41
|
+
| {
|
|
42
|
+
readonly kind: 'schema'
|
|
43
|
+
readonly schema: z.ZodType
|
|
44
|
+
readonly nodes: number
|
|
45
|
+
readonly depth: number
|
|
46
|
+
/** The chain that built it, factory first (`iso` for `z.iso.*`), across a const and the methods applied to it. */
|
|
47
|
+
readonly steps: readonly string[]
|
|
48
|
+
readonly at: Located
|
|
49
|
+
}
|
|
50
|
+
/** `c.rpc(…)` or `l.query(…)`, with its arguments read. */
|
|
51
|
+
| { readonly kind: 'builder'; readonly kit: Kit; readonly name: string; readonly args: readonly LiteralValue[]; readonly at: Located }
|
|
52
|
+
| { readonly kind: 'contract'; readonly args: readonly LiteralValue[]; readonly at: Located }
|
|
53
|
+
/** `defineApp(vocabulary, tables, options?)`: the vocabulary and the options; the tables are `src/db/tables.ts`'. */
|
|
54
|
+
| AppValue
|
|
55
|
+
/** `app.contract((c) => ({ … }))`, with the kit it was called on. */
|
|
56
|
+
| { readonly kind: 'kit-contract'; readonly app: AppValue; readonly args: readonly LiteralValue[]; readonly at: Located }
|
|
57
|
+
/** `attachment({ … })`, with its argument read; `surface.ts` builds the output schema the evaluated module holds. */
|
|
58
|
+
| { readonly kind: 'attachment'; readonly args: readonly LiteralValue[]; readonly at: Located }
|
|
59
|
+
/** A builder callback: its parameter is only ever a kit, so its body is read once, as a value. */
|
|
60
|
+
| { readonly kind: 'callback'; readonly result: LiteralValue; readonly at: Located }
|
|
61
|
+
|
|
62
|
+
export interface AppValue {
|
|
63
|
+
readonly kind: 'app'
|
|
64
|
+
readonly vocabulary: LiteralValue
|
|
65
|
+
readonly options: LiteralValue | undefined
|
|
66
|
+
readonly at: Located
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
type Scope = ReadonlyMap<string, Kit>
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Where a callback may stand, as `rules/literal.ts` allows it: a builder's callback argument, or the `procedures` of
|
|
73
|
+
* its options (a link, an export).
|
|
74
|
+
*/
|
|
75
|
+
type Role = { readonly callback: Kit } | { readonly procedures: Kit } | null
|
|
76
|
+
|
|
77
|
+
/** The value kit of a callback parameter whose builder kind `rules/literal.ts` names. */
|
|
78
|
+
const callbackKit = (kind: BuilderKind): Kit => (kind === 'shell' ? 'shell' : kind === 'endpoints' ? 'endpoints' : 'surface')
|
|
79
|
+
|
|
80
|
+
/** The fields `defineVocabulary` copies from its argument, in its order. */
|
|
81
|
+
const VOCABULARY_SECTIONS = ['app', 'dimensions', 'actions', 'roles', 'locale', 'displayName', 'mail'] as const
|
|
82
|
+
|
|
83
|
+
const ALLOWED_VALUE = 'object and array literals, strings, numbers, booleans, null, the c.* and z.* builders, and const imports of such values'
|
|
84
|
+
|
|
85
|
+
export class ValueReader {
|
|
86
|
+
readonly #graph: ModuleGraph
|
|
87
|
+
readonly #problems: ReaderProblems
|
|
88
|
+
/** Each const is read once, so a schema const used twice is one schema, as when the module is evaluated. */
|
|
89
|
+
readonly #consts = new Map<string, LiteralValue | null | 'reading'>()
|
|
90
|
+
|
|
91
|
+
constructor(graph: ModuleGraph, problems: ReaderProblems) {
|
|
92
|
+
this.#graph = graph
|
|
93
|
+
this.#problems = problems
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The value of one expression outside any builder callback, or `null` after recording why there is none. */
|
|
97
|
+
value(located: Located): LiteralValue | null {
|
|
98
|
+
return this.#read(located, new Map(), null)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The value `name` is exported as from `module`, or `null` after recording why there is none. */
|
|
102
|
+
exported(module: ParsedModule, name: string, at: AstNode): LiteralValue | null {
|
|
103
|
+
const binding = this.#graph.exported(module, name)
|
|
104
|
+
if (binding.kind === 'const' && binding.init !== undefined) {
|
|
105
|
+
return this.#const(binding.module, binding.name, binding.init)
|
|
106
|
+
}
|
|
107
|
+
if (binding.kind === 'default') {
|
|
108
|
+
return this.#const(binding.module, 'default', binding.expression)
|
|
109
|
+
}
|
|
110
|
+
this.#problems.add({ module, node: at }, `'${module.file}' exports no const '${name}'`, `export const ${name} = …`)
|
|
111
|
+
return null
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
#const(module: ParsedModule, name: string, init: AstNode): LiteralValue | null {
|
|
115
|
+
const key = `${module.file}#${name}`
|
|
116
|
+
const known = this.#consts.get(key)
|
|
117
|
+
if (known === 'reading') {
|
|
118
|
+
this.#problems.add({ module, node: init }, `'${name}' is defined through itself`, 'a const whose value is written out as a literal')
|
|
119
|
+
return null
|
|
120
|
+
}
|
|
121
|
+
if (known !== undefined) {
|
|
122
|
+
return known
|
|
123
|
+
}
|
|
124
|
+
this.#consts.set(key, 'reading')
|
|
125
|
+
const value = this.#read({ module, node: init }, new Map(), null)
|
|
126
|
+
this.#consts.set(key, value)
|
|
127
|
+
return value
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
#read(located: Located, scope: Scope, role: Role): LiteralValue | null {
|
|
131
|
+
const { module, node } = located
|
|
132
|
+
const fail = (message: string, allowed = ALLOWED_VALUE): null => {
|
|
133
|
+
this.#problems.add(located, message, allowed)
|
|
134
|
+
return null
|
|
135
|
+
}
|
|
136
|
+
switch (node.type) {
|
|
137
|
+
case 'Literal':
|
|
138
|
+
return this.#literal(located)
|
|
139
|
+
case 'TemplateLiteral': {
|
|
140
|
+
const quasi = children(node, 'quasis')[0]
|
|
141
|
+
const cooked = quasi ? templateCooked(quasi) : undefined
|
|
142
|
+
return children(node, 'expressions').length === 0 && cooked !== undefined
|
|
143
|
+
? { kind: 'string', value: cooked, at: located }
|
|
144
|
+
: fail('a template literal with ${…} or an invalid escape', 'a plain string')
|
|
145
|
+
}
|
|
146
|
+
case 'UnaryExpression': {
|
|
147
|
+
const argument = child(node, 'argument')
|
|
148
|
+
const value = argument?.type === 'Literal' ? argument['value'] : undefined
|
|
149
|
+
return node['operator'] === '-' && typeof value === 'number'
|
|
150
|
+
? { kind: 'number', value: -value, at: located }
|
|
151
|
+
: fail(`unary '${String(node['operator'])}'`, 'the resulting value written as a literal')
|
|
152
|
+
}
|
|
153
|
+
case 'TSSatisfiesExpression':
|
|
154
|
+
case 'TSAsExpression': {
|
|
155
|
+
const inner = child(node, 'expression')
|
|
156
|
+
if (node.type === 'TSAsExpression' && !isConstAssertion(node)) {
|
|
157
|
+
return fail('type assertion `as T`', '`as const` or `satisfies T`')
|
|
158
|
+
}
|
|
159
|
+
return inner === undefined ? fail('an empty expression') : this.#read({ module, node: inner }, scope, role)
|
|
160
|
+
}
|
|
161
|
+
case 'ArrayExpression':
|
|
162
|
+
return this.#array(located, scope)
|
|
163
|
+
case 'ObjectExpression':
|
|
164
|
+
return this.#object(located, scope, role !== null && 'procedures' in role ? role.procedures : null)
|
|
165
|
+
case 'Identifier':
|
|
166
|
+
return this.#identifier(located, scope)
|
|
167
|
+
case 'ArrowFunctionExpression':
|
|
168
|
+
return role !== null && 'callback' in role
|
|
169
|
+
? this.#callback(located, scope, role.callback)
|
|
170
|
+
: fail(
|
|
171
|
+
'function',
|
|
172
|
+
'a function only as a builder callback: defineContract(…, (c) => …), app.contract((c) => …), app.group(…, (r) => …), procedures: (l) => …',
|
|
173
|
+
)
|
|
174
|
+
case 'CallExpression':
|
|
175
|
+
return this.#call(located, scope)
|
|
176
|
+
default:
|
|
177
|
+
return fail(`${node.type} is not a literal`)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
#literal(located: Located): LiteralValue | null {
|
|
182
|
+
const { node } = located
|
|
183
|
+
const value = node['value']
|
|
184
|
+
const regex = node['regex']
|
|
185
|
+
if (regex !== undefined) {
|
|
186
|
+
const pattern = isRecord(regex) ? regex['pattern'] : undefined
|
|
187
|
+
const flags = isRecord(regex) ? regex['flags'] : undefined
|
|
188
|
+
const compiled = typeof pattern === 'string' && typeof flags === 'string' ? compile(pattern, flags) : undefined
|
|
189
|
+
if (compiled === undefined) {
|
|
190
|
+
this.#problems.add(located, 'a regular expression the reader cannot compile', 'a regular expression literal without errors')
|
|
191
|
+
return null
|
|
192
|
+
}
|
|
193
|
+
return { kind: 'regex', value: compiled, at: located }
|
|
194
|
+
}
|
|
195
|
+
if (node['bigint'] !== undefined) {
|
|
196
|
+
this.#problems.add(located, 'bigint literal', 'a number or a string')
|
|
197
|
+
return null
|
|
198
|
+
}
|
|
199
|
+
if (typeof value === 'string') {
|
|
200
|
+
return { kind: 'string', value, at: located }
|
|
201
|
+
}
|
|
202
|
+
if (typeof value === 'number') {
|
|
203
|
+
return { kind: 'number', value, at: located }
|
|
204
|
+
}
|
|
205
|
+
if (typeof value === 'boolean') {
|
|
206
|
+
return { kind: 'boolean', value, at: located }
|
|
207
|
+
}
|
|
208
|
+
if (value === null) {
|
|
209
|
+
return { kind: 'null', at: located }
|
|
210
|
+
}
|
|
211
|
+
this.#problems.add(located, 'a literal the reader does not know', ALLOWED_VALUE)
|
|
212
|
+
return null
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
#array(located: Located, scope: Scope): LiteralValue | null {
|
|
216
|
+
const items: LiteralValue[] = []
|
|
217
|
+
let complete = true
|
|
218
|
+
for (const element of children(located.node, 'elements')) {
|
|
219
|
+
if (element === null || element.type === 'SpreadElement') {
|
|
220
|
+
this.#problems.add(element === null ? located : { module: located.module, node: element }, 'array hole or spread', 'every element written out')
|
|
221
|
+
complete = false
|
|
222
|
+
continue
|
|
223
|
+
}
|
|
224
|
+
const item = this.#read({ module: located.module, node: element }, scope, null)
|
|
225
|
+
if (item === null) {
|
|
226
|
+
complete = false
|
|
227
|
+
} else {
|
|
228
|
+
items.push(item)
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return complete ? { kind: 'array', items, at: located } : null
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
#object(located: Located, scope: Scope, procedures: Kit | null): LiteralValue | null {
|
|
235
|
+
const entries = new Map<string, LiteralValue>()
|
|
236
|
+
let complete = true
|
|
237
|
+
for (const property of children(located.node, 'properties')) {
|
|
238
|
+
if (property === null) {
|
|
239
|
+
continue
|
|
240
|
+
}
|
|
241
|
+
const at = { module: located.module, node: property }
|
|
242
|
+
const key = staticKey(property, 'key')
|
|
243
|
+
const value = child(property, 'value')
|
|
244
|
+
if (
|
|
245
|
+
property.type !== 'Property' || property['computed'] === true || property['kind'] !== 'init' || property['method'] === true
|
|
246
|
+
|| key === undefined || key === '__proto__' || value === undefined
|
|
247
|
+
) {
|
|
248
|
+
this.#problems.add(at, 'a spread, computed key, method or __proto__ key', 'a plain or quoted key with a literal value')
|
|
249
|
+
complete = false
|
|
250
|
+
continue
|
|
251
|
+
}
|
|
252
|
+
const role: Role = procedures !== null && key === 'procedures' ? { callback: procedures } : null
|
|
253
|
+
const read = this.#read({ module: located.module, node: value }, scope, role)
|
|
254
|
+
if (read === null) {
|
|
255
|
+
complete = false
|
|
256
|
+
} else {
|
|
257
|
+
entries.set(key, read)
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return complete ? { kind: 'object', entries, at: located } : null
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
#identifier(located: Located, scope: Scope): LiteralValue | null {
|
|
264
|
+
const name = identifierName(located.node) ?? ''
|
|
265
|
+
const builder = this.#builderOf(located.module, name)
|
|
266
|
+
if (scope.has(name) || builder !== undefined || isKitName(this.#graph, located.module, name)) {
|
|
267
|
+
this.#problems.add(located, `'${name}' used as a value`, `${name}.<builder>(…) calls only`)
|
|
268
|
+
return null
|
|
269
|
+
}
|
|
270
|
+
const binding = this.#graph.binding(located.module, name)
|
|
271
|
+
if (binding.kind === 'const' && binding.init !== undefined) {
|
|
272
|
+
return this.#const(binding.module, binding.name, binding.init)
|
|
273
|
+
}
|
|
274
|
+
if (binding.kind === 'default') {
|
|
275
|
+
return this.#const(binding.module, 'default', binding.expression)
|
|
276
|
+
}
|
|
277
|
+
const reason = binding.kind === 'unresolved' ? binding.reason : `'${name}' is not a const`
|
|
278
|
+
this.#problems.add(located, reason, 'a top-level const in this file, or a named import of one')
|
|
279
|
+
return null
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
#callback(located: Located, scope: Scope, kit: Kit): LiteralValue | null {
|
|
283
|
+
const { node } = located
|
|
284
|
+
const parameters = children(node, 'params')
|
|
285
|
+
const parameter = parameters.length === 1 ? identifierName(parameters[0] ?? undefined) : undefined
|
|
286
|
+
const body = child(node, 'body')
|
|
287
|
+
if (parameter === undefined || body === undefined || body.type === 'BlockStatement' || node['async'] === true) {
|
|
288
|
+
this.#problems.add(located, 'a builder callback of another shape', 'one parameter and an expression body: (c) => ({ … })')
|
|
289
|
+
return null
|
|
290
|
+
}
|
|
291
|
+
const result = this.#read({ module: located.module, node: body }, new Map([...scope, [parameter, kit]]), null)
|
|
292
|
+
return result === null ? null : { kind: 'callback', result, at: located }
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
#call(located: Located, scope: Scope): LiteralValue | null {
|
|
296
|
+
const { root, links } = decompose(located.node)
|
|
297
|
+
const rootName = identifierName(root) ?? ''
|
|
298
|
+
const kit = scope.get(rootName)
|
|
299
|
+
const optional = links.find((link) => link.kind === 'call' && link.node['optional'] === true)
|
|
300
|
+
if (optional !== undefined) {
|
|
301
|
+
this.#problems.add({ module: located.module, node: optional.node }, 'optional call', 'a plain call')
|
|
302
|
+
return null
|
|
303
|
+
}
|
|
304
|
+
const computed = links.find(isComputedMember)
|
|
305
|
+
if (computed !== undefined) {
|
|
306
|
+
this.#problems.add({ module: located.module, node: computed.node }, 'a computed builder member', `${rootName}.<builder>(…) written with a dot`)
|
|
307
|
+
return null
|
|
308
|
+
}
|
|
309
|
+
if (kit !== undefined) {
|
|
310
|
+
return this.#kitCall(located, kit, links, scope)
|
|
311
|
+
}
|
|
312
|
+
if (isKitName(this.#graph, located.module, rootName)) {
|
|
313
|
+
return this.#appMethodCall(located, rootName, links, scope)
|
|
314
|
+
}
|
|
315
|
+
switch (this.#builderOf(located.module, rootName)) {
|
|
316
|
+
case 'zod':
|
|
317
|
+
return this.#zodChain(located, links, scope)
|
|
318
|
+
case 'contract':
|
|
319
|
+
return this.#contractCall(located, links, scope)
|
|
320
|
+
case 'vocabulary':
|
|
321
|
+
return this.#vocabularyCall(located, links, scope)
|
|
322
|
+
case 'app':
|
|
323
|
+
return this.#appCall(located, links, scope)
|
|
324
|
+
case 'tables':
|
|
325
|
+
this.#problems.add(located, 'defineTables in the contract graph', 'table policies only in src/db/tables.ts')
|
|
326
|
+
return null
|
|
327
|
+
case 'attachment':
|
|
328
|
+
return this.#attachmentCall(located, links, scope)
|
|
329
|
+
default:
|
|
330
|
+
if (this.#isConstName(located.module, rootName) && links[0]?.kind === 'member') {
|
|
331
|
+
return this.#schemaMethods(located, root, links, scope)
|
|
332
|
+
}
|
|
333
|
+
this.#problems.add(located, 'call of a function that is not c.*, z.* or defineContract', 'the value the function returns, written as a literal')
|
|
334
|
+
return null
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** Whether `name` is a top-level const of `module` or an import of one: a schema it holds takes methods. */
|
|
339
|
+
#isConstName(module: ParsedModule, name: string): boolean {
|
|
340
|
+
const index = this.#graph.index(module)
|
|
341
|
+
return index.consts.has(name) || index.imports.has(name)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** `Status.optional()`: the methods applied to the schema a literal const holds, as zod applies them at runtime. */
|
|
345
|
+
#schemaMethods(located: Located, root: AstNode, links: readonly Link[], scope: Scope): LiteralValue | null {
|
|
346
|
+
const steps = this.#steps(located, links, 0)
|
|
347
|
+
if (steps === null) {
|
|
348
|
+
return null
|
|
349
|
+
}
|
|
350
|
+
const receiver = this.#identifier({ module: located.module, node: root }, scope)
|
|
351
|
+
if (receiver === null) {
|
|
352
|
+
return null
|
|
353
|
+
}
|
|
354
|
+
if (receiver.kind !== 'schema') {
|
|
355
|
+
const name = identifierName(root) ?? ''
|
|
356
|
+
this.#problems.add(located, `'${name}' is not a schema, so it takes no .${steps[0]?.name ?? ''}()`, 'a method on a z schema')
|
|
357
|
+
return null
|
|
358
|
+
}
|
|
359
|
+
return this.#applySteps(located, steps, scope, receiver, false)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
#kitCall(located: Located, kit: Kit, links: readonly Link[], scope: Scope): LiteralValue | null {
|
|
363
|
+
const [method, call] = links
|
|
364
|
+
const name = method?.kind === 'member' ? method.name : undefined
|
|
365
|
+
if (name === undefined || call?.kind !== 'call' || links.length !== 2) {
|
|
366
|
+
this.#problems.add(
|
|
367
|
+
located,
|
|
368
|
+
'not a call of a contract builder',
|
|
369
|
+
kit === 'link' || kit === 'endpoints' ? `${kitName(kit)}.query(…) or ${kitName(kit)}.mutation(…)` : 'c.<builder>(…)',
|
|
370
|
+
)
|
|
371
|
+
return null
|
|
372
|
+
}
|
|
373
|
+
const args = this.#arguments(
|
|
374
|
+
located.module,
|
|
375
|
+
call.node,
|
|
376
|
+
scope,
|
|
377
|
+
(position) => (kit === 'surface' && name === 'link' && position === 1 ? { procedures: 'link' } : null),
|
|
378
|
+
)
|
|
379
|
+
return args === null ? null : { kind: 'builder', kit, name, args, at: located }
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* `defineApp(vocabulary)` or `defineApp(vocabulary, tables, options?)`. The tables are not read here: `src/db/tables.ts` is read on its own
|
|
384
|
+
* (`readTablesSource`), and the runtime takes them for the types only.
|
|
385
|
+
*/
|
|
386
|
+
#appCall(located: Located, links: readonly Link[], scope: Scope): LiteralValue | null {
|
|
387
|
+
const [call] = links
|
|
388
|
+
const argumentNodes = call?.kind === 'call' && links.length === 1 ? children(call.node, 'arguments') : []
|
|
389
|
+
if (call?.kind !== 'call' || links.length !== 1 || argumentNodes.length < 1 || argumentNodes.length > 3) {
|
|
390
|
+
this.#problems.add(
|
|
391
|
+
located,
|
|
392
|
+
'defineApp used other than as one call of the vocabulary, the tables and its options',
|
|
393
|
+
'defineApp(vocabulary, tables, { imports })',
|
|
394
|
+
)
|
|
395
|
+
return null
|
|
396
|
+
}
|
|
397
|
+
const read = (node: AstNode | null | undefined): LiteralValue | null | undefined => {
|
|
398
|
+
if (node === undefined) return undefined
|
|
399
|
+
if (node === null || node.type === 'SpreadElement') {
|
|
400
|
+
this.#problems.add({ module: located.module, node: node ?? call.node }, 'spread', 'the arguments written out')
|
|
401
|
+
return null
|
|
402
|
+
}
|
|
403
|
+
return this.#read({ module: located.module, node }, scope, null)
|
|
404
|
+
}
|
|
405
|
+
const vocabulary = read(argumentNodes[0])
|
|
406
|
+
const options = read(argumentNodes[2])
|
|
407
|
+
if (vocabulary === null || vocabulary === undefined || options === null) {
|
|
408
|
+
return null
|
|
409
|
+
}
|
|
410
|
+
return { kind: 'app', vocabulary, options, at: located }
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** `app.<builder>(…)` on the kit a const `defineApp(…)` returned; its callbacks are read with the kit they hand. */
|
|
414
|
+
#appMethodCall(located: Located, rootName: string, links: readonly Link[], scope: Scope): LiteralValue | null {
|
|
415
|
+
const [method, call] = links
|
|
416
|
+
const name = method?.kind === 'member' ? method.name : undefined
|
|
417
|
+
if (name === undefined || !KIT_BUILDERS.has(name) || call?.kind !== 'call' || links.length !== 2) {
|
|
418
|
+
this.#problems.add(located, 'not a call of a builder of the kit', `one of ${rootName}.${[...KIT_BUILDERS].join(`, ${rootName}.`)}(…)`)
|
|
419
|
+
return null
|
|
420
|
+
}
|
|
421
|
+
const binding = this.#graph.binding(located.module, rootName)
|
|
422
|
+
const app = binding.kind === 'const' && binding.init !== undefined ? this.#const(binding.module, binding.name, binding.init) : null
|
|
423
|
+
if (app?.kind !== 'app') {
|
|
424
|
+
return null
|
|
425
|
+
}
|
|
426
|
+
const callback = Object.hasOwn(KIT_CALLBACKS, name) ? KIT_CALLBACKS[name] : undefined
|
|
427
|
+
const args = this.#arguments(located.module, call.node, scope, (position): Role => {
|
|
428
|
+
if (callback === undefined || callback.position !== position) return null
|
|
429
|
+
return callback.options ? { procedures: callbackKit(callback.kind) } : { callback: callbackKit(callback.kind) }
|
|
430
|
+
})
|
|
431
|
+
if (args === null) {
|
|
432
|
+
return null
|
|
433
|
+
}
|
|
434
|
+
return name === 'contract' ? { kind: 'kit-contract', app, args, at: located } : { kind: 'builder', kit: 'app', name, args, at: located }
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* `defineVocabulary({ … })` returns the object's `app`, `dimensions`, `actions` and `roles`, and nothing else, so the
|
|
439
|
+
* reader reads that projection of an object literal and refuses any other argument.
|
|
440
|
+
*/
|
|
441
|
+
#vocabularyCall(located: Located, links: readonly Link[], scope: Scope): LiteralValue | null {
|
|
442
|
+
const [call] = links
|
|
443
|
+
const args = call?.kind === 'call' && links.length === 1 ? this.#arguments(located.module, call.node, scope, () => null) : null
|
|
444
|
+
const [vocabulary] = args ?? []
|
|
445
|
+
if (args === null || args.length !== 1 || vocabulary?.kind !== 'object') {
|
|
446
|
+
this.#problems.add(located, 'defineVocabulary used other than as one call of one object', 'defineVocabulary({ app, dimensions, actions, roles })')
|
|
447
|
+
return null
|
|
448
|
+
}
|
|
449
|
+
const entries = new Map<string, LiteralValue>()
|
|
450
|
+
for (const section of VOCABULARY_SECTIONS) {
|
|
451
|
+
const entry = vocabulary.entries.get(section)
|
|
452
|
+
if (entry !== undefined) {
|
|
453
|
+
entries.set(section, entry)
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return { kind: 'object', entries, at: vocabulary.at }
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
#attachmentCall(located: Located, links: readonly Link[], scope: Scope): LiteralValue | null {
|
|
460
|
+
const [call] = links
|
|
461
|
+
if (call?.kind !== 'call' || links.length !== 1) {
|
|
462
|
+
this.#problems.add(located, 'attachment used other than as one call', "attachment({ format: 'csv', filename, row, maxBytes })")
|
|
463
|
+
return null
|
|
464
|
+
}
|
|
465
|
+
const args = this.#arguments(located.module, call.node, scope, () => null)
|
|
466
|
+
return args === null ? null : { kind: 'attachment', args, at: located }
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
#contractCall(located: Located, links: readonly Link[], scope: Scope): LiteralValue | null {
|
|
470
|
+
const [call] = links
|
|
471
|
+
if (call?.kind !== 'call' || links.length !== 1) {
|
|
472
|
+
this.#problems.add(located, 'defineContract used other than as one call', 'defineContract(vocabulary, (c) => ({ … }))')
|
|
473
|
+
return null
|
|
474
|
+
}
|
|
475
|
+
const args = this.#arguments(located.module, call.node, scope, (position) => (position === 1 ? { callback: 'surface' } : null))
|
|
476
|
+
return args === null ? null : { kind: 'contract', args, at: located }
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/** `z.<factory>(…)` or `z.iso.<format>(…)`, then `.<method>(…)` links, each applied to a real zod schema. */
|
|
480
|
+
#zodChain(located: Located, links: readonly Link[], scope: Scope): LiteralValue | null {
|
|
481
|
+
const isIso = links[0]?.kind === 'member' && links[0].name === 'iso'
|
|
482
|
+
const steps = this.#steps(located, links, isIso ? 1 : 0)
|
|
483
|
+
return steps === null ? null : this.#applySteps(located, steps, scope, null, isIso)
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** The `.<name>(…)` pairs of a chain from `start`. */
|
|
487
|
+
#steps(located: Located, links: readonly Link[], start: number): readonly SchemaStep[] | null {
|
|
488
|
+
const steps: SchemaStep[] = []
|
|
489
|
+
for (let index = start; index < links.length; index += 2) {
|
|
490
|
+
const member = links[index]
|
|
491
|
+
const call = links[index + 1]
|
|
492
|
+
if (member?.kind !== 'member' || member.name === undefined || call?.kind !== 'call') {
|
|
493
|
+
this.#problems.add(located, 'a z chain other than z.<factory>(…).<method>(…)', 'a schema of the supported z subset (packages/check/CLAUDE.md)')
|
|
494
|
+
return null
|
|
495
|
+
}
|
|
496
|
+
steps.push({ name: member.name, call: call.node })
|
|
497
|
+
}
|
|
498
|
+
return steps
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** Each step applied in turn: the first a factory unless the chain starts from a `receiver` schema. */
|
|
502
|
+
#applySteps(
|
|
503
|
+
located: Located,
|
|
504
|
+
steps: readonly SchemaStep[],
|
|
505
|
+
scope: Scope,
|
|
506
|
+
receiver: Extract<LiteralValue, { kind: 'schema' }> | null,
|
|
507
|
+
isIso: boolean,
|
|
508
|
+
): LiteralValue | null {
|
|
509
|
+
const added = steps.map((step) => step.name)
|
|
510
|
+
// `z.iso.<format>()` counts as the one format `iso`, as the literal rule reads it.
|
|
511
|
+
const names = receiver === null ? (isIso ? ['iso', ...added.slice(1)] : added) : [...receiver.steps, ...added]
|
|
512
|
+
if (receiver !== null) {
|
|
513
|
+
// A method on a schema const meets the const's own checks only here: the literal rule saw the two apart.
|
|
514
|
+
const order = stringStepProblem(names)
|
|
515
|
+
const offending = order === undefined ? undefined : steps[order.index - receiver.steps.length]
|
|
516
|
+
if (order !== undefined && offending !== undefined) {
|
|
517
|
+
this.#problems.add({ module: located.module, node: offending.call }, order.message, order.allowed)
|
|
518
|
+
return null
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
let schema: z.ZodType | undefined = receiver?.schema
|
|
522
|
+
let size: SchemaSize = receiver === null ? { nodes: 0, depth: 0 } : { nodes: receiver.nodes, depth: receiver.depth }
|
|
523
|
+
for (const step of steps) {
|
|
524
|
+
const at = { module: located.module, node: step.call }
|
|
525
|
+
const args = this.#arguments(located.module, step.call, scope, () => null)
|
|
526
|
+
if (args === null) {
|
|
527
|
+
return null
|
|
528
|
+
}
|
|
529
|
+
const factory = schema === undefined
|
|
530
|
+
size = stepSize(factory ? null : step.name, size, schemaParts(args))
|
|
531
|
+
if (size.nodes > CONTRACT_LIMITS.schemaNodes || size.depth > CONTRACT_LIMITS.schemaDepth) {
|
|
532
|
+
this.#problems.add(at, `the schema expands to ${size.nodes} nodes nested ${size.depth} deep`, schemaLimits())
|
|
533
|
+
return null
|
|
534
|
+
}
|
|
535
|
+
const built = buildSchema(factory ? { factory: step.name, iso: isIso } : { method: step.name, receiver: schema }, args)
|
|
536
|
+
if (!built.ok) {
|
|
537
|
+
this.#problems.add(at, built.message, built.allowed)
|
|
538
|
+
return null
|
|
539
|
+
}
|
|
540
|
+
schema = built.schema
|
|
541
|
+
}
|
|
542
|
+
return schema === undefined ? null : { kind: 'schema', schema, nodes: size.nodes, depth: size.depth, steps: names, at: located }
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
#arguments(module: ParsedModule, call: AstNode, scope: Scope, roleAt: (position: number) => Role): LiteralValue[] | null {
|
|
546
|
+
const values: LiteralValue[] = []
|
|
547
|
+
let complete = true
|
|
548
|
+
for (const [position, argument] of children(call, 'arguments').entries()) {
|
|
549
|
+
if (argument === null || argument.type === 'SpreadElement') {
|
|
550
|
+
this.#problems.add({ module, node: argument ?? call }, 'spread', 'the arguments written out')
|
|
551
|
+
complete = false
|
|
552
|
+
continue
|
|
553
|
+
}
|
|
554
|
+
const value = this.#read({ module, node: argument }, scope, roleAt(position))
|
|
555
|
+
if (value === null) {
|
|
556
|
+
complete = false
|
|
557
|
+
} else {
|
|
558
|
+
values.push(value)
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return complete ? values : null
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** What an imported name builds, when it is one of the builders the platform reads. */
|
|
565
|
+
#builderOf(module: ParsedModule, name: string): BuilderKind | undefined {
|
|
566
|
+
const imported = this.#graph.index(module).imports.get(name)
|
|
567
|
+
return imported === undefined ? undefined : BUILDER_IMPORTS[imported.specifier]?.[imported.imported]
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
interface SchemaStep {
|
|
572
|
+
readonly name: string
|
|
573
|
+
readonly call: AstNode
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
interface SchemaSize {
|
|
577
|
+
readonly nodes: number
|
|
578
|
+
readonly depth: number
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
export const schemaLimits = (): string =>
|
|
582
|
+
`a schema within ${CONTRACT_LIMITS.schemaNodes} expanded nodes and ${CONTRACT_LIMITS.schemaDepth} levels (CONTRACT_LIMITS)`
|
|
583
|
+
|
|
584
|
+
/** The schemas among a step's arguments, through the arrays and objects that hold them, summed and at their deepest. */
|
|
585
|
+
const schemaParts = (values: readonly LiteralValue[]): SchemaSize => {
|
|
586
|
+
let nodes = 0
|
|
587
|
+
let depth = 0
|
|
588
|
+
const pending = [...values]
|
|
589
|
+
for (let value = pending.pop(); value !== undefined; value = pending.pop()) {
|
|
590
|
+
if (value.kind === 'schema') {
|
|
591
|
+
nodes += value.nodes
|
|
592
|
+
depth = Math.max(depth, value.depth)
|
|
593
|
+
} else if (value.kind === 'array') {
|
|
594
|
+
pending.push(...value.items)
|
|
595
|
+
} else if (value.kind === 'object') {
|
|
596
|
+
pending.push(...value.entries.values())
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return { nodes, depth }
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/** Methods whose result holds the schema they are called on as one more level. */
|
|
603
|
+
const WRAPPING_METHODS = new Set(['optional', 'nullable', 'nullish', 'default', 'array', 'or'])
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* The size after one step: a factory is one node above its argument schemas; a wrapping method one node above the
|
|
607
|
+
* schema and its argument; `extend` and friends merge shapes; a check adds no node.
|
|
608
|
+
*/
|
|
609
|
+
const stepSize = (method: string | null, receiver: SchemaSize, parts: SchemaSize): SchemaSize => {
|
|
610
|
+
if (method === null) {
|
|
611
|
+
return { nodes: parts.nodes + 1, depth: parts.depth + 1 }
|
|
612
|
+
}
|
|
613
|
+
if (WRAPPING_METHODS.has(method)) {
|
|
614
|
+
return { nodes: receiver.nodes + parts.nodes + 1, depth: Math.max(receiver.depth, parts.depth) + 1 }
|
|
615
|
+
}
|
|
616
|
+
return { nodes: receiver.nodes + parts.nodes, depth: Math.max(receiver.depth, parts.depth) }
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> => typeof value === 'object' && value !== null
|
|
620
|
+
|
|
621
|
+
const templateCooked = (element: AstNode): string | undefined => {
|
|
622
|
+
const value = element['value']
|
|
623
|
+
return isRecord(value) && typeof value['cooked'] === 'string' ? value['cooked'] : undefined
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
const compile = (pattern: string, flags: string): RegExp | undefined => {
|
|
627
|
+
try {
|
|
628
|
+
return new RegExp(pattern, flags)
|
|
629
|
+
} catch {
|
|
630
|
+
return undefined
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/** The name of a value kind, for a problem that names what was expected. */
|
|
635
|
+
export const describeKind = (
|
|
636
|
+
value: LiteralValue,
|
|
637
|
+
): string =>
|
|
638
|
+
value.kind === 'builder'
|
|
639
|
+
? `${kitName(value.kit)}.${value.name}(…)`
|
|
640
|
+
: value.kind === 'kit-contract'
|
|
641
|
+
? 'app.contract(…)'
|
|
642
|
+
: value.kind === 'attachment'
|
|
643
|
+
? 'attachment(…)'
|
|
644
|
+
: value.kind
|