mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 16:38:05 -06:00
fix(35050): Decorator emit incorrect within try block (#41951)
* fix(35050): fix decorated block-scoped class emit * Only use internal name when targeting ES5/3 Co-authored-by: Ron Buckton <ron.buckton@microsoft.com>
This commit is contained in:
parent
e881a69939
commit
a15030ff6f
@ -823,7 +823,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}
|
||||
@ -1233,7 +1238,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);
|
||||
|
||||
38
tests/baselines/reference/decoratedBlockScopedClass1.js
Normal file
38
tests/baselines/reference/decoratedBlockScopedClass1.js
Normal file
@ -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
tests/baselines/reference/decoratedBlockScopedClass1.symbols
Normal file
28
tests/baselines/reference/decoratedBlockScopedClass1.symbols
Normal file
@ -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
tests/baselines/reference/decoratedBlockScopedClass1.types
Normal file
31
tests/baselines/reference/decoratedBlockScopedClass1.types
Normal file
@ -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
tests/baselines/reference/decoratedBlockScopedClass2.js
Normal file
44
tests/baselines/reference/decoratedBlockScopedClass2.js
Normal file
@ -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
tests/baselines/reference/decoratedBlockScopedClass2.symbols
Normal file
32
tests/baselines/reference/decoratedBlockScopedClass2.symbols
Normal file
@ -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
tests/baselines/reference/decoratedBlockScopedClass2.types
Normal file
35
tests/baselines/reference/decoratedBlockScopedClass2.types
Normal file
@ -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
tests/baselines/reference/decoratedBlockScopedClass3.js
Normal file
66
tests/baselines/reference/decoratedBlockScopedClass3.js
Normal file
@ -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
tests/baselines/reference/decoratedBlockScopedClass3.symbols
Normal file
51
tests/baselines/reference/decoratedBlockScopedClass3.symbols
Normal file
@ -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))
|
||||
|
||||
56
tests/baselines/reference/decoratedBlockScopedClass3.types
Normal file
56
tests/baselines/reference/decoratedBlockScopedClass3.types
Normal file
@ -0,0 +1,56 @@
|
||||
=== 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
|
||||
|
||||
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
|
||||
|
||||
@ -0,0 +1,16 @@
|
||||
// @target: es5
|
||||
// @experimentaldecorators: true
|
||||
// @emitDecoratorMetadata: true
|
||||
// @filename: a.ts
|
||||
|
||||
function decorator() {
|
||||
return (target: new (...args: any[]) => any) => {}
|
||||
}
|
||||
|
||||
@decorator()
|
||||
class Foo {
|
||||
public static func(): Foo {
|
||||
return new Foo();
|
||||
}
|
||||
}
|
||||
Foo.func();
|
||||
@ -0,0 +1,19 @@
|
||||
// @target: es5
|
||||
// @experimentaldecorators: true
|
||||
// @emitDecoratorMetadata: true
|
||||
// @filename: 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) {}
|
||||
@ -0,0 +1,27 @@
|
||||
// @target: es5
|
||||
// @experimentaldecorators: true
|
||||
// @emitDecoratorMetadata: true
|
||||
// @filename: 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) {}
|
||||
Loading…
x
Reference in New Issue
Block a user