mirror of
https://github.com/oven-sh/bun
synced 2026-02-11 11:29:02 +00:00
Handle case with TS decorators and export default anonymous class (#3578)
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
@@ -18146,10 +18146,20 @@ fn NewParser_(
|
||||
data.default_name = createDefaultName(p, stmt.loc) catch unreachable;
|
||||
}
|
||||
|
||||
// We only inject a name into classes when there is a decorator
|
||||
if (class.class.has_decorators) {
|
||||
if (class.class.class_name == null or
|
||||
class.class.class_name.?.ref == null)
|
||||
{
|
||||
class.class.class_name = data.default_name;
|
||||
}
|
||||
}
|
||||
|
||||
// This is to handle TS decorators, mostly.
|
||||
var class_stmts = p.lowerClass(.{ .stmt = s2 });
|
||||
std.debug.assert(class_stmts[0].data == .s_class);
|
||||
|
||||
if (class_stmts.len > 1) {
|
||||
std.debug.assert(class_stmts[0].data == .s_class);
|
||||
data.value.stmt = class_stmts[0];
|
||||
stmts.append(stmt.*) catch {};
|
||||
stmts.appendSlice(class_stmts[1..]) catch {};
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
function decorator(target: any, propertyKey: any) {
|
||||
target[propertyKey + "decorated"] = true;
|
||||
}
|
||||
|
||||
export default class {
|
||||
@decorator
|
||||
method() {
|
||||
return 42;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
// @ts-nocheck
|
||||
import { test, expect, describe } from "bun:test";
|
||||
import DecoratedClass from "./decorator-export-default-class-fixture";
|
||||
import DecoratedAnonClass from "./decorator-export-default-class-fixture-anon";
|
||||
|
||||
test("decorator order of evaluation", () => {
|
||||
let counter = 0;
|
||||
@@ -990,6 +991,10 @@ describe("constructor statements", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("export default class works", () => {
|
||||
test("export default class Named works", () => {
|
||||
expect(new DecoratedClass()["methoddecorated"]).toBe(true);
|
||||
});
|
||||
|
||||
test("export default class works (anonymous name)", () => {
|
||||
expect(new DecoratedAnonClass()["methoddecorated"]).toBe(true);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
export default class {
|
||||
static {
|
||||
this.boop = "boop";
|
||||
}
|
||||
}
|
||||
6
test/transpiler/export-default.test.js
Normal file
6
test/transpiler/export-default.test.js
Normal file
@@ -0,0 +1,6 @@
|
||||
import WithStatic from "./export-default-with-static-initializer";
|
||||
import { test, expect } from "bun:test";
|
||||
|
||||
test("static initializer", () => {
|
||||
expect(WithStatic.boop).toBe("boop");
|
||||
});
|
||||
Reference in New Issue
Block a user