Content-Length: 397839 | pFad | https://github.com/angular/angular/commit/1191e62d70ee16f3b083b635dd60a9f2e0c2d4c7

25 fix(compiler-cli): avoid ECMAScript private field metadata emit (#61227) · angular/angular@1191e62 · GitHub
Skip to content

Commit 1191e62

Browse files
clydinalxhub
authored andcommitted
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
1 parent 378ba12 commit 1191e62

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

packages/compiler-cli/src/ngtsc/annotations/common/src/metadata.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import ts from 'typescript';
2020

2121
import {
22+
ClassMemberAccessLevel,
2223
CtorParameter,
2324
DeclarationNode,
2425
Decorator,
@@ -84,11 +85,14 @@ export function extractClassMetadata(
8485

8586
// Do the same for property decorators.
8687
let metaPropDecorators: Expression | null = null;
87-
const classMembers = reflection
88-
.getMembersOfClass(clazz)
89-
.filter(
90-
(member) => !member.isStatic && member.decorators !== null && member.decorators.length > 0,
91-
);
88+
const classMembers = reflection.getMembersOfClass(clazz).filter(
89+
(member) =>
90+
!member.isStatic &&
91+
member.decorators !== null &&
92+
member.decorators.length > 0 &&
93+
// Private fields are not supported in the metadata emit
94+
member.accessLevel !== ClassMemberAccessLevel.EcmaScriptPrivate,
95+
);
9296
const duplicateDecoratedMembers = classMembers.filter(
9397
(member, i, arr) => arr.findIndex((arrayMember) => arrayMember.name === member.name) < i,
9498
);

packages/compiler-cli/src/ngtsc/annotations/common/test/metadata_spec.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,32 @@ runInEachFileSystem(() => {
111111
`{ 'has-dashes-in-name': [{ type: Input }], noDashesInName: [{ type: Input }] })`,
112112
);
113113
});
114+
115+
it('should not emit metadata for ECMAScript private fields', () => {
116+
const res = compileAndPrint(
117+
`
118+
import {Directive, Input} from '@angular/core';
119+
120+
@Directive() class Target {
121+
@Input() #privateName = 123;
122+
@Input() publicName = 456;
123+
}
124+
`,
125+
false,
126+
);
127+
expect(res).toContain(`{ publicName: [{ type: Input }] })`);
128+
});
114129
});
115130

116-
function compileAndPrint(contents: string): string {
131+
function compileAndPrint(contents: string, experimentalDecorators: boolean = true): string {
117132
const _ = absoluteFrom;
118133
const CORE: TestFile = {
119134
name: _('/node_modules/@angular/core/index.d.ts'),
120135
contents: `
121136
export declare function Input(...args: any[]): any;
122137
export declare function Inject(...args: any[]): any;
123138
export declare function Component(...args: any[]): any;
139+
export declare function Directive(...args: any[]): any;
124140
export declare class Injector {}
125141
`,
126142
};
@@ -133,7 +149,7 @@ runInEachFileSystem(() => {
133149
contents,
134150
},
135151
],
136-
{target: ts.ScriptTarget.ES2015},
152+
{target: ts.ScriptTarget.ES2015, experimentalDecorators},
137153
);
138154
const host = new TypeScriptReflectionHost(program.getTypeChecker());
139155
const target = getDeclaration(program, _('/index.ts'), 'Target', ts.isClassDeclaration);

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/angular/angular/commit/1191e62d70ee16f3b083b635dd60a9f2e0c2d4c7

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy