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): Allow expr_simplifier to do arithmetic with string literals #8683

Merged
merged 7 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -896,8 +896,9 @@ impl SimplifyExpr {
);

if (lv.is_unknown() && rv.is_unknown())
|| !left.get_type().casted_to_number_on_add()
|| !right.get_type().casted_to_number_on_add()
|| op == op!(bin, "+")
&& (!left.get_type().casted_to_number_on_add()
|| !right.get_type().casted_to_number_on_add())
{
return Unknown;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,27 @@ fn test_fold_literals_as_numbers() {
fold("x/false", "x/0"); // should we add an error check? :)
}

#[test]
fn test_fold_arithmetic_with_strings() {
// Left side of expression is a string
fold("'10' - 5", "5");
fold("'4' / 2", "2");
fold("'11' % 2", "1");
fold("'10' ** 2", "100");

// Right side of expression is a string
fold("10 - '5'", "5");
fold("4 / '2'", "2");
fold("11 % '2'", "1");
fold("10 ** '2'", "100");

// Both sides are strings
fold("'10' - '5'", "5");
fold("'4' / '2'", "2");
fold("'11' % '2'", "1");
fold("'10' ** '2'", "100");
}

#[test]
fn test_not_fold_back_to_true_false() {
fold("!0", "!0");
Expand Down