feat(language-service): expose determining the NgModule of a Directive (#32710)

This sets up the Language Service to support #32565.
This PR exposes a `getDirectiveModule` method on `TypeScriptServiceHost`
that returns the NgModule owning a Directive given the Directive's
TypeScript node or its Angular `StaticSymbol`. Both types are supported
to reduce extraneous helper methods.

PR Close #32710
This commit is contained in:
ayazhafiz 2019-09-16 22:33:48 -05:00 committed by Andrew Kushnir
parent ab29874f09
commit 2846505dbd
2 changed files with 28 additions and 0 deletions

View File

@ -441,6 +441,14 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
return astResult; return astResult;
} }
/**
* Gets a StaticSymbol from a file and symbol name.
* @return Angular StaticSymbol matching the file and name, if any
*/
getStaticSymbol(file: string, name: string): StaticSymbol|undefined {
return this.reflector.getStaticSymbol(file, name);
}
/** /**
* Find the NgModule which the directive associated with the `classSymbol` * Find the NgModule which the directive associated with the `classSymbol`
* belongs to, then return its schema and transitive directives and pipes. * belongs to, then return its schema and transitive directives and pipes.

View File

@ -170,4 +170,24 @@ describe('TypeScriptServiceHost', () => {
const newModules = ngLSHost.getAnalyzedModules(); const newModules = ngLSHost.getAnalyzedModules();
expect(newModules).toBe(oldModules); expect(newModules).toBe(oldModules);
}); });
it('should get the correct StaticSymbol for a Directive', () => {
const tsLSHost = new MockTypescriptHost(['/app/app.component.ts', '/app/main.ts'], toh);
const tsLS = ts.createLanguageService(tsLSHost);
const ngLSHost = new TypeScriptServiceHost(tsLSHost, tsLS);
ngLSHost.getAnalyzedModules(); // modules are analyzed lazily
const sf = ngLSHost.getSourceFile('/app/app.component.ts');
expect(sf).toBeDefined();
const directiveDecl = sf !.forEachChild(n => {
if (ts.isClassDeclaration(n) && n.name && n.name.text === 'AppComponent') return n;
});
expect(directiveDecl).toBeDefined();
expect(directiveDecl !.name).toBeDefined();
const fileName = directiveDecl !.getSourceFile().fileName;
const symbolName = directiveDecl !.name !.getText();
const directiveSymbol = ngLSHost.getStaticSymbol(fileName, symbolName);
expect(directiveSymbol).toBeDefined();
expect(directiveSymbol !.name).toBe('AppComponent');
});
}); });