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(minifier): fold typeof foo == undefined into foo == undefined when possible #8160

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 @@ -239,8 +239,10 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
}
}

/// Compress `typeof foo == "undefined"` into `typeof foo > "u"`
/// Compress `typeof foo == "undefined"`
///
/// - `typeof foo == "undefined"` (if foo is resolved) -> `foo === undefined`
/// - `typeof foo != "undefined"` (if foo is resolved) -> `foo !== undefined`
/// - `typeof foo == "undefined"` -> `typeof foo > "u"`
/// - `typeof foo != "undefined"` -> `typeof foo < "u"`
///
Expand All @@ -249,12 +251,12 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
expr: &mut BinaryExpression<'a>,
ctx: Ctx<'a, 'b>,
) -> Option<Expression<'a>> {
let new_op = match expr.operator {
let (new_eq_op, new_comp_op) = match expr.operator {
BinaryOperator::Equality | BinaryOperator::StrictEquality => {
BinaryOperator::GreaterThan
(BinaryOperator::StrictEquality, BinaryOperator::GreaterThan)
}
BinaryOperator::Inequality | BinaryOperator::StrictInequality => {
BinaryOperator::LessThan
(BinaryOperator::StrictInequality, BinaryOperator::LessThan)
}
_ => return None,
};
Expand All @@ -273,10 +275,17 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
},
);
let (_void_exp, id_ref) = pair?;
let argument = Expression::Identifier(ctx.alloc(id_ref));
let left = ctx.ast.expression_unary(SPAN, UnaryOperator::Typeof, argument);
let right = ctx.ast.expression_string_literal(SPAN, "u", None);
Some(ctx.ast.expression_binary(expr.span, left, new_op, right))
let is_resolved = ctx.scopes().find_binding(ctx.current_scope_id(), &id_ref.name).is_some();
if is_resolved {
let left = Expression::Identifier(ctx.alloc(id_ref));
let right = ctx.ast.void_0(SPAN);
Some(ctx.ast.expression_binary(expr.span, left, new_eq_op, right))
} else {
let argument = Expression::Identifier(ctx.alloc(id_ref));
let left = ctx.ast.expression_unary(SPAN, UnaryOperator::Typeof, argument);
let right = ctx.ast.expression_string_literal(SPAN, "u", None);
Some(ctx.ast.expression_binary(expr.span, left, new_comp_op, right))
}
}

/// Compress `foo === null || foo === undefined` into `foo == null`.
Expand Down Expand Up @@ -1153,6 +1162,33 @@ mod test {
test_same("const foo = () => { foo; return 'baz' }");
}

#[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; 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; function foo() { typeof x !== 'undefined' }",
"var x; function foo() { x !== void 0 }",
);
test(
"typeof x !== 'undefined'; function foo() { var x }",
"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");
// input and output both errors with same TDZ error
test("typeof x !== 'undefined'; let x", "x !== void 0; let x");
}

/// 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() {
Expand Down
8 changes: 4 additions & 4 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Original | minified | minified | gzip | gzip | Fixture

173.90 kB | 60.16 kB | 59.82 kB | 19.49 kB | 19.33 kB | moment.js

287.63 kB | 90.59 kB | 90.07 kB | 32.19 kB | 31.95 kB | jquery.js
287.63 kB | 90.59 kB | 90.07 kB | 32.18 kB | 31.95 kB | jquery.js

342.15 kB | 118.61 kB | 118.14 kB | 44.54 kB | 44.37 kB | vue.js

Expand All @@ -17,11 +17,11 @@ Original | minified | minified | gzip | gzip | Fixture

1.25 MB | 656.66 kB | 646.76 kB | 164.14 kB | 163.73 kB | three.js

2.14 MB | 735.28 kB | 724.14 kB | 180.98 kB | 181.07 kB | victory.js
2.14 MB | 735.26 kB | 724.14 kB | 180.97 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 332.23 kB | 331.56 kB | echarts.js

6.69 MB | 2.36 MB | 2.31 MB | 494.93 kB | 488.28 kB | antd.js
6.69 MB | 2.36 MB | 2.31 MB | 494.89 kB | 488.28 kB | antd.js

10.95 MB | 3.51 MB | 3.49 MB | 910.92 kB | 915.50 kB | typescript.js
10.95 MB | 3.51 MB | 3.49 MB | 910.90 kB | 915.50 kB | typescript.js

Loading