Skip to content
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

fix decorator and declare #3828

Merged
merged 2 commits into from
Jul 26, 2023
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
1 change: 1 addition & 0 deletions src/js_ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ pub const G = struct {
get,
set,
spread,
declare,
class_static_block,

pub fn jsonStringify(self: @This(), opts: anytype, o: anytype) !void {
Expand Down
11 changes: 10 additions & 1 deletion src/js_parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12062,7 +12062,14 @@ fn NewParser_(
// https://github.com/oven-sh/bun/issues/1907
if (opts.is_class and is_typescript_enabled and strings.eqlComptime(raw, "declare")) {
const scope_index = p.scopes_in_order.items.len;
_ = try p.parseProperty(kind, opts, null);
if (try p.parseProperty(kind, opts, null)) |_prop| {
var prop = _prop;
if (prop.kind == .normal and prop.value == null and opts.ts_decorators.len > 0) {
prop.kind = .declare;
return prop;
}
}

p.discardScopesUpTo(scope_index);
return null;
}
Expand Down Expand Up @@ -19512,6 +19519,8 @@ fn NewParser_(
}
}

// TODO: prop.kind == .declare and prop.value == null

if (prop.ts_decorators.len > 0) {
const loc = prop.key.?.loc;
const descriptor_key = switch (prop.key.?.data) {
Expand Down
20 changes: 20 additions & 0 deletions test/transpiler/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,3 +998,23 @@ test("export default class Named works", () => {
test("export default class works (anonymous name)", () => {
expect(new DecoratedAnonClass()["methoddecorated"]).toBe(true);
});

test("decorator and declare", () => {
let counter = 0;
function d1(t) {
t();
}
class A {
@d1(() => {
counter++;
})
declare a: number;

m() {
counter++;
}
}

new A();
expect(counter).toBe(1);
});