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

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

@ -8,6 +8,7 @@ import fs from 'fs-extra';
import _ from 'lodash'; import _ from 'lodash';
export * from './tsgenerator'; export * from './tsgenerator';
export * from './parser';
const babelPlugins: ParserPlugin[] = ['jsx', 'typescript']; const babelPlugins: ParserPlugin[] = ['jsx', 'typescript'];
const buildNamedExport = template('export function %%name%%(): any', { const buildNamedExport = template('export function %%name%%(): any', {

@ -10,46 +10,72 @@ import {
interface Options { interface Options {
entryPath: string; entryPath: string;
project?: ProjectOptions; project?: ProjectOptions;
hardcodeExportType?: Record<string, string>;
} }
export function parseDeclarationEntry(options: Options) { export function parseDeclarationEntry(options: Options) {
const project = new Project(options.project); const project = new Project(options.project);
const sourceFile = project.getSourceFileOrThrow(options.entryPath); const sourceFile = project.getSourceFileOrThrow(options.entryPath);
const hardcodeExportType = options.hardcodeExportType ?? {};
const exportDefs: { name: string; type: string }[] = [];
for (const [name, declarations] of sourceFile.getExportedDeclarations()) { for (const [name, declarations] of sourceFile.getExportedDeclarations()) {
console.log( if (hardcodeExportType[name]) {
`${name}: ${declarations exportDefs.push({
.map((d) => { name,
if (d.isKind(SyntaxKind.FunctionDeclaration)) { type: hardcodeExportType[name],
return d });
.getType() continue;
.getCallSignatures() }
.map(
(s) => console.log('parsing:', name);
`(${s
.getParameters() const typeDef = declarations
.map((s) => parseFunctionParameter(s)) .map((d) => {
.join(', ')}) => ${s.getReturnType().getText()}` if (d.isKind(SyntaxKind.FunctionDeclaration)) {
) // 如果是方法导出
.join(' | '); return d
} else { .getType()
return d.getType().getText(); .getCallSignatures()
} .map((s) => {
}) let fnText = '';
.join(' | ')}` 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 { function parseFunctionParameter(parameter: Symbol): string {
debugger;
parameter.getFlags();
const name = parameter.getName(); const name = parameter.getName();
const isOptional = parameter.isOptional(); const isOptional = parameter.isOptional();
const type = parseType(parameter.getDeclarations()[0].getType()); const type = parseType(parameter.getDeclarations()[0].getType());
@ -65,12 +91,20 @@ function parseFunctionParameter(parameter: Symbol): string {
* *
*/ */
function parseType(type: Type<ts.Type>): string { function parseType(type: Type<ts.Type>): string {
if (type.isAnonymous()) {
return type.getText();
}
if (type.isObject()) { if (type.isObject()) {
const properties = type.getApparentProperties(); const properties = type.getApparentProperties();
debugger;
return `{ ${properties return `{ ${properties
.map( .map((p) => {
(p) => `${p.getName()}: ${parseType(p.getDeclarations()[0].getType())}` const t = p.getDeclarations()[0].getType();
) const text = parseType(t);
return `${p.getName()}: ${text}`;
})
.join(', ')} }`; .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