@@ -223,23 +223,39 @@ 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+
227+ // if the definition's kind is a reference, the identifier is a template variable referred in an internal/external template
226228 if ( definition . kind . toString ( ) === 'reference' ) {
227- // template variable in an internal/external template
228229 return langServ . getDefinitionAndBoundSpan ( entryPath , definition . textSpan . start ) . definitions [ 0 ] ;
229230 }
230231 let typeDefs = langServ . getTypeDefinitionAtPosition ( entryPath , definition . textSpan . start ) ;
232+ // if there are no type definitions found, the identifier is a ts property, referred in an internal/external template
233+ // or is a reference in a decorator
231234 if ( ! typeDefs ) {
232- // ts property referred in an internal/external template
233- const classDeclaration = langServ
234- . getProgram ( )
235- . getSourceFile ( definition . fileName )
235+ /*
236+ normally, the tsserver will consider non .ts files as external to the project
237+ however, we load .html files which we can handle with the Angular language service
238+ here we're only looking for definitions in a .ts source file
239+ we call the getSourceFile function which accesses a map of files, previously loaded by the tsserver
240+ at this point the map contains all .html files that we've included
241+ we have to ignore them, since the language service will attempt to parse them as .ts files
242+ */
243+ if ( ! definition . fileName . endsWith ( '.ts' ) ) { return null ; }
244+
245+ const sourceFile = langServ . getProgram ( ) . getSourceFile ( definition . fileName ) ;
246+ if ( ! sourceFile ) { return null ; }
247+
248+ const classDeclaration = sourceFile
236249 . statements
237250 . filter ( < ( a : tss . Statement ) => a is tss . ClassDeclaration > ( m => m . kind === tss . SyntaxKind . ClassDeclaration ) )
238251 . 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 ; }
252+
253+ // there must be at least one class declaration in the .ts file and the property must belong to it
254+ if ( ! classDeclaration ) { return null ; }
255+
256+ const member : ts . ClassElement = classDeclaration . members . find ( m => m . name . getText ( ) === definition . name ) ;
257+ if ( ! member ?. name ) { return null ; }
258+
243259 typeDefs = langServ . getTypeDefinitionAtPosition ( definition . fileName , member . name . getStart ( ) + 1 ) ;
244260 }
245261 if ( typeDefs ?. length ) {
0 commit comments