Skip to content

Commit 202746e

Browse files
committed
TS: Guard getTypeAtLocation with try/catch
1 parent 0388e9c commit 202746e

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

javascript/extractor/lib/typescript/src/ast_extractor.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ function forEachNode(ast: ts.Node, callback: (node: ts.Node) => void) {
6767
visit(ast);
6868
}
6969

70+
function tryGetTypeOfNode(typeChecker: ts.TypeChecker, node: AugmentedNode): ts.Type | null {
71+
try {
72+
return typeChecker.getTypeAtLocation(node);
73+
} catch (e) {
74+
let sourceFile = node.getSourceFile();
75+
let { line, character } = sourceFile.getLineAndCharacterOfPosition(node.pos);
76+
console.warn(`Could not compute type of ${ts.SyntaxKind[node.kind]} at ${sourceFile.fileName}:${line+1}:${character+1}`);
77+
return null;
78+
}
79+
}
80+
7081
export function augmentAst(ast: AugmentedSourceFile, code: string, project: Project | null) {
7182
ast.$lineStarts = ast.getLineStarts();
7283

@@ -196,7 +207,7 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
196207
let contextualType = isContextuallyTypedNode(node)
197208
? typeChecker.getContextualType(node)
198209
: null;
199-
let type = contextualType || typeChecker.getTypeAtLocation(node);
210+
let type = contextualType || tryGetTypeOfNode(typeChecker, node);
200211
if (type != null) {
201212
let parent = node.parent;
202213
let unfoldAlias = ts.isTypeAliasDeclaration(parent) && node === parent.type;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
| bar.ts:1:10:1:10 | A | any |
2+
| bar.ts:1:10:1:10 | A | any |
3+
| bar.ts:1:19:1:29 | "@blah/foo" | any |
4+
| bar.ts:3:5:3:5 | x | any |
5+
| node_modules/@blah/foo/index.d.ts:1:8:1:15 | FooArray | typeof FooArray in library-tests/TypeScript/RegressionTests/ImportSelf/node_modules/@blah/foo/index.d.ts |
6+
| node_modules/@blah/foo/index.d.ts:1:20:1:20 | A | any |
7+
| node_modules/@blah/foo/index.d.ts:1:20:1:20 | A | any |
8+
| node_modules/@blah/foo/index.d.ts:1:29:1:39 | '@blah/foo' | any |
9+
| node_modules/@blah/foo/index.d.ts:3:11:3:18 | FooArray | typeof FooArray in library-tests/TypeScript/RegressionTests/ImportSelf/node_modules/@blah/foo/index.d.ts |
10+
| node_modules/@blah/foo/index.d.ts:4:12:4:15 | blah | () => void |
11+
| node_modules/@blah/foo/index.d.ts:8:10:8:10 | A | any |
12+
| node_modules/@blah/foo/index.d.ts:8:10:8:10 | A | any |

0 commit comments

Comments
 (0)