use crate::pat::Pat;
use crate::{AssignOp, BinaryOp, LogicalOp, PropKind, UnaryOp, UpdateOp};
use crate::{Class, Func, FuncArg, FuncBody, Ident};
use std::borrow::Cow;
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))]
pub enum Expr<'a> {
Array(ArrayExpr<'a>),
ArrowFunc(ArrowFuncExpr<'a>),
ArrowParamPlaceHolder(Vec<FuncArg<'a>>, bool),
Assign(AssignExpr<'a>),
Await(Box<Expr<'a>>),
Binary(BinaryExpr<'a>),
Class(Class<'a>),
Call(CallExpr<'a>),
Conditional(ConditionalExpr<'a>),
Func(Func<'a>),
Ident(Ident<'a>),
Lit(Lit<'a>),
Logical(LogicalExpr<'a>),
Member(MemberExpr<'a>),
MetaProp(MetaProp<'a>),
New(NewExpr<'a>),
Obj(ObjExpr<'a>),
Sequence(SequenceExpr<'a>),
Spread(Box<Expr<'a>>),
Super,
TaggedTemplate(TaggedTemplateExpr<'a>),
This,
Unary(UnaryExpr<'a>),
Update(UpdateExpr<'a>),
Yield(YieldExpr<'a>),
}
impl<'a> Expr<'a> {
pub fn ident_from(s: &'a str) -> Self {
Expr::Ident(Ident::from(s))
}
}
pub type ArrayExpr<'a> = Vec<Option<Expr<'a>>>;
pub type ObjExpr<'a> = Vec<ObjProp<'a>>;
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))]
pub enum ObjProp<'a> {
Prop(Prop<'a>),
Spread(Expr<'a>),
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub struct Prop<'a> {
pub key: PropKey<'a>,
pub value: PropValue<'a>,
pub kind: PropKind,
pub method: bool,
pub computed: bool,
pub short_hand: bool,
pub is_static: bool,
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))]
pub enum PropKey<'a> {
Lit(Lit<'a>),
Expr(Expr<'a>),
Pat(Pat<'a>),
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))]
pub enum PropValue<'a> {
Expr(Expr<'a>),
Pat(Pat<'a>),
None,
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct UnaryExpr<'a> {
pub operator: UnaryOp,
pub prefix: bool,
pub argument: Box<Expr<'a>>,
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct UpdateExpr<'a> {
pub operator: UpdateOp,
pub argument: Box<Expr<'a>>,
pub prefix: bool,
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct BinaryExpr<'a> {
pub operator: BinaryOp,
pub left: Box<Expr<'a>>,
pub right: Box<Expr<'a>>,
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct AssignExpr<'a> {
pub operator: AssignOp,
pub left: AssignLeft<'a>,
pub right: Box<Expr<'a>>,
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))]
pub enum AssignLeft<'a> {
Pat(Pat<'a>),
Expr(Box<Expr<'a>>),
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct LogicalExpr<'a> {
pub operator: LogicalOp,
pub left: Box<Expr<'a>>,
pub right: Box<Expr<'a>>,
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct MemberExpr<'a> {
pub object: Box<Expr<'a>>,
pub property: Box<Expr<'a>>,
pub computed: bool,
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct ConditionalExpr<'a> {
pub test: Box<Expr<'a>>,
pub alternate: Box<Expr<'a>>,
pub consequent: Box<Expr<'a>>,
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct CallExpr<'a> {
pub callee: Box<Expr<'a>>,
pub arguments: Vec<Expr<'a>>,
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct NewExpr<'a> {
pub callee: Box<Expr<'a>>,
pub arguments: Vec<Expr<'a>>,
}
pub type SequenceExpr<'a> = Vec<Expr<'a>>;
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct ArrowFuncExpr<'a> {
pub id: Option<Ident<'a>>,
pub params: Vec<FuncArg<'a>>,
pub body: ArrowFuncBody<'a>,
pub expression: bool,
pub generator: bool,
pub is_async: bool,
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub enum ArrowFuncBody<'a> {
FuncBody(FuncBody<'a>),
Expr(Box<Expr<'a>>),
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct YieldExpr<'a> {
pub argument: Option<Box<Expr<'a>>>,
pub delegate: bool,
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct TaggedTemplateExpr<'a> {
pub tag: Box<Expr<'a>>,
pub quasi: TemplateLit<'a>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub struct TemplateLit<'a> {
pub quasis: Vec<TemplateElement<'a>>,
pub expressions: Vec<Expr<'a>>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub struct TemplateElement<'a> {
pub tail: bool,
pub cooked: Cow<'a, str>,
pub raw: Cow<'a, str>,
}
impl<'a> TemplateElement<'a> {
pub fn from(tail: bool, cooked: &'a str, raw: &'a str) -> TemplateElement<'a> {
Self {
tail,
cooked: Cow::Borrowed(cooked),
raw: Cow::Borrowed(raw),
}
}
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct MetaProp<'a> {
pub meta: Ident<'a>,
pub property: Ident<'a>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub enum Lit<'a> {
Null,
String(StringLit<'a>),
Number(Cow<'a, str>),
Boolean(bool),
RegEx(RegEx<'a>),
Template(TemplateLit<'a>),
}
impl<'a> Lit<'a> {
pub fn number_from(s: &'a str) -> Self {
Lit::Number(Cow::Borrowed(s))
}
pub fn single_string_from(s: &'a str) -> Self {
Lit::String(StringLit::single_from(s))
}
pub fn double_string_from(s: &'a str) -> Self {
Lit::String(StringLit::double_from(s))
}
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub enum StringLit<'a> {
Double(Cow<'a, str>),
Single(Cow<'a, str>),
}
impl<'a> StringLit<'a> {
pub fn double_from(s: &'a str) -> StringLit<'a> {
StringLit::Double(Cow::Borrowed(s))
}
pub fn single_from(s: &'a str) -> StringLit<'a> {
StringLit::Single(Cow::Borrowed(s))
}
pub fn clone_inner(&self) -> Cow<'a, str> {
match self {
StringLit::Single(ref s) => s.clone(),
StringLit::Double(ref s) => s.clone(),
}
}
pub fn inner_matches(&self, o: &str) -> bool {
match self {
StringLit::Single(ref s) => s == o,
StringLit::Double(ref d) => d == o,
}
}
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
#[cfg_attr(all(feature = "serialization"), serde(rename_all = "camelCase"))]
pub struct RegEx<'a> {
pub pattern: Cow<'a, str>,
pub flags: Cow<'a, str>,
}
impl<'a> RegEx<'a> {
pub fn from(p: &'a str, f: &'a str) -> Self {
RegEx {
pattern: Cow::Borrowed(p),
flags: Cow::Borrowed(f),
}
}
}