@kubb/ast 5.28.6 → 5.29.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/README.md +19 -10
- package/dist/factory.cjs +6 -6
- package/dist/factory.d.cts +2 -2
- package/dist/factory.d.ts +2 -2
- package/dist/factory.js +2 -2
- package/dist/{combineFileMembers-B-mdjXrG.js → file-AcKQt60j.js} +137 -123
- package/dist/file-AcKQt60j.js.map +1 -0
- package/dist/{file-DJqd2j3y.d.cts → file-CLh8vdcR.d.cts} +20 -29
- package/dist/{file-lssk85C8.cjs → file-DVw2Htda.cjs} +13 -13
- package/dist/{file-lssk85C8.cjs.map → file-DVw2Htda.cjs.map} +1 -1
- package/dist/{combineFileMembers-DZCKxAN0.cjs → file-Db_PYobC.cjs} +136 -122
- package/dist/file-Db_PYobC.cjs.map +1 -0
- package/dist/{file-Dg70hXm3.d.ts → file-VRwu7Set.d.ts} +20 -29
- package/dist/{file-CO9KnOvT.js → file-pjdyWLJ0.js} +2 -2
- package/dist/{file-CO9KnOvT.js.map → file-pjdyWLJ0.js.map} +1 -1
- package/dist/file.cjs +6 -6
- package/dist/file.d.cts +1 -1
- package/dist/file.d.ts +1 -1
- package/dist/file.js +2 -2
- package/dist/{graph-DWlCmvm6.js → graph-Cu2-NiKi.js} +78 -15
- package/dist/graph-Cu2-NiKi.js.map +1 -0
- package/dist/{graph-D6Ffy9YD.cjs → graph-CymJtC1M.cjs} +78 -15
- package/dist/graph-CymJtC1M.cjs.map +1 -0
- package/dist/graph.cjs +1 -1
- package/dist/graph.d.cts +1 -1
- package/dist/graph.d.ts +1 -1
- package/dist/graph.js +1 -1
- package/dist/{index-BGhhO3z8.d.ts → index-B4DY8ewx.d.ts} +2 -2
- package/dist/{index-DjwCRndP.d.ts → index-BEvWuiSq.d.ts} +1 -1
- package/dist/{index-DphKh9u9.d.ts → index-BXHrCLGe.d.ts} +1 -1
- package/dist/{index-C4ZZnf_X.d.cts → index-BgmSdJKJ.d.cts} +2 -2
- package/dist/{index-CKd3ogUZ.d.cts → index-C0YF5kEb.d.cts} +2 -2
- package/dist/{index-Ck6nqnxZ.d.cts → index-CGvAotBX.d.cts} +2 -2
- package/dist/{index-DVX6E3vZ.d.ts → index-CHilsNw-.d.ts} +2 -2
- package/dist/{index-C0_Y9yA1.d.ts → index-CsySa8TT.d.ts} +2 -2
- package/dist/{index-BM3jzSca.d.ts → index-DA-Ztg07.d.ts} +1 -1
- package/dist/{index-CiVsdeOh.d.cts → index-DPGecgel.d.cts} +1 -1
- package/dist/{index-DdD4PIY8.d.cts → index-Dbtc7izw.d.cts} +1 -1
- package/dist/{index-DSW89P6k.d.cts → index-QDfAzfWY.d.cts} +1 -1
- package/dist/{index-B8ZxUpy4.d.ts → index-QZa0vqFW.d.ts} +1 -1
- package/dist/{index-7A3j-6Np.d.cts → index-oY3-BSwC.d.cts} +1 -1
- package/dist/index.cjs +8 -8
- package/dist/index.d.cts +9 -9
- package/dist/index.d.ts +9 -9
- package/dist/index.js +8 -8
- package/dist/isRecord-CwlfDtHp.cjs +16 -0
- package/dist/isRecord-CwlfDtHp.cjs.map +1 -0
- package/dist/isRecord-DaSp13D_.js +11 -0
- package/dist/isRecord-DaSp13D_.js.map +1 -0
- package/dist/{json-DeqPhPUQ.js → json-IKokiUCp.js} +34 -19
- package/dist/json-IKokiUCp.js.map +1 -0
- package/dist/{json-KV2VhBue.cjs → json-mEYdjumm.cjs} +34 -19
- package/dist/json-mEYdjumm.cjs.map +1 -0
- package/dist/json.cjs +1 -1
- package/dist/json.d.cts +1 -1
- package/dist/json.d.ts +1 -1
- package/dist/json.js +1 -1
- package/dist/{macro-3uGSg-ic.js → macro-DhgoqDs6.js} +2 -2
- package/dist/{macro-3uGSg-ic.js.map → macro-DhgoqDs6.js.map} +1 -1
- package/dist/{macro-Ywn4-0s5.cjs → macro-h3bm9G48.cjs} +2 -2
- package/dist/{macro-Ywn4-0s5.cjs.map → macro-h3bm9G48.cjs.map} +1 -1
- package/dist/macro.cjs +1 -1
- package/dist/macro.d.cts +1 -1
- package/dist/macro.d.ts +1 -1
- package/dist/macro.js +1 -1
- package/dist/{node-BeccjrHH.cjs → node-Dhh4jTDv.cjs} +2 -2
- package/dist/{node-BeccjrHH.cjs.map → node-Dhh4jTDv.cjs.map} +1 -1
- package/dist/{node-Dm56432_.js → node-Dy0mq_hi.js} +2 -2
- package/dist/{node-Dm56432_.js.map → node-Dy0mq_hi.js.map} +1 -1
- package/dist/node.cjs +2 -2
- package/dist/node.d.cts +2 -2
- package/dist/node.d.ts +2 -2
- package/dist/node.js +2 -2
- package/dist/{nodes-KgbYiyJQ.js → nodes-B-MW8yk-.js} +2 -2
- package/dist/{nodes-KgbYiyJQ.js.map → nodes-B-MW8yk-.js.map} +1 -1
- package/dist/{nodes-BGxHocS_.cjs → nodes-BjVv21pb.cjs} +12 -12
- package/dist/{nodes-BGxHocS_.cjs.map → nodes-BjVv21pb.cjs.map} +1 -1
- package/dist/promise-DMQPWp6M.cjs.map +1 -1
- package/dist/promise-bi4pc8Xr.js.map +1 -1
- package/dist/{schema-XGRiAvok.cjs → schema-CMe0Dvgd.cjs} +2 -2
- package/dist/{schema-XGRiAvok.cjs.map → schema-CMe0Dvgd.cjs.map} +1 -1
- package/dist/{schema-B05JSQRq.js → schema-CVIk19Mh.js} +2 -2
- package/dist/{schema-B05JSQRq.js.map → schema-CVIk19Mh.js.map} +1 -1
- package/dist/schema.cjs +2 -2
- package/dist/schema.d.cts +2 -2
- package/dist/schema.d.ts +2 -2
- package/dist/schema.js +2 -2
- package/dist/{visitor-xxcbpM5-.d.ts → visitor-BWHpwRgc.d.ts} +2 -2
- package/dist/{visitor-BIldddZd.d.cts → visitor-CPlsMCPZ.d.cts} +2 -2
- package/dist/{visitor-DA5yfOJz.js → visitor-CitcdWu0.js} +5 -10
- package/dist/visitor-CitcdWu0.js.map +1 -0
- package/dist/{visitor-DhuKS-7x.cjs → visitor-DT712cxr.cjs} +5 -10
- package/dist/visitor-DT712cxr.cjs.map +1 -0
- package/package.json +1 -1
- package/dist/combineFileMembers-B-mdjXrG.js.map +0 -1
- package/dist/combineFileMembers-DZCKxAN0.cjs.map +0 -1
- package/dist/graph-D6Ffy9YD.cjs.map +0 -1
- package/dist/graph-DWlCmvm6.js.map +0 -1
- package/dist/json-DeqPhPUQ.js.map +0 -1
- package/dist/json-KV2VhBue.cjs.map +0 -1
- package/dist/visitor-DA5yfOJz.js.map +0 -1
- package/dist/visitor-DhuKS-7x.cjs.map +0 -1
|
@@ -307,6 +307,21 @@ declare function createBreak(): BreakNode;
|
|
|
307
307
|
* ```
|
|
308
308
|
*/
|
|
309
309
|
declare function createJsx(value: string): JsxNode;
|
|
310
|
+
/**
|
|
311
|
+
* Creates an `ImportNode` and fills the deprecated `name` and `isTypeOnly` from `names`. A deprecated field
|
|
312
|
+
* that differs from the one `names` gives overrides it.
|
|
313
|
+
*
|
|
314
|
+
* A spread keeps the record of the derived fields only when the original node has one, which a node made
|
|
315
|
+
* from `names` does. For a node made without `names`, or a JSON or `structuredClone` copy, leave out `name`
|
|
316
|
+
* and `isTypeOnly` before you pass new `names`, or the old `name` replaces the new `names` and the old
|
|
317
|
+
* `isTypeOnly` sets their space.
|
|
318
|
+
*/
|
|
319
|
+
declare function createImport(input: ImportInput): ImportNode;
|
|
320
|
+
/**
|
|
321
|
+
* Creates an `ExportNode`, with the deprecated fields of {@link createImport}. For a node made without
|
|
322
|
+
* `names`, or a JSON or `structuredClone` copy, leave out `name` and `isTypeOnly` before you pass new `names`.
|
|
323
|
+
*/
|
|
324
|
+
declare function createExport(input: ExportInput): ExportNode;
|
|
310
325
|
/**
|
|
311
326
|
* Supported file extensions.
|
|
312
327
|
*/
|
|
@@ -523,8 +538,8 @@ type SourceNode = BaseNode & {
|
|
|
523
538
|
* baseName: 'petStore.ts',
|
|
524
539
|
* path: 'src/models/petStore.ts',
|
|
525
540
|
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], export: true })],
|
|
526
|
-
* imports: [createImport({ name:
|
|
527
|
-
* exports: [createExport({ name:
|
|
541
|
+
* imports: [createImport({ names: [{ name: 'z' }], path: 'zod' })],
|
|
542
|
+
* exports: [createExport({ names: [{ name: 'Pet' }], path: './petStore' })],
|
|
528
543
|
* })
|
|
529
544
|
* // file.id = SHA256 hash of the path
|
|
530
545
|
* // file.name = 'petStore'
|
|
@@ -602,30 +617,6 @@ type ImportInput = Omit<ImportNode, 'kind' | 'name'> & {
|
|
|
602
617
|
name?: ImportName;
|
|
603
618
|
};
|
|
604
619
|
type ExportInput = Omit<ExportNode, 'kind'>;
|
|
605
|
-
/**
|
|
606
|
-
* Creates an `ImportNode`. A node built from `names` also gets the deprecated fields. A deprecated `name` or
|
|
607
|
-
* `isTypeOnly` that differs from the one `names` gives overrides it, so `{ ...imp, isTypeOnly: true }` still works.
|
|
608
|
-
* Fields a spread copies over unchanged are not overrides, so `{ ...imp, names }` uses the new `names`.
|
|
609
|
-
*
|
|
610
|
-
* @example Named import
|
|
611
|
-
* ```ts
|
|
612
|
-
* createImport({ names: [{ name: 'useState' }], path: 'react' })
|
|
613
|
-
* // import { useState } from 'react'
|
|
614
|
-
* ```
|
|
615
|
-
*/
|
|
616
|
-
declare function createImport(input: ImportInput): ImportNode;
|
|
617
|
-
/**
|
|
618
|
-
* Creates an `ExportNode`. A node built from `names` also gets the deprecated fields. A deprecated `name` or
|
|
619
|
-
* `isTypeOnly` that differs from the one `names` gives overrides it. Fields a spread copies over unchanged are not
|
|
620
|
-
* overrides, so `{ ...exp, names }` uses the new `names`.
|
|
621
|
-
*
|
|
622
|
-
* @example Named export
|
|
623
|
-
* ```ts
|
|
624
|
-
* createExport({ names: [{ name: 'Pet' }], path: './Pet' })
|
|
625
|
-
* // export { Pet } from './Pet'
|
|
626
|
-
* ```
|
|
627
|
-
*/
|
|
628
|
-
declare function createExport(input: ExportInput): ExportNode;
|
|
629
620
|
/**
|
|
630
621
|
* Creates a `SourceNode` representing a fragment of source code within a file.
|
|
631
622
|
*
|
|
@@ -661,8 +652,8 @@ type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind'
|
|
|
661
652
|
* baseName: 'petStore.ts',
|
|
662
653
|
* path: 'src/models/petStore.ts',
|
|
663
654
|
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
664
|
-
* imports: [createImport({ name:
|
|
665
|
-
* exports: [createExport({ name:
|
|
655
|
+
* imports: [createImport({ names: [{ name: 'z' }], path: 'zod' })],
|
|
656
|
+
* exports: [createExport({ names: [{ name: 'Pet' }], path: './petStore' })],
|
|
666
657
|
* })
|
|
667
658
|
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
668
659
|
* // file.name = 'petStore'
|
|
@@ -679,4 +670,4 @@ type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind'
|
|
|
679
670
|
* ```
|
|
680
671
|
*/
|
|
681
672
|
declare function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta>;
|
|
682
|
-
export { createArrowFunction as C, createJsx as D, createFunction as E, createText as O, TypeNode as S, createConst as T, ConstNode as _, ImportNode as a, JsxNode as b, Space as c,
|
|
673
|
+
export { createArrowFunction as C, createJsx as D, createFunction as E, createText as O, TypeNode as S, createConst as T, ConstNode as _, ImportNode as a, JsxNode as b, Space as c, createSource as d, createExport as f, CodeNode as g, BreakNode as h, ImportInput as i, createType as k, UserFileNode as l, ArrowFunctionNode as m, ExportNode as n, ModuleName as o, createImport as p, FileNode as r, SourceNode as s, ExportInput as t, createFile as u, FunctionNode as v, createBreak as w, TextNode as x, JSDocNode as y };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as __exportAll } from "./rolldown-runtime-BYyVTPww.js";
|
|
2
|
-
import { c as getSpace, i as
|
|
2
|
+
import { c as getSpace, i as getRelativePath, l as isExported, o as createImport, s as getNames, t as createFile } from "./file-AcKQt60j.js";
|
|
3
3
|
import { t as nodeKinds } from "./constants-UorsTFsa.js";
|
|
4
4
|
import { n as getNodeText, t as CODE_NODE_TEXT_FIELDS } from "./nodeText-DFak5VpX.js";
|
|
5
5
|
// ../../internals/utils/src/text.ts
|
|
@@ -193,4 +193,4 @@ var file_exports = /* @__PURE__ */ __exportAll({
|
|
|
193
193
|
});
|
|
194
194
|
export { inlineFiles as n, file_exports as t };
|
|
195
195
|
|
|
196
|
-
//# sourceMappingURL=file-
|
|
196
|
+
//# sourceMappingURL=file-pjdyWLJ0.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-CO9KnOvT.js","names":[],"sources":["../../../internals/utils/src/text.ts","../src/utils/inline.ts","../src/file/index.ts"],"sourcesContent":["/**\n * Indents every non-empty line of `text` by `unit`, leaving blank lines empty.\n * @example\n * `indent({ text: 'a\\n\\nb' }) // ' a\\n\\n b'`\n */\nexport function indent({ text, unit = ' ' }: { text: string; unit?: string }): string {\n if (!text) return ''\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${unit}${line}` : ''))\n .join('\\n')\n}\n\n/**\n * Strips the common leading whitespace and the surrounding blank lines, counting tabs and spaces alike, so indented template literals start at column zero.\n */\nexport function dedent(text: string): string {\n if (!text) return ''\n // Most text nodes are one line, so the common indent is its leading whitespace.\n if (!text.includes('\\n')) return text.trim() ? text.trimStart() : ''\n\n const lines = text.split('\\n')\n const indents = lines.map((line) => {\n const indent = line.length - line.trimStart().length\n return indent === line.length ? -1 : indent\n })\n const start = indents.findIndex((indent) => indent !== -1)\n if (start === -1) return ''\n const end = indents.findLastIndex((indent) => indent !== -1)\n // `-1` marks a blank line. `reduce`, not `Math.min(...indents)`, which overflows the stack on very long text.\n const min = indents.reduce((lowest, indent) => (indent !== -1 && indent < lowest ? indent : lowest), Infinity)\n\n return lines\n .slice(start, end + 1)\n .map((line, i) => (indents[start + i] === -1 ? '' : line.slice(min)))\n .join('\\n')\n}\n\n/**\n * Strips one matching pair of `\"`, `'` or `` ` `` from both ends of `text`. Returns `text` unchanged without a matching pair.\n * @example\n * `unquote('\"hello\"') // 'hello'`\n */\nexport function unquote(text: string): string {\n if (text.length < 2) return text\n const first = text[0]\n return (first === '\"' || first === \"'\" || first === '`') && text.at(-1) === first ? text.slice(1, -1) : text\n}\n\n/**\n * Returns `name` when it is free, otherwise the first free `name2`, `name3`, and so on.\n * @example\n * `uniqueName('Options', (name) => name === 'Options') // 'Options2'`\n */\nexport function uniqueName(name: string, isTaken: (candidate: string) => boolean): string {\n let candidate = name\n for (let suffix = 2; isTaken(candidate); suffix++) candidate = `${name}${suffix}`\n return candidate\n}\n\n/** The offset where each line of `text` starts, for {@link toPosition}. */\nexport function lineStarts(text: string): Array<number> {\n const starts = [0]\n for (let index = text.indexOf('\\n'); index !== -1; index = text.indexOf('\\n', index + 1)) starts.push(index + 1)\n return starts\n}\n\n/** The 1-based line and column (UTF-16) of `offset`, from the {@link lineStarts} of the same text. */\nexport function toPosition(starts: ReadonlyArray<number>, offset: number): { line: number; column: number } {\n let low = 0\n let high = starts.length - 1\n while (low < high) {\n const middle = (low + high + 1) >> 1\n if (starts[middle]! <= offset) low = middle\n else high = middle - 1\n }\n return { line: low + 1, column: offset - starts[low]! + 1 }\n}\n","import { nodeKinds } from '../constants.ts'\nimport type { CodeNode } from '../nodes/code.ts'\nimport { createFile, createImport } from '../nodes/file.ts'\nimport type { FileNode, ImportNode, SourceNode } from '../nodes/file.ts'\nimport { uniqueName } from '@internals/utils'\nimport { getNames } from './moduleNames.ts'\nimport { CODE_NODE_TEXT_FIELDS, getNodeText } from './nodeText.ts'\n\nexport type InlineFilesResult = {\n /**\n * Files that imported an inline name, rewritten to hold its definition.\n */\n files: Array<FileNode>\n /**\n * Paths of the virtual files, which are not written.\n */\n virtualPaths: Array<string>\n /**\n * One message per name that could not be inlined.\n */\n problems: Array<{ path: string; message: string }>\n}\n\ntype Definition = { expression: string; imports: Array<ImportNode> }\n\n/**\n * The language-specific half of {@link inlineFiles}, supplied by the parser that prints the file.\n *\n * @deprecated Make the parser's `inline` a function, `(files) => InlineResult`, as `@kubb/parser-ts` does. Removed in v6.\n */\nexport type InlineLanguage = {\n /**\n * Swaps in what `replace` returns for every identifier in `text` that references a name, skipping strings and comments.\n */\n replaceIdentifiers(text: string, replace: (name: string) => string | undefined): string\n /**\n * Makes `expression` safe to put where a name was, such as `(A | B)` in TypeScript.\n */\n wrap(expression: string): string\n}\n\nexport type InlineFilesOptions = {\n /**\n * The inline support for a file's language, or `undefined` when it has none.\n */\n language: (file: FileNode) => InlineLanguage | undefined\n}\n\n/**\n * Replaces every name a file imports from a virtual file (one marked `inline`, so it is never written) with the body of that name's\n * declaration, until no inline names are left. The definitions' own imports move to the importing file.\n *\n * @deprecated Make the parser's `inline` a function, `(files) => InlineResult`, as `@kubb/parser-ts` does. Removed in v6.\n */\nexport function inlineFiles(files: ReadonlyArray<FileNode>, { language }: InlineFilesOptions): InlineFilesResult {\n const virtualFiles = new Map(files.filter(isInlineFile).map((file) => [file.path, file]))\n const result: InlineFilesResult = { files: [], virtualPaths: [...virtualFiles.keys()], problems: [] }\n if (virtualFiles.size === 0) return result\n\n // `null` marks a definition that is being resolved, so a name that reaches itself is a cycle.\n const definitions = new Map<string, Definition | null>()\n\n function define(path: string, name: string, from: string, lang: InlineLanguage): Definition | undefined {\n const key = `${path}\\0${name}`\n if (definitions.has(key)) {\n const definition = definitions.get(key)\n if (definition === null) result.problems.push({ path: from, message: `\"${name}\" from \"${path}\" is defined in terms of itself.` })\n return definition ?? undefined\n }\n\n const file = virtualFiles.get(path)!\n const expression = bodyOf(file.sources.find((source) => source.name === name))\n if (expression === undefined) {\n result.problems.push({ path: from, message: `\"${name}\" is imported from \"${path}\", which never writes it and has no inline definition for it.` })\n return undefined\n }\n\n definitions.set(key, null)\n const imports: Array<ImportNode> = []\n const { text, used } = replaceNames(expression, file, lang, (inner) => {\n imports.push(...inner.imports)\n return inner.expression\n })\n imports.push(...file.imports.flatMap((imp) => (virtualFiles.has(imp.path) ? [] : pickNames(imp, used))))\n\n const definition = { expression: text, imports }\n definitions.set(key, definition)\n return definition\n }\n\n // `use` takes each replaced definition's imports and returns its text; `used` holds the names left as they are.\n function replaceNames(input: string, file: FileNode, lang: InlineLanguage, use: (definition: Definition) => string) {\n const used = new Set<string>()\n const text = lang.replaceIdentifiers(input, (local) => {\n const target = findTarget(file, local)\n const definition = target && define(target.path, target.name, file.path, lang)\n if (!definition) {\n used.add(local)\n return undefined\n }\n\n return lang.wrap(use(definition))\n })\n\n return { text, used }\n }\n\n function findTarget(file: FileNode, local: string): { path: string; name: string } | undefined {\n if (virtualFiles.has(file.path) && file.sources.some((source) => source.name === local)) return { path: file.path, name: local }\n\n for (const imp of file.imports) {\n const item = virtualFiles.has(imp.path) && getNames(imp).find((item) => (item.alias ?? item.name) === local)\n if (item) return { path: imp.path, name: item.name }\n }\n\n return undefined\n }\n\n for (const file of files) {\n if (virtualFiles.has(file.path) || !file.imports.some((imp) => virtualFiles.has(imp.path))) continue\n\n const lang = language(file)\n if (!lang) {\n result.problems.push({ path: file.path, message: `No parser for \"${file.extname}\" files supports inline sources.` })\n continue\n }\n\n const carried: Array<ImportNode> = []\n const carry = carryImports(file, lang, carried)\n const sources = file.sources.map((source) => ({ ...source, nodes: rewriteNodes(source.nodes, (text) => replaceNames(text, file, lang, carry).text) }))\n\n // A carried import keeps its target; `root` moves to the importing file so the relative path is computed from there.\n const imports = [...file.imports.filter((imp) => !virtualFiles.has(imp.path)), ...carried.map((imp) => (imp.root ? { ...imp, root: file.path } : imp))]\n\n result.files.push(createFile({ ...file, sources, imports }))\n }\n\n return result\n}\n\n// Moves each definition's imports into `carried` and returns the definition's text. A carried name the file already binds to\n// something else, such as a schema `Options` next to the client's own `Options`, gets an alias like `Options2`.\nfunction carryImports(file: FileNode, lang: InlineLanguage, carried: Array<ImportNode>) {\n // Each name the file uses, to the import (module and exported name) or declaration behind it. `''` marks a global, such as\n // the fetch `Response` next to a schema `Response`.\n const bindings = new Map<string, string>()\n for (const imp of file.imports) for (const item of getNames(imp)) bindings.set(item.alias ?? item.name, `${imp.path}\\0${item.name}`)\n for (const source of file.sources) {\n if (source.name) bindings.set(source.name, file.path)\n rewriteNodes(source.nodes, (text) =>\n lang.replaceIdentifiers(text, (name) => {\n if (!bindings.has(name)) bindings.set(name, '')\n return undefined\n }),\n )\n }\n\n return (definition: Definition): string => {\n const aliases = new Map<string, string>()\n for (const imp of definition.imports) {\n const before = aliases.size\n const names = getNames(imp).map((item) => {\n const local = item.alias ?? item.name\n const binding = `${imp.path}\\0${item.name}`\n const name = uniqueName(local, (candidate) => bindings.has(candidate) && bindings.get(candidate) !== binding)\n bindings.set(name, binding)\n if (name === local) return item\n aliases.set(local, name)\n return { ...item, alias: name }\n })\n carried.push(aliases.size > before ? createImport({ path: imp.path, ...(imp.root != null ? { root: imp.root } : {}), names }) : imp)\n }\n\n return aliases.size ? lang.replaceIdentifiers(definition.expression, (name) => aliases.get(name)) : definition.expression\n }\n}\n\n// A file of only deprecated per-source `inline` counts as inline too.\nfunction isInlineFile(file: FileNode): boolean {\n return !!file.inline || (file.sources.length > 0 && file.sources.every((source) => source.inline))\n}\n\n// The right-hand side of a `Const` or `Type`. A function body is not an expression, and printed text such as `Code` has none.\nfunction bodyOf(source: SourceNode | undefined): string | undefined {\n if (source?.inline) return source.inline.expression\n const node = source?.nodes?.length === 1 ? source.nodes[0] : undefined\n if (node?.kind !== nodeKinds.const && node?.kind !== nodeKinds.type) return undefined\n return getNodeText(node.nodes) || undefined\n}\n\nfunction pickNames(imp: ImportNode, used: ReadonlySet<string>): Array<ImportNode> {\n const names = getNames(imp).filter((item) => used.has(item.alias ?? item.name))\n return names.length > 0 ? [createImport({ path: imp.path, ...(imp.root != null ? { root: imp.root } : {}), names })] : []\n}\n\n// Rewrites the string fields `getNodeText` reads; JSDoc stays as written.\nfunction rewriteNodes(nodes: Array<CodeNode> | undefined, rewrite: (text: string) => string): Array<CodeNode> | undefined {\n return nodes?.map((node): CodeNode => {\n // Backward-compat: compiled plugins may still pass bare strings at runtime\n if (typeof node === 'string') return rewrite(node) as unknown as CodeNode\n if (node.kind === nodeKinds.break) return node\n\n const next = { ...node } as unknown as Record<string, unknown>\n for (const field of CODE_NODE_TEXT_FIELDS[node.kind] ?? []) {\n const value = next[field]\n if (Array.isArray(value)) next[field] = value.map(rewrite)\n if (typeof value === 'string' && value) next[field] = rewrite(value)\n }\n if ('nodes' in node && node.nodes) next.nodes = rewriteNodes(node.nodes, rewrite)\n return next as unknown as CodeNode\n })\n}\n","export { getRelativePath } from '../utils/combineFileMembers.ts'\nexport { inlineFiles as inline } from '../utils/inline.ts'\nexport { getNames, getSpace, isExported } from '../utils/moduleNames.ts'\n"],"mappings":";;;;;;;;;;AAsDA,SAAgB,WAAW,MAAc,SAAiD;CACxF,IAAI,YAAY;CAChB,KAAK,IAAI,SAAS,GAAG,QAAQ,SAAS,GAAG,UAAU,YAAY,GAAG,OAAO;CACzE,OAAO;AACT;;;;;;;;ACJA,SAAgB,YAAY,OAAgC,EAAE,YAAmD;CAC/G,MAAM,eAAe,IAAI,IAAI,MAAM,OAAO,YAAY,CAAC,CAAC,KAAK,SAAS,CAAC,KAAK,MAAM,IAAI,CAAC,CAAC;CACxF,MAAM,SAA4B;EAAE,OAAO,CAAC;EAAG,cAAc,CAAC,GAAG,aAAa,KAAK,CAAC;EAAG,UAAU,CAAC;CAAE;CACpG,IAAI,aAAa,SAAS,GAAG,OAAO;CAGpC,MAAM,8BAAc,IAAI,IAA+B;CAEvD,SAAS,OAAO,MAAc,MAAc,MAAc,MAA8C;EACtG,MAAM,MAAM,GAAG,KAAK,IAAI;EACxB,IAAI,YAAY,IAAI,GAAG,GAAG;GACxB,MAAM,aAAa,YAAY,IAAI,GAAG;GACtC,IAAI,eAAe,MAAM,OAAO,SAAS,KAAK;IAAE,MAAM;IAAM,SAAS,IAAI,KAAK,UAAU,KAAK;GAAkC,CAAC;GAChI,OAAO,cAAc,KAAA;EACvB;EAEA,MAAM,OAAO,aAAa,IAA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7D,IAAI,MAAM,SAAS,UAAU,SAAS,MAAM,SAAS,UAAU,MAAM,OAAO,KAAA;CAC5E,OAAO,YAAY,KAAK,KAAK,KAAK,KAAA;AACpC;AAEA,SAAS,UAAU,KAAiB,MAA8C;CAChF,MAAM,QAAQ,SAAS,GAAG,CAAC,CAAC,QAAQ,SAAS,KAAK,IAAI,KAAK,SAAS,KAAK,IAAI,CAAC;CAC9E,OAAO,MAAM,SAAS,IAAI,CAAC,aAAa;EAAE,MAAM,IAAI;EAAM,GAAI,IAAI,QAAQ,OAAO,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;EAAI;CAAM,CAAC,CAAC,IAAI,CAAC;AAC1H;AAGA,SAAS,aAAa,OAAoC,SAAgE;CACxH,OAAO,OAAO,KAAK,SAAmB;EAEpC,IAAI,OAAO,SAAS,UAAU,OAAO,QAAQ,IAAI;EACjD,IAAI,KAAK,SAAS,UAAU,OAAO,OAAO;EAE1C,MAAM,OAAO,EAAE,GAAG,KAAK;EACvB,KAAK,MAAM,SAAS,sBAAsB,KAAK,SAAS,CAAC,GAAG;GAC1D,MAAM,QAAQ,KAAK;GACnB,IAAI,MAAM,QAAQ,KAAK,GAAG,KAAK,SAAS,MAAM,IAAI,OAAO;GACzD,IAAI,OAAO,UAAU,YAAY,OAAO,KAAK,SAAS,QAAQ,KAAK;EACrE;EACA,IAAI,WAAW,QAAQ,KAAK,OAAO,KAAK,QAAQ,aAAa,KAAK,OAAO,OAAO;EAChF,OAAO;CACT,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"file-pjdyWLJ0.js","names":[],"sources":["../../../internals/utils/src/text.ts","../src/utils/inline.ts","../src/file/index.ts"],"sourcesContent":["/**\n * Indents every non-empty line of `text` by `unit`, leaving blank lines empty.\n * @example\n * `indent({ text: 'a\\n\\nb' }) // ' a\\n\\n b'`\n */\nexport function indent({ text, unit = ' ' }: { text: string; unit?: string }): string {\n if (!text) return ''\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${unit}${line}` : ''))\n .join('\\n')\n}\n\n/**\n * Strips the common leading whitespace and the surrounding blank lines, counting tabs and spaces alike, so indented template literals start at column zero.\n */\nexport function dedent(text: string): string {\n if (!text) return ''\n // Most text nodes are one line, so the common indent is its leading whitespace.\n if (!text.includes('\\n')) return text.trim() ? text.trimStart() : ''\n\n const lines = text.split('\\n')\n const indents = lines.map((line) => {\n const indent = line.length - line.trimStart().length\n return indent === line.length ? -1 : indent\n })\n const start = indents.findIndex((indent) => indent !== -1)\n if (start === -1) return ''\n const end = indents.findLastIndex((indent) => indent !== -1)\n // `-1` marks a blank line. `reduce`, not `Math.min(...indents)`, which overflows the stack on very long text.\n const min = indents.reduce((lowest, indent) => (indent !== -1 && indent < lowest ? indent : lowest), Infinity)\n\n return lines\n .slice(start, end + 1)\n .map((line, i) => (indents[start + i] === -1 ? '' : line.slice(min)))\n .join('\\n')\n}\n\n/**\n * Strips one matching pair of `\"`, `'` or `` ` `` from both ends of `text`. Returns `text` unchanged without a matching pair.\n * @example\n * `unquote('\"hello\"') // 'hello'`\n */\nexport function unquote(text: string): string {\n if (text.length < 2) return text\n const first = text[0]\n return (first === '\"' || first === \"'\" || first === '`') && text.at(-1) === first ? text.slice(1, -1) : text\n}\n\n/**\n * Returns `name` when it is free, otherwise the first free `name2`, `name3`, and so on.\n * @example\n * `uniqueName('Options', (name) => name === 'Options') // 'Options2'`\n */\nexport function uniqueName(name: string, isTaken: (candidate: string) => boolean): string {\n let candidate = name\n for (let suffix = 2; isTaken(candidate); suffix++) candidate = `${name}${suffix}`\n return candidate\n}\n\n/** The offset where each line of `text` starts, for {@link toPosition}. */\nexport function lineStarts(text: string): Array<number> {\n const starts = [0]\n for (let index = text.indexOf('\\n'); index !== -1; index = text.indexOf('\\n', index + 1)) starts.push(index + 1)\n return starts\n}\n\n/** The 1-based line and column (UTF-16) of `offset`, from the {@link lineStarts} of the same text. */\nexport function toPosition(starts: ReadonlyArray<number>, offset: number): { line: number; column: number } {\n let low = 0\n let high = starts.length - 1\n while (low < high) {\n const middle = (low + high + 1) >> 1\n if (starts[middle]! <= offset) low = middle\n else high = middle - 1\n }\n return { line: low + 1, column: offset - starts[low]! + 1 }\n}\n","import { nodeKinds } from '../constants.ts'\nimport type { CodeNode } from '../nodes/code.ts'\nimport { createFile, createImport } from '../nodes/file.ts'\nimport type { FileNode, ImportNode, SourceNode } from '../nodes/file.ts'\nimport { uniqueName } from '@internals/utils'\nimport { getNames } from './moduleNames.ts'\nimport { CODE_NODE_TEXT_FIELDS, getNodeText } from './nodeText.ts'\n\nexport type InlineFilesResult = {\n /**\n * Files that imported an inline name, rewritten to hold its definition.\n */\n files: Array<FileNode>\n /**\n * Paths of the virtual files, which are not written.\n */\n virtualPaths: Array<string>\n /**\n * One message per name that could not be inlined.\n */\n problems: Array<{ path: string; message: string }>\n}\n\ntype Definition = { expression: string; imports: Array<ImportNode> }\n\n/**\n * The language-specific half of {@link inlineFiles}, supplied by the parser that prints the file.\n *\n * @deprecated Make the parser's `inline` a function, `(files) => InlineResult`, as `@kubb/parser-ts` does. Removed in v6.\n */\nexport type InlineLanguage = {\n /**\n * Swaps in what `replace` returns for every identifier in `text` that references a name, skipping strings and comments.\n */\n replaceIdentifiers(text: string, replace: (name: string) => string | undefined): string\n /**\n * Makes `expression` safe to put where a name was, such as `(A | B)` in TypeScript.\n */\n wrap(expression: string): string\n}\n\nexport type InlineFilesOptions = {\n /**\n * The inline support for a file's language, or `undefined` when it has none.\n */\n language: (file: FileNode) => InlineLanguage | undefined\n}\n\n/**\n * Replaces every name a file imports from a virtual file (one marked `inline`, so it is never written) with the body of that name's\n * declaration, until no inline names are left. The definitions' own imports move to the importing file.\n *\n * @deprecated Make the parser's `inline` a function, `(files) => InlineResult`, as `@kubb/parser-ts` does. Removed in v6.\n */\nexport function inlineFiles(files: ReadonlyArray<FileNode>, { language }: InlineFilesOptions): InlineFilesResult {\n const virtualFiles = new Map(files.filter(isInlineFile).map((file) => [file.path, file]))\n const result: InlineFilesResult = { files: [], virtualPaths: [...virtualFiles.keys()], problems: [] }\n if (virtualFiles.size === 0) return result\n\n // `null` marks a definition that is being resolved, so a name that reaches itself is a cycle.\n const definitions = new Map<string, Definition | null>()\n\n function define(path: string, name: string, from: string, lang: InlineLanguage): Definition | undefined {\n const key = `${path}\\0${name}`\n if (definitions.has(key)) {\n const definition = definitions.get(key)\n if (definition === null) result.problems.push({ path: from, message: `\"${name}\" from \"${path}\" is defined in terms of itself.` })\n return definition ?? undefined\n }\n\n const file = virtualFiles.get(path)!\n const expression = bodyOf(file.sources.find((source) => source.name === name))\n if (expression === undefined) {\n result.problems.push({ path: from, message: `\"${name}\" is imported from \"${path}\", which never writes it and has no inline definition for it.` })\n return undefined\n }\n\n definitions.set(key, null)\n const imports: Array<ImportNode> = []\n const { text, used } = replaceNames(expression, file, lang, (inner) => {\n imports.push(...inner.imports)\n return inner.expression\n })\n imports.push(...file.imports.flatMap((imp) => (virtualFiles.has(imp.path) ? [] : pickNames(imp, used))))\n\n const definition = { expression: text, imports }\n definitions.set(key, definition)\n return definition\n }\n\n // `use` takes each replaced definition's imports and returns its text; `used` holds the names left as they are.\n function replaceNames(input: string, file: FileNode, lang: InlineLanguage, use: (definition: Definition) => string) {\n const used = new Set<string>()\n const text = lang.replaceIdentifiers(input, (local) => {\n const target = findTarget(file, local)\n const definition = target && define(target.path, target.name, file.path, lang)\n if (!definition) {\n used.add(local)\n return undefined\n }\n\n return lang.wrap(use(definition))\n })\n\n return { text, used }\n }\n\n function findTarget(file: FileNode, local: string): { path: string; name: string } | undefined {\n if (virtualFiles.has(file.path) && file.sources.some((source) => source.name === local)) return { path: file.path, name: local }\n\n for (const imp of file.imports) {\n const item = virtualFiles.has(imp.path) && getNames(imp).find((item) => (item.alias ?? item.name) === local)\n if (item) return { path: imp.path, name: item.name }\n }\n\n return undefined\n }\n\n for (const file of files) {\n if (virtualFiles.has(file.path) || !file.imports.some((imp) => virtualFiles.has(imp.path))) continue\n\n const lang = language(file)\n if (!lang) {\n result.problems.push({ path: file.path, message: `No parser for \"${file.extname}\" files supports inline sources.` })\n continue\n }\n\n const carried: Array<ImportNode> = []\n const carry = carryImports(file, lang, carried)\n const sources = file.sources.map((source) => ({ ...source, nodes: rewriteNodes(source.nodes, (text) => replaceNames(text, file, lang, carry).text) }))\n\n // A carried import keeps its target; `root` moves to the importing file so the relative path is computed from there.\n const imports = [...file.imports.filter((imp) => !virtualFiles.has(imp.path)), ...carried.map((imp) => (imp.root ? { ...imp, root: file.path } : imp))]\n\n result.files.push(createFile({ ...file, sources, imports }))\n }\n\n return result\n}\n\n// Moves each definition's imports into `carried` and returns the definition's text. A carried name the file already binds to\n// something else, such as a schema `Options` next to the client's own `Options`, gets an alias like `Options2`.\nfunction carryImports(file: FileNode, lang: InlineLanguage, carried: Array<ImportNode>) {\n // Each name the file uses, to the import (module and exported name) or declaration behind it. `''` marks a global, such as\n // the fetch `Response` next to a schema `Response`.\n const bindings = new Map<string, string>()\n for (const imp of file.imports) for (const item of getNames(imp)) bindings.set(item.alias ?? item.name, `${imp.path}\\0${item.name}`)\n for (const source of file.sources) {\n if (source.name) bindings.set(source.name, file.path)\n rewriteNodes(source.nodes, (text) =>\n lang.replaceIdentifiers(text, (name) => {\n if (!bindings.has(name)) bindings.set(name, '')\n return undefined\n }),\n )\n }\n\n return (definition: Definition): string => {\n const aliases = new Map<string, string>()\n for (const imp of definition.imports) {\n const before = aliases.size\n const names = getNames(imp).map((item) => {\n const local = item.alias ?? item.name\n const binding = `${imp.path}\\0${item.name}`\n const name = uniqueName(local, (candidate) => bindings.has(candidate) && bindings.get(candidate) !== binding)\n bindings.set(name, binding)\n if (name === local) return item\n aliases.set(local, name)\n return { ...item, alias: name }\n })\n carried.push(aliases.size > before ? createImport({ path: imp.path, ...(imp.root != null ? { root: imp.root } : {}), names }) : imp)\n }\n\n return aliases.size ? lang.replaceIdentifiers(definition.expression, (name) => aliases.get(name)) : definition.expression\n }\n}\n\n// A file of only deprecated per-source `inline` counts as inline too.\nfunction isInlineFile(file: FileNode): boolean {\n return !!file.inline || (file.sources.length > 0 && file.sources.every((source) => source.inline))\n}\n\n// The right-hand side of a `Const` or `Type`. A function body is not an expression, and printed text such as `Code` has none.\nfunction bodyOf(source: SourceNode | undefined): string | undefined {\n if (source?.inline) return source.inline.expression\n const node = source?.nodes?.length === 1 ? source.nodes[0] : undefined\n if (node?.kind !== nodeKinds.const && node?.kind !== nodeKinds.type) return undefined\n return getNodeText(node.nodes) || undefined\n}\n\nfunction pickNames(imp: ImportNode, used: ReadonlySet<string>): Array<ImportNode> {\n const names = getNames(imp).filter((item) => used.has(item.alias ?? item.name))\n return names.length > 0 ? [createImport({ path: imp.path, ...(imp.root != null ? { root: imp.root } : {}), names })] : []\n}\n\n// Rewrites the string fields `getNodeText` reads; JSDoc stays as written.\nfunction rewriteNodes(nodes: Array<CodeNode> | undefined, rewrite: (text: string) => string): Array<CodeNode> | undefined {\n return nodes?.map((node): CodeNode => {\n // Backward-compat: compiled plugins may still pass bare strings at runtime\n if (typeof node === 'string') return rewrite(node) as unknown as CodeNode\n if (node.kind === nodeKinds.break) return node\n\n const next = { ...node } as unknown as Record<string, unknown>\n for (const field of CODE_NODE_TEXT_FIELDS[node.kind] ?? []) {\n const value = next[field]\n if (Array.isArray(value)) next[field] = value.map(rewrite)\n if (typeof value === 'string' && value) next[field] = rewrite(value)\n }\n if ('nodes' in node && node.nodes) next.nodes = rewriteNodes(node.nodes, rewrite)\n return next as unknown as CodeNode\n })\n}\n","export { getRelativePath } from '../utils/combineFileMembers.ts'\nexport { inlineFiles as inline } from '../utils/inline.ts'\nexport { getNames, getSpace, isExported } from '../utils/moduleNames.ts'\n"],"mappings":";;;;;;;;;;AAsDA,SAAgB,WAAW,MAAc,SAAiD;CACxF,IAAI,YAAY;CAChB,KAAK,IAAI,SAAS,GAAG,QAAQ,SAAS,GAAG,UAAU,YAAY,GAAG,OAAO;CACzE,OAAO;AACT;;;;;;;;ACJA,SAAgB,YAAY,OAAgC,EAAE,YA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AM,SAAS,sBAAsB,KAAK,SAAS,CAAC,GAAG;GAC1D,MAAM,QAAQ,KAAK;GACnB,IAAI,MAAM,QAAQ,KAAK,GAAG,KAAK,SAAS,MAAM,IAAI,OAAO;GACzD,IAAI,OAAO,UAAU,YAAY,OAAO,KAAK,SAAS,QAAQ,KAAK;EACrE;EACA,IAAI,WAAW,QAAQ,KAAK,OAAO,KAAK,QAAQ,aAAa,KAAK,OAAO,OAAO;EAChF,OAAO;CACT,CAAC;AACH"}
|
package/dist/file.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_file = require("./file-
|
|
3
|
-
const
|
|
4
|
-
exports.getNames =
|
|
5
|
-
exports.getRelativePath =
|
|
6
|
-
exports.getSpace =
|
|
2
|
+
const require_file = require("./file-DVw2Htda.cjs");
|
|
3
|
+
const require_file$1 = require("./file-Db_PYobC.cjs");
|
|
4
|
+
exports.getNames = require_file$1.getNames;
|
|
5
|
+
exports.getRelativePath = require_file$1.getRelativePath;
|
|
6
|
+
exports.getSpace = require_file$1.getSpace;
|
|
7
7
|
exports.inline = require_file.inlineFiles;
|
|
8
|
-
exports.isExported =
|
|
8
|
+
exports.isExported = require_file$1.isExported;
|
package/dist/file.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as getRelativePath, i as isExported, n as getNames, r as getSpace, s as inlineFiles } from "./index-
|
|
1
|
+
import { a as getRelativePath, i as isExported, n as getNames, r as getSpace, s as inlineFiles } from "./index-oY3-BSwC.cjs";
|
|
2
2
|
export { getNames, getRelativePath, getSpace, inlineFiles as inline, isExported };
|
package/dist/file.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as getRelativePath, i as isExported, n as getNames, r as getSpace, s as inlineFiles } from "./index-
|
|
1
|
+
import { a as getRelativePath, i as isExported, n as getNames, r as getSpace, s as inlineFiles } from "./index-DA-Ztg07.js";
|
|
2
2
|
export { getNames, getRelativePath, getSpace, inlineFiles as inline, isExported };
|
package/dist/file.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { n as inlineFiles } from "./file-
|
|
2
|
-
import { c as getSpace,
|
|
1
|
+
import { n as inlineFiles } from "./file-pjdyWLJ0.js";
|
|
2
|
+
import { c as getSpace, i as getRelativePath, l as isExported, s as getNames } from "./file-AcKQt60j.js";
|
|
3
3
|
export { getNames, getRelativePath, getSpace, inlineFiles as inline, isExported };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { t as __exportAll } from "./rolldown-runtime-BYyVTPww.js";
|
|
2
2
|
import { t as memo } from "./promise-bi4pc8Xr.js";
|
|
3
3
|
import { t as nodeKinds } from "./constants-UorsTFsa.js";
|
|
4
|
-
import { i as visitChildNodes, t as collect } from "./visitor-
|
|
4
|
+
import { i as visitChildNodes, t as collect } from "./visitor-CitcdWu0.js";
|
|
5
5
|
import { a as resolveRefName } from "./refs-C0hb_qu-.js";
|
|
6
6
|
// src/utils/schemaGraph.ts
|
|
7
7
|
const refEntries = memo.create({ weak: true });
|
|
@@ -107,27 +107,90 @@ function collectUsedSchemaNames(roots, schemas) {
|
|
|
107
107
|
* Finds every schema that takes part in a circular dependency chain, including direct self-loops.
|
|
108
108
|
*
|
|
109
109
|
* Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so
|
|
110
|
-
* the generated code does not recurse forever. Refs are followed by name only, so the
|
|
111
|
-
*
|
|
110
|
+
* the generated code does not recurse forever. Refs are followed by name only, so the cost stays linear in the size
|
|
111
|
+
* of the schema graph. The result lists names in the order of `schemas`.
|
|
112
112
|
*
|
|
113
113
|
* @note Call this once on the full graph, then check a schema's `ast.graph.getRefNames` against the result.
|
|
114
114
|
*/
|
|
115
115
|
function findCircularSchemas(schemas) {
|
|
116
116
|
const graph = /* @__PURE__ */ new Map();
|
|
117
117
|
for (const schema of schemas) if (schema.name) graph.set(schema.name, collectSchemaRefs(schema));
|
|
118
|
-
|
|
118
|
+
const circular = createCycleFinder(graph).find();
|
|
119
|
+
return new Set([...graph.keys()].filter((name) => circular.has(name)));
|
|
119
120
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
121
|
+
var CycleFinder = class {
|
|
122
|
+
#graph;
|
|
123
|
+
#index = /* @__PURE__ */ new Map();
|
|
124
|
+
#lowLink = /* @__PURE__ */ new Map();
|
|
125
|
+
#onStack = /* @__PURE__ */ new Set();
|
|
126
|
+
#componentStack = [];
|
|
127
|
+
#frames = [];
|
|
128
|
+
#circular = /* @__PURE__ */ new Set();
|
|
129
|
+
constructor(graph) {
|
|
130
|
+
this.#graph = graph;
|
|
129
131
|
}
|
|
130
|
-
|
|
132
|
+
find() {
|
|
133
|
+
for (const root of this.#graph.keys()) if (!this.#index.has(root)) this.#walk(root);
|
|
134
|
+
return this.#circular;
|
|
135
|
+
}
|
|
136
|
+
#walk(root) {
|
|
137
|
+
this.#enter(root);
|
|
138
|
+
while (this.#frames.length > 0) {
|
|
139
|
+
const frame = this.#frames[this.#frames.length - 1];
|
|
140
|
+
const step = frame.edges.next();
|
|
141
|
+
if (step.done) {
|
|
142
|
+
this.#leave(frame);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
this.#follow({
|
|
146
|
+
frame,
|
|
147
|
+
next: step.value
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
#enter(name) {
|
|
152
|
+
this.#index.set(name, this.#index.size);
|
|
153
|
+
this.#lowLink.set(name, this.#index.size - 1);
|
|
154
|
+
this.#componentStack.push(name);
|
|
155
|
+
this.#onStack.add(name);
|
|
156
|
+
this.#frames.push({
|
|
157
|
+
name,
|
|
158
|
+
edges: this.#graph.get(name).values()
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
#follow({ frame, next }) {
|
|
162
|
+
if (!this.#graph.has(next)) return;
|
|
163
|
+
if (!this.#index.has(next)) return this.#enter(next);
|
|
164
|
+
if (this.#onStack.has(next)) this.#lower({
|
|
165
|
+
name: frame.name,
|
|
166
|
+
value: this.#index.get(next)
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
#lower({ name, value }) {
|
|
170
|
+
this.#lowLink.set(name, Math.min(this.#lowLink.get(name), value));
|
|
171
|
+
}
|
|
172
|
+
#leave(frame) {
|
|
173
|
+
this.#frames.pop();
|
|
174
|
+
const parent = this.#frames[this.#frames.length - 1];
|
|
175
|
+
if (parent) this.#lower({
|
|
176
|
+
name: parent.name,
|
|
177
|
+
value: this.#lowLink.get(frame.name)
|
|
178
|
+
});
|
|
179
|
+
if (this.#lowLink.get(frame.name) === this.#index.get(frame.name)) this.#closeComponent(frame.name);
|
|
180
|
+
}
|
|
181
|
+
#closeComponent(root) {
|
|
182
|
+
const component = [];
|
|
183
|
+
let member;
|
|
184
|
+
do {
|
|
185
|
+
member = this.#componentStack.pop();
|
|
186
|
+
this.#onStack.delete(member);
|
|
187
|
+
component.push(member);
|
|
188
|
+
} while (member !== root);
|
|
189
|
+
if (component.length > 1 || this.#graph.get(root).has(root)) for (const name of component) this.#circular.add(name);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
function createCycleFinder(graph) {
|
|
193
|
+
return new CycleFinder(graph);
|
|
131
194
|
}
|
|
132
195
|
// src/graph/index.ts
|
|
133
196
|
var graph_exports = /* @__PURE__ */ __exportAll({
|
|
@@ -138,4 +201,4 @@ var graph_exports = /* @__PURE__ */ __exportAll({
|
|
|
138
201
|
});
|
|
139
202
|
export { findCircularSchemas as a, collectUsedSchemaNames as i, collectImportedRefNames as n, collectSchemaRefs as r, graph_exports as t };
|
|
140
203
|
|
|
141
|
-
//# sourceMappingURL=graph-
|
|
204
|
+
//# sourceMappingURL=graph-Cu2-NiKi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph-Cu2-NiKi.js","names":[],"sources":["../src/utils/schemaGraph.ts","../src/graph/index.ts"],"sourcesContent":["import { memo } from '@internals/utils'\nimport { nodeKinds } from '../constants.ts'\nimport type { Node } from '../nodes/index.ts'\nimport type { OperationNode } from '../nodes/operation.ts'\nimport type { SchemaNode } from '../nodes/schema.ts'\nimport { collect, visitChildNodes } from '../visitor.ts'\nimport { resolveRefName } from './refs.ts'\n\ntype RefEntry = { name: string; hasPointer: boolean }\n\nconst refEntries = memo.create<SchemaNode, ReadonlyArray<RefEntry>>({ weak: true })\nconst checkRefEntries = memo.create<SchemaNode, ReadonlyArray<RefEntry>>({ weak: true })\nconst schemaRefs = memo.create<SchemaNode, ReadonlySet<string>>({ weak: true })\nconst importedRefNames = memo.create<SchemaNode, ReadonlyArray<string>>({ weak: true })\nconst importedCheckRefNames = memo.create<SchemaNode, ReadonlyArray<string>>({ weak: true })\n\n/**\n * Walks a node's subtree once and lists every ref it holds, in traversal order, as the resolved\n * name plus whether the ref carries a `$ref` pointer. Memoized per node and per `checks` mode, so\n * each helper below scans a schema once.\n */\nfunction collectRefEntries(node: SchemaNode, { checks }: { checks: boolean }): ReadonlyArray<RefEntry> {\n return memo(checks ? checkRefEntries : refEntries, node, () => {\n const entries: Array<RefEntry> = []\n const walk = (current: Node): boolean => {\n if (current.kind === nodeKinds.schema && current.type === 'ref') {\n const name = resolveRefName(current)\n if (name) entries.push({ name, hasPointer: Boolean(current.ref) })\n }\n visitChildNodes(current, walk, { checks })\n return false\n }\n walk(node)\n return entries\n })\n}\n\n/**\n * Collects the names of every schema a node's subtree references, resolved with `ast.ref.getName`.\n * Each ref contributes its name only, so the schema it points to is never traversed. Memoized by\n * node identity, so repeated calls on one schema scan it once.\n *\n * @example\n * ```ts\n * collectSchemaRefs(petSchema)\n * // Set { 'Category', 'Tag' }\n * ```\n */\nexport function collectSchemaRefs(node: SchemaNode): ReadonlySet<string> {\n return memo(schemaRefs, node, () => new Set(collectRefEntries(node, { checks: true }).map((entry) => entry.name)))\n}\n\n/**\n * Collects the de-duplicated target names of every pointer-carrying ref in a node's subtree, in\n * first-occurrence order. Memoized by node identity, so `resolver.imports` reads the same result\n * across the ts, zod, and faker plugins instead of re-scanning the same schema per plugin.\n *\n * Only refs that carry a `$ref` pointer count, so a synthesized ref pointing at a sibling in the\n * same file (a union member created by name) is left out. That leaves exactly the set\n * `resolver.imports` emits. Refs inside `conditionals`, `contains` and `content` count only with\n * `checks: true`, for a plugin that prints those checks.\n *\n * @example\n * ```ts\n * collectImportedRefNames(petSchema)\n * // ['Category', 'Tag']\n * ```\n */\nexport function collectImportedRefNames(node: SchemaNode, { checks = false }: { checks?: boolean } = {}): ReadonlyArray<string> {\n return memo(checks ? importedCheckRefNames : importedRefNames, node, () => [\n ...new Set(collectRefEntries(node, { checks }).flatMap((entry) => (entry.hasPointer ? [entry.name] : []))),\n ])\n}\n\n/**\n * Collects the names of all top-level schemas transitively used by a set of operations or schemas.\n * A schema root counts the schemas it references, not itself.\n *\n * An operation uses a schema when its parameters, request body, or responses reference it, directly\n * or through other named schemas. Once a name is added to the result it is not revisited, so\n * reference cycles terminate.\n *\n * Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.\n *\n * @example Only generate schemas referenced by included operations\n * ```ts\n * const includedOps = operations.filter((op) => resolver.default.options(op, { options, include }) !== null)\n * const allowed = collectUsedSchemaNames(includedOps, schemas)\n *\n * for (const schema of schemas) {\n * if (schema.name && !allowed.has(schema.name)) continue\n * // generate schema\n * }\n * ```\n */\nexport function collectUsedSchemaNames(roots: ReadonlyArray<OperationNode | SchemaNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {\n const schemaMap = new Map<string, SchemaNode>()\n for (const schema of schemas) {\n if (schema.name) schemaMap.set(schema.name, schema)\n }\n\n const result = new Set<string>()\n\n function visitSchema(schema: SchemaNode): void {\n for (const name of collectSchemaRefs(schema)) {\n if (!result.has(name)) {\n result.add(name)\n const namedSchema = schemaMap.get(name)\n if (namedSchema) visitSchema(namedSchema)\n }\n }\n }\n\n for (const root of roots) {\n for (const schema of collect<SchemaNode>(root, { depth: 'shallow', schema: (node) => node })) {\n visitSchema(schema)\n }\n }\n\n return result\n}\n\n/**\n * Finds every schema that takes part in a circular dependency chain, including direct self-loops.\n *\n * Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so\n * the generated code does not recurse forever. Refs are followed by name only, so the cost stays linear in the size\n * of the schema graph. The result lists names in the order of `schemas`.\n *\n * @note Call this once on the full graph, then check a schema's `ast.graph.getRefNames` against the result.\n */\nexport function findCircularSchemas(schemas: ReadonlyArray<SchemaNode>): Set<string> {\n const graph = new Map<string, ReadonlySet<string>>()\n for (const schema of schemas) {\n if (schema.name) graph.set(schema.name, collectSchemaRefs(schema))\n }\n\n const circular = createCycleFinder(graph).find()\n\n return new Set([...graph.keys()].filter((name) => circular.has(name)))\n}\n\ntype Frame = { name: string; edges: Iterator<string> }\n\n// Tarjan's algorithm with an explicit stack, so a long ref chain cannot overflow the call stack.\n// A name is circular when its component has more than one member or it references itself.\nclass CycleFinder {\n readonly #graph: ReadonlyMap<string, ReadonlySet<string>>\n readonly #index = new Map<string, number>()\n readonly #lowLink = new Map<string, number>()\n readonly #onStack = new Set<string>()\n readonly #componentStack: Array<string> = []\n readonly #frames: Array<Frame> = []\n readonly #circular = new Set<string>()\n\n constructor(graph: ReadonlyMap<string, ReadonlySet<string>>) {\n this.#graph = graph\n }\n\n find(): Set<string> {\n for (const root of this.#graph.keys()) {\n if (!this.#index.has(root)) this.#walk(root)\n }\n return this.#circular\n }\n\n #walk(root: string): void {\n this.#enter(root)\n while (this.#frames.length > 0) {\n const frame = this.#frames[this.#frames.length - 1]!\n const step = frame.edges.next()\n if (step.done) {\n this.#leave(frame)\n continue\n }\n this.#follow({ frame, next: step.value })\n }\n }\n\n #enter(name: string): void {\n this.#index.set(name, this.#index.size)\n this.#lowLink.set(name, this.#index.size - 1)\n this.#componentStack.push(name)\n this.#onStack.add(name)\n this.#frames.push({ name, edges: this.#graph.get(name)!.values() })\n }\n\n #follow({ frame, next }: { frame: Frame; next: string }): void {\n if (!this.#graph.has(next)) return\n if (!this.#index.has(next)) return this.#enter(next)\n if (this.#onStack.has(next)) this.#lower({ name: frame.name, value: this.#index.get(next)! })\n }\n\n #lower({ name, value }: { name: string; value: number }): void {\n this.#lowLink.set(name, Math.min(this.#lowLink.get(name)!, value))\n }\n\n #leave(frame: Frame): void {\n this.#frames.pop()\n const parent = this.#frames[this.#frames.length - 1]\n if (parent) this.#lower({ name: parent.name, value: this.#lowLink.get(frame.name)! })\n if (this.#lowLink.get(frame.name) === this.#index.get(frame.name)) this.#closeComponent(frame.name)\n }\n\n #closeComponent(root: string): void {\n const component: Array<string> = []\n let member: string\n do {\n member = this.#componentStack.pop()!\n this.#onStack.delete(member)\n component.push(member)\n } while (member !== root)\n\n if (component.length > 1 || this.#graph.get(root)!.has(root)) {\n for (const name of component) this.#circular.add(name)\n }\n }\n}\n\nfunction createCycleFinder(graph: ReadonlyMap<string, ReadonlySet<string>>): CycleFinder {\n return new CycleFinder(graph)\n}\n","export {\n collectImportedRefNames as getImportedNames,\n collectSchemaRefs as getRefNames,\n collectUsedSchemaNames as getUsedNames,\n findCircularSchemas as getCircularNames,\n} from '../utils/schemaGraph.ts'\n"],"mappings":";;;;;;AAUA,MAAM,aAAa,KAAK,OAA4C,EAAE,MAAM,KAAK,CAAC;AAClF,MAAM,kBAAkB,KAAK,OAA4C,EAAE,MAAM,KAAK,CAAC;AACvF,MAAM,aAAa,KAAK,OAAwC,EAAE,MAAM,KAAK,CAAC;AAC9E,MAAM,mBAAmB,KAAK,OAA0C,EAAE,MAAM,KAAK,CAAC;AACtF,MAAM,wBAAwB,KAAK,OAA0C,EAAE,MAAM,KAAK,CAAC;;;;;;AAO3F,SAAS,kBAAkB,MAAkB,EAAE,UAAwD;CACrG,OAAO,KAAK,SAAS,kBAAkB,YAAY,YAAY;EAC7D,MAAM,UAA2B,CAAC;E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,YAAY,KAAK;AAC9B"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const require_rolldown_runtime = require("./rolldown-runtime-BV8xUvPy.cjs");
|
|
2
2
|
const require_promise = require("./promise-DMQPWp6M.cjs");
|
|
3
3
|
const require_constants = require("./constants-BCRkFAiy.cjs");
|
|
4
|
-
const require_visitor = require("./visitor-
|
|
4
|
+
const require_visitor = require("./visitor-DT712cxr.cjs");
|
|
5
5
|
const require_refs = require("./refs-CJZBa9hL.cjs");
|
|
6
6
|
// src/utils/schemaGraph.ts
|
|
7
7
|
const refEntries = require_promise.memo.create({ weak: true });
|
|
@@ -107,27 +107,90 @@ function collectUsedSchemaNames(roots, schemas) {
|
|
|
107
107
|
* Finds every schema that takes part in a circular dependency chain, including direct self-loops.
|
|
108
108
|
*
|
|
109
109
|
* Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so
|
|
110
|
-
* the generated code does not recurse forever. Refs are followed by name only, so the
|
|
111
|
-
*
|
|
110
|
+
* the generated code does not recurse forever. Refs are followed by name only, so the cost stays linear in the size
|
|
111
|
+
* of the schema graph. The result lists names in the order of `schemas`.
|
|
112
112
|
*
|
|
113
113
|
* @note Call this once on the full graph, then check a schema's `ast.graph.getRefNames` against the result.
|
|
114
114
|
*/
|
|
115
115
|
function findCircularSchemas(schemas) {
|
|
116
116
|
const graph = /* @__PURE__ */ new Map();
|
|
117
117
|
for (const schema of schemas) if (schema.name) graph.set(schema.name, collectSchemaRefs(schema));
|
|
118
|
-
|
|
118
|
+
const circular = createCycleFinder(graph).find();
|
|
119
|
+
return new Set([...graph.keys()].filter((name) => circular.has(name)));
|
|
119
120
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
121
|
+
var CycleFinder = class {
|
|
122
|
+
#graph;
|
|
123
|
+
#index = /* @__PURE__ */ new Map();
|
|
124
|
+
#lowLink = /* @__PURE__ */ new Map();
|
|
125
|
+
#onStack = /* @__PURE__ */ new Set();
|
|
126
|
+
#componentStack = [];
|
|
127
|
+
#frames = [];
|
|
128
|
+
#circular = /* @__PURE__ */ new Set();
|
|
129
|
+
constructor(graph) {
|
|
130
|
+
this.#graph = graph;
|
|
129
131
|
}
|
|
130
|
-
|
|
132
|
+
find() {
|
|
133
|
+
for (const root of this.#graph.keys()) if (!this.#index.has(root)) this.#walk(root);
|
|
134
|
+
return this.#circular;
|
|
135
|
+
}
|
|
136
|
+
#walk(root) {
|
|
137
|
+
this.#enter(root);
|
|
138
|
+
while (this.#frames.length > 0) {
|
|
139
|
+
const frame = this.#frames[this.#frames.length - 1];
|
|
140
|
+
const step = frame.edges.next();
|
|
141
|
+
if (step.done) {
|
|
142
|
+
this.#leave(frame);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
this.#follow({
|
|
146
|
+
frame,
|
|
147
|
+
next: step.value
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
#enter(name) {
|
|
152
|
+
this.#index.set(name, this.#index.size);
|
|
153
|
+
this.#lowLink.set(name, this.#index.size - 1);
|
|
154
|
+
this.#componentStack.push(name);
|
|
155
|
+
this.#onStack.add(name);
|
|
156
|
+
this.#frames.push({
|
|
157
|
+
name,
|
|
158
|
+
edges: this.#graph.get(name).values()
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
#follow({ frame, next }) {
|
|
162
|
+
if (!this.#graph.has(next)) return;
|
|
163
|
+
if (!this.#index.has(next)) return this.#enter(next);
|
|
164
|
+
if (this.#onStack.has(next)) this.#lower({
|
|
165
|
+
name: frame.name,
|
|
166
|
+
value: this.#index.get(next)
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
#lower({ name, value }) {
|
|
170
|
+
this.#lowLink.set(name, Math.min(this.#lowLink.get(name), value));
|
|
171
|
+
}
|
|
172
|
+
#leave(frame) {
|
|
173
|
+
this.#frames.pop();
|
|
174
|
+
const parent = this.#frames[this.#frames.length - 1];
|
|
175
|
+
if (parent) this.#lower({
|
|
176
|
+
name: parent.name,
|
|
177
|
+
value: this.#lowLink.get(frame.name)
|
|
178
|
+
});
|
|
179
|
+
if (this.#lowLink.get(frame.name) === this.#index.get(frame.name)) this.#closeComponent(frame.name);
|
|
180
|
+
}
|
|
181
|
+
#closeComponent(root) {
|
|
182
|
+
const component = [];
|
|
183
|
+
let member;
|
|
184
|
+
do {
|
|
185
|
+
member = this.#componentStack.pop();
|
|
186
|
+
this.#onStack.delete(member);
|
|
187
|
+
component.push(member);
|
|
188
|
+
} while (member !== root);
|
|
189
|
+
if (component.length > 1 || this.#graph.get(root).has(root)) for (const name of component) this.#circular.add(name);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
function createCycleFinder(graph) {
|
|
193
|
+
return new CycleFinder(graph);
|
|
131
194
|
}
|
|
132
195
|
// src/graph/index.ts
|
|
133
196
|
var graph_exports = /* @__PURE__ */ require_rolldown_runtime.__exportAll({
|
|
@@ -167,4 +230,4 @@ Object.defineProperty(exports, "graph_exports", {
|
|
|
167
230
|
}
|
|
168
231
|
});
|
|
169
232
|
|
|
170
|
-
//# sourceMappingURL=graph-
|
|
233
|
+
//# sourceMappingURL=graph-CymJtC1M.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph-CymJtC1M.cjs","names":["memo","nodeKinds","resolveRefName","visitChildNodes","collect"],"sources":["../src/utils/schemaGraph.ts","../src/graph/index.ts"],"sourcesContent":["import { memo } from '@internals/utils'\nimport { nodeKinds } from '../constants.ts'\nimport type { Node } from '../nodes/index.ts'\nimport type { OperationNode } from '../nodes/operation.ts'\nimport type { SchemaNode } from '../nodes/schema.ts'\nimport { collect, visitChildNodes } from '../visitor.ts'\nimport { resolveRefName } from './refs.ts'\n\ntype RefEntry = { name: string; hasPointer: boolean }\n\nconst refEntries = memo.create<SchemaNode, ReadonlyArray<RefEntry>>({ weak: true })\nconst checkRefEntries = memo.create<SchemaNode, ReadonlyArray<RefEntry>>({ weak: true })\nconst schemaRefs = memo.create<SchemaNode, ReadonlySet<string>>({ weak: true })\nconst importedRefNames = memo.create<SchemaNode, ReadonlyArray<string>>({ weak: true })\nconst importedCheckRefNames = memo.create<SchemaNode, ReadonlyArray<string>>({ weak: true })\n\n/**\n * Walks a node's subtree once and lists every ref it holds, in traversal order, as the resolved\n * name plus whether the ref carries a `$ref` pointer. Memoized per node and per `checks` mode, so\n * each helper below scans a schema once.\n */\nfunction collectRefEntries(node: SchemaNode, { checks }: { checks: boolean }): ReadonlyArray<RefEntry> {\n return memo(checks ? checkRefEntries : refEntries, node, () => {\n const entries: Array<RefEntry> = []\n const walk = (current: Node): boolean => {\n if (current.kind === nodeKinds.schema && current.type === 'ref') {\n const name = resolveRefName(current)\n if (name) entries.push({ name, hasPointer: Boolean(current.ref) })\n }\n visitChildNodes(current, walk, { checks })\n return false\n }\n walk(node)\n return entries\n })\n}\n\n/**\n * Collects the names of every schema a node's subtree references, resolved with `ast.ref.getName`.\n * Each ref contributes its name only, so the schema it points to is never traversed. Memoized by\n * node identity, so repeated calls on one schema scan it once.\n *\n * @example\n * ```ts\n * collectSchemaRefs(petSchema)\n * // Set { 'Category', 'Tag' }\n * ```\n */\nexport function collectSchemaRefs(node: SchemaNode): ReadonlySet<string> {\n return memo(schemaRefs, node, () => new Set(collectRefEntries(node, { checks: true }).map((entry) => entry.name)))\n}\n\n/**\n * Collects the de-duplicated target names of every pointer-carrying ref in a node's subtree, in\n * first-occurrence order. Memoized by node identity, so `resolver.imports` reads the same result\n * across the ts, zod, and faker plugins instead of re-scanning the same schema per plugin.\n *\n * Only refs that carry a `$ref` pointer count, so a synthesized ref pointing at a sibling in the\n * same file (a union member created by name) is left out. That leaves exactly the set\n * `resolver.imports` emits. Refs inside `conditionals`, `contains` and `content` count only with\n * `checks: true`, for a plugin that prints those checks.\n *\n * @example\n * ```ts\n * collectImportedRefNames(petSchema)\n * // ['Category', 'Tag']\n * ```\n */\nexport function collectImportedRefNames(node: SchemaNode, { checks = false }: { checks?: boolean } = {}): ReadonlyArray<string> {\n return memo(checks ? importedCheckRefNames : importedRefNames, node, () => [\n ...new Set(collectRefEntries(node, { checks }).flatMap((entry) => (entry.hasPointer ? [entry.name] : []))),\n ])\n}\n\n/**\n * Collects the names of all top-level schemas transitively used by a set of operations or schemas.\n * A schema root counts the schemas it references, not itself.\n *\n * An operation uses a schema when its parameters, request body, or responses reference it, directly\n * or through other named schemas. Once a name is added to the result it is not revisited, so\n * reference cycles terminate.\n *\n * Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.\n *\n * @example Only generate schemas referenced by included operations\n * ```ts\n * const includedOps = operations.filter((op) => resolver.default.options(op, { options, include }) !== null)\n * const allowed = collectUsedSchemaNames(includedOps, schemas)\n *\n * for (const schema of schemas) {\n * if (schema.name && !allowed.has(schema.name)) continue\n * // generate schema\n * }\n * ```\n */\nexport function collectUsedSchemaNames(roots: ReadonlyArray<OperationNode | SchemaNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {\n const schemaMap = new Map<string, SchemaNode>()\n for (const schema of schemas) {\n if (schema.name) schemaMap.set(schema.name, schema)\n }\n\n const result = new Set<string>()\n\n function visitSchema(schema: SchemaNode): void {\n for (const name of collectSchemaRefs(schema)) {\n if (!result.has(name)) {\n result.add(name)\n const namedSchema = schemaMap.get(name)\n if (namedSchema) visitSchema(namedSchema)\n }\n }\n }\n\n for (const root of roots) {\n for (const schema of collect<SchemaNode>(root, { depth: 'shallow', schema: (node) => node })) {\n visitSchema(schema)\n }\n }\n\n return result\n}\n\n/**\n * Finds every schema that takes part in a circular dependency chain, including direct self-loops.\n *\n * Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so\n * the generated code does not recurse forever. Refs are followed by name only, so the cost stays linear in the size\n * of the schema graph. The result lists names in the order of `schemas`.\n *\n * @note Call this once on the full graph, then check a schema's `ast.graph.getRefNames` against the result.\n */\nexport function findCircularSchemas(schemas: ReadonlyArray<SchemaNode>): Set<string> {\n const graph = new Map<string, ReadonlySet<string>>()\n for (const schema of schemas) {\n if (schema.name) graph.set(schema.name, collectSchemaRefs(schema))\n }\n\n const circular = createCycleFinder(graph).find()\n\n return new Set([...graph.keys()].filter((name) => circular.has(name)))\n}\n\ntype Frame = { name: string; edges: Iterator<string> }\n\n// Tarjan's algorithm with an explicit stack, so a long ref chain cannot overflow the call stack.\n// A name is circular when its component has more than one member or it references itself.\nclass CycleFinder {\n readonly #graph: ReadonlyMap<string, ReadonlySet<string>>\n readonly #index = new Map<string, number>()\n readonly #lowLink = new Map<string, number>()\n readonly #onStack = new Set<string>()\n readonly #componentStack: Array<string> = []\n readonly #frames: Array<Frame> = []\n readonly #circular = new Set<string>()\n\n constructor(graph: ReadonlyMap<string, ReadonlySet<string>>) {\n this.#graph = graph\n }\n\n find(): Set<string> {\n for (const root of this.#graph.keys()) {\n if (!this.#index.has(root)) this.#walk(root)\n }\n return this.#circular\n }\n\n #walk(root: string): void {\n this.#enter(root)\n while (this.#frames.length > 0) {\n const frame = this.#frames[this.#frames.length - 1]!\n const step = frame.edges.next()\n if (step.done) {\n this.#leave(frame)\n continue\n }\n this.#follow({ frame, next: step.value })\n }\n }\n\n #enter(name: string): void {\n this.#index.set(name, this.#index.size)\n this.#lowLink.set(name, this.#index.size - 1)\n this.#componentStack.push(name)\n this.#onStack.add(name)\n this.#frames.push({ name, edges: this.#graph.get(name)!.values() })\n }\n\n #follow({ frame, next }: { frame: Frame; next: string }): void {\n if (!this.#graph.has(next)) return\n if (!this.#index.has(next)) return this.#enter(next)\n if (this.#onStack.has(next)) this.#lower({ name: frame.name, value: this.#index.get(next)! })\n }\n\n #lower({ name, value }: { name: string; value: number }): void {\n this.#lowLink.set(name, Math.min(this.#lowLink.get(name)!, value))\n }\n\n #leave(frame: Frame): void {\n this.#frames.pop()\n const parent = this.#frames[this.#frames.length - 1]\n if (parent) this.#lower({ name: parent.name, value: this.#lowLink.get(frame.name)! })\n if (this.#lowLink.get(frame.name) === this.#index.get(frame.name)) this.#closeComponent(frame.name)\n }\n\n #closeComponent(root: string): void {\n const component: Array<string> = []\n let member: string\n do {\n member = this.#componentStack.pop()!\n this.#onStack.delete(member)\n component.push(member)\n } while (member !== root)\n\n if (component.length > 1 || this.#graph.get(root)!.has(root)) {\n for (const name of component) this.#circular.add(name)\n }\n }\n}\n\nfunction createCycleFinder(graph: ReadonlyMap<string, ReadonlySet<string>>): CycleFinder {\n return new CycleFinder(graph)\n}\n","export {\n collectImportedRefNames as getImportedNames,\n collectSchemaRefs as getRefNames,\n collectUsedSchemaNames as getUsedNames,\n findCircularSchemas as getCircularNames,\n} from '../utils/schemaGraph.ts'\n"],"mappings":";;;;;;AAUA,MAAM,aAAaA,gBAAAA,KAAK,OAA4C,EAAE,MAAM,KAAK,CAAC;AAClF,MAAM,kBAAkBA,gBAAAA,KAAK,OAA4C,EAAE,MAAM,KAAK,CAAC;AACvF,MAAM,aAAaA,gBAAAA,KAAK,OAAwC,EAAE,MAAM,KAAK,CAAC;AAC9E,MAAM,mBAAmBA,gBAAAA,KAAK,OAA0C,EAAE,MAAM,KAAK,CAAC;AACtF,MAAM,wBAAwBA,gBAAAA,KAAK,OAA0C,EAAE,MAAM,KAAK,CAAC;;;;;;AAO3F,SAAS,kBAAkB,MAAkB,EAAE,UAAwD;CACrG,OAAOA,gBAAAA,KAAK,SAAS,kBAAkB,YAAY,YAAY;EAC7D,MAAM,UAA2B,CAAC;EAClC,MAAM,QAAQ,YAA2B;GACvC,IAAI,QAAQ,SAASC,kBAAAA,UAAU,UAAU,QAAQ,SAAS,OAAO;IAC/D,MAAM,OAAOC,aAAAA,eAAe,OAAO;IACnC,IAAI,MAAM,QAAQ,KAAK;KAAE;KAAM,YAAY,QAAQ,QAAQ,GAAG;IAAE,CAAC;GACnE;GACAC,gBAAAA,gBAAgB,SAAS,MAAM,EAAE,OAAO,CAAC;GACzC,OAAO;EACT;E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IAAI,CAAC,KAAK,OAAO,IAAI,IAAI,GAAG,OAAO,KAAK,OAAO,IAAI;EACnD,IAAI,KAAK,SAAS,IAAI,IAAI,GAAG,KAAK,OAAO;GAAE,MAAM,MAAM;GAAM,OAAO,KAAK,OAAO,IAAI,IAAI;EAAG,CAAC;CAC9F;CAEA,OAAO,EAAE,MAAM,SAAgD;EAC7D,KAAK,SAAS,IAAI,MAAM,KAAK,IAAI,KAAK,SAAS,IAAI,IAAI,GAAI,KAAK,CAAC;CACnE;CAEA,OAAO,OAAoB;EACzB,KAAK,QAAQ,IAAI;EACjB,MAAM,SAAS,KAAK,QAAQ,KAAK,QAAQ,SAAS;EAClD,IAAI,QAAQ,KAAK,OAAO;GAAE,MAAM,OAAO;GAAM,OAAO,KAAK,SAAS,IAAI,MAAM,IAAI;EAAG,CAAC;EACpF,IAAI,KAAK,SAAS,IAAI,MAAM,IAAI,MAAM,KAAK,OAAO,IAAI,MAAM,IAAI,GAAG,KAAK,gBAAgB,MAAM,IAAI;CACpG;CAEA,gBAAgB,MAAoB;EAClC,MAAM,YAA2B,CAAC;EAClC,IAAI;EACJ,GAAG;GACD,SAAS,KAAK,gBAAgB,IAAI;GAClC,KAAK,SAAS,OAAO,MAAM;GAC3B,UAAU,KAAK,MAAM;EACvB,SAAS,WAAW;EAEpB,IAAI,UAAU,SAAS,KAAK,KAAK,OAAO,IAAI,IAAI,CAAC,CAAE,IAAI,IAAI,GACzD,KAAK,MAAM,QAAQ,WAAW,KAAK,UAAU,IAAI,IAAI;CAEzD;AACF;AAEA,SAAS,kBAAkB,OAA8D;CACvF,OAAO,IAAI,YAAY,KAAK;AAC9B"}
|
package/dist/graph.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_graph = require("./graph-
|
|
2
|
+
const require_graph = require("./graph-CymJtC1M.cjs");
|
|
3
3
|
exports.getCircularNames = require_graph.findCircularSchemas;
|
|
4
4
|
exports.getImportedNames = require_graph.collectImportedRefNames;
|
|
5
5
|
exports.getRefNames = require_graph.collectSchemaRefs;
|
package/dist/graph.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as findCircularSchemas, i as collectUsedSchemaNames, n as collectImportedRefNames, r as collectSchemaRefs } from "./index-
|
|
1
|
+
import { a as findCircularSchemas, i as collectUsedSchemaNames, n as collectImportedRefNames, r as collectSchemaRefs } from "./index-BgmSdJKJ.cjs";
|
|
2
2
|
export { findCircularSchemas as getCircularNames, collectImportedRefNames as getImportedNames, collectSchemaRefs as getRefNames, collectUsedSchemaNames as getUsedNames };
|
package/dist/graph.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as findCircularSchemas, i as collectUsedSchemaNames, n as collectImportedRefNames, r as collectSchemaRefs } from "./index-
|
|
1
|
+
import { a as findCircularSchemas, i as collectUsedSchemaNames, n as collectImportedRefNames, r as collectSchemaRefs } from "./index-B4DY8ewx.js";
|
|
2
2
|
export { findCircularSchemas as getCircularNames, collectImportedRefNames as getImportedNames, collectSchemaRefs as getRefNames, collectUsedSchemaNames as getUsedNames };
|
package/dist/graph.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { a as getCircularNames, n as getImportedNames, r as getRefNames, i as getUsedNames } from "./graph-
|
|
1
|
+
export { a as getCircularNames, n as getImportedNames, r as getRefNames, i as getUsedNames } from "./graph-Cu2-NiKi.js";
|
|
@@ -56,8 +56,8 @@ declare function collectUsedSchemaNames(roots: ReadonlyArray<OperationNode | Sch
|
|
|
56
56
|
* Finds every schema that takes part in a circular dependency chain, including direct self-loops.
|
|
57
57
|
*
|
|
58
58
|
* Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so
|
|
59
|
-
* the generated code does not recurse forever. Refs are followed by name only, so the
|
|
60
|
-
*
|
|
59
|
+
* the generated code does not recurse forever. Refs are followed by name only, so the cost stays linear in the size
|
|
60
|
+
* of the schema graph. The result lists names in the order of `schemas`.
|
|
61
61
|
*
|
|
62
62
|
* @note Call this once on the full graph, then check a schema's `ast.graph.getRefNames` against the result.
|
|
63
63
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as createArrowFunction, D as createJsx, E as createFunction, O as createText, S as TypeNode, T as createConst, _ as ConstNode, a as ImportNode, b as JsxNode, c as Space, d as
|
|
1
|
+
import { C as createArrowFunction, D as createJsx, E as createFunction, O as createText, S as TypeNode, T as createConst, _ as ConstNode, a as ImportNode, b as JsxNode, c as Space, d as createSource, f as createExport, h as BreakNode, i as ImportInput, k as createType, l as UserFileNode, m as ArrowFunctionNode, n as ExportNode, o as ModuleName, p as createImport, r as FileNode, s as SourceNode, t as ExportInput, u as createFile, v as FunctionNode, w as createBreak, x as TextNode } from "./file-VRwu7Set.js";
|
|
2
2
|
import { $ as createSchema, D as InputNode, K as SchemaNode, _ as createParameter, a as OperationsNode, d as createResponse, et as PropertyNode, f as RequestBodyNode, h as ParameterNode, i as OperationNode, j as createInput, l as ResponseNode, o as createOperation, p as createRequestBody, s as createOperations, tt as createProperty, v as ContentNode, x as createContent } from "./operation-DJ7F0cFp.js";
|
|
3
3
|
declare namespace index_d_exports {
|
|
4
4
|
export { ExportInput, ImportInput, ModuleName, Node, Space, UserFileNode, createArrowFunction, createBreak, createConst, createContent, createExport, createFile, createFunction, createImport, createInput, createJsx, createOperation, createOperations, createParameter, createProperty, createRequestBody, createResponse, createSchema, createSource, createText, createType };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { K as SchemaNode, Y as StringSchemaNode, et as PropertyNode } from "./operation-DJ7F0cFp.js";
|
|
2
2
|
import { a as schemaTypes } from "./base-A-xF4pUL.js";
|
|
3
3
|
import { n as narrowSchema } from "./guards-CTqHwyNs.js";
|
|
4
|
-
import { o as findSchema } from "./visitor-
|
|
4
|
+
import { o as findSchema } from "./visitor-BWHpwRgc.js";
|
|
5
5
|
/**
|
|
6
6
|
* Generic JSON Schema optionality: a non-required field is optional, and a
|
|
7
7
|
* non-required nullable field is nullish.
|
|
@@ -56,8 +56,8 @@ declare function collectUsedSchemaNames(roots: ReadonlyArray<OperationNode | Sch
|
|
|
56
56
|
* Finds every schema that takes part in a circular dependency chain, including direct self-loops.
|
|
57
57
|
*
|
|
58
58
|
* Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so
|
|
59
|
-
* the generated code does not recurse forever. Refs are followed by name only, so the
|
|
60
|
-
*
|
|
59
|
+
* the generated code does not recurse forever. Refs are followed by name only, so the cost stays linear in the size
|
|
60
|
+
* of the schema graph. The result lists names in the order of `schemas`.
|
|
61
61
|
*
|
|
62
62
|
* @note Call this once on the full graph, then check a schema's `ast.graph.getRefNames` against the result.
|
|
63
63
|
*/
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { t as Node } from "./index-
|
|
2
|
-
import { i as VisitorKey, n as VisitorContext, r as VisitorDepth, t as Visitor } from "./visitor-
|
|
1
|
+
import { t as Node } from "./index-Dbtc7izw.cjs";
|
|
2
|
+
import { i as VisitorKey, n as VisitorContext, r as VisitorDepth, t as Visitor } from "./visitor-CPlsMCPZ.cjs";
|
|
3
3
|
/**
|
|
4
4
|
* Ordering hint shared by macros and plugins. `pre` runs before unmarked items, `post` after,
|
|
5
5
|
* and `undefined` keeps declaration order.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { i as nodeKinds } from "./base-CyJhBRMi.cjs";
|
|
2
|
-
import { g as CodeNode } from "./file-
|
|
3
|
-
import { a as collect, s as transform } from "./visitor-
|
|
2
|
+
import { g as CodeNode } from "./file-CLh8vdcR.cjs";
|
|
3
|
+
import { a as collect, s as transform } from "./visitor-CPlsMCPZ.cjs";
|
|
4
4
|
/**
|
|
5
5
|
* Extracts all string content from a `CodeNode` tree recursively.
|
|
6
6
|
* Collects the text fields and nested node content, to build the source string for import filtering.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { t as Node } from "./index-
|
|
2
|
-
import { i as VisitorKey, n as VisitorContext, r as VisitorDepth, t as Visitor } from "./visitor-
|
|
1
|
+
import { t as Node } from "./index-BEvWuiSq.js";
|
|
2
|
+
import { i as VisitorKey, n as VisitorContext, r as VisitorDepth, t as Visitor } from "./visitor-BWHpwRgc.js";
|
|
3
3
|
/**
|
|
4
4
|
* Ordering hint shared by macros and plugins. `pre` runs before unmarked items, `post` after,
|
|
5
5
|
* and `undefined` keeps declaration order.
|