Skip to content

Commit 1275418

Browse files
committed
feat(minifier): new Date() has ValueType::Object
1 parent e28c491 commit 1275418

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

crates/oxc_ecmascript/src/constant_evaluation/value_type.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use oxc_ast::ast::{
2-
AssignmentExpression, AssignmentOperator, BinaryExpression, ConditionalExpression, Expression,
3-
LogicalExpression, LogicalOperator, StaticMemberExpression, UnaryExpression,
4-
};
1+
use oxc_ast::ast::*;
52
use oxc_syntax::operator::{BinaryOperator, UnaryOperator};
63

74
use crate::{
@@ -111,6 +108,7 @@ impl<'a> DetermineValueType<'a> for Expression<'a> {
111108
Expression::LogicalExpression(e) => e.value_type(is_global_reference),
112109
Expression::ParenthesizedExpression(e) => e.expression.value_type(is_global_reference),
113110
Expression::StaticMemberExpression(e) => e.value_type(is_global_reference),
111+
Expression::NewExpression(e) => e.value_type(is_global_reference),
114112
_ => ValueType::Undetermined,
115113
}
116114
}
@@ -288,3 +286,16 @@ impl<'a> DetermineValueType<'a> for StaticMemberExpression<'a> {
288286
ValueType::Undetermined
289287
}
290288
}
289+
290+
impl<'a> DetermineValueType<'a> for NewExpression<'a> {
291+
fn value_type(&self, is_global_reference: &impl IsGlobalReference<'a>) -> ValueType {
292+
if let Some(ident) = self.callee.get_identifier_reference() {
293+
if ident.name.as_str() == "Date"
294+
&& is_global_reference.is_global_reference(ident) == Some(true)
295+
{
296+
return ValueType::Object;
297+
}
298+
}
299+
ValueType::Undetermined
300+
}
301+
}

crates/oxc_minifier/src/peephole/fold_constants.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,19 +1209,20 @@ mod test {
12091209
}
12101210

12111211
#[test]
1212-
#[ignore]
12131212
fn test_object_comparison1() {
1214-
fold("!new Date()", "false");
1215-
fold("!!new Date()", "true");
1216-
1217-
fold("new Date() == null", "false");
1218-
fold("new Date() == undefined", "false");
1219-
fold("new Date() != null", "true");
1220-
fold("new Date() != undefined", "true");
1221-
fold("null == new Date()", "false");
1222-
fold("undefined == new Date()", "false");
1223-
fold("null != new Date()", "true");
1224-
fold("undefined != new Date()", "true");
1213+
fold("!new Date()", "!1");
1214+
fold("!!new Date()", "!0");
1215+
fold_same("!new Date(foo)");
1216+
1217+
fold("new Date() == null", "!1");
1218+
fold("new Date() == undefined", "!1");
1219+
fold("new Date() != null", "!0");
1220+
fold("new Date() != undefined", "!0");
1221+
fold("null == new Date()", "!1");
1222+
fold("undefined == new Date()", "!1");
1223+
fold("null != new Date()", "!0");
1224+
fold("undefined != new Date()", "!0");
1225+
fold("new Date(foo) != undefined", "new Date(foo) != null");
12251226
}
12261227

12271228
#[test]

0 commit comments

Comments
 (0)