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

feat(es/minifier): Handle expressions when indexing String, Array and Object types #8750

Open
wants to merge 80 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
e926076
Initial commit: string literals
levi-nz Mar 15, 2024
e7122e1
WIP: array literals
levi-nz Mar 15, 2024
aaecdda
Correctly handle array indexing
levi-nz Mar 16, 2024
6c990d1
Update unit test
levi-nz Mar 17, 2024
0544208
Fix broken index logic
levi-nz Mar 17, 2024
dd4a564
Fix indexing `ObjectLit`s
levi-nz Mar 17, 2024
7916e48
Remove fraction check from `LiteralVisitor`
levi-nz Mar 18, 2024
2075a25
cargo fmt
levi-nz Mar 18, 2024
07f427d
Update test files
levi-nz Mar 18, 2024
ee8f34b
Handle `x['0']`
levi-nz Mar 31, 2024
7cfc984
Don't return `KnownOp::Len` if `obj` is an ObjectLiteral
levi-nz Mar 31, 2024
25fcd45
Handle `x['length']`
levi-nz Mar 31, 2024
ca089fb
Correctly handle builtin properties
levi-nz Mar 31, 2024
823ed75
Don't replace ArrayLiteral if doing so may have side effects
levi-nz Mar 31, 2024
0c8386e
Update unit test
levi-nz Mar 31, 2024
4b4d8ad
Update minifier test files
levi-nz Mar 31, 2024
a0997fb
cargo fmt
levi-nz Mar 31, 2024
c2cd898
Minifier shouldn't handle potential `pristine_globals` cases
levi-nz Apr 1, 2024
da150a2
cargo fmt
levi-nz Apr 1, 2024
e7f6670
Update test files
levi-nz Apr 1, 2024
c1a13e2
Don't add a dummy value if no side effects exist
levi-nz Apr 1, 2024
e50ab82
Update test files
levi-nz Apr 1, 2024
3f86a8f
Oops
levi-nz Apr 1, 2024
82c02aa
Update unit test
levi-nz Apr 1, 2024
a4a08e2
Add test case for side effects
levi-nz Apr 1, 2024
19bf289
Add compression for `MemberExpr`
levi-nz Jun 24, 2024
d96c998
cargo fmt
levi-nz Apr 1, 2024
3acffd1
String does not appear to inherit Function
levi-nz Apr 2, 2024
01d5b38
Array also does not inherit Function
levi-nz Apr 2, 2024
42bc03b
Remove comment
levi-nz Apr 2, 2024
15b42f1
`FUNCTION_SYMBOLS` is no longer required
levi-nz Apr 2, 2024
f59430d
Add unit test files
levi-nz Apr 2, 2024
13c0eed
Update test files
levi-nz Apr 2, 2024
9d1864e
Update test config
levi-nz Apr 3, 2024
f7e608b
Better test dir names
levi-nz Apr 3, 2024
0268ff6
Remove evil clone
levi-nz Apr 3, 2024
8d5ae30
I don't know why this changed, but it did.
levi-nz Apr 3, 2024
af09c01
Add array side effect unit test
levi-nz Apr 3, 2024
167e671
Don't optimize if inside left-hand side of AssignExpr
levi-nz Apr 3, 2024
194249e
Remove redundant import
levi-nz Apr 3, 2024
f82d615
Move call to end
levi-nz Apr 3, 2024
9bbedee
Cleanup iterator call
levi-nz Apr 3, 2024
8a9e2cf
Handle side effects and `__proto__` in object compression
levi-nz Apr 8, 2024
f462bfc
Remove redundant return statement
levi-nz Apr 8, 2024
3d0f354
cargo fmt
levi-nz Apr 8, 2024
7469da7
Remove redundant `Box::new` call
levi-nz Apr 8, 2024
eb0c7ee
Use `preserve_effects`
levi-nz Apr 8, 2024
02032cc
cargo fmt
levi-nz Apr 8, 2024
82e4b3e
Exclude `watch` and `unwatch`
levi-nz Apr 9, 2024
5ee31d3
UPDATE=1 cargo test --test projects --test tsc
levi-nz Apr 9, 2024
d6fa0e7
UPDATE=1 cargo test -p swc_ecma_minifier --features concurrent
levi-nz Apr 9, 2024
a691c25
Remove redundant import
levi-nz Apr 10, 2024
f3a2ff0
Fix `test_fold_array_lit_spread_get_elem`
levi-nz Apr 10, 2024
c2f2e1f
Fix `test_fold_get_elem1`
levi-nz Apr 10, 2024
570bb69
Add `self.changed = true;`
levi-nz Apr 10, 2024
38960d6
Use `ctx`
levi-nz Apr 22, 2024
9eb723f
Fix doc
levi-nz Apr 25, 2024
9be7be7
Handle SeqExpr in `optimize_member_expr`
levi-nz Apr 25, 2024
94d9db5
Remove dup `self.changed = true;`
levi-nz May 10, 2024
65ceb8d
Apply clippy suggestions
levi-nz May 10, 2024
fc27b52
Cleanup `optimize_member_expr`
levi-nz May 12, 2024
95a1784
Fix optimization tests
levi-nz May 12, 2024
e872c02
Fix optimization with `OptCall`
levi-nz May 25, 2024
68a4bb9
Move code into its own `eval` function
levi-nz May 25, 2024
2e96ce6
Fix error
levi-nz May 25, 2024
a7f8c7d
Remove redundant imports
levi-nz May 25, 2024
3e10aa6
Optimize imports
levi-nz May 25, 2024
84dab43
Update parserForStatement9.2.minified.js
levi-nz May 25, 2024
d910b1a
Cleanup simplify
levi-nz May 27, 2024
cefb0c0
Handle `__proto__` object when indexing known object keys
levi-nz May 30, 2024
7c7836e
cargo fmt
levi-nz May 30, 2024
6bc56e9
Remove `is_opt_call`
levi-nz Jun 2, 2024
cc0eabe
Manually fix some files
levi-nz Jun 24, 2024
b88a29d
Manually fix swc_ecma_minifier/Cargo.toml
levi-nz Jun 24, 2024
98f4eba
Update call to `nth_char`
levi-nz Jun 24, 2024
fb62635
Fix remaining `undefined` calls
levi-nz Jun 24, 2024
af081f2
Update unit test file
levi-nz Jun 24, 2024
f5a12fd
Update unit test files
levi-nz Jun 24, 2024
25fad28
Add `report_change` call
levi-nz Jun 24, 2024
6db7b18
`test_issue8747` -> `test_issue_8747`
levi-nz Jun 24, 2024
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//// [destructuringArrayBindingPatternAndAssignment2.ts]
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
var _ref_1 = (_sliced_to_array([][0], 1)[0], _sliced_to_array([][1], 1));
var _ref_1 = (_sliced_to_array(void 0, 1)[0], _sliced_to_array(void 0, 1));
_sliced_to_array(_ref_1[0], 1)[0];
var _undefined = _sliced_to_array(void 0, 2), _undefined_1 = (_sliced_to_array(_undefined[0], 1)[0], _sliced_to_array(_undefined[1], 1));
_sliced_to_array(_undefined_1[0], 1)[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_property_key } from "@swc/helpers/_/_to_property_key";
var trace = [], order = function(n) {
return trace.push(n);
}, tmp = [][0];
}, tmp = void 0;
(void 0 === tmp ? order(0) : tmp)[order(1)];
var tmp1 = {};
(void 0 === tmp1 ? order(0) : tmp1)[order(1)];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ new Class(), d0({
y: new SubClass()
}).y, ({
y: new Class()
}).y, ({}).y;
}).y;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//// [parserForStatement9.ts]
for(var tmp = [][0], x = void 0 === tmp ? ('a' in {}) : tmp; !x; x = !x)console.log(x);
for(var _ref_x = {}.x, x1 = void 0 === _ref_x ? ('a' in {}) : _ref_x; !x1; x1 = !x1)console.log(x1);
for(var tmp = void 0, x = void 0 === tmp ? ('a' in {}) : tmp; !x; x = !x)console.log(x);
for(var _ref_x = void 0, x1 = void 0 === _ref_x ? ('a' in {}) : _ref_x; !x1; x1 = !x1)console.log(x1);
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
//// [templateStringInIndexExpressionES6.ts]
"abc0abc"["0"];
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Emu.D = function _class() {
_class_call_check(this, _class), this._model = 1;
};
//// [second.js]
({}).D._wrapperInstance;
(void 0)._wrapperInstance;
9 changes: 5 additions & 4 deletions crates/swc_ecma_minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ name = "swc_ecma_minifier"
repository = "https://github.com/swc-project/swc.git"
version = "0.197.0"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[lib]
bench = false
Expand Down Expand Up @@ -49,6 +49,7 @@ ryu-js = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tracing = { workspace = true }
phf = { workspace = true }

swc_atoms = { version = "0.6.5", path = "../swc_atoms" }
swc_common = { version = "0.34.0", path = "../swc_common" }
Expand Down Expand Up @@ -80,4 +81,4 @@ testing = { version = "0.36.0", path = "../testing" }

[[bench]]
harness = false
name = "full"
name = "full"
26 changes: 26 additions & 0 deletions crates/swc_ecma_minifier/src/compress/pure/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use radix_fmt::Radix;
use swc_common::{util::take::Take, Spanned, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_utils::{number::ToJsString, ExprExt, IsEmpty, Value};
#[cfg(feature = "debug")]
use {crate::debug::dump, tracing::debug};

use super::Pure;
use crate::compress::util::{eval_as_number, is_pure_undefined_or_null};
Expand Down Expand Up @@ -639,6 +641,30 @@ impl Pure<'_> {
}
}

pub(super) fn eval_member_expr(&mut self, e: &mut Expr) {
let member_expr = match e {
Expr::Member(x) => x,
_ => return,
};

#[cfg(feature = "debug")]
debug!(
"before: optimize_member_expr: {}",
dump(&*member_expr, false)
);

if let Some(replacement) =
self.optimize_member_expr(&mut member_expr.obj, &member_expr.prop)
{
*e = replacement;
self.changed = true;
report_change!("member_expr: Optimized member expression");

levi-nz marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "debug")]
debug!("after: optimize_member_expr: {}", dump(&*e, false));
}
}

fn eval_trivial_two(&mut self, a: &Expr, b: &mut Expr) {
if let Expr::Assign(AssignExpr {
left: a_left,
Expand Down
Loading
Loading