Skip to content

Commit 4f5a347

Browse files
Copilotfregante
andcommitted
Add script to include example URLs in JSDocs for index.d.ts
- Created add-examples-to-dts.ts script that runs after build - Script imports index.ts to populate test data - Extracts example URLs using getTests from collector.ts - Adds @example JSDoc comments to each exported function in index.d.ts - Updated build script to run the new script after TypeScript compilation Co-authored-by: fregante <1402241+fregante@users.noreply.github.com>
1 parent 29bace4 commit 4f5a347

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

add-examples-to-dts.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env tsx
2+
import {readFileSync, writeFileSync} from 'node:fs';
3+
// Import index.ts first to populate the test data
4+
import './index.ts';
5+
import {getTests} from './collector.ts';
6+
7+
// Read the generated .d.ts file
8+
const dtsPath = './distribution/index.d.ts';
9+
const dtsContent = readFileSync(dtsPath, 'utf8');
10+
11+
// Process each exported function
12+
const lines = dtsContent.split('\n');
13+
const outputLines: string[] = [];
14+
15+
for (let i = 0; i < lines.length; i++) {
16+
const line = lines[i];
17+
18+
// Check if this is a function declaration
19+
const match = line.match(/^export declare const (\w+):/);
20+
if (match) {
21+
const functionName = match[1];
22+
23+
// Get the tests/examples for this function
24+
const examples = getTests(functionName);
25+
26+
// Only add examples if they exist and aren't just references to other functions
27+
if (examples && examples.length > 0 && examples[0] !== 'combinedTestOnly') {
28+
// Filter to only include actual URLs (not references to other functions)
29+
const urlExamples = examples.filter(url => url.startsWith('http'));
30+
31+
if (urlExamples.length > 0) {
32+
// Add JSDoc comment with examples before the declaration
33+
outputLines.push('/**');
34+
for (const url of urlExamples) {
35+
outputLines.push(` * @example ${url}`);
36+
}
37+
outputLines.push(' */');
38+
}
39+
}
40+
}
41+
42+
outputLines.push(line);
43+
}
44+
45+
// Write the modified content back
46+
writeFileSync(dtsPath, outputLines.join('\n'), 'utf8');
47+
48+
console.log('✓ Added example URLs to index.d.ts');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"distribution/index.d.ts"
2525
],
2626
"scripts": {
27-
"build": "run-p build:*",
27+
"build": "run-p build:esbuild build:typescript build:demo && npx tsx add-examples-to-dts.ts",
2828
"build:esbuild": "esbuild index.ts --bundle --external:github-reserved-names --outdir=distribution --format=esm --drop-labels=TEST",
2929
"build:typescript": "tsc --declaration --emitDeclarationOnly",
3030
"build:demo": "vite build demo",

0 commit comments

Comments
 (0)