Skip to content

Commit 3969096

Browse files
clydinAndrewKushnir
authored andcommitted
fix(compiler-cli): handle conditional expressions when extracting dependencies (#59637)
Updates the HMR dependencies extraction logic to handle conditional expressions. For example, `providers: [condition ? providersA : providersB]`. PR Close #59637
1 parent 26f6d4c commit 3969096

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

packages/compiler-cli/src/ngtsc/hmr/src/extract_dependencies.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ class PotentialTopLevelReadsVisitor extends o.RecursiveAstVisitor {
310310
return (parent.propertyName || parent.name) === node;
311311
}
312312

313+
if (ts.isConditionalExpression(parent)) {
314+
return parent.condition === node || parent.whenFalse === node || parent.whenTrue === node;
315+
}
316+
313317
// Otherwise it's not top-level.
314318
return false;
315319
}

packages/compiler-cli/test/ngtsc/hmr_spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,38 @@ runInEachFileSystem(() => {
549549
);
550550
});
551551

552+
it('should capture conditional expression dependencies', () => {
553+
enableHmr();
554+
env.write(
555+
'test.ts',
556+
`
557+
import {Component, InjectionToken} from '@angular/core';
558+
559+
const providersA: any[] = [];
560+
const providersB: any[] = [];
561+
const condition = true;
562+
563+
@Component({
564+
template: '',
565+
providers: [condition ? providersA : providersB]
566+
})
567+
export class Cmp {}
568+
`,
569+
);
570+
571+
env.driveMain();
572+
573+
const jsContents = env.getContents('test.js');
574+
const hmrContents = env.driveHmr('test.ts', 'Cmp');
575+
576+
expect(jsContents).toContain(
577+
'ɵɵreplaceMetadata(Cmp, m.default, [i0], [condition, providersA, providersB, Component]));',
578+
);
579+
expect(hmrContents).toContain(
580+
'export default function Cmp_UpdateMetadata(Cmp, ɵɵnamespaces, condition, providersA, providersB, Component) {',
581+
);
582+
});
583+
552584
it('should capture parenthesized dependencies', () => {
553585
enableHmr();
554586
env.write(

0 commit comments

Comments
 (0)