fix(core): static-query migration should handle queries on accessors (#30327)

Currently the static-query migration ignores queries declared on getters
or setters as these are not part of a `PropertyDeclaration`. We need to
handle these queries in order to cover all queries within a given project.

The usage strategy is not able to detect timing for queries on accessors,
so we add a TODO and print a message. The template strategy is able
to detect the proper timing for such queries because it's not dependent
on detecting the usage of the query.

Resolves FW-1215

PR Close #30327
This commit is contained in:
Paul Gschwendtner
2019-05-08 13:57:59 +02:00
committed by Alex Rickabaugh
parent cb6ad971c3
commit 0ffdb48f62
6 changed files with 138 additions and 13 deletions

View File

@ -401,6 +401,55 @@ describe('static-queries migration with template strategy', () => {
.toContain(`@ViewChild('myRef', { static: true }) query: any;`);
});
it('should detect queries declared on setter', async() => {
writeFile('/index.ts', `
import {Component, NgModule, ViewChild} from '@angular/core';
@Component({templateUrl: 'my-tmpl.html'})
export class MyComp {
@ViewChild('myRef')
set query(result: any) { /* noop */}
}
@NgModule({declarations: [MyComp]})
export class MyModule {}
`);
writeFile(`/my-tmpl.html`, `
<span #myRef>My Ref</span>
`);
await runMigration();
expect(tree.readContent('/index.ts'))
.toMatch(/@ViewChild\('myRef', { static: true }\)\s+set query/);
});
it('should detect queries declared on getter', async() => {
writeFile('/index.ts', `
import {Component, NgModule, ViewChild} from '@angular/core';
@Component({templateUrl: 'my-tmpl.html'})
export class MyComp {
@ViewChild('myRef')
get query() { return null; }
set query(result: any) { /* noop */}
}
@NgModule({declarations: [MyComp]})
export class MyModule {}
`);
writeFile(`/my-tmpl.html`, `
<span #myRef>My Ref</span>
`);
await runMigration();
expect(tree.readContent('/index.ts'))
.toMatch(/@ViewChild\('myRef', { static: true }\)\s+get query/);
});
it('should add a todo if a query is not declared in any component', async() => {
writeFile('/index.ts', `
import {Component, NgModule, ViewChild, SomeToken} from '@angular/core';

View File

@ -18,6 +18,7 @@ describe('static-queries migration with usage strategy', () => {
let tree: UnitTestTree;
let tmpDirPath: string;
let previousWorkingDir: string;
let warnOutput: string[] = [];
// Enables the query usage strategy when running the `static-query` migration. By
// default the schematic runs the template strategy and there is currently no easy
@ -39,6 +40,13 @@ describe('static-queries migration with usage strategy', () => {
projects: {t: {architect: {build: {options: {tsConfig: './tsconfig.json'}}}}}
}));
warnOutput = [];
runner.logger.subscribe(logEntry => {
if (logEntry.level === 'warn') {
warnOutput.push(logEntry.message);
}
});
previousWorkingDir = shx.pwd();
tmpDirPath = getSystemPath(host.root);
@ -252,6 +260,47 @@ describe('static-queries migration with usage strategy', () => {
.toContain(`@${queryType}('test', { /* test */ read: null, static: true }) query: any;`);
});
it('should add a todo for queries declared on setter', async() => {
writeFile('/index.ts', `
import {Component, ${queryType}} from '@angular/core';
@Component({template: '<span #test></span>'})
export class MyComp {
@${queryType}('test')
set query(result: any) {};
}
`);
await runMigration();
expect(tree.readContent('/index.ts'))
.toContain(`@${queryType}('test', /* TODO: add static flag */ {})`);
expect(warnOutput.length).toBe(1);
expect(warnOutput[0])
.toMatch(/index.ts@6:11: Queries defined on accessors cannot be analyzed.$/);
});
it('should add a todo for queries declared on getter', async() => {
writeFile('/index.ts', `
import {Component, ${queryType}} from '@angular/core';
@Component({template: '<span #test></span>'})
export class MyComp {
@${queryType}('test')
get query() { return null; }
set query(result: any) {}
}
`);
await runMigration();
expect(tree.readContent('/index.ts'))
.toContain(`@${queryType}('test', /* TODO: add static flag */ {})`);
expect(warnOutput.length).toBe(1);
expect(warnOutput[0])
.toMatch(/index.ts@6:11: Queries defined on accessors cannot be analyzed.$/);
});
it('should not overwrite existing explicit query timing', async() => {
writeFile('/index.ts', `
import {Component, ${queryType}} from '@angular/core';