@fabrika/check 0.9.0-dev.26 → 0.9.0-dev.27
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 +6 -6
- package/src/contract.ts +2 -2
- package/src/graph.ts +49 -12
- package/src/reader/value.ts +23 -2
- package/src/rules/contract.ts +3 -2
- package/src/rules/identity.ts +6 -9
- package/src/rules/literal.ts +39 -8
- package/types/src/contract.d.ts +1 -1
- package/types/src/graph.d.ts +12 -2
- package/types/src/reader/value.d.ts +1 -1
- package/types/src/rules/literal.d.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/check",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.27",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -52,11 +52,11 @@
|
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"@babel/parser": "8.0.7",
|
|
55
|
-
"@fabrika/app": "0.9.0-dev.
|
|
56
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
57
|
-
"@fabrika/db": "0.9.0-dev.
|
|
58
|
-
"@fabrika/operations-contract": "0.9.0-dev.
|
|
59
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
55
|
+
"@fabrika/app": "0.9.0-dev.27",
|
|
56
|
+
"@fabrika/auth-core": "0.9.0-dev.27",
|
|
57
|
+
"@fabrika/db": "0.9.0-dev.27",
|
|
58
|
+
"@fabrika/operations-contract": "0.9.0-dev.27",
|
|
59
|
+
"@fabrika/platform-node": "0.9.0-dev.27",
|
|
60
60
|
"zod": "^3.25.76"
|
|
61
61
|
}
|
|
62
62
|
}
|
package/src/contract.ts
CHANGED
|
@@ -142,7 +142,7 @@ export function readContractModule(graph: ModuleGraph): ContractRead {
|
|
|
142
142
|
return contract === null || problems.findings.length > 0 ? { ok: false, findings: problems.findings } : { ok: true, contract }
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
/** The contract module and every module its value imports reach. */
|
|
145
|
+
/** The contract module and every module its value imports and re-exports reach. */
|
|
146
146
|
export function contractModules(graph: ModuleGraph, entry: ParsedModule): ParsedModule[] {
|
|
147
147
|
const reached = new Map<string, ParsedModule>()
|
|
148
148
|
const queue = [entry]
|
|
@@ -151,7 +151,7 @@ export function contractModules(graph: ModuleGraph, entry: ParsedModule): Parsed
|
|
|
151
151
|
continue
|
|
152
152
|
}
|
|
153
153
|
reached.set(module.file, module)
|
|
154
|
-
for (const imported of graph.
|
|
154
|
+
for (const imported of graph.links(module)) {
|
|
155
155
|
const target = graph.resolve(module.file, imported.specifier)
|
|
156
156
|
if (target !== undefined) {
|
|
157
157
|
queue.push(target)
|
package/src/graph.ts
CHANGED
|
@@ -41,6 +41,8 @@ interface ModuleIndex {
|
|
|
41
41
|
readonly imports: ReadonlyMap<string, ImportBinding>
|
|
42
42
|
/** Exported name → the local name it exports, or the default export's expression. */
|
|
43
43
|
readonly exports: ReadonlyMap<string, string | AstNode>
|
|
44
|
+
/** `export { x as y } from './m'`: the exported name (`local`) → the module and the name it takes from it. */
|
|
45
|
+
readonly reexports: ReadonlyMap<string, ImportBinding>
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
const importedName = (specifier: AstNode): string => {
|
|
@@ -55,6 +57,7 @@ const indexModule = (module: ParsedModule): ModuleIndex => {
|
|
|
55
57
|
const others = new Set<string>()
|
|
56
58
|
const imports = new Map<string, ImportBinding>()
|
|
57
59
|
const exports = new Map<string, string | AstNode>()
|
|
60
|
+
const reexports = new Map<string, ImportBinding>()
|
|
58
61
|
for (const statement of children(module.program, 'body')) {
|
|
59
62
|
if (statement === null) continue
|
|
60
63
|
if (statement.type === 'ImportDeclaration') {
|
|
@@ -73,13 +76,17 @@ const indexModule = (module: ParsedModule): ModuleIndex => {
|
|
|
73
76
|
continue
|
|
74
77
|
}
|
|
75
78
|
const isExport = statement.type === 'ExportNamedDeclaration'
|
|
76
|
-
|
|
79
|
+
const source = isExport ? staticString(child(statement, 'source')) : undefined
|
|
80
|
+
if (isExport && stringField(statement, 'exportKind') !== 'type') {
|
|
77
81
|
for (const item of children(statement, 'specifiers')) {
|
|
78
82
|
if (item === null || stringField(item, 'exportKind') === 'type') continue
|
|
79
|
-
const
|
|
83
|
+
const localNode = child(item, 'local')
|
|
84
|
+
const local = identifierName(localNode) ?? staticString(localNode)
|
|
80
85
|
const exported = child(item, 'exported')
|
|
81
86
|
const name = identifierName(exported) ?? staticString(exported)
|
|
82
|
-
if (local
|
|
87
|
+
if (local === undefined || name === undefined) continue
|
|
88
|
+
if (source === undefined) exports.set(name, local)
|
|
89
|
+
else reexports.set(name, { local: name, specifier: source, imported: local, node: item })
|
|
83
90
|
}
|
|
84
91
|
}
|
|
85
92
|
const declaration = isExport ? child(statement, 'declaration') : statement
|
|
@@ -103,7 +110,7 @@ const indexModule = (module: ParsedModule): ModuleIndex => {
|
|
|
103
110
|
}
|
|
104
111
|
if (isExport) { for (const name of declared) exports.set(name, name) }
|
|
105
112
|
}
|
|
106
|
-
return { consts, others, imports, exports }
|
|
113
|
+
return { consts, others, imports, exports, reexports }
|
|
107
114
|
}
|
|
108
115
|
|
|
109
116
|
/** Erased wrappers: `x satisfies T`, `x as const`, parentheses. */
|
|
@@ -143,6 +150,12 @@ export class ModuleGraph {
|
|
|
143
150
|
return resolveRelative((path) => this.#modules.has(path), from, specifier)
|
|
144
151
|
}
|
|
145
152
|
|
|
153
|
+
/** What a module loads by name: its value imports and its re-exports. */
|
|
154
|
+
links(module: ParsedModule): readonly ImportBinding[] {
|
|
155
|
+
const index = this.index(module)
|
|
156
|
+
return [...index.imports.values(), ...index.reexports.values()]
|
|
157
|
+
}
|
|
158
|
+
|
|
146
159
|
/** The checked module a relative specifier names; none when two candidates compete, the index of a directory import. */
|
|
147
160
|
resolve(from: string, specifier: string): ParsedModule | undefined {
|
|
148
161
|
if (!isRelativeSpecifier(specifier)) return undefined
|
|
@@ -169,7 +182,17 @@ export class ModuleGraph {
|
|
|
169
182
|
|
|
170
183
|
/** What an exported name of `module` denotes. */
|
|
171
184
|
exported(module: ParsedModule, name: string, seen: ReadonlySet<string> = new Set()): Binding {
|
|
172
|
-
const
|
|
185
|
+
const index = this.index(module)
|
|
186
|
+
const reexported = index.reexports.get(name)
|
|
187
|
+
if (reexported !== undefined) {
|
|
188
|
+
const key = `${module.file}#export ${name}`
|
|
189
|
+
if (seen.has(key)) return { kind: 'unresolved', reason: `'${name}' is re-exported in a cycle` }
|
|
190
|
+
if (!isRelativeSpecifier(reexported.specifier)) return { kind: 'package', specifier: reexported.specifier, imported: reexported.imported }
|
|
191
|
+
const target = this.resolve(module.file, reexported.specifier)
|
|
192
|
+
if (target === undefined) return { kind: 'unresolved', reason: `unresolved module '${reexported.specifier}'` }
|
|
193
|
+
return this.exported(target, reexported.imported, new Set([...seen, key]))
|
|
194
|
+
}
|
|
195
|
+
const exported = index.exports.get(name)
|
|
173
196
|
if (exported === undefined) return { kind: 'unresolved', reason: `'${module.file}' does not export '${name}'` }
|
|
174
197
|
if (typeof exported !== 'string') return { kind: 'default', module, expression: exported }
|
|
175
198
|
return this.binding(module, exported, seen)
|
|
@@ -194,13 +217,27 @@ export class ModuleGraph {
|
|
|
194
217
|
}
|
|
195
218
|
}
|
|
196
219
|
|
|
197
|
-
/**
|
|
198
|
-
|
|
199
|
-
|
|
220
|
+
/**
|
|
221
|
+
* The entries of an object literal by key, in the order the object holds them, as evaluating it would build them: a
|
|
222
|
+
* spread of a value that resolves to an object literal contributes its entries, and a later key overrides an earlier
|
|
223
|
+
* one. A computed key, a method and a spread of anything else are not followed.
|
|
224
|
+
*/
|
|
225
|
+
export const objectEntries = (graph: ModuleGraph, located: Located, depth = 0): ReadonlyMap<string, Located> => {
|
|
226
|
+
const entries = new Map<string, Located>()
|
|
227
|
+
if (located.node.type !== 'ObjectExpression' || depth > 32) return entries
|
|
200
228
|
for (const property of children(located.node, 'properties')) {
|
|
201
|
-
if (property?.type
|
|
202
|
-
|
|
203
|
-
|
|
229
|
+
if (property?.type === 'SpreadElement') {
|
|
230
|
+
const argument = child(property, 'argument')
|
|
231
|
+
const spread = argument === undefined ? undefined : graph.resolveValue({ module: located.module, node: argument })
|
|
232
|
+
if (spread !== undefined) { for (const [key, value] of objectEntries(graph, spread, depth + 1)) entries.set(key, value) }
|
|
233
|
+
continue
|
|
234
|
+
}
|
|
235
|
+
const key = property?.type === 'Property' && property['computed'] !== true ? staticKey(property, 'key') : undefined
|
|
236
|
+
const value = property === null || key === undefined ? undefined : child(property, 'value')
|
|
237
|
+
if (key !== undefined && value !== undefined) entries.set(key, { module: located.module, node: value })
|
|
204
238
|
}
|
|
205
|
-
return
|
|
239
|
+
return entries
|
|
206
240
|
}
|
|
241
|
+
|
|
242
|
+
/** The value of `key` in an object literal, read statically through `objectEntries`. */
|
|
243
|
+
export const propertyValue = (graph: ModuleGraph, located: Located, key: string): Located | undefined => objectEntries(graph, located).get(key)
|
package/src/reader/value.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { file, signedToken, WebhookSecret } from '@fabrika/app/contract'
|
|
5
5
|
import type { z } from 'zod/v4'
|
|
6
6
|
import { type AstNode, child, children, identifierName, isConstAssertion, type ParsedModule, staticKey } from '../ast'
|
|
7
|
-
import type
|
|
7
|
+
import { type Located, type ModuleGraph, unwrapValue } from '../graph'
|
|
8
8
|
import { CONTRACT_LIMITS } from '../limits'
|
|
9
9
|
import {
|
|
10
10
|
BUILDER_IMPORTS,
|
|
@@ -98,6 +98,7 @@ const VOCABULARY_SECTIONS = [
|
|
|
98
98
|
] as const
|
|
99
99
|
|
|
100
100
|
const ALLOWED_VALUE = 'object and array literals, strings, numbers, booleans, null, the c.* and z.* builders, and const imports of such values'
|
|
101
|
+
const ALLOWED_SPREAD = "a spread of a const whose value is an object literal (this module's or a named import), or the entries written out"
|
|
101
102
|
|
|
102
103
|
export class ValueReader {
|
|
103
104
|
readonly #graph: ModuleGraph
|
|
@@ -256,13 +257,19 @@ export class ValueReader {
|
|
|
256
257
|
continue
|
|
257
258
|
}
|
|
258
259
|
const at = { module: located.module, node: property }
|
|
260
|
+
if (property.type === 'SpreadElement') {
|
|
261
|
+
const spread = this.#spread(at, scope)
|
|
262
|
+
if (spread === null) complete = false
|
|
263
|
+
else for (const [key, value] of spread) entries.set(key, value)
|
|
264
|
+
continue
|
|
265
|
+
}
|
|
259
266
|
const key = staticKey(property, 'key')
|
|
260
267
|
const value = child(property, 'value')
|
|
261
268
|
if (
|
|
262
269
|
property.type !== 'Property' || property['computed'] === true || property['kind'] !== 'init' || property['method'] === true
|
|
263
270
|
|| key === undefined || key === '__proto__' || value === undefined
|
|
264
271
|
) {
|
|
265
|
-
this.#problems.add(at, 'a
|
|
272
|
+
this.#problems.add(at, 'a computed key, method or __proto__ key', 'a plain or quoted key with a literal value')
|
|
266
273
|
complete = false
|
|
267
274
|
continue
|
|
268
275
|
}
|
|
@@ -277,6 +284,20 @@ export class ValueReader {
|
|
|
277
284
|
return complete ? { kind: 'object', entries, at: located } : null
|
|
278
285
|
}
|
|
279
286
|
|
|
287
|
+
/** The entries `{ ...x }` copies: those of an object literal, in place or a const's (`rules/literal.ts`' spread). */
|
|
288
|
+
#spread(at: Located, scope: Scope): ReadonlyMap<string, LiteralValue> | null {
|
|
289
|
+
const argument = child(at.node, 'argument')
|
|
290
|
+
const value = argument === undefined ? undefined : unwrapValue(argument)
|
|
291
|
+
const target = value === undefined ? undefined : this.#graph.resolveValue({ module: at.module, node: value })
|
|
292
|
+
const read = value === undefined || target?.node.type !== 'ObjectExpression'
|
|
293
|
+
? undefined
|
|
294
|
+
: this.#read({ module: at.module, node: value }, scope, null)
|
|
295
|
+
if (read === null) return null
|
|
296
|
+
if (read?.kind === 'object') return read.entries
|
|
297
|
+
this.#problems.add(at, 'spread of a value that is not a constant object literal', ALLOWED_SPREAD)
|
|
298
|
+
return null
|
|
299
|
+
}
|
|
300
|
+
|
|
280
301
|
#identifier(located: Located, scope: Scope): LiteralValue | null {
|
|
281
302
|
const name = identifierName(located.node) ?? ''
|
|
282
303
|
const builder = this.#builderOf(located.module, name)
|
package/src/rules/contract.ts
CHANGED
|
@@ -20,8 +20,9 @@ export const checkContractRead = (graph: ModuleGraph): Finding[] => {
|
|
|
20
20
|
return [...read.findings]
|
|
21
21
|
}
|
|
22
22
|
const module = graph.module(CONTRACT_FILE)
|
|
23
|
-
const binding = module === undefined ? undefined : graph.
|
|
24
|
-
|
|
23
|
+
const binding = module === undefined ? undefined : graph.exported(module, CONTRACT_EXPORT)
|
|
24
|
+
// A contract declared in another module (an import or a re-export) is reported at this file's start.
|
|
25
|
+
const at = binding?.kind === 'const' && binding.module === module && binding.init !== undefined ? positionOf(binding.init) : { line: 1, column: 1 }
|
|
25
26
|
try {
|
|
26
27
|
return memberAdmitsExternal(describeContract(read.contract), at)
|
|
27
28
|
} catch (error) {
|
package/src/rules/identity.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type AstNode, child, children, type ParsedModule, staticKey, walk } from '../ast'
|
|
2
2
|
import { type Finding, reporter } from '../finding'
|
|
3
|
-
import { type Located, type ModuleGraph, propertyValue } from '../graph'
|
|
3
|
+
import { type Located, type ModuleGraph, objectEntries, propertyValue } from '../graph'
|
|
4
4
|
|
|
5
5
|
const IDENTITY_FIELDS = new Set([
|
|
6
6
|
'userId',
|
|
@@ -35,10 +35,7 @@ const schemaFields = (located: Located, graph: ModuleGraph, depth = 0): Readonly
|
|
|
35
35
|
if (method === undefined || !argument) return fields
|
|
36
36
|
if (SHAPE_METHODS.has(method)) {
|
|
37
37
|
const shape = graph.resolveValue({ module: value.module, node: argument })
|
|
38
|
-
for (const
|
|
39
|
-
const key = property === null ? undefined : staticKey(property, 'key')
|
|
40
|
-
if (key !== undefined) fields.add(key)
|
|
41
|
-
}
|
|
38
|
+
for (const key of shape === undefined ? [] : objectEntries(graph, shape).keys()) fields.add(key)
|
|
42
39
|
} else if (MERGE_METHODS.has(method)) add({ module: value.module, node: argument })
|
|
43
40
|
return fields
|
|
44
41
|
}
|
|
@@ -46,8 +43,8 @@ const schemaFields = (located: Located, graph: ModuleGraph, depth = 0): Readonly
|
|
|
46
43
|
/** The field an `in`/`on` access form takes from the input; the framework removes it and checks the caller's access to it. */
|
|
47
44
|
const checkedField = (access: Located | undefined, graph: ModuleGraph): string | undefined => {
|
|
48
45
|
const value = access === undefined ? undefined : graph.resolveValue(access)
|
|
49
|
-
if (value === undefined || (propertyValue(value, 'in') === undefined && propertyValue(value, 'on') === undefined)) return undefined
|
|
50
|
-
const from = propertyValue(value, 'from')
|
|
46
|
+
if (value === undefined || (propertyValue(graph, value, 'in') === undefined && propertyValue(graph, value, 'on') === undefined)) return undefined
|
|
47
|
+
const from = propertyValue(graph, value, 'from')
|
|
51
48
|
return from === undefined ? undefined : graph.resolveString(from)
|
|
52
49
|
}
|
|
53
50
|
|
|
@@ -61,9 +58,9 @@ export const checkClientIdentity = (contract: ParsedModule, graph: ModuleGraph):
|
|
|
61
58
|
for (const argument of children(node, 'arguments')) {
|
|
62
59
|
if (argument === null) continue
|
|
63
60
|
const options = graph.resolveValue({ module: contract, node: argument })
|
|
64
|
-
const input = options === undefined ? undefined : propertyValue(options, 'input')
|
|
61
|
+
const input = options === undefined ? undefined : propertyValue(graph, options, 'input')
|
|
65
62
|
if (options === undefined || input === undefined) continue
|
|
66
|
-
const checked = checkedField(propertyValue(options, 'access'), graph)
|
|
63
|
+
const checked = checkedField(propertyValue(graph, options, 'access'), graph)
|
|
67
64
|
const at: AstNode = input.module === contract ? input.node : argument
|
|
68
65
|
for (const field of schemaFields(input, graph)) {
|
|
69
66
|
if (!IDENTITY_FIELDS.has(field) || field === checked) continue
|
package/src/rules/literal.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type AstNode, child, children, identifierName, isConstAssertion, type ParsedModule, staticKey, staticString, stringField } from '../ast'
|
|
2
2
|
import { type Finding, type Report, reporter } from '../finding'
|
|
3
|
-
import type
|
|
3
|
+
import { type Binding, type ModuleGraph, unwrapValue } from '../graph'
|
|
4
4
|
import { isRelativeSpecifier } from '../layout'
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -165,6 +165,7 @@ const ALLOWED_VALUE = 'object and array literals, strings, numbers, booleans, nu
|
|
|
165
165
|
const ALLOWED_SCHEMA = 'a schema of the supported z subset (packages/check/CLAUDE.md): strict objects, no transforms, refinements or pipes'
|
|
166
166
|
const ALLOWED_CALLBACK =
|
|
167
167
|
'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(…)'
|
|
168
|
+
const ALLOWED_SPREAD = "a spread of a const whose value is an object literal (this module's or a named import), or the entries written out"
|
|
168
169
|
const ALLOWED_IMPORT =
|
|
169
170
|
'z from zod/v4 or @fabrika/app/schema, defineContract, defineVocabulary or defineApp from @fabrika/app/contract, defineTables from @fabrika/db/tables, or a literal const of the application'
|
|
170
171
|
|
|
@@ -527,7 +528,7 @@ const readModule = (module: ParsedModule, graph: ModuleGraph, findings: Finding[
|
|
|
527
528
|
for (const property of children(node, 'properties')) {
|
|
528
529
|
if (property === null) continue
|
|
529
530
|
if (property.type === 'SpreadElement') {
|
|
530
|
-
|
|
531
|
+
checkObjectSpread(property, scope, dependencies)
|
|
531
532
|
continue
|
|
532
533
|
}
|
|
533
534
|
if (property['computed'] === true) {
|
|
@@ -549,6 +550,23 @@ const readModule = (module: ParsedModule, graph: ModuleGraph, findings: Finding[
|
|
|
549
550
|
}
|
|
550
551
|
}
|
|
551
552
|
|
|
553
|
+
/**
|
|
554
|
+
* `{ ...Base }`: a spread of an object literal, written in place or as a const of this module or an imported one (through
|
|
555
|
+
* `as const`, `satisfies` and parentheses), whose entries the reader copies as evaluating it would.
|
|
556
|
+
*/
|
|
557
|
+
const checkObjectSpread = (spread: AstNode, scope: Scope, dependencies: string[]): void => {
|
|
558
|
+
const argument = child(spread, 'argument')
|
|
559
|
+
const value = argument === undefined ? undefined : unwrapValue(argument)
|
|
560
|
+
if (value?.type === 'ObjectExpression') return checkObject(value, scope, dependencies)
|
|
561
|
+
const name = identifierName(value)
|
|
562
|
+
if (value === undefined || name === undefined) return fail(spread, 'spread of a value that is not a constant object', ALLOWED_SPREAD)
|
|
563
|
+
const before = reported
|
|
564
|
+
checkIdentifier(value, scope, dependencies)
|
|
565
|
+
if (reported !== before) return
|
|
566
|
+
const resolved = graph.resolveValue({ module, node: value })
|
|
567
|
+
if (resolved?.node.type !== 'ObjectExpression') fail(spread, `spread of '${name}', which is not a constant object literal`, ALLOWED_SPREAD)
|
|
568
|
+
}
|
|
569
|
+
|
|
552
570
|
const checkExpression = (node: AstNode, scope: Scope, dependencies: string[]): void => {
|
|
553
571
|
switch (node.type) {
|
|
554
572
|
case 'Literal':
|
|
@@ -617,8 +635,17 @@ const readModule = (module: ParsedModule, graph: ModuleGraph, findings: Finding[
|
|
|
617
635
|
const declaration = statement.type === 'ExportNamedDeclaration' ? (child(statement, 'declaration') ?? statement) : statement
|
|
618
636
|
if (declaration.type === 'TSTypeAliasDeclaration' || declaration.type === 'TSInterfaceDeclaration') continue
|
|
619
637
|
if (declaration.type === 'ExportNamedDeclaration') {
|
|
620
|
-
|
|
621
|
-
|
|
638
|
+
// A relative re-export is checked with the imports (`checkLiteralGraph`), as its binding. Bun erases one whose
|
|
639
|
+
// every specifier is a type, and evaluates the source of `export {} from`, as of `import {} from`.
|
|
640
|
+
const source = staticString(child(declaration, 'source'))
|
|
641
|
+
if (source !== undefined) {
|
|
642
|
+
const specifiers = children(declaration, 'specifiers').filter((item): item is AstNode => item !== null)
|
|
643
|
+
const erased = stringField(declaration, 'exportKind') === 'type'
|
|
644
|
+
|| (specifiers.length > 0 && specifiers.every((item) => stringField(item, 'exportKind') === 'type'))
|
|
645
|
+
if (!erased && specifiers.length === 0) fail(declaration, `side-effect re-export of '${source}'`, 'named imports of literal consts')
|
|
646
|
+
else if (!erased && !isRelativeSpecifier(source)) {
|
|
647
|
+
fail(declaration, `re-export from the package '${source}'`, 'import the builder in the module that uses it')
|
|
648
|
+
}
|
|
622
649
|
continue
|
|
623
650
|
}
|
|
624
651
|
for (const item of children(declaration, 'specifiers')) {
|
|
@@ -665,7 +692,7 @@ export const checkLiteralGraph = (entries: readonly string[], graph: ModuleGraph
|
|
|
665
692
|
for (let module = queue.shift(); module !== undefined; module = queue.shift()) {
|
|
666
693
|
if (summaries.has(module.file)) continue
|
|
667
694
|
summaries.set(module.file, readModule(module, graph, findings))
|
|
668
|
-
for (const binding of graph.
|
|
695
|
+
for (const binding of graph.links(module)) {
|
|
669
696
|
const target = graph.resolve(module.file, binding.specifier)
|
|
670
697
|
if (target !== undefined) queue.push(target)
|
|
671
698
|
}
|
|
@@ -709,7 +736,11 @@ export const checkLiteralGraph = (entries: readonly string[], graph: ModuleGraph
|
|
|
709
736
|
for (const name of summary.consts.keys()) {
|
|
710
737
|
isLiteral(name === 'default' ? graph.exported(summary.module, 'default') : graph.binding(summary.module, name))
|
|
711
738
|
}
|
|
712
|
-
|
|
739
|
+
const index = graph.index(summary.module)
|
|
740
|
+
for (const imported of graph.links(summary.module)) {
|
|
741
|
+
const reexport = index.reexports.get(imported.local) === imported
|
|
742
|
+
// A package re-export is refused at its declaration (`readModule`).
|
|
743
|
+
if (reexport && !isRelativeSpecifier(imported.specifier)) continue
|
|
713
744
|
if (!isRelativeSpecifier(imported.specifier)) {
|
|
714
745
|
const browserSafe = SERVER_ENTRY_OF[imported.specifier]
|
|
715
746
|
if (browserSafe !== undefined && BUILDER_IMPORTS[browserSafe]?.[imported.imported] !== undefined) {
|
|
@@ -741,7 +772,7 @@ export const checkLiteralGraph = (entries: readonly string[], graph: ModuleGraph
|
|
|
741
772
|
)
|
|
742
773
|
continue
|
|
743
774
|
}
|
|
744
|
-
const binding = graph.binding(summary.module, imported.local)
|
|
775
|
+
const binding = reexport ? graph.exported(summary.module, imported.local) : graph.binding(summary.module, imported.local)
|
|
745
776
|
if (binding.kind === 'unresolved') summary.report(imported.node, 'literal', binding.reason, 'a named import of a const the module exports')
|
|
746
777
|
else if (!isLiteral(binding) && !refusedInPlace(binding)) {
|
|
747
778
|
summary.report(
|
|
@@ -761,7 +792,7 @@ const reportImportCycles = (summaries: ReadonlyMap<string, LiteralSummary>, grap
|
|
|
761
792
|
const state = new Map<string, 'visiting' | 'done'>()
|
|
762
793
|
const visit = (summary: LiteralSummary): void => {
|
|
763
794
|
state.set(summary.module.file, 'visiting')
|
|
764
|
-
for (const imported of graph.
|
|
795
|
+
for (const imported of graph.links(summary.module)) {
|
|
765
796
|
const target = graph.resolve(summary.module.file, imported.specifier)
|
|
766
797
|
const next = target === undefined ? undefined : summaries.get(target.file)
|
|
767
798
|
if (next === undefined) continue
|
package/types/src/contract.d.ts
CHANGED
|
@@ -55,5 +55,5 @@ export type ContractRead = {
|
|
|
55
55
|
};
|
|
56
56
|
/** Reads the `contract` export of a graph whose contract modules already pass the literal rule. */
|
|
57
57
|
export declare function readContractModule(graph: ModuleGraph): ContractRead;
|
|
58
|
-
/** The contract module and every module its value imports reach. */
|
|
58
|
+
/** The contract module and every module its value imports and re-exports reach. */
|
|
59
59
|
export declare function contractModules(graph: ModuleGraph, entry: ParsedModule): ParsedModule[];
|
package/types/src/graph.d.ts
CHANGED
|
@@ -41,6 +41,8 @@ interface ModuleIndex {
|
|
|
41
41
|
readonly imports: ReadonlyMap<string, ImportBinding>;
|
|
42
42
|
/** Exported name → the local name it exports, or the default export's expression. */
|
|
43
43
|
readonly exports: ReadonlyMap<string, string | AstNode>;
|
|
44
|
+
/** `export { x as y } from './m'`: the exported name (`local`) → the module and the name it takes from it. */
|
|
45
|
+
readonly reexports: ReadonlyMap<string, ImportBinding>;
|
|
44
46
|
}
|
|
45
47
|
/** Erased wrappers: `x satisfies T`, `x as const`, parentheses. */
|
|
46
48
|
export declare const unwrapValue: (node: AstNode) => AstNode;
|
|
@@ -52,6 +54,8 @@ export declare class ModuleGraph {
|
|
|
52
54
|
index(module: ParsedModule): ModuleIndex;
|
|
53
55
|
/** How a relative specifier resolves among the checked modules. */
|
|
54
56
|
resolution(from: string, specifier: string): Resolution;
|
|
57
|
+
/** What a module loads by name: its value imports and its re-exports. */
|
|
58
|
+
links(module: ParsedModule): readonly ImportBinding[];
|
|
55
59
|
/** The checked module a relative specifier names; none when two candidates compete, the index of a directory import. */
|
|
56
60
|
resolve(from: string, specifier: string): ParsedModule | undefined;
|
|
57
61
|
/** What `name` denotes in `module`; an import is followed to the exporting module's declaration. */
|
|
@@ -63,6 +67,12 @@ export declare class ModuleGraph {
|
|
|
63
67
|
/** The fixed string a value resolves to. */
|
|
64
68
|
resolveString(located: Located): string | undefined;
|
|
65
69
|
}
|
|
66
|
-
/**
|
|
67
|
-
|
|
70
|
+
/**
|
|
71
|
+
* The entries of an object literal by key, in the order the object holds them, as evaluating it would build them: a
|
|
72
|
+
* spread of a value that resolves to an object literal contributes its entries, and a later key overrides an earlier
|
|
73
|
+
* one. A computed key, a method and a spread of anything else are not followed.
|
|
74
|
+
*/
|
|
75
|
+
export declare const objectEntries: (graph: ModuleGraph, located: Located, depth?: number) => ReadonlyMap<string, Located>;
|
|
76
|
+
/** The value of `key` in an object literal, read statically through `objectEntries`. */
|
|
77
|
+
export declare const propertyValue: (graph: ModuleGraph, located: Located, key: string) => Located | undefined;
|
|
68
78
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type AstNode, type ParsedModule } from '../ast';
|
|
2
|
-
import type
|
|
2
|
+
import { type Located, type ModuleGraph } from '../graph';
|
|
3
3
|
import type { ReaderProblems } from './problems';
|
|
4
4
|
import { type ReadSchema } from './schema';
|
|
5
5
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type AstNode, type ParsedModule } from '../ast';
|
|
2
2
|
import { type Finding } from '../finding';
|
|
3
|
-
import type
|
|
3
|
+
import { type ModuleGraph } from '../graph';
|
|
4
4
|
/**
|
|
5
5
|
* What a call root builds. `surface` is the parameter of a `defineContract` callback (`c`, `l`); `app` is
|
|
6
6
|
* `defineApp`, `kit` a const it returned, `shell` the parameter of `app.contract`'s callback and `endpoints` that of
|