@matatbread/matbot-tool-types 0.4.5 → 0.4.7

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.
Files changed (2) hide show
  1. package/package.json +5 -5
  2. package/src/build-dts.ts +42 -4
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "matbotRuntime": [
4
4
  "node"
5
5
  ],
6
- "version": "0.4.5",
6
+ "version": "0.4.7",
7
7
  "engines": {
8
8
  "node": ">=24.0.0"
9
9
  },
@@ -36,14 +36,14 @@
36
36
  "typescript": "^6.0.3"
37
37
  },
38
38
  "peerDependencies": {
39
- "@matatbread/matbot-plugin-api": "^0.4.5",
40
- "@matatbread/matbot-core": "^0.4.5"
39
+ "@matatbread/matbot-plugin-api": "^0.4.7",
40
+ "@matatbread/matbot-core": "^0.4.7"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "^22.0.0",
44
44
  "typescript": "^6.0.3",
45
- "@matatbread/matbot-plugin-api": "^0.4.5",
46
- "@matatbread/matbot-core": "^0.4.5"
45
+ "@matatbread/matbot-plugin-api": "^0.4.7",
46
+ "@matatbread/matbot-core": "^0.4.7"
47
47
  },
48
48
  "scripts": {
49
49
  "typecheck": "tsc --noEmit"
package/src/build-dts.ts CHANGED
@@ -248,31 +248,69 @@ export async function buildMatbotToolsDts(
248
248
  const importNames = new Set<string>();
249
249
  const bundledDecls = new Map<string, string>();
250
250
 
251
+ // Bundling flattens every referenced workspace type into ONE scope, but a type's name is only unique
252
+ // within its own file: two plugins each declaring a file-local `SkipKind` is legal TypeScript and here
253
+ // a deliberate one (`plugins/background/src/index.ts` says why it isn't shared), so the generator has
254
+ // to represent it rather than ask the sources not to do it. Emitted side by side they were a TS2300
255
+ // that nothing compiled the dts to catch, and both references then resolved to an error type — the
256
+ // narrowing the contract exists to give, silently gone. So the first symbol to claim a name keeps it
257
+ // and later ones are alpha-renamed (`SkipKind$1`), with every reference rewritten to match.
258
+ //
259
+ // Keyed by SYMBOL, never by declaration: a merged interface is several declarations of one symbol, and
260
+ // renaming those apart would break the merge the source relies on. Names already spoken for by a
261
+ // plugin-api import are reserved up front — which one of those gets imported isn't known until the
262
+ // walk ends, and a needless `$1` is cosmetic where a collision is fatal.
263
+ const bundleNames = new Map<TS.Symbol, string>();
264
+ const usedNames = new Set<string>(apiTypeNames);
265
+ const bundleName = (sym: TS.Symbol): string => {
266
+ const already = bundleNames.get(sym);
267
+ if (already !== undefined) return already;
268
+ const base = sym.name;
269
+ let name = base;
270
+ for (let i = 1; usedNames.has(name); i++) name = `${base}$${i}`;
271
+ usedNames.add(name);
272
+ bundleNames.set(sym, name);
273
+ return name;
274
+ };
275
+
251
276
  interface Ctx { imports: Set<string>; seen: Set<string>; heritageBail: string | null; }
252
277
 
253
278
  // Emit `node`'s source text with every bailing type-reference replaced in place by `unknown` (leaf
254
279
  // substitution). Accumulates imports (plugin-api refs) and bundled declarations (workspace refs,
255
280
  // recursively emitted through here too). Sets `ctx.heritageBail` if a bail sits in an `extends` base,
256
281
  // where `unknown` can't go — the caller then collapses that whole member.
257
- const emitNode = (node: TS.Node, ctx: Ctx): string => {
282
+ const emitNode = (node: TS.Node, ctx: Ctx, declName?: string): string => {
258
283
  const base = node.getStart();
259
284
  const repls: Array<{ s: number; e: number; t: string }> = [];
285
+ // Rewrite the declaration's OWN name when it was renamed out of a collision. Claim the name before
286
+ // recursing, so a self-referential type reaches the same entry instead of allocating a second.
287
+ if (declName !== undefined && (ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node))
288
+ && node.name.getText() !== declName) {
289
+ repls.push({ s: node.name.getStart() - base, e: node.name.getEnd() - base, t: declName });
290
+ }
260
291
  const bundle = (sym: TS.Symbol): void => {
292
+ const name = bundleName(sym);
261
293
  for (const d of sym.declarations ?? []) {
262
294
  if (!ts.isInterfaceDeclaration(d) && !ts.isTypeAliasDeclaration(d)) continue;
263
295
  const key = declKey(d);
264
296
  if (ctx.seen.has(key)) continue;
265
297
  ctx.seen.add(key);
266
- bundledDecls.set(key, stripModifiers(emitNode(d, ctx)));
298
+ bundledDecls.set(key, stripModifiers(emitNode(d, ctx, name)));
267
299
  }
268
300
  };
301
+ // A reference names a symbol, not a file, so it also carries the local spelling of an `import { X as Y }`.
302
+ // Rewriting to the allocated name settles both that and the collision rename in one substitution.
303
+ const rename = (ref: TS.Node, to: string): void => {
304
+ if (ref.getText() === to) return;
305
+ repls.push({ s: ref.getStart() - base, e: ref.getEnd() - base, t: to });
306
+ };
269
307
  const visit = (n: TS.Node): void => {
270
308
  if (ctx.heritageBail) return;
271
309
  if (ts.isTypeReferenceNode(n)) {
272
310
  const c = classifyEntity(n.typeName);
273
311
  if (c.tag === 'bail') { repls.push({ s: n.getStart() - base, e: n.getEnd() - base, t: unknownOf(c.label) }); return; }
274
312
  if (c.tag === 'api') importNames.add(n.typeName.getText());
275
- else if (c.tag === 'bundle') bundle(c.sym);
313
+ else if (c.tag === 'bundle') { rename(n.typeName, bundleName(c.sym)); bundle(c.sym); }
276
314
  n.typeArguments?.forEach(visit);
277
315
  return;
278
316
  }
@@ -280,7 +318,7 @@ export async function buildMatbotToolsDts(
280
318
  const c = classifyEntity(n.expression);
281
319
  if (c.tag === 'bail') { ctx.heritageBail = c.label; return; }
282
320
  if (c.tag === 'api') importNames.add(n.expression.getText());
283
- else if (c.tag === 'bundle') bundle(c.sym);
321
+ else if (c.tag === 'bundle') { rename(n.expression, bundleName(c.sym)); bundle(c.sym); }
284
322
  n.typeArguments?.forEach(visit);
285
323
  return;
286
324
  }