diff --git a/client/packages/plugin-declaration-generator/src/index.ts b/client/packages/plugin-declaration-generator/src/index.ts index 9032b576..255dc587 100644 --- a/client/packages/plugin-declaration-generator/src/index.ts +++ b/client/packages/plugin-declaration-generator/src/index.ts @@ -8,6 +8,7 @@ import fs from 'fs-extra'; import _ from 'lodash'; export * from './tsgenerator'; +export * from './parser'; const babelPlugins: ParserPlugin[] = ['jsx', 'typescript']; const buildNamedExport = template('export function %%name%%(): any', { diff --git a/client/packages/plugin-declaration-generator/src/parser.ts b/client/packages/plugin-declaration-generator/src/parser.ts index 34763710..23416ff8 100644 --- a/client/packages/plugin-declaration-generator/src/parser.ts +++ b/client/packages/plugin-declaration-generator/src/parser.ts @@ -10,46 +10,72 @@ import { interface Options { entryPath: string; project?: ProjectOptions; + hardcodeExportType?: Record; } export function parseDeclarationEntry(options: Options) { const project = new Project(options.project); const sourceFile = project.getSourceFileOrThrow(options.entryPath); + const hardcodeExportType = options.hardcodeExportType ?? {}; + const exportDefs: { name: string; type: string }[] = []; for (const [name, declarations] of sourceFile.getExportedDeclarations()) { - console.log( - `${name}: ${declarations - .map((d) => { - if (d.isKind(SyntaxKind.FunctionDeclaration)) { - return d - .getType() - .getCallSignatures() - .map( - (s) => - `(${s - .getParameters() - .map((s) => parseFunctionParameter(s)) - .join(', ')}) => ${s.getReturnType().getText()}` - ) - .join(' | '); - } else { - return d.getType().getText(); - } - }) - .join(' | ')}` - ); + if (hardcodeExportType[name]) { + exportDefs.push({ + name, + type: hardcodeExportType[name], + }); + continue; + } + + console.log('parsing:', name); + + const typeDef = declarations + .map((d) => { + if (d.isKind(SyntaxKind.FunctionDeclaration)) { + // 如果是方法导出 + return d + .getType() + .getCallSignatures() + .map((s) => { + let fnText = ''; + const typeParameters = s.getTypeParameters(); + if (typeParameters.length > 0) { + fnText += `<${typeParameters + .map((tp) => tp.getText()) + .join(', ')}>`; + } + + fnText += `(${s + .getParameters() + .map((p) => { + return parseFunctionParameter(p); + }) + .join(', ')}) => ${s.getReturnType().getText()}`; + + return fnText; + }) + .join(' | '); + } else { + // 其他 + return d.getType().getText(); + } + }) + .join(' | '); + + exportDefs.push({ + name, + type: typeDef, + }); } - return project; + return { exportDefs, project }; } /** * 解析函数参数为字符串 */ function parseFunctionParameter(parameter: Symbol): string { - debugger; - parameter.getFlags(); - const name = parameter.getName(); const isOptional = parameter.isOptional(); const type = parseType(parameter.getDeclarations()[0].getType()); @@ -65,12 +91,20 @@ function parseFunctionParameter(parameter: Symbol): string { * 解析函数 */ function parseType(type: Type): string { + if (type.isAnonymous()) { + return type.getText(); + } + if (type.isObject()) { const properties = type.getApparentProperties(); + debugger; + return `{ ${properties - .map( - (p) => `${p.getName()}: ${parseType(p.getDeclarations()[0].getType())}` - ) + .map((p) => { + const t = p.getDeclarations()[0].getType(); + const text = parseType(t); + return `${p.getName()}: ${text}`; + }) .join(', ')} }`; } diff --git a/client/web/scripts/generate-plugin-declaration.tsmorph.ts b/client/web/scripts/generate-plugin-declaration.tsmorph.ts new file mode 100644 index 00000000..30944ab1 --- /dev/null +++ b/client/web/scripts/generate-plugin-declaration.tsmorph.ts @@ -0,0 +1,15 @@ +import { parseDeclarationEntry } from 'tailchat-plugin-declaration-generator'; +import path from 'path'; + +// WIP +function generateDeclarationFile() { + const { exportDefs } = parseDeclarationEntry({ + entryPath: path.resolve(__dirname, './test-export.ts'), + project: { + tsConfigFilePath: path.resolve(__dirname, '../tsconfig.json'), + }, + }); + console.log(exportDefs); +} + +generateDeclarationFile(); diff --git a/client/web/scripts/test-export.ts b/client/web/scripts/test-export.ts new file mode 100644 index 00000000..35518140 --- /dev/null +++ b/client/web/scripts/test-export.ts @@ -0,0 +1 @@ +export { localTrans } from '../src/plugin/common/index';