Skip to content

Commit 332a2b9

Browse files
refactor(tsUtils): ignore ts files when searching for definitions
1 parent 96493b7 commit 332a2b9

File tree

1 file changed

+13
-1
lines changed
  • projects/igniteui-angular/migrations/common

1 file changed

+13
-1
lines changed

projects/igniteui-angular/migrations/common/tsUtils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,21 +223,33 @@ export function createProjectService(serverHost: tss.server.ServerHost): tss.ser
223223
export 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+
226227
// if the definition's kind is a reference, the identifier is a template variable referred in an internal/external template
227228
if (definition.kind.toString() === 'reference') {
228229
return langServ.getDefinitionAndBoundSpan(entryPath, definition.textSpan.start).definitions[0];
229230
}
230231
let typeDefs = langServ.getTypeDefinitionAtPosition(entryPath, definition.textSpan.start);
231232
// 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
232234
if (!typeDefs) {
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+
233245
const sourceFile = langServ.getProgram().getSourceFile(definition.fileName);
234-
// if the language service cannot resolve the source, this means its not a .ts file
235246
if (!sourceFile) { return null; }
236247

237248
const classDeclaration = sourceFile
238249
.statements
239250
.filter(<(a: tss.Statement) => a is tss.ClassDeclaration>(m => m.kind === tss.SyntaxKind.ClassDeclaration))
240251
.find(m => m.name.getText() === definition.containerName);
252+
241253
// there must be at least one class declaration in the .ts file and the property must belong to it
242254
if (!classDeclaration) { return null; }
243255

0 commit comments

Comments
 (0)