@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,116 @@
|
|
|
1
|
+
// Reads `src/db/tables.ts` into table policies without executing it: the arguments of
|
|
2
|
+
// `defineTables(vocabulary, columns)({ … })` are read as literals, then parsed by `@fabrika/db`'s own policy parse,
|
|
3
|
+
// which derives each table's containment from the vocabulary as `defineTables` does.
|
|
4
|
+
|
|
5
|
+
import { PolicyError, readTables, type Tables } from '@fabrika/db/tables'
|
|
6
|
+
import { children, identifierName, type ParsedModule } from '../ast'
|
|
7
|
+
import type { Located, ModuleGraph } from '../graph'
|
|
8
|
+
import { TABLES_EXPORT, TABLES_FILE } from '../layout'
|
|
9
|
+
import { BUILDER_IMPORTS, decompose } from '../rules/literal'
|
|
10
|
+
import type { ReaderProblems } from './problems'
|
|
11
|
+
import { type LiteralValue, ValueReader } from './value'
|
|
12
|
+
|
|
13
|
+
const ALLOWED_CALL = `export const ${TABLES_EXPORT} = defineTables(vocabulary, columns)({ … })`
|
|
14
|
+
const ALLOWED_POLICY = 'a table policy defineTables accepts (packages/db/CLAUDE.md)'
|
|
15
|
+
|
|
16
|
+
/** The policies of `src/db/tables.ts`, as `defineTables` returns them; `null` after recording why not. */
|
|
17
|
+
export function readTablesValue(graph: ModuleGraph, module: ParsedModule, problems: ReaderProblems): Tables<unknown, unknown> | null {
|
|
18
|
+
const binding = graph.exported(module, TABLES_EXPORT)
|
|
19
|
+
if (binding.kind !== 'const' || binding.init === undefined) {
|
|
20
|
+
problems.add({ module, node: module.program }, `${TABLES_FILE} exports no const '${TABLES_EXPORT}'`, ALLOWED_CALL)
|
|
21
|
+
return null
|
|
22
|
+
}
|
|
23
|
+
const at: Located = { module: binding.module, node: binding.init }
|
|
24
|
+
const { root, links } = decompose(binding.init)
|
|
25
|
+
const imported = graph.index(binding.module).imports.get(identifierName(root) ?? '')
|
|
26
|
+
const builder = imported === undefined ? undefined : BUILDER_IMPORTS[imported.specifier]?.[imported.imported]
|
|
27
|
+
const [first, second] = links
|
|
28
|
+
const [vocabularyNode, columnsNode] = first?.kind === 'call' ? tableArguments(first.node) : [undefined, undefined]
|
|
29
|
+
const policiesNode = second?.kind === 'call' ? soleArgument(second.node) : undefined
|
|
30
|
+
if (builder !== 'tables' || links.length !== 2 || vocabularyNode === undefined || columnsNode === undefined || policiesNode === undefined) {
|
|
31
|
+
problems.add(at, `'${TABLES_EXPORT}' is not one call of defineTables with the vocabulary and the column list, then the policies`, ALLOWED_CALL)
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
const reader = new ValueReader(graph, problems)
|
|
35
|
+
const vocabulary = reader.value({ module: binding.module, node: vocabularyNode })
|
|
36
|
+
const columns = reader.value({ module: binding.module, node: columnsNode })
|
|
37
|
+
const policies = reader.value({ module: binding.module, node: policiesNode })
|
|
38
|
+
if (vocabulary === null || columns === null || policies === null) {
|
|
39
|
+
return null
|
|
40
|
+
}
|
|
41
|
+
if (!isVocabulary(vocabulary)) {
|
|
42
|
+
problems.add({ module: binding.module, node: vocabularyNode }, 'the first argument of defineTables is not the vocabulary', ALLOWED_CALL)
|
|
43
|
+
return null
|
|
44
|
+
}
|
|
45
|
+
const plainVocabulary = plainOf(vocabulary, problems)
|
|
46
|
+
const plainColumns = plainOf(columns, problems)
|
|
47
|
+
const plainPolicies = plainOf(policies, problems)
|
|
48
|
+
if (plainVocabulary === undefined || plainColumns === undefined || plainPolicies === undefined) {
|
|
49
|
+
return null
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return readTables(plainColumns, plainPolicies, plainVocabulary)
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (!(error instanceof PolicyError)) {
|
|
55
|
+
throw error
|
|
56
|
+
}
|
|
57
|
+
problems.add(at, error.message, ALLOWED_POLICY)
|
|
58
|
+
return null
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** `(vocabulary, columns)`; anything else, the form that names the action union by hand included, is neither. */
|
|
63
|
+
function tableArguments(call: Located['node']): readonly [Located['node'] | undefined, Located['node'] | undefined] {
|
|
64
|
+
const args = children(call, 'arguments')
|
|
65
|
+
const [vocabulary, columns] = args
|
|
66
|
+
const argument = (node: (typeof args)[number] | undefined) => node === null || node === undefined || node.type === 'SpreadElement' ? undefined : node
|
|
67
|
+
return args.length === 2 ? [argument(vocabulary), argument(columns)] : [undefined, undefined]
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** What `defineVocabulary` returns, as the reader has it: an object with the vocabulary's sections. */
|
|
71
|
+
function isVocabulary(value: LiteralValue): boolean {
|
|
72
|
+
return value.kind === 'object' && ['app', 'dimensions', 'actions', 'roles'].every((section) => value.entries.has(section))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function soleArgument(call: Located['node']): Located['node'] | undefined {
|
|
76
|
+
const args = children(call, 'arguments')
|
|
77
|
+
const [only] = args
|
|
78
|
+
return args.length === 1 && only !== null && only !== undefined && only.type !== 'SpreadElement' ? only : undefined
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The value as `defineTables` would receive it at runtime; `undefined` after recording a value it cannot hold. */
|
|
82
|
+
function plainOf(value: LiteralValue, problems: ReaderProblems): unknown {
|
|
83
|
+
switch (value.kind) {
|
|
84
|
+
case 'string':
|
|
85
|
+
case 'number':
|
|
86
|
+
case 'boolean':
|
|
87
|
+
return value.value
|
|
88
|
+
case 'null':
|
|
89
|
+
return null
|
|
90
|
+
case 'array': {
|
|
91
|
+
const items: unknown[] = []
|
|
92
|
+
for (const item of value.items) {
|
|
93
|
+
const plain = plainOf(item, problems)
|
|
94
|
+
if (plain === undefined) {
|
|
95
|
+
return undefined
|
|
96
|
+
}
|
|
97
|
+
items.push(plain)
|
|
98
|
+
}
|
|
99
|
+
return items
|
|
100
|
+
}
|
|
101
|
+
case 'object': {
|
|
102
|
+
const entries: [string, unknown][] = []
|
|
103
|
+
for (const [key, entry] of value.entries) {
|
|
104
|
+
const plain = plainOf(entry, problems)
|
|
105
|
+
if (plain === undefined) {
|
|
106
|
+
return undefined
|
|
107
|
+
}
|
|
108
|
+
entries.push([key, plain])
|
|
109
|
+
}
|
|
110
|
+
return Object.fromEntries(entries)
|
|
111
|
+
}
|
|
112
|
+
default:
|
|
113
|
+
problems.add(value.at, `a ${value.kind} in a table policy`, 'strings, arrays and objects of them')
|
|
114
|
+
return undefined
|
|
115
|
+
}
|
|
116
|
+
}
|