Skip to content

Commit

Permalink
fix(minifier): do not compare bigint with object (#7294)
Browse files Browse the repository at this point in the history
@Boshen, could you please update the snap of runtime and commit to this PR? I want to see the effects after the unit tests are added.
  • Loading branch information
7086cmd committed Nov 16, 2024
1 parent 20d9080 commit cf99be0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
11 changes: 9 additions & 2 deletions crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,14 @@ impl<'a, 'b> PeepholeFoldConstants {
}
}

if matches!(left, ValueType::String | ValueType::Number)
if matches!(left, ValueType::String | ValueType::Number | ValueType::BigInt)
&& matches!(right, ValueType::Object)
{
return None;
}

if matches!(left, ValueType::Object)
&& matches!(right, ValueType::String | ValueType::Number)
&& matches!(right, ValueType::String | ValueType::Number | ValueType::BigInt)
{
return None;
}
Expand Down Expand Up @@ -873,6 +873,13 @@ mod test {
test("'1' !== 1n", "true");
}

#[test]
fn test_object_bigint_comparison() {
test_same("{ valueOf: function() { return 0n; } } != 0n");
test_same("0n != { valueOf: function() { return 0n; } }");
test_same("0n != { toString: function() { return '0'; } }");
}

#[test]
fn test_nan_comparison() {
test("NaN < 1", "false");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{node_util::Ctx, CompressorPass};
pub struct PeepholeSubstituteAlternateSyntax {
/// Do not compress syntaxes that are hard to analyze inside the fixed loop.
/// e.g. Do not compress `undefined -> void 0`, `true` -> `!0`.
/// Opposite of `late` in Closure Compier.
/// Opposite of `late` in Closure Compiler.
in_fixed_loop: bool,

// states
Expand Down
8 changes: 1 addition & 7 deletions tasks/coverage/snapshots/runtime.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ commit: 06454619

runtime Summary:
AST Parsed : 18055/18055 (100.00%)
Positive Passed: 17796/18055 (98.57%)
Positive Passed: 17798/18055 (98.58%)
tasks/coverage/test262/test/language/arguments-object/async-gen-meth-args-trailing-comma-multiple.js
transform error: Test262Error: Expected SameValue0», «2») to be true

Expand Down Expand Up @@ -447,12 +447,6 @@ transform error: Test262Error: Expected a TypeError to be thrown but no exceptio
tasks/coverage/test262/test/language/expressions/class/heritage-async-arrow-function.js
transform error: Test262Error: Expected a TypeError to be thrown but no exception was thrown at all

tasks/coverage/test262/test/language/expressions/does-not-equals/bigint-and-object.js
minify error: Test262Error: The result of (0n != {valueOf: function() {return 0n;}}) is false Expected SameValuetrue», «false») to be true

tasks/coverage/test262/test/language/expressions/equals/bigint-and-object.js
minify error: Test262Error: The result of (0n == {valueOf: function() {return 0n;}}) is true Expected SameValuefalse», «true») to be true

tasks/coverage/test262/test/language/expressions/exponentiation/bigint-negative-exponent-throws.js
transform error: Test262Error: (-1n) ** -1n throws RangeError Expected a RangeError but got a TypeError

Expand Down

0 comments on commit cf99be0

Please sign in to comment.