From 360730ce594bb840618e3047a97922dee3faa042 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Sat, 9 Mar 2019 16:10:50 +0100 Subject: [PATCH] refactor(core): static-query migration does not detect external call expressions. (#29133) Currently the static-query migration does not properly handle functions which are declared externally. This is because we don't resolve the symbol of the call-expression through its type. Currently we just determine the symbol of the call-expression through the given call expression node, which doesn't necessarily refer to the *value declaration* of the call expression. e.g. the symbol refers to the import declaration which imports the external function. This means that we currently can't check the external function as we couldn't find the actual value declaration. We can fix this by resolving the type of the call expression and using the type in order to retrieve the symbol containing the *value declaration* PR Close #29133 --- .../angular/declaration_usage_visitor.ts | 3 +- .../test/static_queries_migration_spec.ts | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/core/schematics/migrations/static-queries/angular/declaration_usage_visitor.ts b/packages/core/schematics/migrations/static-queries/angular/declaration_usage_visitor.ts index 1a5a3732a6..62972e61fa 100644 --- a/packages/core/schematics/migrations/static-queries/angular/declaration_usage_visitor.ts +++ b/packages/core/schematics/migrations/static-queries/angular/declaration_usage_visitor.ts @@ -35,7 +35,8 @@ export class DeclarationUsageVisitor { return; } - const callExprSymbol = this.typeChecker.getSymbolAtLocation(node); + const callExprType = this.typeChecker.getTypeAtLocation(node); + const callExprSymbol = callExprType.getSymbol(); if (!callExprSymbol || !callExprSymbol.valueDeclaration || !isFunctionLikeDeclaration(callExprSymbol.valueDeclaration)) { diff --git a/packages/core/schematics/test/static_queries_migration_spec.ts b/packages/core/schematics/test/static_queries_migration_spec.ts index dc23eae3b0..849ebae5fe 100644 --- a/packages/core/schematics/test/static_queries_migration_spec.ts +++ b/packages/core/schematics/test/static_queries_migration_spec.ts @@ -711,6 +711,35 @@ describe('static-queries migration', () => { .toContain(`@${queryType}('test', { static: true }) query2: any;`); }); + it('should detect static queries used in external function-like declaration', () => { + writeFile('/index.ts', ` + import {Component, ${queryType}} from '@angular/core'; + import {externalFn} from './external'; + + @Component({template: ''}) + export class MyComp { + private @${queryType}('test') query: any; + + ngOnInit() { + externalFn(this); + } + } + `); + + writeFile('/external.ts', ` + import {MyComp} from './index'; + + export function externalFn(ctx: MyComp) { + ctx.query.usedStatically(); + } + `); + + runMigration(); + + expect(tree.readContent('/index.ts')) + .toContain(`@${queryType}('test', { static: true }) query: any;`); + }); + it('should properly handle multiple tsconfig files', () => { writeFile('/src/index.ts', ` import {Component, ${queryType}} from '@angular/core';