Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(api-report): a hook's or a function's overloads carry their tags (#9802)

Christopher Chedeau5e05bdf42fc9
diff --git a/scripts/api-report/__fixtures__/repo/packages/demo/lib/index.d.ts b/scripts/api-report/__fixtures__/repo/packages/demo/lib/index.d.ts
index 5f49a33795..56d0cd653d 100644
--- a/scripts/api-report/__fixtures__/repo/packages/demo/lib/index.d.ts
+++ b/scripts/api-report/__fixtures__/repo/packages/demo/lib/index.d.ts
@@ -49,6 +49,17 @@ export declare function useChipState(
): readonly [boolean, (next: boolean) => void];
export declare function formatChip(label: string): string;
export declare function formatChip(label: string, size: ChipSize): string;
+export declare function useChipMode(mode: 'a'): string;
+/** @deprecated Pass a mode. */
+export declare function useChipMode(): string;
+export declare function useChipMode(mode: 'b'): number;
+/** @deprecated Pass a size. */
+export declare function parseChip(label: string, legacy: true): string;
+export declare function parseChip(label: string): string;
+/** @deprecated Use formatChip. */
+export declare function legacyChip(label: string): string;
+/** @deprecated Use formatChip. */
+export declare function legacyChip(label: string, size: ChipSize): string;
export declare const CHIP_SIZES: readonly ChipSize[];
/** @internal */
export declare function resetChips(): void;
diff --git a/scripts/api-report/api-report.test.mjs b/scripts/api-report/api-report.test.mjs
index b5a1d9d57c..0d4823e875 100644
--- a/scripts/api-report/api-report.test.mjs
+++ b/scripts/api-report/api-report.test.mjs
@@ -2,10 +2,10 @@
/**
* @file Tests for the API report generator and the API changes renderer.
- * @input `__fixtures__/repo`: a built fixture package with every row kind, an
- * overloaded component, an `./internal/*` entry point, a stylesheet,
- * a component doc with theming targets, a private package, and a
- * public-attribute record
+ * @input `__fixtures__/repo`: a built fixture package with every row kind,
+ * overloaded components, hooks and functions, an `./internal/*` entry
+ * point, a stylesheet, a component doc with theming targets, a
+ * private package, and a public-attribute record
* @output Vitest assertions on report content, order, determinism, and change rows
* @position scripts/api-report — guards architecture:public-component-api INV24
*/
@@ -113,6 +113,35 @@ describe('generateReport', () => {
expect(report).toContain('internal Pill via .');
});
+ it("tags each overload of a hook or a function with its own declaration's tags", () => {
+ expect(report).toContain(
+ [
+ 'hook useChipMode',
+ " overload (mode: 'a') => string",
+ ' overload () => string @deprecated',
+ " overload (mode: 'b') => number",
+ ].join('\n'),
+ );
+ expect(report).toContain(
+ [
+ 'function parseChip',
+ ' overload (label: string, legacy: true) => string @deprecated',
+ ' overload (label: string) => string',
+ ].join('\n'),
+ );
+ });
+
+ it('tags the row of an overloaded hook or function only with the tags every overload has', () => {
+ expect(report).toMatch(/^function parseChip$/m);
+ expect(report).toContain(
+ [
+ 'function legacyChip @deprecated',
+ ' overload (label: string) => string @deprecated',
+ ' overload (label: string, size: ChipSize) => string @deprecated',
+ ].join('\n'),
+ );
+ });
+
it('leaves out ./internal/*, commented-out custom properties, and unlisted attributes', () => {
expect(report).not.toContain('secret');
expect(report).not.toContain('--commented-out');
diff --git a/scripts/api-report/generate.mjs b/scripts/api-report/generate.mjs
index 9c9401d2d5..6513790e0d 100644
--- a/scripts/api-report/generate.mjs
+++ b/scripts/api-report/generate.mjs
@@ -566,9 +566,31 @@ export async function generateReport(opts) {
]);
} else if (type.getCallSignatures().length) {
const hook = /^use[A-Z]/.test(name);
- groups[hook ? 'Hooks' : 'Functions'].push(
- sigLines(type, `${hook ? 'hook' : 'function'} ${name}${m}`),
- );
+ const head = `${hook ? 'hook' : 'function'} ${name}`;
+ const sigs = type.getCallSignatures();
+ const overloads = sigs.map(sig => sig.getDeclaration());
+ if (
+ sigs.length > 1 &&
+ overloads.every(d => d && ts.isFunctionDeclaration(d))
+ ) {
+ // Each overload carries its own tags, as a component's do. The
+ // row carries a tag only when every overload has it.
+ const tags = overloads.map(d => marks(d, name));
+ const shared = tags[0]
+ .split(' ')
+ .filter(
+ t => t && tags.every(each => each.split(' ').includes(t)),
+ );
+ rowTags = shared.length ? ` ${shared.join(' ')}` : '';
+ groups[hook ? 'Hooks' : 'Functions'].push([
+ `${head}${rowTags}`,
+ ...sigs.map((sig, i) => ` overload ${oneSig(sig)}${tags[i]}`),
+ ]);
+ } else {
+ groups[hook ? 'Hooks' : 'Functions'].push(
+ sigLines(type, `${head}${m}`),
+ );
+ }
} else {
const full = checker.typeToString(type, undefined, TF);
const named = decl?.type && ts.isTypeReferenceNode(decl.type);