@@ -223,23 +223,27 @@ export function createProjectService(serverHost: tss.server.ServerHost): tss.ser
223223export function getTypeDefinitionAtPosition ( langServ : tss . LanguageService , entryPath : string , position : number ) : tss . DefinitionInfo | null {
224224 const definition = langServ . getDefinitionAndBoundSpan ( entryPath , position ) ?. definitions [ 0 ] ;
225225 if ( ! definition ) { return null ; }
226+ // if the definition's kind is a reference, the identifier is a template variable referred in an internal/external template
226227 if ( definition . kind . toString ( ) === 'reference' ) {
227- // template variable in an internal/external template
228228 return langServ . getDefinitionAndBoundSpan ( entryPath , definition . textSpan . start ) . definitions [ 0 ] ;
229229 }
230230 let typeDefs = langServ . getTypeDefinitionAtPosition ( entryPath , definition . textSpan . start ) ;
231+ // if there are no type definitions found, the identifier is a ts property, referred in an internal/external template
231232 if ( ! typeDefs ) {
232- // ts property referred in an internal/external template
233- const classDeclaration = langServ
234- . getProgram ( )
235- . getSourceFile ( definition . fileName )
233+ const sourceFile = langServ . getProgram ( ) . getSourceFile ( definition . fileName ) ;
234+ // if the language service cannot resolve the source, this means its not a .ts file
235+ if ( ! sourceFile ) { return null ; }
236+
237+ const classDeclaration = sourceFile
236238 . statements
237239 . filter ( < ( a : tss . Statement ) => a is tss . ClassDeclaration > ( m => m . kind === tss . SyntaxKind . ClassDeclaration ) )
238240 . find ( m => m . name . getText ( ) === definition . containerName ) ;
239- const member : ts . ClassElement = classDeclaration
240- ?. members
241- . find ( m => m . name . getText ( ) === definition . name ) ;
242- if ( ! member || ! member . name ) { return null ; }
241+ // there must be at least one class declaration in the .ts file and the property must belong to it
242+ if ( ! classDeclaration ) { return null ; }
243+
244+ const member : ts . ClassElement = classDeclaration . members . find ( m => m . name . getText ( ) === definition . name ) ;
245+ if ( ! member ?. name ) { return null ; }
246+
243247 typeDefs = langServ . getTypeDefinitionAtPosition ( definition . fileName , member . name . getStart ( ) + 1 ) ;
244248 }
245249 if ( typeDefs ?. length ) {
0 commit comments