aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-04-29 21:37:04 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-04-29 21:37:04 -0700
commitc3dc64d468997f2841e8f1c9383a8c1c76e56cdc (patch)
tree98decd3c2ae7f22f9e62aa9c54e735509aa48f85
parentbd1f9d8370ada6cc4fb145689a3b42f1d7da28a1 (diff)
downloadbun-c3dc64d468997f2841e8f1c9383a8c1c76e56cdc.tar.gz
bun-c3dc64d468997f2841e8f1c9383a8c1c76e56cdc.tar.zst
bun-c3dc64d468997f2841e8f1c9383a8c1c76e56cdc.zip
Fix a load order issue
-rw-r--r--src/js_ast.zig44
-rw-r--r--src/js_parser.zig18
2 files changed, 55 insertions, 7 deletions
diff --git a/src/js_ast.zig b/src/js_ast.zig
index 410539761..69177d4e6 100644
--- a/src/js_ast.zig
+++ b/src/js_ast.zig
@@ -765,6 +765,44 @@ pub const G = struct {
close_brace_loc: logger.Loc = logger.Loc.Empty,
properties: []Property = &([_]Property{}),
has_decorators: bool = false,
+
+ pub fn canBeMoved(this: *const Class) bool {
+ if (this.extends != null)
+ return false;
+
+ if (this.has_decorators) {
+ return false;
+ }
+
+ for (this.properties) |property| {
+ if (property.kind == .class_static_block)
+ return false;
+
+ const flags = property.flags;
+ if (flags.contains(.is_computed) or flags.contains(.is_spread)) {
+ return false;
+ }
+
+ if (property.kind == .normal) {
+ if (flags.contains(.is_static)) {
+ for ([2]?Expr{ property.value, property.initializer }) |val_| {
+ if (val_) |val| {
+ switch (val.data) {
+ .e_arrow, .e_function => {},
+ else => {
+ if (!val.canBeConstValue()) {
+ return false;
+ }
+ },
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return true;
+ }
};
// invalid shadowing if left as Comment
@@ -5207,15 +5245,15 @@ pub const S = struct {
default_name: LocRef, // value may be a SFunction or SClass
value: StmtOrExpr,
- pub fn canBeMovedAround(self: ExportDefault) bool {
+ pub fn canBeMoved(self: *const ExportDefault) bool {
return switch (self.value) {
.expr => |e| switch (e.data) {
- .e_class => |class| class.extends == null,
+ .e_class => |class| class.canBeMoved(),
.e_arrow, .e_function => true,
else => e.canBeConstValue(),
},
.stmt => |s| switch (s.data) {
- .s_class => |class| class.class.extends == null,
+ .s_class => |class| class.class.canBeMoved(),
.s_function => true,
else => false,
},
diff --git a/src/js_parser.zig b/src/js_parser.zig
index 5933b3e9d..649434690 100644
--- a/src/js_parser.zig
+++ b/src/js_parser.zig
@@ -2988,21 +2988,31 @@ pub const Parser = struct {
// https://github.com/kysely-org/kysely/issues/412
// TODO: this breaks code if they have any static variables or properties which reference anything from the parent scope
// we need to fix it before we merge v0.6.0
- var list = if (!p.options.bundle and class.is_export and class.class.extends == null) &before else &parts;
+ var list = if (!p.options.bundle and class.class.canBeMoved()) &before else &parts;
var sliced = try ListManaged(Stmt).initCapacity(p.allocator, 1);
sliced.items.len = 1;
sliced.items[0] = stmt;
- try p.appendPart(list, sliced.items);
+ try p.appendPart(&parts, sliced.items);
+
+ if (&parts != list) {
+ before.append(parts.getLast()) catch unreachable;
+ parts.items.len -= 1;
+ }
},
.s_export_default => |value| {
// We move export default statements when we can
// This automatically resolves some cyclical import issues in packages like luxon
// https://github.com/oven-sh/bun/issues/1961
- var list = if (!p.options.bundle and value.canBeMovedAround()) &before else &parts;
+ var list = if (!p.options.bundle and value.canBeMoved()) &before else &parts;
var sliced = try ListManaged(Stmt).initCapacity(p.allocator, 1);
sliced.items.len = 1;
sliced.items[0] = stmt;
- try p.appendPart(list, sliced.items);
+ try p.appendPart(&parts, sliced.items);
+
+ if (&parts != list) {
+ before.append(parts.getLast()) catch unreachable;
+ parts.items.len -= 1;
+ }
},
else => {