Skip to content

Commit 360730c

Browse files
devversionkara
authored andcommitted
refactor(core): static-query migration does not detect external call expressions. (angular#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 angular#29133
1 parent fca1724 commit 360730c

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/core/schematics/migrations/static-queries/angular/declaration_usage_visitor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export class DeclarationUsageVisitor {
3535
return;
3636
}
3737

38-
const callExprSymbol = this.typeChecker.getSymbolAtLocation(node);
38+
const callExprType = this.typeChecker.getTypeAtLocation(node);
39+
const callExprSymbol = callExprType.getSymbol();
3940

4041
if (!callExprSymbol || !callExprSymbol.valueDeclaration ||
4142
!isFunctionLikeDeclaration(callExprSymbol.valueDeclaration)) {

packages/core/schematics/test/static_queries_migration_spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,35 @@ describe('static-queries migration', () => {
711711
.toContain(`@${queryType}('test', { static: true }) query2: any;`);
712712
});
713713

714+
it('should detect static queries used in external function-like declaration', () => {
715+
writeFile('/index.ts', `
716+
import {Component, ${queryType}} from '@angular/core';
717+
import {externalFn} from './external';
718+
719+
@Component({template: '<span #test></span>'})
720+
export class MyComp {
721+
private @${queryType}('test') query: any;
722+
723+
ngOnInit() {
724+
externalFn(this);
725+
}
726+
}
727+
`);
728+
729+
writeFile('/external.ts', `
730+
import {MyComp} from './index';
731+
732+
export function externalFn(ctx: MyComp) {
733+
ctx.query.usedStatically();
734+
}
735+
`);
736+
737+
runMigration();
738+
739+
expect(tree.readContent('/index.ts'))
740+
.toContain(`@${queryType}('test', { static: true }) query: any;`);
741+
});
742+
714743
it('should properly handle multiple tsconfig files', () => {
715744
writeFile('/src/index.ts', `
716745
import {Component, ${queryType}} from '@angular/core';

0 commit comments

Comments
 (0)