Skip to content

fix(46345): declare on computed class field with decorator makes invalid code that crashes due to missing _a #46446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ namespace ts {
//

const prefix = getClassMemberPrefix(node, member);
const memberName = getExpressionForPropertyName(member, /*generateNameForComputedPropertyName*/ true);
const memberName = getExpressionForPropertyName(member, /*generateNameForComputedPropertyName*/ !hasSyntacticModifier(member, ModifierFlags.Ambient));
const descriptor = languageVersion > ScriptTarget.ES3
? member.kind === SyntaxKind.PropertyDeclaration
// We emit `void 0` here to indicate to `__decorate` that it can invoke `Object.defineProperty` directly, but that it
Expand Down
26 changes: 26 additions & 0 deletions tests/baselines/reference/decoratorInAmbientContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [decoratorInAmbientContext.ts]
declare function decorator(target: any, key: any): any;

const b = Symbol('b');
class Foo {
@decorator declare a: number;
@decorator declare [b]: number;
}


//// [decoratorInAmbientContext.js]
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const b = Symbol('b');
class Foo {
}
__decorate([
decorator
], Foo.prototype, "a", void 0);
__decorate([
decorator
], Foo.prototype, b, void 0);
23 changes: 23 additions & 0 deletions tests/baselines/reference/decoratorInAmbientContext.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/conformance/decorators/decoratorInAmbientContext.ts ===
declare function decorator(target: any, key: any): any;
>decorator : Symbol(decorator, Decl(decoratorInAmbientContext.ts, 0, 0))
>target : Symbol(target, Decl(decoratorInAmbientContext.ts, 0, 27))
>key : Symbol(key, Decl(decoratorInAmbientContext.ts, 0, 39))

const b = Symbol('b');
>b : Symbol(b, Decl(decoratorInAmbientContext.ts, 2, 5))
>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, --, --))

class Foo {
>Foo : Symbol(Foo, Decl(decoratorInAmbientContext.ts, 2, 22))

@decorator declare a: number;
>decorator : Symbol(decorator, Decl(decoratorInAmbientContext.ts, 0, 0))
>a : Symbol(Foo.a, Decl(decoratorInAmbientContext.ts, 3, 11))

@decorator declare [b]: number;
>decorator : Symbol(decorator, Decl(decoratorInAmbientContext.ts, 0, 0))
>[b] : Symbol(Foo[b], Decl(decoratorInAmbientContext.ts, 4, 33))
>b : Symbol(b, Decl(decoratorInAmbientContext.ts, 2, 5))
}

25 changes: 25 additions & 0 deletions tests/baselines/reference/decoratorInAmbientContext.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/conformance/decorators/decoratorInAmbientContext.ts ===
declare function decorator(target: any, key: any): any;
>decorator : (target: any, key: any) => any
>target : any
>key : any

const b = Symbol('b');
>b : unique symbol
>Symbol('b') : unique symbol
>Symbol : SymbolConstructor
>'b' : "b"

class Foo {
>Foo : Foo

@decorator declare a: number;
>decorator : (target: any, key: any) => any
>a : number

@decorator declare [b]: number;
>decorator : (target: any, key: any) => any
>[b] : number
>b : unique symbol
}

10 changes: 10 additions & 0 deletions tests/cases/conformance/decorators/decoratorInAmbientContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @target: esnext
// @experimentalDecorators: true

declare function decorator(target: any, key: any): any;

const b = Symbol('b');
class Foo {
@decorator declare a: number;
@decorator declare [b]: number;
}