diff --git a/packages/plugin-declaration-generator/src/index.ts b/packages/plugin-declaration-generator/src/index.ts index 914a3348..33d46eec 100644 --- a/packages/plugin-declaration-generator/src/index.ts +++ b/packages/plugin-declaration-generator/src/index.ts @@ -3,7 +3,7 @@ import traverse from '@babel/traverse'; import generate from '@babel/generator'; import template from '@babel/template'; import type { Comment } from '@babel/types'; -import { program } from '@babel/types'; +import { program, isFunctionDeclaration } from '@babel/types'; import fs from 'fs-extra'; import _ from 'lodash'; @@ -47,7 +47,7 @@ function getSourceCodeExportedFunction(sourcecode: string): ExportedItem[] { traverse(ast, { ExportNamedDeclaration({ node }) { if (node.declaration) { - if (node.declaration.type === 'FunctionDeclaration') { + if (isFunctionDeclaration(node.declaration)) { const name = node.declaration.id?.name; if (typeof name === 'string') { exported.push({ diff --git a/packages/plugin-declaration-generator/src/tsgenerator.ts b/packages/plugin-declaration-generator/src/tsgenerator.ts index 27d0d3a5..8db8a2c1 100644 --- a/packages/plugin-declaration-generator/src/tsgenerator.ts +++ b/packages/plugin-declaration-generator/src/tsgenerator.ts @@ -10,14 +10,44 @@ interface ExportModuleItem { comment?: string; } +export function parseModuleDeclaration( + filePath: string, + options: ts.CompilerOptions +) { + const program = parseFile(filePath, options); + const modules: Record = {}; + + const sourceFile = program?.getSourceFile(filePath); + sourceFile?.forEachChild((node) => { + if ( + ts.isModuleDeclaration(node) && + node.body && + ts.isModuleBlock(node.body) + ) { + const moduleName = node.name.text; + if (!modules[moduleName]) { + modules[moduleName] = []; + } + + node.body.forEachChild((item) => { + if (ts.isVariableStatement(item)) { + item.declarationList.declarations.forEach((declaration) => { + const name = declaration.name.getText(); + modules[moduleName].push(name); + }); + } + }); + } + }); + + return { modules }; +} + /** - * 解析文件 + * 解析导出文件 */ -export function parseFile(filePath: string, options: ts.CompilerOptions) { - const host = new FileServiceHost(filePath, options); - - const service = ts.createLanguageService(host, ts.createDocumentRegistry()); - const program = service.getProgram(); +export function parseExports(filePath: string, options: ts.CompilerOptions) { + const program = parseFile(filePath, options); const exportModules: ExportModuleItem[] = []; const sourceFile = program?.getSourceFile(filePath); @@ -46,6 +76,18 @@ export function parseFile(filePath: string, options: ts.CompilerOptions) { return { exportModules }; } +/** + * 解析文件 + */ +export function parseFile(filePath: string, options: ts.CompilerOptions) { + const host = new FileServiceHost(filePath, options); + + const service = ts.createLanguageService(host, ts.createDocumentRegistry()); + const program = service.getProgram(); + + return program; +} + function isExportFunc(node: ts.Node): node is ts.FunctionDeclaration { if (ts.isFunctionDeclaration(node)) { if (node.modifiers) { diff --git a/packages/plugin-declaration-generator/test/index.d.ts b/packages/plugin-declaration-generator/test/index.d.ts new file mode 100644 index 00000000..b11ae915 --- /dev/null +++ b/packages/plugin-declaration-generator/test/index.d.ts @@ -0,0 +1,4 @@ +declare module '@capital/foo' { + export const a: any; + export const b: any; +} diff --git a/packages/plugin-declaration-generator/test/index.ts b/packages/plugin-declaration-generator/test/index.ts index 89f01bbd..c1bb3362 100644 --- a/packages/plugin-declaration-generator/test/index.ts +++ b/packages/plugin-declaration-generator/test/index.ts @@ -1,11 +1,20 @@ -import { parseFile } from '../src/tsgenerator'; +import { parseExports, parseModuleDeclaration } from '../src/tsgenerator'; import path from 'path'; -const { exportModules } = parseFile( +const { exportModules } = parseExports( path.resolve(__dirname, './demo/index.ts'), { paths: { '@/*': ['./*'] }, } ); -console.log(exportModules); +console.log('exportModules', exportModules); + +const { modules } = parseModuleDeclaration( + path.resolve(__dirname, './index.d.ts'), + { + paths: { '@/*': ['./*'] }, + } +); + +console.log('modules', modules);