refactor(language-service): clean up and exports and consolidate types (#36533)
PR Close #36533
This commit is contained in:
@ -9,7 +9,8 @@
|
||||
import * as ng from '@angular/compiler';
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {getDirectiveClassLike, getPathToNodeAtPosition} from '../src/utils';
|
||||
import {getClassDeclFromDecoratorProp, getDirectiveClassLike, getPathToNodeAtPosition} from '../src/utils';
|
||||
import {MockTypescriptHost} from './test_utils';
|
||||
|
||||
describe('getDirectiveClassLike', () => {
|
||||
it('should return a directive class', () => {
|
||||
@ -80,3 +81,42 @@ describe('getPathToNodeAtPosition', () => {
|
||||
expect(path.tail instanceof ng.Attribute).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getClassDeclFromTemplateNode', () => {
|
||||
it('should find class declaration in syntax-only mode', () => {
|
||||
const sourceFile = ts.createSourceFile(
|
||||
'foo.ts', `
|
||||
@Component({
|
||||
template: '<div></div>'
|
||||
})
|
||||
class MyComponent {}`,
|
||||
ts.ScriptTarget.ES2015, true /* setParentNodes */);
|
||||
function visit(node: ts.Node): ts.ClassDeclaration|undefined {
|
||||
if (ts.isPropertyAssignment(node)) {
|
||||
return getClassDeclFromDecoratorProp(node);
|
||||
}
|
||||
return node.forEachChild(visit);
|
||||
}
|
||||
const classDecl = sourceFile.forEachChild(visit);
|
||||
expect(classDecl).toBeTruthy();
|
||||
expect(classDecl!.kind).toBe(ts.SyntaxKind.ClassDeclaration);
|
||||
expect((classDecl as ts.ClassDeclaration).name!.text).toBe('MyComponent');
|
||||
});
|
||||
|
||||
|
||||
it('should return class declaration for AppComponent', () => {
|
||||
const host = new MockTypescriptHost(['/app/app.component.ts']);
|
||||
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 {
|
||||
if (ts.isPropertyAssignment(node)) {
|
||||
return getClassDeclFromDecoratorProp(node);
|
||||
}
|
||||
return node.forEachChild(visit);
|
||||
});
|
||||
expect(classDecl).toBeTruthy();
|
||||
expect(ts.isClassDeclaration(classDecl!)).toBe(true);
|
||||
expect((classDecl as ts.ClassDeclaration).name!.text).toBe('AppComponent');
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user