Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ mod test {

#[test]
fn test_convert_to_dotted_properties_quoted_props() {
test_same("({'':0})");
test_same("({'1.0':0})");
test("({'\\u1d17A':0})", "({ ᴗA: 0 })");
// test("({'a\\u0004b':0})");
test_same("v = ({'':0})");
test_same("v = ({'1.0':0})");
test("v = ({'\\u1d17A':0})", "v = ({ ᴗA: 0 })");
// test("v = ({'a\\u0004b':0})");
}

#[test]
Expand Down
16 changes: 8 additions & 8 deletions crates/oxc_minifier/src/peephole/minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,14 +1315,14 @@ mod test {

#[test]
fn test_try_compress_type_of_equal_string() {
test("typeof foo === 'number'", "typeof foo == 'number'");
test("'number' === typeof foo", "typeof foo == 'number'");
test("typeof foo === `number`", "typeof foo == 'number'");
test("`number` === typeof foo", "typeof foo == 'number'");
test("typeof foo !== 'number'", "typeof foo != 'number'");
test("'number' !== typeof foo", "typeof foo != 'number'");
test("typeof foo !== `number`", "typeof foo != 'number'");
test("`number` !== typeof foo", "typeof foo != 'number'");
test("v = typeof foo === 'number'", "v = typeof foo == 'number'");
test("v = 'number' === typeof foo", "v = typeof foo == 'number'");
test("v = typeof foo === `number`", "v = typeof foo == 'number'");
test("v = `number` === typeof foo", "v = typeof foo == 'number'");
test("v = typeof foo !== 'number'", "v = typeof foo != 'number'");
test("v = 'number' !== typeof foo", "v = typeof foo != 'number'");
test("v = typeof foo !== `number`", "v = typeof foo != 'number'");
test("v = `number` !== typeof foo", "v = typeof foo != 'number'");
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_minifier/src/peephole/minimize_not_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ mod test {
#[test]
fn minimize_nots_with_binary_expressions() {
test("!(x === undefined)", "x !== void 0");
test("!(typeof(x) === 'undefined')", "typeof x > 'u'");
test("!(typeof(x) === 'undefined')", "");
test("!(typeof(x()) === 'undefined')", "x() !== void 0");
test("!(x === void 0)", "x !== void 0");
test("!!delete x.y", "delete x.y");
test("!!!delete x.y", "delete x.y");
Expand Down
18 changes: 5 additions & 13 deletions crates/oxc_minifier/src/peephole/remove_unused_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ impl<'a> PeepholeOptimizations {
/// `SimplifyUnusedExpr`: <https://github.com/evanw/esbuild/blob/v0.24.2/internal/js_ast/js_ast_helpers.go#L534>
pub fn remove_unused_expression(&self, e: &mut Expression<'a>, ctx: Ctx<'a, '_>) -> bool {
match e {
Expression::NullLiteral(_)
| Expression::BooleanLiteral(_)
| Expression::NumericLiteral(_)
| Expression::BigIntLiteral(_)
| Expression::StringLiteral(_)
| Expression::ThisExpression(_)
| Expression::RegExpLiteral(_)
| Expression::FunctionExpression(_)
| Expression::ArrowFunctionExpression(_)
| Expression::MetaProperty(_) => true,
Expression::Identifier(ident) => ctx.symbols().has_binding(ident.reference_id()),
Expression::ArrayExpression(_) => Self::fold_array_expression(e, ctx),
Expression::UnaryExpression(_) => self.fold_unary_expression(e, ctx),
Expression::NewExpression(e) => Self::fold_new_constructor(e, ctx),
Expand All @@ -32,10 +21,13 @@ impl<'a> PeepholeOptimizations {
// TODO
// Expression::TemplateLiteral(_)
// | Expression::ObjectExpression(_)
// | Expression::ConditionalExpression(_) => {
// | Expression::ConditionalExpression(_)
// | Expression::BinaryExpression(_)
// | Expression::CallExpression(_)
// | Expression::NewExpression(_) => {
// false
// }
_ => false,
_ => !e.may_have_side_effects(&ctx),
}
}

Expand Down
26 changes: 13 additions & 13 deletions crates/oxc_minifier/src/peephole/replace_known_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1824,19 +1824,19 @@ mod test {

#[test]
fn test_fold_pow() {
test("Math.pow(2, 3)", "2 ** 3");
test("Math.pow(a, 3)", "a ** 3");
test("Math.pow(2, b)", "2 ** b");
test("Math.pow(a, b)", "a ** +b");
test("Math.pow(2n, 3n)", "2n ** +3n"); // errors both before and after
test("Math.pow(a + b, c)", "(a + b) ** +c");
test_same("Math.pow()");
test_same("Math.pow(1)");
test_same("Math.pow(...a, 1)");
test_same("Math.pow(1, ...a)");
test_same("Math.pow(1, 2, 3)");
test_es2015("Math.pow(2, 3)", "Math.pow(2, 3)");
test_same("Unknown.pow(1, 2)");
test("v = Math.pow(2, 3)", "v = 2 ** 3");
test("v = Math.pow(a, 3)", "v = a ** 3");
test("v = Math.pow(2, b)", "v = 2 ** b");
test("v = Math.pow(a, b)", "v = a ** +b");
test("v = Math.pow(2n, 3n)", "v = 2n ** +3n"); // errors both before and after
test("v = Math.pow(a + b, c)", "v = (a + b) ** +c");
test_same("v = Math.pow()");
test_same("v = Math.pow(1)");
test_same("v = Math.pow(...a, 1)");
test_same("v = Math.pow(1, ...a)");
test_same("v = Math.pow(1, 2, 3)");
test_es2015("v = Math.pow(2, 3)", "v = Math.pow(2, 3)");
test_same("v = Unknown.pow(1, 2)");
}

#[test]
Expand Down
56 changes: 28 additions & 28 deletions crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,47 +1517,47 @@ mod test {

#[test]
fn test_fold_is_typeof_equals_undefined_resolved() {
test("var x; typeof x !== 'undefined'", "var x; x !== void 0");
test("var x; typeof x != 'undefined'", "var x; x !== void 0");
test("var x; 'undefined' !== typeof x", "var x; x !== void 0");
test("var x; 'undefined' != typeof x", "var x; x !== void 0");
test("var x; v = typeof x !== 'undefined'", "var x; v = x !== void 0");
test("var x; v = typeof x != 'undefined'", "var x; v = x !== void 0");
test("var x; v = 'undefined' !== typeof x", "var x; v = x !== void 0");
test("var x; v = 'undefined' != typeof x", "var x; v = x !== void 0");

test("var x; typeof x === 'undefined'", "var x; x === void 0");
test("var x; typeof x == 'undefined'", "var x; x === void 0");
test("var x; 'undefined' === typeof x", "var x; x === void 0");
test("var x; 'undefined' == typeof x", "var x; x === void 0");
test("var x; v = typeof x === 'undefined'", "var x; v = x === void 0");
test("var x; v = typeof x == 'undefined'", "var x; v = x === void 0");
test("var x; v = 'undefined' === typeof x", "var x; v = x === void 0");
test("var x; v = 'undefined' == typeof x", "var x; v = x === void 0");

test(
"var x; function foo() { typeof x !== 'undefined' }",
"var x; function foo() { x !== void 0 }",
"var x; function foo() { v = typeof x !== 'undefined' }",
"var x; function foo() { v = x !== void 0 }",
);
test(
"typeof x !== 'undefined'; function foo() { var x }",
"typeof x < 'u'; function foo() { var x }",
"v = typeof x !== 'undefined'; function foo() { var x }",
"v = typeof x < 'u'; function foo() { var x }",
);
test("typeof x !== 'undefined'; { var x }", "x !== void 0; var x;");
test("typeof x !== 'undefined'; { let x }", "typeof x < 'u'; { let x }");
test("typeof x !== 'undefined'; var x", "x !== void 0; var x");
test("v = typeof x !== 'undefined'; { var x }", "v = x !== void 0; var x;");
test("v = typeof x !== 'undefined'; { let x }", "v = typeof x < 'u'; { let x }");
test("v = typeof x !== 'undefined'; var x", "v = x !== void 0; var x");
// input and output both errors with same TDZ error
test("typeof x !== 'undefined'; let x", "x !== void 0; let x");
test("v = typeof x !== 'undefined'; let x", "v = x !== void 0; let x");

test("typeof x.y === 'undefined'", "x.y === void 0");
test("typeof x.y !== 'undefined'", "x.y !== void 0");
test("typeof (x + '') === 'undefined'", "x + '' === void 0");
test("v = typeof x.y === 'undefined'", "v = x.y === void 0");
test("v = typeof x.y !== 'undefined'", "v = x.y !== void 0");
test("v = typeof (x + '') === 'undefined'", "v = x + '' === void 0");
}

/// Port from <https://github.com/evanw/esbuild/blob/v0.24.2/internal/js_parser/js_parser_test.go#L4658>
#[test]
fn test_fold_is_typeof_equals_undefined() {
test("typeof x !== 'undefined'", "typeof x < 'u'");
test("typeof x != 'undefined'", "typeof x < 'u'");
test("'undefined' !== typeof x", "typeof x < 'u'");
test("'undefined' != typeof x", "typeof x < 'u'");

test("typeof x === 'undefined'", "typeof x > 'u'");
test("typeof x == 'undefined'", "typeof x > 'u'");
test("'undefined' === typeof x", "typeof x > 'u'");
test("'undefined' == typeof x", "typeof x > 'u'");
test("v = typeof x !== 'undefined'", "v = typeof x < 'u'");
test("v = typeof x != 'undefined'", "v = typeof x < 'u'");
test("v = 'undefined' !== typeof x", "v = typeof x < 'u'");
test("v = 'undefined' != typeof x", "v = typeof x < 'u'");

test("v = typeof x === 'undefined'", "v = typeof x > 'u'");
test("v = typeof x == 'undefined'", "v = typeof x > 'u'");
test("v = 'undefined' === typeof x", "v = typeof x > 'u'");
test("v = 'undefined' == typeof x", "v = typeof x > 'u'");
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Original | minified | minified | gzip | gzip | Fixture

1.01 MB | 440.17 kB | 458.89 kB | 122.38 kB | 126.71 kB | bundle.min.js

1.25 MB | 647.03 kB | 646.76 kB | 160.30 kB | 163.73 kB | three.js
1.25 MB | 647.02 kB | 646.76 kB | 160.29 kB | 163.73 kB | three.js

2.14 MB | 716.14 kB | 724.14 kB | 161.79 kB | 181.07 kB | victory.js

Expand Down
Loading