@@ -10,19 +10,13 @@ export interface AugmentedSourceFile extends ts.SourceFile {
1010 /** Internal property that we expose as a workaround. */
1111 redirectInfo ?: object | null ;
1212 $tokens ?: Token [ ] ;
13- $symbol ?: number ;
1413 $lineStarts ?: ReadonlyArray < number > ;
1514}
1615
1716export interface AugmentedNode extends ts . Node {
1817 $pos ?: any ;
1918 $end ?: any ;
2019 $declarationKind ?: string ;
21- $type ?: number ;
22- $symbol ?: number ;
23- $resolvedSignature ?: number ;
24- $overloadIndex ?: number ;
25- $declaredSignature ?: number ;
2620}
2721
2822export type AugmentedPos = number ;
@@ -73,7 +67,7 @@ function tryGetTypeOfNode(typeChecker: ts.TypeChecker, node: AugmentedNode): ts.
7367 } catch ( e ) {
7468 let sourceFile = node . getSourceFile ( ) ;
7569 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 } ` ) ;
70+ console . warn ( `Could not compute type of ${ ts . SyntaxKind [ node . kind ] } at ${ sourceFile . fileName } :${ line + 1 } :${ character + 1 } ` ) ;
7771 return null ;
7872 }
7973}
@@ -157,17 +151,6 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
157151 } ) ;
158152 }
159153
160- let typeChecker = project && project . program . getTypeChecker ( ) ;
161- let typeTable = project && project . typeTable ;
162-
163- // Associate a symbol with the AST node root, in case it is a module.
164- if ( typeTable != null ) {
165- let symbol = typeChecker . getSymbolAtLocation ( ast ) ;
166- if ( symbol != null ) {
167- ast . $symbol = typeTable . getSymbolId ( symbol ) ;
168- }
169- }
170-
171154 visitAstNode ( ast ) ;
172155 function visitAstNode ( node : AugmentedNode ) {
173156 ts . forEachChild ( node , visitAstNode ) ;
@@ -190,192 +173,5 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
190173 node . $declarationKind = "var" ;
191174 }
192175 }
193-
194- if ( typeChecker != null ) {
195- if ( isTypedNode ( node ) && ! typeTable . skipExtractingTypes ) {
196- let contextualType = isContextuallyTypedNode ( node )
197- ? typeChecker . getContextualType ( node )
198- : null ;
199- let type = contextualType || tryGetTypeOfNode ( typeChecker , node ) ;
200- if ( type != null ) {
201- let parent = node . parent ;
202- let unfoldAlias = ts . isTypeAliasDeclaration ( parent ) && node === parent . type ;
203- let id = typeTable . buildType ( type , unfoldAlias ) ;
204- if ( id != null ) {
205- node . $type = id ;
206- }
207- }
208- // Extract the target call signature of a function call.
209- // In case the callee is overloaded or generic, this is not something we can
210- // derive from the callee type in QL.
211- if ( ts . isCallOrNewExpression ( node ) ) {
212- let kind = ts . isCallExpression ( node ) ? ts . SignatureKind . Call : ts . SignatureKind . Construct ;
213- let resolvedSignature = typeChecker . getResolvedSignature ( node ) ;
214- if ( resolvedSignature != null ) {
215- let resolvedId = typeTable . getSignatureId ( kind , resolvedSignature ) ;
216- if ( resolvedId != null ) {
217- ( node as AugmentedNode ) . $resolvedSignature = resolvedId ;
218- }
219- let declaration = resolvedSignature . declaration ;
220- if ( declaration != null ) {
221- // Find the generic signature, i.e. without call-site type arguments substituted,
222- // but with overloading resolved.
223- let calleeType = typeChecker . getTypeAtLocation ( node . expression ) ;
224- if ( calleeType != null && declaration != null ) {
225- let calleeSignatures = typeChecker . getSignaturesOfType ( calleeType , kind ) ;
226- for ( let i = 0 ; i < calleeSignatures . length ; ++ i ) {
227- if ( calleeSignatures [ i ] . declaration === declaration ) {
228- ( node as AugmentedNode ) . $overloadIndex = i ;
229- break ;
230- }
231- }
232- }
233- // Extract the symbol so the declaration can be found from QL.
234- let name = ( declaration as any ) . name ;
235- let symbol = name && typeChecker . getSymbolAtLocation ( name ) ;
236- if ( symbol != null ) {
237- ( node as AugmentedNode ) . $symbol = typeTable . getSymbolId ( symbol ) ;
238- }
239- }
240- }
241- }
242- }
243- let symbolNode =
244- isNamedNodeWithSymbol ( node ) ? node . name :
245- ts . isImportDeclaration ( node ) ? node . moduleSpecifier :
246- ts . isExternalModuleReference ( node ) ? node . expression :
247- null ;
248- if ( symbolNode != null ) {
249- let symbol = typeChecker . getSymbolAtLocation ( symbolNode ) ;
250- if ( symbol != null ) {
251- node . $symbol = typeTable . getSymbolId ( symbol ) ;
252- }
253- }
254- if ( ts . isTypeReferenceNode ( node ) ) {
255- // For type references we inject a symbol on each part of the name.
256- // We traverse each node in the name here since we know these are part of
257- // a type annotation. This means we don't have to do it for all identifiers
258- // and qualified names, which would extract more information than we need.
259- let namePart : ( ts . EntityName & AugmentedNode ) = node . typeName ;
260- while ( ts . isQualifiedName ( namePart ) ) {
261- let symbol = typeChecker . getSymbolAtLocation ( namePart . right ) ;
262- if ( symbol != null ) {
263- namePart . $symbol = typeTable . getSymbolId ( symbol ) ;
264- }
265-
266- // Traverse into the prefix.
267- namePart = namePart . left ;
268- }
269- let symbol = typeChecker . getSymbolAtLocation ( namePart ) ;
270- if ( symbol != null ) {
271- namePart . $symbol = typeTable . getSymbolId ( symbol ) ;
272- }
273- }
274- if ( ts . isFunctionLike ( node ) ) {
275- let signature = typeChecker . getSignatureFromDeclaration ( node ) ;
276- if ( signature != null ) {
277- let kind = ts . isConstructSignatureDeclaration ( node ) || ts . isConstructorDeclaration ( node )
278- ? ts . SignatureKind . Construct : ts . SignatureKind . Call ;
279- let id = typeTable . getSignatureId ( kind , signature ) ;
280- if ( id != null ) {
281- ( node as AugmentedNode ) . $declaredSignature = id ;
282- }
283- }
284- }
285- }
286- }
287- }
288-
289- type NamedNodeWithSymbol = AugmentedNode & ( ts . ClassDeclaration | ts . InterfaceDeclaration
290- | ts . TypeAliasDeclaration | ts . EnumDeclaration | ts . EnumMember | ts . ModuleDeclaration | ts . FunctionDeclaration
291- | ts . MethodDeclaration | ts . MethodSignature ) ;
292-
293- /**
294- * True if the given AST node has a name, and should be associated with a symbol.
295- */
296- function isNamedNodeWithSymbol ( node : ts . Node ) : node is NamedNodeWithSymbol {
297- switch ( node . kind ) {
298- case ts . SyntaxKind . ClassDeclaration :
299- case ts . SyntaxKind . InterfaceDeclaration :
300- case ts . SyntaxKind . TypeAliasDeclaration :
301- case ts . SyntaxKind . EnumDeclaration :
302- case ts . SyntaxKind . EnumMember :
303- case ts . SyntaxKind . ModuleDeclaration :
304- case ts . SyntaxKind . FunctionDeclaration :
305- case ts . SyntaxKind . MethodDeclaration :
306- case ts . SyntaxKind . MethodSignature :
307- return true ;
308176 }
309- return false ;
310- }
311-
312- /**
313- * True if the given AST node has a type.
314- */
315- function isTypedNode ( node : ts . Node ) : boolean {
316- switch ( node . kind ) {
317- case ts . SyntaxKind . ArrayLiteralExpression :
318- case ts . SyntaxKind . ArrowFunction :
319- case ts . SyntaxKind . AsExpression :
320- case ts . SyntaxKind . SatisfiesExpression :
321- case ts . SyntaxKind . AwaitExpression :
322- case ts . SyntaxKind . BinaryExpression :
323- case ts . SyntaxKind . CallExpression :
324- case ts . SyntaxKind . ClassExpression :
325- case ts . SyntaxKind . ClassDeclaration :
326- case ts . SyntaxKind . CommaListExpression :
327- case ts . SyntaxKind . ConditionalExpression :
328- case ts . SyntaxKind . Constructor :
329- case ts . SyntaxKind . DeleteExpression :
330- case ts . SyntaxKind . ElementAccessExpression :
331- case ts . SyntaxKind . ExpressionStatement :
332- case ts . SyntaxKind . ExpressionWithTypeArguments :
333- case ts . SyntaxKind . FalseKeyword :
334- case ts . SyntaxKind . FunctionDeclaration :
335- case ts . SyntaxKind . FunctionExpression :
336- case ts . SyntaxKind . GetAccessor :
337- case ts . SyntaxKind . Identifier :
338- case ts . SyntaxKind . IndexSignature :
339- case ts . SyntaxKind . JsxExpression :
340- case ts . SyntaxKind . LiteralType :
341- case ts . SyntaxKind . MethodDeclaration :
342- case ts . SyntaxKind . MethodSignature :
343- case ts . SyntaxKind . NewExpression :
344- case ts . SyntaxKind . NonNullExpression :
345- case ts . SyntaxKind . NoSubstitutionTemplateLiteral :
346- case ts . SyntaxKind . NumericLiteral :
347- case ts . SyntaxKind . ObjectKeyword :
348- case ts . SyntaxKind . ObjectLiteralExpression :
349- case ts . SyntaxKind . OmittedExpression :
350- case ts . SyntaxKind . ParenthesizedExpression :
351- case ts . SyntaxKind . PartiallyEmittedExpression :
352- case ts . SyntaxKind . PostfixUnaryExpression :
353- case ts . SyntaxKind . PrefixUnaryExpression :
354- case ts . SyntaxKind . PropertyAccessExpression :
355- case ts . SyntaxKind . RegularExpressionLiteral :
356- case ts . SyntaxKind . SetAccessor :
357- case ts . SyntaxKind . StringLiteral :
358- case ts . SyntaxKind . TaggedTemplateExpression :
359- case ts . SyntaxKind . TemplateExpression :
360- case ts . SyntaxKind . TemplateHead :
361- case ts . SyntaxKind . TemplateMiddle :
362- case ts . SyntaxKind . TemplateSpan :
363- case ts . SyntaxKind . TemplateTail :
364- case ts . SyntaxKind . TrueKeyword :
365- case ts . SyntaxKind . TypeAssertionExpression :
366- case ts . SyntaxKind . TypeLiteral :
367- case ts . SyntaxKind . TypeOfExpression :
368- case ts . SyntaxKind . VoidExpression :
369- case ts . SyntaxKind . YieldExpression :
370- return true ;
371- default :
372- return ts . isTypeNode ( node ) ;
373- }
374- }
375-
376- type ContextuallyTypedNode = ( ts . ArrayLiteralExpression | ts . ObjectLiteralExpression ) & AugmentedNode ;
377-
378- function isContextuallyTypedNode ( node : ts . Node ) : node is ContextuallyTypedNode {
379- let kind = node . kind ;
380- return kind === ts . SyntaxKind . ArrayLiteralExpression || kind === ts . SyntaxKind . ObjectLiteralExpression ;
381177}
0 commit comments