Unverified Commit 747466ca authored by Sam Poder's avatar Sam Poder Committed by GitHub

[turbopack] Separate `JsValue` into it's own folder in the analyzer (#94208)

This is the first in a stack of PRs to break up the analyzer and,
eventually, move it to its own crate. There shouldn't be any code
changes - everything was just copy and pasted into separate files (let
me know if this split makes sense).

---------
Co-authored-by: 's avatarvercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
parent 8216e23a
use std::{
borrow::Cow,
fmt::{Display, Formatter},
hash::{Hash, Hasher},
sync::Arc,
};
use num_bigint::BigInt;
use num_traits::Zero;
use swc_core::{
atoms::Wtf8Atom,
ecma::{ast::Lit, atoms::Atom},
};
use turbo_rcstr::RcStr;
use turbopack_core::compile_time_info::TotalOrderF64;
use crate::{
analyzer::{JsValue, imports::ImportAnnotations},
utils::StringifyJs,
};
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum ObjectPart {
KeyValue(JsValue, JsValue),
Spread(JsValue),
}
impl Default for ObjectPart {
fn default() -> Self {
ObjectPart::Spread(Default::default())
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct ConstantNumber(pub TotalOrderF64);
impl ConstantNumber {
pub fn as_u32_index(&self) -> Option<usize> {
let index: u32 = *self.0 as u32;
(index as f64 == *self.0).then_some(index as usize)
}
}
impl From<f64> for ConstantNumber {
fn from(value: f64) -> Self {
ConstantNumber(value.into())
}
}
#[derive(Debug, Clone)]
pub enum ConstantString {
Atom(Atom),
RcStr(RcStr),
}
impl ConstantString {
pub fn as_str(&self) -> &str {
match self {
Self::Atom(s) => s,
Self::RcStr(s) => s,
}
}
pub fn as_rcstr(&self) -> RcStr {
match self {
Self::Atom(s) => RcStr::from(s.as_str()),
Self::RcStr(s) => s.clone(),
}
}
pub fn as_atom(&self) -> Cow<'_, Atom> {
match self {
Self::Atom(s) => Cow::Borrowed(s),
Self::RcStr(s) => Cow::Owned(s.as_str().into()),
}
}
pub fn is_empty(&self) -> bool {
self.as_str().is_empty()
}
}
impl PartialEq for ConstantString {
fn eq(&self, other: &Self) -> bool {
self.as_str() == other.as_str()
}
}
impl Eq for ConstantString {}
impl Hash for ConstantString {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_str().hash(state);
}
}
impl Display for ConstantString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
impl From<Atom> for ConstantString {
fn from(v: Atom) -> Self {
ConstantString::Atom(v)
}
}
impl From<&'static str> for ConstantString {
fn from(v: &'static str) -> Self {
ConstantString::Atom(v.into())
}
}
impl From<String> for ConstantString {
fn from(v: String) -> Self {
ConstantString::Atom(v.into())
}
}
impl From<RcStr> for ConstantString {
fn from(v: RcStr) -> Self {
ConstantString::RcStr(v)
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
pub enum ConstantValue {
#[default]
Undefined,
Str(ConstantString),
Num(ConstantNumber),
True,
False,
Null,
BigInt(Box<BigInt>),
Regex(Box<(Atom, Atom)>),
}
impl ConstantValue {
pub fn as_str(&self) -> Option<&str> {
match self {
Self::Str(s) => Some(s.as_str()),
_ => None,
}
}
pub fn as_bool(&self) -> Option<bool> {
match self {
Self::True => Some(true),
Self::False => Some(false),
_ => None,
}
}
pub fn is_truthy(&self) -> bool {
match self {
Self::Undefined | Self::False | Self::Null => false,
Self::True | Self::Regex(..) => true,
Self::Str(s) => !s.is_empty(),
Self::Num(ConstantNumber(n)) => **n != 0.0,
Self::BigInt(n) => !n.is_zero(),
}
}
pub fn is_nullish(&self) -> bool {
match self {
Self::Undefined | Self::Null => true,
Self::Str(..)
| Self::Num(..)
| Self::True
| Self::False
| Self::BigInt(..)
| Self::Regex(..) => false,
}
}
pub fn is_empty_string(&self) -> bool {
match self {
Self::Str(s) => s.is_empty(),
_ => false,
}
}
pub fn is_value_type(&self) -> bool {
!matches!(self, Self::Regex(..))
}
}
impl From<bool> for ConstantValue {
fn from(v: bool) -> Self {
match v {
true => ConstantValue::True,
false => ConstantValue::False,
}
}
}
impl From<&'_ str> for ConstantValue {
fn from(v: &str) -> Self {
ConstantValue::Str(ConstantString::Atom(v.into()))
}
}
impl From<Lit> for ConstantValue {
fn from(v: Lit) -> Self {
match v {
Lit::Str(v) => {
ConstantValue::Str(ConstantString::Atom(v.value.to_atom_lossy().into_owned()))
}
Lit::Bool(v) => {
if v.value {
ConstantValue::True
} else {
ConstantValue::False
}
}
Lit::Null(_) => ConstantValue::Null,
Lit::Num(v) => ConstantValue::Num(ConstantNumber(v.value.into())),
Lit::BigInt(v) => ConstantValue::BigInt(v.value),
Lit::Regex(v) => ConstantValue::Regex(Box::new((v.exp, v.flags))),
Lit::JSXText(v) => ConstantValue::Str(ConstantString::Atom(v.value)),
}
}
}
impl Display for ConstantValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ConstantValue::Undefined => write!(f, "undefined"),
ConstantValue::Str(str) => write!(f, "{}", StringifyJs(str.as_str())),
ConstantValue::True => write!(f, "true"),
ConstantValue::False => write!(f, "false"),
ConstantValue::Null => write!(f, "null"),
ConstantValue::Num(ConstantNumber(n)) => write!(f, "{n}"),
ConstantValue::BigInt(n) => write!(f, "{n}"),
ConstantValue::Regex(regex) => write!(f, "/{}/{}", regex.0, regex.1),
}
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct ModuleValue {
pub module: Wtf8Atom,
pub annotations: Option<Arc<ImportAnnotations>>,
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum LogicalOperator {
And,
Or,
NullishCoalescing,
}
impl LogicalOperator {
pub(super) fn joiner(&self) -> &'static str {
match self {
LogicalOperator::And => " && ",
LogicalOperator::Or => " || ",
LogicalOperator::NullishCoalescing => " ?? ",
}
}
pub(super) fn multi_line_joiner(&self) -> &'static str {
match self {
LogicalOperator::And => "&& ",
LogicalOperator::Or => "|| ",
LogicalOperator::NullishCoalescing => "?? ",
}
}
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum BinaryOperator {
Equal,
NotEqual,
StrictEqual,
StrictNotEqual,
}
impl BinaryOperator {
pub(super) fn joiner(&self) -> &'static str {
match self {
BinaryOperator::Equal => " == ",
BinaryOperator::NotEqual => " != ",
BinaryOperator::StrictEqual => " === ",
BinaryOperator::StrictNotEqual => " !== ",
}
}
pub(super) fn positive_op(&self) -> (PositiveBinaryOperator, bool) {
match self {
BinaryOperator::Equal => (PositiveBinaryOperator::Equal, false),
BinaryOperator::NotEqual => (PositiveBinaryOperator::Equal, true),
BinaryOperator::StrictEqual => (PositiveBinaryOperator::StrictEqual, false),
BinaryOperator::StrictNotEqual => (PositiveBinaryOperator::StrictEqual, true),
}
}
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum PositiveBinaryOperator {
Equal,
StrictEqual,
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum JsValueUrlKind {
Absolute,
Relative,
}
impl Display for JsValueUrlKind {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
JsValueUrlKind::Absolute => "absolute",
JsValueUrlKind::Relative => "relative",
})
}
}
/// The four categories of [JsValue]s.
pub(super) enum JsValueMetaKind {
/// Doesn't contain nested values.
Leaf,
/// Contains nested values. Nested values represent some structure and can't
/// be replaced during linking. They might contain placeholders.
Nested,
/// Contains nested values. Operations are replaced during linking. They
/// might contain placeholders.
Operation,
/// These values are replaced during linking.
Placeholder,
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum LogicalProperty {
Truthy,
Falsy,
Nullish,
NonNullish,
}
impl Display for LogicalProperty {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LogicalProperty::Truthy => write!(f, "truthy"),
LogicalProperty::Falsy => write!(f, "falsy"),
LogicalProperty::Nullish => write!(f, "nullish"),
LogicalProperty::NonNullish => write!(f, "non-nullish"),
}
}
}
use std::fmt::Display;
use either::Either;
use crate::analyzer::{JsValue, ModuleValue, ObjectPart};
impl Display for ObjectPart {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ObjectPart::KeyValue(key, value) => write!(f, "{key}: {value}"),
ObjectPart::Spread(value) => write!(f, "...{value}"),
}
}
}
impl Display for JsValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
JsValue::Constant(v) => write!(f, "{v}"),
JsValue::Url(url, kind) => write!(f, "{url} {kind}"),
JsValue::Array { items, mutable, .. } => write!(
f,
"{}[{}]",
if *mutable { "" } else { "frozen " },
items
.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(", ")
),
JsValue::Object { parts, mutable, .. } => write!(
f,
"{}{{{}}}",
if *mutable { "" } else { "frozen " },
parts
.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(", ")
),
JsValue::Alternatives {
total_nodes: _,
values: list,
logical_property,
} => {
let list = list
.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(" | ");
if let Some(logical_property) = logical_property {
write!(f, "({list}){{{logical_property}}}")
} else {
write!(f, "({list})")
}
}
JsValue::FreeVar(name) => write!(f, "FreeVar({name:?})"),
JsValue::Variable(name) => write!(f, "Variable({}#{:?})", name.0, name.1),
JsValue::Concat(_, list) => write!(
f,
"`{}`",
list.iter()
.map(|v| v
.as_str()
.map_or_else(|| format!("${{{v}}}"), |str| str.to_string()))
.collect::<Vec<_>>()
.join("")
),
JsValue::Add(_, list) => write!(
f,
"({})",
list.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(" + ")
),
JsValue::Not(_, value) => write!(f, "!({value})"),
JsValue::Logical(_, op, list) => write!(
f,
"({})",
list.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(op.joiner())
),
JsValue::Binary(_, a, op, b) => write!(f, "({}{}{})", a, op.joiner(), b),
JsValue::Tenary(_, test, cons, alt) => write!(f, "({test} ? {cons} : {alt})"),
JsValue::New(_, call) => write!(
f,
"new {}({})",
call.callee(),
call.args()
.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(", ")
),
JsValue::Call(_, call) => write!(
f,
"{}({})",
call.callee(),
call.args()
.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(", ")
),
JsValue::SuperCall(_, args) => write!(
f,
"super({})",
args.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(", ")
),
JsValue::MemberCall(_, call) => write!(
f,
"{}[{}]({})",
call.obj(),
call.prop(),
call.args()
.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(", ")
),
JsValue::Member(_, obj, prop) => write!(f, "{obj}[{prop}]"),
JsValue::Module(ModuleValue {
module: name,
annotations,
}) => {
write!(
f,
"Module({}, {})",
name.to_string_lossy(),
if let Some(annotations) = annotations {
Either::Left(annotations)
} else {
Either::Right("{}")
}
)
}
JsValue::Unknown { .. } => write!(f, "???"),
JsValue::WellKnownObject(obj) => write!(f, "WellKnownObject({obj:?})"),
JsValue::WellKnownFunction(func) => write!(f, "WellKnownFunction({func:?})"),
JsValue::Function(_, func_ident, return_value) => {
write!(f, "Function#{func_ident}(return = {return_value:?})")
}
JsValue::Argument(func_ident, index) => {
write!(f, "arguments[{index}#{func_ident}]")
}
JsValue::Iterated(_, iterable) => write!(f, "Iterated({iterable})"),
JsValue::TypeOf(_, operand) => write!(f, "typeof({operand})"),
JsValue::Promise(_, operand) => write!(f, "Promise<{operand}>"),
JsValue::Awaited(_, operand) => write!(f, "await({operand})"),
}
}
}
use std::{hash::BuildHasherDefault, mem::take};
use rustc_hash::FxHasher;
use turbo_tasks::FxIndexSet;
use crate::analyzer::{JsValue, jsvalue::similar::SimilarJsValue};
// Alternatives management
impl JsValue {
/// Add an alternative to the current value. Might be a no-op if the value
/// already contains this alternative. Potentially expensive operation
/// as it has to compare the value with all existing alternatives.
pub(crate) fn add_alt(&mut self, v: Self) {
if self == &v {
return;
}
if let JsValue::Alternatives {
total_nodes: c,
values,
logical_property: _,
} = self
{
if !values.contains(&v) {
*c += v.total_nodes();
values.push(v);
}
} else {
let l = take(self);
*self = JsValue::Alternatives {
total_nodes: 1 + l.total_nodes() + v.total_nodes(),
values: vec![l, v],
logical_property: None,
};
}
}
}
// Normalization
impl JsValue {
/// Normalizes only the current node. Nested alternatives, concatenations,
/// or operations are collapsed.
pub fn normalize_shallow(&mut self) {
match self {
JsValue::Alternatives {
total_nodes: _,
values,
logical_property: _,
} => {
if values.len() == 1 {
*self = take(&mut values[0]);
} else {
let mut set = FxIndexSet::with_capacity_and_hasher(
values.len(),
BuildHasherDefault::<FxHasher>::default(),
);
for v in take(values) {
match v {
JsValue::Alternatives {
total_nodes: _,
values,
logical_property: _,
} => {
for v in values {
set.insert(SimilarJsValue(v));
}
}
v => {
set.insert(SimilarJsValue(v));
}
}
}
if set.len() == 1 {
*self = set.into_iter().next().unwrap().0;
} else {
*values = set.into_iter().map(|v| v.0).collect();
self.update_total_nodes();
}
}
}
JsValue::Concat(_, v) => {
// Remove empty strings
v.retain(|v| v.as_str() != Some(""));
// TODO(kdy1): Remove duplicate
let mut new: Vec<JsValue> = vec![];
for v in take(v) {
if let Some(str) = v.as_str() {
if let Some(last) = new.last_mut() {
if let Some(last_str) = last.as_str() {
*last = [last_str, str].concat().into();
} else {
new.push(v);
}
} else {
new.push(v);
}
} else if let JsValue::Concat(_, v) = v {
new.extend(v);
} else {
new.push(v);
}
}
if new.len() == 1 {
*self = new.into_iter().next().unwrap();
} else {
*v = new;
self.update_total_nodes();
}
}
JsValue::Add(_, v) => {
let mut added: Vec<JsValue> = Vec::new();
let mut iter = take(v).into_iter();
while let Some(item) = iter.next() {
if item.is_string() == Some(true) {
let mut concat = match added.len() {
0 => Vec::new(),
1 => vec![added.into_iter().next().unwrap()],
_ => vec![JsValue::Add(
1 + added.iter().map(|v| v.total_nodes()).sum::<u32>(),
added,
)],
};
concat.push(item);
for item in iter.by_ref() {
concat.push(item);
}
*self = JsValue::Concat(
1 + concat.iter().map(|v| v.total_nodes()).sum::<u32>(),
concat,
);
return;
} else {
added.push(item);
}
}
if added.len() == 1 {
*self = added.into_iter().next().unwrap();
} else {
*v = added;
self.update_total_nodes();
}
}
JsValue::Logical(_, op, list)
// Nested logical expressions can be normalized: e. g. `a && (b && c)` => `a &&
// b && c`
if list.iter().any(|v| {
if let JsValue::Logical(_, inner_op, _) = v {
inner_op == op
} else {
false
}
}) => {
// Taking the old list and constructing a new merged list
for mut v in take(list).into_iter() {
if let JsValue::Logical(_, inner_op, inner_list) = &mut v {
if inner_op == op {
list.append(inner_list);
} else {
list.push(v);
}
} else {
list.push(v);
}
}
self.update_total_nodes();
}
_ => {}
}
}
/// Normalizes the current node and all nested nodes.
pub fn normalize(&mut self) {
self.for_each_children_mut(&mut |child| {
child.normalize();
true
});
self.normalize_shallow();
}
}
// Similarity
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment