Keen Yee Liau 40b28742a9 refactor(language-service): Differentiate Inline and External template (#32127)
This commit creates two concrete classes Inline and External
TemplateSource to differentiate between templates in TS file and
HTML file.
Knowing the template type makes the code much more explicit which
filetype we are dealing with.

With these two classes, there is no need for `getTemplateAt()` method in
TypeScriptHost. Removing this method is safe since it is not used in the
extension. This reduces the API surface of TypescriptHost.

PR Close #32127
2019-08-15 10:04:17 -07:00

32 lines
1.1 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import {getClassDeclFromTemplateNode} from '../src/template';
import {toh} from './test_data';
import {MockTypescriptHost} from './test_utils';
describe('getClassDeclFromTemplateNode', () => {
it('should return class declaration', () => {
const host = new MockTypescriptHost(['/app/app.component.ts'], toh);
const tsLS = ts.createLanguageService(host);
const sourceFile = tsLS.getProgram() !.getSourceFile('/app/app.component.ts');
expect(sourceFile).toBeTruthy();
const classDecl = sourceFile !.forEachChild(function visit(node): ts.Node | undefined {
const candidate = getClassDeclFromTemplateNode(node);
if (candidate) {
return candidate;
}
return node.forEachChild(visit);
});
expect(classDecl).toBeTruthy();
expect(ts.isClassDeclaration(classDecl !)).toBe(true);
expect((classDecl as ts.ClassDeclaration).name !.text).toBe('AppComponent');
});
});