Skip to content

Commit 002259c

Browse files
Several performance improvements
* ignore *.d.ts files * clear seen types map after writing a *.typemap file as we do type generation per file only anyway
1 parent 8553cd4 commit 002259c

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

index.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const IGNORE_DIRS = [
2626
"build",
2727
];
2828

29-
const IGNORE_FILE_PATTERN = new RegExp("(conf|test|spec)\\.(js|ts)$", "i");
29+
const IGNORE_FILE_PATTERN = new RegExp("(conf|test|spec|\\.d)\\.(js|ts)$", "i");
3030

3131
const getAllFiles = (dir, extn, files, result, regex) => {
3232
files = files || fs.readdirSync(dir);
@@ -137,7 +137,6 @@ const toVueAst = (file) => {
137137
);
138138
};
139139

140-
141140
function createTsc(srcFiles) {
142141
try {
143142
const program = tsc.createProgram(srcFiles, {
@@ -158,7 +157,7 @@ function createTsc(srcFiles) {
158157
}
159158
}
160159

161-
function safeTypeToString(node, context) {
160+
function safeTypeWithContextToString(node, context) {
162161
try {
163162
return typeChecker.typeToString(node, context,
164163
tsc.TypeFormatFlags.NoTruncation | tsc.TypeFormatFlags.InTypeAlias
@@ -185,10 +184,10 @@ function createTsc(srcFiles) {
185184
if (funcSignature) {
186185
typeStr = safeTypeToString(funcSignature.getReturnType());
187186
} else {
188-
typeStr = safeTypeToString(typeChecker.getTypeAtLocation(node), node);
187+
typeStr = safeTypeWithContextToString(typeChecker.getTypeAtLocation(node), node);
189188
}
190189
} else {
191-
typeStr = safeTypeToString(typeChecker.getTypeAtLocation(node), node);
190+
typeStr = safeTypeWithContextToString(typeChecker.getTypeAtLocation(node), node);
192191
}
193192
seenTypes.set(node.getStart(), typeStr);
194193
tsc.forEachChild(node, addType);
@@ -206,7 +205,6 @@ function createTsc(srcFiles) {
206205
}
207206
}
208207

209-
210208
/**
211209
* Generate AST for JavaScript or TypeScript
212210
*/
@@ -231,6 +229,7 @@ const createJSAst = async (options) => {
231229
const tsAst = ts.program.getSourceFile(file);
232230
tsc.forEachChild(tsAst, ts.addType);
233231
writeTypesFile(file, ts.seenTypes, options);
232+
ts.seenTypes.clear();
234233
} catch (err) {
235234
console.warn("Retrieving types", file, ":", err.message);
236235
}
@@ -258,7 +257,6 @@ const createVueAst = async (options) => {
258257
}
259258
};
260259

261-
262260
/**
263261
* Deal with cyclic reference in json
264262
*/
@@ -313,7 +311,6 @@ const writeTypesFile = (file, seenTypes, options) => {
313311
console.log("Converted types for", relativePath, "to", outTypeFile);
314312
};
315313

316-
317314
const createXAst = async (options) => {
318315
const src_dir = options.src;
319316
try {
@@ -328,7 +325,7 @@ const createXAst = async (options) => {
328325
) {
329326
return await createJSAst(options);
330327
}
331-
console.error(src_dir, "unkown project type");
328+
console.error(src_dir, "unknown project type");
332329
process.exit(1);
333330
};
334331

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@joernio/astgen",
3-
"version": "2.19.0",
3+
"version": "2.20.0",
44
"description": "Generate JS/TS AST in json format with Babel",
55
"exports": "./index.js",
66
"keywords": [

0 commit comments

Comments
 (0)