Skip to content

Commit 47ce190

Browse files
authored
Turbopack's analyzer: always treat promises as truth-y and non-nullish (vercel#94186)
Promises are not included in the list of JavaScript's falsy values, therefore, they are always truthy. See https://developer.mozilla.org/en-US/docs/Glossary/Truthy. Additionally, promises are neither `null` or `undefined` so they are not nullish: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing. This PR updates Turbopack's analyzer to reflect this + adds unit tests for `is_truthy` and `is_nullish`.
1 parent 5ee7005 commit 47ce190

1 file changed

Lines changed: 43 additions & 1 deletion

File tree

  • turbopack/crates/turbopack-ecmascript/src/analyzer

turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2430,6 +2430,7 @@ impl JsValue {
24302430
JsValue::Url(..)
24312431
| JsValue::Array { .. }
24322432
| JsValue::Object { .. }
2433+
| JsValue::Promise(..)
24332434
| JsValue::WellKnownObject(..)
24342435
| JsValue::WellKnownFunction(..)
24352436
| JsValue::Function(..) => Some(true),
@@ -2515,6 +2516,7 @@ impl JsValue {
25152516
| JsValue::WellKnownFunction(..)
25162517
| JsValue::Not(..)
25172518
| JsValue::Binary(..)
2519+
| JsValue::Promise(..)
25182520
| JsValue::Function(..) => Some(false),
25192521
JsValue::Alternatives {
25202522
total_nodes: _,
@@ -4017,7 +4019,7 @@ mod tests {
40174019
};
40184020

40194021
use super::{
4020-
JsValue,
4022+
ConstantValue, JsValue,
40214023
graph::{ConditionalKind, Effect, EffectArg, EvalContext, VarGraph, create_graph},
40224024
linker::link,
40234025
};
@@ -4686,4 +4688,44 @@ mod tests {
46864688
input
46874689
);
46884690
}
4691+
4692+
#[rstest]
4693+
#[case(JsValue::from(1.0))]
4694+
#[case(JsValue::from("hi"))]
4695+
#[case(ConstantValue::True.into())]
4696+
#[case(JsValue::promise(ConstantValue::Null.into()))]
4697+
fn is_truthy_positive(#[case] v: JsValue) {
4698+
assert_eq!(v.is_truthy(), Some(true), "expected '{v}' to be truthy");
4699+
}
4700+
4701+
#[rstest]
4702+
#[case(JsValue::from(0.0))]
4703+
#[case(JsValue::from(""))]
4704+
#[case(ConstantValue::False.into())]
4705+
#[case(ConstantValue::Null.into())]
4706+
#[case(ConstantValue::Undefined.into())]
4707+
fn is_truthy_negative(#[case] v: JsValue) {
4708+
assert_eq!(v.is_truthy(), Some(false), "expected '{v}' to be falsy");
4709+
}
4710+
4711+
#[rstest]
4712+
#[case(ConstantValue::Null.into())]
4713+
#[case(ConstantValue::Undefined.into())]
4714+
fn is_nullish_positive(#[case] v: JsValue) {
4715+
assert_eq!(v.is_nullish(), Some(true), "expected '{v}' to be nullish");
4716+
}
4717+
4718+
#[rstest]
4719+
#[case(JsValue::from(0.0))]
4720+
#[case(JsValue::from(""))]
4721+
#[case(JsValue::from("hi"))]
4722+
#[case(ConstantValue::True.into())]
4723+
#[case(JsValue::promise(ConstantValue::Null.into()))]
4724+
fn is_nullish_negative(#[case] v: JsValue) {
4725+
assert_eq!(
4726+
v.is_nullish(),
4727+
Some(false),
4728+
"expected '{v}' not to be nullish"
4729+
);
4730+
}
46894731
}

0 commit comments

Comments
 (0)