@@ -14,33 +14,47 @@ export const extractAssertions = (program: Program): Map<Assertion, Set<CallExpr
1414 const checker = program . getTypeChecker ( ) ;
1515
1616 /**
17- * Recursively loop over all the nodes and extract all the assertions out of the source files .
17+ * Checks if the given node is semantically valid and is an assertion .
1818 */
19- function walkNodes ( node : Node ) {
20- if ( isCallExpression ( node ) ) {
21- const expression = isPropertyAccessExpression ( node . expression ) ?
22- node . expression . name :
23- node . expression ;
19+ function handleNode ( node : CallExpression ) {
20+ const expression = isPropertyAccessExpression ( node . expression ) ?
21+ node . expression . name :
22+ node . expression ;
23+
24+ const maybeSymbol = checker . getSymbolAtLocation ( expression ) ;
25+
26+ if ( ! maybeSymbol ) {
27+ // Bail out if a Symbol doesn't exist for this Node
28+ // This either means a symbol could not be resolved
29+ // for an identifier, or that the expression is
30+ // syntactically valid, but not semantically valid.
31+ return ;
32+ }
33+
34+ const symbol = maybeSymbol . flags & SymbolFlags . Alias ?
35+ checker . getAliasedSymbol ( maybeSymbol ) :
36+ maybeSymbol ;
2437
25- const maybeAlias = checker . getSymbolAtLocation ( expression ) ;
26- if ( maybeAlias ) {
27- const symbol = maybeAlias . flags & SymbolFlags . Alias ?
28- checker . getAliasedSymbol ( maybeAlias ) :
29- maybeAlias ;
38+ const identifier = symbol . getName ( ) ;
3039
31- const identifier = symbol . getName ( ) ;
40+ // Check if the call type is a valid assertion
41+ if ( assertionFnNames . has ( identifier ) ) {
42+ const assertion = identifier as Assertion ;
3243
33- // Check if the call type is a valid assertion
34- if ( assertionFnNames . has ( identifier ) ) {
35- const assertion = identifier as Assertion ;
44+ const nodes = assertions . get ( assertion ) ?? new Set < CallExpression > ( ) ;
3645
37- const nodes = assertions . get ( assertion ) ?? new Set < CallExpression > ( ) ;
46+ nodes . add ( node ) ;
3847
39- nodes . add ( node ) ;
48+ assertions . set ( assertion , nodes ) ;
49+ }
50+ }
4051
41- assertions . set ( assertion , nodes ) ;
42- }
43- }
52+ /**
53+ * Recursively loop over all the nodes and extract all the assertions out of the source files.
54+ */
55+ function walkNodes ( node : Node ) {
56+ if ( isCallExpression ( node ) ) {
57+ handleNode ( node ) ;
4458 }
4559
4660 forEachChild ( node , walkNodes ) ;
0 commit comments