@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.
@@ -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
+ }