perf: 优化插件类型生成代码

pull/56/head
moonrailgun 2 years ago
parent effa7c7f91
commit 218181d921

@ -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', {

@ -10,46 +10,72 @@ import {
interface Options {
entryPath: string;
project?: ProjectOptions;
hardcodeExportType?: Record<string, string>;
}
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<ts.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(', ')} }`;
}

@ -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();

@ -0,0 +1 @@
export { localTrans } from '../src/plugin/common/index';
Loading…
Cancel
Save