From 7a3a41f33f1c24bb985eee253b6dbe4ebeb1db73 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Thu, 8 May 2025 16:51:07 -0400 Subject: [PATCH] fix(compiler-cli): avoid ECMAScript private field metadata emit (#61227) The Angular class metadata emit structure does not support the use of private fields. If the class metadata emit is enabled and an ECMAScript private (i.e., `#` prefixed) member contains a decorator, the member will now be excluded from the emitting `setClassMetadata` call. This prevents runtime errors due to invalid syntax. PR Close #61227 --- .../ngtsc/annotations/common/src/metadata.ts | 14 ++++++++----- .../annotations/common/test/metadata_spec.ts | 20 +++++++++++++++++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/annotations/common/src/metadata.ts b/packages/compiler-cli/src/ngtsc/annotations/common/src/metadata.ts index 33719780c96..e6d782d461c 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/common/src/metadata.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/common/src/metadata.ts @@ -19,6 +19,7 @@ import { import ts from 'typescript'; import { + ClassMemberAccessLevel, CtorParameter, DeclarationNode, Decorator, @@ -84,11 +85,14 @@ export function extractClassMetadata( // Do the same for property decorators. let metaPropDecorators: Expression | null = null; - const classMembers = reflection - .getMembersOfClass(clazz) - .filter( - (member) => !member.isStatic && member.decorators !== null && member.decorators.length > 0, - ); + const classMembers = reflection.getMembersOfClass(clazz).filter( + (member) => + !member.isStatic && + member.decorators !== null && + member.decorators.length > 0 && + // Private fields are not supported in the metadata emit + member.accessLevel !== ClassMemberAccessLevel.EcmaScriptPrivate, + ); const duplicateDecoratedMembers = classMembers.filter( (member, i, arr) => arr.findIndex((arrayMember) => arrayMember.name === member.name) < i, ); diff --git a/packages/compiler-cli/src/ngtsc/annotations/common/test/metadata_spec.ts b/packages/compiler-cli/src/ngtsc/annotations/common/test/metadata_spec.ts index 71282476817..5814cf434d8 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/common/test/metadata_spec.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/common/test/metadata_spec.ts @@ -111,9 +111,24 @@ runInEachFileSystem(() => { `{ 'has-dashes-in-name': [{ type: Input }], noDashesInName: [{ type: Input }] })`, ); }); + + it('should not emit metadata for ECMAScript private fields', () => { + const res = compileAndPrint( + ` + import {Directive, Input} from '@angular/core'; + + @Directive() class Target { + @Input() #privateName = 123; + @Input() publicName = 456; + } + `, + false, + ); + expect(res).toContain(`{ publicName: [{ type: Input }] })`); + }); }); - function compileAndPrint(contents: string): string { + function compileAndPrint(contents: string, experimentalDecorators: boolean = true): string { const _ = absoluteFrom; const CORE: TestFile = { name: _('/node_modules/@angular/core/index.d.ts'), @@ -121,6 +136,7 @@ runInEachFileSystem(() => { export declare function Input(...args: any[]): any; export declare function Inject(...args: any[]): any; export declare function Component(...args: any[]): any; + export declare function Directive(...args: any[]): any; export declare class Injector {} `, }; @@ -133,7 +149,7 @@ runInEachFileSystem(() => { contents, }, ], - {target: ts.ScriptTarget.ES2015}, + {target: ts.ScriptTarget.ES2015, experimentalDecorators}, ); const host = new TypeScriptReflectionHost(program.getTypeChecker()); const target = getDeclaration(program, _('/index.ts'), 'Target', ts.isClassDeclaration);