refactor(language-service): test case (#34564)

PR Close #34564
This commit is contained in:
ivanwonder 2020-01-04 11:30:14 +08:00 committed by atscott
parent 7005645592
commit c8c6fd7153
3 changed files with 21 additions and 37 deletions

View File

@ -148,14 +148,14 @@ function findAttribute(info: AstResult, position: number): Attribute|undefined {
return path.first(Attribute);
}
function findParentOfDirectivePropertyAst(ast: TemplateAst[], binding: BoundDirectivePropertyAst) {
function findParentOfBinding(ast: TemplateAst[], binding: BoundDirectivePropertyAst) {
let res: DirectiveAst|undefined;
const visitor = new class extends RecursiveTemplateAstVisitor {
visitDirective(ast: DirectiveAst) {
const result = this.visitChildren(ast, visit => { visit(ast.inputs); });
return result;
}
visitDirectiveProperty(ast: BoundDirectivePropertyAst, context: any) {
visitDirectiveProperty(ast: BoundDirectivePropertyAst, context: DirectiveAst) {
if (ast === binding) {
res = context;
}
@ -167,7 +167,7 @@ function findParentOfDirectivePropertyAst(ast: TemplateAst[], binding: BoundDire
function findInputBinding(
info: AstResult, path: TemplateAstPath, binding: BoundDirectivePropertyAst): Symbol|undefined {
const directiveAst = findParentOfDirectivePropertyAst(info.templateAst, binding);
const directiveAst = findParentOfBinding(info.templateAst, binding);
if (directiveAst) {
const invertedInput = invertMap(directiveAst.directive.inputs);
const fieldName = invertedInput[binding.templateName];

View File

@ -13,6 +13,8 @@ import {TypeScriptServiceHost} from '../src/typescript_host';
import {MockTypescriptHost} from './test_utils';
const TEST_TEMPLATE = '/app/test.ng';
describe('definitions', () => {
const mockHost = new MockTypescriptHost(['/app/main.ts']);
const service = ts.createLanguageService(mockHost);
@ -263,21 +265,17 @@ describe('definitions', () => {
});
it('should be able to find a structural directive', () => {
const fileName = mockHost.addCode(`
@Component({
template: '<div ~{start-my}*«ngIf»="true"~{end-my}></div>'
})
export class MyComponent { }`);
mockHost.override(TEST_TEMPLATE, `<div ~{start-my}*«ngIf»="true"~{end-my}></div>`);
// Get the marker for ngIf in the code added above.
const marker = mockHost.getReferenceMarkerFor(fileName, 'ngIf');
const marker = mockHost.getReferenceMarkerFor(TEST_TEMPLATE, 'ngIf');
const result = ngService.getDefinitionAt(fileName, marker.start);
const result = ngService.getDefinitionAt(TEST_TEMPLATE, marker.start);
expect(result).toBeDefined();
const {textSpan, definitions} = result !;
// Get the marker for bounded text in the code added above
const boundedText = mockHost.getLocationMarkerFor(fileName, 'my');
const boundedText = mockHost.getLocationMarkerFor(TEST_TEMPLATE, 'my');
expect(textSpan).toEqual(boundedText);
expect(definitions).toBeDefined();
@ -292,22 +290,18 @@ describe('definitions', () => {
});
it('should be able to find a two-way binding', () => {
const fileName = mockHost.addCode(`
@Component({
template: '<test-comp string-model ~{start-my}[(«model»)]="test"~{end-my}></test-comp>'
})
export class MyComponent {
test = "";
}`);
mockHost.override(
TEST_TEMPLATE,
`<test-comp string-model ~{start-my}[(«model»)]="title"~{end-my}></test-comp>`);
// Get the marker for «model» in the code added above.
const marker = mockHost.getReferenceMarkerFor(fileName, 'model');
const marker = mockHost.getReferenceMarkerFor(TEST_TEMPLATE, 'model');
const result = ngService.getDefinitionAt(fileName, marker.start);
const result = ngService.getDefinitionAt(TEST_TEMPLATE, marker.start);
expect(result).toBeDefined();
const {textSpan, definitions} = result !;
// Get the marker for bounded text in the code added above
const boundedText = mockHost.getLocationMarkerFor(fileName, 'my');
const boundedText = mockHost.getLocationMarkerFor(TEST_TEMPLATE, 'my');
expect(textSpan).toEqual(boundedText);
// There should be exactly 1 definition

View File

@ -149,13 +149,9 @@ describe('hover', () => {
});
it('should be able to find a structural directive', () => {
const fileName = mockHost.addCode(`
@Component({
template: '<div «*ᐱngIfᐱ="true"»></div>'
})
export class MyComponent { }`);
const marker = mockHost.getDefinitionMarkerFor(fileName, 'ngIf');
const quickInfo = ngLS.getHoverAt(fileName, marker.start);
mockHost.override(TEST_TEMPLATE, `<div «*ᐱngIfᐱ="true"»></div>`);
const marker = mockHost.getDefinitionMarkerFor(TEST_TEMPLATE, 'ngIf');
const quickInfo = ngLS.getHoverAt(TEST_TEMPLATE, marker.start);
expect(quickInfo).toBeTruthy();
const {textSpan, displayParts} = quickInfo !;
expect(textSpan).toEqual(marker);
@ -163,15 +159,9 @@ describe('hover', () => {
});
it('should be able to find a reference to a two-way binding', () => {
const fileName = mockHost.addCode(`
@Component({
template: '<test-comp string-model «[(ᐱmodelᐱ)]="test"»></test-comp>'
})
export class MyComponent {
test = "";
}`);
const marker = mockHost.getDefinitionMarkerFor(fileName, 'model');
const quickInfo = ngLS.getHoverAt(fileName, marker.start);
mockHost.override(TEST_TEMPLATE, `<test-comp string-model «[(ᐱmodelᐱ)]="title"»></test-comp>`);
const marker = mockHost.getDefinitionMarkerFor(TEST_TEMPLATE, 'model');
const quickInfo = ngLS.getHoverAt(TEST_TEMPLATE, marker.start);
expect(quickInfo).toBeTruthy();
const {textSpan, displayParts} = quickInfo !;
expect(textSpan).toEqual(marker);