Skip to content

Commit 4bddf34

Browse files
committed
Do not reimplement AST
1 parent 69b1686 commit 4bddf34

3 files changed

Lines changed: 30 additions & 385 deletions

File tree

indexer/src/cli.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import {writeFileSync} from 'fs'
22
import path from 'path'
33
import {homedir} from 'os'
44
import * as yargs from 'yargs'
5-
import * as Types from './types'
65

7-
import {ts} from 'ts-morph'
6+
import * as TsMorph from 'ts-morph'
87

9-
import extractFunctions from './index'
8+
import * as Tsearch from './index'
109

1110
// .option('directory', {type: 'positional'})
1211

@@ -19,20 +18,23 @@ const cli = yargs.option('out', {
1918

2019
const normalize = (p: string) => path.normalize(p.replace(/^~/, homedir()))
2120

22-
function main() {
21+
const main = () => {
2322
const {_: directories, out} = cli.argv
2423

25-
const results: Types.FunctionRecord[] = directories.flatMap(directory =>
26-
extractFunctions(normalize(directory), {
24+
const results: {
25+
name: string
26+
type: TsMorph.Structure
27+
}[] = directories.flatMap(directory =>
28+
Tsearch.findFunctions(normalize(directory), {
2729
compilerOptions: {
28-
target: ts.ScriptTarget.ES5,
29-
module: ts.ModuleKind.CommonJS,
30+
target: TsMorph.ts.ScriptTarget.ES5,
31+
module: TsMorph.ts.ModuleKind.CommonJS,
3032
},
3133
}),
3234
)
3335

3436
if (!out) {
35-
console.log(JSON.stringify(results))
37+
process.stdout.write(JSON.stringify(results) + '\n')
3638
return
3739
}
3840

indexer/src/index.ts

Lines changed: 19 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,120 +2,35 @@ import path from 'path'
22

33
import * 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

Comments
 (0)