-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathadd-examples-to-dts.ts
More file actions
144 lines (118 loc) · 4.49 KB
/
add-examples-to-dts.ts
File metadata and controls
144 lines (118 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* eslint-disable n/prefer-global/process, unicorn/no-process-exit, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-argument */
import {readFileSync, writeFileSync} from 'node:fs';
import {execSync} from 'node:child_process';
import {Project, type JSDocableNode} from 'ts-morph';
// Import index.ts to populate the test data via side effect
// eslint-disable-next-line import-x/no-unassigned-import
import './index.ts';
import {getTests} from './collector.ts';
// Read the generated .d.ts file
const dtsPath = './distribution/index.d.ts';
const dtsContent = readFileSync(dtsPath, 'utf8');
// Check if script has already been run
const marker = '/* Examples added by add-examples-to-dts.ts */';
if (dtsContent.includes(marker)) {
console.error('❌ Error: Examples have already been added to this file');
process.exit(1);
}
// Create a ts-morph project and load the file
const project = new Project();
const sourceFile = project.createSourceFile(dtsPath, dtsContent, {overwrite: true});
let examplesAdded = 0;
/**
* Add example URLs to a JSDocable node (e.g., variable statement or type alias)
*/
function addExamplesToNode(node: JSDocableNode, urlExamples: string[]): void {
const jsDoc = node.getJsDocs().at(0);
if (jsDoc) {
// Add @example tags to existing JSDoc
const existingTags = jsDoc.getTags();
const description = jsDoc.getDescription().trim();
// Build new JSDoc content
const newJsDocLines: string[] = [];
if (description) {
newJsDocLines.push(description);
}
// Add existing tags (that aren't @example tags)
for (const tag of existingTags) {
if (tag.getTagName() !== 'example') {
newJsDocLines.push(tag.getText());
}
}
// Add new @example tags
for (const url of urlExamples) {
newJsDocLines.push(`@example ${url}`);
}
// Replace the JSDoc
jsDoc.remove();
node.addJsDoc(newJsDocLines.join('\n'));
} else {
// Create new JSDoc with examples
const jsDocLines: string[] = [];
for (const url of urlExamples) {
jsDocLines.push(`@example ${url}`);
}
node.addJsDoc(jsDocLines.join('\n'));
}
}
// Process each exported variable declaration (these are the function declarations)
for (const statement of sourceFile.getVariableStatements()) {
// Only process exported statements
if (!statement.isExported()) {
continue;
}
for (const declaration of statement.getDeclarations()) {
const functionName = declaration.getName();
// Get the tests/examples for this function
const examples = getTests(functionName);
// Only add examples if they exist and aren't the special 'combinedTestOnly' marker
if (examples && examples.length > 0 && examples[0] !== 'combinedTestOnly') {
// Filter to only include actual URLs (not references to other functions)
const urlExamples = examples.filter((url: string) => url.startsWith('http'));
if (urlExamples.length > 0) {
addExamplesToNode(statement, urlExamples);
examplesAdded += urlExamples.length;
}
}
}
}
// Also process exported type aliases (like RepoExplorerInfo)
for (const typeAlias of sourceFile.getTypeAliases()) {
if (!typeAlias.isExported()) {
continue;
}
const typeName = typeAlias.getName();
// Get the tests/examples for this type (unlikely but keeping consistency)
const examples = getTests(typeName);
if (examples && examples.length > 0 && examples[0] !== 'combinedTestOnly') {
const urlExamples = examples.filter((url: string) => url.startsWith('http'));
if (urlExamples.length > 0) {
addExamplesToNode(typeAlias, urlExamples);
examplesAdded += urlExamples.length;
}
}
}
// Validate that we added some examples
if (examplesAdded === 0) {
console.error('❌ Error: No examples were added. This likely indicates a problem with the script.');
process.exit(1);
}
// Get the modified content and add marker
const modifiedContent = sourceFile.getFullText();
const finalContent = `${marker}\n${modifiedContent}`;
// Write the modified content back
writeFileSync(dtsPath, finalContent, 'utf8');
console.log(`✓ Added ${examplesAdded} example URLs to index.d.ts`);
// Validate with TypeScript
try {
execSync('npx tsc --noEmit distribution/index.d.ts', {
cwd: process.cwd(),
stdio: 'pipe',
});
console.log('✓ TypeScript validation passed');
} catch (error: unknown) {
console.error('❌ TypeScript validation failed:');
const execError = error as {stdout?: Uint8Array; stderr?: Uint8Array; message?: string};
console.error(execError.stdout?.toString() ?? execError.stderr?.toString() ?? execError.message);
process.exit(1);
}