@webpieces/api-doc-model 0.4.829 → 0.4.830

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,41 @@
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
+ /** The package whose `package.json` sits in `directory`, when it is one with a name. */
40
+ private read;
41
+ }
@@ -0,0 +1,73 @@
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
+ /** The npm package a source file belongs to: the nearest `package.json` above it. Data-only. */
8
+ class DeclaringPackage {
9
+ name;
10
+ version;
11
+ directory;
12
+ constructor(
13
+ /** `name` from that `package.json`, e.g. `@myorg/lang-api-dtos`. */
14
+ name,
15
+ /** `version` from that `package.json`; the empty string when it states none. */
16
+ version,
17
+ /** The absolute directory holding that `package.json`. */
18
+ directory) {
19
+ this.name = name;
20
+ this.version = version;
21
+ this.directory = directory;
22
+ }
23
+ }
24
+ exports.DeclaringPackage = DeclaringPackage;
25
+ /**
26
+ * WHICH PACKAGE declares a type — the owner of its schema (#1058).
27
+ *
28
+ * A schema is defined in exactly one OpenAPI document: the one belonging to the package whose source
29
+ * declares the TypeScript type. That is what lets two different `LocalizedDescriptionsDto`s live in
30
+ * two packages without colliding, and what lets a contract document `$ref` a DTO library's schema
31
+ * instead of copying it. The answer is the nearest `package.json` above the declaring file — which is
32
+ * the same answer whether the compiler reached the file as workspace SOURCE through tsconfig `paths`
33
+ * or as a published `.d.ts` under `node_modules`.
34
+ *
35
+ * A file with no `package.json` above it belongs to no package; `of` returns `undefined` and the
36
+ * caller decides what that means (the OpenAPI generator refuses to reference it, the MCP renderer
37
+ * does not care).
38
+ *
39
+ * Cached per directory, because a contract reaches hundreds of declarations in a handful of files.
40
+ */
41
+ class PackageOfFile {
42
+ byDirectory = new Map();
43
+ /** The package declaring `fileName`, or undefined when no `package.json` with a name is above it. */
44
+ of(fileName) {
45
+ return this.ofDirectory(path.dirname(path.resolve(fileName)));
46
+ }
47
+ ofDirectory(directory) {
48
+ if (this.byDirectory.has(directory)) {
49
+ return this.byDirectory.get(directory);
50
+ }
51
+ const found = this.read(directory) ?? this.parentOf(directory);
52
+ this.byDirectory.set(directory, found);
53
+ return found;
54
+ }
55
+ parentOf(directory) {
56
+ const parent = path.dirname(directory);
57
+ return parent === directory ? undefined : this.ofDirectory(parent);
58
+ }
59
+ /** The package whose `package.json` sits in `directory`, when it is one with a name. */
60
+ read(directory) {
61
+ const file = path.join(directory, 'package.json');
62
+ if (!fs.existsSync(file)) {
63
+ return undefined;
64
+ }
65
+ const raw = JSON.parse(fs.readFileSync(file, 'utf8'));
66
+ if (typeof raw.name !== 'string' || raw.name.trim() === '') {
67
+ return undefined;
68
+ }
69
+ return new DeclaringPackage(raw.name, typeof raw.version === 'string' ? raw.version : '', directory);
70
+ }
71
+ }
72
+ exports.PackageOfFile = PackageOfFile;
73
+ //# 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;AAElC,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,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAClE,CAAC;IAEO,WAAW,CAAC,SAAiB;QACjC,IAAI,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;YAClC,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAC3C,CAAC;QACD,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;QAC/D,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;QACvC,OAAO,KAAK,CAAC;IACjB,CAAC;IAEO,QAAQ,CAAC,SAAiB;QAC9B,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QACvC,OAAO,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IACvE,CAAC;IAED,wFAAwF;IAChF,IAAI,CAAC,SAAiB;QAC1B,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;QAClD,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;YACvB,OAAO,SAAS,CAAC;QACrB,CAAC;QACD,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAsB,CAAC;QAC3E,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;AAtCD,sCAsCC","sourcesContent":["import * as fs from 'node:fs';\nimport * as path from 'node:path';\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 /** 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 = JSON.parse(fs.readFileSync(file, 'utf8')) as PackageJsonFields;\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,IAAI,CAAC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N,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;
@@ -6,9 +6,10 @@ const ts = tslib_1.__importStar(require("typescript"));
6
6
  const core_util_1 = require("@webpieces/core-util");
7
7
  const ApiDocModel_1 = require("../model/ApiDocModel");
8
8
  const TypeRef_1 = require("../model/TypeRef");
9
- const ApiDocExtractionError_1 = require("./ApiDocExtractionError");
10
9
  const JsDoc_1 = require("./JsDoc");
11
10
  const ObjectShapeBases_1 = require("./ObjectShapeBases");
11
+ const FieldDecorators_1 = require("./FieldDecorators");
12
+ const TypeRegistry_1 = require("./TypeRegistry");
12
13
  const SourceLocation_1 = require("./SourceLocation");
13
14
  const StringValueSets_1 = require("./StringValueSets");
14
15
  /**
@@ -50,26 +51,44 @@ const INTEGER_ALIAS = 'Integer';
50
51
  * truncating one would silently publish an incomplete document. The ONLY thing that is cut is a
51
52
  * self-referential ANONYMOUS type, and it is cut by NODE IDENTITY (it has no name to `$ref`), with an
52
53
  * {@link UnmappedType} recorded so #982's guard has something to name.
54
+ *
55
+ * ## Every type is keyed by its DECLARING PACKAGE too (#1058)
56
+ *
57
+ * A type is registered under {@link DocumentedType.keyOf}: its bare name when the HOME package (the
58
+ * one whose file is being extracted) declares it, `<package>:<name>` otherwise. Two packages'
59
+ * same-named types are therefore two entries rather than one silently standing in for the other, and
60
+ * a renderer can tell which document owns each schema. The name is always the DECLARATION's own, never
61
+ * an import alias, so one type reached under two spellings is still one entry.
53
62
  */
54
63
  class TypeResolver {
55
- types = new Map();
56
64
  unmapped = [];
57
- /** Named types already registered (or mid-registration) — the cycle stop for the NAMED case. */
58
- registered = new Set();
65
+ /** Every named type, its key and the declaration that claimed it — see {@link TypeRegistry}. */
66
+ registry;
59
67
  /** Anonymous type literals currently being expanded — the cycle stop for the ANONYMOUS case. */
60
68
  expandingAnonymous = new Set();
61
69
  bases;
62
- constructor(checker) {
70
+ constructor(checker, packages,
71
+ /** The package of the file being extracted — its types are keyed by their bare name. */
72
+ homePackage) {
63
73
  this.bases = new ObjectShapeBases_1.ObjectShapeBases(checker);
74
+ this.registry = new TypeRegistry_1.TypeRegistry(packages, homePackage);
64
75
  }
65
- /** Every named type reached so far, by name. */
76
+ /** Every named type reached so far, by key — see {@link DocumentedType.keyOf}. */
66
77
  collectedTypes() {
67
- return this.types;
78
+ return this.registry.types;
68
79
  }
69
80
  /** Everything that could not be represented — recorded, never dropped. */
70
81
  collectedUnmapped() {
71
82
  return this.unmapped;
72
83
  }
84
+ /** Same-named, different declarations in one package — see {@link TypeNameCollision}. */
85
+ collectedCollisions() {
86
+ return this.registry.collisions;
87
+ }
88
+ /** The key `name`, declared at `node`, is registered under. */
89
+ keyFor(name, node) {
90
+ return this.registry.keyFor(name, node);
91
+ }
73
92
  /** Resolve one written type, registering whatever named types it reaches. */
74
93
  resolve(node, ownerName) {
75
94
  if (ts.isParenthesizedTypeNode(node)) {
@@ -171,7 +190,7 @@ class TypeResolver {
171
190
  return TypeRef_1.TypeRef.primitiveOf('null');
172
191
  }
173
192
  const refs = branches.map((t) => this.resolve(t, ownerName));
174
- const values = new StringValueSets_1.StringValueSets(this.types).valuesOfAll(refs);
193
+ const values = new StringValueSets_1.StringValueSets(this.registry.types).valuesOfAll(refs);
175
194
  if (values !== undefined) {
176
195
  return TypeRef_1.TypeRef.enumOf(values);
177
196
  }
@@ -179,7 +198,7 @@ class TypeResolver {
179
198
  return this.recordUnmapped(node, 'a union whose branches are not all NAMED object types has no discriminator a renderer could narrow on');
180
199
  }
181
200
  const names = refs.map((r) => r.refName);
182
- const discriminator = new StringValueSets_1.StringValueSets(this.types).derive(names);
201
+ const discriminator = new StringValueSets_1.StringValueSets(this.registry.types).derive(names);
183
202
  if (discriminator === undefined) {
184
203
  return this.recordUnmapped(node, 'no property is typed as string values (a literal, a string-enum member, or a union of them) on EVERY ' +
185
204
  'branch with no value shared by two branches, so this union has no derivable discriminator');
@@ -203,11 +222,12 @@ class TypeResolver {
203
222
  return;
204
223
  }
205
224
  const name = alias.name.text;
206
- if (this.registered.has(name)) {
225
+ const key = this.registry.keyFor(name, alias);
226
+ if (this.registry.alreadyRegistered(key, name, alias)) {
207
227
  return;
208
228
  }
209
- this.registered.add(name);
210
- this.types.set(name, new ApiDocModel_1.DocumentedType(name, JsDoc_1.JsDoc.read(alias).description, [], [], branchNames, discriminator, undefined));
229
+ this.registry.registered.add(key);
230
+ this.registry.types.set(key, this.registry.documented(name, alias, JsDoc_1.JsDoc.read(alias).description, [], [], branchNames, discriminator, undefined));
211
231
  }
212
232
  /**
213
233
  * `Integer`, `Array<T>`, `Record<string, V>`, `Promise<T>`, and everything named — an interface,
@@ -234,7 +254,19 @@ class TypeResolver {
234
254
  if (declaration === undefined) {
235
255
  return this.recordUnmapped(node, `no declaration found for '${name}'`);
236
256
  }
237
- return this.resolveDeclaration(name, declaration, ownerName);
257
+ return this.resolveDeclaration(TypeResolver.declaredName(declaration, name), declaration, ownerName);
258
+ }
259
+ /**
260
+ * The DECLARATION's own name, not the spelling at the use site: `import { Foo as Bar }` still
261
+ * reaches the type `Foo`, which is what its owning package's document calls it (#1058).
262
+ */
263
+ // webpieces-disable no-function-outside-class -- private static helper of this class
264
+ static declaredName(declaration, written) {
265
+ const named = ts.isInterfaceDeclaration(declaration) ||
266
+ ts.isClassDeclaration(declaration) ||
267
+ ts.isTypeAliasDeclaration(declaration) ||
268
+ ts.isEnumDeclaration(declaration);
269
+ return named && declaration.name !== undefined ? declaration.name.text : written;
238
270
  }
239
271
  /**
240
272
  * Resolve a type by its DECLARATION rather than by a reference to it.
@@ -261,17 +293,18 @@ class TypeResolver {
261
293
  }
262
294
  /** `type X = ...` — registered under X when it has a shape of its own, else transparent. */
263
295
  resolveAlias(name, alias, ownerName) {
264
- if (this.registered.has(name)) {
265
- return this.types.get(name)?.unionRefNames.length
266
- ? TypeRef_1.TypeRef.union(this.types.get(name).unionRefNames)
267
- : TypeRef_1.TypeRef.ref(name);
296
+ const key = this.registry.keyFor(name, alias);
297
+ if (this.registry.alreadyRegistered(key, name, alias)) {
298
+ return this.registry.types.get(key)?.unionRefNames.length
299
+ ? TypeRef_1.TypeRef.union(this.registry.types.get(key).unionRefNames)
300
+ : TypeRef_1.TypeRef.ref(key);
268
301
  }
269
302
  if (ts.isUnionTypeNode(alias.type)) {
270
303
  const resolved = this.resolve(alias.type, ownerName);
271
304
  if (resolved.kind === 'enum') {
272
- this.registered.add(name);
273
- this.types.set(name, new ApiDocModel_1.DocumentedType(name, JsDoc_1.JsDoc.read(alias).description, [], resolved.enumValues, [], undefined, undefined));
274
- return TypeRef_1.TypeRef.ref(name);
305
+ this.registry.registered.add(key);
306
+ this.registry.types.set(key, this.registry.documented(name, alias, JsDoc_1.JsDoc.read(alias).description, [], resolved.enumValues, [], undefined, undefined));
307
+ return TypeRef_1.TypeRef.ref(key);
275
308
  }
276
309
  return resolved;
277
310
  }
@@ -294,8 +327,9 @@ class TypeResolver {
294
327
  }
295
328
  /** A TS `enum` of string members — the one non-union enum shape a document can carry. */
296
329
  registerStringEnum(name, declaration) {
297
- if (this.registered.has(name)) {
298
- return TypeRef_1.TypeRef.ref(name);
330
+ const key = this.registry.keyFor(name, declaration);
331
+ if (this.registry.alreadyRegistered(key, name, declaration)) {
332
+ return TypeRef_1.TypeRef.ref(key);
299
333
  }
300
334
  const values = [];
301
335
  for (const member of declaration.members) {
@@ -306,9 +340,9 @@ class TypeResolver {
306
340
  if (values.length !== declaration.members.length) {
307
341
  return this.recordUnmapped(declaration, `enum '${name}' has members that are not string literals`);
308
342
  }
309
- this.registered.add(name);
310
- this.types.set(name, new ApiDocModel_1.DocumentedType(name, JsDoc_1.JsDoc.read(declaration).description, [], values, [], undefined, undefined));
311
- return TypeRef_1.TypeRef.ref(name);
343
+ this.registry.registered.add(key);
344
+ this.registry.types.set(key, this.registry.documented(name, declaration, JsDoc_1.JsDoc.read(declaration).description, [], values, [], undefined, undefined));
345
+ return TypeRef_1.TypeRef.ref(key);
312
346
  }
313
347
  /**
314
348
  * Register a named object shape, RESERVING THE NAME BEFORE walking its fields. That order is the
@@ -319,14 +353,22 @@ class TypeResolver {
319
353
  * as its own. See {@link collectFields}.
320
354
  */
321
355
  registerNamedObject(name, shape, docSource) {
322
- if (this.registered.has(name)) {
323
- return TypeRef_1.TypeRef.ref(name);
356
+ const declaration = docSource ?? shape;
357
+ const key = this.registry.keyFor(name, declaration);
358
+ if (this.registry.registered.has(key)) {
359
+ // An ANONYMOUS `{ ... }` is named after where it is written, so two of them can share an
360
+ // owner-derived name; that was never a declared-name collision and is not recorded as one.
361
+ if (!ts.isTypeLiteralNode(declaration)) {
362
+ this.registry.alreadyRegistered(key, name, declaration);
363
+ }
364
+ return TypeRef_1.TypeRef.ref(key);
324
365
  }
325
- this.registered.add(name);
366
+ this.registry.claimedBy.set(key, declaration);
367
+ this.registry.registered.add(key);
326
368
  const fields = new Map();
327
369
  const indexSignature = this.collectFields(shape, name, fields, new Set());
328
- this.types.set(name, new ApiDocModel_1.DocumentedType(name, JsDoc_1.JsDoc.read(docSource ?? shape).description, Array.from(fields.values()), [], [], undefined, indexSignature));
329
- return TypeRef_1.TypeRef.ref(name);
370
+ this.registry.types.set(key, this.registry.documented(name, declaration, JsDoc_1.JsDoc.read(declaration).description, Array.from(fields.values()), [], [], undefined, indexSignature));
371
+ return TypeRef_1.TypeRef.ref(key);
330
372
  }
331
373
  /**
332
374
  * Collect `shape`'s fields into `fields`, FLATTENING every base it `extends` (#1055).
@@ -411,88 +453,18 @@ class TypeResolver {
411
453
  if (member.type === undefined) {
412
454
  return undefined;
413
455
  }
414
- const optional = member.questionToken !== undefined || TypeResolver.declaresUndefined(member.type);
415
- const nullable = TypeResolver.declaresNull(member.type);
456
+ const optional = member.questionToken !== undefined || FieldDecorators_1.FieldDecorators.declaresUndefined(member.type);
457
+ const nullable = FieldDecorators_1.FieldDecorators.declaresNull(member.type);
416
458
  let type = this.resolve(member.type, `${ownerName}.${name}`);
417
- if (TypeResolver.hasDecorator(member, INT_DECORATOR)) {
418
- type = TypeResolver.markInteger(type);
459
+ if (FieldDecorators_1.FieldDecorators.hasDecorator(member, INT_DECORATOR)) {
460
+ type = FieldDecorators_1.FieldDecorators.markInteger(type);
419
461
  }
420
462
  const doc = JsDoc_1.JsDoc.read(member);
421
- const min = this.numericArgument(member, MIN_DECORATOR);
422
- const max = this.numericArgument(member, MAX_DECORATOR);
423
- this.assertNumericConstraintsFit(member, name, type, min, max);
463
+ const min = FieldDecorators_1.FieldDecorators.numericArgument(member, MIN_DECORATOR);
464
+ const max = FieldDecorators_1.FieldDecorators.numericArgument(member, MAX_DECORATOR);
465
+ FieldDecorators_1.FieldDecorators.assertNumericConstraintsFit(member, name, type, min, max);
424
466
  return new ApiDocModel_1.DocumentedField(name, type, optional, nullable, doc.description, doc.mcp, doc.format, min, max, doc.mcpHeader, SourceLocation_1.SourceLocation.of(member));
425
467
  }
426
- /**
427
- * `@WpMin` / `@WpMax` on a non-numeric field is a BUILD FAILURE, not a warning. A minimum on a
428
- * string is not something a renderer can emit sensibly, and a document that silently dropped it
429
- * would publish a contract weaker than the one its author wrote down.
430
- */
431
- assertNumericConstraintsFit(member, name, type, min, max) {
432
- if (min === undefined && max === undefined) {
433
- return;
434
- }
435
- const numeric = type.isNumeric() || (type.kind === 'array' && type.items.isNumeric());
436
- if (numeric) {
437
- return;
438
- }
439
- throw new ApiDocExtractionError_1.ApiDocExtractionError(`@${MIN_DECORATOR}/@${MAX_DECORATOR} on non-numeric field '${name}'`, SourceLocation_1.SourceLocation.of(member), 'Put the constraint on a `number` / `Integer` field, or drop it.');
440
- }
441
- /** `@WpInt()` decorates the FIELD, so the integer flag is pushed onto the right leaf. */
442
- // webpieces-disable no-function-outside-class -- private static helper of this class
443
- static markInteger(type) {
444
- if (type.kind === 'array') {
445
- return TypeRef_1.TypeRef.array(type.items.asInteger());
446
- }
447
- if (type.kind === 'openMap') {
448
- return TypeRef_1.TypeRef.openMap(type.values.asInteger());
449
- }
450
- return type.asInteger();
451
- }
452
- // webpieces-disable no-function-outside-class -- private static helper of this class
453
- static declaresUndefined(node) {
454
- return (ts.isUnionTypeNode(node) &&
455
- node.types.some((t) => t.kind === ts.SyntaxKind.UndefinedKeyword));
456
- }
457
- // webpieces-disable no-function-outside-class -- private static helper of this class
458
- static declaresNull(node) {
459
- return (ts.isUnionTypeNode(node) &&
460
- node.types.some((t) => t.kind === ts.SyntaxKind.NullKeyword ||
461
- (ts.isLiteralTypeNode(t) && t.literal.kind === ts.SyntaxKind.NullKeyword)));
462
- }
463
- // webpieces-disable no-function-outside-class -- private static helper of this class
464
- static hasDecorator(node, decoratorName) {
465
- return TypeResolver.decoratorCall(node, decoratorName) !== undefined;
466
- }
467
- // webpieces-disable no-function-outside-class -- private static helper of this class
468
- static decoratorCall(node, decoratorName) {
469
- const decorators = ts.canHaveDecorators(node) ? (ts.getDecorators(node) ?? []) : [];
470
- for (const decorator of decorators) {
471
- const call = decorator.expression;
472
- if (ts.isCallExpression(call) &&
473
- ts.isIdentifier(call.expression) &&
474
- call.expression.text === decoratorName) {
475
- return call;
476
- }
477
- }
478
- return undefined;
479
- }
480
- numericArgument(node, decoratorName) {
481
- const call = TypeResolver.decoratorCall(node, decoratorName);
482
- const argument = call?.arguments[0];
483
- if (argument === undefined) {
484
- return undefined;
485
- }
486
- if (ts.isNumericLiteral(argument)) {
487
- return Number(argument.text);
488
- }
489
- if (ts.isPrefixUnaryExpression(argument) &&
490
- argument.operator === ts.SyntaxKind.MinusToken &&
491
- ts.isNumericLiteral(argument.operand)) {
492
- return -Number(argument.operand.text);
493
- }
494
- throw new ApiDocExtractionError_1.ApiDocExtractionError(`@${decoratorName} argument is not a numeric literal: '${argument.getText()}'`, SourceLocation_1.SourceLocation.of(argument), 'Write the bound as a numeric literal. A value only known at runtime cannot be published.');
495
- }
496
468
  /** The declaration a type name points at, through imports and aliases. */
497
469
  declarationOf(name) {
498
470
  return this.bases.declarationOf(name);