2016-11-22 12:10:23 -05:00
|
|
|
/**
|
|
|
|
* @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 {createLanguageService} from '../src/language_service';
|
2019-08-01 16:07:32 -04:00
|
|
|
import {LanguageService} from '../src/types';
|
2016-11-22 12:10:23 -05:00
|
|
|
import {TypeScriptServiceHost} from '../src/typescript_host';
|
|
|
|
|
2019-08-01 16:07:32 -04:00
|
|
|
import {MockTypescriptHost} from './test_utils';
|
2016-11-22 12:10:23 -05:00
|
|
|
|
|
|
|
describe('definitions', () => {
|
2019-08-01 16:07:32 -04:00
|
|
|
let mockHost: MockTypescriptHost;
|
|
|
|
let service: ts.LanguageService;
|
|
|
|
let ngHost: TypeScriptServiceHost;
|
|
|
|
let ngService: LanguageService;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// Create a new mockHost every time to reset any files that are overridden.
|
2019-09-17 17:33:41 -04:00
|
|
|
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
|
2019-08-01 16:07:32 -04:00
|
|
|
service = ts.createLanguageService(mockHost);
|
|
|
|
ngHost = new TypeScriptServiceHost(mockHost, service);
|
|
|
|
ngService = createLanguageService(ngHost);
|
|
|
|
});
|
2016-11-22 12:10:23 -05:00
|
|
|
|
|
|
|
it('should be able to find field in an interpolation', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
const fileName = mockHost.addCode(`
|
2019-08-01 16:07:32 -04:00
|
|
|
@Component({
|
|
|
|
template: '{{«name»}}'
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
«ᐱnameᐱ: string;»
|
|
|
|
}`);
|
|
|
|
|
2019-08-28 15:55:12 -04:00
|
|
|
const marker = mockHost.getReferenceMarkerFor(fileName, 'name');
|
2019-08-01 16:07:32 -04:00
|
|
|
const result = ngService.getDefinitionAt(fileName, marker.start);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
const {textSpan, definitions} = result !;
|
|
|
|
|
|
|
|
expect(textSpan).toEqual(marker);
|
|
|
|
expect(definitions).toBeDefined();
|
|
|
|
expect(definitions !.length).toBe(1);
|
|
|
|
const def = definitions ![0];
|
|
|
|
|
|
|
|
expect(def.fileName).toBe(fileName);
|
|
|
|
expect(def.name).toBe('name');
|
|
|
|
expect(def.kind).toBe('property');
|
2019-08-28 15:55:12 -04:00
|
|
|
expect(def.textSpan).toEqual(mockHost.getDefinitionMarkerFor(fileName, 'name'));
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to find a field in a attribute reference', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
const fileName = mockHost.addCode(`
|
2019-08-01 16:07:32 -04:00
|
|
|
@Component({
|
|
|
|
template: '<input [(ngModel)]="«name»">'
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
«ᐱnameᐱ: string;»
|
|
|
|
}`);
|
|
|
|
|
2019-08-28 15:55:12 -04:00
|
|
|
const marker = mockHost.getReferenceMarkerFor(fileName, 'name');
|
2019-08-01 16:07:32 -04:00
|
|
|
const result = ngService.getDefinitionAt(fileName, marker.start);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
const {textSpan, definitions} = result !;
|
|
|
|
|
|
|
|
expect(textSpan).toEqual(marker);
|
|
|
|
expect(definitions).toBeDefined();
|
|
|
|
expect(definitions !.length).toBe(1);
|
|
|
|
const def = definitions ![0];
|
|
|
|
|
|
|
|
expect(def.fileName).toBe(fileName);
|
|
|
|
expect(def.name).toBe('name');
|
|
|
|
expect(def.kind).toBe('property');
|
2019-08-28 15:55:12 -04:00
|
|
|
expect(def.textSpan).toEqual(mockHost.getDefinitionMarkerFor(fileName, 'name'));
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to find a method from a call', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
const fileName = mockHost.addCode(`
|
2019-08-01 16:07:32 -04:00
|
|
|
@Component({
|
|
|
|
template: '<div (click)="~{start-my}«myClick»()~{end-my};"></div>'
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
«ᐱmyClickᐱ() { }»
|
|
|
|
}`);
|
|
|
|
|
2019-08-28 15:55:12 -04:00
|
|
|
const marker = mockHost.getReferenceMarkerFor(fileName, 'myClick');
|
2019-08-01 16:07:32 -04:00
|
|
|
const result = ngService.getDefinitionAt(fileName, marker.start);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
const {textSpan, definitions} = result !;
|
|
|
|
|
2019-08-28 15:55:12 -04:00
|
|
|
expect(textSpan).toEqual(mockHost.getLocationMarkerFor(fileName, 'my'));
|
2019-08-01 16:07:32 -04:00
|
|
|
expect(definitions).toBeDefined();
|
|
|
|
expect(definitions !.length).toBe(1);
|
|
|
|
const def = definitions ![0];
|
|
|
|
|
|
|
|
expect(def.fileName).toBe(fileName);
|
|
|
|
expect(def.name).toBe('myClick');
|
|
|
|
expect(def.kind).toBe('method');
|
2019-08-28 15:55:12 -04:00
|
|
|
expect(def.textSpan).toEqual(mockHost.getDefinitionMarkerFor(fileName, 'myClick'));
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to find a field reference in an *ngIf', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
const fileName = mockHost.addCode(`
|
2019-08-01 16:07:32 -04:00
|
|
|
@Component({
|
|
|
|
template: '<div *ngIf="«include»"></div>'
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
«ᐱincludeᐱ = true;»
|
|
|
|
}`);
|
|
|
|
|
2019-08-28 15:55:12 -04:00
|
|
|
const marker = mockHost.getReferenceMarkerFor(fileName, 'include');
|
2019-08-01 16:07:32 -04:00
|
|
|
const result = ngService.getDefinitionAt(fileName, marker.start);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
const {textSpan, definitions} = result !;
|
|
|
|
|
|
|
|
expect(textSpan).toEqual(marker);
|
|
|
|
expect(definitions).toBeDefined();
|
|
|
|
expect(definitions !.length).toBe(1);
|
|
|
|
const def = definitions ![0];
|
|
|
|
|
|
|
|
expect(def.fileName).toBe(fileName);
|
|
|
|
expect(def.name).toBe('include');
|
|
|
|
expect(def.kind).toBe('property');
|
2019-08-28 15:55:12 -04:00
|
|
|
expect(def.textSpan).toEqual(mockHost.getDefinitionMarkerFor(fileName, 'include'));
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to find a reference to a component', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
const fileName = mockHost.addCode(`
|
2019-08-01 16:07:32 -04:00
|
|
|
@Component({
|
|
|
|
template: '~{start-my}<«test-comp»></test-comp>~{end-my}'
|
|
|
|
})
|
|
|
|
export class MyComponent { }`);
|
|
|
|
|
|
|
|
// Get the marker for «test-comp» in the code added above.
|
2019-08-28 15:55:12 -04:00
|
|
|
const marker = mockHost.getReferenceMarkerFor(fileName, 'test-comp');
|
2019-08-01 16:07:32 -04:00
|
|
|
|
|
|
|
const result = ngService.getDefinitionAt(fileName, marker.start);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
const {textSpan, definitions} = result !;
|
|
|
|
|
|
|
|
// Get the marker for bounded text in the code added above.
|
2019-08-28 15:55:12 -04:00
|
|
|
const boundedText = mockHost.getLocationMarkerFor(fileName, 'my');
|
2019-08-01 16:07:32 -04:00
|
|
|
expect(textSpan).toEqual(boundedText);
|
|
|
|
|
|
|
|
// There should be exactly 1 definition
|
|
|
|
expect(definitions).toBeDefined();
|
|
|
|
expect(definitions !.length).toBe(1);
|
|
|
|
const def = definitions ![0];
|
|
|
|
|
|
|
|
const refFileName = '/app/parsing-cases.ts';
|
|
|
|
expect(def.fileName).toBe(refFileName);
|
|
|
|
expect(def.name).toBe('TestComponent');
|
|
|
|
expect(def.kind).toBe('component');
|
2019-09-12 18:20:54 -04:00
|
|
|
const content = mockHost.getFileContent(refFileName) !;
|
|
|
|
const begin = '/*BeginTestComponent*/ ';
|
|
|
|
const start = content.indexOf(begin) + begin.length;
|
|
|
|
const end = content.indexOf(' /*EndTestComponent*/');
|
|
|
|
expect(def.textSpan).toEqual({
|
|
|
|
start,
|
|
|
|
length: end - start,
|
|
|
|
});
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to find an event provider', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
const fileName = mockHost.addCode(`
|
2019-08-01 16:07:32 -04:00
|
|
|
@Component({
|
|
|
|
template: '<test-comp ~{start-my}(«test»)="myHandler()"~{end-my}></div>'
|
|
|
|
})
|
|
|
|
export class MyComponent { myHandler() {} }`);
|
|
|
|
|
|
|
|
// Get the marker for «test» in the code added above.
|
2019-08-28 15:55:12 -04:00
|
|
|
const marker = mockHost.getReferenceMarkerFor(fileName, 'test');
|
2019-08-01 16:07:32 -04:00
|
|
|
|
|
|
|
const result = ngService.getDefinitionAt(fileName, marker.start);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
const {textSpan, definitions} = result !;
|
|
|
|
|
|
|
|
// Get the marker for bounded text in the code added above
|
2019-08-28 15:55:12 -04:00
|
|
|
const boundedText = mockHost.getLocationMarkerFor(fileName, 'my');
|
2019-08-01 16:07:32 -04:00
|
|
|
expect(textSpan).toEqual(boundedText);
|
|
|
|
|
|
|
|
// There should be exactly 1 definition
|
|
|
|
expect(definitions).toBeDefined();
|
|
|
|
expect(definitions !.length).toBe(1);
|
|
|
|
const def = definitions ![0];
|
|
|
|
|
|
|
|
const refFileName = '/app/parsing-cases.ts';
|
|
|
|
expect(def.fileName).toBe(refFileName);
|
|
|
|
expect(def.name).toBe('testEvent');
|
|
|
|
expect(def.kind).toBe('event');
|
2019-09-12 18:20:54 -04:00
|
|
|
const content = mockHost.getFileContent(refFileName) !;
|
|
|
|
const ref = `@Output('test') testEvent = new EventEmitter();`;
|
|
|
|
expect(def.textSpan).toEqual({
|
|
|
|
start: content.indexOf(ref),
|
|
|
|
length: ref.length,
|
|
|
|
});
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to find an input provider', () => {
|
2019-08-01 16:07:32 -04:00
|
|
|
// '/app/parsing-cases.ts', 'tcName',
|
2019-08-28 15:55:12 -04:00
|
|
|
const fileName = mockHost.addCode(`
|
2019-08-01 16:07:32 -04:00
|
|
|
@Component({
|
|
|
|
template: '<test-comp ~{start-my}[«tcName»]="name"~{end-my}></div>'
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
name = 'my name';
|
|
|
|
}`);
|
|
|
|
|
|
|
|
// Get the marker for «test» in the code added above.
|
2019-08-28 15:55:12 -04:00
|
|
|
const marker = mockHost.getReferenceMarkerFor(fileName, 'tcName');
|
2019-08-01 16:07:32 -04:00
|
|
|
|
|
|
|
const result = ngService.getDefinitionAt(fileName, marker.start);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
const {textSpan, definitions} = result !;
|
|
|
|
|
|
|
|
// Get the marker for bounded text in the code added above
|
2019-08-28 15:55:12 -04:00
|
|
|
const boundedText = mockHost.getLocationMarkerFor(fileName, 'my');
|
2019-08-01 16:07:32 -04:00
|
|
|
expect(textSpan).toEqual(boundedText);
|
|
|
|
|
|
|
|
// There should be exactly 1 definition
|
|
|
|
expect(definitions).toBeDefined();
|
|
|
|
expect(definitions !.length).toBe(1);
|
|
|
|
const def = definitions ![0];
|
|
|
|
|
|
|
|
const refFileName = '/app/parsing-cases.ts';
|
|
|
|
expect(def.fileName).toBe(refFileName);
|
|
|
|
expect(def.name).toBe('name');
|
|
|
|
expect(def.kind).toBe('property');
|
2019-09-12 18:20:54 -04:00
|
|
|
const content = mockHost.getFileContent(refFileName) !;
|
|
|
|
const ref = `@Input('tcName') name = 'test';`;
|
|
|
|
expect(def.textSpan).toEqual({
|
|
|
|
start: content.indexOf(ref),
|
|
|
|
length: ref.length,
|
|
|
|
});
|
2016-11-22 12:10:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to find a pipe', () => {
|
2019-08-28 15:55:12 -04:00
|
|
|
const fileName = mockHost.addCode(`
|
2019-08-01 16:07:32 -04:00
|
|
|
@Component({
|
|
|
|
template: '<div *ngIf="~{start-my}input | «async»~{end-my}"></div>'
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
input: EventEmitter;
|
|
|
|
}`);
|
2016-11-22 12:10:23 -05:00
|
|
|
|
2019-08-01 16:07:32 -04:00
|
|
|
// Get the marker for «test» in the code added above.
|
2019-08-28 15:55:12 -04:00
|
|
|
const marker = mockHost.getReferenceMarkerFor(fileName, 'async');
|
2019-08-01 16:07:32 -04:00
|
|
|
|
|
|
|
const result = ngService.getDefinitionAt(fileName, marker.start);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
const {textSpan, definitions} = result !;
|
2016-11-22 12:10:23 -05:00
|
|
|
|
2019-08-01 16:07:32 -04:00
|
|
|
// Get the marker for bounded text in the code added above
|
2019-08-28 15:55:12 -04:00
|
|
|
const boundedText = mockHost.getLocationMarkerFor(fileName, 'my');
|
2019-08-01 16:07:32 -04:00
|
|
|
expect(textSpan).toEqual(boundedText);
|
|
|
|
|
|
|
|
expect(definitions).toBeDefined();
|
|
|
|
expect(definitions !.length).toBe(4);
|
|
|
|
|
|
|
|
const refFileName = '/node_modules/@angular/common/common.d.ts';
|
|
|
|
for (const def of definitions !) {
|
|
|
|
expect(def.fileName).toBe(refFileName);
|
|
|
|
expect(def.name).toBe('async');
|
|
|
|
expect(def.kind).toBe('pipe');
|
|
|
|
// Not asserting the textSpan of definition because it's external file
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
2019-08-01 16:07:32 -04:00
|
|
|
});
|
2019-09-01 10:58:14 -04:00
|
|
|
|
|
|
|
it('should be able to find a template from a url', () => {
|
|
|
|
const fileName = mockHost.addCode(`
|
|
|
|
@Component({
|
|
|
|
templateUrl: './«test».ng',
|
|
|
|
})
|
|
|
|
export class MyComponent {}`);
|
|
|
|
|
|
|
|
const marker = mockHost.getReferenceMarkerFor(fileName, 'test');
|
|
|
|
const result = ngService.getDefinitionAt(fileName, marker.start);
|
|
|
|
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
const {textSpan, definitions} = result !;
|
|
|
|
|
|
|
|
expect(textSpan).toEqual({start: marker.start - 2, length: 9});
|
|
|
|
|
|
|
|
expect(definitions).toBeDefined();
|
|
|
|
expect(definitions !.length).toBe(1);
|
|
|
|
const [def] = definitions !;
|
|
|
|
expect(def.fileName).toBe('/app/test.ng');
|
|
|
|
expect(def.textSpan).toEqual({start: 0, length: 0});
|
|
|
|
});
|
2019-09-01 10:56:29 -04:00
|
|
|
|
|
|
|
it('should be able to find a stylesheet from a url', () => {
|
|
|
|
const fileName = mockHost.addCode(`
|
|
|
|
@Component({
|
|
|
|
templateUrl: './test.ng',
|
|
|
|
styleUrls: ['./«test».css'],
|
|
|
|
})
|
|
|
|
export class MyComponent {}`);
|
|
|
|
|
|
|
|
const marker = mockHost.getReferenceMarkerFor(fileName, 'test');
|
|
|
|
const result = ngService.getDefinitionAt(fileName, marker.start);
|
|
|
|
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
const {textSpan, definitions} = result !;
|
|
|
|
|
|
|
|
expect(textSpan).toEqual({start: marker.start - 2, length: 10});
|
|
|
|
|
|
|
|
expect(definitions).toBeDefined();
|
|
|
|
expect(definitions !.length).toBe(1);
|
|
|
|
const [def] = definitions !;
|
|
|
|
expect(def.fileName).toBe('/app/test.css');
|
|
|
|
expect(def.textSpan).toEqual({start: 0, length: 0});
|
|
|
|
});
|
2019-08-01 16:07:32 -04:00
|
|
|
});
|