Skip to content

fix(35050): Decorator emit incorrect within try block #41951

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 3 commits into from
Jul 8, 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
14 changes: 12 additions & 2 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,12 @@ namespace ts {

const location = moveRangePastDecorators(node);
const classAlias = getClassAliasIfNeeded(node);
const declName = factory.getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);

// When we transform to ES5/3 this will be moved inside an IIFE and should reference the name
// without any block-scoped variable collision handling
const declName = languageVersion <= ScriptTarget.ES2015 ?
factory.getInternalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true) :
factory.getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);

// ... = class ${name} ${heritageClauses} {
// ${members}
Expand Down Expand Up @@ -1256,7 +1261,12 @@ namespace ts {
}

const classAlias = classAliases && classAliases[getOriginalNodeId(node)];
const localName = factory.getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);

// When we transform to ES5/3 this will be moved inside an IIFE and should reference the name
// without any block-scoped variable collision handling
const localName = languageVersion <= ScriptTarget.ES2015 ?
factory.getInternalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true) :
factory.getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
const decorate = emitHelpers().createDecorateHelper(decoratorExpressions, localName);
const expression = factory.createAssignment(localName, classAlias ? factory.createAssignment(classAlias, decorate) : decorate);
setEmitFlags(expression, EmitFlags.NoComments);
Expand Down
38 changes: 38 additions & 0 deletions tests/baselines/reference/decoratedBlockScopedClass1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [a.ts]
function decorator() {
return (target: new (...args: any[]) => any) => {}
}

@decorator()
class Foo {
public static func(): Foo {
return new Foo();
}
}
Foo.func();


//// [a.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;
};
function decorator() {
return function (target) { };
}
var Foo = /** @class */ (function () {
function Foo() {
}
Foo_1 = Foo;
Foo.func = function () {
return new Foo_1();
};
var Foo_1;
Foo = Foo_1 = __decorate([
decorator()
], Foo);
return Foo;
}());
Foo.func();
28 changes: 28 additions & 0 deletions tests/baselines/reference/decoratedBlockScopedClass1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/conformance/decorators/class/a.ts ===
function decorator() {
>decorator : Symbol(decorator, Decl(a.ts, 0, 0))

return (target: new (...args: any[]) => any) => {}
>target : Symbol(target, Decl(a.ts, 1, 12))
>args : Symbol(args, Decl(a.ts, 1, 25))
}

@decorator()
>decorator : Symbol(decorator, Decl(a.ts, 0, 0))

class Foo {
>Foo : Symbol(Foo, Decl(a.ts, 2, 1))

public static func(): Foo {
>func : Symbol(Foo.func, Decl(a.ts, 5, 11))
>Foo : Symbol(Foo, Decl(a.ts, 2, 1))

return new Foo();
>Foo : Symbol(Foo, Decl(a.ts, 2, 1))
}
}
Foo.func();
>Foo.func : Symbol(Foo.func, Decl(a.ts, 5, 11))
>Foo : Symbol(Foo, Decl(a.ts, 2, 1))
>func : Symbol(Foo.func, Decl(a.ts, 5, 11))

31 changes: 31 additions & 0 deletions tests/baselines/reference/decoratedBlockScopedClass1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
=== tests/cases/conformance/decorators/class/a.ts ===
function decorator() {
>decorator : () => (target: new (...args: any[]) => any) => void

return (target: new (...args: any[]) => any) => {}
>(target: new (...args: any[]) => any) => {} : (target: new (...args: any[]) => any) => void
>target : new (...args: any[]) => any
>args : any[]
}

@decorator()
>decorator() : (target: new (...args: any[]) => any) => void
>decorator : () => (target: new (...args: any[]) => any) => void

class Foo {
>Foo : Foo

public static func(): Foo {
>func : () => Foo

return new Foo();
>new Foo() : Foo
>Foo : typeof Foo
}
}
Foo.func();
>Foo.func() : Foo
>Foo.func : () => Foo
>Foo : typeof Foo
>func : () => Foo

44 changes: 44 additions & 0 deletions tests/baselines/reference/decoratedBlockScopedClass2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [a.ts]
function decorator() {
return (target: new (...args: any[]) => any) => {}
}

try {
@decorator()
class Foo {
public static func(): Foo {
return new Foo();
}
}
Foo.func();
}
catch (e) {}


//// [a.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;
};
function decorator() {
return function (target) { };
}
try {
var Foo_1 = /** @class */ (function () {
function Foo() {
}
Foo_2 = Foo;
Foo.func = function () {
return new Foo_2();
};
var Foo_2;
Foo = Foo_2 = __decorate([
decorator()
], Foo);
return Foo;
}());
Foo_1.func();
}
catch (e) { }
32 changes: 32 additions & 0 deletions tests/baselines/reference/decoratedBlockScopedClass2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/conformance/decorators/class/a.ts ===
function decorator() {
>decorator : Symbol(decorator, Decl(a.ts, 0, 0))

return (target: new (...args: any[]) => any) => {}
>target : Symbol(target, Decl(a.ts, 1, 12))
>args : Symbol(args, Decl(a.ts, 1, 25))
}

try {
@decorator()
>decorator : Symbol(decorator, Decl(a.ts, 0, 0))

class Foo {
>Foo : Symbol(Foo, Decl(a.ts, 4, 5))

public static func(): Foo {
>func : Symbol(Foo.func, Decl(a.ts, 6, 15))
>Foo : Symbol(Foo, Decl(a.ts, 4, 5))

return new Foo();
>Foo : Symbol(Foo, Decl(a.ts, 4, 5))
}
}
Foo.func();
>Foo.func : Symbol(Foo.func, Decl(a.ts, 6, 15))
>Foo : Symbol(Foo, Decl(a.ts, 4, 5))
>func : Symbol(Foo.func, Decl(a.ts, 6, 15))
}
catch (e) {}
>e : Symbol(e, Decl(a.ts, 13, 7))

35 changes: 35 additions & 0 deletions tests/baselines/reference/decoratedBlockScopedClass2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
=== tests/cases/conformance/decorators/class/a.ts ===
function decorator() {
>decorator : () => (target: new (...args: any[]) => any) => void

return (target: new (...args: any[]) => any) => {}
>(target: new (...args: any[]) => any) => {} : (target: new (...args: any[]) => any) => void
>target : new (...args: any[]) => any
>args : any[]
}

try {
@decorator()
>decorator() : (target: new (...args: any[]) => any) => void
>decorator : () => (target: new (...args: any[]) => any) => void

class Foo {
>Foo : Foo

public static func(): Foo {
>func : () => Foo

return new Foo();
>new Foo() : Foo
>Foo : typeof Foo
}
}
Foo.func();
>Foo.func() : Foo
>Foo.func : () => Foo
>Foo : typeof Foo
>func : () => Foo
}
catch (e) {}
>e : any

66 changes: 66 additions & 0 deletions tests/baselines/reference/decoratedBlockScopedClass3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//// [a.ts]
function decorator() {
return (target: new (...args: any[]) => any) => {}
}

@decorator()
class Foo {
public static func(): Foo {
return new Foo();
}
}
Foo.func();

try {
@decorator()
class Foo {
public static func(): Foo {
return new Foo();
}
}
Foo.func();
}
catch (e) {}


//// [a.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;
};
function decorator() {
return function (target) { };
}
var Foo = /** @class */ (function () {
function Foo() {
}
Foo_1 = Foo;
Foo.func = function () {
return new Foo_1();
};
var Foo_1;
Foo = Foo_1 = __decorate([
decorator()
], Foo);
return Foo;
}());
Foo.func();
try {
var Foo_2 = /** @class */ (function () {
function Foo() {
}
Foo_3 = Foo;
Foo.func = function () {
return new Foo_3();
};
var Foo_3;
Foo = Foo_3 = __decorate([
decorator()
], Foo);
return Foo;
}());
Foo_2.func();
}
catch (e) { }
51 changes: 51 additions & 0 deletions tests/baselines/reference/decoratedBlockScopedClass3.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
=== tests/cases/conformance/decorators/class/a.ts ===
function decorator() {
>decorator : Symbol(decorator, Decl(a.ts, 0, 0))

return (target: new (...args: any[]) => any) => {}
>target : Symbol(target, Decl(a.ts, 1, 12))
>args : Symbol(args, Decl(a.ts, 1, 25))
}

@decorator()
>decorator : Symbol(decorator, Decl(a.ts, 0, 0))

class Foo {
>Foo : Symbol(Foo, Decl(a.ts, 2, 1))

public static func(): Foo {
>func : Symbol(Foo.func, Decl(a.ts, 5, 11))
>Foo : Symbol(Foo, Decl(a.ts, 2, 1))

return new Foo();
>Foo : Symbol(Foo, Decl(a.ts, 2, 1))
}
}
Foo.func();
>Foo.func : Symbol(Foo.func, Decl(a.ts, 5, 11))
>Foo : Symbol(Foo, Decl(a.ts, 2, 1))
>func : Symbol(Foo.func, Decl(a.ts, 5, 11))

try {
@decorator()
>decorator : Symbol(decorator, Decl(a.ts, 0, 0))

class Foo {
>Foo : Symbol(Foo, Decl(a.ts, 12, 5))

public static func(): Foo {
>func : Symbol(Foo.func, Decl(a.ts, 14, 15))
>Foo : Symbol(Foo, Decl(a.ts, 12, 5))

return new Foo();
>Foo : Symbol(Foo, Decl(a.ts, 12, 5))
}
}
Foo.func();
>Foo.func : Symbol(Foo.func, Decl(a.ts, 14, 15))
>Foo : Symbol(Foo, Decl(a.ts, 12, 5))
>func : Symbol(Foo.func, Decl(a.ts, 14, 15))
}
catch (e) {}
>e : Symbol(e, Decl(a.ts, 21, 7))

Loading