Skip to content

Commit 778460f

Browse files
JoostKcrisbeto
authored andcommitted
fix(compiler-cli): support qualified names in typeof type references
This commit expands the static interpreter to now understand qualified names in `typeof` type queries. Fixes #65686 (cherry picked from commit f12e160)
1 parent 4d9c456 commit 778460f

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

packages/compiler-cli/src/ngtsc/partial_evaluator/src/interpreter.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,13 +734,14 @@ export class StaticInterpreter {
734734
}
735735

736736
private visitTypeQuery(node: ts.TypeQueryNode, context: Context): ResolvedValue {
737-
if (!ts.isIdentifier(node.exprName)) {
737+
const exprName = ts.isQualifiedName(node.exprName) ? node.exprName.right : node.exprName;
738+
if (!ts.isIdentifier(exprName)) {
738739
return DynamicValue.fromUnknown(node);
739740
}
740741

741-
const decl = this.host.getDeclarationOfIdentifier(node.exprName);
742+
const decl = this.host.getDeclarationOfIdentifier(exprName);
742743
if (decl === null) {
743-
return DynamicValue.fromUnknownIdentifier(node.exprName);
744+
return DynamicValue.fromUnknownIdentifier(exprName);
744745
}
745746

746747
const declContext: Context = {...context, ...joinModuleContext(context, node, decl)};

packages/compiler-cli/src/ngtsc/partial_evaluator/test/evaluator_spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,33 @@ runInEachFileSystem(() => {
425425
expect(local.ownedByModuleGuess).toBeNull();
426426
});
427427

428+
// https://github.com/angular/angular/issues/65686
429+
it('supports declarations of readonly tuples with class references using qualified names', () => {
430+
const tuple = evaluate(
431+
`
432+
import * as ext from 'external';
433+
declare class Local {}
434+
declare const x: readonly [typeof ext.External];`,
435+
`x`,
436+
[
437+
{
438+
name: _('/node_modules/external/index.d.ts'),
439+
contents: 'export declare class External {}',
440+
},
441+
],
442+
);
443+
if (!Array.isArray(tuple)) {
444+
return fail('Should have evaluated tuple as an array');
445+
}
446+
const [external] = tuple;
447+
if (!(external instanceof Reference)) {
448+
return fail('Should have evaluated `typeof ext.External` to a Reference');
449+
}
450+
expect(ts.isClassDeclaration(external.node)).toBe(true);
451+
expect(external.debugName).toBe('External');
452+
expect(external.ownedByModuleGuess).toBe('external');
453+
});
454+
428455
it('evaluates tuple elements it cannot understand to DynamicValue', () => {
429456
const value = evaluate(`declare const x: ['foo', string];`, `x`) as [string, DynamicValue];
430457

0 commit comments

Comments
 (0)