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 {absoluteFrom, AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
|
|
|
|
import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
|
2020-11-03 19:49:30 -05:00
|
|
|
|
2020-12-15 18:55:54 -05:00
|
|
|
import {extractCursorInfo, LanguageServiceTestEnvironment} from './env';
|
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
|
|
|
import {assertFileNames, createModuleWithDeclarations, humanizeDocumentSpanLike} from './test_utils';
|
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');
|
|
|
|
const templateFile = {contents: '', name: absoluteFrom('/app.html')};
|
|
|
|
const appFile = {
|
|
|
|
name: absoluteFrom('/app.ts'),
|
|
|
|
contents: `
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({templateUrl: '/app.html'})
|
|
|
|
export class AppCmp {}
|
|
|
|
`,
|
|
|
|
};
|
|
|
|
|
|
|
|
const env = createModuleWithDeclarations([appFile], [templateFile]);
|
2021-01-21 18:54:40 -05:00
|
|
|
const {cursor} = env.updateFileWithCursor(
|
|
|
|
absoluteFrom('/app.html'), '<input #myInput /> {{myIn¦put.value}}');
|
2021-01-15 18:57:24 -05:00
|
|
|
env.expectNoSourceDiagnostics();
|
|
|
|
const {definitions} = env.ngLS.getDefinitionAndBoundSpan(absoluteFrom('/app.html'), cursor)!;
|
|
|
|
expect(definitions![0].name).toEqual('myInput');
|
|
|
|
assertFileNames(Array.from(definitions!), ['app.html']);
|
|
|
|
});
|
|
|
|
|
2020-11-03 19:49:30 -05:00
|
|
|
it('returns the pipe class as definition when checkTypeOfPipes is false', () => {
|
|
|
|
initMockFileSystem('Native');
|
2020-12-15 18:55:54 -05:00
|
|
|
const {cursor, text} = extractCursorInfo('{{"1/1/2020" | dat¦e}}');
|
|
|
|
const templateFile = {contents: text, name: absoluteFrom('/app.html')};
|
|
|
|
const appFile = {
|
|
|
|
name: absoluteFrom('/app.ts'),
|
|
|
|
contents: `
|
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 {}
|
|
|
|
`,
|
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
|
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
|
|
|
const env = createModuleWithDeclarations([appFile], [templateFile], {strictTemplates: false});
|
2020-12-15 18:55:54 -05:00
|
|
|
const {textSpan, definitions} =
|
|
|
|
getDefinitionsAndAssertBoundSpan(env, absoluteFrom('/app.html'), cursor);
|
|
|
|
expect(text.substr(textSpan.start, textSpan.length)).toEqual('date');
|
2020-11-03 19:49:30 -05:00
|
|
|
|
2020-12-15 18:55:54 -05:00
|
|
|
expect(definitions.length).toEqual(1);
|
2020-11-03 19:49:30 -05:00
|
|
|
const [def] = definitions;
|
|
|
|
expect(def.textSpan).toContain('DatePipe');
|
|
|
|
expect(def.contextSpan).toContain('DatePipe');
|
|
|
|
});
|
|
|
|
|
2020-12-15 18:55:54 -05:00
|
|
|
it('gets definitions for all inputs when attribute matches more than one', () => {
|
|
|
|
initMockFileSystem('Native');
|
|
|
|
const {cursor, text} = extractCursorInfo('<div dir inpu¦tA="abc"></div>');
|
|
|
|
const templateFile = {contents: text, name: absoluteFrom('/app.html')};
|
|
|
|
const dirFile = {
|
|
|
|
name: absoluteFrom('/dir.ts'),
|
|
|
|
contents: `
|
|
|
|
import {Directive, Input} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[dir]'})
|
|
|
|
export class MyDir {
|
|
|
|
@Input() inputA!: any;
|
|
|
|
}`,
|
|
|
|
};
|
|
|
|
const dirFile2 = {
|
|
|
|
name: absoluteFrom('/dir2.ts'),
|
|
|
|
contents: `
|
|
|
|
import {Directive, Input} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[dir]'})
|
|
|
|
export class MyDir2 {
|
|
|
|
@Input() inputA!: any;
|
|
|
|
}`,
|
|
|
|
};
|
|
|
|
const appFile = {
|
|
|
|
name: absoluteFrom('/app.ts'),
|
|
|
|
contents: `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
import {CommonModule} from '@angular/common';
|
|
|
|
|
|
|
|
@Component({templateUrl: 'app.html'})
|
|
|
|
export class AppCmp {}
|
|
|
|
`
|
|
|
|
};
|
|
|
|
const env = createModuleWithDeclarations([appFile, dirFile, dirFile2], [templateFile]);
|
|
|
|
const {textSpan, definitions} =
|
|
|
|
getDefinitionsAndAssertBoundSpan(env, absoluteFrom('/app.html'), cursor);
|
|
|
|
expect(text.substr(textSpan.start, textSpan.length)).toEqual('inputA');
|
|
|
|
|
|
|
|
expect(definitions.length).toEqual(2);
|
|
|
|
const [def, def2] = definitions;
|
|
|
|
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');
|
|
|
|
const {cursor, text} = extractCursorInfo('<div dir (someEv¦ent)="doSomething()"></div>');
|
|
|
|
const templateFile = {contents: text, name: absoluteFrom('/app.html')};
|
|
|
|
const dirFile = {
|
|
|
|
name: absoluteFrom('/dir.ts'),
|
|
|
|
contents: `
|
|
|
|
import {Directive, Output, EventEmitter} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[dir]'})
|
|
|
|
export class MyDir {
|
|
|
|
@Output() someEvent = new EventEmitter<void>();
|
|
|
|
}`,
|
|
|
|
};
|
|
|
|
const dirFile2 = {
|
|
|
|
name: absoluteFrom('/dir2.ts'),
|
|
|
|
contents: `
|
|
|
|
import {Directive, Output, EventEmitter} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[dir]'})
|
|
|
|
export class MyDir2 {
|
|
|
|
@Output() someEvent = new EventEmitter<void>();
|
|
|
|
}`,
|
|
|
|
};
|
|
|
|
const appFile = {
|
|
|
|
name: absoluteFrom('/app.ts'),
|
|
|
|
contents: `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
import {CommonModule} from '@angular/common';
|
|
|
|
|
|
|
|
@Component({templateUrl: 'app.html'})
|
|
|
|
export class AppCmp {
|
|
|
|
doSomething() {}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
};
|
|
|
|
const env = createModuleWithDeclarations([appFile, dirFile, dirFile2], [templateFile]);
|
|
|
|
const {textSpan, definitions} =
|
|
|
|
getDefinitionsAndAssertBoundSpan(env, absoluteFrom('/app.html'), cursor);
|
|
|
|
expect(text.substr(textSpan.start, textSpan.length)).toEqual('someEvent');
|
|
|
|
|
|
|
|
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']);
|
|
|
|
});
|
|
|
|
|
2020-11-03 19:49:30 -05:00
|
|
|
function getDefinitionsAndAssertBoundSpan(
|
2020-12-15 18:55:54 -05:00
|
|
|
env: LanguageServiceTestEnvironment, fileName: AbsoluteFsPath, cursor: number) {
|
2020-11-03 19:49:30 -05:00
|
|
|
env.expectNoSourceDiagnostics();
|
2020-12-15 18:55:54 -05:00
|
|
|
const definitionAndBoundSpan = env.ngLS.getDefinitionAndBoundSpan(fileName, cursor);
|
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
|
|
|
}
|
|
|
|
});
|