@tsdoctor/model 0.10.7 → 0.10.8

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.
@@ -1,3 +1,4 @@
1
+ import { resolveUndeclared } from "./internal/undeclared.js";
1
2
  import { ApiItemKind, ApiModel, ExcerptTokenKind } from "@microsoft/api-extractor-model";
2
3
  import { VirtualPackage } from "@tsdoctor/vfs";
3
4
 
@@ -55,6 +56,11 @@ var ApiExtractedPackage = class ApiExtractedPackage extends VirtualPackage {
55
56
  parts.push(packageDoc);
56
57
  parts.push("");
57
58
  }
59
+ const undeclared = resolveUndeclared(ep, this.apiPackage.name, (token) => this.normalizeTokenText(token), this.siblingExports(ep));
60
+ if (undeclared.imports.length > 0) {
61
+ parts.push(...undeclared.imports);
62
+ parts.push("");
63
+ }
58
64
  for (const member of ep.members) {
59
65
  const decl = this.generateDeclaration(member);
60
66
  if (decl) {
@@ -62,6 +68,10 @@ var ApiExtractedPackage = class ApiExtractedPackage extends VirtualPackage {
62
68
  parts.push("");
63
69
  }
64
70
  }
71
+ if (undeclared.stubs) {
72
+ parts.push(undeclared.stubs);
73
+ parts.push("");
74
+ }
65
75
  parts.push("export { }");
66
76
  parts.push("");
67
77
  return parts.join("\n");
@@ -467,6 +477,21 @@ var ApiExtractedPackage = class ApiExtractedPackage extends VirtualPackage {
467
477
  }
468
478
  return parts.join("");
469
479
  }
480
+ /**
481
+ * Every member name the package's other entry points export, mapped to the
482
+ * module specifier that imports it. The main entry wins a name several
483
+ * entries export.
484
+ */
485
+ siblingExports(entryPoint) {
486
+ const exports = /* @__PURE__ */ new Map();
487
+ const others = this.apiPackage.entryPoints.filter((other) => other !== entryPoint).sort((a, b) => Number(a.displayName !== "") - Number(b.displayName !== ""));
488
+ for (const other of others) {
489
+ const entryName = this.getEntryPointName(other);
490
+ const specifier = entryName ? `${this.name}/${entryName}` : this.name;
491
+ for (const member of other.members) if (!exports.has(member.displayName)) exports.set(member.displayName, specifier);
492
+ }
493
+ return exports;
494
+ }
470
495
  getEntryPointName(entryPoint) {
471
496
  if (entryPoint.displayName === "") return void 0;
472
497
  return entryPoint.displayName;
package/index.d.ts CHANGED
@@ -106,6 +106,12 @@ export declare class ApiExtractedPackage extends VirtualPackage {
106
106
  * syntax into rendered documentation.
107
107
  */
108
108
  private extractPlainText;
109
+ /**
110
+ * Every member name the package's other entry points export, mapped to the
111
+ * module specifier that imports it. The main entry wins a name several
112
+ * entries export.
113
+ */
114
+ private siblingExports;
109
115
  private getEntryPointName;
110
116
  }
111
117
  declare namespace EntryPoints_d_exports {
@@ -0,0 +1,181 @@
1
+ import { ApiDeclaredItem, ExcerptTokenKind } from "@microsoft/api-extractor-model";
2
+
3
+ // src/internal/undeclared.ts
4
+ /**
5
+ * Global namespaces a dotted `Content` name may legitimately start with. A
6
+ * stub of one would shadow the real declaration, so these are never stubbed.
7
+ */
8
+ const AMBIENT_ROOTS = /* @__PURE__ */ new Set([
9
+ "Array",
10
+ "Atomics",
11
+ "BigInt",
12
+ "CSS",
13
+ "Date",
14
+ "Error",
15
+ "Intl",
16
+ "JSON",
17
+ "JSX",
18
+ "Math",
19
+ "NodeJS",
20
+ "Number",
21
+ "Object",
22
+ "Promise",
23
+ "React",
24
+ "Reflect",
25
+ "String",
26
+ "Symbol",
27
+ "WebAssembly",
28
+ "console",
29
+ "globalThis",
30
+ "import",
31
+ "this"
32
+ ]);
33
+ /** A dotted identifier path not itself preceded by an identifier character or a dot. */
34
+ const DOTTED_NAME = /(?<![\w$.])[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+/g;
35
+ /** String and template literals, whose contents are never names. */
36
+ const STRING_LITERAL = /"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|`(?:[^`\\]|\\.)*`/g;
37
+ /**
38
+ * Resolve the names an entry point references but does not declare: import
39
+ * the ones a sibling entry point exports, stub the rest.
40
+ *
41
+ * @param entryPoint - The entry point being reconstructed
42
+ * @param packageName - The canonical package name of the documented package
43
+ * @param renderToken - How the reconstruction spells a token (alias-normalized)
44
+ * @param siblingExports - Name to module specifier for every member of the package's other entry points
45
+ */
46
+ function resolveUndeclared(entryPoint, packageName, renderToken, siblingExports) {
47
+ const declared = new Set(entryPoint.members.map((member) => member.displayName));
48
+ const resolvedRoots = /* @__PURE__ */ new Set();
49
+ const candidates = [];
50
+ const visit = (item) => {
51
+ if (item instanceof ApiDeclaredItem) collect(item.excerptTokens, packageName, renderToken, declared, resolvedRoots, candidates);
52
+ for (const member of item.members) visit(member);
53
+ };
54
+ for (const member of entryPoint.members) visit(member);
55
+ const siblingImports = /* @__PURE__ */ new Map();
56
+ const roots = /* @__PURE__ */ new Map();
57
+ for (const { path, arity } of candidates) {
58
+ const [root] = path;
59
+ if (!root || declared.has(root) || resolvedRoots.has(root) || AMBIENT_ROOTS.has(root)) continue;
60
+ const specifier = siblingExports.get(root);
61
+ if (specifier !== void 0) {
62
+ let names = siblingImports.get(specifier);
63
+ if (!names) {
64
+ names = /* @__PURE__ */ new Set();
65
+ siblingImports.set(specifier, names);
66
+ }
67
+ names.add(root);
68
+ continue;
69
+ }
70
+ let level = roots;
71
+ let node;
72
+ for (const segment of path) {
73
+ node = level.get(segment);
74
+ if (!node) {
75
+ node = {
76
+ isType: false,
77
+ arity: 0,
78
+ children: /* @__PURE__ */ new Map()
79
+ };
80
+ level.set(segment, node);
81
+ }
82
+ level = node.children;
83
+ }
84
+ if (node) {
85
+ node.isType = true;
86
+ node.arity = Math.max(node.arity, arity);
87
+ }
88
+ }
89
+ const imports = [...siblingImports].sort(([a], [b]) => a.localeCompare(b)).map(([specifier, names]) => `import { ${[...names].sort().join(", ")} } from "${specifier}";`);
90
+ if (roots.size === 0) return {
91
+ imports,
92
+ stubs: ""
93
+ };
94
+ const lines = ["// Referenced by this entry point but neither exported nor resolvable in the API model;", "// declared as `any` so the rest of the file type-checks."];
95
+ for (const [name, node] of [...roots].sort(([a], [b]) => a.localeCompare(b))) renderNode(name, node, "", true, lines);
96
+ return {
97
+ imports,
98
+ stubs: lines.join("\n")
99
+ };
100
+ }
101
+ function collect(tokens, packageName, renderToken, declared, resolvedRoots, candidates) {
102
+ const texts = tokens.map(renderToken);
103
+ const masked = texts.join("").replace(STRING_LITERAL, (literal) => " ".repeat(literal.length));
104
+ const contentRanges = [];
105
+ let offset = 0;
106
+ tokens.forEach((token, index) => {
107
+ const text = texts[index] ?? "";
108
+ const start = offset;
109
+ offset += text.length;
110
+ if (token.kind !== ExcerptTokenKind.Reference) {
111
+ contentRanges.push([start, offset]);
112
+ return;
113
+ }
114
+ const canonical = token.canonicalReference?.toString();
115
+ if (!canonical) {
116
+ contentRanges.push([start, offset]);
117
+ return;
118
+ }
119
+ const path = text.trim().split(".");
120
+ const [root] = path;
121
+ if (!root) return;
122
+ const bang = canonical.indexOf("!");
123
+ if (bang === -1 || canonical.slice(0, bang) !== packageName) {
124
+ resolvedRoots.add(root);
125
+ return;
126
+ }
127
+ const symbolRoot = canonical.slice(bang + 1).replace(/^~/, "").split(/[.#~:]/)[0] ?? "";
128
+ if (declared.has(root) || declared.has(symbolRoot)) return;
129
+ candidates.push({
130
+ path,
131
+ arity: arityAt(masked, offset)
132
+ });
133
+ });
134
+ for (const match of masked.matchAll(DOTTED_NAME)) {
135
+ const start = match.index;
136
+ const end = start + match[0].length;
137
+ if (!contentRanges.some(([from, to]) => start >= from && start < to)) continue;
138
+ if (/\btypeof\s+$/.test(masked.slice(0, start)) || masked[end] === "(") continue;
139
+ candidates.push({
140
+ path: match[0].split("."),
141
+ arity: arityAt(masked, end)
142
+ });
143
+ }
144
+ }
145
+ /**
146
+ * The number of type arguments applied at `position`: 0 unless the next
147
+ * non-space character opens a `<…>` list. Arrow `=>` is not a closer.
148
+ */
149
+ function arityAt(text, position) {
150
+ let index = position;
151
+ while (text[index] === " ") index++;
152
+ if (text[index] !== "<") return 0;
153
+ let depth = 0;
154
+ let commas = 0;
155
+ for (; index < text.length; index++) {
156
+ const char = text[index];
157
+ if (char === "<" || char === "(" || char === "[" || char === "{") depth++;
158
+ else if (char === ">" || char === ")" || char === "]" || char === "}") {
159
+ if (char === ">" && text[index - 1] === "=") continue;
160
+ depth--;
161
+ if (depth === 0) return commas + 1;
162
+ } else if (char === "," && depth === 1) commas++;
163
+ }
164
+ return 0;
165
+ }
166
+ function renderNode(name, node, indent, topLevel, lines) {
167
+ const keyword = topLevel ? "declare" : "export";
168
+ if (node.isType) {
169
+ const parameters = Array.from({ length: node.arity }, (_, i) => `T${i} = any`);
170
+ const generic = parameters.length > 0 ? `<${parameters.join(", ")}>` : "";
171
+ lines.push(`${indent}${keyword} type ${name}${generic} = any;`);
172
+ lines.push(`${indent}${keyword} const ${name}: any;`);
173
+ }
174
+ if (node.children.size > 0) {
175
+ lines.push(`${indent}${keyword} namespace ${name} {`);
176
+ for (const [child, childNode] of [...node.children].sort(([a], [b]) => a.localeCompare(b))) renderNode(child, childNode, `${indent}\t`, false, lines);
177
+ lines.push(`${indent}}`);
178
+ }
179
+ }
180
+
181
+ export { resolveUndeclared };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsdoctor/model",
3
- "version": "0.10.7",
3
+ "version": "0.10.8",
4
4
  "private": false,
5
5
  "description": "Render Microsoft API Extractor models into LLM-lean markdown. Pure model loading, TSDoc extraction, type-signature formatting, and per-item markdown rendering.",
6
6
  "keywords": [