MCPcopy Create free account
hub / github.com/arktypeio/arktype / checkConfigOrder

Method checkConfigOrder

ark/aot/precompile.ts:7474–7527  ·  view source on GitHub ↗
(module: SourceModule)

Source from the content-addressed store, hash-verified

7472
7473 // an import loading a module that creates a Type before the config module
7474 private checkConfigOrder(module: SourceModule): Diagnostic[] {
7475 const config = this.config
7476 if (
7477 config === undefined ||
7478 module.id === config ||
7479 !this.importClosureOf(module.id).includes(config)
7480 )
7481 return []
7482 const loaded = new Set([module.id])
7483 // each module an import loads, in the order the program runs them
7484 const load = (id: string, order: string[]) => {
7485 if (loaded.has(id)) return
7486 loaded.add(id)
7487 for (const [target, isErased] of this.importsOf(id))
7488 if (!isErased) load(target, order)
7489 order.push(id)
7490 }
7491 for (const statement of module.program.body) {
7492 const specifier = "source" in statement && statement.source?.value
7493 if (!specifier || isTypeOnly(statement)) continue
7494 const target = this.graph.host.resolve(specifier, module.id)
7495 if (!target || !this.graph.isFollowed(target)) continue
7496 const order: string[] = []
7497 load(target, order)
7498 const configured = order.indexOf(config)
7499 // what the config module loads itself is reported there
7500 const creating = order
7501 .slice(0, configured === -1 ? undefined : configured)
7502 .find(
7503 id =>
7504 !this.importClosureOf(config).includes(id) &&
7505 this.createsTypeAtLoad(id)
7506 )
7507 if (creating) {
7508 return [
7509 this.diagnostic(
7510 module,
7511 statement,
7512 "R3",
7513 `${module.snippetOf(statement)} loads ${this.graph.pathOf(creating)}, which creates a Type, before the config module ${this.graph.pathOf(config)} runs configure()`,
7514 [
7515 `unless the program loaded ${this.graph.pathOf(config)} earlier, creating the Type throws`
7516 ],
7517 this.unsafeHintOf(
7518 `Import ${this.graph.pathOf(config)} first, before any module that creates a Type.`
7519 ),
7520 this.unsafeSeverity
7521 )
7522 ]
7523 }
7524 if (configured !== -1) return []
7525 }
7526 return []
7527 }
7528
7529 private createsTypeAtLoad(id: string): boolean {
7530 const module = this.graph.moduleOf(id)

Callers 2

analyzeSitesMethod · 0.95
precompileUnscannedMethod · 0.95

Calls 11

importClosureOfMethod · 0.95
createsTypeAtLoadMethod · 0.95
diagnosticMethod · 0.95
unsafeHintOfMethod · 0.95
isTypeOnlyFunction · 0.90
isFollowedMethod · 0.80
findMethod · 0.80
snippetOfMethod · 0.80
resolveMethod · 0.65
loadFunction · 0.50
pathOfMethod · 0.45

Tested by

no test coverage detected