Skip to content

Commit 40caf34

Browse files
authored
Fix decorator emit crash (#60224)
1 parent e99e6e2 commit 40caf34

File tree

6 files changed

+105
-11
lines changed

6 files changed

+105
-11
lines changed

src/compiler/transformers/esDecorators.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ export function transformESDecorators(context: TransformationContext): (x: Sourc
686686
let shouldTransformPrivateStaticElementsInClass = false;
687687

688688
// 1. Class decorators are evaluated outside of the private name scope of the class.
689-
const classDecorators = transformAllDecoratorsOfDeclaration(getAllDecoratorsOfClass(node));
689+
const classDecorators = transformAllDecoratorsOfDeclaration(getAllDecoratorsOfClass(node, /*useLegacyDecorators*/ false));
690690
if (classDecorators) {
691691
// - Since class decorators don't have privileged access to private names defined inside the class,
692692
// they must be evaluated outside of the class body.

src/compiler/transformers/legacyDecorators.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ export function transformLegacyDecorators(context: TransformationContext): (x: S
679679
* @param node The class node.
680680
*/
681681
function generateConstructorDecorationExpression(node: ClassExpression | ClassDeclaration) {
682-
const allDecorators = getAllDecoratorsOfClass(node);
682+
const allDecorators = getAllDecoratorsOfClass(node, /*useLegacyDecorators*/ true);
683683
const decoratorExpressions = transformAllDecoratorsOfDeclaration(allDecorators);
684684
if (!decoratorExpressions) {
685685
return undefined;

src/compiler/transformers/utilities.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,9 @@ function getDecoratorsOfParameters(node: FunctionLikeDeclaration | undefined) {
679679
*
680680
* @internal
681681
*/
682-
export function getAllDecoratorsOfClass(node: ClassLikeDeclaration): AllDecorators | undefined {
682+
export function getAllDecoratorsOfClass(node: ClassLikeDeclaration, useLegacyDecorators: boolean): AllDecorators | undefined {
683683
const decorators = getDecorators(node);
684-
const parameters = getDecoratorsOfParameters(getFirstConstructorWithBody(node));
684+
const parameters = useLegacyDecorators ? getDecoratorsOfParameters(getFirstConstructorWithBody(node)) : undefined;
685685
if (!some(decorators) && !some(parameters)) {
686686
return undefined;
687687
}
@@ -705,12 +705,12 @@ export function getAllDecoratorsOfClassElement(member: ClassElement, parent: Cla
705705
case SyntaxKind.GetAccessor:
706706
case SyntaxKind.SetAccessor:
707707
if (!useLegacyDecorators) {
708-
return getAllDecoratorsOfMethod(member as AccessorDeclaration);
708+
return getAllDecoratorsOfMethod(member as AccessorDeclaration, /*useLegacyDecorators*/ false);
709709
}
710-
return getAllDecoratorsOfAccessors(member as AccessorDeclaration, parent);
710+
return getAllDecoratorsOfAccessors(member as AccessorDeclaration, parent, /*useLegacyDecorators*/ true);
711711

712712
case SyntaxKind.MethodDeclaration:
713-
return getAllDecoratorsOfMethod(member as MethodDeclaration);
713+
return getAllDecoratorsOfMethod(member as MethodDeclaration, useLegacyDecorators);
714714

715715
case SyntaxKind.PropertyDeclaration:
716716
return getAllDecoratorsOfProperty(member as PropertyDeclaration);
@@ -726,7 +726,7 @@ export function getAllDecoratorsOfClassElement(member: ClassElement, parent: Cla
726726
* @param parent The class node that contains the accessor.
727727
* @param accessor The class accessor member.
728728
*/
729-
function getAllDecoratorsOfAccessors(accessor: AccessorDeclaration, parent: ClassExpression | ClassDeclaration): AllDecorators | undefined {
729+
function getAllDecoratorsOfAccessors(accessor: AccessorDeclaration, parent: ClassExpression | ClassDeclaration, useLegacyDecorators: boolean): AllDecorators | undefined {
730730
if (!accessor.body) {
731731
return undefined;
732732
}
@@ -741,7 +741,7 @@ function getAllDecoratorsOfAccessors(accessor: AccessorDeclaration, parent: Clas
741741
}
742742

743743
const decorators = getDecorators(firstAccessorWithDecorators);
744-
const parameters = getDecoratorsOfParameters(setAccessor);
744+
const parameters = useLegacyDecorators ? getDecoratorsOfParameters(setAccessor) : undefined;
745745
if (!some(decorators) && !some(parameters)) {
746746
return undefined;
747747
}
@@ -759,13 +759,13 @@ function getAllDecoratorsOfAccessors(accessor: AccessorDeclaration, parent: Clas
759759
*
760760
* @param method The class method member.
761761
*/
762-
function getAllDecoratorsOfMethod(method: MethodDeclaration | AccessorDeclaration): AllDecorators | undefined {
762+
function getAllDecoratorsOfMethod(method: MethodDeclaration | AccessorDeclaration, useLegacyDecorators: boolean): AllDecorators | undefined {
763763
if (!method.body) {
764764
return undefined;
765765
}
766766

767767
const decorators = getDecorators(method);
768-
const parameters = getDecoratorsOfParameters(method);
768+
const parameters = useLegacyDecorators ? getDecoratorsOfParameters(method) : undefined;
769769
if (!some(decorators) && !some(parameters)) {
770770
return undefined;
771771
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
parameterDecoratorsEmitCrash.ts(6,17): error TS1206: Decorators are not valid here.
2+
3+
4+
==== parameterDecoratorsEmitCrash.ts (1 errors) ====
5+
// https://github.com/microsoft/TypeScript/issues/58269
6+
declare var dec: any;
7+
8+
export class C {
9+
@dec x: any;
10+
constructor(@dec x: any) {}
11+
~
12+
!!! error TS1206: Decorators are not valid here.
13+
}
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//// [tests/cases/compiler/parameterDecoratorsEmitCrash.ts] ////
2+
3+
//// [parameterDecoratorsEmitCrash.ts]
4+
// https://github.com/microsoft/TypeScript/issues/58269
5+
declare var dec: any;
6+
7+
export class C {
8+
@dec x: any;
9+
constructor(@dec x: any) {}
10+
}
11+
12+
13+
//// [parameterDecoratorsEmitCrash.js]
14+
"use strict";
15+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
16+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
17+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
18+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
19+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
20+
var _, done = false;
21+
for (var i = decorators.length - 1; i >= 0; i--) {
22+
var context = {};
23+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
24+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
25+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
26+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
27+
if (kind === "accessor") {
28+
if (result === void 0) continue;
29+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
30+
if (_ = accept(result.get)) descriptor.get = _;
31+
if (_ = accept(result.set)) descriptor.set = _;
32+
if (_ = accept(result.init)) initializers.unshift(_);
33+
}
34+
else if (_ = accept(result)) {
35+
if (kind === "field") initializers.unshift(_);
36+
else descriptor[key] = _;
37+
}
38+
}
39+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
40+
done = true;
41+
};
42+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
43+
var useValue = arguments.length > 2;
44+
for (var i = 0; i < initializers.length; i++) {
45+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
46+
}
47+
return useValue ? value : void 0;
48+
};
49+
Object.defineProperty(exports, "__esModule", { value: true });
50+
exports.C = void 0;
51+
var C = function () {
52+
var _a;
53+
var _x_decorators;
54+
var _x_initializers = [];
55+
var _x_extraInitializers = [];
56+
return _a = /** @class */ (function () {
57+
function C(x) {
58+
this.x = __runInitializers(this, _x_initializers, void 0);
59+
__runInitializers(this, _x_extraInitializers);
60+
}
61+
return C;
62+
}()),
63+
(function () {
64+
var _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
65+
_x_decorators = [dec];
66+
__esDecorate(null, null, _x_decorators, { kind: "field", name: "x", static: false, private: false, access: { has: function (obj) { return "x" in obj; }, get: function (obj) { return obj.x; }, set: function (obj, value) { obj.x = value; } }, metadata: _metadata }, _x_initializers, _x_extraInitializers);
67+
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
68+
})(),
69+
_a;
70+
}();
71+
exports.C = C;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @noTypesAndSymbols: true
2+
3+
// https://github.com/microsoft/TypeScript/issues/58269
4+
declare var dec: any;
5+
6+
export class C {
7+
@dec x: any;
8+
constructor(@dec x: any) {}
9+
}

0 commit comments

Comments
 (0)