mirror of
https://github.com/swc-project/swc.git
synced 2024-12-25 06:36:08 +03:00
feat(es): Improve APIs for plugin authors (#5632)
- All variants of `Expr` now implement `Into<Box<Expr>>`. - All variants of `Expr` now implement `Into<PatOrExpr>`. - All variants of `Pat` now implement `Into<PatOrExpr>`. - All variants of `Pat` now implement `Into<Param>`. - `StmtOrModuleItem` is improved.
This commit is contained in:
parent
048beefb33
commit
e8edb67def
@ -265,11 +265,51 @@ impl Take for Expr {
|
||||
|
||||
bridge_expr_from!(Ident, Id);
|
||||
bridge_expr_from!(FnExpr, Function);
|
||||
bridge_expr_from!(ClassExpr, Class);
|
||||
|
||||
bridge_from!(Box<Expr>, Expr, ArrayLit);
|
||||
bridge_from!(Box<Expr>, Expr, ObjectLit);
|
||||
bridge_from!(Box<Expr>, Expr, MemberExpr);
|
||||
bridge_from!(Box<Expr>, Expr, SuperPropExpr);
|
||||
macro_rules! boxed_expr {
|
||||
($T:ty) => {
|
||||
bridge_from!(Box<Expr>, Expr, $T);
|
||||
bridge_from!(PatOrExpr, Box<Expr>, $T);
|
||||
};
|
||||
}
|
||||
|
||||
boxed_expr!(ThisExpr);
|
||||
boxed_expr!(ArrayLit);
|
||||
boxed_expr!(ObjectLit);
|
||||
boxed_expr!(FnExpr);
|
||||
boxed_expr!(UnaryExpr);
|
||||
boxed_expr!(UpdateExpr);
|
||||
boxed_expr!(BinExpr);
|
||||
boxed_expr!(AssignExpr);
|
||||
boxed_expr!(MemberExpr);
|
||||
boxed_expr!(SuperPropExpr);
|
||||
boxed_expr!(CondExpr);
|
||||
boxed_expr!(CallExpr);
|
||||
boxed_expr!(NewExpr);
|
||||
boxed_expr!(SeqExpr);
|
||||
bridge_from!(Box<Expr>, Expr, Ident);
|
||||
boxed_expr!(Lit);
|
||||
boxed_expr!(Tpl);
|
||||
boxed_expr!(TaggedTpl);
|
||||
boxed_expr!(ArrowExpr);
|
||||
boxed_expr!(ClassExpr);
|
||||
boxed_expr!(YieldExpr);
|
||||
boxed_expr!(MetaPropExpr);
|
||||
boxed_expr!(AwaitExpr);
|
||||
boxed_expr!(ParenExpr);
|
||||
boxed_expr!(JSXMemberExpr);
|
||||
boxed_expr!(JSXNamespacedName);
|
||||
boxed_expr!(JSXEmptyExpr);
|
||||
boxed_expr!(Box<JSXElement>);
|
||||
boxed_expr!(JSXFragment);
|
||||
boxed_expr!(TsTypeAssertion);
|
||||
boxed_expr!(TsConstAssertion);
|
||||
boxed_expr!(TsNonNullExpr);
|
||||
boxed_expr!(TsAsExpr);
|
||||
boxed_expr!(TsInstantiation);
|
||||
boxed_expr!(PrivateName);
|
||||
boxed_expr!(OptChainExpr);
|
||||
|
||||
#[ast_node("ThisExpression")]
|
||||
#[derive(Eq, Hash, Copy, EqIgnoreSpan)]
|
||||
@ -494,6 +534,12 @@ impl Take for ClassExpr {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Class> for ClassExpr {
|
||||
fn from(class: Class) -> Self {
|
||||
Self { ident: None, class }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Spanned, Clone, Debug, PartialEq, Serialize)]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
@ -1168,9 +1214,6 @@ pub enum PatOrExpr {
|
||||
bridge_from!(PatOrExpr, Box<Pat>, Pat);
|
||||
bridge_from!(PatOrExpr, Pat, Ident);
|
||||
bridge_from!(PatOrExpr, Pat, Id);
|
||||
bridge_from!(PatOrExpr, Box<Expr>, Expr);
|
||||
bridge_from!(PatOrExpr, Box<Expr>, MemberExpr);
|
||||
bridge_from!(PatOrExpr, Box<Expr>, SuperPropExpr);
|
||||
|
||||
impl PatOrExpr {
|
||||
/// Returns the [Pat] if this is a pattern, otherwise returns [None].
|
||||
|
@ -61,8 +61,20 @@ impl Take for Pat {
|
||||
|
||||
bridge_pat_from!(BindingIdent, Ident);
|
||||
bridge_pat_from!(BindingIdent, Id);
|
||||
bridge_from!(crate::Param, crate::Pat, BindingIdent);
|
||||
bridge_from!(Box<crate::Pat>, crate::Pat, BindingIdent);
|
||||
|
||||
macro_rules! pat_to_other {
|
||||
($T:ty) => {
|
||||
bridge_from!(crate::Param, crate::Pat, $T);
|
||||
bridge_from!(Box<crate::Pat>, crate::Pat, $T);
|
||||
bridge_from!(crate::PatOrExpr, crate::Pat, $T);
|
||||
};
|
||||
}
|
||||
|
||||
pat_to_other!(BindingIdent);
|
||||
pat_to_other!(ArrayPat);
|
||||
pat_to_other!(ObjectPat);
|
||||
pat_to_other!(AssignPat);
|
||||
pat_to_other!(RestPat);
|
||||
|
||||
#[ast_node("ArrayPattern")]
|
||||
#[derive(Eq, Hash, EqIgnoreSpan)]
|
||||
|
@ -186,10 +186,16 @@ impl Visit for ArgumentsFinder {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait StmtOrModuleItem: Send + Sync {
|
||||
pub trait StmtOrModuleItem: Send + Sync + Sized {
|
||||
fn into_stmt(self) -> Result<Stmt, ModuleDecl>;
|
||||
|
||||
fn as_stmt(&self) -> Result<&Stmt, &ModuleDecl>;
|
||||
|
||||
fn as_stmt_mut(&mut self) -> Result<&mut Stmt, &mut ModuleDecl>;
|
||||
|
||||
fn from_stmt(stmt: Stmt) -> Self;
|
||||
|
||||
fn try_from_module_decl(decl: ModuleDecl) -> Result<Self, ModuleDecl>;
|
||||
}
|
||||
|
||||
impl StmtOrModuleItem for Stmt {
|
||||
@ -202,6 +208,21 @@ impl StmtOrModuleItem for Stmt {
|
||||
fn as_stmt(&self) -> Result<&Stmt, &ModuleDecl> {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_stmt_mut(&mut self) -> Result<&mut Stmt, &mut ModuleDecl> {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_stmt(stmt: Stmt) -> Self {
|
||||
stmt
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_from_module_decl(decl: ModuleDecl) -> Result<Self, ModuleDecl> {
|
||||
Err(decl)
|
||||
}
|
||||
}
|
||||
|
||||
impl StmtOrModuleItem for ModuleItem {
|
||||
@ -220,6 +241,24 @@ impl StmtOrModuleItem for ModuleItem {
|
||||
ModuleItem::Stmt(v) => Ok(v),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_stmt_mut(&mut self) -> Result<&mut Stmt, &mut ModuleDecl> {
|
||||
match self {
|
||||
ModuleItem::ModuleDecl(v) => Err(v),
|
||||
ModuleItem::Stmt(v) => Ok(v),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_stmt(stmt: Stmt) -> Self {
|
||||
ModuleItem::Stmt(stmt)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_from_module_decl(decl: ModuleDecl) -> Result<Self, ModuleDecl> {
|
||||
Ok(ModuleItem::ModuleDecl(decl))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ModuleItemLike: StmtLike {
|
||||
|
Loading…
Reference in New Issue
Block a user