Skip to content
Permalink
Browse files

fix(language-service): Remove getTemplateReferences() from LanguageSe…

…rvice API (#33807)

The method `getTemplateReferences()` appears in both the LanguageService
interface and LanguageServiceHost interface. It should belong in the
latter and not the former, since the former deals with the semantics of
the language and not the mechanics.

PR Close #33807
  • Loading branch information
kyliau authored and alxhub committed Nov 13, 2019
1 parent af2cca0 commit 0688a28be3d2dc8be69da2b3e5087bad11ce40a0
@@ -28,11 +28,6 @@ export function createLanguageService(host: TypeScriptServiceHost): LanguageServ
class LanguageServiceImpl implements LanguageService { class LanguageServiceImpl implements LanguageService {
constructor(private readonly host: TypeScriptServiceHost) {} constructor(private readonly host: TypeScriptServiceHost) {}


getTemplateReferences(): string[] {
this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
return this.host.getTemplateReferences();
}

getDiagnostics(fileName: string): tss.Diagnostic[] { getDiagnostics(fileName: string): tss.Diagnostic[] {
const analyzedModules = this.host.getAnalyzedModules(); // same role as 'synchronizeHostData' const analyzedModules = this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
const results: Diagnostic[] = []; const results: Diagnostic[] = [];
@@ -44,7 +44,6 @@ export function getExternalFiles(project: tss.server.Project): string[] {
// Without an Angular host there is no way to get template references. // Without an Angular host there is no way to get template references.
return []; return [];
} }
ngLSHost.getAnalyzedModules();
const templates = ngLSHost.getTemplateReferences(); const templates = ngLSHost.getTemplateReferences();
const logger = project.projectService.logger; const logger = project.projectService.logger;
if (logger.hasLevel(tss.server.LogLevel.verbose)) { if (logger.hasLevel(tss.server.LogLevel.verbose)) {
@@ -394,11 +394,6 @@ export interface Hover {
* @publicApi * @publicApi
*/ */
export interface LanguageService { export interface LanguageService {
/**
* Returns a list of all the external templates referenced by the project.
*/
getTemplateReferences(): string[];

/** /**
* Returns a list of all error for all templates in the given file. * Returns a list of all error for all templates in the given file.
*/ */
@@ -144,7 +144,10 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
return this.resolver.getReflector() as StaticReflector; return this.resolver.getReflector() as StaticReflector;
} }


getTemplateReferences(): string[] { return [...this.templateReferences]; } getTemplateReferences(): string[] {
this.getAnalyzedModules();
return [...this.templateReferences];
}


/** /**
* Checks whether the program has changed and returns all analyzed modules. * Checks whether the program has changed and returns all analyzed modules.
@@ -14,7 +14,7 @@ import {TypeScriptServiceHost} from '../src/typescript_host';
import {MockTypescriptHost} from './test_utils'; import {MockTypescriptHost} from './test_utils';


describe('service without angular', () => { describe('service without angular', () => {
const mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']); const mockHost = new MockTypescriptHost(['/app/main.ts']);
const service = ts.createLanguageService(mockHost); const service = ts.createLanguageService(mockHost);
const ngHost = new TypeScriptServiceHost(mockHost, service); const ngHost = new TypeScriptServiceHost(mockHost, service);
const ngService = createLanguageService(ngHost); const ngService = createLanguageService(ngHost);
@@ -23,8 +23,6 @@ describe('service without angular', () => {


beforeEach(() => { mockHost.reset(); }); beforeEach(() => { mockHost.reset(); });


it('should not crash a get template references',
() => { expect(() => ngService.getTemplateReferences()).not.toThrow(); });
it('should not crash a get diagnostics', it('should not crash a get diagnostics',
() => { expect(() => ngService.getDiagnostics(fileName)).not.toThrow(); }); () => { expect(() => ngService.getDiagnostics(fileName)).not.toThrow(); });


@@ -8,8 +8,6 @@


import * as ts from 'typescript'; import * as ts from 'typescript';


import {createLanguageService} from '../src/language_service';
import {LanguageService} from '../src/types';
import {TypeScriptServiceHost} from '../src/typescript_host'; import {TypeScriptServiceHost} from '../src/typescript_host';


import {MockTypescriptHost} from './test_utils'; import {MockTypescriptHost} from './test_utils';
@@ -18,15 +16,13 @@ describe('references', () => {
const mockHost = new MockTypescriptHost(['/app/main.ts']); const mockHost = new MockTypescriptHost(['/app/main.ts']);
const service = ts.createLanguageService(mockHost); const service = ts.createLanguageService(mockHost);
const ngHost = new TypeScriptServiceHost(mockHost, service); const ngHost = new TypeScriptServiceHost(mockHost, service);
const ngService = createLanguageService(ngHost);


beforeEach(() => { mockHost.reset(); }); beforeEach(() => { mockHost.reset(); });


it('should be able to get template references', it('should be able to determine that test.ng is a template reference', () => {
() => { expect(() => ngService.getTemplateReferences()).not.toThrow(); }); const templates = ngHost.getTemplateReferences();

expect(templates).toEqual(['/app/test.ng']);
it('should be able to determine that test.ng is a template reference', });
() => { expect(ngService.getTemplateReferences()).toContain('/app/test.ng'); });


it('should be able to get template references for an invalid project', () => { it('should be able to get template references for an invalid project', () => {
const moduleCode = ` const moduleCode = `
@@ -42,7 +38,8 @@ describe('references', () => {
`; `;
mockHost.addScript('/app/test.module.ts', moduleCode); mockHost.addScript('/app/test.module.ts', moduleCode);
mockHost.addScript('/app/test.component.ts', classCode); mockHost.addScript('/app/test.component.ts', classCode);
expect(() => { ngService.getTemplateReferences(); }).not.toThrow(); const templates = ngHost.getTemplateReferences();
expect(templates).toEqual(['/app/test.ng']);
}); });


}); });

0 comments on commit 0688a28

Please sign in to comment.
You can’t perform that action at this time.