Skip to content

Commit 1850f57

Browse files
Copilotjakebailey
andauthored
Fix panic on anonymous class declaration with decorator targeting ES2022 (#3926)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 913fed9 commit 1850f57

6 files changed

Lines changed: 117 additions & 1 deletion

File tree

internal/transformers/tstransforms/runtimesyntax.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ func (tx *RuntimeSyntaxTransformer) visitClassDeclaration(node *ast.ClassDeclara
676676
}
677677

678678
name := tx.Visitor().VisitNode(node.Name())
679-
if exported && name == nil {
679+
if name == nil && (exported || ast.ChildIsDecorated(tx.compilerOptions.ExperimentalDecorators.IsTrue(), node.AsNode(), nil)) {
680680
name = tx.Factory().NewGeneratedNameForNode(node.AsNode())
681681
}
682682
heritageClauses := tx.Visitor().VisitNodes(node.HeritageClauses)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
anonymousClassDecoratorEs2022.ts(1,1): error TS1211: A class declaration without the 'default' modifier must have a name.
2+
anonymousClassDecoratorEs2022.ts(2,4): error TS2304: Cannot find name 'x'.
3+
4+
5+
==== anonymousClassDecoratorEs2022.ts (2 errors) ====
6+
class {
7+
~~~~~
8+
!!! error TS1211: A class declaration without the 'default' modifier must have a name.
9+
@x
10+
~
11+
!!! error TS2304: Cannot find name 'x'.
12+
m() {
13+
// ...
14+
}
15+
};
16+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//// [tests/cases/compiler/anonymousClassDecoratorEs2022.ts] ////
2+
3+
//// [anonymousClassDecoratorEs2022.ts]
4+
class {
5+
@x
6+
m() {
7+
// ...
8+
}
9+
};
10+
11+
12+
//// [anonymousClassDecoratorEs2022.js]
13+
"use strict";
14+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
15+
var useValue = arguments.length > 2;
16+
for (var i = 0; i < initializers.length; i++) {
17+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
18+
}
19+
return useValue ? value : void 0;
20+
};
21+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
22+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
23+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
24+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
25+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
26+
var _, done = false;
27+
for (var i = decorators.length - 1; i >= 0; i--) {
28+
var context = {};
29+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
30+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
31+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
32+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
33+
if (kind === "accessor") {
34+
if (result === void 0) continue;
35+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
36+
if (_ = accept(result.get)) descriptor.get = _;
37+
if (_ = accept(result.set)) descriptor.set = _;
38+
if (_ = accept(result.init)) initializers.unshift(_);
39+
}
40+
else if (_ = accept(result)) {
41+
if (kind === "field") initializers.unshift(_);
42+
else descriptor[key] = _;
43+
}
44+
}
45+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
46+
done = true;
47+
};
48+
let default_1 = (() => {
49+
let _instanceExtraInitializers = [];
50+
let _m_decorators;
51+
return class default_1 {
52+
static {
53+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
54+
_m_decorators = [x];
55+
__esDecorate(this, null, _m_decorators, { kind: "method", name: "m", static: false, private: false, access: { has: obj => "m" in obj, get: obj => obj.m }, metadata: _metadata }, null, _instanceExtraInitializers);
56+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
57+
}
58+
m() {
59+
// ...
60+
}
61+
constructor() {
62+
__runInitializers(this, _instanceExtraInitializers);
63+
}
64+
};
65+
})();
66+
;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/anonymousClassDecoratorEs2022.ts] ////
2+
3+
=== anonymousClassDecoratorEs2022.ts ===
4+
class {
5+
@x
6+
m() {
7+
>m : Symbol(__missing.m, Decl(anonymousClassDecoratorEs2022.ts, 0, 7))
8+
9+
// ...
10+
}
11+
};
12+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tests/cases/compiler/anonymousClassDecoratorEs2022.ts] ////
2+
3+
=== anonymousClassDecoratorEs2022.ts ===
4+
class {
5+
@x
6+
>x : any
7+
8+
m() {
9+
>m : () => void
10+
11+
// ...
12+
}
13+
};
14+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @target: es2022
2+
3+
class {
4+
@x
5+
m() {
6+
// ...
7+
}
8+
};

0 commit comments

Comments
 (0)