feat: add parseModuleDeclaration

feat/desktop
moonrailgun 3 years ago
parent 0b5ecf9777
commit 384f7000fb

@ -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({

@ -10,14 +10,44 @@ interface ExportModuleItem {
comment?: string;
}
export function parseModuleDeclaration(
filePath: string,
options: ts.CompilerOptions
) {
const program = parseFile(filePath, options);
const modules: Record<string, any[]> = {};
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) {

@ -0,0 +1,4 @@
declare module '@capital/foo' {
export const a: any;
export const b: any;
}

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

Loading…
Cancel
Save