Skip to content

Commit 1ac0500

Browse files
JoostKjosephperrott
authored andcommitted
perf(compiler-cli): only generate template context declaration when used (#39321)
The variable declaration for a template context is only needed when it is referenced from somewhere, so the TCB operation to generate the declaration is marked as optional. PR Close #39321
1 parent 67ea7b6 commit 1ac0500

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,8 @@ class TcbTemplateContextOp extends TcbOp {
194194
super();
195195
}
196196

197-
get optional() {
198-
return false;
199-
}
197+
// The declaration of the context variable is only needed when the context is actually referenced.
198+
readonly optional = true;
200199

201200
execute(): ts.Identifier {
202201
// Allocate a template ctx variable and declare it with an 'any' type. The type of this variable

packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -763,13 +763,13 @@ describe('type check blocks', () => {
763763

764764
it('generates a references var when enabled', () => {
765765
const block = tcb(TEMPLATE, DIRECTIVES);
766-
expect(block).toContain('var _t2 = (_t1 as any as core.TemplateRef<any>);');
766+
expect(block).toContain('var _t1 = (_t2 as any as core.TemplateRef<any>);');
767767
});
768768

769769
it('generates a reference var when disabled', () => {
770770
const DISABLED_CONFIG: TypeCheckingConfig = {...BASE_CONFIG, checkTemplateBodies: false};
771771
const block = tcb(TEMPLATE, DIRECTIVES, DISABLED_CONFIG);
772-
expect(block).toContain('var _t2 = (_t1 as any as core.TemplateRef<any>);');
772+
expect(block).toContain('var _t1 = (_t2 as any as core.TemplateRef<any>);');
773773
});
774774
});
775775

@@ -913,8 +913,8 @@ describe('type check blocks', () => {
913913
it('should trace references to an <ng-template> when enabled', () => {
914914
const block = tcb(TEMPLATE, DIRECTIVES);
915915
expect(block).toContain(
916-
'var _t4 = (_t3 as any as core.TemplateRef<any>); ' +
917-
'"" + (((_t4).value2));');
916+
'var _t3 = (_t4 as any as core.TemplateRef<any>); ' +
917+
'"" + (((_t3).value2));');
918918
});
919919

920920
it('should use any for reference types when disabled', () => {

0 commit comments

Comments
 (0)