fix(types): preserve complete nested types during post-processing
Use the TypeScript parser instead of a regex that truncates nested generics and function types. Validate generated syntax and add a regression test. Include workspace aliases and PTY ambient declarations. The full type package build still reports six errors in third-party node-pty source; this commit does not claim to resolve that remaining issue.
This commit is contained in:
1 parent
9109b8cfe2
commit
4f1401da78
4 files changed
+77
-6
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { readdir, readFile, writeFile, rename } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import ts from 'typescript';
|
||||
|
||||
const __dirname = fileURLToPath(new URL('../', import.meta.url));
|
||||
const distDir = join(__dirname, 'dist');
|
||||
@@ -41,9 +42,21 @@ function replaceExternalTypes (content) {
|
||||
let result = content;
|
||||
|
||||
// 替换带泛型的类型(先处理复杂的)
|
||||
result = result.replace(/NapProtoDecodeStructType<[^>]+>/g, 'any');
|
||||
result = result.replace(/NapProtoEncodeStructType<[^>]+>/g, 'any');
|
||||
result = result.replace(/ValidateFunction<[^>]+>/g, 'any');
|
||||
const sourceFile = ts.createSourceFile('declaration.d.ts', result, ts.ScriptTarget.Latest, true);
|
||||
const genericTypeNames = new Set(['NapProtoDecodeStructType', 'NapProtoEncodeStructType', 'ValidateFunction']);
|
||||
const replacements = [];
|
||||
const visit = (node) => {
|
||||
const typeName = ts.isTypeReferenceNode(node) ? node.typeName : ts.isImportTypeNode(node) ? node.qualifier : undefined;
|
||||
if (typeName && genericTypeNames.has(typeName.getText(sourceFile).split('.').at(-1))) {
|
||||
replacements.push({ start: node.getStart(sourceFile), end: node.getEnd() });
|
||||
return;
|
||||
}
|
||||
ts.forEachChild(node, visit);
|
||||
};
|
||||
visit(sourceFile);
|
||||
for (const replacement of replacements.reverse()) {
|
||||
result = result.slice(0, replacement.start) + 'any' + result.slice(replacement.end);
|
||||
}
|
||||
|
||||
// 替换 winston.Logger 等带命名空间的类型
|
||||
result = result.replace(/winston\.Logger/g, 'any');
|
||||
@@ -103,6 +116,11 @@ async function processFile (filePath) {
|
||||
// 3. Replace "export declare enum" with "export enum"
|
||||
content = content.replace(/export declare enum/g, 'export enum');
|
||||
|
||||
const parsed = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
|
||||
if (parsed.parseDiagnostics.length > 0) {
|
||||
throw new Error(`Invalid generated declaration ${filePath}: ${parsed.parseDiagnostics.map(diagnostic => ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')).join('\n')}`);
|
||||
}
|
||||
|
||||
// Write back the modified content
|
||||
await writeFile(filePath, content, 'utf-8');
|
||||
|
||||
|
||||
Reference in new issue
Block a user