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
46 changes: 44 additions & 2 deletions crates/oxc_ecmascript/src/constant_evaluation/value_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use oxc_ast::ast::{BinaryExpression, ConditionalExpression, Expression, LogicalExpression};
use oxc_ast::ast::{
AssignmentExpression, AssignmentOperator, BinaryExpression, ConditionalExpression, Expression,
LogicalExpression,
};
use oxc_syntax::operator::{BinaryOperator, UnaryOperator};

/// JavaScript Language Type
Expand Down Expand Up @@ -97,7 +100,7 @@ impl<'a> From<&Expression<'a>> for ValueType {
Expression::SequenceExpression(e) => {
e.expressions.last().map_or(ValueType::Undetermined, Self::from)
}
Expression::AssignmentExpression(e) => Self::from(&e.right),
Expression::AssignmentExpression(e) => Self::from(&**e),
Expression::ConditionalExpression(e) => Self::from(&**e),
Expression::LogicalExpression(e) => Self::from(&**e),
_ => Self::Undetermined,
Expand Down Expand Up @@ -167,6 +170,45 @@ impl<'a> From<&BinaryExpression<'a>> for ValueType {
}
}

impl<'a> From<&AssignmentExpression<'a>> for ValueType {
fn from(e: &AssignmentExpression<'a>) -> Self {
match e.operator {
AssignmentOperator::Assign => Self::from(&e.right),
AssignmentOperator::Addition => {
let right = Self::from(&e.right);
if right.is_string() {
Self::String
} else {
Self::Undetermined
}
}
AssignmentOperator::Subtraction
| AssignmentOperator::Multiplication
| AssignmentOperator::Division
| AssignmentOperator::Remainder
| AssignmentOperator::ShiftLeft
| AssignmentOperator::BitwiseOR
| AssignmentOperator::ShiftRight
| AssignmentOperator::BitwiseXOR
| AssignmentOperator::BitwiseAnd
| AssignmentOperator::Exponential => {
let right = Self::from(&e.right);
if right.is_bigint() {
Self::BigInt
} else if !(right.is_object() || right.is_undetermined()) {
Self::Number
} else {
Self::Undetermined
}
}
AssignmentOperator::ShiftRightZeroFill => Self::Number,
AssignmentOperator::LogicalAnd
| AssignmentOperator::LogicalOr
| AssignmentOperator::LogicalNullish => Self::Undetermined,
}
}
}

impl<'a> From<&ConditionalExpression<'a>> for ValueType {
fn from(e: &ConditionalExpression<'a>) -> Self {
let left = Self::from(&e.consequent);
Expand Down
28 changes: 26 additions & 2 deletions crates/oxc_minifier/tests/ecmascript/value_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,32 @@ fn assignment_tests() {
test("a = 1n", ValueType::BigInt);
test("a = foo", ValueType::Undetermined);

// test("a += 1", ValueType::Undetermined);
// test("a += 1n", ValueType::Undetermined);
test("a += ''", ValueType::String);
// if `a` is a string, the result is string
// if `a` is an object, the result can be string or number or bigint
// if `a` is not a string nor an object, the result can be number or bigint
test("a += 1", ValueType::Undetermined);
test("a += 1n", ValueType::Undetermined);

test("a -= 1", ValueType::Number); // an error is thrown if `a` is converted to bigint
test("a -= undefined", ValueType::Number);
test("a -= null", ValueType::Number);
test("a -= ''", ValueType::Number);
test("a -= true", ValueType::Number);
test("a -= 1n", ValueType::BigInt); // an error is thrown if `a` is converted to number
test("a -= {}", ValueType::Undetermined); // number or bigint
test("a *= 1", ValueType::Number);
test("a /= 1", ValueType::Number);
test("a %= 1", ValueType::Number);
test("a <<= 1", ValueType::Number);
test("a |= 1", ValueType::Number);
test("a >>= 1", ValueType::Number);
test("a ^= 1", ValueType::Number);
test("a &= 1", ValueType::Number);
test("a **= 1", ValueType::Number);

test("a >>>= 1", ValueType::Number);
test("a >>>= 1n", ValueType::Number); // throws an error
}

#[test]
Expand Down
Loading