2020-11-03 19:49:30 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google LLC 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
|
|
|
|
*/
|
|
|
|
|
2020-12-15 18:55:54 -05:00
|
|
|
import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
|
2020-11-03 19:49:30 -05:00
|
|
|
|
2021-01-22 12:03:37 -05:00
|
|
|
import {assertFileNames, assertTextSpans, createModuleAndProjectWithDeclarations, humanizeDocumentSpanLike, LanguageServiceTestEnv, OpenBuffer} from '../testing';
|
2020-11-03 19:49:30 -05:00
|
|
|
|
|
|
|
describe('definitions', () => {
|
2021-01-15 18:57:24 -05:00
|
|
|
it('gets definition for template reference in overridden template', () => {
|
|
|
|
initMockFileSystem('Native');
|
2021-02-08 13:43:21 -05:00
|
|
|
const files = {
|
|
|
|
'app.html': '',
|
|
|
|
'app.ts': `
|
2021-01-15 18:57:24 -05:00
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({templateUrl: '/app.html'})
|
|
|
|
export class AppCmp {}
|
|
|
|
`,
|
|
|
|
};
|
2021-02-08 13:43:21 -05:00
|
|
|
const env = LanguageServiceTestEnv.setup();
|
2021-01-15 18:57:24 -05:00
|
|
|
|
2021-02-08 13:43:21 -05:00
|
|
|
const project = createModuleAndProjectWithDeclarations(env, 'test', files);
|
|
|
|
const template = project.openFile('app.html');
|
|
|
|
template.contents = '<input #myInput /> {{myInput.value}}';
|
|
|
|
project.expectNoSourceDiagnostics();
|
|
|
|
|
|
|
|
template.moveCursorToText('{{myIn¦put.value}}');
|
|
|
|
const {definitions} = getDefinitionsAndAssertBoundSpan(env, template);
|
2021-01-15 18:57:24 -05:00
|
|
|
expect(definitions![0].name).toEqual('myInput');
|
|
|
|
assertFileNames(Array.from(definitions!), ['app.html']);
|
|
|
|
});
|
|
|
|
|
2021-01-22 12:03:37 -05:00
|
|
|
it('returns the pipe definitions when checkTypeOfPipes is false', () => {
|
2020-11-03 19:49:30 -05:00
|
|
|
initMockFileSystem('Native');
|
2021-02-08 13:43:21 -05:00
|
|
|
const files = {
|
|
|
|
'app.ts': `
|
2020-11-03 19:49:30 -05:00
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
import {CommonModule} from '@angular/common';
|
|
|
|
|
|
|
|
@Component({templateUrl: 'app.html'})
|
|
|
|
export class AppCmp {}
|
|
|
|
`,
|
2021-02-08 13:43:21 -05:00
|
|
|
'app.html': '{{"1/1/2020" | date}}'
|
2020-12-15 18:55:54 -05:00
|
|
|
};
|
2020-11-03 19:49:30 -05:00
|
|
|
// checkTypeOfPipes is set to false when strict templates is false
|
2021-02-08 13:43:21 -05:00
|
|
|
const env = LanguageServiceTestEnv.setup();
|
|
|
|
const project =
|
|
|
|
createModuleAndProjectWithDeclarations(env, 'test', files, {strictTemplates: false});
|
|
|
|
const template = project.openFile('app.html');
|
|
|
|
project.expectNoSourceDiagnostics();
|
|
|
|
template.moveCursorToText('da¦te');
|
|
|
|
|
|
|
|
const {textSpan, definitions} = getDefinitionsAndAssertBoundSpan(env, template);
|
|
|
|
expect(template.contents.substr(textSpan.start, textSpan.length)).toEqual('date');
|
2021-01-22 12:03:37 -05:00
|
|
|
expect(definitions.length).toEqual(3);
|
|
|
|
assertTextSpans(definitions, ['transform']);
|
|
|
|
assertFileNames(definitions, ['index.d.ts']);
|
2020-11-03 19:49:30 -05:00
|
|
|
});
|
|
|
|
|
2020-12-15 18:55:54 -05:00
|
|
|
it('gets definitions for all inputs when attribute matches more than one', () => {
|
|
|
|
initMockFileSystem('Native');
|
2021-02-08 13:43:21 -05:00
|
|
|
const files = {
|
|
|
|
'app.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
import {CommonModule} from '@angular/common';
|
|
|
|
|
|
|
|
@Component({templateUrl: 'app.html'})
|
|
|
|
export class AppCmp {}
|
|
|
|
`,
|
|
|
|
'app.html': '<div dir inputA="abc"></div>',
|
|
|
|
'dir.ts': `
|
2020-12-15 18:55:54 -05:00
|
|
|
import {Directive, Input} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[dir]'})
|
|
|
|
export class MyDir {
|
|
|
|
@Input() inputA!: any;
|
|
|
|
}`,
|
2021-02-08 13:43:21 -05:00
|
|
|
'dir2.ts': `
|
2020-12-15 18:55:54 -05:00
|
|
|
import {Directive, Input} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[dir]'})
|
|
|
|
export class MyDir2 {
|
|
|
|
@Input() inputA!: any;
|
2021-02-08 13:43:21 -05:00
|
|
|
}`
|
2020-12-15 18:55:54 -05:00
|
|
|
|
|
|
|
};
|
2021-02-08 13:43:21 -05:00
|
|
|
const env = LanguageServiceTestEnv.setup();
|
|
|
|
const project = createModuleAndProjectWithDeclarations(env, 'test', files);
|
|
|
|
const template = project.openFile('app.html');
|
|
|
|
template.moveCursorToText('inpu¦tA="abc"');
|
2020-12-15 18:55:54 -05:00
|
|
|
|
2021-02-08 13:43:21 -05:00
|
|
|
const {textSpan, definitions} = getDefinitionsAndAssertBoundSpan(env, template);
|
|
|
|
expect(template.contents.substr(textSpan.start, textSpan.length)).toEqual('inputA');
|
|
|
|
|
|
|
|
expect(definitions!.length).toEqual(2);
|
|
|
|
const [def, def2] = definitions!;
|
2020-12-15 18:55:54 -05:00
|
|
|
expect(def.textSpan).toContain('inputA');
|
|
|
|
expect(def2.textSpan).toContain('inputA');
|
|
|
|
// TODO(atscott): investigate why the text span includes more than just 'inputA'
|
|
|
|
// assertTextSpans([def, def2], ['inputA']);
|
|
|
|
assertFileNames([def, def2], ['dir2.ts', 'dir.ts']);
|
|
|
|
});
|
|
|
|
|
2020-12-15 20:40:28 -05:00
|
|
|
it('gets definitions for all outputs when attribute matches more than one', () => {
|
|
|
|
initMockFileSystem('Native');
|
2021-02-08 13:43:21 -05:00
|
|
|
const files = {
|
|
|
|
'app.html': '<div dir (someEvent)="doSomething()"></div>',
|
|
|
|
'dir.ts': `
|
2020-12-15 20:40:28 -05:00
|
|
|
import {Directive, Output, EventEmitter} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[dir]'})
|
|
|
|
export class MyDir {
|
|
|
|
@Output() someEvent = new EventEmitter<void>();
|
|
|
|
}`,
|
2021-02-08 13:43:21 -05:00
|
|
|
'dir2.ts': `
|
2020-12-15 20:40:28 -05:00
|
|
|
import {Directive, Output, EventEmitter} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[dir]'})
|
|
|
|
export class MyDir2 {
|
|
|
|
@Output() someEvent = new EventEmitter<void>();
|
|
|
|
}`,
|
2021-02-08 13:43:21 -05:00
|
|
|
'app.ts': `
|
2020-12-15 20:40:28 -05:00
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
import {CommonModule} from '@angular/common';
|
|
|
|
|
|
|
|
@Component({templateUrl: 'app.html'})
|
|
|
|
export class AppCmp {
|
|
|
|
doSomething() {}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
};
|
2021-02-08 13:43:21 -05:00
|
|
|
const env = LanguageServiceTestEnv.setup();
|
|
|
|
const project = createModuleAndProjectWithDeclarations(env, 'test', files);
|
|
|
|
const template = project.openFile('app.html');
|
|
|
|
template.moveCursorToText('(someEv¦ent)');
|
|
|
|
|
|
|
|
const {textSpan, definitions} = getDefinitionsAndAssertBoundSpan(env, template);
|
|
|
|
expect(template.contents.substr(textSpan.start, textSpan.length)).toEqual('someEvent');
|
2020-12-15 20:40:28 -05:00
|
|
|
|
|
|
|
expect(definitions.length).toEqual(2);
|
|
|
|
const [def, def2] = definitions;
|
|
|
|
expect(def.textSpan).toContain('someEvent');
|
|
|
|
expect(def2.textSpan).toContain('someEvent');
|
|
|
|
// TODO(atscott): investigate why the text span includes more than just 'someEvent'
|
|
|
|
// assertTextSpans([def, def2], ['someEvent']);
|
|
|
|
assertFileNames([def, def2], ['dir2.ts', 'dir.ts']);
|
|
|
|
});
|
|
|
|
|
2021-04-09 13:24:08 -04:00
|
|
|
it('should go to the pre-compiled style sheet', () => {
|
|
|
|
initMockFileSystem('Native');
|
|
|
|
const files = {
|
|
|
|
'app.ts': `
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: '',
|
|
|
|
styleUrls: ['./style.css'],
|
|
|
|
})
|
|
|
|
export class AppCmp {}
|
|
|
|
`,
|
|
|
|
'style.scss': '',
|
|
|
|
};
|
|
|
|
const env = LanguageServiceTestEnv.setup();
|
|
|
|
const project = createModuleAndProjectWithDeclarations(env, 'test', files);
|
|
|
|
const appFile = project.openFile('app.ts');
|
|
|
|
appFile.moveCursorToText(`['./styl¦e.css']`);
|
|
|
|
const {textSpan, definitions} = getDefinitionsAndAssertBoundSpan(env, appFile);
|
|
|
|
expect(appFile.contents.substr(textSpan.start, textSpan.length)).toEqual('./style.css');
|
|
|
|
|
|
|
|
expect(definitions.length).toEqual(1);
|
|
|
|
assertFileNames(definitions, ['style.scss']);
|
|
|
|
});
|
|
|
|
|
2021-06-11 16:05:42 -04:00
|
|
|
it('gets definition for property of variable declared in template', () => {
|
|
|
|
initMockFileSystem('Native');
|
|
|
|
const files = {
|
|
|
|
'app.html': `
|
|
|
|
<ng-container *ngIf="{prop: myVal} as myVar">
|
|
|
|
{{myVar.prop.name}}
|
|
|
|
</ng-container>
|
|
|
|
`,
|
|
|
|
'app.ts': `
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({templateUrl: '/app.html'})
|
|
|
|
export class AppCmp {
|
|
|
|
myVal = {name: 'Andrew'};
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
};
|
|
|
|
const env = LanguageServiceTestEnv.setup();
|
|
|
|
|
|
|
|
const project = createModuleAndProjectWithDeclarations(env, 'test', files);
|
|
|
|
const template = project.openFile('app.html');
|
|
|
|
project.expectNoSourceDiagnostics();
|
|
|
|
|
|
|
|
template.moveCursorToText('{{myVar.pro¦p.name}}');
|
|
|
|
const {definitions} = getDefinitionsAndAssertBoundSpan(env, template);
|
|
|
|
expect(definitions![0].name).toEqual('"prop"');
|
|
|
|
assertFileNames(Array.from(definitions!), ['app.html']);
|
|
|
|
});
|
|
|
|
|
2021-02-08 13:43:21 -05:00
|
|
|
function getDefinitionsAndAssertBoundSpan(env: LanguageServiceTestEnv, file: OpenBuffer) {
|
2020-11-03 19:49:30 -05:00
|
|
|
env.expectNoSourceDiagnostics();
|
2021-02-08 13:43:21 -05:00
|
|
|
const definitionAndBoundSpan = file.getDefinitionAndBoundSpan();
|
2020-11-03 19:49:30 -05:00
|
|
|
const {textSpan, definitions} = definitionAndBoundSpan!;
|
|
|
|
expect(definitions).toBeTruthy();
|
feat(language-service): initial implementation for `findRenameLocations` (#40140)
This commit lays the groundwork for potentially providing rename
locations from the Ivy native LS. The approach is very similar to what
was done with the feature to find references. One difference, however,
is that we did not require the references to be fully "correct". That
is, the exact text spans did not matter so much, as long as we provide a
location that logically includes the referenced item.
An example of a necessary difference between rename locations and references is
directives. The entire element in the template is a "reference" of the
directive's class. However, it's not a valid location to be renamed. The
same goes for aliased inputs/outputs. The locations in the template
directly map to the class property, which is correct for references, but
would not be correct for rename locations, which should instead map to
the string node fo the alias.
As an initial approach to address the aforementioned issues with rename
locations, we check that all the rename location nodes have the same text. If
_any_ node has text that differs from the request, we do not return any
rename locations. This works as a way to prevent renames that could
break the the program by missing some required nodes in the rename action, but
allowing other nodes to be renamed.
PR Close #40140
2020-11-30 14:16:48 -05:00
|
|
|
return {textSpan, definitions: definitions!.map(d => humanizeDocumentSpanLike(d, env))};
|
2020-11-03 19:49:30 -05:00
|
|
|
}
|
|
|
|
});
|