Skip to content

Commit 18c2c58

Browse files
Improved type generation for functions
1 parent 3dc6494 commit 18c2c58

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

.github/FUNDING.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
This script creates Abstract Syntax Tree (AST) of all JS/TS files in JSON format.
44
The AST is created by using the bundled babel parser (for JavaScript, TypeScript).
5+
Type maps are generated using the Typescript compiler / type checker API.
56

67
## Supported languages
78

8-
| Language | Tool used |
9-
| ---------- | --------------------------- |
10-
| JavaScript | babel |
11-
| TypeScript | babel |
12-
| Vue | vue-template-compiler |
13-
| Svelte | svelte/compiler |
14-
| JSX | babel |
15-
| TSX | babel |
9+
| Language | Tool used | Notes |
10+
| ---------- | --------------------------- | ------------- |
11+
| JavaScript | babel | types via tsc |
12+
| TypeScript | babel | types via tsc |
13+
| Vue | babel | |
14+
| JSX | babel | |
15+
| TSX | babel | |
1616

1717
## Usage
1818

@@ -50,6 +50,6 @@ astgen
5050
To specify the project type and the path to the project.
5151

5252
```bash
53-
astgen -t nodejs -i <path to project>
53+
astgen -t js -i <path to project>
5454
astgen -t vue -i <path containing .vue files>
5555
```

index.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,29 @@ function createTsc(srcFiles) {
158158

159159
function addType(node) {
160160
let typeStr;
161-
if (tsc.isFunctionLike(node)) {
162-
const funcType = typeChecker.getTypeAtLocation(node);
163-
const funcSignature = typeChecker.getSignaturesOfType(funcType, tsc.SignatureKind.Call)[0];
164-
typeStr = typeChecker.typeToString(funcSignature.getReturnType(),
161+
if (tsc.isSetAccessor(node) ||
162+
tsc.isGetAccessor(node) ||
163+
tsc.isConstructSignatureDeclaration(node) ||
164+
tsc.isMethodDeclaration(node) ||
165+
tsc.isFunctionDeclaration(node) ||
166+
tsc.isConstructorDeclaration(node)) {
167+
const signature = typeChecker.getSignatureFromDeclaration(node);
168+
const returnType = typeChecker.getReturnTypeOfSignature(signature);
169+
typeStr = typeChecker.typeToString(returnType,
165170
tsc.TypeFormatFlags.NoTruncation | tsc.TypeFormatFlags.InTypeAlias
166171
);
172+
} else if (tsc.isFunctionLike(node)) {
173+
const funcType = typeChecker.getTypeAtLocation(node);
174+
const funcSignature = typeChecker.getSignaturesOfType(funcType, tsc.SignatureKind.Call)[0];
175+
if (funcSignature) {
176+
typeStr = typeChecker.typeToString(funcSignature.getReturnType(),
177+
tsc.TypeFormatFlags.NoTruncation | tsc.TypeFormatFlags.InTypeAlias
178+
);
179+
} else {
180+
typeStr = typeChecker.typeToString(typeChecker.getTypeAtLocation(node), node,
181+
tsc.TypeFormatFlags.NoTruncation | tsc.TypeFormatFlags.InTypeAlias
182+
);
183+
}
167184
} else {
168185
typeStr = typeChecker.typeToString(typeChecker.getTypeAtLocation(node), node,
169186
tsc.TypeFormatFlags.NoTruncation | tsc.TypeFormatFlags.InTypeAlias
@@ -203,7 +220,7 @@ const createJSAst = async (options) => {
203220
if (ts) {
204221
const tsAst = ts.program.getSourceFile(file);
205222
tsc.forEachChild(tsAst, ts.addType);
206-
writeTypesFile(tsAst.fileName, ts.seenTypes, options);
223+
writeTypesFile(file, ts.seenTypes, options);
207224
}
208225
} catch (err) {
209226
console.error(file, err.message);

0 commit comments

Comments
 (0)