Skip to content

Commit 5a13dff

Browse files
clydinAndrewKushnir
authored andcommitted
fix(compiler-cli): handle new expressions when extracting dependencies (#59637)
Updates the HMR dependencies extraction logic to handle new expressions. For example, `deps: [[new Optional(), dep]]`. PR Close #59637
1 parent 8de0f3f commit 5a13dff

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ class PotentialTopLevelReadsVisitor extends o.RecursiveAstVisitor {
245245
ts.isWhileStatement(parent) ||
246246
ts.isSwitchStatement(parent) ||
247247
ts.isCaseClause(parent) ||
248-
ts.isThrowStatement(parent)
248+
ts.isThrowStatement(parent) ||
249+
ts.isNewExpression(parent)
249250
) {
250251
return parent.expression === node;
251252
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,43 @@ runInEachFileSystem(() => {
616616
);
617617
});
618618

619+
it('should capture new expression dependencies', () => {
620+
enableHmr();
621+
env.write(
622+
'test.ts',
623+
`
624+
import {Component, InjectionToken, Optional} from '@angular/core';
625+
const token = new InjectionToken<number>('TEST');
626+
const dep = new InjectionToken<number>('TEST-DEP');
627+
const value = 5;
628+
@Component({
629+
template: '',
630+
providers: [{
631+
provide: token,
632+
useFactory: () => {
633+
const v = value;
634+
return v;
635+
},
636+
deps: [[new Optional(), dep]]
637+
}]
638+
})
639+
export class Cmp {}
640+
`,
641+
);
642+
643+
env.driveMain();
644+
645+
const jsContents = env.getContents('test.js');
646+
const hmrContents = env.driveHmr('test.ts', 'Cmp');
647+
648+
expect(jsContents).toContain(
649+
'ɵɵreplaceMetadata(Cmp, m.default, [i0], [token, value, Optional, dep, Component]));',
650+
);
651+
expect(hmrContents).toContain(
652+
'export default function Cmp_UpdateMetadata(Cmp, ɵɵnamespaces, token, value, Optional, dep, Component) {',
653+
);
654+
});
655+
619656
it('should preserve eager standalone imports in HMR even if they are not used in the template', () => {
620657
enableHmr({
621658
// Disable class metadata since it can add noise to the test.

0 commit comments

Comments
 (0)