@@ -2,120 +2,35 @@ import path from 'path'
22
33import * as ts from 'ts-morph'
44
5- import { FunctionRecord , callSignature } from './types'
6-
7- export default function findFunctions (
5+ export const findFunctions = (
86 directory : string ,
97 options : ts . ProjectOptions ,
10- ) : FunctionRecord [ ] {
8+ ) : { name : string ; type : ts . Structure } [ ] => {
119 const project = new ts . Project ( {
1210 ...options ,
1311 addFilesFromTsConfig : false ,
1412 skipFileDependencyResolution : true ,
1513 } )
1614
1715 const filesGlob = path . join ( directory , '**' , '*.ts' )
18- let packageJson
19- try {
20- packageJson = require ( path . join ( directory , 'package.json' ) )
21- } catch ( e ) {
22- packageJson = { }
23- }
24-
25- const moduleName =
26- packageJson ?. name ?? path . relative ( path . join ( directory , '..' ) , directory )
2716
2817 project . addSourceFilesAtPaths ( filesGlob )
2918
30- return project . getSourceFiles ( ) . reduce < FunctionRecord [ ] > ( ( acc , file ) => {
31- const fileName = path . relative ( directory , file . getFilePath ( ) )
32-
33- const entries = Array . from ( file . getExportedDeclarations ( ) . entries ( ) )
34-
35- const fns = entries . reduce < FunctionRecord [ ] > (
36- ( acc , [ _indentifier , exports ] ) => {
37- exports . forEach ( exp => {
38- if ( ts . TypeGuards . isFunctionDeclaration ( exp ) ) {
39- acc . push ( functionDeclaration ( exp , fileName , moduleName ) )
40- return
41- }
42-
43- if ( ts . TypeGuards . isVariableDeclaration ( exp ) ) {
44- const fn = arrowFunction ( exp , fileName , moduleName )
45- if ( fn ) {
46- acc . push ( fn )
47- }
48- }
49- } )
50-
51- return acc
52- } ,
53- [ ] ,
54- )
55-
56- acc . push ( ...fns )
57- return acc
58- } , [ ] )
59- }
60-
61- function functionDeclaration (
62- node : ts . FunctionDeclaration ,
63- file : string ,
64- module : string ,
65- ) : FunctionRecord {
66- return {
67- name : node . getName ( ) ,
68- text : node . getFullText ( ) ,
69- docs : node
70- . getJsDocs ( )
71- . map ( doc => doc . getFullText ( ) )
72- . join ( '' ) ,
73- signature : callSignature ( {
74- typeParameters : node . getTypeParameters ( ) ,
75- parameters : node . getParameters ( ) ,
76- returnType : node . getReturnType ( ) ,
77- } ) ,
78- module,
79- location : {
80- path : file ,
81- lines : {
82- from : node . getStartLineNumber ( ) ,
83- to : node . getEndLineNumber ( ) ,
84- } ,
85- } ,
86- }
87- }
88-
89- function arrowFunction (
90- node : ts . VariableDeclaration ,
91- file : string ,
92- module : string ,
93- ) : FunctionRecord | undefined {
94- const [ arrow ] = node . getChildrenOfKind ( ts . SyntaxKind . ArrowFunction )
95-
96- if ( ! arrow ) {
97- return undefined
98- }
99-
100- return {
101- name : node . getName ( ) ,
102- text : `const${ node . getFullText ( ) } ` ,
103- docs : arrow
104- . getJsDocs ( )
105- . map ( doc => doc . getFullText ( ) )
106- . join ( '' ) ,
107- signature : callSignature ( {
108- typeParameters : arrow . getTypeParameters ( ) ,
109- parameters : arrow . getParameters ( ) ,
110- returnType : arrow . getReturnType ( ) ,
111- } ) ,
112- module,
113- location : {
114- path : file ,
115- lines : {
116- from : node . getStartLineNumber ( ) ,
117- to : node . getEndLineNumber ( ) ,
118- } ,
119- } ,
120- }
19+ const sourceFiles = project . getSourceFiles ( )
20+ const decls : { name : string ; type : ts . Structure } [ ] = sourceFiles . flatMap ( x =>
21+ Array . from ( x . getExportedDeclarations ( ) . values ( ) ) . flatMap ( decls =>
22+ decls . flatMap ( decl => {
23+ // ((decl as any).getTypeNode() as any).getStructure(),
24+ if ( ! ( 'getTypeNode' in decl ) ) return [ ]
25+ const typeNode = decl . getTypeNode ( )
26+ if ( typeNode === undefined || ! ( 'getStructure' in typeNode ) ) return [ ]
27+ const node = {
28+ name : decl . getName ( ) ,
29+ type : ( typeNode as any ) . getStructure ( ) ,
30+ }
31+ return [ node ]
32+ } ) ,
33+ ) ,
34+ )
35+ return decls
12136}
0 commit comments