Skip to content

Commit 4b794fe

Browse files
authored
fix(46345): omit temp variable for computed property name in ambient context (microsoft#46446)
1 parent bedc8d4 commit 4b794fe

File tree

5 files changed

+85
-1
lines changed

5 files changed

+85
-1
lines changed

src/compiler/transformers/ts.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ namespace ts {
11891189
//
11901190

11911191
const prefix = getClassMemberPrefix(node, member);
1192-
const memberName = getExpressionForPropertyName(member, /*generateNameForComputedPropertyName*/ true);
1192+
const memberName = getExpressionForPropertyName(member, /*generateNameForComputedPropertyName*/ !hasSyntacticModifier(member, ModifierFlags.Ambient));
11931193
const descriptor = languageVersion > ScriptTarget.ES3
11941194
? member.kind === SyntaxKind.PropertyDeclaration
11951195
// We emit `void 0` here to indicate to `__decorate` that it can invoke `Object.defineProperty` directly, but that it
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [decoratorInAmbientContext.ts]
2+
declare function decorator(target: any, key: any): any;
3+
4+
const b = Symbol('b');
5+
class Foo {
6+
@decorator declare a: number;
7+
@decorator declare [b]: number;
8+
}
9+
10+
11+
//// [decoratorInAmbientContext.js]
12+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
13+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
14+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
15+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
16+
return c > 3 && r && Object.defineProperty(target, key, r), r;
17+
};
18+
const b = Symbol('b');
19+
class Foo {
20+
}
21+
__decorate([
22+
decorator
23+
], Foo.prototype, "a", void 0);
24+
__decorate([
25+
decorator
26+
], Foo.prototype, b, void 0);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/conformance/decorators/decoratorInAmbientContext.ts ===
2+
declare function decorator(target: any, key: any): any;
3+
>decorator : Symbol(decorator, Decl(decoratorInAmbientContext.ts, 0, 0))
4+
>target : Symbol(target, Decl(decoratorInAmbientContext.ts, 0, 27))
5+
>key : Symbol(key, Decl(decoratorInAmbientContext.ts, 0, 39))
6+
7+
const b = Symbol('b');
8+
>b : Symbol(b, Decl(decoratorInAmbientContext.ts, 2, 5))
9+
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
10+
11+
class Foo {
12+
>Foo : Symbol(Foo, Decl(decoratorInAmbientContext.ts, 2, 22))
13+
14+
@decorator declare a: number;
15+
>decorator : Symbol(decorator, Decl(decoratorInAmbientContext.ts, 0, 0))
16+
>a : Symbol(Foo.a, Decl(decoratorInAmbientContext.ts, 3, 11))
17+
18+
@decorator declare [b]: number;
19+
>decorator : Symbol(decorator, Decl(decoratorInAmbientContext.ts, 0, 0))
20+
>[b] : Symbol(Foo[b], Decl(decoratorInAmbientContext.ts, 4, 33))
21+
>b : Symbol(b, Decl(decoratorInAmbientContext.ts, 2, 5))
22+
}
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/conformance/decorators/decoratorInAmbientContext.ts ===
2+
declare function decorator(target: any, key: any): any;
3+
>decorator : (target: any, key: any) => any
4+
>target : any
5+
>key : any
6+
7+
const b = Symbol('b');
8+
>b : unique symbol
9+
>Symbol('b') : unique symbol
10+
>Symbol : SymbolConstructor
11+
>'b' : "b"
12+
13+
class Foo {
14+
>Foo : Foo
15+
16+
@decorator declare a: number;
17+
>decorator : (target: any, key: any) => any
18+
>a : number
19+
20+
@decorator declare [b]: number;
21+
>decorator : (target: any, key: any) => any
22+
>[b] : number
23+
>b : unique symbol
24+
}
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @target: esnext
2+
// @experimentalDecorators: true
3+
4+
declare function decorator(target: any, key: any): any;
5+
6+
const b = Symbol('b');
7+
class Foo {
8+
@decorator declare a: number;
9+
@decorator declare [b]: number;
10+
}

0 commit comments

Comments
 (0)