Skip to content

Commit 6da9e58

Browse files
crisbetoAndrewKushnir
authored andcommitted
fix(compiler-cli): preserve quotes in class member names (angular#38387)
When we were outputting class members for `setClassMetadata` calls, we were using the string representation of the member name. This can lead to us generating invalid code when the name contains dashes and is quoted (e.g. `@Output() 'has-dashes' = new EventEmitter()`), because the quotes will be stripped for the string representation. These changes fix the issue by using the original name AST node that was used for the declaration and which knows whether it's supposed to be quoted or not. Fixes angular#38311. PR Close angular#38387
1 parent 250e299 commit 6da9e58

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export function generateSetClassMetadataCall(
7070
`Duplicate decorated properties found on class '${clazz.name.text}': ` +
7171
duplicateDecoratedMemberNames.join(', '));
7272
}
73-
const decoratedMembers =
74-
classMembers.map(member => classMemberToMetadata(member.name, member.decorators!, isCore));
73+
const decoratedMembers = classMembers.map(
74+
member => classMemberToMetadata(member.nameNode ?? member.name, member.decorators!, isCore));
7575
if (decoratedMembers.length > 0) {
7676
metaPropDecorators = ts.createObjectLiteral(decoratedMembers);
7777
}
@@ -127,7 +127,7 @@ function ctorParameterToMetadata(
127127
* Convert a reflected class member to metadata.
128128
*/
129129
function classMemberToMetadata(
130-
name: string, decorators: Decorator[], isCore: boolean): ts.PropertyAssignment {
130+
name: ts.PropertyName|string, decorators: Decorator[], isCore: boolean): ts.PropertyAssignment {
131131
const ngDecorators = decorators.filter(dec => isAngularDecorator(dec, isCore))
132132
.map((decorator: Decorator) => decoratorToMetadata(decorator));
133133
const decoratorMeta = ts.createArrayLiteral(ngDecorators);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ runInEachFileSystem(() => {
9090
`);
9191
expect(res).toBe('');
9292
});
93+
94+
it('should preserve quotes around class member names', () => {
95+
const res = compileAndPrint(`
96+
import {Component, Input} from '@angular/core';
97+
98+
@Component('metadata') class Target {
99+
@Input() 'has-dashes-in-name' = 123;
100+
@Input() noDashesInName = 456;
101+
}
102+
`);
103+
expect(res).toContain(
104+
`{ 'has-dashes-in-name': [{ type: Input }], noDashesInName: [{ type: Input }] })`);
105+
});
93106
});
94107

95108
function compileAndPrint(contents: string): string {

0 commit comments

Comments
 (0)