aboutsummaryrefslogtreecommitdiff
path: root/src/js_parser.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-05-15 02:36:43 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-05-15 02:36:43 -0700
commit4d751db27a65c800c1c4cd6e6a241e360a9faa54 (patch)
treeddda375657b963ef5b9c4a1f980d5bb42ee164a7 /src/js_parser.zig
parent4ec2ed4f6c62aa713c518cf3a5c44a6687802f5a (diff)
downloadbun-4d751db27a65c800c1c4cd6e6a241e360a9faa54.tar.gz
bun-4d751db27a65c800c1c4cd6e6a241e360a9faa54.tar.zst
bun-4d751db27a65c800c1c4cd6e6a241e360a9faa54.zip
More DCE
Diffstat (limited to 'src/js_parser.zig')
-rw-r--r--src/js_parser.zig61
1 files changed, 34 insertions, 27 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig
index 4cab7ffdc..f78e5b160 100644
--- a/src/js_parser.zig
+++ b/src/js_parser.zig
@@ -1634,29 +1634,25 @@ pub const SideEffects = enum(u1) {
},
.e_object => {
- // Arrays with "..." spread expressions can't be unwrapped because the
- // "..." triggers code evaluation via iterators. In that case, just trim
- // the other items instead and leave the array expression there.
-
+ // Objects with "..." spread expressions can't be unwrapped because the
+ // "..." triggers code evaluation via getters. In that case, just trim
+ // the other items instead and leave the object expression there.
var properties_slice = expr.data.e_object.properties.slice();
var end: usize = 0;
- var any_computed = false;
for (properties_slice) |spread| {
end = 0;
- any_computed = any_computed or spread.flags.contains(.is_computed);
if (spread.kind == .spread) {
// Spread properties must always be evaluated
for (properties_slice) |prop_| {
var prop = prop_;
if (prop_.kind != .spread) {
- if (prop.value != null) {
- if (simpifyUnusedExpr(p, prop.value.?)) |value| {
- prop.value = value;
- } else if (!prop.flags.contains(.is_computed)) {
- continue;
- } else {
- prop.value = p.newExpr(E.Number{ .value = 0.0 }, prop.value.?.loc);
- }
+ var value = simpifyUnusedExpr(p, prop.value.?);
+ if (value != null) {
+ prop.value = value;
+ } else if (!prop.flags.contains(.is_computed)) {
+ continue;
+ } else {
+ prop.value = p.newExpr(E.Number{ .value = 0.0 }, prop.value.?.loc);
}
}
@@ -1670,21 +1666,32 @@ pub const SideEffects = enum(u1) {
}
}
- if (any_computed) {
- // Otherwise, the object can be completely removed. We only need to keep any
- // object properties with side effects. Apply this simplification recursively.
- // for (properties_slice) |prop| {
- // if (prop.flags.is_computed) {
- // // Make sure "ToString" is still evaluated on the key
+ var result = Expr.init(E.Missing, E.Missing{}, expr.loc);
- // }
- // }
-
- // keep this for now because we need better test coverage to do this correctly
- return expr;
+ // Otherwise, the object can be completely removed. We only need to keep any
+ // object properties with side effects. Apply this simplification recursively.
+ for (properties_slice) |prop| {
+ if (prop.flags.contains(.is_computed)) {
+ // Make sure "ToString" is still evaluated on the key
+ result = result.joinWithComma(
+ p.newExpr(
+ E.Binary{
+ .op = .bin_add,
+ .left = prop.key.?,
+ .right = p.newExpr(E.String{}, prop.key.?.loc),
+ },
+ prop.key.?.loc,
+ ),
+ p.allocator,
+ );
+ }
+ result = result.joinWithComma(
+ simpifyUnusedExpr(p, prop.value.?) orelse prop.value.?.toEmpty(),
+ p.allocator,
+ );
}
- return null;
+ return result;
},
.e_array => {
var items = expr.data.e_array.items.slice();
@@ -16879,7 +16886,7 @@ fn NewParser_(
for (ex.properties.slice()) |*property| {
// The key must still be evaluated if it's computed or a spread
- if (property.kind == .spread or property.flags.contains(.is_computed) or property.flags.contains(.is_spread)) {
+ if (property.kind == .spread or (property.flags.contains(.is_computed) and !property.key.?.isPrimitiveLiteral()) or property.flags.contains(.is_spread)) {
return false;
}