Skip to content

Commit

Permalink
ensure super() is not merged with adjacent statements (#2757)
Browse files Browse the repository at this point in the history
* ensure `super()` is not merged with adjacent statements

* add TODO
  • Loading branch information
dylan-conway authored Apr 27, 2023
1 parent 6c6118e commit 75e3546
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/js_ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2511,6 +2511,10 @@ pub const Stmt = struct {
return @as(Stmt.Tag, self.data) == .s_type_script;
}

pub fn isSuperCall(self: Stmt) bool {
return self.data == .s_expr and self.data.s_expr.value.data == .e_call and self.data.s_expr.value.data.e_call.target.data == .e_super;
}

pub fn empty() Stmt {
return Stmt{ .data = .{ .s_empty = None }, .loc = logger.Loc{} };
}
Expand Down
19 changes: 14 additions & 5 deletions src/js_parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19154,6 +19154,9 @@ fn NewParser_(

constructor_function.?.func.body.stmts = constructor_stmts.items;
}

// TODO: make sure "super()" comes before instance field initializers
// https://github.com/evanw/esbuild/blob/e9413cc4f7ab87263ea244a999c6fa1f1e34dc65/internal/js_parser/js_parser_lower.go#L2742
}

var stmts_count: usize = 1 + static_members.items.len + instance_decorators.items.len + static_decorators.items.len;
Expand Down Expand Up @@ -20128,6 +20131,12 @@ fn NewParser_(
break;
}

// don't merge super calls to ensure they are called before "this" is accessed
if (stmt.isSuperCall()) {
output.append(stmt) catch unreachable;
continue;
}

switch (stmt.data) {
.s_empty => continue,

Expand All @@ -20151,7 +20160,7 @@ fn NewParser_(
// Merge adjacent expression statements
if (output.items.len > 0) {
var prev_stmt = &output.items[output.items.len - 1];
if (prev_stmt.data == .s_expr) {
if (prev_stmt.data == .s_expr and !prev_stmt.isSuperCall()) {
prev_stmt.data.s_expr.does_not_affect_tree_shaking = prev_stmt.data.s_expr.does_not_affect_tree_shaking and
s_expr.does_not_affect_tree_shaking;
prev_stmt.data.s_expr.value = prev_stmt.data.s_expr.value.joinWithComma(
Expand All @@ -20166,7 +20175,7 @@ fn NewParser_(
// Absorb a previous expression statement
if (output.items.len > 0) {
var prev_stmt = &output.items[output.items.len - 1];
if (prev_stmt.data == .s_expr) {
if (prev_stmt.data == .s_expr and !prev_stmt.isSuperCall()) {
s_switch.test_ = prev_stmt.data.s_expr.value.joinWithComma(s_switch.test_, p.allocator);
output.items.len -= 1;
}
Expand All @@ -20176,7 +20185,7 @@ fn NewParser_(
// Absorb a previous expression statement
if (output.items.len > 0) {
var prev_stmt = &output.items[output.items.len - 1];
if (prev_stmt.data == .s_expr) {
if (prev_stmt.data == .s_expr and !prev_stmt.isSuperCall()) {
s_if.test_ = prev_stmt.data.s_expr.value.joinWithComma(s_if.test_, p.allocator);
output.items.len -= 1;
}
Expand All @@ -20189,7 +20198,7 @@ fn NewParser_(
// Merge return statements with the previous expression statement
if (output.items.len > 0 and ret.value != null) {
var prev_stmt = &output.items[output.items.len - 1];
if (prev_stmt.data == .s_expr) {
if (prev_stmt.data == .s_expr and !prev_stmt.isSuperCall()) {
ret.value = prev_stmt.data.s_expr.value.joinWithComma(ret.value.?, p.allocator);
prev_stmt.* = stmt;
continue;
Expand All @@ -20207,7 +20216,7 @@ fn NewParser_(
// Merge throw statements with the previous expression statement
if (output.items.len > 0) {
var prev_stmt = &output.items[output.items.len - 1];
if (prev_stmt.data == .s_expr) {
if (prev_stmt.data == .s_expr and !prev_stmt.isSuperCall()) {
prev_stmt.* = p.s(S.Throw{
.value = prev_stmt.data.s_expr.value.joinWithComma(
stmt.data.s_throw.value,
Expand Down

0 comments on commit 75e3546

Please sign in to comment.