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

View File

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

View File

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