@webpieces/api-doc-model 0.4.829 → 0.4.831

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FieldDecorators.js","sourceRoot":"","sources":["../../../../../../packages/docs/api-doc-model/src/extract/FieldDecorators.ts"],"names":[],"mappings":";;;;AAAA,uDAAiC;AACjC,oDAAoD;AACpD,8CAA2C;AAC3C,mEAAgE;AAChE,qDAAkD;AAElD,mGAAmG;AACnG,MAAM,aAAa,GAAG,iBAAK,CAAC,IAAI,CAAC;AACjC,MAAM,aAAa,GAAG,iBAAK,CAAC,IAAI,CAAC;AAEjC;;;;GAIG;AACH,MAAa,eAAe;IACxB;;;;OAIG;IACH,6EAA6E;IAC7E,MAAM,CAAC,2BAA2B,CAC9B,MAAe,EACf,IAAY,EACZ,IAAa,EACb,GAAuB,EACvB,GAAuB;QAEvB,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACzC,OAAO;QACX,CAAC;QACD,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,OAAO,IAAI,IAAI,CAAC,KAAM,CAAC,SAAS,EAAE,CAAC,CAAC;QACvF,IAAI,OAAO,EAAE,CAAC;YACV,OAAO;QACX,CAAC;QACD,MAAM,IAAI,6CAAqB,CAC3B,IAAI,aAAa,KAAK,aAAa,0BAA0B,IAAI,GAAG,EACpE,+BAAc,CAAC,EAAE,CAAC,MAAM,CAAC,EACzB,iEAAiE,CACpE,CAAC;IACN,CAAC;IAED,yFAAyF;IACzF,6EAA6E;IAC7E,MAAM,CAAC,WAAW,CAAC,IAAa;QAC5B,IAAI,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;YACxB,OAAO,iBAAO,CAAC,KAAK,CAAC,IAAI,CAAC,KAAM,CAAC,SAAS,EAAE,CAAC,CAAC;QAClD,CA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,0CAwGC","sourcesContent":["import * as ts from 'typescript';\nimport { WpMax, WpMin } from '@webpieces/core-util';\nimport { TypeRef } from '../model/TypeRef';\nimport { ApiDocExtractionError } from './ApiDocExtractionError';\nimport { SourceLocation } from './SourceLocation';\n\n/** Named from the REAL SYMBOLS, so a rename in `core-util` fails to compile here (issue #1001). */\nconst MIN_DECORATOR = WpMin.name;\nconst MAX_DECORATOR = WpMax.name;\n\n/**\n * What a DTO PROPERTY's decorators and declared type say beyond its type: `@WpInt()`, `@WpMin` /\n * `@WpMax`, and whether the declaration admits `undefined` or `null`. Read by {@link TypeResolver}\n * for every field it resolves; split out of it so each file stays one concern.\n */\nexport class FieldDecorators {\n /**\n * `@WpMin` / `@WpMax` on a non-numeric field is a BUILD FAILURE, not a warning. A minimum on a\n * string is not something a renderer can emit sensibly, and a document that silently dropped it\n * would publish a contract weaker than the one its author wrote down.\n */\n // webpieces-disable no-function-outside-class -- static helper of this class\n static assertNumericConstraintsFit(\n member: ts.Node,\n name: string,\n type: TypeRef,\n min: number | undefined,\n max: number | undefined,\n ): void {\n if (min === undefined && max === undefined) {\n return;\n }\n const numeric = type.isNumeric() || (type.kind === 'array' && type.items!.isNumeric());\n if (numeric) {\n return;\n }\n throw new ApiDocExtractionError(\n `@${MIN_DECORATOR}/@${MAX_DECORATOR} on non-numeric field '${name}'`,\n SourceLocation.of(member),\n 'Put the constraint on a `number` / `Integer` field, or drop it.',\n );\n }\n\n /** `@WpInt()` decorates the FIELD, so the integer flag is pushed onto the right leaf. */\n // webpieces-disable no-function-outside-class -- static helper of this class\n static markInteger(type: TypeRef): TypeRef {\n if (type.kind === 'array') {\n return TypeRef.array(type.items!.asInteger());\n }\n if (type.kind === 'openMap') {\n return TypeRef.openMap(type.values!.asInteger());\n }\n return type.asInteger();\n }\n\n // webpieces-disable no-function-outside-class -- static helper of this class\n static declaresUndefined(node: ts.TypeNode): boolean {\n return (\n ts.isUnionTypeNode(node) &&\n node.types.some((t: ts.TypeNode) => t.kind === ts.SyntaxKind.UndefinedKeyword)\n );\n }\n\n // webpieces-disable no-function-outside-class -- static helper of this class\n static declaresNull(node: ts.TypeNode): boolean {\n return (\n ts.isUnionTypeNode(node) &&\n node.types.some(\n (t: ts.TypeNode) =>\n t.kind === ts.SyntaxKind.NullKeyword ||\n (ts.isLiteralTypeNode(t) && t.literal.kind === ts.SyntaxKind.NullKeyword),\n )\n );\n }\n\n // webpieces-disable no-function-outside-class -- static helper of this class\n static hasDecorator(node: ts.Node, decoratorName: string): boolean {\n return FieldDecorators.decoratorCall(node, decoratorName) !== undefined;\n }\n\n // webpieces-disable no-function-outside-class -- static helper of this class\n static decoratorCall(node: ts.Node, decoratorName: string): ts.CallExpression | undefined {\n const decorators = ts.canHaveDecorators(node) ? (ts.getDecorators(node) ?? []) : [];\n for (const decorator of decorators) {\n const call = decorator.expression;\n if (\n ts.isCallExpression(call) &&\n ts.isIdentifier(call.expression) &&\n call.expression.text === decoratorName\n ) {\n return call;\n }\n }\n return undefined;\n }\n\n // webpieces-disable no-function-outside-class -- static helper of this class\n static numericArgument(node: ts.Node, decoratorName: string): number | undefined {\n const call = FieldDecorators.decoratorCall(node, decoratorName);\n const argument = call?.arguments[0];\n if (argument === undefined) {\n return undefined;\n }\n if (ts.isNumericLiteral(argument)) {\n return Number(argument.text);\n }\n if (\n ts.isPrefixUnaryExpression(argument) &&\n argument.operator === ts.SyntaxKind.MinusToken &&\n ts.isNumericLiteral(argument.operand)\n ) {\n return -Number(argument.operand.text);\n }\n throw new ApiDocExtractionError(\n `@${decoratorName} argument is not a numeric literal: '${argument.getText()}'`,\n SourceLocation.of(argument),\n 'Write the bound as a numeric literal. A value only known at runtime cannot be published.',\n );\n }\n}\n"]}
@@ -0,0 +1,43 @@
1
+ /** The npm package a source file belongs to: the nearest `package.json` above it. Data-only. */
2
+ export declare class DeclaringPackage {
3
+ /** `name` from that `package.json`, e.g. `@myorg/lang-api-dtos`. */
4
+ readonly name: string;
5
+ /** `version` from that `package.json`; the empty string when it states none. */
6
+ readonly version: string;
7
+ /** The absolute directory holding that `package.json`. */
8
+ readonly directory: string;
9
+ constructor(
10
+ /** `name` from that `package.json`, e.g. `@myorg/lang-api-dtos`. */
11
+ name: string,
12
+ /** `version` from that `package.json`; the empty string when it states none. */
13
+ version: string,
14
+ /** The absolute directory holding that `package.json`. */
15
+ directory: string);
16
+ }
17
+ /**
18
+ * WHICH PACKAGE declares a type — the owner of its schema (#1058).
19
+ *
20
+ * A schema is defined in exactly one OpenAPI document: the one belonging to the package whose source
21
+ * declares the TypeScript type. That is what lets two different `LocalizedDescriptionsDto`s live in
22
+ * two packages without colliding, and what lets a contract document `$ref` a DTO library's schema
23
+ * instead of copying it. The answer is the nearest `package.json` above the declaring file — which is
24
+ * the same answer whether the compiler reached the file as workspace SOURCE through tsconfig `paths`
25
+ * or as a published `.d.ts` under `node_modules`.
26
+ *
27
+ * A file with no `package.json` above it belongs to no package; `of` returns `undefined` and the
28
+ * caller decides what that means (the OpenAPI generator refuses to reference it, the MCP renderer
29
+ * does not care).
30
+ *
31
+ * Cached per directory, because a contract reaches hundreds of declarations in a handful of files.
32
+ */
33
+ export declare class PackageOfFile {
34
+ private readonly byDirectory;
35
+ /** The package declaring `fileName`, or undefined when no `package.json` with a name is above it. */
36
+ of(fileName: string): DeclaringPackage | undefined;
37
+ private ofDirectory;
38
+ private parentOf;
39
+ /** A `package.json`, or the ONE error type of this package naming it when it is not JSON. */
40
+ private static parse;
41
+ /** The package whose `package.json` sits in `directory`, when it is one with a name. */
42
+ private read;
43
+ }
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PackageOfFile = exports.DeclaringPackage = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const fs = tslib_1.__importStar(require("node:fs"));
6
+ const path = tslib_1.__importStar(require("node:path"));
7
+ const ApiDocExtractionError_1 = require("./ApiDocExtractionError");
8
+ /** The npm package a source file belongs to: the nearest `package.json` above it. Data-only. */
9
+ class DeclaringPackage {
10
+ name;
11
+ version;
12
+ directory;
13
+ constructor(
14
+ /** `name` from that `package.json`, e.g. `@myorg/lang-api-dtos`. */
15
+ name,
16
+ /** `version` from that `package.json`; the empty string when it states none. */
17
+ version,
18
+ /** The absolute directory holding that `package.json`. */
19
+ directory) {
20
+ this.name = name;
21
+ this.version = version;
22
+ this.directory = directory;
23
+ }
24
+ }
25
+ exports.DeclaringPackage = DeclaringPackage;
26
+ /**
27
+ * WHICH PACKAGE declares a type — the owner of its schema (#1058).
28
+ *
29
+ * A schema is defined in exactly one OpenAPI document: the one belonging to the package whose source
30
+ * declares the TypeScript type. That is what lets two different `LocalizedDescriptionsDto`s live in
31
+ * two packages without colliding, and what lets a contract document `$ref` a DTO library's schema
32
+ * instead of copying it. The answer is the nearest `package.json` above the declaring file — which is
33
+ * the same answer whether the compiler reached the file as workspace SOURCE through tsconfig `paths`
34
+ * or as a published `.d.ts` under `node_modules`.
35
+ *
36
+ * A file with no `package.json` above it belongs to no package; `of` returns `undefined` and the
37
+ * caller decides what that means (the OpenAPI generator refuses to reference it, the MCP renderer
38
+ * does not care).
39
+ *
40
+ * Cached per directory, because a contract reaches hundreds of declarations in a handful of files.
41
+ */
42
+ class PackageOfFile {
43
+ byDirectory = new Map();
44
+ /** The package declaring `fileName`, or undefined when no `package.json` with a name is above it. */
45
+ of(fileName) {
46
+ return this.ofDirectory(path.dirname(path.resolve(fileName)));
47
+ }
48
+ ofDirectory(directory) {
49
+ if (this.byDirectory.has(directory)) {
50
+ return this.byDirectory.get(directory);
51
+ }
52
+ const found = this.read(directory) ?? this.parentOf(directory);
53
+ this.byDirectory.set(directory, found);
54
+ return found;
55
+ }
56
+ parentOf(directory) {
57
+ const parent = path.dirname(directory);
58
+ return parent === directory ? undefined : this.ofDirectory(parent);
59
+ }
60
+ /** A `package.json`, or the ONE error type of this package naming it when it is not JSON. */
61
+ // webpieces-disable no-function-outside-class -- private static reader of this class
62
+ static parse(file) {
63
+ // eslint-disable-next-line @webpieces/no-unmanaged-exceptions -- re-thrown as the ONE error type of this package, naming the file
64
+ try {
65
+ return JSON.parse(fs.readFileSync(file, 'utf8'));
66
+ }
67
+ catch (err) {
68
+ //const error = toError(err);
69
+ throw new ApiDocExtractionError_1.ApiDocExtractionError(`package.json is not valid JSON: ${err instanceof Error ? err.message : String(err)}`, file, 'Fix that package.json — the package a type is declared in is read from it.');
70
+ }
71
+ }
72
+ /** The package whose `package.json` sits in `directory`, when it is one with a name. */
73
+ read(directory) {
74
+ const file = path.join(directory, 'package.json');
75
+ if (!fs.existsSync(file)) {
76
+ return undefined;
77
+ }
78
+ const raw = PackageOfFile.parse(file);
79
+ if (typeof raw.name !== 'string' || raw.name.trim() === '') {
80
+ return undefined;
81
+ }
82
+ return new DeclaringPackage(raw.name, typeof raw.version === 'string' ? raw.version : '', directory);
83
+ }
84
+ }
85
+ exports.PackageOfFile = PackageOfFile;
86
+ //# sourceMappingURL=PackageOfFile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PackageOfFile.js","sourceRoot":"","sources":["../../../../../../packages/docs/api-doc-model/src/extract/PackageOfFile.ts"],"names":[],"mappings":";;;;AAAA,oDAA8B;AAC9B,wDAAkC;AAClC,mEAAgE;AAEhE,gGAAgG;AAChG,MAAa,gBAAgB;IAGZ;IAEA;IAEA;IANb;IACI,oEAAoE;IAC3D,IAAY;IACrB,gFAAgF;IACvE,OAAe;IACxB,0DAA0D;IACjD,SAAiB;QAJjB,SAAI,GAAJ,IAAI,CAAQ;QAEZ,YAAO,GAAP,OAAO,CAAQ;QAEf,cAAS,GAAT,SAAS,CAAQ;IAC3B,CAAC;CACP;AATD,4CASC;AAMD;;;;;;;;;;;;;;;GAeG;AACH,MAAa,aAAa;IACL,WAAW,GAAG,IAAI,GAAG,EAAwC,CAAC;IAE/E,qGAAqG;IACrG,EAAE,CAAC,QAAgB;QACf,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OA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c,CAAC,CAAC;QAClD,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;YACvB,OAAO,SAAS,CAAC;QACrB,CAAC;QACD,MAAM,GAAG,GAAG,aAAa,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACtC,IAAI,OAAO,GAAG,CAAC,IAAI,KAAK,QAAQ,IAAI,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;YACzD,OAAO,SAAS,CAAC;QACrB,CAAC;QACD,OAAO,IAAI,gBAAgB,CACvB,GAAG,CAAC,IAAI,EACR,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAClD,SAAS,CACZ,CAAC;IACN,CAAC;CACJ;AAtDD,sCAsDC","sourcesContent":["import * as fs from 'node:fs';\nimport * as path from 'node:path';\nimport { ApiDocExtractionError } from './ApiDocExtractionError';\n\n/** The npm package a source file belongs to: the nearest `package.json` above it. Data-only. */\nexport class DeclaringPackage {\n constructor(\n /** `name` from that `package.json`, e.g. `@myorg/lang-api-dtos`. */\n readonly name: string,\n /** `version` from that `package.json`; the empty string when it states none. */\n readonly version: string,\n /** The absolute directory holding that `package.json`. */\n readonly directory: string,\n ) {}\n}\n\n/** The two fields of a `package.json` this lookup reads — raw JSON, narrowed where they are read. */\n// webpieces-disable no-any-unknown -- a file off disk: each field is narrowed with typeof before use\ntype PackageJsonFields = { readonly name?: unknown; readonly version?: unknown };\n\n/**\n * WHICH PACKAGE declares a type — the owner of its schema (#1058).\n *\n * A schema is defined in exactly one OpenAPI document: the one belonging to the package whose source\n * declares the TypeScript type. That is what lets two different `LocalizedDescriptionsDto`s live in\n * two packages without colliding, and what lets a contract document `$ref` a DTO library's schema\n * instead of copying it. The answer is the nearest `package.json` above the declaring file — which is\n * the same answer whether the compiler reached the file as workspace SOURCE through tsconfig `paths`\n * or as a published `.d.ts` under `node_modules`.\n *\n * A file with no `package.json` above it belongs to no package; `of` returns `undefined` and the\n * caller decides what that means (the OpenAPI generator refuses to reference it, the MCP renderer\n * does not care).\n *\n * Cached per directory, because a contract reaches hundreds of declarations in a handful of files.\n */\nexport class PackageOfFile {\n private readonly byDirectory = new Map<string, DeclaringPackage | undefined>();\n\n /** The package declaring `fileName`, or undefined when no `package.json` with a name is above it. */\n of(fileName: string): DeclaringPackage | undefined {\n return this.ofDirectory(path.dirname(path.resolve(fileName)));\n }\n\n private ofDirectory(directory: string): DeclaringPackage | undefined {\n if (this.byDirectory.has(directory)) {\n return this.byDirectory.get(directory);\n }\n const found = this.read(directory) ?? this.parentOf(directory);\n this.byDirectory.set(directory, found);\n return found;\n }\n\n private parentOf(directory: string): DeclaringPackage | undefined {\n const parent = path.dirname(directory);\n return parent === directory ? undefined : this.ofDirectory(parent);\n }\n\n /** A `package.json`, or the ONE error type of this package naming it when it is not JSON. */\n // webpieces-disable no-function-outside-class -- private static reader of this class\n private static parse(file: string): PackageJsonFields {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions -- re-thrown as the ONE error type of this package, naming the file\n try {\n return JSON.parse(fs.readFileSync(file, 'utf8')) as PackageJsonFields;\n } catch (err: unknown) {\n //const error = toError(err);\n throw new ApiDocExtractionError(\n `package.json is not valid JSON: ${err instanceof Error ? err.message : String(err)}`,\n file,\n 'Fix that package.json — the package a type is declared in is read from it.',\n );\n }\n }\n\n /** The package whose `package.json` sits in `directory`, when it is one with a name. */\n private read(directory: string): DeclaringPackage | undefined {\n const file = path.join(directory, 'package.json');\n if (!fs.existsSync(file)) {\n return undefined;\n }\n const raw = PackageOfFile.parse(file);\n if (typeof raw.name !== 'string' || raw.name.trim() === '') {\n return undefined;\n }\n return new DeclaringPackage(\n raw.name,\n typeof raw.version === 'string' ? raw.version : '',\n directory,\n );\n }\n}\n"]}
@@ -0,0 +1,35 @@
1
+ import * as ts from 'typescript';
2
+ import { DocumentedField, DocumentedType, TypeNameCollision, UnionDiscriminator } from '../model/ApiDocModel';
3
+ import { TypeRef } from '../model/TypeRef';
4
+ import { PackageOfFile } from './PackageOfFile';
5
+ /**
6
+ * The named types a {@link TypeResolver} has registered, by KEY (#1058): the bare name for a type the
7
+ * HOME package declares, `<package>:<name>` for any other ({@link DocumentedType.keyOf}).
8
+ *
9
+ * It also remembers which DECLARATION claimed each key, so a second, different declaration arriving
10
+ * under it — two `DupDto`s in one package — is recorded as a {@link TypeNameCollision} rather than
11
+ * silently standing in for the first. The resolver still resolves it to the first, exactly as before,
12
+ * so the MCP projection is unchanged; an OpenAPI document refuses the model.
13
+ */
14
+ export declare class TypeRegistry {
15
+ private readonly packages;
16
+ private readonly homePackage;
17
+ readonly types: Map<string, DocumentedType>;
18
+ readonly collisions: TypeNameCollision[];
19
+ /** Named types already registered (or mid-registration) — the cycle stop for the NAMED case. */
20
+ readonly registered: Set<string>;
21
+ /** The DECLARATION each key was claimed by, so a second, different one is noticed. */
22
+ readonly claimedBy: Map<string, ts.Node>;
23
+ constructor(packages: PackageOfFile, homePackage: string | undefined);
24
+ /** The key `name`, declared at `node`, is registered under. */
25
+ keyFor(name: string, node: ts.Node): string;
26
+ private packageOf;
27
+ /**
28
+ * True when `key` is already registered, recording a {@link TypeNameCollision} when `declaration`
29
+ * is not the one that claimed it. False claims the key for `declaration`.
30
+ */
31
+ alreadyRegistered(key: string, name: string, declaration: ts.Node): boolean;
32
+ private noteCollision;
33
+ /** A {@link DocumentedType} carrying the declaring package and location of `declaration`. */
34
+ documented(name: string, declaration: ts.Node, description: string, fields: readonly DocumentedField[], enumValues: readonly string[], unionRefNames: readonly string[], discriminator: UnionDiscriminator | undefined, indexSignatureValue: TypeRef | undefined): DocumentedType;
35
+ }
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TypeRegistry = void 0;
4
+ const ApiDocModel_1 = require("../model/ApiDocModel");
5
+ const SourceLocation_1 = require("./SourceLocation");
6
+ /**
7
+ * The named types a {@link TypeResolver} has registered, by KEY (#1058): the bare name for a type the
8
+ * HOME package declares, `<package>:<name>` for any other ({@link DocumentedType.keyOf}).
9
+ *
10
+ * It also remembers which DECLARATION claimed each key, so a second, different declaration arriving
11
+ * under it — two `DupDto`s in one package — is recorded as a {@link TypeNameCollision} rather than
12
+ * silently standing in for the first. The resolver still resolves it to the first, exactly as before,
13
+ * so the MCP projection is unchanged; an OpenAPI document refuses the model.
14
+ */
15
+ class TypeRegistry {
16
+ packages;
17
+ homePackage;
18
+ types = new Map();
19
+ collisions = [];
20
+ /** Named types already registered (or mid-registration) — the cycle stop for the NAMED case. */
21
+ registered = new Set();
22
+ /** The DECLARATION each key was claimed by, so a second, different one is noticed. */
23
+ claimedBy = new Map();
24
+ constructor(packages, homePackage) {
25
+ this.packages = packages;
26
+ this.homePackage = homePackage;
27
+ }
28
+ /** The key `name`, declared at `node`, is registered under. */
29
+ keyFor(name, node) {
30
+ return ApiDocModel_1.DocumentedType.keyOf(name, this.packageOf(node), this.homePackage);
31
+ }
32
+ packageOf(node) {
33
+ return this.packages.of(node.getSourceFile().fileName)?.name;
34
+ }
35
+ /**
36
+ * True when `key` is already registered, recording a {@link TypeNameCollision} when `declaration`
37
+ * is not the one that claimed it. False claims the key for `declaration`.
38
+ */
39
+ alreadyRegistered(key, name, declaration) {
40
+ if (!this.registered.has(key)) {
41
+ this.claimedBy.set(key, declaration);
42
+ return false;
43
+ }
44
+ const first = this.claimedBy.get(key);
45
+ if (first !== undefined && first !== declaration) {
46
+ this.noteCollision(name, first, declaration);
47
+ }
48
+ return true;
49
+ }
50
+ noteCollision(name, first, second) {
51
+ const firstLocation = SourceLocation_1.SourceLocation.of(first);
52
+ const secondLocation = SourceLocation_1.SourceLocation.of(second);
53
+ const seen = this.collisions.some((each) => each.name === name &&
54
+ each.firstLocation === firstLocation &&
55
+ each.secondLocation === secondLocation);
56
+ if (!seen) {
57
+ this.collisions.push(new ApiDocModel_1.TypeNameCollision(name, this.packageOf(second), firstLocation, secondLocation));
58
+ }
59
+ }
60
+ /** A {@link DocumentedType} carrying the declaring package and location of `declaration`. */
61
+ documented(name, declaration, description, fields, enumValues, unionRefNames, discriminator, indexSignatureValue) {
62
+ return new ApiDocModel_1.DocumentedType(name, description, fields, enumValues, unionRefNames, discriminator, indexSignatureValue, this.packageOf(declaration), SourceLocation_1.SourceLocation.of(declaration));
63
+ }
64
+ }
65
+ exports.TypeRegistry = TypeRegistry;
66
+ //# sourceMappingURL=TypeRegistry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TypeRegistry.js","sourceRoot":"","sources":["../../../../../../packages/docs/api-doc-model/src/extract/TypeRegistry.ts"],"names":[],"mappings":";;;AACA,sDAK8B;AAG9B,qDAAkD;AAElD;;;;;;;;GAQG;AACH,MAAa,YAAY;IASA;IACA;IATZ,KAAK,GAAG,IAAI,GAAG,EAA0B,CAAC;IAC1C,UAAU,GAAwB,EAAE,CAAC;IAC9C,gGAAgG;IACvF,UAAU,GAAG,IAAI,GAAG,EAAU,CAAC;IACxC,sFAAsF;IAC7E,SAAS,GAAG,IAAI,GAAG,EAAmB,CAAC;IAEhD,YACqB,QAAuB,EACvB,WAA+B;QAD/B,aAAQ,GAAR,QAAQ,CAAe;QACvB,gBAAW,GAAX,WAAW,CAAoB;IACjD,CAAC;IAEJ,+DAA+D;IAC/D,MAAM,CAAC,IAAY,EAAE,IAAa;QAC9B,OAAO,4BAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;IAC9E,CAAC;IAEO,SAAS,CAAC,IAAa;QAC3B,OAAO,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC,QAAQ,CAAC,EAAE,IAAI,CAAC;IACjE,CAAC;IAED;;;OAGG;IACH,iBAAiB,CAAC,GAAW,EAAE,IAAY,EAAE,WAAoB;QAC7D,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X,MAAM,EACN,UAAU,EACV,aAAa,EACb,aAAa,EACb,mBAAmB,EACnB,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,EAC3B,+BAAc,CAAC,EAAE,CAAC,WAAW,CAAC,CACjC,CAAC;IACN,CAAC;CACJ;AA7ED,oCA6EC","sourcesContent":["import * as ts from 'typescript';\nimport {\n DocumentedField,\n DocumentedType,\n TypeNameCollision,\n UnionDiscriminator,\n} from '../model/ApiDocModel';\nimport { TypeRef } from '../model/TypeRef';\nimport { PackageOfFile } from './PackageOfFile';\nimport { SourceLocation } from './SourceLocation';\n\n/**\n * The named types a {@link TypeResolver} has registered, by KEY (#1058): the bare name for a type the\n * HOME package declares, `<package>:<name>` for any other ({@link DocumentedType.keyOf}).\n *\n * It also remembers which DECLARATION claimed each key, so a second, different declaration arriving\n * under it — two `DupDto`s in one package — is recorded as a {@link TypeNameCollision} rather than\n * silently standing in for the first. The resolver still resolves it to the first, exactly as before,\n * so the MCP projection is unchanged; an OpenAPI document refuses the model.\n */\nexport class TypeRegistry {\n readonly types = new Map<string, DocumentedType>();\n readonly collisions: TypeNameCollision[] = [];\n /** Named types already registered (or mid-registration) — the cycle stop for the NAMED case. */\n readonly registered = new Set<string>();\n /** The DECLARATION each key was claimed by, so a second, different one is noticed. */\n readonly claimedBy = new Map<string, ts.Node>();\n\n constructor(\n private readonly packages: PackageOfFile,\n private readonly homePackage: string | undefined,\n ) {}\n\n /** The key `name`, declared at `node`, is registered under. */\n keyFor(name: string, node: ts.Node): string {\n return DocumentedType.keyOf(name, this.packageOf(node), this.homePackage);\n }\n\n private packageOf(node: ts.Node): string | undefined {\n return this.packages.of(node.getSourceFile().fileName)?.name;\n }\n\n /**\n * True when `key` is already registered, recording a {@link TypeNameCollision} when `declaration`\n * is not the one that claimed it. False claims the key for `declaration`.\n */\n alreadyRegistered(key: string, name: string, declaration: ts.Node): boolean {\n if (!this.registered.has(key)) {\n this.claimedBy.set(key, declaration);\n return false;\n }\n const first = this.claimedBy.get(key);\n if (first !== undefined && first !== declaration) {\n this.noteCollision(name, first, declaration);\n }\n return true;\n }\n\n private noteCollision(name: string, first: ts.Node, second: ts.Node): void {\n const firstLocation = SourceLocation.of(first);\n const secondLocation = SourceLocation.of(second);\n const seen = this.collisions.some(\n (each: TypeNameCollision) =>\n each.name === name &&\n each.firstLocation === firstLocation &&\n each.secondLocation === secondLocation,\n );\n if (!seen) {\n this.collisions.push(\n new TypeNameCollision(name, this.packageOf(second), firstLocation, secondLocation),\n );\n }\n }\n\n /** A {@link DocumentedType} carrying the declaring package and location of `declaration`. */\n documented(\n name: string,\n declaration: ts.Node,\n description: string,\n fields: readonly DocumentedField[],\n enumValues: readonly string[],\n unionRefNames: readonly string[],\n discriminator: UnionDiscriminator | undefined,\n indexSignatureValue: TypeRef | undefined,\n ): DocumentedType {\n return new DocumentedType(\n name,\n description,\n fields,\n enumValues,\n unionRefNames,\n discriminator,\n indexSignatureValue,\n this.packageOf(declaration),\n SourceLocation.of(declaration),\n );\n }\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  import * as ts from 'typescript';
2
- import { DocumentedType, UnmappedType } from '../model/ApiDocModel';
2
+ import { DocumentedType, TypeNameCollision, UnmappedType } from '../model/ApiDocModel';
3
3
  import { TypeRef } from '../model/TypeRef';
4
+ import { PackageOfFile } from './PackageOfFile';
4
5
  /**
5
6
  * Resolve declared TypeScript types into {@link TypeRef}s, registering every NAMED type it meets as
6
7
  * a {@link DocumentedType} a renderer can `$ref`.
@@ -21,20 +22,33 @@ import { TypeRef } from '../model/TypeRef';
21
22
  * truncating one would silently publish an incomplete document. The ONLY thing that is cut is a
22
23
  * self-referential ANONYMOUS type, and it is cut by NODE IDENTITY (it has no name to `$ref`), with an
23
24
  * {@link UnmappedType} recorded so #982's guard has something to name.
25
+ *
26
+ * ## Every type is keyed by its DECLARING PACKAGE too (#1058)
27
+ *
28
+ * A type is registered under {@link DocumentedType.keyOf}: its bare name when the HOME package (the
29
+ * one whose file is being extracted) declares it, `<package>:<name>` otherwise. Two packages'
30
+ * same-named types are therefore two entries rather than one silently standing in for the other, and
31
+ * a renderer can tell which document owns each schema. The name is always the DECLARATION's own, never
32
+ * an import alias, so one type reached under two spellings is still one entry.
24
33
  */
25
34
  export declare class TypeResolver {
26
- private readonly types;
27
35
  private readonly unmapped;
28
- /** Named types already registered (or mid-registration) — the cycle stop for the NAMED case. */
29
- private readonly registered;
36
+ /** Every named type, its key and the declaration that claimed it — see {@link TypeRegistry}. */
37
+ private readonly registry;
30
38
  /** Anonymous type literals currently being expanded — the cycle stop for the ANONYMOUS case. */
31
39
  private readonly expandingAnonymous;
32
40
  private readonly bases;
33
- constructor(checker: ts.TypeChecker);
34
- /** Every named type reached so far, by name. */
41
+ constructor(checker: ts.TypeChecker, packages: PackageOfFile,
42
+ /** The package of the file being extracted — its types are keyed by their bare name. */
43
+ homePackage: string | undefined);
44
+ /** Every named type reached so far, by key — see {@link DocumentedType.keyOf}. */
35
45
  collectedTypes(): ReadonlyMap<string, DocumentedType>;
36
46
  /** Everything that could not be represented — recorded, never dropped. */
37
47
  collectedUnmapped(): readonly UnmappedType[];
48
+ /** Same-named, different declarations in one package — see {@link TypeNameCollision}. */
49
+ collectedCollisions(): readonly TypeNameCollision[];
50
+ /** The key `name`, declared at `node`, is registered under. */
51
+ keyFor(name: string, node: ts.Node): string;
38
52
  /** Resolve one written type, registering whatever named types it reaches. */
39
53
  resolve(node: ts.TypeNode, ownerName: string): TypeRef;
40
54
  /** `string` / `number` / `boolean` / `null` / `unknown`, or undefined for anything else. */
@@ -69,6 +83,11 @@ export declare class TypeResolver {
69
83
  * rather than guessed at.
70
84
  */
71
85
  private resolveReference;
86
+ /**
87
+ * The DECLARATION's own name, not the spelling at the use site: `import { Foo as Bar }` still
88
+ * reaches the type `Foo`, which is what its owning package's document calls it (#1058).
89
+ */
90
+ private static declaredName;
72
91
  /**
73
92
  * Resolve a type by its DECLARATION rather than by a reference to it.
74
93
  *
@@ -132,19 +151,6 @@ export declare class TypeResolver {
132
151
  private resolveAnonymousObject;
133
152
  /** ONE property of a DTO — its type, its optionality, its prose and its numeric constraints. */
134
153
  private fieldOf;
135
- /**
136
- * `@WpMin` / `@WpMax` on a non-numeric field is a BUILD FAILURE, not a warning. A minimum on a
137
- * string is not something a renderer can emit sensibly, and a document that silently dropped it
138
- * would publish a contract weaker than the one its author wrote down.
139
- */
140
- private assertNumericConstraintsFit;
141
- /** `@WpInt()` decorates the FIELD, so the integer flag is pushed onto the right leaf. */
142
- private static markInteger;
143
- private static declaresUndefined;
144
- private static declaresNull;
145
- private static hasDecorator;
146
- private static decoratorCall;
147
- private numericArgument;
148
154
  /** The declaration a type name points at, through imports and aliases. */
149
155
  private declarationOf;
150
156
  private recordUnmapped;