From 9e2dad106c314c1265daacfb2de36e7427c385f2 Mon Sep 17 00:00:00 2001 From: Robert Masen Date: Wed, 7 Jun 2023 18:14:35 -0500 Subject: [PATCH 1/8] remove SourceText wrapper --- src/expr.rs | 34 +++++++++---------- src/lib.rs | 77 ++++-------------------------------------- src/pat.rs | 2 +- src/spanned/convert.rs | 39 ++++++++++++++++++--- src/spanned/mod.rs | 10 +++--- 5 files changed, 63 insertions(+), 99 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 6dc3d41..cb74922 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1,5 +1,5 @@ use crate::pat::Pat; -use crate::{AssignOp, BinaryOp, LogicalOp, PropKind, SourceText, UnaryOp, UpdateOp}; +use crate::{AssignOp, BinaryOp, LogicalOp, PropKind, UnaryOp, UpdateOp}; use crate::{Class, Func, FuncArg, FuncBody, Ident}; /// A slightly more granular program part that a statement #[derive(Debug, Clone, PartialEq)] @@ -95,7 +95,7 @@ pub enum Expr { impl Expr { pub fn ident_from(inner: T) -> Self { Self::Ident(Ident { - name: SourceText(inner), + name: inner, }) } } @@ -348,7 +348,7 @@ pub enum QuasiQuote { pub struct TemplateElement { pub open_quote: QuasiQuote, /// The non-quoted version - pub content: SourceText, + pub content: T, pub close_quote: QuasiQuote, } @@ -399,7 +399,7 @@ pub enum Lit { /// `0xf` /// `0o7` /// `0b1` - Number(SourceText), + Number(T), /// `true` /// `false` Boolean(bool), @@ -413,7 +413,7 @@ pub enum Lit { impl Lit { pub fn number_from(s: T) -> Self { - Lit::Number(SourceText(s)) + Lit::Number(s) } pub fn single_string_from(s: T) -> Self { Lit::String(StringLit::single_from(s)) @@ -426,16 +426,16 @@ impl Lit { #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] pub enum StringLit { - Double(SourceText), - Single(SourceText), + Double(T), + Single(T), } impl StringLit { pub fn double_from(s: T) -> StringLit { - StringLit::Double(SourceText(s)) + StringLit::Double(s) } pub fn single_from(s: T) -> StringLit { - StringLit::Single(SourceText(s)) + StringLit::Single(s) } } impl StringLit @@ -444,8 +444,8 @@ where { pub fn clone_inner(&self) -> T { match self { - StringLit::Single(ref s) => s.0.clone(), - StringLit::Double(ref s) => s.0.clone(), + StringLit::Single(ref s) => s.clone(), + StringLit::Double(ref s) => s.clone(), } } } @@ -456,8 +456,8 @@ where { pub fn inner_matches(&self, o: &str) -> bool { match self { - StringLit::Single(ref s) => o.eq(s.0.as_ref()), - StringLit::Double(ref d) => o.eq(d.0.as_ref()), + StringLit::Single(ref s) => o.eq(s.as_ref()), + StringLit::Double(ref d) => o.eq(d.as_ref()), } } } @@ -466,15 +466,15 @@ where #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] #[cfg_attr(all(feature = "serialization"), serde(rename_all = "camelCase"))] pub struct RegEx { - pub pattern: SourceText, - pub flags: Option>, + pub pattern: T, + pub flags: Option, } impl RegEx { pub fn from(p: T, f: Option) -> Self { RegEx { - pattern: SourceText(p), - flags: f.map(SourceText), + pattern: p, + flags: f, } } } diff --git a/src/lib.rs b/src/lib.rs index 9774678..64a1e6e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,78 +10,13 @@ pub mod serde; pub mod spanned; pub mod stmt; -use std::{borrow::Cow, fmt::Debug, ops::Deref}; +use std::{borrow::Cow, fmt::Debug}; use decl::Decl; use expr::{Expr, Lit, Prop}; use pat::Pat; use stmt::Stmt; -#[derive(Clone, Default)] -pub struct SourceText(pub T); - -impl From for SourceText { - fn from(value: T) -> Self { - Self(value) - } -} - -impl Deref for SourceText<&str> { - type Target = str; - fn deref(&self) -> &Self::Target { - self.0 - } -} - -impl Deref for SourceText { - type Target = str; - fn deref(&self) -> &Self::Target { - self.0.as_str() - } -} - -impl AsRef for SourceText -where - T: AsRef, -{ - fn as_ref(&self) -> &str { - self.0.as_ref() - } -} - -impl std::fmt::Display for SourceText -where - T: std::fmt::Display, -{ - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} - -impl std::cmp::PartialEq for SourceText -where - U: PartialEq, -{ - fn eq(&self, other: &U) -> bool { - other.eq(&self.0) - } -} - -impl<'a> std::cmp::PartialEq> for &'a str { - fn eq(&self, other: &SourceText<&'a str>) -> bool { - (&other.0).eq(self) - } -} - -impl std::fmt::Debug for SourceText -where - T: std::fmt::Debug, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", &self.0) - } -} - #[derive(Debug, Clone, PartialEq)] #[cfg_attr( all(feature = "serde", not(feature = "esprima")), @@ -89,13 +24,13 @@ where )] #[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct Ident { - pub name: SourceText, + pub name: T, } impl<'a> From<&'a str> for Ident<&'a str> { fn from(value: &'a str) -> Self { Self { - name: SourceText(value), + name: value, } } } @@ -103,7 +38,7 @@ impl<'a> From<&'a str> for Ident<&'a str> { impl From for Ident { fn from(value: String) -> Self { Self { - name: SourceText(value), + name: value, } } } @@ -111,7 +46,7 @@ impl From for Ident { impl<'a> From> for Ident> { fn from(value: Cow<'a, str>) -> Self { Self { - name: SourceText(value), + name: value, } } } @@ -180,7 +115,7 @@ impl ProgramPart { #[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct Dir { pub expr: Lit, - pub dir: SourceText, + pub dir: T, } /// A function, this will be part of either a function diff --git a/src/pat.rs b/src/pat.rs index b1e31c2..bde0a1f 100644 --- a/src/pat.rs +++ b/src/pat.rs @@ -20,7 +20,7 @@ pub enum Pat { impl Pat { pub fn ident_from(inner: T) -> Self { Self::Ident(Ident { - name: crate::SourceText(inner), + name: inner, }) } } diff --git a/src/spanned/convert.rs b/src/spanned/convert.rs index 84c79fc..7230c6d 100644 --- a/src/spanned/convert.rs +++ b/src/spanned/convert.rs @@ -1,6 +1,8 @@ //! All conversions from spanned into non-spanned types //! +use std::borrow::Cow; + use crate::{ spanned::{ decl::{ @@ -24,7 +26,6 @@ use crate::{ Class, ClassBody, Dir, Func, FuncArg, FuncArgEntry, FuncBody, Ident, Program, ProgramPart, Slice, VarKind, }, - SourceText, }; mod decl { @@ -513,7 +514,7 @@ mod expr { fn from(other: TemplateElement) -> Self { Self { open_quote: other.open_quote.into(), - content: other.content.into(), + content: other.content.source, close_quote: other.close_quote.into(), } } @@ -1006,8 +1007,38 @@ mod stmt { } } -impl From> for SourceText { - fn from(other: Slice) -> Self { +impl From> for String { + fn from(other: Slice) -> Self { + other.source + } +} + +impl<'a> From> for &'a str { + fn from(other: Slice<&'a str>) -> &'a str { + other.source + } +} + +impl<'a> From>> for Cow<'a, str> { + fn from(other: Slice>) -> Cow<'a, str> { + other.source + } +} + +impl<'a> From> for &'a [u8] { + fn from(other: Slice<&'a [u8]>) -> &'a [u8] { + other.source + } +} + +impl<'a> From>> for Cow<'a, [u8]> { + fn from(other: Slice>) -> Cow<'a, [u8]> { + other.source + } +} + +impl From>> for Vec { + fn from(other: Slice>) -> Self { other.source } } diff --git a/src/spanned/mod.rs b/src/spanned/mod.rs index 80d7096..3fe67da 100644 --- a/src/spanned/mod.rs +++ b/src/spanned/mod.rs @@ -10,8 +10,6 @@ use expr::{Expr, Lit, Prop}; use pat::Pat; use stmt::Stmt; -use crate::SourceText; - use self::{ pat::RestPat, tokens::{ @@ -53,7 +51,7 @@ impl From> for Ident { impl Ident { pub fn name(&self) -> &T { - &self.slice.source.0 + &self.slice.source } } @@ -138,7 +136,7 @@ impl ProgramPart { #[derive(Debug, Clone, PartialEq)] pub struct Dir { pub expr: Lit, - pub dir: SourceText, + pub dir: T, pub semi_colon: Option, } @@ -350,14 +348,14 @@ impl VarKind { #[derive(Debug, Clone, PartialEq)] pub struct Slice { - pub source: SourceText, + pub source: T, pub loc: SourceLocation, } impl Slice { pub fn new(source: T, start_line: u32, start_col: u32, end_line: u32, end_column: u32) -> Self { Self { - source: SourceText(source), + source: source, loc: SourceLocation::new(start_line, start_col, end_line, end_column), } } From 45b82ab6ae5533f710d0341fd5e9777ecaea44f6 Mon Sep 17 00:00:00 2001 From: Robert Masen Date: Sun, 11 Jun 2023 09:43:42 -0500 Subject: [PATCH 2/8] Add IntoAllocated trait and implementation --- src/decl.rs | 107 +++++++++- src/expr.rs | 326 +++++++++++++++++++++++++++- src/lib.rs | 132 +++++++++++- src/pat.rs | 53 ++++- src/spanned/convert.rs | 42 ++-- src/spanned/decl.rs | 222 +++++++++++++++++++ src/spanned/expr.rs | 468 ++++++++++++++++++++++++++++++++++++++++- src/spanned/mod.rs | 144 +++++++++++++ src/spanned/pat.rs | 87 ++++++++ src/spanned/stmt.rs | 271 +++++++++++++++++++++++- src/stmt.rs | 204 +++++++++++++++++- 11 files changed, 2010 insertions(+), 46 deletions(-) diff --git a/src/decl.rs b/src/decl.rs index 30b6f49..99d23e8 100644 --- a/src/decl.rs +++ b/src/decl.rs @@ -1,6 +1,6 @@ use crate::expr::{Expr, Lit}; use crate::pat::Pat; -use crate::VarKind; +use crate::{VarKind, IntoAllocated}; use crate::{Class, Func, Ident}; /// The declaration of a variable, function, class, import or export @@ -41,6 +41,20 @@ pub enum Decl { Export(Box>), } +impl IntoAllocated for Decl where T: ToString { + type Allocated = Decl; + + fn into_allocated(self) -> Self::Allocated { + match self { + Decl::Var(k, decls) => Decl::Var(k, decls.into_iter().map(|d| d.into_allocated()).collect()), + Decl::Func(inner) => Decl::Func(inner.into_allocated()), + Decl::Class(inner) => Decl::Class(inner.into_allocated()), + Decl::Import(inner) => Decl::Import(inner.into_allocated()), + Decl::Export(inner) => Decl::Export(inner.into_allocated()), + } + } +} + /// The identifier and optional value of a variable declaration #[derive(Debug, Clone, PartialEq)] #[cfg_attr( @@ -53,6 +67,17 @@ pub struct VarDecl { pub init: Option>, } +impl IntoAllocated for VarDecl where T: ToString { + type Allocated = VarDecl; + + fn into_allocated(self) -> Self::Allocated { + VarDecl { + id: self.id.into_allocated(), + init: self.init.map(|i| i.into_allocated()), + } + } +} + /// A declaration that imports exported /// members of another module /// @@ -66,6 +91,17 @@ pub struct ModImport { pub source: Lit, } +impl IntoAllocated for ModImport where T: ToString { + type Allocated = ModImport; + + fn into_allocated(self) -> Self::Allocated { + ModImport { + specifiers: self.specifiers.into_iter().map(|s| s.into_allocated()).collect(), + source: self.source.into_allocated(), + } + } +} + /// The name of the thing being imported #[derive(Debug, Clone, PartialEq)] #[cfg_attr( @@ -97,6 +133,19 @@ pub enum ImportSpecifier { /// ``` Namespace(Ident), } + +impl IntoAllocated for ImportSpecifier where T: ToString { + type Allocated = ImportSpecifier; + + fn into_allocated(self) -> Self::Allocated { + match self { + ImportSpecifier::Normal(inner) => ImportSpecifier::Normal(inner.into_iter().map(|n| n.into_allocated()).collect()), + ImportSpecifier::Default(inner) => ImportSpecifier::Default(inner.into_allocated()), + ImportSpecifier::Namespace(inner) => ImportSpecifier::Namespace(inner.into_allocated()), + } + } +} + #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] pub struct NormalImportSpec { @@ -104,6 +153,17 @@ pub struct NormalImportSpec { pub imported: Ident, } +impl IntoAllocated for NormalImportSpec where T: ToString { + type Allocated = NormalImportSpec; + + fn into_allocated(self) -> Self::Allocated { + NormalImportSpec { + alias: self.alias.map(|i| i.into_allocated()), + imported: self.imported.into_allocated(), + } + } +} + /// Something exported from this module #[derive(Debug, Clone, PartialEq)] #[cfg_attr( @@ -138,6 +198,18 @@ pub enum ModExport { }, } +impl IntoAllocated for ModExport where T: ToString { + type Allocated = ModExport; + + fn into_allocated(self) -> Self::Allocated { + match self { + ModExport::Default(inner) => ModExport::Default(inner.into_allocated()), + ModExport::Named(inner) => ModExport::Named(inner.into_allocated()), + ModExport::All { alias, name } => ModExport::All { alias: alias.map(|i| i.into_allocated()), name: name.into_allocated()}, + } + } +} + /// An export that has a name /// ```js /// export function thing() {} @@ -149,6 +221,17 @@ pub enum NamedExportDecl { Specifier(Vec>, Option>), } +impl IntoAllocated for NamedExportDecl where T: ToString { + type Allocated = NamedExportDecl; + + fn into_allocated(self) -> Self::Allocated { + match self { + NamedExportDecl::Decl(inner) => NamedExportDecl::Decl(inner.into_allocated()), + NamedExportDecl::Specifier(specs, lit) => NamedExportDecl::Specifier(specs.into_iter().map(|s| s.into_allocated()).collect(), lit.map(|l| l.into_allocated())), + } + } +} + /// A default export /// ```js /// export default class Thing {} @@ -164,6 +247,17 @@ pub enum DefaultExportDecl { Expr(Expr), } +impl IntoAllocated for DefaultExportDecl where T: ToString { + type Allocated = DefaultExportDecl; + + fn into_allocated(self) -> Self::Allocated { + match self { + DefaultExportDecl::Decl(inner) => DefaultExportDecl::Decl(inner.into_allocated()), + DefaultExportDecl::Expr(inner) => DefaultExportDecl::Expr(inner.into_allocated()), + } + } +} + /// The name of the thing being exported /// this might include an alias /// ```js @@ -182,3 +276,14 @@ pub struct ExportSpecifier { pub local: Ident, pub alias: Option>, } + +impl IntoAllocated for ExportSpecifier where T: ToString { + type Allocated = ExportSpecifier; + + fn into_allocated(self) -> Self::Allocated { + ExportSpecifier { + local: self.local.into_allocated(), + alias: self.alias.map(|a| a.into_allocated()), + } + } +} diff --git a/src/expr.rs b/src/expr.rs index cb74922..a9b1db5 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1,5 +1,5 @@ use crate::pat::Pat; -use crate::{AssignOp, BinaryOp, LogicalOp, PropKind, UnaryOp, UpdateOp}; +use crate::{AssignOp, BinaryOp, LogicalOp, PropKind, UnaryOp, UpdateOp, IntoAllocated}; use crate::{Class, Func, FuncArg, FuncBody, Ident}; /// A slightly more granular program part that a statement #[derive(Debug, Clone, PartialEq)] @@ -92,11 +92,43 @@ pub enum Expr { Yield(YieldExpr), } +impl IntoAllocated for Expr where T: ToString { + type Allocated = Expr; + + fn into_allocated(self) -> Self::Allocated { + match self { + Expr::Array(inner) => Expr::Array(inner.into_iter().map(|o| o.map(|e| e.into_allocated())).collect()), + Expr::ArrowFunc(inner) => Expr::ArrowFunc(inner.into_allocated()), + Expr::ArrowParamPlaceHolder(args, is_async) => Expr::ArrowParamPlaceHolder(args.into_iter().map(|a| a.into_allocated()).collect(), is_async), + Expr::Assign(inner) => Expr::Assign(inner.into_allocated()), + Expr::Await(inner) => Expr::Await(inner.into_allocated()), + Expr::Binary(inner) => Expr::Binary(inner.into_allocated()), + Expr::Class(inner) => Expr::Class(inner.into_allocated()), + Expr::Call(inner) => Expr::Call(inner.into_allocated()), + Expr::Conditional(inner) => Expr::Conditional(inner.into_allocated()), + Expr::Func(inner) => Expr::Func(inner.into_allocated()), + Expr::Ident(inner) => Expr::Ident(inner.into_allocated()), + Expr::Lit(inner) => Expr::Lit(inner.into_allocated()), + Expr::Logical(inner) => Expr::Logical(inner.into_allocated()), + Expr::Member(inner) => Expr::Member(inner.into_allocated()), + Expr::MetaProp(inner) => Expr::MetaProp(inner.into_allocated()), + Expr::New(inner) => Expr::New(inner.into_allocated()), + Expr::Obj(inner) => Expr::Obj(inner.into_iter().map(|p| p.into_allocated()).collect()), + Expr::Sequence(inner) => Expr::Sequence(inner.into_iter().map(|e| e.into_allocated()).collect()), + Expr::Spread(inner) => Expr::Spread(inner.into_allocated()), + Expr::Super => Expr::Super, + Expr::TaggedTemplate(inner) => Expr::TaggedTemplate(inner.into_allocated()), + Expr::This => Expr::This, + Expr::Unary(inner) => Expr::Unary(inner.into_allocated()), + Expr::Update(inner) => Expr::Update(inner.into_allocated()), + Expr::Yield(inner) => Expr::Yield(inner.into_allocated()), + } + } +} + impl Expr { pub fn ident_from(inner: T) -> Self { - Self::Ident(Ident { - name: inner, - }) + Self::Ident(Ident { name: inner }) } } @@ -113,6 +145,17 @@ pub enum ObjProp { Spread(Expr), } +impl IntoAllocated for ObjProp where T: ToString { + type Allocated = ObjProp; + + fn into_allocated(self) -> Self::Allocated { + match self { + ObjProp::Prop(inner) => ObjProp::Prop(inner.into_allocated()), + ObjProp::Spread(inner) => ObjProp::Spread(inner.into_allocated()), + } + } +} + /// A single part of an object literal or class #[derive(Debug, Clone, PartialEq)] #[cfg_attr( @@ -130,6 +173,22 @@ pub struct Prop { pub is_static: bool, } +impl IntoAllocated for Prop where T: ToString { + type Allocated = Prop; + + fn into_allocated(self) -> Self::Allocated { + Prop { + key: self.key.into_allocated(), + value: self.value.into_allocated(), + kind: self.kind, + method: self.method, + computed: self.computed, + short_hand: self.short_hand, + is_static: self.is_static, + } + } +} + /// An object literal or class property identifier #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] @@ -140,6 +199,18 @@ pub enum PropKey { Pat(Pat), } +impl IntoAllocated for PropKey where T: ToString { + type Allocated = PropKey; + + fn into_allocated(self) -> Self::Allocated { + match self { + PropKey::Lit(inner) => PropKey::Lit(inner.into_allocated()), + PropKey::Expr(inner) => PropKey::Expr(inner.into_allocated()), + PropKey::Pat(inner) => PropKey::Pat(inner.into_allocated()), + } + } +} + /// The value of an object literal or class property #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] @@ -150,6 +221,18 @@ pub enum PropValue { None, } +impl IntoAllocated for PropValue where T: ToString { + type Allocated = PropValue; + + fn into_allocated(self) -> Self::Allocated { + match self { + PropValue::Expr(inner) => PropValue::Expr(inner.into_allocated()), + PropValue::Pat(inner) => PropValue::Pat(inner.into_allocated()), + PropValue::None => PropValue::None, + } + } +} + /// An operation that takes one argument #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] @@ -159,6 +242,18 @@ pub struct UnaryExpr { pub argument: Box>, } +impl IntoAllocated for UnaryExpr where T: ToString { + type Allocated = UnaryExpr; + + fn into_allocated(self) -> Self::Allocated { + UnaryExpr { + operator: self.operator, + prefix: self.prefix, + argument: self.argument.into_allocated(), + } + } +} + /// Increment or decrementing a value #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] @@ -168,6 +263,18 @@ pub struct UpdateExpr { pub prefix: bool, } +impl IntoAllocated for UpdateExpr where T: ToString { + type Allocated = UpdateExpr; + + fn into_allocated(self) -> Self::Allocated { + UpdateExpr { + operator: self.operator, + argument: self.argument.into_allocated(), + prefix: self.prefix, + } + } +} + /// An operation that requires 2 arguments #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] @@ -177,6 +284,18 @@ pub struct BinaryExpr { pub right: Box>, } +impl IntoAllocated for BinaryExpr where T: ToString { + type Allocated = BinaryExpr; + + fn into_allocated(self) -> Self::Allocated { + BinaryExpr { + operator: self.operator, + left: self.left.into_allocated(), + right: self.right.into_allocated(), + } + } +} + /// An assignment or update + assignment operation #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] @@ -186,6 +305,18 @@ pub struct AssignExpr { pub right: Box>, } +impl IntoAllocated for AssignExpr where T: ToString { + type Allocated = AssignExpr; + + fn into_allocated(self) -> Self::Allocated { + AssignExpr { + operator: self.operator, + left: self.left.into_allocated(), + right: self.right.into_allocated(), + } + } +} + /// The value being assigned to #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] @@ -195,6 +326,17 @@ pub enum AssignLeft { Expr(Box>), } +impl IntoAllocated for AssignLeft where T: ToString { + type Allocated = AssignLeft; + + fn into_allocated(self) -> Self::Allocated { + match self { + AssignLeft::Pat(inner) => AssignLeft::Pat(inner.into_allocated()), + AssignLeft::Expr(inner) => AssignLeft::Expr(inner.into_allocated()), + } + } +} + /// A specialized `BinaryExpr` for logical evaluation /// ```js /// true && true @@ -208,6 +350,18 @@ pub struct LogicalExpr { pub right: Box>, } +impl IntoAllocated for LogicalExpr where T: ToString { + type Allocated = LogicalExpr; + + fn into_allocated(self) -> Self::Allocated { + LogicalExpr { + operator: self.operator, + left: self.left.into_allocated(), + right: self.right.into_allocated(), + } + } +} + /// Accessing the member of a value /// ```js /// b['thing']; @@ -221,6 +375,18 @@ pub struct MemberExpr { pub computed: bool, } +impl IntoAllocated for MemberExpr where T: ToString { + type Allocated = MemberExpr; + + fn into_allocated(self) -> Self::Allocated { + MemberExpr { + object: self.object.into_allocated(), + property: self.property.into_allocated(), + computed: self.computed, + } + } +} + /// A ternery expression /// ```js /// var a = true ? 'stuff' : 'things'; @@ -233,6 +399,18 @@ pub struct ConditionalExpr { pub consequent: Box>, } +impl IntoAllocated for ConditionalExpr where T: ToString { + type Allocated = ConditionalExpr; + + fn into_allocated(self) -> Self::Allocated { + ConditionalExpr { + test: self.test.into_allocated(), + alternate: self.alternate.into_allocated(), + consequent: self.consequent.into_allocated(), + } + } +} + /// Calling a function or method /// ```js /// Math.random() @@ -244,6 +422,17 @@ pub struct CallExpr { pub arguments: Vec>, } +impl IntoAllocated for CallExpr where T: ToString { + type Allocated = CallExpr; + + fn into_allocated(self) -> Self::Allocated { + CallExpr { + callee: self.callee.into_allocated(), + arguments: self.arguments.into_iter().map(IntoAllocated::into_allocated).collect(), + } + } +} + /// Calling a constructor /// ```js /// new Uint8Array(32); @@ -255,6 +444,17 @@ pub struct NewExpr { pub arguments: Vec>, } +impl IntoAllocated for NewExpr where T: ToString { + type Allocated = NewExpr; + + fn into_allocated(self) -> Self::Allocated { + NewExpr { + callee: self.callee.into_allocated(), + arguments: self.arguments.into_iter().map(|a| a.into_allocated()).collect(), + } + } +} + /// A collection of `Exprs` separated by commas pub type SequenceExpr = Vec>; @@ -276,6 +476,21 @@ pub struct ArrowFuncExpr { pub is_async: bool, } +impl IntoAllocated for ArrowFuncExpr where T: ToString { + type Allocated = ArrowFuncExpr; + + fn into_allocated(self) -> Self::Allocated { + ArrowFuncExpr { + id: self.id.map(|i| i.into_allocated()), + params: self.params.into_iter().map(|p| p.into_allocated()).collect(), + body: self.body.into_allocated(), + expression: self.expression, + generator: self.generator, + is_async: self.is_async, + } + } +} + /// The body portion of an arrow function can be either an expression or a block of statements #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] @@ -284,6 +499,17 @@ pub enum ArrowFuncBody { Expr(Box>), } +impl IntoAllocated for ArrowFuncBody where T: ToString { + type Allocated = ArrowFuncBody; + + fn into_allocated(self) -> Self::Allocated { + match self { + ArrowFuncBody::FuncBody(inner) => ArrowFuncBody::FuncBody(inner.into_allocated()), + ArrowFuncBody::Expr(inner) => ArrowFuncBody::Expr(inner.into_allocated()), + } + } +} + /// yield a value from inside of a generator function /// ```js /// function *gen() { @@ -299,6 +525,16 @@ pub struct YieldExpr { pub delegate: bool, } +impl IntoAllocated for YieldExpr where T: ToString { + type Allocated = YieldExpr; + + fn into_allocated(self) -> Self::Allocated { + YieldExpr { + delegate: self.delegate, + argument: self.argument.map(IntoAllocated::into_allocated), + } + } +} /// A Template literal preceded by a function identifier /// see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates) for more details #[derive(PartialEq, Debug, Clone)] @@ -308,6 +544,17 @@ pub struct TaggedTemplateExpr { pub quasi: TemplateLit, } +impl IntoAllocated for TaggedTemplateExpr where T: ToString { + type Allocated = TaggedTemplateExpr; + + fn into_allocated(self) -> Self::Allocated { + TaggedTemplateExpr { + tag: self.tag.into_allocated(), + quasi: self.quasi.into_allocated(), + } + } +} + /// A template string literal /// ```js /// `I own ${0} birds`; @@ -323,6 +570,17 @@ pub struct TemplateLit { pub expressions: Vec>, } +impl IntoAllocated for TemplateLit where T: ToString { + type Allocated = TemplateLit; + + fn into_allocated(self) -> Self::Allocated { + TemplateLit { + quasis: self.quasis.into_iter().map(|e| e.into_allocated()).collect(), + expressions: self.expressions.into_iter().map(|e| e.into_allocated()).collect(), + } + } +} + #[derive(Debug, Clone, PartialEq)] #[cfg_attr( all(feature = "serde", not(feature = "esprima")), @@ -352,6 +610,18 @@ pub struct TemplateElement { pub close_quote: QuasiQuote, } +impl IntoAllocated for TemplateElement where T: ToString { + type Allocated = TemplateElement; + + fn into_allocated(self) -> Self::Allocated { + TemplateElement { + open_quote: self.open_quote, + content: self.content.to_string(), + close_quote: self.close_quote, + } + } +} + impl TemplateElement { pub fn is_tail(&self) -> bool { matches!( @@ -378,6 +648,17 @@ pub struct MetaProp { pub property: Ident, } +impl IntoAllocated for MetaProp where T: ToString { + type Allocated = MetaProp; + + fn into_allocated(self) -> Self::Allocated { + MetaProp { + meta: self.meta.into_allocated(), + property: self.property.into_allocated(), + } + } +} + /// A literal value #[derive(Debug, Clone, PartialEq)] #[cfg_attr( @@ -411,6 +692,21 @@ pub enum Lit { Template(TemplateLit), } +impl IntoAllocated for Lit where T: ToString { + type Allocated = Lit; + + fn into_allocated(self) -> Self::Allocated { + match self { + Lit::Null => Lit::Null, + Lit::String(inner) => Lit::String(inner.into_allocated()), + Lit::Number(inner) => Lit::Number(inner.to_string()), + Lit::Boolean(inner) => Lit::Boolean(inner), + Lit::RegEx(inner) => Lit::RegEx(inner.into_allocated()), + Lit::Template(inner) => Lit::Template(inner.into_allocated()), + } + } +} + impl Lit { pub fn number_from(s: T) -> Self { Lit::Number(s) @@ -430,6 +726,17 @@ pub enum StringLit { Single(T), } +impl IntoAllocated for StringLit where T: ToString { + type Allocated = StringLit; + + fn into_allocated(self) -> Self::Allocated { + match self { + StringLit::Double(inner) => StringLit::Double(inner.to_string()), + StringLit::Single(inner) => StringLit::Single(inner.to_string()), + } + } +} + impl StringLit { pub fn double_from(s: T) -> StringLit { StringLit::Double(s) @@ -470,6 +777,17 @@ pub struct RegEx { pub flags: Option, } +impl IntoAllocated for RegEx where T: ToString { + type Allocated = RegEx; + + fn into_allocated(self) -> Self::Allocated { + RegEx { + pattern: self.pattern.to_string(), + flags: self.flags.map(|f| f.to_string()) + } + } +} + impl RegEx { pub fn from(p: T, f: Option) -> Self { RegEx { diff --git a/src/lib.rs b/src/lib.rs index 64a1e6e..7ed6550 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,27 +27,31 @@ pub struct Ident { pub name: T, } +impl IntoAllocated for Ident where T: ToString { + type Allocated = Ident; + + fn into_allocated(self) -> Self::Allocated { + Ident { + name: self.name.to_string(), + } + } +} + impl<'a> From<&'a str> for Ident<&'a str> { fn from(value: &'a str) -> Self { - Self { - name: value, - } + Self { name: value } } } impl From for Ident { fn from(value: String) -> Self { - Self { - name: value, - } + Self { name: value } } } impl<'a> From> for Ident> { fn from(value: Cow<'a, str>) -> Self { - Self { - name: value, - } + Self { name: value } } } @@ -69,6 +73,17 @@ pub enum Program { Script(Vec>), } +impl IntoAllocated for Program where T: ToString { + type Allocated = Program; + + fn into_allocated(self) -> Self::Allocated { + match self { + Program::Mod(inner) => Program::Mod(inner.into_iter().map(|p| p.into_allocated()).collect()), + Program::Script(inner) => Program::Script(inner.into_iter().map(|p| p.into_allocated()).collect()), + } + } +} + impl Program { pub fn module(parts: Vec>) -> Self { Program::Mod(parts) @@ -96,6 +111,18 @@ pub enum ProgramPart { Stmt(Stmt), } +impl IntoAllocated for ProgramPart where T: ToString { + type Allocated = ProgramPart; + + fn into_allocated(self) -> Self::Allocated { + match self { + ProgramPart::Dir(inner) => ProgramPart::Dir(inner.into_allocated()), + ProgramPart::Decl(inner) => ProgramPart::Decl(inner.into_allocated()), + ProgramPart::Stmt(inner) => ProgramPart::Stmt(inner.into_allocated()), + } + } +} + impl ProgramPart { pub fn decl(inner: Decl) -> Self { ProgramPart::Decl(inner) @@ -118,6 +145,17 @@ pub struct Dir { pub dir: T, } +impl IntoAllocated for Dir where T: ToString { + type Allocated = Dir; + + fn into_allocated(self) -> Self::Allocated { + Dir { + expr: self.expr.into_allocated(), + dir: self.dir.to_string(), + } + } +} + /// A function, this will be part of either a function /// declaration (ID is required) or a function expression /// (ID is optional) @@ -138,6 +176,20 @@ pub struct Func { pub is_async: bool, } +impl IntoAllocated for Func where T: ToString { + type Allocated = Func; + + fn into_allocated(self) -> Self::Allocated { + Func { + id: self.id.map(IntoAllocated::into_allocated), + params: self.params.into_iter().map(|p| p.into_allocated()).collect(), + body: self.body.into_allocated(), + generator: self.generator, + is_async: self.is_async, + } + } +} + impl Func { pub fn new( id: Option>, @@ -169,6 +221,17 @@ pub enum FuncArg { Pat(Pat), } +impl IntoAllocated for FuncArg where T: ToString { + type Allocated = FuncArg; + + fn into_allocated(self) -> Self::Allocated { + match self { + FuncArg::Expr(inner) => FuncArg::Expr(inner.into_allocated()), + FuncArg::Pat(inner) => FuncArg::Pat(inner.into_allocated()), + } + } +} + impl FuncArg { pub fn expr(expr: Expr) -> FuncArg { FuncArg::Expr(expr) @@ -186,6 +249,15 @@ impl FuncArg { )] #[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct FuncBody(pub Vec>); + +impl IntoAllocated for FuncBody where T: ToString { + type Allocated = FuncBody; + + fn into_allocated(self) -> Self::Allocated { + FuncBody(self.0.into_iter().map(|p| p.into_allocated()).collect()) + } +} + /// A way to declare object templates /// ```js /// class Thing { @@ -219,6 +291,19 @@ pub struct Class { pub super_class: Option>>, pub body: ClassBody, } + +impl IntoAllocated for Class where T: ToString { + type Allocated = Class; + + fn into_allocated(self) -> Self::Allocated { + Class { + id: self.id.map(IntoAllocated::into_allocated), + super_class: self.super_class.map(IntoAllocated::into_allocated), + body: self.body.into_allocated(), + } + } +} + #[derive(Debug, Clone, PartialEq)] #[cfg_attr( all(feature = "serde", not(feature = "esprima")), @@ -227,6 +312,14 @@ pub struct Class { #[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct ClassBody(pub Vec>); +impl IntoAllocated for ClassBody where T: ToString { + type Allocated = ClassBody; + + fn into_allocated(self) -> Self::Allocated { + ClassBody(self.0.into_iter().map(IntoAllocated::into_allocated).collect()) + } +} + impl Class { pub fn new(id: Option>, super_class: Option>, body: Vec>) -> Class { Class { @@ -371,6 +464,27 @@ pub enum PropKind { Method, } +pub trait IntoAllocated { + type Allocated; + + fn into_allocated(self) -> Self::Allocated; +} + +impl IntoAllocated for Box where T: IntoAllocated { + type Allocated = Box; + + fn into_allocated(self) -> Self::Allocated { + Box::new((*self).into_allocated()) + } +} + +impl IntoAllocated for Option where T: IntoAllocated { + type Allocated = Option; + fn into_allocated(self) -> Self::Allocated { + self.map(IntoAllocated::into_allocated) + } +} + pub mod prelude { pub use crate::decl::{ Decl, DefaultExportDecl, ExportSpecifier, ImportSpecifier, ModExport, ModImport, diff --git a/src/pat.rs b/src/pat.rs index bde0a1f..bc7a66e 100644 --- a/src/pat.rs +++ b/src/pat.rs @@ -1,5 +1,5 @@ use crate::expr::{Expr, Prop}; -use crate::Ident; +use crate::{Ident, IntoAllocated}; /// All of the different ways you can declare an identifier /// and/or value #[derive(Debug, Clone, PartialEq)] @@ -17,11 +17,23 @@ pub enum Pat { Assign(AssignPat), } +impl IntoAllocated for Pat where T: ToString { + type Allocated = Pat; + + fn into_allocated(self) -> Self::Allocated { + match self { + Pat::Ident(inner) => Pat::Ident(inner.into_allocated()), + Pat::Obj(inner) => Pat::Obj(inner.into_iter().map(|a| a.into_allocated()).collect()), + Pat::Array(inner) => Pat::Array(inner.into_iter().map(|o| o.map(|a| a.into_allocated())).collect()), + Pat::RestElement(inner) => Pat::RestElement(inner.into_allocated()), + Pat::Assign(inner) => Pat::Assign(inner.into_allocated()), + } + } +} + impl Pat { pub fn ident_from(inner: T) -> Self { - Self::Ident(Ident { - name: inner, - }) + Self::Ident(Ident { name: inner }) } } @@ -33,6 +45,17 @@ pub enum ArrayPatPart { Expr(Expr), } +impl IntoAllocated for ArrayPatPart where T: ToString { + type Allocated = ArrayPatPart; + + fn into_allocated(self) -> Self::Allocated { + match self { + ArrayPatPart::Pat(inner) => ArrayPatPart::Pat(inner.into_allocated()), + ArrayPatPart::Expr(inner) => ArrayPatPart::Expr(inner.into_allocated()), + } + } +} + /// similar to an `ObjectExpr` pub type ObjPat = Vec>; /// A single part of an ObjectPat @@ -44,6 +67,17 @@ pub enum ObjPatPart { Rest(Box>), } +impl IntoAllocated for ObjPatPart where T: ToString { + type Allocated = ObjPatPart; + + fn into_allocated(self) -> Self::Allocated { + match self { + ObjPatPart::Assign(inner) => ObjPatPart::Assign(inner.into_allocated()), + ObjPatPart::Rest(inner) => ObjPatPart::Rest(inner.into_allocated()), + } + } +} + /// An assignment as a pattern #[derive(Debug, Clone, PartialEq)] #[cfg_attr( @@ -55,3 +89,14 @@ pub struct AssignPat { pub left: Box>, pub right: Box>, } + +impl IntoAllocated for AssignPat where T: ToString { + type Allocated = AssignPat; + + fn into_allocated(self) -> Self::Allocated { + AssignPat { + left: self.left.into_allocated(), + right: self.right.into_allocated(), + } + } +} diff --git a/src/spanned/convert.rs b/src/spanned/convert.rs index 7230c6d..76d3409 100644 --- a/src/spanned/convert.rs +++ b/src/spanned/convert.rs @@ -3,29 +3,27 @@ use std::borrow::Cow; -use crate::{ - spanned::{ - decl::{ - Alias, Decl, DefaultExportDeclValue, DefaultImportSpec, ExportSpecifier, - ImportSpecifier, ModExport, ModExportSpecifier, ModImport, NamedExportDecl, - NamespaceImportSpec, NormalImportSpec, VarDecl, - }, - expr::{ - ArrowFuncBody, ArrowFuncExpr, AssignExpr, AssignLeft, BinaryExpr, CallExpr, - ConditionalExpr, Expr, Lit, LogicalExpr, MemberExpr, MetaProp, NewExpr, ObjProp, Prop, - PropInitKey, PropKey, PropMethod, PropValue, RegEx, SequenceExprEntry, StringLit, - TaggedTemplateExpr, TemplateElement, TemplateLit, UnaryExpr, UpdateExpr, YieldExpr, - }, - pat::{ArrayElement, ArrayPat, ArrayPatPart, AssignPat, ObjPat, ObjPatPart, Pat}, - stmt::{ - BlockStmt, CatchClause, DoWhileStmt, FinallyClause, ForInStmt, ForOfStmt, ForStmt, - IfStmt, LabeledStmt, LoopInit, LoopLeft, Stmt, SwitchCase, SwitchStmt, TryStmt, - WhileStmt, WithStmt, - }, - tokens::{AssignOp, BinaryOp, LogicalOp, UnaryOp, UpdateOp}, - Class, ClassBody, Dir, Func, FuncArg, FuncArgEntry, FuncBody, Ident, Program, ProgramPart, - Slice, VarKind, +use crate::spanned::{ + decl::{ + Alias, Decl, DefaultExportDeclValue, DefaultImportSpec, ExportSpecifier, ImportSpecifier, + ModExport, ModExportSpecifier, ModImport, NamedExportDecl, NamespaceImportSpec, + NormalImportSpec, VarDecl, }, + expr::{ + ArrowFuncBody, ArrowFuncExpr, AssignExpr, AssignLeft, BinaryExpr, CallExpr, + ConditionalExpr, Expr, Lit, LogicalExpr, MemberExpr, MetaProp, NewExpr, ObjProp, Prop, + PropInitKey, PropKey, PropMethod, PropValue, RegEx, SequenceExprEntry, StringLit, + TaggedTemplateExpr, TemplateElement, TemplateLit, UnaryExpr, UpdateExpr, YieldExpr, + }, + pat::{ArrayElement, ArrayPat, ArrayPatPart, AssignPat, ObjPat, ObjPatPart, Pat}, + stmt::{ + BlockStmt, CatchClause, DoWhileStmt, FinallyClause, ForInStmt, ForOfStmt, ForStmt, IfStmt, + LabeledStmt, LoopInit, LoopLeft, Stmt, SwitchCase, SwitchStmt, TryStmt, WhileStmt, + WithStmt, + }, + tokens::{AssignOp, BinaryOp, LogicalOp, UnaryOp, UpdateOp}, + Class, ClassBody, Dir, Func, FuncArg, FuncArgEntry, FuncBody, Ident, Program, ProgramPart, + Slice, VarKind, }; mod decl { diff --git a/src/spanned/decl.rs b/src/spanned/decl.rs index 89fb7db..13e452b 100644 --- a/src/spanned/decl.rs +++ b/src/spanned/decl.rs @@ -1,3 +1,4 @@ +use crate::IntoAllocated; use crate::spanned::expr::{Expr, Lit}; use crate::spanned::pat::Pat; use crate::spanned::VarKind; @@ -50,6 +51,26 @@ pub enum Decl { }, } + +impl IntoAllocated for Decl +where + T: ToString, +{ + type Allocated = Decl; + fn into_allocated(self) -> Decl { + match self { + Decl::Var { decls, semi_colon } => Decl::Var { + decls: decls.into_allocated(), + semi_colon: semi_colon, + }, + Decl::Func(f) => Decl::Func(f.into_allocated()), + Decl::Class(c) => Decl::Class(c.into_allocated()), + Decl::Import { import, semi_colon } => Decl::Import { import: import.into_allocated(), semi_colon }, + Decl::Export { export, semi_colon } => Decl::Export { export: export.into_allocated(), semi_colon }, + } + } +} + impl Node for Decl { fn loc(&self) -> super::SourceLocation { match self { @@ -92,6 +113,20 @@ pub struct VarDecls { pub decls: Vec>>, } + +impl IntoAllocated for VarDecls +where + T: ToString, +{ + type Allocated = VarDecls; + fn into_allocated(self) -> VarDecls { + VarDecls { + keyword: self.keyword, + decls: self.decls.into_iter().map(|d| d.into_allocated()).collect(), + } + } +} + impl Node for VarDecls { fn loc(&self) -> SourceLocation { if let Some(last) = self.decls.last() { @@ -113,6 +148,21 @@ pub struct VarDecl { pub init: Option>, } + +impl IntoAllocated for VarDecl +where + T: ToString, +{ + type Allocated = VarDecl; + fn into_allocated(self) -> VarDecl { + VarDecl { + id: self.id.into_allocated(), + eq: self.eq, + init: self.init.map(|i| i.into_allocated()), + } + } +} + impl Node for VarDecl { fn loc(&self) -> SourceLocation { if let Some(init) = &self.init { @@ -135,6 +185,16 @@ pub enum ModDecl { Export(ModExport), } +impl IntoAllocated for ModDecl where T: ToString { + type Allocated = ModDecl; + fn into_allocated(self) -> ModDecl { + match self { + ModDecl::Import(inner) => ModDecl::Import(inner.into_allocated()), + ModDecl::Export(inner) => ModDecl::Export(inner.into_allocated()), + } + } +} + impl Node for ModDecl { fn loc(&self) -> SourceLocation { match self { @@ -158,6 +218,13 @@ pub struct ModImport { pub source: Lit, } +impl IntoAllocated for ModImport where T: ToString { + type Allocated = ModImport; + fn into_allocated(self) -> ModImport { + ModImport { keyword_import:self.keyword_import, specifiers: self.specifiers.into_iter().map(|s| s.into_allocated()).collect(), keyword_from: self.keyword_from, source: self.source.into_allocated() } + } +} + impl Node for ModImport { fn loc(&self) -> SourceLocation { SourceLocation { @@ -196,6 +263,17 @@ pub enum ImportSpecifier { Namespace(NamespaceImportSpec), } +impl IntoAllocated for ImportSpecifier where T: ToString { + type Allocated = ImportSpecifier; + fn into_allocated(self) -> ImportSpecifier { + match self { + ImportSpecifier::Normal(inner) => ImportSpecifier::Normal(inner.into_allocated()), + ImportSpecifier::Default(inner) => ImportSpecifier::Default(inner.into_allocated()), + ImportSpecifier::Namespace(inner) => ImportSpecifier::Namespace(inner.into_allocated()), + } + } +} + impl Node for ImportSpecifier { fn loc(&self) -> SourceLocation { match self { @@ -213,6 +291,17 @@ pub struct NormalImportSpecs { pub close_brace: CloseBrace, } +impl IntoAllocated for NormalImportSpecs where T: ToString { + type Allocated = NormalImportSpecs; + fn into_allocated(self) -> NormalImportSpecs { + NormalImportSpecs { + open_brace: self.open_brace, + specs: self.specs.into_iter().map(|s| s.into_allocated()).collect(), + close_brace: self.close_brace, + } + } +} + impl Node for NormalImportSpecs { fn loc(&self) -> SourceLocation { SourceLocation { @@ -228,6 +317,13 @@ pub struct NormalImportSpec { pub alias: Option>, } +impl IntoAllocated for NormalImportSpec where T: ToString { + type Allocated = NormalImportSpec; + fn into_allocated(self) -> NormalImportSpec { + NormalImportSpec { imported: self.imported.into_allocated(), alias: self.alias.map(|a| a.into_allocated()) } + } +} + impl Node for NormalImportSpec { fn loc(&self) -> SourceLocation { if let Some(alias) = &self.alias { @@ -246,6 +342,13 @@ pub struct DefaultImportSpec { pub id: Ident, } +impl IntoAllocated for DefaultImportSpec where T: ToString { + type Allocated = DefaultImportSpec; + fn into_allocated(self) -> DefaultImportSpec { + DefaultImportSpec { id: self.id.into_allocated() } + } +} + impl Node for DefaultImportSpec { fn loc(&self) -> SourceLocation { self.id.loc() @@ -259,6 +362,17 @@ pub struct NamespaceImportSpec { pub ident: Ident, } +impl IntoAllocated for NamespaceImportSpec where T: ToString { + type Allocated = NamespaceImportSpec; + fn into_allocated(self) -> NamespaceImportSpec { + NamespaceImportSpec { + star: self.star, + keyword: self.keyword, + ident: self.ident.into_allocated(), + } + } +} + impl Node for NamespaceImportSpec { fn loc(&self) -> SourceLocation { SourceLocation { @@ -274,6 +388,13 @@ pub struct ModExport { pub spec: ModExportSpecifier, } +impl IntoAllocated for ModExport where T: ToString { + type Allocated = ModExport; + fn into_allocated(self) -> ModExport { + ModExport { keyword: self.keyword, spec: self.spec.into_allocated() } + } +} + impl Node for ModExport { fn loc(&self) -> SourceLocation { SourceLocation { @@ -317,6 +438,17 @@ pub enum ModExportSpecifier { }, } +impl IntoAllocated for ModExportSpecifier where T: ToString { + type Allocated = ModExportSpecifier; + fn into_allocated(self) -> ModExportSpecifier { + match self { + ModExportSpecifier::Default { keyword, value } => ModExportSpecifier::Default {keyword, value: value.into_allocated()}, + ModExportSpecifier::Named(inner) => ModExportSpecifier::Named(inner.into_allocated()), + ModExportSpecifier::All { star, alias, keyword, name } => ModExportSpecifier::All { star, alias: alias.map(|a| a.into_allocated()), keyword: keyword, name: name.into_allocated()}, + } + } +} + impl Node for ModExportSpecifier { fn loc(&self) -> SourceLocation { match self { @@ -343,6 +475,16 @@ pub enum NamedExportDecl { Specifier(NamedExportSpec), } +impl IntoAllocated for NamedExportDecl where T: ToString { + type Allocated = NamedExportDecl; + fn into_allocated(self) -> NamedExportDecl { + match self { + NamedExportDecl::Decl(inner) => NamedExportDecl::Decl(inner.into_allocated()), + NamedExportDecl::Specifier(inner) => NamedExportDecl::Specifier(inner.into_allocated()), + } + } +} + impl Node for NamedExportDecl { fn loc(&self) -> SourceLocation { match self { @@ -358,6 +500,16 @@ pub struct DefaultExportDecl { pub value: DefaultExportDeclValue, } +impl IntoAllocated for DefaultExportDecl where T: ToString { + type Allocated = DefaultExportDecl; + fn into_allocated(self) -> DefaultExportDecl { + DefaultExportDecl { + keyword: self.keyword, + value: self.value.into_allocated(), + } + } +} + impl Node for DefaultExportDecl { fn loc(&self) -> SourceLocation { SourceLocation { @@ -378,6 +530,17 @@ pub enum ExportDeclValue { List(ExportList), } +impl IntoAllocated for ExportDeclValue where T: ToString { + type Allocated = ExportDeclValue; + fn into_allocated(self) -> ExportDeclValue { + match self { + ExportDeclValue::Decl(inner) => ExportDeclValue::Decl(inner.into_allocated()), + ExportDeclValue::Expr(inner) => ExportDeclValue::Expr(inner.into_allocated()), + ExportDeclValue::List(inner) => ExportDeclValue::List(inner.into_allocated()), + } + } +} + impl Node for ExportDeclValue { fn loc(&self) -> SourceLocation { match self { @@ -394,6 +557,16 @@ pub enum DefaultExportDeclValue { Expr(Expr), } +impl IntoAllocated for DefaultExportDeclValue where T: ToString { + type Allocated = DefaultExportDeclValue; + fn into_allocated(self) -> DefaultExportDeclValue { + match self { + DefaultExportDeclValue::Decl(inner) => DefaultExportDeclValue::Decl(inner.into_allocated()), + DefaultExportDeclValue::Expr(inner) => DefaultExportDeclValue::Expr(inner.into_allocated()), + } + } +} + impl Node for DefaultExportDeclValue { fn loc(&self) -> SourceLocation { match self { @@ -409,6 +582,16 @@ pub struct NamedExportSpec { pub source: Option>, } +impl IntoAllocated for NamedExportSpec where T: ToString { + type Allocated = NamedExportSpec; + fn into_allocated(self) -> NamedExportSpec { + NamedExportSpec { + list: self.list.into_allocated(), + source: self.source.map(|s| s.into_allocated()), + } + } +} + impl Node for NamedExportSpec { fn loc(&self) -> SourceLocation { if let Some(source) = &self.source { @@ -428,6 +611,16 @@ pub struct NamedExportSource { pub module: Lit, } +impl IntoAllocated for NamedExportSource where T: ToString { + type Allocated = NamedExportSource; + fn into_allocated(self) -> NamedExportSource { + NamedExportSource { + keyword_from: self.keyword_from, + module: self.module.into_allocated(), + } + } +} + impl Node for NamedExportSource { fn loc(&self) -> SourceLocation { SourceLocation { @@ -444,6 +637,13 @@ pub struct ExportList { pub close_brace: CloseBrace, } +impl IntoAllocated for ExportList where T: ToString { + type Allocated = ExportList; + fn into_allocated(self) -> ExportList { + ExportList { open_brace: self.open_brace, elements: self.elements.into_iter().map(|e| e.into_allocated()).collect(), close_brace: self.close_brace } + } +} + impl Node for ExportList { fn loc(&self) -> SourceLocation { SourceLocation { @@ -467,6 +667,17 @@ pub struct ExportSpecifier { pub alias: Option>, } +impl IntoAllocated for ExportSpecifier where T: ToString { + type Allocated = ExportSpecifier; + + fn into_allocated(self) -> Self::Allocated { + ExportSpecifier { + local: self.local.into_allocated(), + alias: self.alias.map(|a| a.into_allocated()), + } + } +} + impl Node for ExportSpecifier { fn loc(&self) -> SourceLocation { if let Some(alias) = &self.alias { @@ -486,6 +697,17 @@ pub struct Alias { pub ident: Ident, } +impl IntoAllocated for Alias where T: ToString { + type Allocated = Alias; + + fn into_allocated(self) -> Self::Allocated { + Alias { + keyword: self.keyword, + ident: self.ident.into_allocated(), + } + } +} + impl Node for Alias { fn loc(&self) -> SourceLocation { SourceLocation { diff --git a/src/spanned/expr.rs b/src/spanned/expr.rs index a7ae64d..e352ac0 100644 --- a/src/spanned/expr.rs +++ b/src/spanned/expr.rs @@ -1,3 +1,4 @@ +use crate::IntoAllocated; use crate::spanned::pat::Pat; use crate::spanned::{Class, Func, FuncArg, FuncBody, Ident}; @@ -95,6 +96,40 @@ pub enum Expr { Yield(YieldExpr), } +impl IntoAllocated for Expr where T: ToString { + type Allocated = Expr; + fn into_allocated(self) -> Self::Allocated { + match self { + Expr::Array(inner) => Expr::Array(inner.into_allocated()), + Expr::ArrowFunc(inner) => Expr::ArrowFunc(inner.into_allocated()), + Expr::ArrowParamPlaceHolder(inner) => Expr::ArrowParamPlaceHolder(inner.into_allocated()), + Expr::Assign(inner) => Expr::Assign(inner.into_allocated()), + Expr::Await(inner) => Expr::Await(inner.into_allocated()), + Expr::Binary(inner) => Expr::Binary(inner.into_allocated()), + Expr::Class(inner) => Expr::Class(inner.into_allocated()), + Expr::Call(inner) => Expr::Call(inner.into_allocated()), + Expr::Conditional(inner) => Expr::Conditional(inner.into_allocated()), + Expr::Func(inner) => Expr::Func(inner.into_allocated()), + Expr::Ident(inner) => Expr::Ident(inner.into_allocated()), + Expr::Lit(inner) => Expr::Lit(inner.into_allocated()), + Expr::Logical(inner) => Expr::Logical(inner.into_allocated()), + Expr::Member(inner) => Expr::Member(inner.into_allocated()), + Expr::MetaProp(inner) => Expr::MetaProp(inner.into_allocated()), + Expr::New(inner) => Expr::New(inner.into_allocated()), + Expr::Obj(inner) => Expr::Obj(inner.into_allocated()), + Expr::Sequence(inner) => Expr::Sequence(inner.into_iter().map(IntoAllocated::into_allocated).collect()), + Expr::Spread(inner) => Expr::Spread(inner.into_allocated()), + Expr::Super(inner) => Expr::Super(inner), + Expr::TaggedTemplate(inner) => Expr::TaggedTemplate(inner.into_allocated()), + Expr::This(inner) => Expr::This(inner), + Expr::Unary(inner) => Expr::Unary(inner.into_allocated()), + Expr::Update(inner) => Expr::Update(inner.into_allocated()), + Expr::Wrapped(inner) => Expr::Wrapped(inner.into_allocated()), + Expr::Yield(inner) => Expr::Yield(inner.into_allocated()), + } + } +} + impl Node for Expr { fn loc(&self) -> SourceLocation { match self { @@ -138,6 +173,17 @@ pub struct ArrayExpr { pub close_bracket: CloseBracket, } +impl IntoAllocated for ArrayExpr where T: ToString { + type Allocated = ArrayExpr; + fn into_allocated(self) -> Self::Allocated { + ArrayExpr { + open_bracket: self.open_bracket, + elements: self.elements.into_iter().map(IntoAllocated::into_allocated).collect(), + close_bracket: self.close_bracket, + } + } +} + impl Node for ArrayExpr { fn loc(&self) -> SourceLocation { SourceLocation { @@ -155,6 +201,18 @@ pub struct ObjExpr { pub close_brace: CloseBrace, } +impl IntoAllocated for ObjExpr where T: ToString { + type Allocated = ObjExpr; + + fn into_allocated(self) -> Self::Allocated { + ObjExpr { + open_brace: self.open_brace, + props: self.props.into_iter().map(IntoAllocated::into_allocated).collect(), + close_brace: self.close_brace, + } + } +} + impl Node for ObjExpr { fn loc(&self) -> SourceLocation { SourceLocation { @@ -171,6 +229,16 @@ pub enum ObjProp { Spread(SpreadExpr), } +impl IntoAllocated for ObjProp where T: ToString { + type Allocated = ObjProp; + fn into_allocated(self) -> Self::Allocated { + match self { + ObjProp::Prop(inner) => ObjProp::Prop(inner.into_allocated()), + ObjProp::Spread(inner) => ObjProp::Spread(inner.into_allocated()), + } + } +} + impl Node for ObjProp { fn loc(&self) -> SourceLocation { match self { @@ -186,6 +254,16 @@ pub struct SpreadExpr { pub expr: Expr, } +impl IntoAllocated for SpreadExpr where T: ToString { + type Allocated = SpreadExpr; + fn into_allocated(self) -> Self::Allocated { + SpreadExpr { + dots: self.dots, + expr: self.expr.into_allocated(), + } + } +} + impl Node for SpreadExpr { fn loc(&self) -> SourceLocation { SourceLocation { @@ -204,6 +282,19 @@ pub enum Prop { Set(PropSet), } +impl IntoAllocated for Prop where T: ToString { + type Allocated = Prop; + fn into_allocated(self) -> Self::Allocated { + match self { + Prop::Init(inner) => Prop::Init(inner.into_allocated()), + Prop::Method(inner) => Prop::Method(inner.into_allocated()), + Prop::Ctor(inner) => Prop::Ctor(inner.into_allocated()), + Prop::Get(inner) => Prop::Get(inner.into_allocated()), + Prop::Set(inner) => Prop::Set(inner.into_allocated()), + } + } +} + impl Node for Prop { fn loc(&self) -> SourceLocation { match self { @@ -247,6 +338,17 @@ pub struct PropInit { pub value: Option>, } +impl IntoAllocated for PropInit where T: ToString { + type Allocated = PropInit; + fn into_allocated(self) -> Self::Allocated { + PropInit { + key: self.key.into_allocated(), + colon: self.colon, + value: self.value.into_allocated() + } + } +} + impl Node for PropInit { fn loc(&self) -> SourceLocation { if let Some(value) = &self.value { @@ -275,6 +377,16 @@ pub struct PropInitKey { pub brackets: Option<(OpenBracket, CloseBracket)>, } +impl IntoAllocated for PropInitKey where T: ToString { + type Allocated = PropInitKey; + fn into_allocated(self) -> Self::Allocated { + PropInitKey { + value: self.value.into_allocated(), + brackets: self.brackets, + } + } +} + impl Node for PropInitKey { fn loc(&self) -> SourceLocation { if let Some((open, close)) = &self.brackets { @@ -300,6 +412,22 @@ pub struct PropMethod { pub body: FuncBody, } +impl IntoAllocated for PropMethod where T: ToString { + type Allocated = PropMethod; + fn into_allocated(self) -> Self::Allocated { + PropMethod { + keyword_static: self.keyword_static, + keyword_async: self.keyword_async, + id: self.id.into_allocated(), + star: self.star, + open_paren: self.open_paren, + params: self.params.into_iter().map(IntoAllocated::into_allocated).collect(), + close_paren: self.close_paren, + body: self.body.into_allocated(), + } + } +} + impl Node for PropMethod { fn loc(&self) -> SourceLocation { let start = if let Some(keyword) = &self.keyword_async { @@ -325,6 +453,19 @@ pub struct PropCtor { pub body: FuncBody, } +impl IntoAllocated for PropCtor where T: ToString { + type Allocated = PropCtor; + fn into_allocated(self) -> Self::Allocated { + PropCtor { + keyword: self.keyword.into_allocated(), + open_paren: self.open_paren, + params: self.params.into_iter().map(IntoAllocated::into_allocated).collect(), + close_paren: self.close_paren, + body: self.body.into_allocated(), + } + } +} + impl Node for PropCtor { fn loc(&self) -> SourceLocation { SourceLocation { @@ -344,6 +485,20 @@ pub struct PropGet { pub body: FuncBody, } +impl IntoAllocated for PropGet where T: ToString { + type Allocated = PropGet; + fn into_allocated(self) -> Self::Allocated { + PropGet { + keyword_static: self.keyword_static, + keyword_get: self.keyword_get, + id: self.id.into_allocated(), + open_paren: self.open_paren, + close_paren: self.close_paren, + body: self.body.into_allocated(), + } + } +} + impl Node for PropGet { fn loc(&self) -> SourceLocation { if let Some(keyword_static) = &self.keyword_static { @@ -370,6 +525,21 @@ pub struct PropSet { pub body: FuncBody, } +impl IntoAllocated for PropSet where T: ToString { + type Allocated = PropSet; + fn into_allocated(self) -> Self::Allocated { + PropSet { + keyword_static: self.keyword_static, + keyword_set: self.keyword_set, + id: self.id.into_allocated(), + open_paren: self.open_paren, + arg: self.arg.into_allocated(), + close_paren: self.close_paren, + body: self.body.into_allocated(), + } + } +} + impl Node for PropSet { fn loc(&self) -> SourceLocation { if let Some(keyword_static) = &self.keyword_static { @@ -393,6 +563,17 @@ pub enum PropKey { Pat(Pat), } +impl IntoAllocated for PropKey where T: ToString { + type Allocated = PropKey; + fn into_allocated(self) -> Self::Allocated { + match self { + PropKey::Lit(inner) => PropKey::Lit(inner.into_allocated()), + PropKey::Expr(inner) => PropKey::Expr(inner.into_allocated()), + PropKey::Pat(inner) => PropKey::Pat(inner.into_allocated()), + } + } +} + impl Node for PropKey { fn loc(&self) -> SourceLocation { match self { @@ -411,6 +592,17 @@ pub enum PropValue { Method(PropMethod), } +impl IntoAllocated for PropValue where T: ToString { + type Allocated = PropValue; + fn into_allocated(self) -> Self::Allocated { + match self { + PropValue::Expr(inner) => PropValue::Expr(inner.into_allocated()), + PropValue::Pat(inner) => PropValue::Pat(inner.into_allocated()), + PropValue::Method(inner) => PropValue::Method(inner.into_allocated()), + } + } +} + impl Node for PropValue { fn loc(&self) -> SourceLocation { match self { @@ -428,6 +620,16 @@ pub struct UnaryExpr { pub argument: Box>, } +impl IntoAllocated for UnaryExpr where T: ToString { + type Allocated = UnaryExpr; + fn into_allocated(self) -> Self::Allocated { + UnaryExpr { + operator: self.operator, + argument: self.argument.into_allocated(), + } + } +} + impl UnaryExpr { pub fn prefix(&self) -> bool { self.operator.loc() < self.argument.loc() @@ -452,6 +654,16 @@ pub struct UpdateExpr { pub argument: Box>, } +impl IntoAllocated for UpdateExpr where T: ToString { + type Allocated = UpdateExpr; + fn into_allocated(self) -> Self::Allocated { + UpdateExpr { + operator: self.operator, + argument: self.argument.into_allocated(), + } + } +} + impl UpdateExpr { pub fn prefix(&self) -> bool { self.operator.loc().start < self.argument.loc().start @@ -484,6 +696,17 @@ pub struct BinaryExpr { pub right: Box>, } +impl IntoAllocated for BinaryExpr where T: ToString { + type Allocated = BinaryExpr; + fn into_allocated(self) -> Self::Allocated { + BinaryExpr { + operator: self.operator, + left: self.left.into_allocated(), + right: self.right.into_allocated(), + } + } +} + impl Node for BinaryExpr { fn loc(&self) -> SourceLocation { SourceLocation { @@ -501,6 +724,17 @@ pub struct AssignExpr { pub right: Box>, } +impl IntoAllocated for AssignExpr where T: ToString { + type Allocated = AssignExpr; + fn into_allocated(self) -> Self::Allocated { + AssignExpr { + operator: self.operator, + left: self.left.into_allocated(), + right: self.right.into_allocated(), + } + } +} + impl Node for AssignExpr { fn loc(&self) -> SourceLocation { SourceLocation { @@ -516,6 +750,16 @@ pub struct AwaitExpr { pub expr: Expr, } +impl IntoAllocated for AwaitExpr where T: ToString { + type Allocated = AwaitExpr; + fn into_allocated(self) -> Self::Allocated { + AwaitExpr { + keyword: self.keyword, + expr: self.expr.into_allocated(), + } + } +} + impl Node for AwaitExpr { fn loc(&self) -> SourceLocation { SourceLocation { @@ -532,6 +776,16 @@ pub enum AssignLeft { Expr(Box>), } +impl IntoAllocated for AssignLeft where T: ToString { + type Allocated = AssignLeft; + fn into_allocated(self) -> Self::Allocated { + match self { + AssignLeft::Pat(inner) => AssignLeft::Pat(inner.into_allocated()), + AssignLeft::Expr(inner) => AssignLeft::Expr(inner.into_allocated()), + } + } +} + impl Node for AssignLeft { fn loc(&self) -> SourceLocation { match self { @@ -553,6 +807,17 @@ pub struct LogicalExpr { pub right: Box>, } +impl IntoAllocated for LogicalExpr where T: ToString { + type Allocated = LogicalExpr; + fn into_allocated(self) -> Self::Allocated { + LogicalExpr { + operator: self.operator, + left: self.left.into_allocated(), + right: self.right.into_allocated(), + } + } +} + impl Node for LogicalExpr { fn loc(&self) -> SourceLocation { SourceLocation { @@ -574,6 +839,18 @@ pub struct MemberExpr { pub indexer: MemberIndexer, } +impl IntoAllocated for MemberExpr where T: ToString { + type Allocated = MemberExpr; + + fn into_allocated(self) -> Self::Allocated { + MemberExpr { + object: self.object.into_allocated(), + property: self.property.into_allocated(), + indexer: self.indexer + } + } +} + impl MemberExpr { pub fn computed(&self) -> bool { matches!(self.indexer, MemberIndexer::Computed { .. }) @@ -593,7 +870,7 @@ impl Node for MemberExpr { } } -#[derive(PartialEq, Debug, Clone)] +#[derive(PartialEq, Debug, Clone, Copy)] pub enum MemberIndexer { Period(Period), Computed { @@ -630,6 +907,19 @@ pub struct ConditionalExpr { pub consequent: Box>, } +impl IntoAllocated for ConditionalExpr where T: ToString { + type Allocated = ConditionalExpr; + fn into_allocated(self) -> Self::Allocated { + ConditionalExpr { + test: self.test.into_allocated(), + question_mark: self.question_mark, + alternate: self.alternate.into_allocated(), + colon: self.colon, + consequent: self.consequent.into_allocated(), + } + } +} + impl Node for ConditionalExpr { fn loc(&self) -> SourceLocation { let start = self.test.loc().start; @@ -650,6 +940,19 @@ pub struct CallExpr { pub close_paren: CloseParen, } +impl IntoAllocated for CallExpr where T: ToString { + type Allocated = CallExpr; + + fn into_allocated(self) -> Self::Allocated { + CallExpr { + callee: self.callee.into_allocated(), + open_paren: self.open_paren, + arguments: self.arguments.into_iter().map(IntoAllocated::into_allocated).collect(), + close_paren: self.close_paren, + } + } +} + impl Node for CallExpr { fn loc(&self) -> SourceLocation { SourceLocation { @@ -672,6 +975,20 @@ pub struct NewExpr { pub close_paren: Option, } +impl IntoAllocated for NewExpr where T: ToString { + type Allocated = NewExpr; + + fn into_allocated(self) -> Self::Allocated { + NewExpr { + keyword: self.keyword, + callee: self.callee.into_allocated(), + open_paren: self.open_paren, + arguments: self.arguments.into_iter().map(IntoAllocated::into_allocated).collect(), + close_paren: self.close_paren, + } + } +} + impl Node for NewExpr { fn loc(&self) -> SourceLocation { let end = if let Some(close) = &self.close_paren { @@ -719,6 +1036,18 @@ pub struct ArrowParamPlaceHolder { pub close_paren: Option, } +impl IntoAllocated for ArrowParamPlaceHolder where T: ToString { + type Allocated = ArrowParamPlaceHolder; + fn into_allocated(self) -> Self::Allocated { + ArrowParamPlaceHolder { + keyword: self.keyword, + open_paren: self.open_paren, + args: self.args.into_iter().map(IntoAllocated::into_allocated).collect(), + close_paren: self.close_paren, + } + } +} + impl Node for ArrowParamPlaceHolder { fn loc(&self) -> SourceLocation { let start = if let Some(keyword) = &self.keyword { @@ -759,6 +1088,21 @@ pub struct ArrowFuncExpr { pub body: ArrowFuncBody, } +impl IntoAllocated for ArrowFuncExpr where T: ToString { + type Allocated = ArrowFuncExpr; + fn into_allocated(self) -> Self::Allocated { + ArrowFuncExpr { + keyword: self.keyword, + star: self.star, + open_paren: self.open_paren, + params: self.params.into_iter().map(IntoAllocated::into_allocated).collect(), + close_paren: self.close_paren, + arrow: self.arrow, + body: self.body.into_allocated() + } + } +} + impl Node for ArrowFuncExpr { fn loc(&self) -> SourceLocation { let start = if let Some(keyword) = &self.keyword { @@ -784,6 +1128,16 @@ pub enum ArrowFuncBody { Expr(Box>), } +impl IntoAllocated for ArrowFuncBody where T: ToString { + type Allocated = ArrowFuncBody; + fn into_allocated(self) -> Self::Allocated { + match self { + ArrowFuncBody::FuncBody(inner) => ArrowFuncBody::FuncBody(inner.into_allocated()), + ArrowFuncBody::Expr(inner) => ArrowFuncBody::Expr(inner.into_allocated()), + } + } +} + impl Node for ArrowFuncBody { fn loc(&self) -> SourceLocation { match self { @@ -808,6 +1162,17 @@ pub struct YieldExpr { pub star: Option, } +impl IntoAllocated for YieldExpr where T: ToString { + type Allocated = YieldExpr; + fn into_allocated(self) -> Self::Allocated { + YieldExpr { + keyword: self.keyword, + argument: self.argument.into_allocated(), + star: self.star, + } + } +} + impl Node for YieldExpr { fn loc(&self) -> SourceLocation { let end = if let Some(arg) = &self.argument { @@ -830,6 +1195,16 @@ pub struct TaggedTemplateExpr { pub quasi: TemplateLit, } +impl IntoAllocated for TaggedTemplateExpr where T: ToString { + type Allocated = TaggedTemplateExpr; + fn into_allocated(self) -> Self::Allocated { + TaggedTemplateExpr { + tag: self.tag.into_allocated(), + quasi: self.quasi.into_allocated(), + } + } +} + impl Node for TaggedTemplateExpr { fn loc(&self) -> SourceLocation { SourceLocation { @@ -849,6 +1224,17 @@ pub struct TemplateLit { pub expressions: Vec>, } +impl IntoAllocated for TemplateLit where T: ToString { + type Allocated = TemplateLit; + fn into_allocated(self) -> Self::Allocated { + TemplateLit { + quasis: self.quasis.into_iter().map(IntoAllocated::into_allocated).collect(), + expressions: self.expressions.into_iter().map(IntoAllocated::into_allocated).collect(), + } + } +} + + impl Node for TemplateLit { fn loc(&self) -> SourceLocation { let start = self @@ -876,6 +1262,17 @@ pub struct TemplateElement { pub close_quote: QuasiQuote, } +impl IntoAllocated for TemplateElement where T: ToString { + type Allocated = TemplateElement; + fn into_allocated(self) -> Self::Allocated { + TemplateElement { + open_quote: self.open_quote, + content: self.content.into_allocated(), + close_quote: self.close_quote, + } + } +} + impl TemplateElement where T: AsRef, @@ -914,6 +1311,17 @@ pub struct MetaProp { pub property: Ident, } +impl IntoAllocated for MetaProp where T: ToString { + type Allocated = MetaProp; + fn into_allocated(self) -> Self::Allocated { + MetaProp { + meta: self.meta.into_allocated(), + dot: self.dot, + property: self.property.into_allocated(), + } + } +} + impl Node for MetaProp { fn loc(&self) -> SourceLocation { SourceLocation { @@ -951,6 +1359,20 @@ pub enum Lit { Template(TemplateLit), } +impl IntoAllocated for Lit where T: ToString { + type Allocated = Lit; + fn into_allocated(self) -> Self::Allocated { + match self { + Lit::Null(inner) => Lit::Null(inner), + Lit::String(inner) => Lit::String(inner.into_allocated()), + Lit::Number(inner) => Lit::Number(inner.into_allocated()), + Lit::Boolean(inner) => Lit::Boolean(inner), + Lit::RegEx(inner) => Lit::RegEx(inner.into_allocated()), + Lit::Template(inner) => Lit::Template(inner.into_allocated()), + } + } +} + impl Lit { pub fn new_true(line: u32, column: u32) -> Self { Self::Boolean(Boolean::new_true(line, column)) @@ -1020,6 +1442,17 @@ pub struct StringLit { pub close_quote: Quote, } +impl IntoAllocated for StringLit where T: ToString { + type Allocated = StringLit; + fn into_allocated(self) -> Self::Allocated { + StringLit { + open_quote: self.open_quote, + content: self.content.into_allocated(), + close_quote: self.close_quote, + } + } +} + impl Node for StringLit { fn loc(&self) -> SourceLocation { SourceLocation { @@ -1038,6 +1471,18 @@ pub struct RegEx { pub flags: Option>, } +impl IntoAllocated for RegEx where T: ToString { + type Allocated = RegEx; + fn into_allocated(self) -> Self::Allocated { + RegEx { + open_slash: self.open_slash, + pattern: self.pattern.into_allocated(), + close_slash: self.close_slash, + flags: self.flags.into_allocated(), + } + } +} + impl Node for RegEx { fn loc(&self) -> SourceLocation { let end = if let Some(flags) = &self.flags { @@ -1059,6 +1504,17 @@ pub struct WrappedExpr { pub close_paren: CloseParen, } +impl IntoAllocated for WrappedExpr where T: ToString { + type Allocated = WrappedExpr; + fn into_allocated(self) -> Self::Allocated { + WrappedExpr { + open_paren: self.open_paren, + expr: self.expr.into_allocated(), + close_paren: self.close_paren, + } + } +} + impl Node for WrappedExpr { fn loc(&self) -> SourceLocation { SourceLocation { @@ -1074,6 +1530,16 @@ pub struct SequenceExprEntry { pub comma: Option, } +impl IntoAllocated for SequenceExprEntry where T: ToString { + type Allocated = SequenceExprEntry; + fn into_allocated(self) -> Self::Allocated { + SequenceExprEntry { + expr: self.expr.into_allocated(), + comma: self.comma, + } + } +} + impl SequenceExprEntry { pub fn no_comma(expr: Expr) -> Self { Self { expr, comma: None } diff --git a/src/spanned/mod.rs b/src/spanned/mod.rs index 3fe67da..5b20bc6 100644 --- a/src/spanned/mod.rs +++ b/src/spanned/mod.rs @@ -10,6 +10,8 @@ use expr::{Expr, Lit, Prop}; use pat::Pat; use stmt::Stmt; +use crate::IntoAllocated; + use self::{ pat::RestPat, tokens::{ @@ -37,6 +39,19 @@ where } } + +impl IntoAllocated for Ident +where + T: ToString, +{ + type Allocated = Ident; + fn into_allocated(self) -> Ident { + Ident { + slice: self.slice.into_allocated(), + } + } +} + impl Node for Ident { fn loc(&self) -> SourceLocation { self.slice.loc @@ -68,6 +83,16 @@ pub enum Program { Script(Vec>), } +impl IntoAllocated for Program where T: ToString { + type Allocated = Program; + fn into_allocated(self) -> Program { + match self { + Program::Mod(parts) => Program::Mod(parts.into_iter().map(IntoAllocated::into_allocated).collect()), + Program::Script(parts) => Program::Script(parts.into_iter().map(IntoAllocated::into_allocated).collect()), + } + } +} + impl Node for Program { fn loc(&self) -> SourceLocation { match self { @@ -112,6 +137,17 @@ pub enum ProgramPart { Stmt(Stmt), } +impl IntoAllocated for ProgramPart where T: ToString { + type Allocated = ProgramPart; + fn into_allocated(self) -> ProgramPart { + match self { + ProgramPart::Dir(part) => ProgramPart::Dir(part.into_allocated()), + ProgramPart::Decl(part) => ProgramPart::Decl(part.into_allocated()), + ProgramPart::Stmt(part) => ProgramPart::Stmt(part.into_allocated()), + } + } +} + impl Node for ProgramPart { fn loc(&self) -> SourceLocation { match self { @@ -140,6 +176,17 @@ pub struct Dir { pub semi_colon: Option, } +impl IntoAllocated for Dir where T: ToString { + type Allocated = Dir; + fn into_allocated(self) -> Dir { + Dir { + expr: self.expr.into_allocated(), + dir: self.dir.to_string(), + semi_colon: self.semi_colon, + } + } +} + impl Node for Dir { fn loc(&self) -> SourceLocation { let expr_loc = self.expr.loc(); @@ -184,6 +231,26 @@ impl Func { } } + +impl IntoAllocated for Func +where + T: ToString, +{ + type Allocated = Func; + fn into_allocated(self) -> Func { + Func { + keyword: self.keyword, + id: self.id.map(|i| i.into_allocated()), + open_paren: self.open_paren, + params: self.params.into_iter().map(|p| p.into_allocated()).collect(), + close_paren: self.close_paren, + body: self.body.into_allocated(), + star: self.star, + keyword_async: self.keyword_async, + } + } +} + impl Node for Func { fn loc(&self) -> SourceLocation { let start = if let Some(keyword) = self.keyword_async { @@ -202,6 +269,13 @@ pub struct FuncArgEntry { pub comma: Option, } +impl IntoAllocated for FuncArgEntry where T: ToString { + type Allocated = FuncArgEntry; + fn into_allocated(self) -> FuncArgEntry { + FuncArgEntry { value: self.value.into_allocated(), comma: self.comma } + } +} + impl Node for FuncArgEntry { fn loc(&self) -> SourceLocation { if let Some(comma) = &self.comma { @@ -222,6 +296,17 @@ pub enum FuncArg { Rest(Box>), } +impl IntoAllocated for FuncArg where T: ToString { + type Allocated = FuncArg; + fn into_allocated(self) -> FuncArg { + match self { + FuncArg::Expr(inner) => FuncArg::Expr(inner.into_allocated()), + FuncArg::Pat(inner) => FuncArg::Pat(inner.into_allocated()), + FuncArg::Rest(inner) => FuncArg::Rest(inner.into_allocated()), + } + } +} + impl Node for FuncArg { fn loc(&self) -> SourceLocation { match self { @@ -240,6 +325,13 @@ pub struct FuncBody { pub close_brace: CloseBrace, } +impl IntoAllocated for FuncBody where T: ToString { + type Allocated = FuncBody; + fn into_allocated(self) -> FuncBody { + FuncBody { open_brace: self.open_brace, stmts: self.stmts.into_iter().map(|s| s.into_allocated()).collect(), close_brace: self.close_brace } + } +} + impl Node for FuncBody { fn loc(&self) -> SourceLocation { SourceLocation { @@ -283,6 +375,19 @@ pub struct Class { pub body: ClassBody, } + +impl IntoAllocated for Class where T: ToString { + type Allocated = Class; + fn into_allocated(self) -> Class { + Class { + keyword: self.keyword, + id: self.id.map(|i| i.into_allocated()), + super_class: self.super_class.map(|s| s.into_allocated()), + body: self.body.into_allocated(), + } + } +} + impl Node for Class { fn loc(&self) -> SourceLocation { SourceLocation { @@ -298,6 +403,13 @@ pub struct SuperClass { pub expr: Expr, } +impl IntoAllocated for SuperClass where T: ToString { + type Allocated = SuperClass; + fn into_allocated(self) -> SuperClass { + SuperClass { keyword_extends: self.keyword_extends, expr: self.expr.into_allocated() } + } +} + #[derive(Debug, Clone, PartialEq)] pub struct ClassBody { pub open_brace: OpenBrace, @@ -305,6 +417,13 @@ pub struct ClassBody { pub close_brace: CloseBrace, } +impl IntoAllocated for ClassBody where T: ToString { + type Allocated = ClassBody; + fn into_allocated(self) -> ClassBody { + ClassBody { open_brace: self.open_brace, props: self.props.into_iter().map(IntoAllocated::into_allocated).collect(), close_brace: self.close_brace } + } +} + impl Node for ClassBody { fn loc(&self) -> SourceLocation { let start = self.open_brace.start(); @@ -352,6 +471,19 @@ pub struct Slice { pub loc: SourceLocation, } +impl IntoAllocated for Slice +where + T: ToString, +{ + type Allocated = Slice; + fn into_allocated(self) -> Slice { + Slice { + loc: self.loc, + source: self.source.to_string(), + } + } +} + impl Slice { pub fn new(source: T, start_line: u32, start_col: u32, end_line: u32, end_column: u32) -> Self { Self { @@ -467,7 +599,19 @@ pub struct ListEntry { pub comma: Option, } +impl IntoAllocated for ListEntry where Item: IntoAllocated { + type Allocated = ListEntry; + + fn into_allocated(self) -> Self::Allocated { + ListEntry { + item: self.item.into_allocated(), + comma: self.comma, + } + } +} + impl ListEntry { + pub fn no_comma(item: Item) -> Self { Self { item, comma: None } } diff --git a/src/spanned/pat.rs b/src/spanned/pat.rs index 5b9e965..c630196 100644 --- a/src/spanned/pat.rs +++ b/src/spanned/pat.rs @@ -1,3 +1,4 @@ +use crate::IntoAllocated; use crate::spanned::expr::{Expr, Prop}; use crate::spanned::Ident; @@ -13,6 +14,18 @@ pub enum Pat { Assign(AssignPat), } +impl IntoAllocated for Pat where T: ToString { + type Allocated = Pat; + fn into_allocated(self) -> Self::Allocated { + match self { + Pat::Ident(inner) => Pat::Ident(inner.into_allocated()), + Pat::Obj(inner) => Pat::Obj(inner.into_allocated()), + Pat::Array(inner) => Pat::Array(inner.into_allocated()), + Pat::Assign(inner) => Pat::Assign(inner.into_allocated()), + } + } +} + impl Node for Pat { fn loc(&self) -> super::SourceLocation { match self { @@ -31,6 +44,17 @@ pub struct ArrayPat { pub close_bracket: CloseBracket, } +impl IntoAllocated for ArrayPat where T: ToString { + type Allocated = ArrayPat; + fn into_allocated(self) -> Self::Allocated { + ArrayPat { + open_bracket: self.open_bracket, + elements: self.elements.into_iter().map(IntoAllocated::into_allocated).collect(), + close_bracket: self.close_bracket, + } + } +} + impl Node for ArrayPat { fn loc(&self) -> super::SourceLocation { SourceLocation { @@ -46,6 +70,16 @@ pub struct ArrayElement { pub comma: Option, } +impl IntoAllocated for ArrayElement where T: ToString { + type Allocated = ArrayElement; + fn into_allocated(self) -> Self::Allocated { + ArrayElement { + part: self.part.into_allocated(), + comma: self.comma, + } + } +} + #[derive(PartialEq, Debug, Clone)] pub enum ArrayPatPart { Pat(Pat), @@ -53,6 +87,17 @@ pub enum ArrayPatPart { Rest(RestPat), } +impl IntoAllocated for ArrayPatPart where T: ToString { + type Allocated = ArrayPatPart; + fn into_allocated(self) -> Self::Allocated { + match self { + ArrayPatPart::Pat(inner) => ArrayPatPart::Pat(inner.into_allocated()), + ArrayPatPart::Expr(inner) => ArrayPatPart::Expr(inner.into_allocated()), + ArrayPatPart::Rest(inner) => ArrayPatPart::Rest(inner.into_allocated()), + } + } +} + impl Node for ArrayPatPart { fn loc(&self) -> SourceLocation { match self { @@ -73,6 +118,17 @@ pub struct ObjPat { pub close_brace: CloseBrace, } +impl IntoAllocated for ObjPat where T: ToString { + type Allocated = ObjPat; + fn into_allocated(self) -> Self::Allocated { + ObjPat { + open_brace: self.open_brace, + props: self.props.into_iter().map(IntoAllocated::into_allocated).collect(), + close_brace: self.close_brace, + } + } +} + impl Node for ObjPat { fn loc(&self) -> SourceLocation { SourceLocation { @@ -89,6 +145,16 @@ pub enum ObjPatPart { Rest(Box>), } +impl IntoAllocated for ObjPatPart where T: ToString { + type Allocated = ObjPatPart; + fn into_allocated(self) -> Self::Allocated { + match self { + ObjPatPart::Assign(inner) => ObjPatPart::Assign(inner.into_allocated()), + ObjPatPart::Rest(inner) => ObjPatPart::Rest(inner.into_allocated()), + } + } +} + impl Node for ObjPatPart { fn loc(&self) -> SourceLocation { match self { @@ -104,6 +170,16 @@ pub struct RestPat { pub pat: Pat, } +impl IntoAllocated for RestPat where T: ToString { + type Allocated = RestPat; + fn into_allocated(self) -> Self::Allocated { + RestPat { + dots: self.dots, + pat: self.pat.into_allocated(), + } + } +} + impl Node for RestPat { fn loc(&self) -> SourceLocation { SourceLocation { @@ -121,6 +197,17 @@ pub struct AssignPat { pub right: Box>, } +impl IntoAllocated for AssignPat where T: ToString { + type Allocated = AssignPat; + fn into_allocated(self) -> Self::Allocated { + AssignPat { + left: self.left.into_allocated(), + operator: self.operator, + right: self.right.into_allocated(), + } + } +} + impl Node for AssignPat { fn loc(&self) -> SourceLocation { SourceLocation { diff --git a/src/spanned/stmt.rs b/src/spanned/stmt.rs index b3dee92..6ed0e90 100644 --- a/src/spanned/stmt.rs +++ b/src/spanned/stmt.rs @@ -1,3 +1,4 @@ +use crate::IntoAllocated; use crate::spanned::decl::VarDecl; use crate::spanned::expr::Expr; use crate::spanned::pat::Pat; @@ -95,7 +96,7 @@ pub enum Stmt { /// An if statement /// ```js /// if (1 < 2) { - /// console.log(Tlways true'); + /// console.log('Always true'); /// } else { /// console.log('Never true'); /// } @@ -205,6 +206,33 @@ pub enum Stmt { }, } +impl IntoAllocated for Stmt where T: ToString { + type Allocated = Stmt; + fn into_allocated(self) -> Self::Allocated { + match self { + Stmt::Expr { expr, semi_colon } => Stmt::Expr { expr: expr.into_allocated(), semi_colon }, + Stmt::Block(inner) => Stmt::Block(inner.into_allocated()), + Stmt::Empty(inner) => Stmt::Empty(inner), + Stmt::Debugger { keyword, semi_colon } => Stmt::Debugger { keyword, semi_colon }, + Stmt::With(inner) => Stmt::With(inner.into_allocated()), + Stmt::Return { keyword, value, semi_colon } => Stmt::Return { keyword, value: value.into_allocated(), semi_colon }, + Stmt::Labeled(inner) => Stmt::Labeled(inner.into_allocated()), + Stmt::Break { keyword, label, semi_colon } => Stmt::Break { keyword, label: label.into_allocated(), semi_colon}, + Stmt::Continue { keyword, label, semi_colon } => Stmt::Continue { keyword, label: label.into_allocated(), semi_colon }, + Stmt::If(inner) => Stmt::If(inner.into_allocated()), + Stmt::Switch(inner) => Stmt::Switch(inner.into_allocated()), + Stmt::Throw { keyword, expr, semi_colon } => Stmt::Throw { keyword, expr: expr.into_allocated(), semi_colon }, + Stmt::Try(inner) => Stmt::Try(inner.into_allocated()), + Stmt::While(inner) => Stmt::While(inner.into_allocated()), + Stmt::DoWhile(inner) => Stmt::DoWhile(inner.into_allocated()), + Stmt::For(inner) => Stmt::For(inner.into_allocated()), + Stmt::ForIn(inner) => Stmt::ForIn(inner.into_allocated()), + Stmt::ForOf(inner) => Stmt::ForOf(inner.into_allocated()), + Stmt::Var { decls, semi_colon } => Stmt::Var { decls: decls.into_allocated(), semi_colon }, + } + } +} + impl Node for Stmt { fn loc(&self) -> SourceLocation { match self { @@ -349,6 +377,18 @@ pub struct WithStmt { pub body: Box>, } +impl IntoAllocated for WithStmt where T: ToString { + type Allocated = WithStmt; + fn into_allocated(self) -> Self::Allocated { + WithStmt { + keyword: self.keyword, + open_paren: self.open_paren, + object: self.object.into_allocated(), + close_paren: self.close_paren, + body: self.body.into_allocated(), + } + } +} impl Node for WithStmt { fn loc(&self) -> SourceLocation { SourceLocation { @@ -373,6 +413,18 @@ pub struct LabeledStmt { pub body: Box>, } +impl IntoAllocated for LabeledStmt where T: ToString { + type Allocated = LabeledStmt; + + fn into_allocated(self) -> Self::Allocated { + LabeledStmt { + label: self.label.into_allocated(), + colon: self.colon, + body: self.body.into_allocated(), + } + } +} + impl Node for LabeledStmt { fn loc(&self) -> SourceLocation { SourceLocation { @@ -385,7 +437,7 @@ impl Node for LabeledStmt { /// An if statement /// ```js /// if (1 < 2) { -/// console.log(Tlways true'); +/// console.log('Always true'); /// } else { /// console.log('Never true'); /// } @@ -400,6 +452,21 @@ pub struct IfStmt { pub alternate: Option>>, } +impl IntoAllocated for IfStmt where T: ToString { + type Allocated = IfStmt; + + fn into_allocated(self) -> Self::Allocated { + IfStmt { + keyword: self.keyword, + open_paren: self.open_paren, + test: self.test.into_allocated(), + close_paren: self.close_paren, + consequent: self.consequent.into_allocated(), + alternate: self.alternate.into_allocated(), + } + } +} + impl Node for IfStmt { fn loc(&self) -> SourceLocation { let start = self.keyword.start(); @@ -418,6 +485,17 @@ pub struct ElseStmt { pub body: Stmt, } +impl IntoAllocated for ElseStmt where T: ToString { + type Allocated = ElseStmt; + + fn into_allocated(self) -> Self::Allocated { + ElseStmt { + keyword: self.keyword, + body: self.body.into_allocated(), + } + } +} + impl Node for ElseStmt { fn loc(&self) -> SourceLocation { SourceLocation { @@ -452,6 +530,22 @@ pub struct SwitchStmt { pub close_brace: CloseBrace, } +impl IntoAllocated for SwitchStmt where T: ToString { + type Allocated = SwitchStmt; + + fn into_allocated(self) -> Self::Allocated { + SwitchStmt { + keyword: self.keyword, + open_paren: self.open_paren, + discriminant: self.discriminant.into_allocated(), + close_paren: self.close_paren, + open_brace: self.open_brace, + cases: self.cases.into_iter().map(IntoAllocated::into_allocated).collect(), + close_brace: self.close_brace, + } + } +} + impl Node for SwitchStmt { fn loc(&self) -> SourceLocation { SourceLocation { @@ -484,6 +578,19 @@ impl Node for SwitchCase { } } +impl IntoAllocated for SwitchCase where T: ToString { + type Allocated = SwitchCase; + + fn into_allocated(self) -> Self::Allocated { + SwitchCase { + keyword: self.keyword, + colon: self.colon, + consequent: self.consequent.into_iter().map(IntoAllocated::into_allocated).collect(), + test: self.test.into_allocated(), + } + } +} + /// A collection of program parts wrapped in curly braces #[derive(Debug, Clone, PartialEq)] pub struct BlockStmt { @@ -492,6 +599,18 @@ pub struct BlockStmt { pub close_brace: CloseBrace, } +impl IntoAllocated for BlockStmt where T: ToString { + type Allocated = BlockStmt; + + fn into_allocated(self) -> Self::Allocated { + BlockStmt { + open_brace: self.open_brace, + stmts: self.stmts.into_iter().map(IntoAllocated::into_allocated).collect(), + close_brace: self.close_brace, + } + } +} + impl Node for BlockStmt { fn loc(&self) -> SourceLocation { SourceLocation { @@ -519,6 +638,18 @@ pub struct TryStmt { pub finalizer: Option>, } +impl IntoAllocated for TryStmt where T: ToString { + type Allocated = TryStmt; + fn into_allocated(self) -> Self::Allocated { + TryStmt { + keyword: self.keyword, + block: self.block.into_allocated(), + handler: self.handler.into_allocated(), + finalizer: self.finalizer.into_allocated(), + } + } +} + impl Node for TryStmt { fn loc(&self) -> SourceLocation { let end = if let Some(finalizer) = &self.finalizer { @@ -543,6 +674,18 @@ pub struct CatchClause { pub body: BlockStmt, } +impl IntoAllocated for CatchClause where T: ToString { + type Allocated = CatchClause; + + fn into_allocated(self) -> Self::Allocated { + CatchClause { + keyword: self.keyword, + param: self.param.into_allocated(), + body: self.body.into_allocated(), + } + } +} + impl Node for CatchClause { fn loc(&self) -> SourceLocation { SourceLocation { @@ -559,6 +702,18 @@ pub struct CatchArg { pub close_paren: CloseParen, } +impl IntoAllocated for CatchArg where T: ToString { + type Allocated = CatchArg; + + fn into_allocated(self) -> Self::Allocated { + CatchArg { + open_paren: self.open_paren, + param: self.param.into_allocated(), + close_paren: self.close_paren, + } + } +} + impl Node for CatchArg { fn loc(&self) -> SourceLocation { SourceLocation { @@ -574,6 +729,16 @@ pub struct FinallyClause { pub body: BlockStmt, } +impl IntoAllocated for FinallyClause where T: ToString { + type Allocated = FinallyClause; + fn into_allocated(self) -> Self::Allocated { + FinallyClause { + keyword: self.keyword, + body: self.body.into_allocated(), + } + } +} + impl Node for FinallyClause { fn loc(&self) -> SourceLocation { SourceLocation { @@ -606,6 +771,20 @@ pub struct WhileStmt { pub body: Box>, } +impl IntoAllocated for WhileStmt where T: ToString { + type Allocated = WhileStmt; + + fn into_allocated(self) -> Self::Allocated { + WhileStmt { + keyword: self.keyword, + open_paren: self.open_paren, + test: self.test.into_allocated(), + close_paren: self.close_paren, + body: self.body.into_allocated() + } + } +} + impl Node for WhileStmt { fn loc(&self) -> SourceLocation { SourceLocation { @@ -632,6 +811,22 @@ pub struct DoWhileStmt { pub semi_colon: Option, } +impl IntoAllocated for DoWhileStmt where T: ToString { + type Allocated = DoWhileStmt; + + fn into_allocated(self) -> Self::Allocated { + DoWhileStmt { + keyword_do: self.keyword_do, + body: self.body.into_allocated(), + keyword_while: self.keyword_while, + open_paren: self.open_paren, + test: self.test.into_allocated(), + close_paren: self.close_paren, + semi_colon: self.semi_colon, + } + } +} + impl Node for DoWhileStmt { fn loc(&self) -> SourceLocation { let end = self @@ -666,6 +861,24 @@ pub struct ForStmt { pub body: Box>, } +impl IntoAllocated for ForStmt where T: ToString { + type Allocated = ForStmt; + + fn into_allocated(self) -> Self::Allocated { + ForStmt { + keyword: self.keyword, + open_paren: self.open_paren, + init: self.init.into_allocated(), + semi1: self.semi1, + test: self.test.into_allocated(), + semi2: self.semi2, + update: self.update.into_allocated(), + close_paren: self.close_paren, + body: self.body.into_allocated(), + } + } +} + impl Node for ForStmt { fn loc(&self) -> SourceLocation { SourceLocation { @@ -685,6 +898,16 @@ pub enum LoopInit { Expr(Expr), } +impl IntoAllocated for LoopInit where T: ToString { + type Allocated = LoopInit; + fn into_allocated(self) -> Self::Allocated { + match self { + LoopInit::Variable(k, v) => LoopInit::Variable(k, v.into_iter().map(IntoAllocated::into_allocated).collect()), + LoopInit::Expr(inner) => LoopInit::Expr(inner.into_allocated()) + } + } +} + impl Node for LoopInit { fn loc(&self) -> SourceLocation { match self { @@ -726,6 +949,22 @@ pub struct ForInStmt { pub body: Box>, } +impl IntoAllocated for ForInStmt where T: ToString { + type Allocated = ForInStmt; + + fn into_allocated(self) -> Self::Allocated { + ForInStmt { + keyword_for: self.keyword_for, + open_paren: self.open_paren, + left: self.left.into_allocated(), + keyword_in: self.keyword_in, + right: self.right.into_allocated(), + close_paren: self.close_paren, + body: self.body.into_allocated(), + } + } +} + impl Node for ForInStmt { fn loc(&self) -> SourceLocation { SourceLocation { @@ -755,6 +994,23 @@ pub struct ForOfStmt { pub is_await: bool, } +impl IntoAllocated for ForOfStmt where T: ToString { + type Allocated = ForOfStmt; + + fn into_allocated(self) -> Self::Allocated { + ForOfStmt { + keyword_for: self.keyword_for, + open_paren: self.open_paren, + left: self.left.into_allocated(), + keyword_of: self.keyword_of, + right: self.right.into_allocated(), + close_paren: self.close_paren, + body: self.body.into_allocated(), + is_await: self.is_await, + } + } +} + impl Node for ForOfStmt { fn loc(&self) -> SourceLocation { SourceLocation { @@ -773,6 +1029,17 @@ pub enum LoopLeft { Pat(Pat), } +impl IntoAllocated for LoopLeft where T: ToString { + type Allocated = LoopLeft; + fn into_allocated(self) -> Self::Allocated { + match self { + LoopLeft::Expr(inner) => LoopLeft::Expr(inner.into_allocated()), + LoopLeft::Variable(k, v) => LoopLeft::Variable(k, v.into_allocated()), + LoopLeft::Pat(inner) => LoopLeft::Pat(inner.into_allocated()), + } + } +} + impl Node for LoopLeft { fn loc(&self) -> SourceLocation { match self { diff --git a/src/stmt.rs b/src/stmt.rs index 6f265da..4849fd7 100644 --- a/src/stmt.rs +++ b/src/stmt.rs @@ -1,7 +1,7 @@ use crate::decl::VarDecl; use crate::expr::Expr; use crate::pat::Pat; -use crate::VarKind; +use crate::{VarKind, IntoAllocated}; use crate::{Ident, ProgramPart}; /// A slightly more granular part of an es program than ProgramPart #[derive(Debug, Clone, PartialEq)] @@ -73,7 +73,7 @@ pub enum Stmt { /// An if statement /// ```js /// if (1 < 2) { - /// console.log(Tlways true'); + /// console.log('Always true'); /// } else { /// console.log('Never true'); /// } @@ -176,6 +176,34 @@ pub enum Stmt { Var(Vec>), } +impl IntoAllocated for Stmt where T: ToString { + type Allocated = Stmt; + + fn into_allocated(self) -> Self::Allocated { + match self { + Stmt::Expr(inner) => Stmt::Expr(inner.into_allocated()), + Stmt::Block(inner) => Stmt::Block(inner.into_allocated()), + Stmt::Empty => Stmt::Empty, + Stmt::Debugger => Stmt::Debugger, + Stmt::With(inner) => Stmt::With(inner.into_allocated()), + Stmt::Return(inner) => Stmt::Return(inner.map(IntoAllocated::into_allocated)), + Stmt::Labeled(inner) => Stmt::Labeled(inner.into_allocated()), + Stmt::Break(inner) => Stmt::Break(inner.map(IntoAllocated::into_allocated)), + Stmt::Continue(inner) => Stmt::Continue(inner.map(IntoAllocated::into_allocated)), + Stmt::If(inner) => Stmt::If(inner.into_allocated()), + Stmt::Switch(inner) => Stmt::Switch(inner.into_allocated()), + Stmt::Throw(inner) => Stmt::Throw(inner.into_allocated()), + Stmt::Try(inner) => Stmt::Try(inner.into_allocated()), + Stmt::While(inner) => Stmt::While(inner.into_allocated()), + Stmt::DoWhile(inner) => Stmt::DoWhile(inner.into_allocated()), + Stmt::For(inner) => Stmt::For(inner.into_allocated()), + Stmt::ForIn(inner) => Stmt::ForIn(inner.into_allocated()), + Stmt::ForOf(inner) => Stmt::ForOf(inner.into_allocated()), + Stmt::Var(inner) => Stmt::Var(inner.into_iter().map(|v| v.into_allocated()).collect()), + } + } +} + /// A with statement, this puts one object at the top of /// the identifier search tree. /// > note: this cannot be used in a strict context @@ -196,6 +224,17 @@ pub struct WithStmt { pub body: Box>, } +impl IntoAllocated for WithStmt where T: ToString { + type Allocated = WithStmt; + + fn into_allocated(self) -> Self::Allocated { + WithStmt { + object: self.object.into_allocated(), + body: self.body.into_allocated(), + } + } +} + /// A break statement /// ```js /// label: { @@ -212,10 +251,21 @@ pub struct LabeledStmt { pub body: Box>, } +impl IntoAllocated for LabeledStmt where T: ToString { + type Allocated = LabeledStmt; + + fn into_allocated(self) -> Self::Allocated { + LabeledStmt { + label: self.label.into_allocated(), + body: self.body.into_allocated(), + } + } +} + /// An if statement /// ```js /// if (1 < 2) { -/// console.log(Tlways true'); +/// console.log('Always true'); /// } else { /// console.log('Never true'); /// } @@ -228,6 +278,18 @@ pub struct IfStmt { pub alternate: Option>>, } +impl IntoAllocated for IfStmt where T: ToString { + type Allocated = IfStmt; + + fn into_allocated(self) -> Self::Allocated { + IfStmt { + test: self.test.into_allocated(), + consequent: self.consequent.into_allocated(), + alternate: self.alternate.map(IntoAllocated::into_allocated), + } + } +} + /// A switch statement /// ```js /// switch (Math.floor(Math.random()) * 10) { @@ -249,6 +311,17 @@ pub struct SwitchStmt { pub cases: Vec>, } +impl IntoAllocated for SwitchStmt where T: ToString { + type Allocated = SwitchStmt; + + fn into_allocated(self) -> Self::Allocated { + SwitchStmt { + discriminant: self.discriminant.into_allocated(), + cases: self.cases.into_iter().map(|c| c.into_allocated()).collect(), + } + } +} + /// A single case part of a switch statement #[derive(Debug, Clone, PartialEq)] #[cfg_attr( @@ -261,6 +334,17 @@ pub struct SwitchCase { pub consequent: Vec>, } +impl IntoAllocated for SwitchCase where T: ToString { + type Allocated = SwitchCase; + + fn into_allocated(self) -> Self::Allocated { + SwitchCase { + test: self.test.map(IntoAllocated::into_allocated), + consequent: self.consequent.into_iter().map(|c| c.into_allocated()).collect(), + } + } +} + /// A collection of program parts wrapped in curly braces #[derive(Debug, Clone, PartialEq)] #[cfg_attr( @@ -270,6 +354,14 @@ pub struct SwitchCase { #[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct BlockStmt(pub Vec>); +impl IntoAllocated for BlockStmt where T: ToString { + type Allocated = BlockStmt; + + fn into_allocated(self) -> Self::Allocated { + BlockStmt(self.0.into_iter().map(|s| s.into_allocated()).collect()) + } +} + /// A try/catch block /// ```js /// try { @@ -288,6 +380,18 @@ pub struct TryStmt { pub finalizer: Option>, } +impl IntoAllocated for TryStmt where T: ToString { + type Allocated = TryStmt; + + fn into_allocated(self) -> Self::Allocated { + TryStmt { + block: self.block.into_allocated(), + handler: self.handler.map(|h| h.into_allocated()), + finalizer: self.finalizer.map(|f| f.into_allocated()), + } + } +} + /// The error handling part of a `TryStmt` #[derive(Debug, Clone, PartialEq)] #[cfg_attr( @@ -300,6 +404,17 @@ pub struct CatchClause { pub body: BlockStmt, } +impl IntoAllocated for CatchClause where T: ToString { + type Allocated = CatchClause; + + fn into_allocated(self) -> Self::Allocated { + CatchClause { + param: self.param.map(IntoAllocated::into_allocated), + body: self.body.into_allocated(), + } + } +} + /// A while loop /// ```js /// while (false) { @@ -321,6 +436,17 @@ pub struct WhileStmt { pub body: Box>, } +impl IntoAllocated for WhileStmt where T: ToString { + type Allocated = WhileStmt; + + fn into_allocated(self) -> Self::Allocated { + WhileStmt { + test: self.test.into_allocated(), + body: self.body.into_allocated(), + } + } +} + /// A while loop that executes its body first /// ```js /// do { @@ -334,6 +460,17 @@ pub struct DoWhileStmt { pub body: Box>, } +impl IntoAllocated for DoWhileStmt where T: ToString { + type Allocated = DoWhileStmt; + + fn into_allocated(self) -> Self::Allocated { + DoWhileStmt { + test: self.test.into_allocated(), + body: self.body.into_allocated() + } + } +} + /// A "c-style" for loop /// ```js /// for (var i = 0; i < 100; i++) console.log(i); @@ -350,6 +487,19 @@ pub struct ForStmt { pub body: Box>, } +impl IntoAllocated for ForStmt where T: ToString { + type Allocated = ForStmt; + + fn into_allocated(self) -> Self::Allocated { + ForStmt { + init: self.init.map(|i| i.into_allocated()), + test: self.test.map(|t| t.into_allocated()), + update: self.update.map(|u| u.into_allocated()), + body: self.body.into_allocated(), + } + } +} + /// The left most triple of a for loops parenthetical /// ```js /// // vvvvvvvvv @@ -365,6 +515,17 @@ pub enum LoopInit { Expr(Expr), } +impl IntoAllocated for LoopInit where T: ToString { + type Allocated = LoopInit; + + fn into_allocated(self) -> Self::Allocated { + match self { + LoopInit::Variable(k, v) => LoopInit::Variable(k, v.into_iter().map(|v| v.into_allocated()).collect()), + LoopInit::Expr(inner) => LoopInit::Expr(inner.into_allocated()), + } + } +} + /// A for in statement, this kind of for statement /// will extract each key from an indexable thing /// ```js @@ -385,6 +546,18 @@ pub struct ForInStmt { pub body: Box>, } +impl IntoAllocated for ForInStmt where T: ToString { + type Allocated = ForInStmt; + + fn into_allocated(self) -> Self::Allocated { + ForInStmt { + left: self.left.into_allocated(), + right: self.right.into_allocated(), + body: self.body.into_allocated(), + } + } +} + /// A for of statement, this kind of for statement /// will extract the value from a generator or iterator /// ```js @@ -402,6 +575,19 @@ pub struct ForOfStmt { pub is_await: bool, } +impl IntoAllocated for ForOfStmt where T: ToString { + type Allocated = ForOfStmt; + + fn into_allocated(self) -> Self::Allocated { + ForOfStmt { + left: self.left.into_allocated(), + right: self.right.into_allocated(), + body: self.body.into_allocated(), + is_await: self.is_await, + } + } +} + /// The values on the left hand side of the keyword /// in a for in or for of loop #[derive(Debug, Clone, PartialEq)] @@ -415,3 +601,15 @@ pub enum LoopLeft { Variable(VarKind, VarDecl), Pat(Pat), } + +impl IntoAllocated for LoopLeft where T: ToString { + type Allocated = LoopLeft; + + fn into_allocated(self) -> Self::Allocated { + match self { + LoopLeft::Expr(inner) => LoopLeft::Expr(inner.into_allocated()), + LoopLeft::Variable(k, v) => LoopLeft::Variable(k, v.into_allocated()), + LoopLeft::Pat(inner) => LoopLeft::Pat(inner.into_allocated()), + } + } +} From 5e92c4969daef1b08d735ea0ae487feeb2165bf0 Mon Sep 17 00:00:00 2001 From: Robert Masen Date: Sun, 11 Jun 2023 09:46:50 -0500 Subject: [PATCH 3/8] remove esprima feature --- Cargo.toml | 1 - src/decl.rs | 18 +- src/expr.rs | 23 +- src/lib.rs | 50 +- src/pat.rs | 9 +- src/serde/de.rs | 1 - src/serde/mod.rs | 4 - src/serde/ser.rs | 1244 ---------------------------------------------- src/stmt.rs | 18 +- tests/serde.rs | 169 ------- 10 files changed, 34 insertions(+), 1503 deletions(-) delete mode 100644 src/serde/de.rs delete mode 100644 src/serde/mod.rs delete mode 100644 src/serde/ser.rs delete mode 100644 tests/serde.rs diff --git a/Cargo.toml b/Cargo.toml index 8496f7c..5c79efd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,3 @@ pretty_env_logger = "0.3" [features] default = [] serialization = ["serde", "serde_derive"] -esprima = ["serialization"] diff --git a/src/decl.rs b/src/decl.rs index 99d23e8..0677858 100644 --- a/src/decl.rs +++ b/src/decl.rs @@ -6,10 +6,9 @@ use crate::{Class, Func, Ident}; /// The declaration of a variable, function, class, import or export #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum Decl { /// A variable declaration /// ```js @@ -58,10 +57,9 @@ impl IntoAllocated for Decl where T: ToString { /// The identifier and optional value of a variable declaration #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct VarDecl { pub id: Pat, pub init: Option>, @@ -105,10 +103,9 @@ impl IntoAllocated for ModImport where T: ToString { /// The name of the thing being imported #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum ImportSpecifier { /// A specifier in curly braces, this might /// have a local alias @@ -167,10 +164,9 @@ impl IntoAllocated for NormalImportSpec where T: ToString { /// Something exported from this module #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum ModExport { /// ```js /// export default function() {}; @@ -238,10 +234,9 @@ impl IntoAllocated for NamedExportDecl where T: ToString { /// ``` #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum DefaultExportDecl { Decl(Decl), Expr(Expr), @@ -268,10 +263,9 @@ impl IntoAllocated for DefaultExportDecl where T: ToString { /// ``` #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct ExportSpecifier { pub local: Ident, pub alias: Option>, diff --git a/src/expr.rs b/src/expr.rs index a9b1db5..b82a6e1 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -4,11 +4,9 @@ use crate::{Class, Func, FuncArg, FuncBody, Ident}; /// A slightly more granular program part that a statement #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] -#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))] pub enum Expr { /// `[0,,]` Array(ArrayExpr), @@ -139,7 +137,6 @@ pub type ObjExpr = Vec>; /// A single part of an object literal #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))] pub enum ObjProp { Prop(Prop), Spread(Expr), @@ -159,10 +156,9 @@ impl IntoAllocated for ObjProp where T: ToString { /// A single part of an object literal or class #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct Prop { pub key: PropKey, pub value: PropValue, @@ -192,7 +188,6 @@ impl IntoAllocated for Prop where T: ToString { /// An object literal or class property identifier #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))] pub enum PropKey { Lit(Lit), Expr(Expr), @@ -214,7 +209,6 @@ impl IntoAllocated for PropKey where T: ToString { /// The value of an object literal or class property #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))] pub enum PropValue { Expr(Expr), Pat(Pat), @@ -320,7 +314,6 @@ impl IntoAllocated for AssignExpr where T: ToString { /// The value being assigned to #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))] pub enum AssignLeft { Pat(Pat), Expr(Box>), @@ -561,10 +554,9 @@ impl IntoAllocated for TaggedTemplateExpr where T: ToString { /// ``` #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct TemplateLit { pub quasis: Vec>, pub expressions: Vec>, @@ -583,10 +575,9 @@ impl IntoAllocated for TemplateLit where T: ToString { #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum QuasiQuote { /// ` BackTick, @@ -599,10 +590,9 @@ pub enum QuasiQuote { /// The text part of a `TemplateLiteral` #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct TemplateElement { pub open_quote: QuasiQuote, /// The non-quoted version @@ -662,10 +652,9 @@ impl IntoAllocated for MetaProp where T: ToString { /// A literal value #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum Lit { /// `null` Null, diff --git a/src/lib.rs b/src/lib.rs index 7ed6550..8a9d21d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,6 @@ -#[cfg(feature = "serde")] -#[macro_use] -extern crate serde_derive; - pub mod decl; pub mod expr; pub mod pat; -#[cfg(feature = "esprima")] -pub mod serde; pub mod spanned; pub mod stmt; @@ -19,10 +13,9 @@ use stmt::Stmt; #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct Ident { pub name: T, } @@ -62,10 +55,9 @@ impl<'a> From> for Ident> { /// a ES6 Mod or a Script. #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum Program { /// An ES6 Mod Mod(Vec>), @@ -97,11 +89,9 @@ impl Program { /// This will be either a Directive, Decl or a Stmt #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] -#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))] pub enum ProgramPart { /// A Directive like `'use strict';` Dir(Dir), @@ -136,10 +126,9 @@ impl ProgramPart { /// top of a file or function #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct Dir { pub expr: Lit, pub dir: T, @@ -211,11 +200,9 @@ impl Func { /// A single function argument from a function signature #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] -#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))] pub enum FuncArg { Expr(Expr), Pat(Pat), @@ -244,10 +231,9 @@ impl FuncArg { /// The block statement that makes up the function's body #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct FuncBody(pub Vec>); impl IntoAllocated for FuncBody where T: ToString { @@ -306,10 +292,9 @@ impl IntoAllocated for Class where T: ToString { #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct ClassBody(pub Vec>); impl IntoAllocated for ClassBody where T: ToString { @@ -333,10 +318,9 @@ impl Class { /// The kind of variable being defined (`var`/`let`/`const`) #[derive(Debug, Clone, PartialEq, Copy)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] #[cfg_attr( all(feature = "serde", feature = "esprima"), serde(rename_all = "camelCase", untagged) @@ -350,10 +334,9 @@ pub enum VarKind { /// The available operators for assignment Exprs #[derive(Debug, Clone, PartialEq, Copy)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum AssignOp { Equal, PlusEqual, @@ -373,10 +356,9 @@ pub enum AssignOp { /// The available logical operators #[derive(Debug, Clone, PartialEq, Copy)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum LogicalOp { Or, And, @@ -385,10 +367,9 @@ pub enum LogicalOp { /// The available operations for `Binary` Exprs #[derive(Debug, Clone, PartialEq, Copy)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum BinaryOp { Equal, NotEqual, @@ -417,10 +398,9 @@ pub enum BinaryOp { /// `++` or `--` #[derive(Debug, Clone, PartialEq, Copy)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum UpdateOp { Increment, Decrement, @@ -430,10 +410,9 @@ pub enum UpdateOp { /// to be `Unary` #[derive(Debug, Clone, PartialEq, Copy)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum UnaryOp { Minus, Plus, @@ -447,10 +426,9 @@ pub enum UnaryOp { /// A flag for determining what kind of property #[derive(Debug, Clone, PartialEq, Copy)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum PropKind { /// A property with a value Init, diff --git a/src/pat.rs b/src/pat.rs index bc7a66e..f67c7f1 100644 --- a/src/pat.rs +++ b/src/pat.rs @@ -4,11 +4,9 @@ use crate::{Ident, IntoAllocated}; /// and/or value #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] -#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))] pub enum Pat { Ident(Ident), Obj(ObjPat), @@ -39,7 +37,6 @@ impl Pat { #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))] pub enum ArrayPatPart { Pat(Pat), Expr(Expr), @@ -61,7 +58,6 @@ pub type ObjPat = Vec>; /// A single part of an ObjectPat #[derive(PartialEq, Debug, Clone)] #[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))] pub enum ObjPatPart { Assign(Prop), Rest(Box>), @@ -81,10 +77,9 @@ impl IntoAllocated for ObjPatPart where T: ToString { /// An assignment as a pattern #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct AssignPat { pub left: Box>, pub right: Box>, diff --git a/src/serde/de.rs b/src/serde/de.rs deleted file mode 100644 index 8b13789..0000000 --- a/src/serde/de.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/serde/mod.rs b/src/serde/mod.rs deleted file mode 100644 index 864d0aa..0000000 --- a/src/serde/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub mod ser; -pub use ser::*; -pub mod de; -pub use de::*; diff --git a/src/serde/ser.rs b/src/serde/ser.rs deleted file mode 100644 index 7f54632..0000000 --- a/src/serde/ser.rs +++ /dev/null @@ -1,1244 +0,0 @@ -use crate::prelude::*; -use serde::ser::{Serialize, SerializeStruct, Serializer}; - -impl<'a> Serialize for Program<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "Program")?; - match self { - Program::Script(ref body) => { - state.serialize_field("sourceType", "script")?; - state.serialize_field("body", &body)?; - } - Program::Mod(ref body) => { - state.serialize_field("sourceType", "module")?; - state.serialize_field("body", body)?; - } - } - state.end() - } -} - -impl<'a> Serialize for ProgramPart<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self { - ProgramPart::Decl(ref d) => d.serialize(serializer), - ProgramPart::Dir(ref d) => d.serialize(serializer), - ProgramPart::Stmt(ref s) => s.serialize(serializer), - } - } -} -impl<'a> Serialize for Dir<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "ExpressionStatement")?; - state.serialize_field("expression", &self.expr)?; - if let Lit::String(ref sl) = self.expr { - match sl { - StringLit::Double(ref s) => { - if !s.is_empty() { - state.serialize_field("directive", &self.dir)?; - } - } - StringLit::Single(ref s) => { - if !s.is_empty() { - state.serialize_field("directive", &self.dir)?; - } - } - } - } - state.end() - } -} -impl<'a> Serialize for Decl<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self { - Decl::Func(ref f) => { - let mut state = serializer.serialize_struct("Node", 7)?; - state.serialize_field("type", "FunctionDeclaration")?; - state.serialize_field("id", &f.id)?; - state.serialize_field("body", &f.body)?; - state.serialize_field("generator", &f.generator)?; - state.serialize_field("async", &f.is_async)?; - state.serialize_field("expression", &false)?; - state.serialize_field("params", &f.params)?; - state.end() - } - Decl::Class(ref c) => { - let mut state = serializer.serialize_struct("Node", 4)?; - state.serialize_field("type", "ClassDeclaration")?; - state.serialize_field("body", &c.body)?; - state.serialize_field("id", &c.id)?; - state.serialize_field("superClass", &c.super_class)?; - state.end() - } - Decl::Var(ref kind, ref vs) => { - let mut state = serializer.serialize_struct("Node", 7)?; - state.serialize_field("type", "VariableDeclaration")?; - state.serialize_field("kind", kind)?; - state.serialize_field("declarations", vs)?; - state.end() - } - Decl::Import(ref imp) => { - let mut state = serializer.serialize_struct("Node", 7)?; - state.serialize_field("type", "ImportDeclaration")?; - state.serialize_field("specifiers", &imp.specifiers)?; - state.serialize_field("source", &imp.source)?; - state.end() - } - Decl::Export(ref exp) => exp.serialize(serializer), - } - } -} - -impl<'a> Serialize for ModExport<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self { - ModExport::All(ref source) => { - let mut state = serializer.serialize_struct("Node", 7)?; - state.serialize_field("type", "ExportAllDeclaration")?; - state.serialize_field("source", source)?; - state.end() - } - ModExport::Default(ref def) => { - let mut state = serializer.serialize_struct("Node", 7)?; - state.serialize_field("type", "ExportDefaultDeclaration")?; - state.serialize_field("declaration", def)?; - state.end() - } - ModExport::Named(ref named) => { - let mut state = serializer.serialize_struct("Node", 7)?; - state.serialize_field("type", "ExportNamedDeclaration")?; - match named { - NamedExportDecl::Decl(ref d) => { - let specs: Vec<()> = Vec::new(); - let source: Option<()> = None; - state.serialize_field("declaration", d)?; - state.serialize_field("specifiers", &specs)?; - state.serialize_field("source", &source)?; - } - NamedExportDecl::Specifier(ref specs, source) => { - let decl: Option<()> = None; - state.serialize_field("declaration", &decl)?; - state.serialize_field("specifiers", specs)?; - state.serialize_field("source", source)?; - } - } - state.end() - } - } - } -} - -impl<'a> Serialize for DefaultExportDecl<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self { - DefaultExportDecl::Decl(ref d) => d.serialize(serializer), - DefaultExportDecl::Expr(ref e) => e.serialize(serializer), - } - } -} - -impl<'a> Serialize for ImportSpecifier<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self { - ImportSpecifier::Default(ref d) => { - let mut state = serializer.serialize_struct("Node", 7)?; - state.serialize_field("type", "ImportDefaultSpecifier")?; - state.serialize_field("local", d)?; - state.end() - } - ImportSpecifier::Namespace(ref n) => { - let mut state = serializer.serialize_struct("Node", 7)?; - state.serialize_field("type", "ImportNamespaceSpecifier")?; - state.serialize_field("local", n)?; - state.end() - } - ImportSpecifier::Normal(ref n) => { - let mut state = serializer.serialize_struct("Node", 7)?; - state.serialize_field("type", "ImportSpecifier")?; - // state.serialize_field("local", &n.)?; - // state.serialize_field("imported", &n.imported)?; - state.end() - } - } - } -} -impl<'a> Serialize for ExportSpecifier<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 7)?; - state.serialize_field("type", "ExportSpecifier")?; - state.serialize_field("exported", &self.exported)?; - state.serialize_field("local", &self.local)?; - state.end() - } -} -impl<'a> Serialize for FuncArg<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self { - FuncArg::Expr(ref ex) => ex.serialize(serializer), - FuncArg::Pat(ref pat) => pat.serialize(serializer), - } - } -} - -impl<'a> Serialize for ClassBody<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 7)?; - state.serialize_field("type", "ClassBody")?; - let body: Vec = self.0.iter().map(MethodDef).collect(); - state.serialize_field("body", &body)?; - state.end() - } -} - -impl Serialize for VarKind { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let s = match self { - VarKind::Const => "const", - VarKind::Let => "let", - VarKind::Var => "var", - }; - serializer.serialize_str(s) - } -} -impl<'a> Serialize for Ident<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "Identifier")?; - let unescaped = unescaper(&self.name).unwrap_or_else(|| self.name.to_string()); - state.serialize_field("name", &unescaped)?; - state.end() - } -} - -impl<'a> Serialize for VarDecl<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "VariableDeclarator")?; - state.serialize_field("id", &self.id)?; - state.serialize_field("init", &self.init)?; - state.end() - } -} - -impl<'a> Serialize for FuncBody<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "BlockStatement")?; - state.serialize_field("body", &self.0)?; - state.end() - } -} - -impl<'a> Serialize for Stmt<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self { - Stmt::Labeled(ref l) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "LabeledStatement")?; - state.serialize_field("label", &l.label)?; - state.serialize_field("body", &l.body)?; - state.end() - } - Stmt::Block(ref b) => b.serialize(serializer), - Stmt::Break(ref b) => { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "BreakStatement")?; - state.serialize_field("label", &b)?; - state.end() - } - Stmt::Continue(ref c) => { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "ContinueStatement")?; - state.serialize_field("label", &c)?; - state.end() - } - Stmt::Debugger => { - let mut state = serializer.serialize_struct("Node", 1)?; - state.serialize_field("type", "DebuggerStatement")?; - state.end() - } - Stmt::DoWhile(ref d) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "DoWhileStatement")?; - state.serialize_field("test", &d.test)?; - state.serialize_field("body", &d.body)?; - state.end() - } - Stmt::Empty => { - let mut state = serializer.serialize_struct("Node", 1)?; - state.serialize_field("type", "EmptyStatement")?; - state.end() - } - Stmt::Expr(ref e) => { - let mut state = serializer.serialize_struct("Node", 1)?; - state.serialize_field("type", "ExpressionStatement")?; - state.serialize_field("expression", e)?; - state.end() - } - Stmt::For(ref f) => { - let mut state = serializer.serialize_struct("Node", 5)?; - state.serialize_field("type", "ForStatement")?; - state.serialize_field("init", &f.init)?; - state.serialize_field("test", &f.test)?; - state.serialize_field("update", &f.update)?; - state.serialize_field("body", &f.body)?; - state.end() - } - Stmt::ForIn(ref f) => { - let mut state = serializer.serialize_struct("Node", 4)?; - state.serialize_field("type", "ForInStatement")?; - state.serialize_field("left", &f.left)?; - state.serialize_field("right", &f.right)?; - state.serialize_field("body", &f.body)?; - state.serialize_field("each", &false)?; - state.end() - } - Stmt::ForOf(ref f) => { - let mut state = serializer.serialize_struct("Node", 4)?; - state.serialize_field("type", "ForOfStatement")?; - state.serialize_field("left", &f.left)?; - state.serialize_field("right", &f.right)?; - state.serialize_field("body", &f.body)?; - state.end() - } - Stmt::If(ref f) => { - let mut state = serializer.serialize_struct("Node", 4)?; - state.serialize_field("type", "IfStatement")?; - state.serialize_field("test", &f.test)?; - state.serialize_field("consequent", &f.consequent)?; - state.serialize_field("alternate", &f.alternate)?; - state.end() - } - Stmt::Return(ref r) => { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "ReturnStatement")?; - state.serialize_field("argument", r)?; - state.end() - } - Stmt::Switch(ref s) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "SwitchStatement")?; - state.serialize_field("discriminant", &s.discriminant)?; - state.serialize_field("cases", &s.cases)?; - state.end() - } - Stmt::Throw(ref t) => { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "ThrowStatement")?; - state.serialize_field("argument", t)?; - state.end() - } - Stmt::Try(ref t) => { - let mut state = serializer.serialize_struct("Node", 4)?; - state.serialize_field("type", "TryStatement")?; - state.serialize_field("block", &t.block)?; - state.serialize_field("handler", &t.handler)?; - state.serialize_field("finalizer", &t.finalizer)?; - state.end() - } - Stmt::Var(ref v) => { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "VariableStatement")?; - state.serialize_field("decls", &v)?; - state.end() - } - Stmt::While(ref w) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "WhileStatement")?; - state.serialize_field("test", &w.test)?; - state.serialize_field("body", &w.body)?; - state.end() - } - Stmt::With(ref w) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "WithStatement")?; - state.serialize_field("object", &w.object)?; - state.serialize_field("body", &w.body)?; - state.end() - } - } - } -} - -impl<'a> Serialize for Lit<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self { - Lit::Number(ref n) => serialize_number(serializer, n), - Lit::String(ref sl) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "Literal")?; - let (quote, value) = match sl { - StringLit::Double(ref s) => ('"', s), - StringLit::Single(ref s) => ('\'', s), - }; - let quoted = format!("{0}{1}{0}", quote, value); - let inner = if let Some(esc) = unescaper("ed) { - if esc.trim_matches(quote) != "\n" { - esc - } else { - format!("{0}{0}", quote) - } - } else { - value.to_string() - }; - state.serialize_field("value", &inner[1..inner.len() - 1])?; - state.serialize_field("raw", "ed)?; - state.end() - } - Lit::RegEx(ref r) => { - let mut state = serializer.serialize_struct("Node", 4)?; - state.serialize_field("type", "Literal")?; - state.serialize_field("raw", &format!("/{}/{}", r.pattern, r.flags))?; - state.serialize_field("value", &format_regex_value(r))?; - state.serialize_field("regex", r)?; - state.end() - } - Lit::Null => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "Literal")?; - state.serialize_field("raw", "null")?; - let value: Option<()> = None; - state.serialize_field("value", &value)?; - state.end() - } - Lit::Boolean(ref b) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "Literal")?; - let raw = if *b { "true" } else { "false" }; - state.serialize_field("raw", raw)?; - state.serialize_field("value", b)?; - state.end() - } - Lit::Template(ref t) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "TemplateLiteral")?; - state.serialize_field("quasis", &t.quasis)?; - state.serialize_field("expressions", &t.expressions)?; - state.end() - } - } - } -} - -fn format_regex_value(r: &RegEx) -> String { - let mut ret = String::from("/"); - let mut escaped = false; - for c in r.pattern.chars() { - if c == '\\' { - escaped = true; - ret.push(c); - } else if !escaped && c == '/' { - ret.push_str(r"\/"); - } else { - ret.push(c); - } - } - ret.push('/'); - if r.flags.is_empty() { - return ret; - } - if r.flags.contains('g') { - ret.push('g'); - } - if r.flags.contains('i') { - ret.push('i'); - } - if r.flags.contains('m') { - ret.push('m'); - } - if r.flags.contains('u') { - ret.push('u') - } - if r.flags.contains('y') { - ret.push('y') - } - ret.push_str(&r.flags.replace( - |c| c == 'g' || c == 'i' || c == 'm' || c == 'u' || c == 'y', - "", - )); - ret -} - -fn serialize_number(s: S, n: &str) -> Result -where - S: Serializer, -{ - let mut state = s.serialize_struct("Node", 3)?; - state.serialize_field("type", "Literal")?; - state.serialize_field("raw", &n)?; - if n.starts_with("0") { - if n.len() == 1 { - serialize_int(&mut state, 10, n)?; - } else if n[1..2].eq_ignore_ascii_case("x") { - serialize_int(&mut state, 16, &n[2..])?; - } else if n[1..2].eq_ignore_ascii_case("o") { - serialize_int(&mut state, 8, &n[2..])?; - } else if n[1..2].eq_ignore_ascii_case("b") { - serialize_int(&mut state, 2, &n[2..])?; - } else if n.chars().all(|c| c.is_digit(8)) { - serialize_int(&mut state, 8, n)?; - } else if n.contains('E') || n.contains('e') || n.contains('.') { - serialize_float(&mut state, n)?; - } else { - serialize_int(&mut state, 10, n)?; - } - } else if n.contains('E') || n.contains('e') || n.contains('.') { - serialize_float(&mut state, n)?; - } else { - serialize_int(&mut state, 10, n)?; - } - state.end() -} - -fn serialize_int(state: &mut T, radix: u32, n: &str) -> Result<(), T::Error> -where - T: SerializeStruct, -{ - if let Ok(value) = i128::from_str_radix(n, radix) { - if value < ::std::i32::MAX as i128 { - state.serialize_field("value", &(value as i32)) - } else { - state.serialize_field("value", &(value as f64)) - } - } else { - state.serialize_field("value", &::std::f32::NAN) - } -} -fn serialize_float(state: &mut T, n: &str) -> Result<(), T::Error> -where - T: SerializeStruct, -{ - if let Ok(value) = n.parse::() { - if value % 1.0 == 0.0 { - state.serialize_field("value", &(value as i32)) - } else { - state.serialize_field("value", &value) - } - } else { - state.serialize_field("value", &::std::f32::NAN) - } -} - -impl<'a> Serialize for Expr<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self { - Expr::Array(ref a) => { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "ArrayExpression")?; - state.serialize_field("elements", a)?; - state.end() - } - Expr::ArrowFunc(ref a) => { - let mut state = serializer.serialize_struct("Node", 6)?; - state.serialize_field("type", "ArrowFunctionExpression")?; - state.serialize_field("id", &a.id)?; - state.serialize_field("expression", &a.expression)?; - state.serialize_field("generator", &a.generator)?; - state.serialize_field("params", &a.params)?; - state.serialize_field("async", &a.is_async)?; - match a.body { - ArrowFuncBody::Expr(ref e) => { - state.serialize_field("body", e)?; - } - ArrowFuncBody::FuncBody(ref b) => { - state.serialize_field("body", b)?; - } - } - state.end() - } - Expr::ArrowParamPlaceHolder(_, _) => { - unreachable!("ArrowParamPlaceHolder Expression should never be returned by the parsing process"); - } - Expr::Assign(ref a) => { - let mut state = serializer.serialize_struct("Node", 4)?; - state.serialize_field("type", "AssignmentExpression")?; - state.serialize_field("left", &a.left)?; - state.serialize_field("operator", &a.operator)?; - state.serialize_field("right", &a.right)?; - state.end() - } - Expr::Await(ref a) => { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "AwaitExpression")?; - state.serialize_field("expression", a)?; - state.end() - } - Expr::Binary(ref b) => { - let mut state = serializer.serialize_struct("Node", 4)?; - state.serialize_field("type", "BinaryExpression")?; - state.serialize_field("left", &b.left)?; - state.serialize_field("operator", &b.operator)?; - state.serialize_field("right", &b.right)?; - state.end() - } - Expr::Call(ref c) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "CallExpression")?; - state.serialize_field("callee", &c.callee)?; - state.serialize_field("arguments", &c.arguments)?; - state.end() - } - Expr::Class(ref c) => { - let mut state = serializer.serialize_struct("Node", 4)?; - state.serialize_field("type", "ClassExpression")?; - state.serialize_field("id", &c.id)?; - state.serialize_field("superClass", &c.super_class)?; - state.serialize_field("body", &c.body)?; - state.end() - } - Expr::Conditional(ref c) => { - let mut state = serializer.serialize_struct("Node", 4)?; - state.serialize_field("type", "ConditionalExpression")?; - state.serialize_field("test", &c.test)?; - state.serialize_field("consequent", &c.consequent)?; - state.serialize_field("alternate", &c.alternate)?; - state.end() - } - Expr::Func(ref f) => { - let mut state = serializer.serialize_struct("Node", 6)?; - state.serialize_field("type", "FunctionExpression")?; - state.serialize_field("id", &f.id)?; - state.serialize_field("params", &f.params)?; - state.serialize_field("body", &f.body)?; - state.serialize_field("generator", &f.generator)?; - state.serialize_field("async", &f.is_async)?; - state.serialize_field("expression", &false)?; - state.end() - } - Expr::Ident(ref i) => i.serialize(serializer), - Expr::Lit(ref l) => l.serialize(serializer), - Expr::Logical(ref l) => { - let mut state = serializer.serialize_struct("Node", 4)?; - state.serialize_field("type", "LogicalExpression")?; - state.serialize_field("left", &l.left)?; - state.serialize_field("operator", &l.operator)?; - state.serialize_field("right", &l.right)?; - state.end() - } - Expr::Member(ref m) => { - let mut state = serializer.serialize_struct("Node", 4)?; - state.serialize_field("type", "MemberExpression")?; - state.serialize_field("object", &m.object)?; - state.serialize_field("property", &m.property)?; - state.serialize_field("computed", &m.computed)?; - state.end() - } - Expr::MetaProp(ref m) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "MetaProperty")?; - state.serialize_field("meta", &m.meta)?; - state.serialize_field("property", &m.property)?; - state.end() - } - Expr::New(ref n) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "NewExpression")?; - state.serialize_field("callee", &n.callee)?; - state.serialize_field("arguments", &n.arguments)?; - state.end() - } - Expr::Obj(ref o) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "ObjectExpression")?; - state.serialize_field("properties", o)?; - state.end() - } - Expr::Sequence(ref s) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "SequenceExpression")?; - state.serialize_field("expressions", s)?; - state.end() - } - Expr::Spread(ref s) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "SpreadElement")?; - state.serialize_field("argument", s)?; - state.end() - } - Expr::Super => { - let mut state = serializer.serialize_struct("Node", 1)?; - state.serialize_field("type", "Super")?; - state.end() - } - Expr::TaggedTemplate(ref t) => { - let mut state = serializer.serialize_struct("Node", 1)?; - state.serialize_field("type", "TaggedTemplateExpression")?; - state.serialize_field("tag", &t.tag)?; - state.serialize_field("quasi", &t.quasi)?; - state.end() - } - Expr::This => { - let mut state = serializer.serialize_struct("Node", 1)?; - state.serialize_field("type", "ThisExpression")?; - state.end() - } - Expr::Unary(ref u) => { - let mut state = serializer.serialize_struct("Node", 1)?; - state.serialize_field("type", "UnaryExpression")?; - state.serialize_field("argument", &u.argument)?; - state.serialize_field("operator", &u.operator)?; - state.serialize_field("prefix", &u.prefix)?; - state.end() - } - Expr::Update(ref u) => { - let mut state = serializer.serialize_struct("Node", 1)?; - state.serialize_field("type", "UpdateExpression")?; - state.serialize_field("argument", &u.argument)?; - state.serialize_field("operator", &u.operator)?; - state.serialize_field("prefix", &u.prefix)?; - state.end() - } - Expr::Yield(ref y) => { - let mut state = serializer.serialize_struct("Node", 1)?; - state.serialize_field("type", "YieldExpression")?; - state.serialize_field("argument", &y.argument)?; - state.serialize_field("delegate", &y.delegate)?; - state.end() - } - } - } -} - -impl<'a> Serialize for Pat<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self { - Pat::Array(ref a) => { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "ArrayPattern")?; - state.serialize_field("elements", a)?; - state.end() - } - Pat::Assign(ref a) => { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "AssignmentPattern")?; - state.serialize_field("left", &a.left)?; - state.serialize_field("right", &a.right)?; - state.end() - } - Pat::Ident(ref i) => i.serialize(serializer), - Pat::Obj(ref o) => { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "ObjectPattern")?; - state.serialize_field("properties", o)?; - state.end() - } - Pat::RestElement(ref r) => { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "RestElement")?; - state.serialize_field("argument", r)?; - state.end() - } - } - } -} - -impl Serialize for PropKind { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self { - PropKind::Ctor => serializer.serialize_str("constructor"), - PropKind::Get => serializer.serialize_str("get"), - PropKind::Init => serializer.serialize_str("init"), - PropKind::Method => serializer.serialize_str("method"), - PropKind::Set => serializer.serialize_str("set"), - } - } -} - -impl<'a> Serialize for Prop<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "Property")?; - state.serialize_field("key", &self.key)?; - state.serialize_field("computed", &self.computed)?; - state.serialize_field("kind", &self.kind)?; - state.serialize_field("method", &self.method)?; - state.serialize_field("shorthand", &self.short_hand)?; - if self.short_hand && self.value == PropValue::None { - state.serialize_field("value", &self.key)?; - } else { - state.serialize_field("value", &self.value)?; - } - if self.is_static { - state.serialize_field("static", &self.is_static)?; - } - state.end() - } -} - -struct MethodDef<'a>(pub &'a Prop<'a>); - -impl<'a> Serialize for MethodDef<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "MethodDefinition")?; - state.serialize_field("static", &self.0.is_static)?; - state.serialize_field("static", &self.0.is_static)?; - state.serialize_field("value", &self.0.value)?; - state.serialize_field("computed", &self.0.computed)?; - state.serialize_field("kind", &self.0.kind)?; - state.serialize_field("key", &self.0.key)?; - state.end() - } -} - -impl<'a> Serialize for TemplateLit<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "TemplateLiteral")?; - state.serialize_field("expressions", &self.expressions)?; - state.serialize_field("quasis", &self.quasis)?; - state.end() - } -} - -impl<'a> Serialize for TemplateElement<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "TemplateElement")?; - state.serialize_field("tail", &self.tail)?; - let mut value = ::std::collections::HashMap::new(); - let cooked = if let Some(s) = unescaper(&self.cooked) { - s - } else { - self.cooked.to_string() - }; - value.insert("cooked", cooked.as_str()); - let end_len = if self.raw.ends_with("${") { 2 } else { 1 }; - value.insert("raw", &self.raw[1..self.raw.len() - end_len]); - state.serialize_field("value", &value)?; - state.end() - } -} -impl<'a> Serialize for BlockStmt<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "BlockStatement")?; - state.serialize_field("body", &self.0)?; - state.end() - } -} - -impl<'a> Serialize for CatchClause<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 2)?; - state.serialize_field("type", "CatchClause")?; - state.serialize_field("param", &self.param)?; - state.serialize_field("body", &self.body)?; - state.end() - } -} -impl<'a> Serialize for SwitchCase<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "SwitchCase")?; - state.serialize_field("test", &self.test)?; - state.serialize_field("consequent", &self.consequent)?; - state.end() - } -} - -impl Serialize for AssignOp { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let s = match self { - AssignOp::Equal => "=", - AssignOp::PlusEqual => "+=", - AssignOp::MinusEqual => "-=", - AssignOp::TimesEqual => "*=", - AssignOp::DivEqual => "/=", - AssignOp::ModEqual => "%=", - AssignOp::LeftShiftEqual => "<<=", - AssignOp::RightShiftEqual => ">>=", - AssignOp::UnsignedRightShiftEqual => ">>>=", - AssignOp::OrEqual => "|=", - AssignOp::XOrEqual => "^=", - AssignOp::AndEqual => "&=", - AssignOp::PowerOfEqual => "**=", - }; - serializer.serialize_str(s) - } -} -impl Serialize for BinaryOp { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let s = match self { - BinaryOp::Equal => "==", - BinaryOp::NotEqual => "!=", - BinaryOp::StrictEqual => "===", - BinaryOp::StrictNotEqual => "!==", - BinaryOp::LessThan => "<", - BinaryOp::GreaterThan => ">", - BinaryOp::LessThanEqual => "<=", - BinaryOp::GreaterThanEqual => ">=", - BinaryOp::LeftShift => "<<", - BinaryOp::RightShift => ">>", - BinaryOp::UnsignedRightShift => ">>>", - BinaryOp::Plus => "+", - BinaryOp::Minus => "-", - BinaryOp::Times => "*", - BinaryOp::Over => "/", - BinaryOp::Mod => "%", - BinaryOp::Or => "|", - BinaryOp::XOr => "^", - BinaryOp::And => "&", - BinaryOp::In => "in", - BinaryOp::InstanceOf => "instanceof", - BinaryOp::PowerOf => "**", - }; - serializer.serialize_str(s) - } -} -impl Serialize for LogicalOp { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let s = match self { - LogicalOp::And => "&&", - LogicalOp::Or => "||", - }; - serializer.serialize_str(s) - } -} -impl Serialize for UnaryOp { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let s = match self { - UnaryOp::Minus => "-", - UnaryOp::Plus => "+", - UnaryOp::Not => "!", - UnaryOp::Tilde => "~", - UnaryOp::TypeOf => "typeof", - UnaryOp::Void => "void", - UnaryOp::Delete => "delete", - }; - serializer.serialize_str(s) - } -} -impl Serialize for UpdateOp { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let s = match self { - UpdateOp::Increment => "++", - UpdateOp::Decrement => "--", - }; - serializer.serialize_str(s) - } -} -impl<'a> Serialize for LoopLeft<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self { - LoopLeft::Expr(ref e) => e.serialize(serializer), - LoopLeft::Pat(ref p) => p.serialize(serializer), - LoopLeft::Variable(ref kind, ref v) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "VariableDeclaration")?; - state.serialize_field("kind", kind)?; - state.serialize_field("declarations", &[v])?; - state.end() - } - } - } -} -impl<'a> Serialize for LoopInit<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self { - LoopInit::Expr(ref e) => e.serialize(serializer), - LoopInit::Variable(ref kind, ref v) => { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "VariableDeclaration")?; - state.serialize_field("kind", kind)?; - state.serialize_field("declarations", v)?; - state.end() - } - } - } -} -impl<'a> Serialize for AssignPat<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut state = serializer.serialize_struct("Node", 3)?; - state.serialize_field("type", "AssignmentPattern")?; - state.serialize_field("left", &self.left)?; - state.serialize_field("right", &self.right)?; - state.end() - } -} - -use std::collections::VecDeque; -fn unescaper(s: &str) -> Option { - let mut queue: VecDeque<_> = String::from(s).chars().collect(); - let mut s = String::new(); - - while let Some(c) = queue.pop_front() { - if c != '\\' { - s.push(c); - continue; - } - - match queue.pop_front() { - Some('b') => s.push('\u{0008}'), - Some('f') => s.push('\u{000C}'), - Some('n') => s.push('\n'), - Some('r') => s.push('\r'), - Some('t') => s.push('\t'), - Some('v') => s.push('\u{000b}'), - Some('\'') => s.push('\''), - Some('\"') => s.push('\"'), - Some('\\') => s.push('\\'), - Some('u') => { - if let Some(x) = unescape_unicode(&mut queue) { - s.push(x); - } else { - return None; - } - } - Some('x') => { - if let Some(x) = unescape_byte(&mut queue) { - s.push(x) - } else { - return None; - } - } - Some('\0') => s.push('\0'), - Some(c) => { - if c.is_digit(8) { - if let Some(x) = unescape_octal(c, &mut queue) { - s.push(x); - } else { - return None; - } - } else { - s.push(c) - } - } - _ => return None, - }; - } - - Some(s) -} - -fn unescape_unicode(queue: &mut VecDeque) -> Option { - let ret = hex_char_code(queue)?; - ::std::char::from_u32(ret) -} - -fn hex_char_code(queue: &mut VecDeque) -> Option { - if let Some(c) = queue.pop_front() { - if c == '{' { - let mut x = 0; - while let Some(c) = queue.pop_front() { - if c == '}' { - break; - } - x = x * 16 + c.to_digit(16)?; - } - Some(x) - } else { - let mut x = c.to_digit(16)?; - for _ in 0..3 { - if let Some(u) = queue.pop_front() { - x = x * 16 + u.to_digit(16)?; - } - } - if x >= 0xD800 && x <= 0xDBFF { - debug_assert!(queue.pop_front() == Some('\\')); - debug_assert!(queue.pop_front() == Some('u')); - let high = (x - 0xD800) * 0x400; - let low = hex_char_code(queue)? - 0xDC00; - x = 0x10000 + high + low; - } - Some(x) - } - } else { - None - } -} - -fn unescape_byte(queue: &mut VecDeque) -> Option { - let mut s = String::new(); - - for _ in 0..2 { - if let Some(c) = queue.pop_front() { - s.push(c) - } else { - return None; - } - } - match u32::from_str_radix(&s, 16) { - Ok(u) => ::std::char::from_u32(u), - Err(e) => { - panic!("{}", e); - } - } -} - -fn unescape_octal(c: char, queue: &mut VecDeque) -> Option { - let (ret, ct) = if let Some(next) = queue.get(0) { - if !next.is_digit(8) { - let d = c.to_digit(8)?; - return std::char::from_u32(d); - } else if c >= '0' && c < '4' { - let s = if let Some(third) = queue.get(1) { - if !third.is_digit(8) { - format!("{}{}", c, next) - } else { - format!("{}{}{}", c, next, third) - } - } else { - format!("{}{}", c, next) - }; - let ct = s.len().saturating_sub(1); - match u32::from_str_radix(&s, 8) { - Ok(r) => (::std::char::from_u32(r), ct), - Err(e) => panic!("{}", e), - } - } else { - match u32::from_str_radix(&format!("{}{}", c, next), 8) { - Ok(r) => (::std::char::from_u32(r), 1), - Err(e) => panic!("{}", e), - } - } - } else { - (Some(c), 0) - }; - for _ in 0..ct { - let _ = queue.pop_front(); - } - ret -} - -#[cfg(test)] -mod test { - use super::*; - #[test] - fn four_hundred() { - let js = r#""\1\00\400\000\""#; - let expectation = "\"\u{1}\u{0} 0\u{0}\""; - assert_eq!(unescaper(js).unwrap(), expectation.to_string()); - } - #[test] - fn escape_lots() { - let js = "\"\\'\\\"\\\\\\b\\f\\n\\r\\t\\v\\0\""; - let expectation = "\"'\"\\\u{8}\u{c}\n\r\t\u{b}\u{0}\""; - assert_eq!(unescaper(js).unwrap(), expectation.to_string()); - } - - #[test] - fn escaped_new_line() { - let js = r#""\\\n""#; - let expectation = "\"\\\n\""; - assert_eq!(unescaper(js).unwrap(), expectation.to_string()); - } - - #[test] - fn unicode_ident() { - let js = "φ"; - let expectation = "φ"; - assert_eq!(unescaper(js).unwrap(), expectation.to_string()); - } - - #[test] - fn unicode_string() { - let js = r#""\uD834\uDF06\u2603\u03C6 \u{0000001F4a9}\u{1D306}\u{2603}\u{3c6} 𝌆☃φ""#; - let expectation = "\"𝌆☃φ 💩𝌆☃φ 𝌆☃φ\""; - assert_eq!(unescaper(js).unwrap(), expectation.to_string()); - } -} diff --git a/src/stmt.rs b/src/stmt.rs index 4849fd7..8ae9ecd 100644 --- a/src/stmt.rs +++ b/src/stmt.rs @@ -6,10 +6,9 @@ use crate::{Ident, ProgramPart}; /// A slightly more granular part of an es program than ProgramPart #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum Stmt { /// Any expression Expr(Expr), @@ -325,10 +324,9 @@ impl IntoAllocated for SwitchStmt where T: ToString { /// A single case part of a switch statement #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct SwitchCase { pub test: Option>, pub consequent: Vec>, @@ -348,10 +346,9 @@ impl IntoAllocated for SwitchCase where T: ToString { /// A collection of program parts wrapped in curly braces #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct BlockStmt(pub Vec>); impl IntoAllocated for BlockStmt where T: ToString { @@ -395,10 +392,9 @@ impl IntoAllocated for TryStmt where T: ToString { /// The error handling part of a `TryStmt` #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub struct CatchClause { pub param: Option>, pub body: BlockStmt, @@ -506,10 +502,9 @@ impl IntoAllocated for ForStmt where T: ToString { /// for (var i = 0;i < 100; i++) #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum LoopInit { Variable(VarKind, Vec>), Expr(Expr), @@ -592,10 +587,9 @@ impl IntoAllocated for ForOfStmt where T: ToString { /// in a for in or for of loop #[derive(Debug, Clone, PartialEq)] #[cfg_attr( - all(feature = "serde", not(feature = "esprima")), + feature = "serde", derive(Deserialize, Serialize) )] -#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))] pub enum LoopLeft { Expr(Expr), Variable(VarKind, VarDecl), diff --git a/tests/serde.rs b/tests/serde.rs deleted file mode 100644 index b36a886..0000000 --- a/tests/serde.rs +++ /dev/null @@ -1,169 +0,0 @@ -#![cfg(feature = "esprima")] -use pretty_env_logger::try_init; -use ressa::*; - -use resast::prelude::*; -use serde_json::{from_str, to_string_pretty, Value}; -use std::{fs::read_to_string, path::Path}; -#[test] -fn serde1() { - let ast = Program::Script(vec![ProgramPart::Decl(Decl::Func(Func { - id: Some(Ident::from("f")), - body: FuncBody(vec![]), - is_async: false, - generator: false, - params: vec![FuncArg::Expr(Expr::Ident(Ident::from("a")))], - }))]); - let json = to_string_pretty(&ast).expect("Failed to serialize ast"); - let expectation = r#"{ - "type": "Program", - "body": [ - { - "type": "FunctionDeclaration", - "id": { - "type": "Identifier", - "name": "f" - }, - "params": [ - { - "type": "Identifier", - "name": "a" - } - ], - "body": { - "type": "BlockStatement", - "body": [] - }, - "generator": false, - "expression": false, - "async": false - } - ], - "sourceType": "script" -} -"#; - - let r: serde_json::Value = serde_json::from_str(&json).expect("failed to deserialize json"); - let j: serde_json::Value = - serde_json::from_str(&expectation).expect("failed to deserialize expectation"); - assert_eq!(r, j); -} - -#[test] -fn serde_es5() { - let json = run_rs_parse("node_modules/everything.js/es5.js"); - let esparsed = esparse("node_modules/everything.js/es5.js"); - check_jsons("es5", json, esparsed); -} -#[test] -fn serde_es2015_script() { - let json = run_rs_parse("node_modules/everything.js/es2015-script.js"); - let esparsed = esparse("node_modules/everything.js/es2015-script.js"); - check_jsons("es2015-script", json, esparsed); -} -#[test] -fn serde_es2015_module() { - let json = run_rs_parse("node_modules/everything.js/es2015-module.js"); - let esparsed = esparse("node_modules/everything.js/es2015-module.js"); - check_jsons("es2015-module", json, esparsed); -} - -fn run_rs_parse(path: impl AsRef) -> Value { - let module = path.as_ref().ends_with("es2015-module.js"); - eprintln!("working on {:?} -> {}", path.as_ref(), module); - let js = get_js_file(path); - let mut b = Builder::new(); - b.set_module(module); - let mut parser = b.js(&js).build().expect("failed to create parser"); - let parsed = parser.parse().expect("failed to parse js"); - let raw = to_string_pretty(&parsed).expect("failed to convert to json string"); - from_str(&raw).expect("failed to revert to Value") -} - -fn check_jsons(name: &str, lhs: Value, rhs: Value) { - if lhs != rhs { - let f1 = - ::std::fs::File::create(&format!("{}.rs.json", name)).expect("failed to write rs.json"); - serde_json::to_writer_pretty(f1, &lhs).expect(""); - let f2 = - ::std::fs::File::create(&format!("{}.js.json", name)).expect("failed to write js.json"); - serde_json::to_writer_pretty(f2, &rhs).expect(""); - panic!("json doesn't match"); - } -} - -pub fn npm_install() { - let mut c = ::std::process::Command::new("npm"); - c.arg("i"); - c.output().expect("failed to run npm install"); -} - -pub fn get_js_file(path: impl AsRef<::std::path::Path>) -> String { - let path = path.as_ref(); - if !path.exists() { - npm_install(); - if !path.exists() { - panic!("npm install failed to make {:?} available", path); - } - } - read_to_string(path).expect(&format!("failed to read {} to a string", path.display())) -} - -pub fn esparse(path: impl AsRef<::std::path::Path>) -> Value { - let path = path.as_ref(); - if !path.exists() { - npm_install(); - if !path.exists() { - panic!("npm install failed to make {:?} available", path); - } - } - let esparse = ::std::process::Command::new("node") - .arg("run_es_parse.js") - .arg(path) - .output() - .expect("failed to execute run_es_parse.js"); - let json = String::from_utf8_lossy(&esparse.stdout).to_string(); - from_str(&json).expect(&format!("failed to convert {} to Value", path.display())) -} - -#[test] -fn func_args() { - let js = "function f(a, b = 0, [c,, d = 0, ...e], {f, g: h, i = 0, i: j = 0}, ...k){}"; - let mut parser = Parser::new(&js).expect(""); - let parsed = parser.parse().expect(""); - let raw = to_string_pretty(&parsed).expect("failed to convert ron to string"); - let json: Value = from_str(&raw).expect("failed to convert string to json"); - ::std::fs::write("args.js", js).expect("failed to write args.js"); - let esparsed = esparse("args.js"); - let _ = ::std::fs::remove_file("args.js"); - if json != esparsed { - let f1 = ::std::fs::File::create("func_args.rs.json").expect("failed to create rs.json"); - serde_json::to_writer_pretty(f1, &json).expect("failed to write rs.json"); - let f2 = ::std::fs::File::create("func_args.js.json").expect("failed to create js.json"); - serde_json::to_writer_pretty(f2, &esparsed).expect("failed to write js.json"); - panic!("json doesn't match"); - } -} - -#[test] -fn arrow_func_args() { - let _ = try_init(); - let js = "({i = 0}, ...k) => {;};"; - let mut parser = Parser::new(&js).expect(""); - let parsed = parser.parse().expect(""); - let raw = to_string_pretty(&parsed).expect("failed to convert ron to string"); - let json: Value = from_str(&raw).expect("failed to convert string to json"); - ::std::fs::write("arrow-args.js", js).expect("failed to write args.js"); - let esparsed = esparse("arrow-args.js"); - let _ = ::std::fs::remove_file("arrow-args.js"); - if json != esparsed { - let f1 = - ::std::fs::File::create("arrow_func_args.rs.json").expect("failed to create rs.json"); - serde_json::to_writer_pretty(f1, &json).expect("failed to write rs.json"); - let f2 = - ::std::fs::File::create("arrow_func_args.js.json").expect("failed to create js.json"); - serde_json::to_writer_pretty(f2, &esparsed).expect("failed to write js.json"); - let _ = ::std::fs::write("arrow_func_args2.ron", &format!("{:#?}", parsed)); - panic!("json doesn't match"); - } -} From 5c87ff429e8bf654f96ffe2d4e50889a542e9fb7 Mon Sep 17 00:00:00 2001 From: Robert Masen Date: Sun, 11 Jun 2023 09:51:30 -0500 Subject: [PATCH 4/8] upgrade edition to 2021 --- Cargo.toml | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5c79efd..fd99c86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "resast" version = "0.6.0-alpha.3" authors = ["rfm "] -edition = "2018" +edition = "2021" description = "Rusty-ECMAScript Abstract Syntax Tree" repository = "https://github.com/rusty-ecma/resast" license = "MIT" @@ -10,14 +10,8 @@ keywords = ["JavaScript", "parsing", "JS", "ES", "ECMA"] categories = ["parsing", "text-processing", "web-programming"] [dependencies] -serde = { version = "1", optional = true } -serde_derive = { version = "1", optional = true } - -[dev-dependencies] -serde_json = "1" -ressa = "0.7.0-beta-6" -pretty_env_logger = "0.3" +serde = { version = "1", optional = true, features = ["derive"] } [features] default = [] -serialization = ["serde", "serde_derive"] +serialization = ["dep:serde"] From 41f15b50808adb450fa8481f4fccfd01db6c105c Mon Sep 17 00:00:00 2001 From: Robert Masen Date: Sun, 11 Jun 2023 10:16:46 -0500 Subject: [PATCH 5/8] fix serde feature --- Cargo.toml | 2 +- src/decl.rs | 18 ++++- src/expr.rs | 123 ++++++++++++++++++++++---------- src/lib.rs | 13 +++- src/pat.rs | 16 +++-- src/spanned/decl.rs | 87 +++++++++++++++++++++++ src/spanned/expr.rs | 162 ++++++++++++++++++++++++++++++++++++++++++ src/spanned/mod.rs | 67 +++++++++++++++++ src/spanned/pat.rs | 35 +++++++++ src/spanned/stmt.rs | 78 ++++++++++++++++++++ src/spanned/tokens.rs | 38 ++++++++++ src/stmt.rs | 64 ++++++++++++----- 12 files changed, 635 insertions(+), 68 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fd99c86..ac50330 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,4 @@ serde = { version = "1", optional = true, features = ["derive"] } [features] default = [] -serialization = ["dep:serde"] +serde = ["dep:serde"] diff --git a/src/decl.rs b/src/decl.rs index 0677858..52ea0b9 100644 --- a/src/decl.rs +++ b/src/decl.rs @@ -3,6 +3,9 @@ use crate::pat::Pat; use crate::{VarKind, IntoAllocated}; use crate::{Class, Func, Ident}; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// The declaration of a variable, function, class, import or export #[derive(Debug, Clone, PartialEq)] #[cfg_attr( @@ -83,7 +86,10 @@ impl IntoAllocated for VarDecl where T: ToString { /// import {Thing} from './stuff.js'; /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ModImport { pub specifiers: Vec>, pub source: Lit, @@ -144,7 +150,10 @@ impl IntoAllocated for ImportSpecifier where T: ToString { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct NormalImportSpec { pub alias: Option>, pub imported: Ident, @@ -211,7 +220,10 @@ impl IntoAllocated for ModExport where T: ToString { /// export function thing() {} /// export {stuff} from 'place'; #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum NamedExportDecl { Decl(Decl), Specifier(Vec>, Option>), diff --git a/src/expr.rs b/src/expr.rs index b82a6e1..ae34a5f 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1,6 +1,10 @@ use crate::pat::Pat; use crate::{AssignOp, BinaryOp, LogicalOp, PropKind, UnaryOp, UpdateOp, IntoAllocated}; use crate::{Class, Func, FuncArg, FuncBody, Ident}; + +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// A slightly more granular program part that a statement #[derive(Debug, Clone, PartialEq)] #[cfg_attr( @@ -136,8 +140,10 @@ pub type ArrayExpr = Vec>>; pub type ObjExpr = Vec>; /// A single part of an object literal #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub enum ObjProp { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub enum ObjProp { Prop(Prop), Spread(Expr), } @@ -187,8 +193,10 @@ impl IntoAllocated for Prop where T: ToString { /// An object literal or class property identifier #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub enum PropKey { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub enum PropKey { Lit(Lit), Expr(Expr), Pat(Pat), @@ -208,7 +216,10 @@ impl IntoAllocated for PropKey where T: ToString { /// The value of an object literal or class property #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum PropValue { Expr(Expr), Pat(Pat), @@ -229,8 +240,10 @@ impl IntoAllocated for PropValue where T: ToString { /// An operation that takes one argument #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct UnaryExpr { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct UnaryExpr { pub operator: UnaryOp, pub prefix: bool, pub argument: Box>, @@ -250,8 +263,10 @@ impl IntoAllocated for UnaryExpr where T: ToString { /// Increment or decrementing a value #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct UpdateExpr { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct UpdateExpr { pub operator: UpdateOp, pub argument: Box>, pub prefix: bool, @@ -271,8 +286,10 @@ impl IntoAllocated for UpdateExpr where T: ToString { /// An operation that requires 2 arguments #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct BinaryExpr { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct BinaryExpr { pub operator: BinaryOp, pub left: Box>, pub right: Box>, @@ -292,8 +309,10 @@ impl IntoAllocated for BinaryExpr where T: ToString { /// An assignment or update + assignment operation #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct AssignExpr { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct AssignExpr { pub operator: AssignOp, pub left: AssignLeft, pub right: Box>, @@ -313,8 +332,10 @@ impl IntoAllocated for AssignExpr where T: ToString { /// The value being assigned to #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub enum AssignLeft { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub enum AssignLeft { Pat(Pat), Expr(Box>), } @@ -336,8 +357,10 @@ impl IntoAllocated for AssignLeft where T: ToString { /// false || true /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct LogicalExpr { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct LogicalExpr { pub operator: LogicalOp, pub left: Box>, pub right: Box>, @@ -361,8 +384,10 @@ impl IntoAllocated for LogicalExpr where T: ToString { /// c.stuff; /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct MemberExpr { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct MemberExpr { pub object: Box>, pub property: Box>, pub computed: bool, @@ -385,8 +410,10 @@ impl IntoAllocated for MemberExpr where T: ToString { /// var a = true ? 'stuff' : 'things'; /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct ConditionalExpr { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct ConditionalExpr { pub test: Box>, pub alternate: Box>, pub consequent: Box>, @@ -409,8 +436,10 @@ impl IntoAllocated for ConditionalExpr where T: ToString { /// Math.random() /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct CallExpr { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct CallExpr { pub callee: Box>, pub arguments: Vec>, } @@ -431,8 +460,10 @@ impl IntoAllocated for CallExpr where T: ToString { /// new Uint8Array(32); /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct NewExpr { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct NewExpr { pub callee: Box>, pub arguments: Vec>, } @@ -459,8 +490,10 @@ pub type SequenceExpr = Vec>; /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct ArrowFuncExpr { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct ArrowFuncExpr { pub id: Option>, pub params: Vec>, pub body: ArrowFuncBody, @@ -486,8 +519,10 @@ impl IntoAllocated for ArrowFuncExpr where T: ToString { /// The body portion of an arrow function can be either an expression or a block of statements #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub enum ArrowFuncBody { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub enum ArrowFuncBody { FuncBody(FuncBody), Expr(Box>), } @@ -512,8 +547,10 @@ impl IntoAllocated for ArrowFuncBody where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct YieldExpr { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct YieldExpr { pub argument: Option>>, pub delegate: bool, } @@ -531,8 +568,10 @@ impl IntoAllocated for YieldExpr where T: ToString { /// A Template literal preceded by a function identifier /// see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates) for more details #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct TaggedTemplateExpr { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct TaggedTemplateExpr { pub tag: Box>, pub quasi: TemplateLit, } @@ -632,8 +671,10 @@ impl TemplateElement { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct MetaProp { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct MetaProp { pub meta: Ident, pub property: Ident, } @@ -709,8 +750,10 @@ impl Lit { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub enum StringLit { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub enum StringLit { Double(T), Single(T), } @@ -759,8 +802,10 @@ where } /// A regular expression literal #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -#[cfg_attr(all(feature = "serialization"), serde(rename_all = "camelCase"))] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct RegEx { pub pattern: T, pub flags: Option, diff --git a/src/lib.rs b/src/lib.rs index 8a9d21d..8bc9ace 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,9 @@ pub mod pat; pub mod spanned; pub mod stmt; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + use std::{borrow::Cow, fmt::Debug}; use decl::Decl; @@ -156,7 +159,10 @@ impl IntoAllocated for Dir where T: ToString { /// let y = function q() {} /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct Func { pub id: Option>, pub params: Vec>, @@ -271,7 +277,10 @@ impl IntoAllocated for FuncBody where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct Class { pub id: Option>, pub super_class: Option>>, diff --git a/src/pat.rs b/src/pat.rs index f67c7f1..a62d5dc 100644 --- a/src/pat.rs +++ b/src/pat.rs @@ -1,5 +1,9 @@ use crate::expr::{Expr, Prop}; use crate::{Ident, IntoAllocated}; + +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// All of the different ways you can declare an identifier /// and/or value #[derive(Debug, Clone, PartialEq)] @@ -36,8 +40,10 @@ impl Pat { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub enum ArrayPatPart { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub enum ArrayPatPart { Pat(Pat), Expr(Expr), } @@ -57,8 +63,10 @@ impl IntoAllocated for ArrayPatPart where T: ToString { pub type ObjPat = Vec>; /// A single part of an ObjectPat #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub enum ObjPatPart { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub enum ObjPatPart { Assign(Prop), Rest(Box>), } diff --git a/src/spanned/decl.rs b/src/spanned/decl.rs index 13e452b..616f185 100644 --- a/src/spanned/decl.rs +++ b/src/spanned/decl.rs @@ -9,8 +9,15 @@ use super::tokens::{ }; use super::{ListEntry, Node, SourceLocation}; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// The declaration of a variable, function, class, import or export #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum Decl { /// A variable declaration /// ```js @@ -108,6 +115,10 @@ impl Node for Decl { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct VarDecls { pub keyword: VarKind, pub decls: Vec>>, @@ -142,6 +153,10 @@ impl Node for VarDecls { /// The identifier and optional value of a variable declaration #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct VarDecl { pub id: Pat, pub eq: Option, @@ -180,6 +195,10 @@ impl Node for VarDecl { /// in an ES Mod, it would be either an import or /// export at the top level #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum ModDecl { Import(ModImport), Export(ModExport), @@ -211,6 +230,10 @@ impl Node for ModDecl { /// import {Thing} from './stuff.js'; /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ModImport { pub keyword_import: Import, pub specifiers: Vec>>, @@ -236,6 +259,10 @@ impl Node for ModImport { /// The name of the thing being imported #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum ImportSpecifier { /// A specifier in curly braces, this might /// have a local alias @@ -285,6 +312,10 @@ impl Node for ImportSpecifier { } #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct NormalImportSpecs { pub open_brace: OpenBrace, pub specs: Vec>>, @@ -312,6 +343,10 @@ impl Node for NormalImportSpecs { } #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct NormalImportSpec { pub imported: Ident, pub alias: Option>, @@ -338,6 +373,10 @@ impl Node for NormalImportSpec { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct DefaultImportSpec { pub id: Ident, } @@ -356,6 +395,10 @@ impl Node for DefaultImportSpec { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct NamespaceImportSpec { pub star: Asterisk, pub keyword: As, @@ -383,6 +426,10 @@ impl Node for NamespaceImportSpec { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ModExport { pub keyword: Export, pub spec: ModExportSpecifier, @@ -406,6 +453,10 @@ impl Node for ModExport { /// Something exported from this module #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum ModExportSpecifier { /// ```js /// export default function() {}; @@ -470,6 +521,10 @@ impl Node for ModExportSpecifier { /// export function thing() {} /// export {stuff} from 'place'; #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum NamedExportDecl { Decl(Decl), Specifier(NamedExportSpec), @@ -495,6 +550,10 @@ impl Node for NamedExportDecl { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct DefaultExportDecl { pub keyword: Default, pub value: DefaultExportDeclValue, @@ -524,6 +583,10 @@ impl Node for DefaultExportDecl { /// export default class Thing {} /// ``` #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum ExportDeclValue { Decl(Decl), Expr(Expr), @@ -552,6 +615,10 @@ impl Node for ExportDeclValue { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum DefaultExportDeclValue { Decl(Decl), Expr(Expr), @@ -577,6 +644,10 @@ impl Node for DefaultExportDeclValue { } #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct NamedExportSpec { pub list: ExportList, pub source: Option>, @@ -606,6 +677,10 @@ impl Node for NamedExportSpec { } #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct NamedExportSource { pub keyword_from: From, pub module: Lit, @@ -631,6 +706,10 @@ impl Node for NamedExportSource { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ExportList { pub open_brace: OpenBrace, pub elements: Vec>>, @@ -662,6 +741,10 @@ impl Node for ExportList { /// export {Stuff as NewThing} from 'place' /// ``` #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ExportSpecifier { pub local: Ident, pub alias: Option>, @@ -692,6 +775,10 @@ impl Node for ExportSpecifier { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct Alias { pub keyword: As, pub ident: Ident, diff --git a/src/spanned/expr.rs b/src/spanned/expr.rs index e352ac0..67f113f 100644 --- a/src/spanned/expr.rs +++ b/src/spanned/expr.rs @@ -9,9 +9,15 @@ use super::tokens::{ UnaryOp, UpdateOp, Yield, }; use super::{FuncArgEntry, ListEntry, Node, Slice, SourceLocation}; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; /// A slightly more granular program part that a statement #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum Expr { /// `[0,,]` Array(ArrayExpr), @@ -167,6 +173,10 @@ type ArrayExprEntry = ListEntry>>; /// `[a, b, c]` #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ArrayExpr { pub open_bracket: OpenBracket, pub elements: Vec>, @@ -195,6 +205,10 @@ impl Node for ArrayExpr { /// `{a: 'b', c, ...d}` #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ObjExpr { pub open_brace: OpenBrace, pub props: Vec>>, @@ -224,6 +238,10 @@ impl Node for ObjExpr { /// A single part of an object literal #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum ObjProp { Prop(Prop), Spread(SpreadExpr), @@ -249,6 +267,10 @@ impl Node for ObjProp { } #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct SpreadExpr { pub dots: Ellipsis, pub expr: Expr, @@ -274,6 +296,10 @@ impl Node for SpreadExpr { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum Prop { Init(PropInit), Method(PropMethod), @@ -332,6 +358,10 @@ impl Prop { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct PropInit { pub key: PropInitKey, pub colon: Option, @@ -372,6 +402,10 @@ impl PropInit { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct PropInitKey { pub value: PropKey, pub brackets: Option<(OpenBracket, CloseBracket)>, @@ -401,6 +435,10 @@ impl Node for PropInitKey { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct PropMethod { pub keyword_static: Option, pub keyword_async: Option, @@ -445,6 +483,10 @@ impl Node for PropMethod { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct PropCtor { pub keyword: PropInitKey, pub open_paren: OpenParen, @@ -476,6 +518,10 @@ impl Node for PropCtor { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct PropGet { pub keyword_static: Option, pub keyword_get: Get, @@ -515,6 +561,10 @@ impl Node for PropGet { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct PropSet { pub keyword_static: Option, pub keyword_set: Set, @@ -557,6 +607,10 @@ impl Node for PropSet { /// An object literal or class property identifier #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum PropKey { Lit(Lit), Expr(Expr), @@ -586,6 +640,10 @@ impl Node for PropKey { /// The value of an object literal or class property #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum PropValue { Expr(Expr), Pat(Pat), @@ -615,6 +673,10 @@ impl Node for PropValue { /// An operation that takes one argument #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct UnaryExpr { pub operator: UnaryOp, pub argument: Box>, @@ -649,6 +711,10 @@ impl Node for UnaryExpr { /// Increment or decrementing a value #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct UpdateExpr { pub operator: UpdateOp, pub argument: Box>, @@ -690,6 +756,10 @@ impl Node for UpdateExpr { /// An operation that requires 2 arguments #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct BinaryExpr { pub operator: BinaryOp, pub left: Box>, @@ -718,6 +788,10 @@ impl Node for BinaryExpr { /// An assignment or update + assignment operation #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct AssignExpr { pub operator: AssignOp, pub left: AssignLeft, @@ -745,6 +819,10 @@ impl Node for AssignExpr { } #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct AwaitExpr { pub keyword: Await, pub expr: Expr, @@ -771,6 +849,10 @@ impl Node for AwaitExpr { /// The value being assigned to #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum AssignLeft { Pat(Pat), Expr(Box>), @@ -801,6 +883,10 @@ impl Node for AssignLeft { /// false || true /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct LogicalExpr { pub operator: LogicalOp, pub left: Box>, @@ -833,6 +919,10 @@ impl Node for LogicalExpr { /// c.stuff; /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct MemberExpr { pub object: Box>, pub property: Box>, @@ -871,6 +961,10 @@ impl Node for MemberExpr { } #[derive(PartialEq, Debug, Clone, Copy)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum MemberIndexer { Period(Period), Computed { @@ -899,6 +993,10 @@ impl Node for MemberIndexer { /// var a = true ? 'stuff' : 'things'; /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ConditionalExpr { pub test: Box>, pub question_mark: QuestionMark, @@ -933,6 +1031,10 @@ impl Node for ConditionalExpr { /// Math.random() /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct CallExpr { pub callee: Box>, pub open_paren: OpenParen, @@ -967,6 +1069,10 @@ impl Node for CallExpr { /// new Uint8Array(32); /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct NewExpr { pub keyword: New, pub callee: Box>, @@ -1028,6 +1134,10 @@ impl Node for SequenceExpr { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ArrowParamPlaceHolder { // async keyword pub keyword: Option, @@ -1078,6 +1188,10 @@ impl Node for ArrowParamPlaceHolder { /// } /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ArrowFuncExpr { pub keyword: Option, pub star: Option, @@ -1123,6 +1237,10 @@ impl Node for ArrowFuncExpr { /// The body portion of an arrow function can be either an expression or a block of statements #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum ArrowFuncBody { FuncBody(FuncBody), Expr(Box>), @@ -1156,6 +1274,10 @@ impl Node for ArrowFuncBody { /// } /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct YieldExpr { pub keyword: Yield, pub argument: Option>>, @@ -1190,6 +1312,10 @@ impl Node for YieldExpr { /// A Template literal preceded by a function identifier /// see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates) for more details #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct TaggedTemplateExpr { pub tag: Box>, pub quasi: TemplateLit, @@ -1219,6 +1345,10 @@ impl Node for TaggedTemplateExpr { /// `I own ${0} birds`; /// ``` #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct TemplateLit { pub quasis: Vec>, pub expressions: Vec>, @@ -1256,6 +1386,10 @@ impl Node for TemplateLit { /// The text part of a `TemplateLiteral` #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct TemplateElement { pub open_quote: QuasiQuote, pub content: Slice, @@ -1305,6 +1439,10 @@ impl Node for TemplateElement { /// } /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct MetaProp { pub meta: Ident, pub dot: Period, @@ -1333,6 +1471,10 @@ impl Node for MetaProp { /// A literal value #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum Lit { /// `null` Null(Null), @@ -1384,6 +1526,10 @@ impl Lit { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum Boolean { True(True), False(False), @@ -1436,6 +1582,10 @@ impl Node for Lit { } #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct StringLit { pub open_quote: Quote, pub content: Slice, @@ -1464,6 +1614,10 @@ impl Node for StringLit { /// A regular expression literal #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct RegEx { pub open_slash: ForwardSlash, pub pattern: Slice, @@ -1498,6 +1652,10 @@ impl Node for RegEx { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct WrappedExpr { pub open_paren: OpenParen, pub expr: Expr, @@ -1525,6 +1683,10 @@ impl Node for WrappedExpr { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct SequenceExprEntry { pub expr: Expr, pub comma: Option, diff --git a/src/spanned/mod.rs b/src/spanned/mod.rs index 5b20bc6..3052860 100644 --- a/src/spanned/mod.rs +++ b/src/spanned/mod.rs @@ -12,6 +12,9 @@ use stmt::Stmt; use crate::IntoAllocated; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + use self::{ pat::RestPat, tokens::{ @@ -25,6 +28,10 @@ pub trait Node { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct Ident { pub slice: Slice, } @@ -76,6 +83,10 @@ impl Ident { /// with a flag denoting if the representation is /// a ES6 Mod or a Script. #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum Program { /// An ES6 Mod Mod(Vec>), @@ -128,6 +139,10 @@ impl Node for Vec> { /// A single part of a Javascript program. /// This will be either a Directive, Decl or a Stmt #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum ProgramPart { /// A Directive like `'use strict';` Dir(Dir), @@ -170,6 +185,10 @@ impl ProgramPart { /// pretty much always `'use strict'`, this can appear at the /// top of a file or function #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct Dir { pub expr: Lit, pub dir: T, @@ -211,6 +230,10 @@ impl Node for Dir { /// let y = function q() {} /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct Func { pub keyword: Function, pub id: Option>, @@ -264,6 +287,10 @@ impl Node for Func { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct FuncArgEntry { pub value: FuncArg, pub comma: Option, @@ -290,6 +317,10 @@ impl Node for FuncArgEntry { /// A single function argument from a function signature #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum FuncArg { Expr(Expr), Pat(Pat), @@ -319,6 +350,10 @@ impl Node for FuncArg { /// The block statement that makes up the function's body #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct FuncBody { pub open_brace: OpenBrace, pub stmts: Vec>, @@ -368,6 +403,10 @@ impl Node for FuncBody { /// } /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct Class { pub keyword: tokens::Class, pub id: Option>, @@ -398,6 +437,10 @@ impl Node for Class { } #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct SuperClass { pub keyword_extends: Extends, pub expr: Expr, @@ -411,6 +454,10 @@ impl IntoAllocated for SuperClass where T: ToString { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ClassBody { pub open_brace: OpenBrace, pub props: Vec>, @@ -434,6 +481,10 @@ impl Node for ClassBody { /// The kind of variable being defined (`var`/`let`/`const`) #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum VarKind { Var(Option), Let(Let), @@ -466,6 +517,10 @@ impl VarKind { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct Slice { pub source: T, pub loc: SourceLocation, @@ -494,6 +549,10 @@ impl Slice { } #[derive(Debug, Clone, PartialEq, Copy)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct SourceLocation { pub start: Position, pub end: Position, @@ -528,6 +587,10 @@ impl core::cmp::PartialOrd for SourceLocation { } #[derive(Debug, Clone, PartialEq, Copy)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct Position { pub line: u32, pub column: u32, @@ -594,6 +657,10 @@ impl std::ops::Sub for Position { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ListEntry { pub item: Item, pub comma: Option, diff --git a/src/spanned/pat.rs b/src/spanned/pat.rs index c630196..07855ff 100644 --- a/src/spanned/pat.rs +++ b/src/spanned/pat.rs @@ -4,9 +4,16 @@ use crate::spanned::Ident; use super::tokens::{CloseBrace, CloseBracket, Comma, Ellipsis, OpenBrace, OpenBracket, Token}; use super::{AssignOp, ListEntry, Node, SourceLocation}; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// All of the different ways you can declare an identifier /// and/or value #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum Pat { Ident(Ident), Obj(ObjPat), @@ -38,6 +45,10 @@ impl Node for Pat { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ArrayPat { pub open_bracket: OpenBracket, pub elements: Vec>>>, @@ -65,6 +76,10 @@ impl Node for ArrayPat { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ArrayElement { pub part: Option>, pub comma: Option, @@ -81,6 +96,10 @@ impl IntoAllocated for ArrayElement where T: ToString { } #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum ArrayPatPart { Pat(Pat), Expr(Expr), @@ -112,6 +131,10 @@ type ObjEntry = ListEntry>; /// similar to an `ObjectExpr` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ObjPat { pub open_brace: OpenBrace, pub props: Vec>, @@ -140,6 +163,10 @@ impl Node for ObjPat { /// A single part of an ObjectPat #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum ObjPatPart { Assign(Prop), Rest(Box>), @@ -165,6 +192,10 @@ impl Node for ObjPatPart { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct RestPat { pub dots: Ellipsis, pub pat: Pat, @@ -191,6 +222,10 @@ impl Node for RestPat { /// An assignment as a pattern #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct AssignPat { pub left: Box>, pub operator: AssignOp, diff --git a/src/spanned/stmt.rs b/src/spanned/stmt.rs index 6ed0e90..4ab3e44 100644 --- a/src/spanned/stmt.rs +++ b/src/spanned/stmt.rs @@ -12,9 +12,15 @@ use super::tokens::{ While, With, }; use super::{ListEntry, Node, SourceLocation}; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; /// A slightly more granular part of an es program than ProgramPart #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum Stmt { /// Any expression Expr { @@ -369,6 +375,10 @@ impl Node for Stmt { /// //rand !== 0 /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct WithStmt { pub keyword: With, pub open_paren: OpenParen, @@ -407,6 +417,10 @@ impl Node for WithStmt { /// } /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct LabeledStmt { pub label: Ident, pub colon: Colon, @@ -443,6 +457,10 @@ impl Node for LabeledStmt { /// } /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct IfStmt { pub keyword: If, pub open_paren: OpenParen, @@ -480,6 +498,10 @@ impl Node for IfStmt { } #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ElseStmt { pub keyword: Else, pub body: Stmt, @@ -520,6 +542,10 @@ impl Node for ElseStmt { /// } /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct SwitchStmt { pub keyword: Switch, pub open_paren: OpenParen, @@ -557,6 +583,10 @@ impl Node for SwitchStmt { /// A single case part of a switch statement #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct SwitchCase { pub keyword: SwitchCaseKeyword, pub test: Option>, @@ -593,6 +623,10 @@ impl IntoAllocated for SwitchCase where T: ToString { /// A collection of program parts wrapped in curly braces #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct BlockStmt { pub open_brace: OpenBrace, pub stmts: Vec>, @@ -631,6 +665,10 @@ impl Node for BlockStmt { /// } /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct TryStmt { pub keyword: Try, pub block: BlockStmt, @@ -668,6 +706,10 @@ impl Node for TryStmt { /// The error handling part of a `TryStmt` #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct CatchClause { pub keyword: Catch, pub param: Option>, @@ -696,6 +738,10 @@ impl Node for CatchClause { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct CatchArg { pub open_paren: OpenParen, pub param: Pat, @@ -724,6 +770,10 @@ impl Node for CatchArg { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct FinallyClause { pub keyword: Finally, pub body: BlockStmt, @@ -763,6 +813,10 @@ impl Node for FinallyClause { /// } /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct WhileStmt { pub keyword: While, pub open_paren: OpenParen, @@ -801,6 +855,10 @@ impl Node for WhileStmt { /// } while (Math.floor(Math.random() * 100) < 75) /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct DoWhileStmt { pub keyword_do: Do, pub body: Box>, @@ -849,6 +907,10 @@ impl Node for DoWhileStmt { /// } /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ForStmt { pub keyword: For, pub open_paren: OpenParen, @@ -893,6 +955,10 @@ impl Node for ForStmt { /// // vvvvvvvvv /// for (var i = 0;i < 100; i++) #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum LoopInit { Variable(VarKind, Vec>>), Expr(Expr), @@ -939,6 +1005,10 @@ impl Node for LoopInit { /// //prints a, b /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ForInStmt { pub keyword_for: For, pub open_paren: OpenParen, @@ -983,6 +1053,10 @@ impl Node for ForInStmt { /// //prints 2, 3, 4, 5, 6 /// ``` #[derive(PartialEq, Debug, Clone)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct ForOfStmt { pub keyword_for: For, pub open_paren: OpenParen, @@ -1023,6 +1097,10 @@ impl Node for ForOfStmt { /// The values on the left hand side of the keyword /// in a for in or for of loop #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum LoopLeft { Expr(Expr), Variable(VarKind, VarDecl), diff --git a/src/spanned/tokens.rs b/src/spanned/tokens.rs index be41d5d..feb3f8c 100644 --- a/src/spanned/tokens.rs +++ b/src/spanned/tokens.rs @@ -1,6 +1,8 @@ //! This modules contains a collection of discrete tokens use crate::spanned::{Node, Position, SourceLocation}; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; macro_rules! impl_token { ($what:ty, $s:expr) => { @@ -71,6 +73,10 @@ macro_rules! impl_token { macro_rules! define_token { ($name:ident, $s:expr) => { #[derive(Debug, Clone, Copy, PartialEq)] + #[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) + )] #[doc = $s] pub struct $name(Position); impl_token!($name, $s); @@ -199,6 +205,10 @@ define_token!(TripleGreaterThan, ">>>"); define_token!(TripleGreaterThanEqual, ">>>="); #[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum Quote { Double(DoubleQuote), Single(SingleQuote), @@ -228,6 +238,10 @@ impl Token for Quote { } #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum QuasiQuote { BackTick(BackTick), CloseBrace(CloseBrace), @@ -262,6 +276,10 @@ impl Token for QuasiQuote { /// The available operators for assignment Exprs #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum AssignOp { Equal(Equal), PlusEqual(PlusEqual), @@ -300,6 +318,10 @@ impl Node for AssignOp { /// The available logical operators #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum LogicalOp { Or(DoublePipe), And(DoubleAmpersand), @@ -316,6 +338,10 @@ impl Node for LogicalOp { /// The available operations for `Binary` Exprs #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum BinaryOp { Equal(DoubleEqual), NotEqual(BangEqual), @@ -373,6 +399,10 @@ define_token!(DoublePlus, "++"); define_token!(DoubleMinus, "--"); /// `++` or `--` #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum UpdateOp { Increment(DoublePlus), Decrement(DoubleMinus), @@ -390,6 +420,10 @@ impl Node for UpdateOp { /// The allowed operators for an Expr /// to be `Unary` #[derive(Debug, Clone, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum UnaryOp { Minus(Minus), Plus(Plus), @@ -415,6 +449,10 @@ impl Node for UnaryOp { } #[derive(Debug, PartialEq, Clone, Copy)] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub enum SwitchCaseKeyword { Case(Case), Default(Default), diff --git a/src/stmt.rs b/src/stmt.rs index 8ae9ecd..1c97115 100644 --- a/src/stmt.rs +++ b/src/stmt.rs @@ -3,6 +3,11 @@ use crate::expr::Expr; use crate::pat::Pat; use crate::{VarKind, IntoAllocated}; use crate::{Ident, ProgramPart}; + + +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; + /// A slightly more granular part of an es program than ProgramPart #[derive(Debug, Clone, PartialEq)] #[cfg_attr( @@ -217,7 +222,10 @@ impl IntoAllocated for Stmt where T: ToString { /// //rand !== 0 /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)] pub struct WithStmt { pub object: Expr, pub body: Box>, @@ -244,8 +252,10 @@ impl IntoAllocated for WithStmt where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct LabeledStmt { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct LabeledStmt { pub label: Ident, pub body: Box>, } @@ -270,8 +280,10 @@ impl IntoAllocated for LabeledStmt where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct IfStmt { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct IfStmt { pub test: Expr, pub consequent: Box>, pub alternate: Option>>, @@ -304,8 +316,10 @@ impl IntoAllocated for IfStmt where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct SwitchStmt { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct SwitchStmt { pub discriminant: Expr, pub cases: Vec>, } @@ -370,8 +384,10 @@ impl IntoAllocated for BlockStmt where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct TryStmt { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct TryStmt { pub block: BlockStmt, pub handler: Option>, pub finalizer: Option>, @@ -426,8 +442,10 @@ impl IntoAllocated for CatchClause where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct WhileStmt { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct WhileStmt { pub test: Expr, pub body: Box>, } @@ -450,8 +468,10 @@ impl IntoAllocated for WhileStmt where T: ToString { /// } while (Math.floor(Math.random() * 100) < 75) /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct DoWhileStmt { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct DoWhileStmt { pub test: Expr, pub body: Box>, } @@ -475,8 +495,10 @@ impl IntoAllocated for DoWhileStmt where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct ForStmt { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct ForStmt { pub init: Option>, pub test: Option>, pub update: Option>, @@ -534,8 +556,10 @@ impl IntoAllocated for LoopInit where T: ToString { /// //prints a, b /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct ForInStmt { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct ForInStmt { pub left: LoopLeft, pub right: Expr, pub body: Box>, @@ -562,8 +586,10 @@ impl IntoAllocated for ForInStmt where T: ToString { /// //prints 2, 3, 4, 5, 6 /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))] -pub struct ForOfStmt { +#[cfg_attr( + feature = "serde", + derive(Deserialize, Serialize) +)]pub struct ForOfStmt { pub left: LoopLeft, pub right: Expr, pub body: Box>, From 5a505b939d7ca91ba2a96dc84f9d333308e7b60d Mon Sep 17 00:00:00 2001 From: Robert Masen Date: Sun, 11 Jun 2023 10:25:10 -0500 Subject: [PATCH 6/8] 0.6.0-alpha.4 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index ac50330..fc9ac9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "resast" -version = "0.6.0-alpha.3" +version = "0.6.0-alpha.4" authors = ["rfm "] edition = "2021" description = "Rusty-ECMAScript Abstract Syntax Tree" From 56d12bc6f6c46f01ca9f49a0f3a80a759e64c8ac Mon Sep 17 00:00:00 2001 From: Robert Masen Date: Sun, 11 Jun 2023 10:32:50 -0500 Subject: [PATCH 7/8] Clippy and Fmt enforcement --- .github/workflows/rust.yml | 18 ++ src/decl.rs | 604 +++++++++++++++++++------------------ src/expr.rs | 325 +++++++++++--------- src/lib.rs | 158 +++++----- src/pat.rs | 51 ++-- src/spanned/convert.rs | 7 +- src/spanned/decl.rs | 274 +++++++++-------- src/spanned/expr.rs | 474 ++++++++++++++++------------- src/spanned/mod.rs | 190 ++++++------ src/spanned/pat.rs | 96 +++--- src/spanned/stmt.rs | 279 ++++++++++------- src/spanned/tokens.rs | 53 +--- src/stmt.rs | 186 ++++++------ 13 files changed, 1470 insertions(+), 1245 deletions(-) create mode 100644 .github/workflows/rust.yml diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..f343b1f --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,18 @@ +name: Rust + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: fmt check + run: cargo fmt --check + - name: clippy + run: cargo clippy -- -Dwarnings + - name: Build + run: cargo build + diff --git a/src/decl.rs b/src/decl.rs index 52ea0b9..41117c0 100644 --- a/src/decl.rs +++ b/src/decl.rs @@ -1,295 +1,309 @@ -use crate::expr::{Expr, Lit}; -use crate::pat::Pat; -use crate::{VarKind, IntoAllocated}; -use crate::{Class, Func, Ident}; - -#[cfg(feature = "serde")] -use serde::{Serialize, Deserialize}; - -/// The declaration of a variable, function, class, import or export -#[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] -pub enum Decl { - /// A variable declaration - /// ```js - /// var x, b; - /// let y, a = 0; - /// const q = 100 - /// ``` - Var(VarKind, Vec>), - /// A function declaration - /// ```js - /// function thing() {} - /// ``` - Func(Func), - /// A class declaration - /// ```js - /// class Thing {} - /// ``` - Class(Class), - /// An import declaration - /// ```js - /// import * as moment from 'moment'; - /// import Thing, {thing} from 'stuff'; - /// ``` - Import(Box>), - /// An export declaration - /// ```js - /// export function thing() {} - /// ``` - Export(Box>), -} - -impl IntoAllocated for Decl where T: ToString { - type Allocated = Decl; - - fn into_allocated(self) -> Self::Allocated { - match self { - Decl::Var(k, decls) => Decl::Var(k, decls.into_iter().map(|d| d.into_allocated()).collect()), - Decl::Func(inner) => Decl::Func(inner.into_allocated()), - Decl::Class(inner) => Decl::Class(inner.into_allocated()), - Decl::Import(inner) => Decl::Import(inner.into_allocated()), - Decl::Export(inner) => Decl::Export(inner.into_allocated()), - } - } -} - -/// The identifier and optional value of a variable declaration -#[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] -pub struct VarDecl { - pub id: Pat, - pub init: Option>, -} - -impl IntoAllocated for VarDecl where T: ToString { - type Allocated = VarDecl; - - fn into_allocated(self) -> Self::Allocated { - VarDecl { - id: self.id.into_allocated(), - init: self.init.map(|i| i.into_allocated()), - } - } -} - -/// A declaration that imports exported -/// members of another module -/// -/// ```js -/// import {Thing} from './stuff.js'; -/// ``` -#[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] -pub struct ModImport { - pub specifiers: Vec>, - pub source: Lit, -} - -impl IntoAllocated for ModImport where T: ToString { - type Allocated = ModImport; - - fn into_allocated(self) -> Self::Allocated { - ModImport { - specifiers: self.specifiers.into_iter().map(|s| s.into_allocated()).collect(), - source: self.source.into_allocated(), - } - } -} - -/// The name of the thing being imported -#[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] -pub enum ImportSpecifier { - /// A specifier in curly braces, this might - /// have a local alias - /// - /// ```js - /// import {Thing} from './stuff.js'; - /// import {People as Persons} from './places.js'; - /// ``` - Normal(Vec>), - /// A specifier that has been exported with the - /// default keyword, this should not be wrapped in - /// curly braces. - /// ```js - /// import DefaultThing from './stuff/js'; - /// ``` - Default(Ident), - /// Import all exported members from a module - /// in a namespace. - /// - /// ```js - /// import * as Moment from 'moment.js'; - /// ``` - Namespace(Ident), -} - -impl IntoAllocated for ImportSpecifier where T: ToString { - type Allocated = ImportSpecifier; - - fn into_allocated(self) -> Self::Allocated { - match self { - ImportSpecifier::Normal(inner) => ImportSpecifier::Normal(inner.into_iter().map(|n| n.into_allocated()).collect()), - ImportSpecifier::Default(inner) => ImportSpecifier::Default(inner.into_allocated()), - ImportSpecifier::Namespace(inner) => ImportSpecifier::Namespace(inner.into_allocated()), - } - } -} - -#[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] -pub struct NormalImportSpec { - pub alias: Option>, - pub imported: Ident, -} - -impl IntoAllocated for NormalImportSpec where T: ToString { - type Allocated = NormalImportSpec; - - fn into_allocated(self) -> Self::Allocated { - NormalImportSpec { - alias: self.alias.map(|i| i.into_allocated()), - imported: self.imported.into_allocated(), - } - } -} - -/// Something exported from this module -#[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] -pub enum ModExport { - /// ```js - /// export default function() {}; - /// //or - /// export default 1; - /// ``` - Default(DefaultExportDecl), - ///```js - /// export {foo} from 'mod'; - /// //or - /// export {foo as bar} from 'mod'; - /// //or - /// export var foo = 1; - /// //or - /// export function bar() { - /// } - /// ``` - Named(NamedExportDecl), - /// ```js - /// export * from 'mod'; - /// ``` - All { - alias: Option>, - name: Lit, - }, -} - -impl IntoAllocated for ModExport where T: ToString { - type Allocated = ModExport; - - fn into_allocated(self) -> Self::Allocated { - match self { - ModExport::Default(inner) => ModExport::Default(inner.into_allocated()), - ModExport::Named(inner) => ModExport::Named(inner.into_allocated()), - ModExport::All { alias, name } => ModExport::All { alias: alias.map(|i| i.into_allocated()), name: name.into_allocated()}, - } - } -} - -/// An export that has a name -/// ```js -/// export function thing() {} -/// export {stuff} from 'place'; -#[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] -pub enum NamedExportDecl { - Decl(Decl), - Specifier(Vec>, Option>), -} - -impl IntoAllocated for NamedExportDecl where T: ToString { - type Allocated = NamedExportDecl; - - fn into_allocated(self) -> Self::Allocated { - match self { - NamedExportDecl::Decl(inner) => NamedExportDecl::Decl(inner.into_allocated()), - NamedExportDecl::Specifier(specs, lit) => NamedExportDecl::Specifier(specs.into_iter().map(|s| s.into_allocated()).collect(), lit.map(|l| l.into_allocated())), - } - } -} - -/// A default export -/// ```js -/// export default class Thing {} -/// ``` -#[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] -pub enum DefaultExportDecl { - Decl(Decl), - Expr(Expr), -} - -impl IntoAllocated for DefaultExportDecl where T: ToString { - type Allocated = DefaultExportDecl; - - fn into_allocated(self) -> Self::Allocated { - match self { - DefaultExportDecl::Decl(inner) => DefaultExportDecl::Decl(inner.into_allocated()), - DefaultExportDecl::Expr(inner) => DefaultExportDecl::Expr(inner.into_allocated()), - } - } -} - -/// The name of the thing being exported -/// this might include an alias -/// ```js -/// //no-alias -/// export {Thing} from 'place'; -/// //aliased -/// export {Stuff as NewThing} from 'place' -/// ``` -#[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] -pub struct ExportSpecifier { - pub local: Ident, - pub alias: Option>, -} - -impl IntoAllocated for ExportSpecifier where T: ToString { - type Allocated = ExportSpecifier; - - fn into_allocated(self) -> Self::Allocated { - ExportSpecifier { - local: self.local.into_allocated(), - alias: self.alias.map(|a| a.into_allocated()), - } - } -} +use crate::expr::{Expr, Lit}; +use crate::pat::Pat; +use crate::{Class, Func, Ident}; +use crate::{IntoAllocated, VarKind}; + +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + +/// The declaration of a variable, function, class, import or export +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub enum Decl { + /// A variable declaration + /// ```js + /// var x, b; + /// let y, a = 0; + /// const q = 100 + /// ``` + Var(VarKind, Vec>), + /// A function declaration + /// ```js + /// function thing() {} + /// ``` + Func(Func), + /// A class declaration + /// ```js + /// class Thing {} + /// ``` + Class(Class), + /// An import declaration + /// ```js + /// import * as moment from 'moment'; + /// import Thing, {thing} from 'stuff'; + /// ``` + Import(Box>), + /// An export declaration + /// ```js + /// export function thing() {} + /// ``` + Export(Box>), +} + +impl IntoAllocated for Decl +where + T: ToString, +{ + type Allocated = Decl; + + fn into_allocated(self) -> Self::Allocated { + match self { + Decl::Var(k, decls) => { + Decl::Var(k, decls.into_iter().map(|d| d.into_allocated()).collect()) + } + Decl::Func(inner) => Decl::Func(inner.into_allocated()), + Decl::Class(inner) => Decl::Class(inner.into_allocated()), + Decl::Import(inner) => Decl::Import(inner.into_allocated()), + Decl::Export(inner) => Decl::Export(inner.into_allocated()), + } + } +} + +/// The identifier and optional value of a variable declaration +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct VarDecl { + pub id: Pat, + pub init: Option>, +} + +impl IntoAllocated for VarDecl +where + T: ToString, +{ + type Allocated = VarDecl; + + fn into_allocated(self) -> Self::Allocated { + VarDecl { + id: self.id.into_allocated(), + init: self.init.map(|i| i.into_allocated()), + } + } +} + +/// A declaration that imports exported +/// members of another module +/// +/// ```js +/// import {Thing} from './stuff.js'; +/// ``` +#[derive(PartialEq, Debug, Clone)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct ModImport { + pub specifiers: Vec>, + pub source: Lit, +} + +impl IntoAllocated for ModImport +where + T: ToString, +{ + type Allocated = ModImport; + + fn into_allocated(self) -> Self::Allocated { + ModImport { + specifiers: self + .specifiers + .into_iter() + .map(|s| s.into_allocated()) + .collect(), + source: self.source.into_allocated(), + } + } +} + +/// The name of the thing being imported +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub enum ImportSpecifier { + /// A specifier in curly braces, this might + /// have a local alias + /// + /// ```js + /// import {Thing} from './stuff.js'; + /// import {People as Persons} from './places.js'; + /// ``` + Normal(Vec>), + /// A specifier that has been exported with the + /// default keyword, this should not be wrapped in + /// curly braces. + /// ```js + /// import DefaultThing from './stuff/js'; + /// ``` + Default(Ident), + /// Import all exported members from a module + /// in a namespace. + /// + /// ```js + /// import * as Moment from 'moment.js'; + /// ``` + Namespace(Ident), +} + +impl IntoAllocated for ImportSpecifier +where + T: ToString, +{ + type Allocated = ImportSpecifier; + + fn into_allocated(self) -> Self::Allocated { + match self { + ImportSpecifier::Normal(inner) => { + ImportSpecifier::Normal(inner.into_iter().map(|n| n.into_allocated()).collect()) + } + ImportSpecifier::Default(inner) => ImportSpecifier::Default(inner.into_allocated()), + ImportSpecifier::Namespace(inner) => ImportSpecifier::Namespace(inner.into_allocated()), + } + } +} + +#[derive(PartialEq, Debug, Clone)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct NormalImportSpec { + pub alias: Option>, + pub imported: Ident, +} + +impl IntoAllocated for NormalImportSpec +where + T: ToString, +{ + type Allocated = NormalImportSpec; + + fn into_allocated(self) -> Self::Allocated { + NormalImportSpec { + alias: self.alias.map(|i| i.into_allocated()), + imported: self.imported.into_allocated(), + } + } +} + +/// Something exported from this module +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub enum ModExport { + /// ```js + /// export default function() {}; + /// //or + /// export default 1; + /// ``` + Default(DefaultExportDecl), + ///```js + /// export {foo} from 'mod'; + /// //or + /// export {foo as bar} from 'mod'; + /// //or + /// export var foo = 1; + /// //or + /// export function bar() { + /// } + /// ``` + Named(NamedExportDecl), + /// ```js + /// export * from 'mod'; + /// ``` + All { + alias: Option>, + name: Lit, + }, +} + +impl IntoAllocated for ModExport +where + T: ToString, +{ + type Allocated = ModExport; + + fn into_allocated(self) -> Self::Allocated { + match self { + ModExport::Default(inner) => ModExport::Default(inner.into_allocated()), + ModExport::Named(inner) => ModExport::Named(inner.into_allocated()), + ModExport::All { alias, name } => ModExport::All { + alias: alias.map(|i| i.into_allocated()), + name: name.into_allocated(), + }, + } + } +} + +/// An export that has a name +/// ```js +/// export function thing() {} +/// export {stuff} from 'place'; +#[derive(PartialEq, Debug, Clone)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub enum NamedExportDecl { + Decl(Decl), + Specifier(Vec>, Option>), +} + +impl IntoAllocated for NamedExportDecl +where + T: ToString, +{ + type Allocated = NamedExportDecl; + + fn into_allocated(self) -> Self::Allocated { + match self { + NamedExportDecl::Decl(inner) => NamedExportDecl::Decl(inner.into_allocated()), + NamedExportDecl::Specifier(specs, lit) => NamedExportDecl::Specifier( + specs.into_iter().map(|s| s.into_allocated()).collect(), + lit.map(|l| l.into_allocated()), + ), + } + } +} + +/// A default export +/// ```js +/// export default class Thing {} +/// ``` +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub enum DefaultExportDecl { + Decl(Decl), + Expr(Expr), +} + +impl IntoAllocated for DefaultExportDecl +where + T: ToString, +{ + type Allocated = DefaultExportDecl; + + fn into_allocated(self) -> Self::Allocated { + match self { + DefaultExportDecl::Decl(inner) => DefaultExportDecl::Decl(inner.into_allocated()), + DefaultExportDecl::Expr(inner) => DefaultExportDecl::Expr(inner.into_allocated()), + } + } +} + +/// The name of the thing being exported +/// this might include an alias +/// ```js +/// //no-alias +/// export {Thing} from 'place'; +/// //aliased +/// export {Stuff as NewThing} from 'place' +/// ``` +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct ExportSpecifier { + pub local: Ident, + pub alias: Option>, +} + +impl IntoAllocated for ExportSpecifier +where + T: ToString, +{ + type Allocated = ExportSpecifier; + + fn into_allocated(self) -> Self::Allocated { + ExportSpecifier { + local: self.local.into_allocated(), + alias: self.alias.map(|a| a.into_allocated()), + } + } +} diff --git a/src/expr.rs b/src/expr.rs index ae34a5f..5a9f69b 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1,16 +1,13 @@ use crate::pat::Pat; -use crate::{AssignOp, BinaryOp, LogicalOp, PropKind, UnaryOp, UpdateOp, IntoAllocated}; +use crate::{AssignOp, BinaryOp, IntoAllocated, LogicalOp, PropKind, UnaryOp, UpdateOp}; use crate::{Class, Func, FuncArg, FuncBody, Ident}; #[cfg(feature = "serde")] -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; /// A slightly more granular program part that a statement #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Expr { /// `[0,,]` Array(ArrayExpr), @@ -94,14 +91,25 @@ pub enum Expr { Yield(YieldExpr), } -impl IntoAllocated for Expr where T: ToString { +impl IntoAllocated for Expr +where + T: ToString, +{ type Allocated = Expr; fn into_allocated(self) -> Self::Allocated { match self { - Expr::Array(inner) => Expr::Array(inner.into_iter().map(|o| o.map(|e| e.into_allocated())).collect()), + Expr::Array(inner) => Expr::Array( + inner + .into_iter() + .map(|o| o.map(|e| e.into_allocated())) + .collect(), + ), Expr::ArrowFunc(inner) => Expr::ArrowFunc(inner.into_allocated()), - Expr::ArrowParamPlaceHolder(args, is_async) => Expr::ArrowParamPlaceHolder(args.into_iter().map(|a| a.into_allocated()).collect(), is_async), + Expr::ArrowParamPlaceHolder(args, is_async) => Expr::ArrowParamPlaceHolder( + args.into_iter().map(|a| a.into_allocated()).collect(), + is_async, + ), Expr::Assign(inner) => Expr::Assign(inner.into_allocated()), Expr::Await(inner) => Expr::Await(inner.into_allocated()), Expr::Binary(inner) => Expr::Binary(inner.into_allocated()), @@ -116,7 +124,9 @@ impl IntoAllocated for Expr where T: ToString { Expr::MetaProp(inner) => Expr::MetaProp(inner.into_allocated()), Expr::New(inner) => Expr::New(inner.into_allocated()), Expr::Obj(inner) => Expr::Obj(inner.into_iter().map(|p| p.into_allocated()).collect()), - Expr::Sequence(inner) => Expr::Sequence(inner.into_iter().map(|e| e.into_allocated()).collect()), + Expr::Sequence(inner) => { + Expr::Sequence(inner.into_iter().map(|e| e.into_allocated()).collect()) + } Expr::Spread(inner) => Expr::Spread(inner.into_allocated()), Expr::Super => Expr::Super, Expr::TaggedTemplate(inner) => Expr::TaggedTemplate(inner.into_allocated()), @@ -140,15 +150,16 @@ pub type ArrayExpr = Vec>>; pub type ObjExpr = Vec>; /// A single part of an object literal #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub enum ObjProp { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub enum ObjProp { Prop(Prop), Spread(Expr), } -impl IntoAllocated for ObjProp where T: ToString { +impl IntoAllocated for ObjProp +where + T: ToString, +{ type Allocated = ObjProp; fn into_allocated(self) -> Self::Allocated { @@ -161,10 +172,7 @@ impl IntoAllocated for ObjProp where T: ToString { /// A single part of an object literal or class #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Prop { pub key: PropKey, pub value: PropValue, @@ -175,7 +183,10 @@ pub struct Prop { pub is_static: bool, } -impl IntoAllocated for Prop where T: ToString { +impl IntoAllocated for Prop +where + T: ToString, +{ type Allocated = Prop; fn into_allocated(self) -> Self::Allocated { @@ -193,16 +204,17 @@ impl IntoAllocated for Prop where T: ToString { /// An object literal or class property identifier #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub enum PropKey { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub enum PropKey { Lit(Lit), Expr(Expr), Pat(Pat), } -impl IntoAllocated for PropKey where T: ToString { +impl IntoAllocated for PropKey +where + T: ToString, +{ type Allocated = PropKey; fn into_allocated(self) -> Self::Allocated { @@ -216,17 +228,17 @@ impl IntoAllocated for PropKey where T: ToString { /// The value of an object literal or class property #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum PropValue { Expr(Expr), Pat(Pat), None, } -impl IntoAllocated for PropValue where T: ToString { +impl IntoAllocated for PropValue +where + T: ToString, +{ type Allocated = PropValue; fn into_allocated(self) -> Self::Allocated { @@ -240,16 +252,17 @@ impl IntoAllocated for PropValue where T: ToString { /// An operation that takes one argument #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct UnaryExpr { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct UnaryExpr { pub operator: UnaryOp, pub prefix: bool, pub argument: Box>, } -impl IntoAllocated for UnaryExpr where T: ToString { +impl IntoAllocated for UnaryExpr +where + T: ToString, +{ type Allocated = UnaryExpr; fn into_allocated(self) -> Self::Allocated { @@ -263,16 +276,17 @@ impl IntoAllocated for UnaryExpr where T: ToString { /// Increment or decrementing a value #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct UpdateExpr { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct UpdateExpr { pub operator: UpdateOp, pub argument: Box>, pub prefix: bool, } -impl IntoAllocated for UpdateExpr where T: ToString { +impl IntoAllocated for UpdateExpr +where + T: ToString, +{ type Allocated = UpdateExpr; fn into_allocated(self) -> Self::Allocated { @@ -286,16 +300,17 @@ impl IntoAllocated for UpdateExpr where T: ToString { /// An operation that requires 2 arguments #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct BinaryExpr { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct BinaryExpr { pub operator: BinaryOp, pub left: Box>, pub right: Box>, } -impl IntoAllocated for BinaryExpr where T: ToString { +impl IntoAllocated for BinaryExpr +where + T: ToString, +{ type Allocated = BinaryExpr; fn into_allocated(self) -> Self::Allocated { @@ -309,16 +324,17 @@ impl IntoAllocated for BinaryExpr where T: ToString { /// An assignment or update + assignment operation #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct AssignExpr { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct AssignExpr { pub operator: AssignOp, pub left: AssignLeft, pub right: Box>, } -impl IntoAllocated for AssignExpr where T: ToString { +impl IntoAllocated for AssignExpr +where + T: ToString, +{ type Allocated = AssignExpr; fn into_allocated(self) -> Self::Allocated { @@ -332,15 +348,16 @@ impl IntoAllocated for AssignExpr where T: ToString { /// The value being assigned to #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub enum AssignLeft { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub enum AssignLeft { Pat(Pat), Expr(Box>), } -impl IntoAllocated for AssignLeft where T: ToString { +impl IntoAllocated for AssignLeft +where + T: ToString, +{ type Allocated = AssignLeft; fn into_allocated(self) -> Self::Allocated { @@ -357,16 +374,17 @@ impl IntoAllocated for AssignLeft where T: ToString { /// false || true /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct LogicalExpr { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct LogicalExpr { pub operator: LogicalOp, pub left: Box>, pub right: Box>, } -impl IntoAllocated for LogicalExpr where T: ToString { +impl IntoAllocated for LogicalExpr +where + T: ToString, +{ type Allocated = LogicalExpr; fn into_allocated(self) -> Self::Allocated { @@ -384,16 +402,17 @@ impl IntoAllocated for LogicalExpr where T: ToString { /// c.stuff; /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct MemberExpr { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct MemberExpr { pub object: Box>, pub property: Box>, pub computed: bool, } -impl IntoAllocated for MemberExpr where T: ToString { +impl IntoAllocated for MemberExpr +where + T: ToString, +{ type Allocated = MemberExpr; fn into_allocated(self) -> Self::Allocated { @@ -410,16 +429,17 @@ impl IntoAllocated for MemberExpr where T: ToString { /// var a = true ? 'stuff' : 'things'; /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct ConditionalExpr { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct ConditionalExpr { pub test: Box>, pub alternate: Box>, pub consequent: Box>, } -impl IntoAllocated for ConditionalExpr where T: ToString { +impl IntoAllocated for ConditionalExpr +where + T: ToString, +{ type Allocated = ConditionalExpr; fn into_allocated(self) -> Self::Allocated { @@ -436,21 +456,26 @@ impl IntoAllocated for ConditionalExpr where T: ToString { /// Math.random() /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct CallExpr { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct CallExpr { pub callee: Box>, pub arguments: Vec>, } -impl IntoAllocated for CallExpr where T: ToString { +impl IntoAllocated for CallExpr +where + T: ToString, +{ type Allocated = CallExpr; fn into_allocated(self) -> Self::Allocated { CallExpr { callee: self.callee.into_allocated(), - arguments: self.arguments.into_iter().map(IntoAllocated::into_allocated).collect(), + arguments: self + .arguments + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), } } } @@ -460,21 +485,26 @@ impl IntoAllocated for CallExpr where T: ToString { /// new Uint8Array(32); /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct NewExpr { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct NewExpr { pub callee: Box>, pub arguments: Vec>, } -impl IntoAllocated for NewExpr where T: ToString { +impl IntoAllocated for NewExpr +where + T: ToString, +{ type Allocated = NewExpr; fn into_allocated(self) -> Self::Allocated { NewExpr { callee: self.callee.into_allocated(), - arguments: self.arguments.into_iter().map(|a| a.into_allocated()).collect(), + arguments: self + .arguments + .into_iter() + .map(|a| a.into_allocated()) + .collect(), } } } @@ -490,10 +520,8 @@ pub type SequenceExpr = Vec>; /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct ArrowFuncExpr { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct ArrowFuncExpr { pub id: Option>, pub params: Vec>, pub body: ArrowFuncBody, @@ -502,13 +530,20 @@ pub type SequenceExpr = Vec>; pub is_async: bool, } -impl IntoAllocated for ArrowFuncExpr where T: ToString { +impl IntoAllocated for ArrowFuncExpr +where + T: ToString, +{ type Allocated = ArrowFuncExpr; fn into_allocated(self) -> Self::Allocated { ArrowFuncExpr { id: self.id.map(|i| i.into_allocated()), - params: self.params.into_iter().map(|p| p.into_allocated()).collect(), + params: self + .params + .into_iter() + .map(|p| p.into_allocated()) + .collect(), body: self.body.into_allocated(), expression: self.expression, generator: self.generator, @@ -519,15 +554,16 @@ impl IntoAllocated for ArrowFuncExpr where T: ToString { /// The body portion of an arrow function can be either an expression or a block of statements #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub enum ArrowFuncBody { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub enum ArrowFuncBody { FuncBody(FuncBody), Expr(Box>), } -impl IntoAllocated for ArrowFuncBody where T: ToString { +impl IntoAllocated for ArrowFuncBody +where + T: ToString, +{ type Allocated = ArrowFuncBody; fn into_allocated(self) -> Self::Allocated { @@ -547,15 +583,16 @@ impl IntoAllocated for ArrowFuncBody where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct YieldExpr { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct YieldExpr { pub argument: Option>>, pub delegate: bool, } -impl IntoAllocated for YieldExpr where T: ToString { +impl IntoAllocated for YieldExpr +where + T: ToString, +{ type Allocated = YieldExpr; fn into_allocated(self) -> Self::Allocated { @@ -568,15 +605,16 @@ impl IntoAllocated for YieldExpr where T: ToString { /// A Template literal preceded by a function identifier /// see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates) for more details #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct TaggedTemplateExpr { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct TaggedTemplateExpr { pub tag: Box>, pub quasi: TemplateLit, } -impl IntoAllocated for TaggedTemplateExpr where T: ToString { +impl IntoAllocated for TaggedTemplateExpr +where + T: ToString, +{ type Allocated = TaggedTemplateExpr; fn into_allocated(self) -> Self::Allocated { @@ -592,31 +630,36 @@ impl IntoAllocated for TaggedTemplateExpr where T: ToString { /// `I own ${0} birds`; /// ``` #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct TemplateLit { pub quasis: Vec>, pub expressions: Vec>, } -impl IntoAllocated for TemplateLit where T: ToString { +impl IntoAllocated for TemplateLit +where + T: ToString, +{ type Allocated = TemplateLit; fn into_allocated(self) -> Self::Allocated { TemplateLit { - quasis: self.quasis.into_iter().map(|e| e.into_allocated()).collect(), - expressions: self.expressions.into_iter().map(|e| e.into_allocated()).collect(), + quasis: self + .quasis + .into_iter() + .map(|e| e.into_allocated()) + .collect(), + expressions: self + .expressions + .into_iter() + .map(|e| e.into_allocated()) + .collect(), } } } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum QuasiQuote { /// ` BackTick, @@ -628,10 +671,7 @@ pub enum QuasiQuote { /// The text part of a `TemplateLiteral` #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct TemplateElement { pub open_quote: QuasiQuote, /// The non-quoted version @@ -639,7 +679,10 @@ pub struct TemplateElement { pub close_quote: QuasiQuote, } -impl IntoAllocated for TemplateElement where T: ToString { +impl IntoAllocated for TemplateElement +where + T: ToString, +{ type Allocated = TemplateElement; fn into_allocated(self) -> Self::Allocated { @@ -671,15 +714,16 @@ impl TemplateElement { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct MetaProp { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct MetaProp { pub meta: Ident, pub property: Ident, } -impl IntoAllocated for MetaProp where T: ToString { +impl IntoAllocated for MetaProp +where + T: ToString, +{ type Allocated = MetaProp; fn into_allocated(self) -> Self::Allocated { @@ -692,10 +736,7 @@ impl IntoAllocated for MetaProp where T: ToString { /// A literal value #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Lit { /// `null` Null, @@ -722,7 +763,10 @@ pub enum Lit { Template(TemplateLit), } -impl IntoAllocated for Lit where T: ToString { +impl IntoAllocated for Lit +where + T: ToString, +{ type Allocated = Lit; fn into_allocated(self) -> Self::Allocated { @@ -750,15 +794,16 @@ impl Lit { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub enum StringLit { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub enum StringLit { Double(T), Single(T), } -impl IntoAllocated for StringLit where T: ToString { +impl IntoAllocated for StringLit +where + T: ToString, +{ type Allocated = StringLit; fn into_allocated(self) -> Self::Allocated { @@ -802,22 +847,22 @@ where } /// A regular expression literal #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct RegEx { pub pattern: T, pub flags: Option, } -impl IntoAllocated for RegEx where T: ToString { +impl IntoAllocated for RegEx +where + T: ToString, +{ type Allocated = RegEx; fn into_allocated(self) -> Self::Allocated { RegEx { pattern: self.pattern.to_string(), - flags: self.flags.map(|f| f.to_string()) + flags: self.flags.map(|f| f.to_string()), } } } diff --git a/src/lib.rs b/src/lib.rs index 8bc9ace..0908e4a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ pub mod spanned; pub mod stmt; #[cfg(feature = "serde")] -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; use std::{borrow::Cow, fmt::Debug}; @@ -15,15 +15,15 @@ use pat::Pat; use stmt::Stmt; #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Ident { pub name: T, } -impl IntoAllocated for Ident where T: ToString { +impl IntoAllocated for Ident +where + T: ToString, +{ type Allocated = Ident; fn into_allocated(self) -> Self::Allocated { @@ -57,10 +57,7 @@ impl<'a> From> for Ident> { /// with a flag denoting if the representation is /// a ES6 Mod or a Script. #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Program { /// An ES6 Mod Mod(Vec>), @@ -68,13 +65,20 @@ pub enum Program { Script(Vec>), } -impl IntoAllocated for Program where T: ToString { +impl IntoAllocated for Program +where + T: ToString, +{ type Allocated = Program; fn into_allocated(self) -> Self::Allocated { match self { - Program::Mod(inner) => Program::Mod(inner.into_iter().map(|p| p.into_allocated()).collect()), - Program::Script(inner) => Program::Script(inner.into_iter().map(|p| p.into_allocated()).collect()), + Program::Mod(inner) => { + Program::Mod(inner.into_iter().map(|p| p.into_allocated()).collect()) + } + Program::Script(inner) => { + Program::Script(inner.into_iter().map(|p| p.into_allocated()).collect()) + } } } } @@ -91,10 +95,7 @@ impl Program { /// A single part of a Javascript program. /// This will be either a Directive, Decl or a Stmt #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum ProgramPart { /// A Directive like `'use strict';` Dir(Dir), @@ -104,7 +105,10 @@ pub enum ProgramPart { Stmt(Stmt), } -impl IntoAllocated for ProgramPart where T: ToString { +impl IntoAllocated for ProgramPart +where + T: ToString, +{ type Allocated = ProgramPart; fn into_allocated(self) -> Self::Allocated { @@ -128,16 +132,16 @@ impl ProgramPart { /// pretty much always `'use strict'`, this can appear at the /// top of a file or function #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Dir { pub expr: Lit, pub dir: T, } -impl IntoAllocated for Dir where T: ToString { +impl IntoAllocated for Dir +where + T: ToString, +{ type Allocated = Dir; fn into_allocated(self) -> Self::Allocated { @@ -159,10 +163,7 @@ impl IntoAllocated for Dir where T: ToString { /// let y = function q() {} /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Func { pub id: Option>, pub params: Vec>, @@ -171,13 +172,20 @@ pub struct Func { pub is_async: bool, } -impl IntoAllocated for Func where T: ToString { +impl IntoAllocated for Func +where + T: ToString, +{ type Allocated = Func; fn into_allocated(self) -> Self::Allocated { Func { id: self.id.map(IntoAllocated::into_allocated), - params: self.params.into_iter().map(|p| p.into_allocated()).collect(), + params: self + .params + .into_iter() + .map(|p| p.into_allocated()) + .collect(), body: self.body.into_allocated(), generator: self.generator, is_async: self.is_async, @@ -205,16 +213,16 @@ impl Func { /// A single function argument from a function signature #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum FuncArg { Expr(Expr), Pat(Pat), } -impl IntoAllocated for FuncArg where T: ToString { +impl IntoAllocated for FuncArg +where + T: ToString, +{ type Allocated = FuncArg; fn into_allocated(self) -> Self::Allocated { @@ -236,13 +244,13 @@ impl FuncArg { /// The block statement that makes up the function's body #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct FuncBody(pub Vec>); -impl IntoAllocated for FuncBody where T: ToString { +impl IntoAllocated for FuncBody +where + T: ToString, +{ type Allocated = FuncBody; fn into_allocated(self) -> Self::Allocated { @@ -277,17 +285,17 @@ impl IntoAllocated for FuncBody where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Class { pub id: Option>, pub super_class: Option>>, pub body: ClassBody, } -impl IntoAllocated for Class where T: ToString { +impl IntoAllocated for Class +where + T: ToString, +{ type Allocated = Class; fn into_allocated(self) -> Self::Allocated { @@ -300,17 +308,22 @@ impl IntoAllocated for Class where T: ToString { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ClassBody(pub Vec>); -impl IntoAllocated for ClassBody where T: ToString { +impl IntoAllocated for ClassBody +where + T: ToString, +{ type Allocated = ClassBody; fn into_allocated(self) -> Self::Allocated { - ClassBody(self.0.into_iter().map(IntoAllocated::into_allocated).collect()) + ClassBody( + self.0 + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), + ) } } @@ -326,10 +339,7 @@ impl Class { /// The kind of variable being defined (`var`/`let`/`const`) #[derive(Debug, Clone, PartialEq, Copy)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] #[cfg_attr( all(feature = "serde", feature = "esprima"), serde(rename_all = "camelCase", untagged) @@ -342,10 +352,7 @@ pub enum VarKind { /// The available operators for assignment Exprs #[derive(Debug, Clone, PartialEq, Copy)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum AssignOp { Equal, PlusEqual, @@ -364,10 +371,7 @@ pub enum AssignOp { /// The available logical operators #[derive(Debug, Clone, PartialEq, Copy)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum LogicalOp { Or, And, @@ -375,10 +379,7 @@ pub enum LogicalOp { /// The available operations for `Binary` Exprs #[derive(Debug, Clone, PartialEq, Copy)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum BinaryOp { Equal, NotEqual, @@ -406,10 +407,7 @@ pub enum BinaryOp { /// `++` or `--` #[derive(Debug, Clone, PartialEq, Copy)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum UpdateOp { Increment, Decrement, @@ -418,10 +416,7 @@ pub enum UpdateOp { /// The allowed operators for an Expr /// to be `Unary` #[derive(Debug, Clone, PartialEq, Copy)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum UnaryOp { Minus, Plus, @@ -434,10 +429,7 @@ pub enum UnaryOp { /// A flag for determining what kind of property #[derive(Debug, Clone, PartialEq, Copy)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum PropKind { /// A property with a value Init, @@ -457,7 +449,10 @@ pub trait IntoAllocated { fn into_allocated(self) -> Self::Allocated; } -impl IntoAllocated for Box where T: IntoAllocated { +impl IntoAllocated for Box +where + T: IntoAllocated, +{ type Allocated = Box; fn into_allocated(self) -> Self::Allocated { @@ -465,7 +460,10 @@ impl IntoAllocated for Box where T: IntoAllocated { } } -impl IntoAllocated for Option where T: IntoAllocated { +impl IntoAllocated for Option +where + T: IntoAllocated, +{ type Allocated = Option; fn into_allocated(self) -> Self::Allocated { self.map(IntoAllocated::into_allocated) diff --git a/src/pat.rs b/src/pat.rs index a62d5dc..f7bce99 100644 --- a/src/pat.rs +++ b/src/pat.rs @@ -2,15 +2,12 @@ use crate::expr::{Expr, Prop}; use crate::{Ident, IntoAllocated}; #[cfg(feature = "serde")] -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; /// All of the different ways you can declare an identifier /// and/or value #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Pat { Ident(Ident), Obj(ObjPat), @@ -19,14 +16,22 @@ pub enum Pat { Assign(AssignPat), } -impl IntoAllocated for Pat where T: ToString { +impl IntoAllocated for Pat +where + T: ToString, +{ type Allocated = Pat; fn into_allocated(self) -> Self::Allocated { match self { Pat::Ident(inner) => Pat::Ident(inner.into_allocated()), Pat::Obj(inner) => Pat::Obj(inner.into_iter().map(|a| a.into_allocated()).collect()), - Pat::Array(inner) => Pat::Array(inner.into_iter().map(|o| o.map(|a| a.into_allocated())).collect()), + Pat::Array(inner) => Pat::Array( + inner + .into_iter() + .map(|o| o.map(|a| a.into_allocated())) + .collect(), + ), Pat::RestElement(inner) => Pat::RestElement(inner.into_allocated()), Pat::Assign(inner) => Pat::Assign(inner.into_allocated()), } @@ -40,15 +45,16 @@ impl Pat { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub enum ArrayPatPart { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub enum ArrayPatPart { Pat(Pat), Expr(Expr), } -impl IntoAllocated for ArrayPatPart where T: ToString { +impl IntoAllocated for ArrayPatPart +where + T: ToString, +{ type Allocated = ArrayPatPart; fn into_allocated(self) -> Self::Allocated { @@ -63,15 +69,16 @@ impl IntoAllocated for ArrayPatPart where T: ToString { pub type ObjPat = Vec>; /// A single part of an ObjectPat #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub enum ObjPatPart { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub enum ObjPatPart { Assign(Prop), Rest(Box>), } -impl IntoAllocated for ObjPatPart where T: ToString { +impl IntoAllocated for ObjPatPart +where + T: ToString, +{ type Allocated = ObjPatPart; fn into_allocated(self) -> Self::Allocated { @@ -84,16 +91,16 @@ impl IntoAllocated for ObjPatPart where T: ToString { /// An assignment as a pattern #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct AssignPat { pub left: Box>, pub right: Box>, } -impl IntoAllocated for AssignPat where T: ToString { +impl IntoAllocated for AssignPat +where + T: ToString, +{ type Allocated = AssignPat; fn into_allocated(self) -> Self::Allocated { diff --git a/src/spanned/convert.rs b/src/spanned/convert.rs index 76d3409..2be8578 100644 --- a/src/spanned/convert.rs +++ b/src/spanned/convert.rs @@ -156,7 +156,7 @@ mod decl { fn from(other: ExportSpecifier) -> Self { let local: crate::Ident = other.local.into(); Self { - local: local, + local, alias: other.alias.map(|a| a.ident.into()), } } @@ -361,12 +361,11 @@ mod expr { impl From> for crate::expr::UpdateExpr { fn from(other: UpdateExpr) -> Self { - let ret = Self { + Self { prefix: other.prefix(), operator: other.operator.into(), argument: Box::new(From::from(*other.argument)), - }; - ret + } } } diff --git a/src/spanned/decl.rs b/src/spanned/decl.rs index 616f185..d358f42 100644 --- a/src/spanned/decl.rs +++ b/src/spanned/decl.rs @@ -1,8 +1,8 @@ -use crate::IntoAllocated; use crate::spanned::expr::{Expr, Lit}; use crate::spanned::pat::Pat; use crate::spanned::VarKind; use crate::spanned::{Class, Func, Ident}; +use crate::IntoAllocated; use super::tokens::{ As, Asterisk, CloseBrace, Default, Equal, Export, From, Import, OpenBrace, Semicolon, Token, @@ -10,14 +10,11 @@ use super::tokens::{ use super::{ListEntry, Node, SourceLocation}; #[cfg(feature = "serde")] -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; /// The declaration of a variable, function, class, import or export #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Decl { /// A variable declaration /// ```js @@ -58,7 +55,6 @@ pub enum Decl { }, } - impl IntoAllocated for Decl where T: ToString, @@ -68,12 +64,18 @@ where match self { Decl::Var { decls, semi_colon } => Decl::Var { decls: decls.into_allocated(), - semi_colon: semi_colon, + semi_colon, }, Decl::Func(f) => Decl::Func(f.into_allocated()), Decl::Class(c) => Decl::Class(c.into_allocated()), - Decl::Import { import, semi_colon } => Decl::Import { import: import.into_allocated(), semi_colon }, - Decl::Export { export, semi_colon } => Decl::Export { export: export.into_allocated(), semi_colon }, + Decl::Import { import, semi_colon } => Decl::Import { + import: import.into_allocated(), + semi_colon, + }, + Decl::Export { export, semi_colon } => Decl::Export { + export: export.into_allocated(), + semi_colon, + }, } } } @@ -115,16 +117,12 @@ impl Node for Decl { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct VarDecls { pub keyword: VarKind, pub decls: Vec>>, } - impl IntoAllocated for VarDecls where T: ToString, @@ -153,17 +151,13 @@ impl Node for VarDecls { /// The identifier and optional value of a variable declaration #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct VarDecl { pub id: Pat, pub eq: Option, pub init: Option>, } - impl IntoAllocated for VarDecl where T: ToString, @@ -195,16 +189,16 @@ impl Node for VarDecl { /// in an ES Mod, it would be either an import or /// export at the top level #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum ModDecl { Import(ModImport), Export(ModExport), } -impl IntoAllocated for ModDecl where T: ToString { +impl IntoAllocated for ModDecl +where + T: ToString, +{ type Allocated = ModDecl; fn into_allocated(self) -> ModDecl { match self { @@ -230,10 +224,7 @@ impl Node for ModDecl { /// import {Thing} from './stuff.js'; /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ModImport { pub keyword_import: Import, pub specifiers: Vec>>, @@ -241,10 +232,22 @@ pub struct ModImport { pub source: Lit, } -impl IntoAllocated for ModImport where T: ToString { +impl IntoAllocated for ModImport +where + T: ToString, +{ type Allocated = ModImport; fn into_allocated(self) -> ModImport { - ModImport { keyword_import:self.keyword_import, specifiers: self.specifiers.into_iter().map(|s| s.into_allocated()).collect(), keyword_from: self.keyword_from, source: self.source.into_allocated() } + ModImport { + keyword_import: self.keyword_import, + specifiers: self + .specifiers + .into_iter() + .map(|s| s.into_allocated()) + .collect(), + keyword_from: self.keyword_from, + source: self.source.into_allocated(), + } } } @@ -259,10 +262,7 @@ impl Node for ModImport { /// The name of the thing being imported #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum ImportSpecifier { /// A specifier in curly braces, this might /// have a local alias @@ -290,7 +290,10 @@ pub enum ImportSpecifier { Namespace(NamespaceImportSpec), } -impl IntoAllocated for ImportSpecifier where T: ToString { +impl IntoAllocated for ImportSpecifier +where + T: ToString, +{ type Allocated = ImportSpecifier; fn into_allocated(self) -> ImportSpecifier { match self { @@ -312,17 +315,17 @@ impl Node for ImportSpecifier { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct NormalImportSpecs { pub open_brace: OpenBrace, pub specs: Vec>>, pub close_brace: CloseBrace, } -impl IntoAllocated for NormalImportSpecs where T: ToString { +impl IntoAllocated for NormalImportSpecs +where + T: ToString, +{ type Allocated = NormalImportSpecs; fn into_allocated(self) -> NormalImportSpecs { NormalImportSpecs { @@ -343,19 +346,22 @@ impl Node for NormalImportSpecs { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct NormalImportSpec { pub imported: Ident, pub alias: Option>, } -impl IntoAllocated for NormalImportSpec where T: ToString { +impl IntoAllocated for NormalImportSpec +where + T: ToString, +{ type Allocated = NormalImportSpec; fn into_allocated(self) -> NormalImportSpec { - NormalImportSpec { imported: self.imported.into_allocated(), alias: self.alias.map(|a| a.into_allocated()) } + NormalImportSpec { + imported: self.imported.into_allocated(), + alias: self.alias.map(|a| a.into_allocated()), + } } } @@ -373,18 +379,20 @@ impl Node for NormalImportSpec { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct DefaultImportSpec { pub id: Ident, } -impl IntoAllocated for DefaultImportSpec where T: ToString { +impl IntoAllocated for DefaultImportSpec +where + T: ToString, +{ type Allocated = DefaultImportSpec; fn into_allocated(self) -> DefaultImportSpec { - DefaultImportSpec { id: self.id.into_allocated() } + DefaultImportSpec { + id: self.id.into_allocated(), + } } } @@ -395,17 +403,17 @@ impl Node for DefaultImportSpec { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct NamespaceImportSpec { pub star: Asterisk, pub keyword: As, pub ident: Ident, } -impl IntoAllocated for NamespaceImportSpec where T: ToString { +impl IntoAllocated for NamespaceImportSpec +where + T: ToString, +{ type Allocated = NamespaceImportSpec; fn into_allocated(self) -> NamespaceImportSpec { NamespaceImportSpec { @@ -426,19 +434,22 @@ impl Node for NamespaceImportSpec { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ModExport { pub keyword: Export, pub spec: ModExportSpecifier, } -impl IntoAllocated for ModExport where T: ToString { +impl IntoAllocated for ModExport +where + T: ToString, +{ type Allocated = ModExport; fn into_allocated(self) -> ModExport { - ModExport { keyword: self.keyword, spec: self.spec.into_allocated() } + ModExport { + keyword: self.keyword, + spec: self.spec.into_allocated(), + } } } @@ -453,10 +464,7 @@ impl Node for ModExport { /// Something exported from this module #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum ModExportSpecifier { /// ```js /// export default function() {}; @@ -489,13 +497,29 @@ pub enum ModExportSpecifier { }, } -impl IntoAllocated for ModExportSpecifier where T: ToString { +impl IntoAllocated for ModExportSpecifier +where + T: ToString, +{ type Allocated = ModExportSpecifier; fn into_allocated(self) -> ModExportSpecifier { match self { - ModExportSpecifier::Default { keyword, value } => ModExportSpecifier::Default {keyword, value: value.into_allocated()}, + ModExportSpecifier::Default { keyword, value } => ModExportSpecifier::Default { + keyword, + value: value.into_allocated(), + }, ModExportSpecifier::Named(inner) => ModExportSpecifier::Named(inner.into_allocated()), - ModExportSpecifier::All { star, alias, keyword, name } => ModExportSpecifier::All { star, alias: alias.map(|a| a.into_allocated()), keyword: keyword, name: name.into_allocated()}, + ModExportSpecifier::All { + star, + alias, + keyword, + name, + } => ModExportSpecifier::All { + star, + alias: alias.map(|a| a.into_allocated()), + keyword, + name: name.into_allocated(), + }, } } } @@ -521,16 +545,16 @@ impl Node for ModExportSpecifier { /// export function thing() {} /// export {stuff} from 'place'; #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum NamedExportDecl { Decl(Decl), Specifier(NamedExportSpec), } -impl IntoAllocated for NamedExportDecl where T: ToString { +impl IntoAllocated for NamedExportDecl +where + T: ToString, +{ type Allocated = NamedExportDecl; fn into_allocated(self) -> NamedExportDecl { match self { @@ -550,16 +574,16 @@ impl Node for NamedExportDecl { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct DefaultExportDecl { pub keyword: Default, pub value: DefaultExportDeclValue, } -impl IntoAllocated for DefaultExportDecl where T: ToString { +impl IntoAllocated for DefaultExportDecl +where + T: ToString, +{ type Allocated = DefaultExportDecl; fn into_allocated(self) -> DefaultExportDecl { DefaultExportDecl { @@ -583,17 +607,17 @@ impl Node for DefaultExportDecl { /// export default class Thing {} /// ``` #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum ExportDeclValue { Decl(Decl), Expr(Expr), List(ExportList), } -impl IntoAllocated for ExportDeclValue where T: ToString { +impl IntoAllocated for ExportDeclValue +where + T: ToString, +{ type Allocated = ExportDeclValue; fn into_allocated(self) -> ExportDeclValue { match self { @@ -615,21 +639,25 @@ impl Node for ExportDeclValue { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum DefaultExportDeclValue { Decl(Decl), Expr(Expr), } -impl IntoAllocated for DefaultExportDeclValue where T: ToString { +impl IntoAllocated for DefaultExportDeclValue +where + T: ToString, +{ type Allocated = DefaultExportDeclValue; fn into_allocated(self) -> DefaultExportDeclValue { match self { - DefaultExportDeclValue::Decl(inner) => DefaultExportDeclValue::Decl(inner.into_allocated()), - DefaultExportDeclValue::Expr(inner) => DefaultExportDeclValue::Expr(inner.into_allocated()), + DefaultExportDeclValue::Decl(inner) => { + DefaultExportDeclValue::Decl(inner.into_allocated()) + } + DefaultExportDeclValue::Expr(inner) => { + DefaultExportDeclValue::Expr(inner.into_allocated()) + } } } } @@ -644,16 +672,16 @@ impl Node for DefaultExportDeclValue { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct NamedExportSpec { pub list: ExportList, pub source: Option>, } -impl IntoAllocated for NamedExportSpec where T: ToString { +impl IntoAllocated for NamedExportSpec +where + T: ToString, +{ type Allocated = NamedExportSpec; fn into_allocated(self) -> NamedExportSpec { NamedExportSpec { @@ -677,16 +705,16 @@ impl Node for NamedExportSpec { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct NamedExportSource { pub keyword_from: From, pub module: Lit, } -impl IntoAllocated for NamedExportSource where T: ToString { +impl IntoAllocated for NamedExportSource +where + T: ToString, +{ type Allocated = NamedExportSource; fn into_allocated(self) -> NamedExportSource { NamedExportSource { @@ -706,20 +734,28 @@ impl Node for NamedExportSource { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ExportList { pub open_brace: OpenBrace, pub elements: Vec>>, pub close_brace: CloseBrace, } -impl IntoAllocated for ExportList where T: ToString { +impl IntoAllocated for ExportList +where + T: ToString, +{ type Allocated = ExportList; fn into_allocated(self) -> ExportList { - ExportList { open_brace: self.open_brace, elements: self.elements.into_iter().map(|e| e.into_allocated()).collect(), close_brace: self.close_brace } + ExportList { + open_brace: self.open_brace, + elements: self + .elements + .into_iter() + .map(|e| e.into_allocated()) + .collect(), + close_brace: self.close_brace, + } } } @@ -741,16 +777,16 @@ impl Node for ExportList { /// export {Stuff as NewThing} from 'place' /// ``` #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ExportSpecifier { pub local: Ident, pub alias: Option>, } -impl IntoAllocated for ExportSpecifier where T: ToString { +impl IntoAllocated for ExportSpecifier +where + T: ToString, +{ type Allocated = ExportSpecifier; fn into_allocated(self) -> Self::Allocated { @@ -775,16 +811,16 @@ impl Node for ExportSpecifier { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Alias { pub keyword: As, pub ident: Ident, } -impl IntoAllocated for Alias where T: ToString { +impl IntoAllocated for Alias +where + T: ToString, +{ type Allocated = Alias; fn into_allocated(self) -> Self::Allocated { diff --git a/src/spanned/expr.rs b/src/spanned/expr.rs index 67f113f..ba3cb33 100644 --- a/src/spanned/expr.rs +++ b/src/spanned/expr.rs @@ -1,6 +1,6 @@ -use crate::IntoAllocated; use crate::spanned::pat::Pat; use crate::spanned::{Class, Func, FuncArg, FuncBody, Ident}; +use crate::IntoAllocated; use super::tokens::{ AssignOp, Asterisk, Async, Await, BinaryOp, CloseBrace, CloseBracket, CloseParen, Colon, Comma, @@ -10,14 +10,11 @@ use super::tokens::{ }; use super::{FuncArgEntry, ListEntry, Node, Slice, SourceLocation}; #[cfg(feature = "serde")] -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; /// A slightly more granular program part that a statement #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Expr { /// `[0,,]` Array(ArrayExpr), @@ -102,13 +99,18 @@ pub enum Expr { Yield(YieldExpr), } -impl IntoAllocated for Expr where T: ToString { +impl IntoAllocated for Expr +where + T: ToString, +{ type Allocated = Expr; fn into_allocated(self) -> Self::Allocated { match self { Expr::Array(inner) => Expr::Array(inner.into_allocated()), Expr::ArrowFunc(inner) => Expr::ArrowFunc(inner.into_allocated()), - Expr::ArrowParamPlaceHolder(inner) => Expr::ArrowParamPlaceHolder(inner.into_allocated()), + Expr::ArrowParamPlaceHolder(inner) => { + Expr::ArrowParamPlaceHolder(inner.into_allocated()) + } Expr::Assign(inner) => Expr::Assign(inner.into_allocated()), Expr::Await(inner) => Expr::Await(inner.into_allocated()), Expr::Binary(inner) => Expr::Binary(inner.into_allocated()), @@ -123,7 +125,12 @@ impl IntoAllocated for Expr where T: ToString { Expr::MetaProp(inner) => Expr::MetaProp(inner.into_allocated()), Expr::New(inner) => Expr::New(inner.into_allocated()), Expr::Obj(inner) => Expr::Obj(inner.into_allocated()), - Expr::Sequence(inner) => Expr::Sequence(inner.into_iter().map(IntoAllocated::into_allocated).collect()), + Expr::Sequence(inner) => Expr::Sequence( + inner + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), + ), Expr::Spread(inner) => Expr::Spread(inner.into_allocated()), Expr::Super(inner) => Expr::Super(inner), Expr::TaggedTemplate(inner) => Expr::TaggedTemplate(inner.into_allocated()), @@ -173,22 +180,26 @@ type ArrayExprEntry = ListEntry>>; /// `[a, b, c]` #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ArrayExpr { pub open_bracket: OpenBracket, pub elements: Vec>, pub close_bracket: CloseBracket, } -impl IntoAllocated for ArrayExpr where T: ToString { +impl IntoAllocated for ArrayExpr +where + T: ToString, +{ type Allocated = ArrayExpr; fn into_allocated(self) -> Self::Allocated { ArrayExpr { open_bracket: self.open_bracket, - elements: self.elements.into_iter().map(IntoAllocated::into_allocated).collect(), + elements: self + .elements + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), close_bracket: self.close_bracket, } } @@ -205,23 +216,27 @@ impl Node for ArrayExpr { /// `{a: 'b', c, ...d}` #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ObjExpr { pub open_brace: OpenBrace, pub props: Vec>>, pub close_brace: CloseBrace, } -impl IntoAllocated for ObjExpr where T: ToString { +impl IntoAllocated for ObjExpr +where + T: ToString, +{ type Allocated = ObjExpr; fn into_allocated(self) -> Self::Allocated { ObjExpr { open_brace: self.open_brace, - props: self.props.into_iter().map(IntoAllocated::into_allocated).collect(), + props: self + .props + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), close_brace: self.close_brace, } } @@ -238,16 +253,16 @@ impl Node for ObjExpr { /// A single part of an object literal #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum ObjProp { Prop(Prop), Spread(SpreadExpr), } -impl IntoAllocated for ObjProp where T: ToString { +impl IntoAllocated for ObjProp +where + T: ToString, +{ type Allocated = ObjProp; fn into_allocated(self) -> Self::Allocated { match self { @@ -267,16 +282,16 @@ impl Node for ObjProp { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct SpreadExpr { pub dots: Ellipsis, pub expr: Expr, } -impl IntoAllocated for SpreadExpr where T: ToString { +impl IntoAllocated for SpreadExpr +where + T: ToString, +{ type Allocated = SpreadExpr; fn into_allocated(self) -> Self::Allocated { SpreadExpr { @@ -296,10 +311,7 @@ impl Node for SpreadExpr { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Prop { Init(PropInit), Method(PropMethod), @@ -308,7 +320,10 @@ pub enum Prop { Set(PropSet), } -impl IntoAllocated for Prop where T: ToString { +impl IntoAllocated for Prop +where + T: ToString, +{ type Allocated = Prop; fn into_allocated(self) -> Self::Allocated { match self { @@ -358,23 +373,23 @@ impl Prop { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct PropInit { pub key: PropInitKey, pub colon: Option, pub value: Option>, } -impl IntoAllocated for PropInit where T: ToString { +impl IntoAllocated for PropInit +where + T: ToString, +{ type Allocated = PropInit; fn into_allocated(self) -> Self::Allocated { PropInit { key: self.key.into_allocated(), colon: self.colon, - value: self.value.into_allocated() + value: self.value.into_allocated(), } } } @@ -402,16 +417,16 @@ impl PropInit { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct PropInitKey { pub value: PropKey, pub brackets: Option<(OpenBracket, CloseBracket)>, } -impl IntoAllocated for PropInitKey where T: ToString { +impl IntoAllocated for PropInitKey +where + T: ToString, +{ type Allocated = PropInitKey; fn into_allocated(self) -> Self::Allocated { PropInitKey { @@ -435,10 +450,7 @@ impl Node for PropInitKey { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct PropMethod { pub keyword_static: Option, pub keyword_async: Option, @@ -450,7 +462,10 @@ pub struct PropMethod { pub body: FuncBody, } -impl IntoAllocated for PropMethod where T: ToString { +impl IntoAllocated for PropMethod +where + T: ToString, +{ type Allocated = PropMethod; fn into_allocated(self) -> Self::Allocated { PropMethod { @@ -459,7 +474,11 @@ impl IntoAllocated for PropMethod where T: ToString { id: self.id.into_allocated(), star: self.star, open_paren: self.open_paren, - params: self.params.into_iter().map(IntoAllocated::into_allocated).collect(), + params: self + .params + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), close_paren: self.close_paren, body: self.body.into_allocated(), } @@ -483,10 +502,7 @@ impl Node for PropMethod { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct PropCtor { pub keyword: PropInitKey, pub open_paren: OpenParen, @@ -495,13 +511,20 @@ pub struct PropCtor { pub body: FuncBody, } -impl IntoAllocated for PropCtor where T: ToString { +impl IntoAllocated for PropCtor +where + T: ToString, +{ type Allocated = PropCtor; fn into_allocated(self) -> Self::Allocated { PropCtor { keyword: self.keyword.into_allocated(), open_paren: self.open_paren, - params: self.params.into_iter().map(IntoAllocated::into_allocated).collect(), + params: self + .params + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), close_paren: self.close_paren, body: self.body.into_allocated(), } @@ -518,10 +541,7 @@ impl Node for PropCtor { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct PropGet { pub keyword_static: Option, pub keyword_get: Get, @@ -531,7 +551,10 @@ pub struct PropGet { pub body: FuncBody, } -impl IntoAllocated for PropGet where T: ToString { +impl IntoAllocated for PropGet +where + T: ToString, +{ type Allocated = PropGet; fn into_allocated(self) -> Self::Allocated { PropGet { @@ -561,10 +584,7 @@ impl Node for PropGet { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct PropSet { pub keyword_static: Option, pub keyword_set: Set, @@ -575,7 +595,10 @@ pub struct PropSet { pub body: FuncBody, } -impl IntoAllocated for PropSet where T: ToString { +impl IntoAllocated for PropSet +where + T: ToString, +{ type Allocated = PropSet; fn into_allocated(self) -> Self::Allocated { PropSet { @@ -607,17 +630,17 @@ impl Node for PropSet { /// An object literal or class property identifier #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum PropKey { Lit(Lit), Expr(Expr), Pat(Pat), } -impl IntoAllocated for PropKey where T: ToString { +impl IntoAllocated for PropKey +where + T: ToString, +{ type Allocated = PropKey; fn into_allocated(self) -> Self::Allocated { match self { @@ -640,17 +663,17 @@ impl Node for PropKey { /// The value of an object literal or class property #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum PropValue { Expr(Expr), Pat(Pat), Method(PropMethod), } -impl IntoAllocated for PropValue where T: ToString { +impl IntoAllocated for PropValue +where + T: ToString, +{ type Allocated = PropValue; fn into_allocated(self) -> Self::Allocated { match self { @@ -673,16 +696,16 @@ impl Node for PropValue { /// An operation that takes one argument #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct UnaryExpr { pub operator: UnaryOp, pub argument: Box>, } -impl IntoAllocated for UnaryExpr where T: ToString { +impl IntoAllocated for UnaryExpr +where + T: ToString, +{ type Allocated = UnaryExpr; fn into_allocated(self) -> Self::Allocated { UnaryExpr { @@ -711,16 +734,16 @@ impl Node for UnaryExpr { /// Increment or decrementing a value #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct UpdateExpr { pub operator: UpdateOp, pub argument: Box>, } -impl IntoAllocated for UpdateExpr where T: ToString { +impl IntoAllocated for UpdateExpr +where + T: ToString, +{ type Allocated = UpdateExpr; fn into_allocated(self) -> Self::Allocated { UpdateExpr { @@ -756,17 +779,17 @@ impl Node for UpdateExpr { /// An operation that requires 2 arguments #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct BinaryExpr { pub operator: BinaryOp, pub left: Box>, pub right: Box>, } -impl IntoAllocated for BinaryExpr where T: ToString { +impl IntoAllocated for BinaryExpr +where + T: ToString, +{ type Allocated = BinaryExpr; fn into_allocated(self) -> Self::Allocated { BinaryExpr { @@ -788,17 +811,17 @@ impl Node for BinaryExpr { /// An assignment or update + assignment operation #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct AssignExpr { pub operator: AssignOp, pub left: AssignLeft, pub right: Box>, } -impl IntoAllocated for AssignExpr where T: ToString { +impl IntoAllocated for AssignExpr +where + T: ToString, +{ type Allocated = AssignExpr; fn into_allocated(self) -> Self::Allocated { AssignExpr { @@ -819,16 +842,16 @@ impl Node for AssignExpr { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct AwaitExpr { pub keyword: Await, pub expr: Expr, } -impl IntoAllocated for AwaitExpr where T: ToString { +impl IntoAllocated for AwaitExpr +where + T: ToString, +{ type Allocated = AwaitExpr; fn into_allocated(self) -> Self::Allocated { AwaitExpr { @@ -849,16 +872,16 @@ impl Node for AwaitExpr { /// The value being assigned to #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum AssignLeft { Pat(Pat), Expr(Box>), } -impl IntoAllocated for AssignLeft where T: ToString { +impl IntoAllocated for AssignLeft +where + T: ToString, +{ type Allocated = AssignLeft; fn into_allocated(self) -> Self::Allocated { match self { @@ -883,17 +906,17 @@ impl Node for AssignLeft { /// false || true /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct LogicalExpr { pub operator: LogicalOp, pub left: Box>, pub right: Box>, } -impl IntoAllocated for LogicalExpr where T: ToString { +impl IntoAllocated for LogicalExpr +where + T: ToString, +{ type Allocated = LogicalExpr; fn into_allocated(self) -> Self::Allocated { LogicalExpr { @@ -919,24 +942,24 @@ impl Node for LogicalExpr { /// c.stuff; /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct MemberExpr { pub object: Box>, pub property: Box>, pub indexer: MemberIndexer, } -impl IntoAllocated for MemberExpr where T: ToString { +impl IntoAllocated for MemberExpr +where + T: ToString, +{ type Allocated = MemberExpr; fn into_allocated(self) -> Self::Allocated { MemberExpr { object: self.object.into_allocated(), property: self.property.into_allocated(), - indexer: self.indexer + indexer: self.indexer, } } } @@ -961,10 +984,7 @@ impl Node for MemberExpr { } #[derive(PartialEq, Debug, Clone, Copy)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum MemberIndexer { Period(Period), Computed { @@ -993,10 +1013,7 @@ impl Node for MemberIndexer { /// var a = true ? 'stuff' : 'things'; /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ConditionalExpr { pub test: Box>, pub question_mark: QuestionMark, @@ -1005,7 +1022,10 @@ pub struct ConditionalExpr { pub consequent: Box>, } -impl IntoAllocated for ConditionalExpr where T: ToString { +impl IntoAllocated for ConditionalExpr +where + T: ToString, +{ type Allocated = ConditionalExpr; fn into_allocated(self) -> Self::Allocated { ConditionalExpr { @@ -1031,10 +1051,7 @@ impl Node for ConditionalExpr { /// Math.random() /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct CallExpr { pub callee: Box>, pub open_paren: OpenParen, @@ -1042,14 +1059,21 @@ pub struct CallExpr { pub close_paren: CloseParen, } -impl IntoAllocated for CallExpr where T: ToString { +impl IntoAllocated for CallExpr +where + T: ToString, +{ type Allocated = CallExpr; fn into_allocated(self) -> Self::Allocated { CallExpr { callee: self.callee.into_allocated(), open_paren: self.open_paren, - arguments: self.arguments.into_iter().map(IntoAllocated::into_allocated).collect(), + arguments: self + .arguments + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), close_paren: self.close_paren, } } @@ -1069,10 +1093,7 @@ impl Node for CallExpr { /// new Uint8Array(32); /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct NewExpr { pub keyword: New, pub callee: Box>, @@ -1081,7 +1102,10 @@ pub struct NewExpr { pub close_paren: Option, } -impl IntoAllocated for NewExpr where T: ToString { +impl IntoAllocated for NewExpr +where + T: ToString, +{ type Allocated = NewExpr; fn into_allocated(self) -> Self::Allocated { @@ -1089,7 +1113,11 @@ impl IntoAllocated for NewExpr where T: ToString { keyword: self.keyword, callee: self.callee.into_allocated(), open_paren: self.open_paren, - arguments: self.arguments.into_iter().map(IntoAllocated::into_allocated).collect(), + arguments: self + .arguments + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), close_paren: self.close_paren, } } @@ -1106,7 +1134,7 @@ impl Node for NewExpr { }; SourceLocation { start: self.keyword.start(), - end: end, + end, } } } @@ -1134,10 +1162,7 @@ impl Node for SequenceExpr { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ArrowParamPlaceHolder { // async keyword pub keyword: Option, @@ -1146,13 +1171,20 @@ pub struct ArrowParamPlaceHolder { pub close_paren: Option, } -impl IntoAllocated for ArrowParamPlaceHolder where T: ToString { +impl IntoAllocated for ArrowParamPlaceHolder +where + T: ToString, +{ type Allocated = ArrowParamPlaceHolder; fn into_allocated(self) -> Self::Allocated { ArrowParamPlaceHolder { keyword: self.keyword, open_paren: self.open_paren, - args: self.args.into_iter().map(IntoAllocated::into_allocated).collect(), + args: self + .args + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), close_paren: self.close_paren, } } @@ -1188,10 +1220,7 @@ impl Node for ArrowParamPlaceHolder { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ArrowFuncExpr { pub keyword: Option, pub star: Option, @@ -1202,17 +1231,24 @@ pub struct ArrowFuncExpr { pub body: ArrowFuncBody, } -impl IntoAllocated for ArrowFuncExpr where T: ToString { +impl IntoAllocated for ArrowFuncExpr +where + T: ToString, +{ type Allocated = ArrowFuncExpr; fn into_allocated(self) -> Self::Allocated { ArrowFuncExpr { keyword: self.keyword, star: self.star, open_paren: self.open_paren, - params: self.params.into_iter().map(IntoAllocated::into_allocated).collect(), + params: self + .params + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), close_paren: self.close_paren, arrow: self.arrow, - body: self.body.into_allocated() + body: self.body.into_allocated(), } } } @@ -1237,16 +1273,16 @@ impl Node for ArrowFuncExpr { /// The body portion of an arrow function can be either an expression or a block of statements #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum ArrowFuncBody { FuncBody(FuncBody), Expr(Box>), } -impl IntoAllocated for ArrowFuncBody where T: ToString { +impl IntoAllocated for ArrowFuncBody +where + T: ToString, +{ type Allocated = ArrowFuncBody; fn into_allocated(self) -> Self::Allocated { match self { @@ -1274,17 +1310,17 @@ impl Node for ArrowFuncBody { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct YieldExpr { pub keyword: Yield, pub argument: Option>>, pub star: Option, } -impl IntoAllocated for YieldExpr where T: ToString { +impl IntoAllocated for YieldExpr +where + T: ToString, +{ type Allocated = YieldExpr; fn into_allocated(self) -> Self::Allocated { YieldExpr { @@ -1312,16 +1348,16 @@ impl Node for YieldExpr { /// A Template literal preceded by a function identifier /// see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates) for more details #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct TaggedTemplateExpr { pub tag: Box>, pub quasi: TemplateLit, } -impl IntoAllocated for TaggedTemplateExpr where T: ToString { +impl IntoAllocated for TaggedTemplateExpr +where + T: ToString, +{ type Allocated = TaggedTemplateExpr; fn into_allocated(self) -> Self::Allocated { TaggedTemplateExpr { @@ -1345,26 +1381,33 @@ impl Node for TaggedTemplateExpr { /// `I own ${0} birds`; /// ``` #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct TemplateLit { pub quasis: Vec>, pub expressions: Vec>, } -impl IntoAllocated for TemplateLit where T: ToString { +impl IntoAllocated for TemplateLit +where + T: ToString, +{ type Allocated = TemplateLit; fn into_allocated(self) -> Self::Allocated { TemplateLit { - quasis: self.quasis.into_iter().map(IntoAllocated::into_allocated).collect(), - expressions: self.expressions.into_iter().map(IntoAllocated::into_allocated).collect(), + quasis: self + .quasis + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), + expressions: self + .expressions + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), } } } - impl Node for TemplateLit { fn loc(&self) -> SourceLocation { let start = self @@ -1386,17 +1429,17 @@ impl Node for TemplateLit { /// The text part of a `TemplateLiteral` #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct TemplateElement { pub open_quote: QuasiQuote, pub content: Slice, pub close_quote: QuasiQuote, } -impl IntoAllocated for TemplateElement where T: ToString { +impl IntoAllocated for TemplateElement +where + T: ToString, +{ type Allocated = TemplateElement; fn into_allocated(self) -> Self::Allocated { TemplateElement { @@ -1439,17 +1482,17 @@ impl Node for TemplateElement { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct MetaProp { pub meta: Ident, pub dot: Period, pub property: Ident, } -impl IntoAllocated for MetaProp where T: ToString { +impl IntoAllocated for MetaProp +where + T: ToString, +{ type Allocated = MetaProp; fn into_allocated(self) -> Self::Allocated { MetaProp { @@ -1471,10 +1514,7 @@ impl Node for MetaProp { /// A literal value #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Lit { /// `null` Null(Null), @@ -1501,7 +1541,10 @@ pub enum Lit { Template(TemplateLit), } -impl IntoAllocated for Lit where T: ToString { +impl IntoAllocated for Lit +where + T: ToString, +{ type Allocated = Lit; fn into_allocated(self) -> Self::Allocated { match self { @@ -1526,10 +1569,7 @@ impl Lit { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Boolean { True(True), False(False), @@ -1582,17 +1622,17 @@ impl Node for Lit { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct StringLit { pub open_quote: Quote, pub content: Slice, pub close_quote: Quote, } -impl IntoAllocated for StringLit where T: ToString { +impl IntoAllocated for StringLit +where + T: ToString, +{ type Allocated = StringLit; fn into_allocated(self) -> Self::Allocated { StringLit { @@ -1614,10 +1654,7 @@ impl Node for StringLit { /// A regular expression literal #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct RegEx { pub open_slash: ForwardSlash, pub pattern: Slice, @@ -1625,7 +1662,10 @@ pub struct RegEx { pub flags: Option>, } -impl IntoAllocated for RegEx where T: ToString { +impl IntoAllocated for RegEx +where + T: ToString, +{ type Allocated = RegEx; fn into_allocated(self) -> Self::Allocated { RegEx { @@ -1652,17 +1692,17 @@ impl Node for RegEx { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct WrappedExpr { pub open_paren: OpenParen, pub expr: Expr, pub close_paren: CloseParen, } -impl IntoAllocated for WrappedExpr where T: ToString { +impl IntoAllocated for WrappedExpr +where + T: ToString, +{ type Allocated = WrappedExpr; fn into_allocated(self) -> Self::Allocated { WrappedExpr { @@ -1683,16 +1723,16 @@ impl Node for WrappedExpr { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct SequenceExprEntry { pub expr: Expr, pub comma: Option, } -impl IntoAllocated for SequenceExprEntry where T: ToString { +impl IntoAllocated for SequenceExprEntry +where + T: ToString, +{ type Allocated = SequenceExprEntry; fn into_allocated(self) -> Self::Allocated { SequenceExprEntry { diff --git a/src/spanned/mod.rs b/src/spanned/mod.rs index 3052860..d65e13a 100644 --- a/src/spanned/mod.rs +++ b/src/spanned/mod.rs @@ -13,7 +13,7 @@ use stmt::Stmt; use crate::IntoAllocated; #[cfg(feature = "serde")] -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; use self::{ pat::RestPat, @@ -28,10 +28,7 @@ pub trait Node { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Ident { pub slice: Slice, } @@ -46,7 +43,6 @@ where } } - impl IntoAllocated for Ident where T: ToString, @@ -83,10 +79,7 @@ impl Ident { /// with a flag denoting if the representation is /// a ES6 Mod or a Script. #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Program { /// An ES6 Mod Mod(Vec>), @@ -94,12 +87,25 @@ pub enum Program { Script(Vec>), } -impl IntoAllocated for Program where T: ToString { +impl IntoAllocated for Program +where + T: ToString, +{ type Allocated = Program; fn into_allocated(self) -> Program { match self { - Program::Mod(parts) => Program::Mod(parts.into_iter().map(IntoAllocated::into_allocated).collect()), - Program::Script(parts) => Program::Script(parts.into_iter().map(IntoAllocated::into_allocated).collect()), + Program::Mod(parts) => Program::Mod( + parts + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), + ), + Program::Script(parts) => Program::Script( + parts + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), + ), } } } @@ -139,10 +145,7 @@ impl Node for Vec> { /// A single part of a Javascript program. /// This will be either a Directive, Decl or a Stmt #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum ProgramPart { /// A Directive like `'use strict';` Dir(Dir), @@ -152,7 +155,10 @@ pub enum ProgramPart { Stmt(Stmt), } -impl IntoAllocated for ProgramPart where T: ToString { +impl IntoAllocated for ProgramPart +where + T: ToString, +{ type Allocated = ProgramPart; fn into_allocated(self) -> ProgramPart { match self { @@ -185,17 +191,17 @@ impl ProgramPart { /// pretty much always `'use strict'`, this can appear at the /// top of a file or function #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Dir { pub expr: Lit, pub dir: T, pub semi_colon: Option, } -impl IntoAllocated for Dir where T: ToString { +impl IntoAllocated for Dir +where + T: ToString, +{ type Allocated = Dir; fn into_allocated(self) -> Dir { Dir { @@ -230,10 +236,7 @@ impl Node for Dir { /// let y = function q() {} /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Func { pub keyword: Function, pub id: Option>, @@ -254,7 +257,6 @@ impl Func { } } - impl IntoAllocated for Func where T: ToString, @@ -265,7 +267,11 @@ where keyword: self.keyword, id: self.id.map(|i| i.into_allocated()), open_paren: self.open_paren, - params: self.params.into_iter().map(|p| p.into_allocated()).collect(), + params: self + .params + .into_iter() + .map(|p| p.into_allocated()) + .collect(), close_paren: self.close_paren, body: self.body.into_allocated(), star: self.star, @@ -287,19 +293,22 @@ impl Node for Func { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct FuncArgEntry { pub value: FuncArg, pub comma: Option, } -impl IntoAllocated for FuncArgEntry where T: ToString { +impl IntoAllocated for FuncArgEntry +where + T: ToString, +{ type Allocated = FuncArgEntry; fn into_allocated(self) -> FuncArgEntry { - FuncArgEntry { value: self.value.into_allocated(), comma: self.comma } + FuncArgEntry { + value: self.value.into_allocated(), + comma: self.comma, + } } } @@ -317,17 +326,17 @@ impl Node for FuncArgEntry { /// A single function argument from a function signature #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum FuncArg { Expr(Expr), Pat(Pat), Rest(Box>), } -impl IntoAllocated for FuncArg where T: ToString { +impl IntoAllocated for FuncArg +where + T: ToString, +{ type Allocated = FuncArg; fn into_allocated(self) -> FuncArg { match self { @@ -350,20 +359,24 @@ impl Node for FuncArg { /// The block statement that makes up the function's body #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct FuncBody { pub open_brace: OpenBrace, pub stmts: Vec>, pub close_brace: CloseBrace, } -impl IntoAllocated for FuncBody where T: ToString { +impl IntoAllocated for FuncBody +where + T: ToString, +{ type Allocated = FuncBody; fn into_allocated(self) -> FuncBody { - FuncBody { open_brace: self.open_brace, stmts: self.stmts.into_iter().map(|s| s.into_allocated()).collect(), close_brace: self.close_brace } + FuncBody { + open_brace: self.open_brace, + stmts: self.stmts.into_iter().map(|s| s.into_allocated()).collect(), + close_brace: self.close_brace, + } } } @@ -403,10 +416,7 @@ impl Node for FuncBody { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Class { pub keyword: tokens::Class, pub id: Option>, @@ -414,12 +424,14 @@ pub struct Class { pub body: ClassBody, } - -impl IntoAllocated for Class where T: ToString { +impl IntoAllocated for Class +where + T: ToString, +{ type Allocated = Class; fn into_allocated(self) -> Class { Class { - keyword: self.keyword, + keyword: self.keyword, id: self.id.map(|i| i.into_allocated()), super_class: self.super_class.map(|s| s.into_allocated()), body: self.body.into_allocated(), @@ -437,37 +449,48 @@ impl Node for Class { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct SuperClass { pub keyword_extends: Extends, pub expr: Expr, } -impl IntoAllocated for SuperClass where T: ToString { +impl IntoAllocated for SuperClass +where + T: ToString, +{ type Allocated = SuperClass; fn into_allocated(self) -> SuperClass { - SuperClass { keyword_extends: self.keyword_extends, expr: self.expr.into_allocated() } + SuperClass { + keyword_extends: self.keyword_extends, + expr: self.expr.into_allocated(), + } } } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ClassBody { pub open_brace: OpenBrace, pub props: Vec>, pub close_brace: CloseBrace, } -impl IntoAllocated for ClassBody where T: ToString { +impl IntoAllocated for ClassBody +where + T: ToString, +{ type Allocated = ClassBody; fn into_allocated(self) -> ClassBody { - ClassBody { open_brace: self.open_brace, props: self.props.into_iter().map(IntoAllocated::into_allocated).collect(), close_brace: self.close_brace } + ClassBody { + open_brace: self.open_brace, + props: self + .props + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), + close_brace: self.close_brace, + } } } @@ -481,10 +504,7 @@ impl Node for ClassBody { /// The kind of variable being defined (`var`/`let`/`const`) #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum VarKind { Var(Option), Let(Let), @@ -514,13 +534,14 @@ impl VarKind { VarKind::Const(_) => 4, } } + + pub const fn is_empty(&self) -> bool { + matches!(self, VarKind::Var(None)) + } } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Slice { pub source: T, pub loc: SourceLocation, @@ -542,17 +563,14 @@ where impl Slice { pub fn new(source: T, start_line: u32, start_col: u32, end_line: u32, end_column: u32) -> Self { Self { - source: source, + source, loc: SourceLocation::new(start_line, start_col, end_line, end_column), } } } #[derive(Debug, Clone, PartialEq, Copy)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct SourceLocation { pub start: Position, pub end: Position, @@ -587,10 +605,7 @@ impl core::cmp::PartialOrd for SourceLocation { } #[derive(Debug, Clone, PartialEq, Copy)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Position { pub line: u32, pub column: u32, @@ -657,16 +672,16 @@ impl std::ops::Sub for Position { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ListEntry { pub item: Item, pub comma: Option, } -impl IntoAllocated for ListEntry where Item: IntoAllocated { +impl IntoAllocated for ListEntry +where + Item: IntoAllocated, +{ type Allocated = ListEntry; fn into_allocated(self) -> Self::Allocated { @@ -678,7 +693,6 @@ impl IntoAllocated for ListEntry where Item: IntoAllocated { } impl ListEntry { - pub fn no_comma(item: Item) -> Self { Self { item, comma: None } } diff --git a/src/spanned/pat.rs b/src/spanned/pat.rs index 07855ff..6a5eacd 100644 --- a/src/spanned/pat.rs +++ b/src/spanned/pat.rs @@ -1,19 +1,16 @@ -use crate::IntoAllocated; use crate::spanned::expr::{Expr, Prop}; use crate::spanned::Ident; +use crate::IntoAllocated; use super::tokens::{CloseBrace, CloseBracket, Comma, Ellipsis, OpenBrace, OpenBracket, Token}; use super::{AssignOp, ListEntry, Node, SourceLocation}; #[cfg(feature = "serde")] -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; /// All of the different ways you can declare an identifier /// and/or value #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Pat { Ident(Ident), Obj(ObjPat), @@ -21,7 +18,10 @@ pub enum Pat { Assign(AssignPat), } -impl IntoAllocated for Pat where T: ToString { +impl IntoAllocated for Pat +where + T: ToString, +{ type Allocated = Pat; fn into_allocated(self) -> Self::Allocated { match self { @@ -45,22 +45,26 @@ impl Node for Pat { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ArrayPat { pub open_bracket: OpenBracket, pub elements: Vec>>>, pub close_bracket: CloseBracket, } -impl IntoAllocated for ArrayPat where T: ToString { +impl IntoAllocated for ArrayPat +where + T: ToString, +{ type Allocated = ArrayPat; fn into_allocated(self) -> Self::Allocated { ArrayPat { open_bracket: self.open_bracket, - elements: self.elements.into_iter().map(IntoAllocated::into_allocated).collect(), + elements: self + .elements + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), close_bracket: self.close_bracket, } } @@ -76,16 +80,16 @@ impl Node for ArrayPat { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ArrayElement { pub part: Option>, pub comma: Option, } -impl IntoAllocated for ArrayElement where T: ToString { +impl IntoAllocated for ArrayElement +where + T: ToString, +{ type Allocated = ArrayElement; fn into_allocated(self) -> Self::Allocated { ArrayElement { @@ -96,17 +100,17 @@ impl IntoAllocated for ArrayElement where T: ToString { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum ArrayPatPart { Pat(Pat), Expr(Expr), Rest(RestPat), } -impl IntoAllocated for ArrayPatPart where T: ToString { +impl IntoAllocated for ArrayPatPart +where + T: ToString, +{ type Allocated = ArrayPatPart; fn into_allocated(self) -> Self::Allocated { match self { @@ -131,22 +135,26 @@ type ObjEntry = ListEntry>; /// similar to an `ObjectExpr` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ObjPat { pub open_brace: OpenBrace, pub props: Vec>, pub close_brace: CloseBrace, } -impl IntoAllocated for ObjPat where T: ToString { +impl IntoAllocated for ObjPat +where + T: ToString, +{ type Allocated = ObjPat; fn into_allocated(self) -> Self::Allocated { ObjPat { open_brace: self.open_brace, - props: self.props.into_iter().map(IntoAllocated::into_allocated).collect(), + props: self + .props + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), close_brace: self.close_brace, } } @@ -163,16 +171,16 @@ impl Node for ObjPat { /// A single part of an ObjectPat #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum ObjPatPart { Assign(Prop), Rest(Box>), } -impl IntoAllocated for ObjPatPart where T: ToString { +impl IntoAllocated for ObjPatPart +where + T: ToString, +{ type Allocated = ObjPatPart; fn into_allocated(self) -> Self::Allocated { match self { @@ -192,16 +200,16 @@ impl Node for ObjPatPart { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct RestPat { pub dots: Ellipsis, pub pat: Pat, } -impl IntoAllocated for RestPat where T: ToString { +impl IntoAllocated for RestPat +where + T: ToString, +{ type Allocated = RestPat; fn into_allocated(self) -> Self::Allocated { RestPat { @@ -222,17 +230,17 @@ impl Node for RestPat { /// An assignment as a pattern #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct AssignPat { pub left: Box>, pub operator: AssignOp, pub right: Box>, } -impl IntoAllocated for AssignPat where T: ToString { +impl IntoAllocated for AssignPat +where + T: ToString, +{ type Allocated = AssignPat; fn into_allocated(self) -> Self::Allocated { AssignPat { diff --git a/src/spanned/stmt.rs b/src/spanned/stmt.rs index 4ab3e44..d5233a2 100644 --- a/src/spanned/stmt.rs +++ b/src/spanned/stmt.rs @@ -1,9 +1,9 @@ -use crate::IntoAllocated; use crate::spanned::decl::VarDecl; use crate::spanned::expr::Expr; use crate::spanned::pat::Pat; use crate::spanned::VarKind; use crate::spanned::{Ident, ProgramPart}; +use crate::IntoAllocated; use super::decl::VarDecls; use super::tokens::{ @@ -13,14 +13,11 @@ use super::tokens::{ }; use super::{ListEntry, Node, SourceLocation}; #[cfg(feature = "serde")] -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; /// A slightly more granular part of an es program than ProgramPart #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Stmt { /// Any expression Expr { @@ -212,29 +209,76 @@ pub enum Stmt { }, } -impl IntoAllocated for Stmt where T: ToString { +impl IntoAllocated for Stmt +where + T: ToString, +{ type Allocated = Stmt; fn into_allocated(self) -> Self::Allocated { match self { - Stmt::Expr { expr, semi_colon } => Stmt::Expr { expr: expr.into_allocated(), semi_colon }, + Stmt::Expr { expr, semi_colon } => Stmt::Expr { + expr: expr.into_allocated(), + semi_colon, + }, Stmt::Block(inner) => Stmt::Block(inner.into_allocated()), Stmt::Empty(inner) => Stmt::Empty(inner), - Stmt::Debugger { keyword, semi_colon } => Stmt::Debugger { keyword, semi_colon }, + Stmt::Debugger { + keyword, + semi_colon, + } => Stmt::Debugger { + keyword, + semi_colon, + }, Stmt::With(inner) => Stmt::With(inner.into_allocated()), - Stmt::Return { keyword, value, semi_colon } => Stmt::Return { keyword, value: value.into_allocated(), semi_colon }, + Stmt::Return { + keyword, + value, + semi_colon, + } => Stmt::Return { + keyword, + value: value.into_allocated(), + semi_colon, + }, Stmt::Labeled(inner) => Stmt::Labeled(inner.into_allocated()), - Stmt::Break { keyword, label, semi_colon } => Stmt::Break { keyword, label: label.into_allocated(), semi_colon}, - Stmt::Continue { keyword, label, semi_colon } => Stmt::Continue { keyword, label: label.into_allocated(), semi_colon }, + Stmt::Break { + keyword, + label, + semi_colon, + } => Stmt::Break { + keyword, + label: label.into_allocated(), + semi_colon, + }, + Stmt::Continue { + keyword, + label, + semi_colon, + } => Stmt::Continue { + keyword, + label: label.into_allocated(), + semi_colon, + }, Stmt::If(inner) => Stmt::If(inner.into_allocated()), Stmt::Switch(inner) => Stmt::Switch(inner.into_allocated()), - Stmt::Throw { keyword, expr, semi_colon } => Stmt::Throw { keyword, expr: expr.into_allocated(), semi_colon }, + Stmt::Throw { + keyword, + expr, + semi_colon, + } => Stmt::Throw { + keyword, + expr: expr.into_allocated(), + semi_colon, + }, Stmt::Try(inner) => Stmt::Try(inner.into_allocated()), Stmt::While(inner) => Stmt::While(inner.into_allocated()), Stmt::DoWhile(inner) => Stmt::DoWhile(inner.into_allocated()), Stmt::For(inner) => Stmt::For(inner.into_allocated()), Stmt::ForIn(inner) => Stmt::ForIn(inner.into_allocated()), Stmt::ForOf(inner) => Stmt::ForOf(inner.into_allocated()), - Stmt::Var { decls, semi_colon } => Stmt::Var { decls: decls.into_allocated(), semi_colon }, + Stmt::Var { decls, semi_colon } => Stmt::Var { + decls: decls.into_allocated(), + semi_colon, + }, } } } @@ -375,10 +419,7 @@ impl Node for Stmt { /// //rand !== 0 /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct WithStmt { pub keyword: With, pub open_paren: OpenParen, @@ -387,7 +428,10 @@ pub struct WithStmt { pub body: Box>, } -impl IntoAllocated for WithStmt where T: ToString { +impl IntoAllocated for WithStmt +where + T: ToString, +{ type Allocated = WithStmt; fn into_allocated(self) -> Self::Allocated { WithStmt { @@ -417,17 +461,17 @@ impl Node for WithStmt { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct LabeledStmt { pub label: Ident, pub colon: Colon, pub body: Box>, } -impl IntoAllocated for LabeledStmt where T: ToString { +impl IntoAllocated for LabeledStmt +where + T: ToString, +{ type Allocated = LabeledStmt; fn into_allocated(self) -> Self::Allocated { @@ -457,10 +501,7 @@ impl Node for LabeledStmt { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct IfStmt { pub keyword: If, pub open_paren: OpenParen, @@ -470,7 +511,10 @@ pub struct IfStmt { pub alternate: Option>>, } -impl IntoAllocated for IfStmt where T: ToString { +impl IntoAllocated for IfStmt +where + T: ToString, +{ type Allocated = IfStmt; fn into_allocated(self) -> Self::Allocated { @@ -498,16 +542,16 @@ impl Node for IfStmt { } #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ElseStmt { pub keyword: Else, pub body: Stmt, } -impl IntoAllocated for ElseStmt where T: ToString { +impl IntoAllocated for ElseStmt +where + T: ToString, +{ type Allocated = ElseStmt; fn into_allocated(self) -> Self::Allocated { @@ -542,10 +586,7 @@ impl Node for ElseStmt { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct SwitchStmt { pub keyword: Switch, pub open_paren: OpenParen, @@ -556,7 +597,10 @@ pub struct SwitchStmt { pub close_brace: CloseBrace, } -impl IntoAllocated for SwitchStmt where T: ToString { +impl IntoAllocated for SwitchStmt +where + T: ToString, +{ type Allocated = SwitchStmt; fn into_allocated(self) -> Self::Allocated { @@ -566,7 +610,11 @@ impl IntoAllocated for SwitchStmt where T: ToString { discriminant: self.discriminant.into_allocated(), close_paren: self.close_paren, open_brace: self.open_brace, - cases: self.cases.into_iter().map(IntoAllocated::into_allocated).collect(), + cases: self + .cases + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), close_brace: self.close_brace, } } @@ -583,10 +631,7 @@ impl Node for SwitchStmt { /// A single case part of a switch statement #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct SwitchCase { pub keyword: SwitchCaseKeyword, pub test: Option>, @@ -608,14 +653,21 @@ impl Node for SwitchCase { } } -impl IntoAllocated for SwitchCase where T: ToString { +impl IntoAllocated for SwitchCase +where + T: ToString, +{ type Allocated = SwitchCase; fn into_allocated(self) -> Self::Allocated { SwitchCase { keyword: self.keyword, colon: self.colon, - consequent: self.consequent.into_iter().map(IntoAllocated::into_allocated).collect(), + consequent: self + .consequent + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), test: self.test.into_allocated(), } } @@ -623,23 +675,27 @@ impl IntoAllocated for SwitchCase where T: ToString { /// A collection of program parts wrapped in curly braces #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct BlockStmt { pub open_brace: OpenBrace, pub stmts: Vec>, pub close_brace: CloseBrace, } -impl IntoAllocated for BlockStmt where T: ToString { +impl IntoAllocated for BlockStmt +where + T: ToString, +{ type Allocated = BlockStmt; fn into_allocated(self) -> Self::Allocated { BlockStmt { open_brace: self.open_brace, - stmts: self.stmts.into_iter().map(IntoAllocated::into_allocated).collect(), + stmts: self + .stmts + .into_iter() + .map(IntoAllocated::into_allocated) + .collect(), close_brace: self.close_brace, } } @@ -665,10 +721,7 @@ impl Node for BlockStmt { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct TryStmt { pub keyword: Try, pub block: BlockStmt, @@ -676,7 +729,10 @@ pub struct TryStmt { pub finalizer: Option>, } -impl IntoAllocated for TryStmt where T: ToString { +impl IntoAllocated for TryStmt +where + T: ToString, +{ type Allocated = TryStmt; fn into_allocated(self) -> Self::Allocated { TryStmt { @@ -706,17 +762,17 @@ impl Node for TryStmt { /// The error handling part of a `TryStmt` #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct CatchClause { pub keyword: Catch, pub param: Option>, pub body: BlockStmt, } -impl IntoAllocated for CatchClause where T: ToString { +impl IntoAllocated for CatchClause +where + T: ToString, +{ type Allocated = CatchClause; fn into_allocated(self) -> Self::Allocated { @@ -738,17 +794,17 @@ impl Node for CatchClause { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct CatchArg { pub open_paren: OpenParen, pub param: Pat, pub close_paren: CloseParen, } -impl IntoAllocated for CatchArg where T: ToString { +impl IntoAllocated for CatchArg +where + T: ToString, +{ type Allocated = CatchArg; fn into_allocated(self) -> Self::Allocated { @@ -770,16 +826,16 @@ impl Node for CatchArg { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct FinallyClause { pub keyword: Finally, pub body: BlockStmt, } -impl IntoAllocated for FinallyClause where T: ToString { +impl IntoAllocated for FinallyClause +where + T: ToString, +{ type Allocated = FinallyClause; fn into_allocated(self) -> Self::Allocated { FinallyClause { @@ -813,10 +869,7 @@ impl Node for FinallyClause { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct WhileStmt { pub keyword: While, pub open_paren: OpenParen, @@ -825,7 +878,10 @@ pub struct WhileStmt { pub body: Box>, } -impl IntoAllocated for WhileStmt where T: ToString { +impl IntoAllocated for WhileStmt +where + T: ToString, +{ type Allocated = WhileStmt; fn into_allocated(self) -> Self::Allocated { @@ -834,7 +890,7 @@ impl IntoAllocated for WhileStmt where T: ToString { open_paren: self.open_paren, test: self.test.into_allocated(), close_paren: self.close_paren, - body: self.body.into_allocated() + body: self.body.into_allocated(), } } } @@ -855,10 +911,7 @@ impl Node for WhileStmt { /// } while (Math.floor(Math.random() * 100) < 75) /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct DoWhileStmt { pub keyword_do: Do, pub body: Box>, @@ -869,7 +922,10 @@ pub struct DoWhileStmt { pub semi_colon: Option, } -impl IntoAllocated for DoWhileStmt where T: ToString { +impl IntoAllocated for DoWhileStmt +where + T: ToString, +{ type Allocated = DoWhileStmt; fn into_allocated(self) -> Self::Allocated { @@ -907,10 +963,7 @@ impl Node for DoWhileStmt { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ForStmt { pub keyword: For, pub open_paren: OpenParen, @@ -923,7 +976,10 @@ pub struct ForStmt { pub body: Box>, } -impl IntoAllocated for ForStmt where T: ToString { +impl IntoAllocated for ForStmt +where + T: ToString, +{ type Allocated = ForStmt; fn into_allocated(self) -> Self::Allocated { @@ -955,21 +1011,24 @@ impl Node for ForStmt { /// // vvvvvvvvv /// for (var i = 0;i < 100; i++) #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum LoopInit { Variable(VarKind, Vec>>), Expr(Expr), } -impl IntoAllocated for LoopInit where T: ToString { +impl IntoAllocated for LoopInit +where + T: ToString, +{ type Allocated = LoopInit; fn into_allocated(self) -> Self::Allocated { match self { - LoopInit::Variable(k, v) => LoopInit::Variable(k, v.into_iter().map(IntoAllocated::into_allocated).collect()), - LoopInit::Expr(inner) => LoopInit::Expr(inner.into_allocated()) + LoopInit::Variable(k, v) => LoopInit::Variable( + k, + v.into_iter().map(IntoAllocated::into_allocated).collect(), + ), + LoopInit::Expr(inner) => LoopInit::Expr(inner.into_allocated()), } } } @@ -1005,10 +1064,7 @@ impl Node for LoopInit { /// //prints a, b /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ForInStmt { pub keyword_for: For, pub open_paren: OpenParen, @@ -1019,7 +1075,10 @@ pub struct ForInStmt { pub body: Box>, } -impl IntoAllocated for ForInStmt where T: ToString { +impl IntoAllocated for ForInStmt +where + T: ToString, +{ type Allocated = ForInStmt; fn into_allocated(self) -> Self::Allocated { @@ -1053,10 +1112,7 @@ impl Node for ForInStmt { /// //prints 2, 3, 4, 5, 6 /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct ForOfStmt { pub keyword_for: For, pub open_paren: OpenParen, @@ -1068,7 +1124,10 @@ pub struct ForOfStmt { pub is_await: bool, } -impl IntoAllocated for ForOfStmt where T: ToString { +impl IntoAllocated for ForOfStmt +where + T: ToString, +{ type Allocated = ForOfStmt; fn into_allocated(self) -> Self::Allocated { @@ -1097,17 +1156,17 @@ impl Node for ForOfStmt { /// The values on the left hand side of the keyword /// in a for in or for of loop #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum LoopLeft { Expr(Expr), Variable(VarKind, VarDecl), Pat(Pat), } -impl IntoAllocated for LoopLeft where T: ToString { +impl IntoAllocated for LoopLeft +where + T: ToString, +{ type Allocated = LoopLeft; fn into_allocated(self) -> Self::Allocated { match self { diff --git a/src/spanned/tokens.rs b/src/spanned/tokens.rs index feb3f8c..72e6da6 100644 --- a/src/spanned/tokens.rs +++ b/src/spanned/tokens.rs @@ -2,7 +2,7 @@ use crate::spanned::{Node, Position, SourceLocation}; #[cfg(feature = "serde")] -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; macro_rules! impl_token { ($what:ty, $s:expr) => { @@ -22,9 +22,9 @@ macro_rules! impl_token { Self(other) } } - impl std::convert::Into for $what { - fn into(self) -> Position { - self.0 + impl std::convert::From<$what> for Position { + fn from(other: $what) -> Position { + other.0 } } impl std::cmp::PartialEq for $what { @@ -73,10 +73,7 @@ macro_rules! impl_token { macro_rules! define_token { ($name:ident, $s:expr) => { #[derive(Debug, Clone, Copy, PartialEq)] - #[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) - )] + #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] #[doc = $s] pub struct $name(Position); impl_token!($name, $s); @@ -205,10 +202,7 @@ define_token!(TripleGreaterThan, ">>>"); define_token!(TripleGreaterThanEqual, ">>>="); #[derive(Debug, Clone, Copy, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Quote { Double(DoubleQuote), Single(SingleQuote), @@ -238,10 +232,7 @@ impl Token for Quote { } #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum QuasiQuote { BackTick(BackTick), CloseBrace(CloseBrace), @@ -276,10 +267,7 @@ impl Token for QuasiQuote { /// The available operators for assignment Exprs #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum AssignOp { Equal(Equal), PlusEqual(PlusEqual), @@ -318,10 +306,7 @@ impl Node for AssignOp { /// The available logical operators #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum LogicalOp { Or(DoublePipe), And(DoubleAmpersand), @@ -338,10 +323,7 @@ impl Node for LogicalOp { /// The available operations for `Binary` Exprs #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum BinaryOp { Equal(DoubleEqual), NotEqual(BangEqual), @@ -399,10 +381,7 @@ define_token!(DoublePlus, "++"); define_token!(DoubleMinus, "--"); /// `++` or `--` #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum UpdateOp { Increment(DoublePlus), Decrement(DoubleMinus), @@ -420,10 +399,7 @@ impl Node for UpdateOp { /// The allowed operators for an Expr /// to be `Unary` #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum UnaryOp { Minus(Minus), Plus(Plus), @@ -449,10 +425,7 @@ impl Node for UnaryOp { } #[derive(Debug, PartialEq, Clone, Copy)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum SwitchCaseKeyword { Case(Case), Default(Default), diff --git a/src/stmt.rs b/src/stmt.rs index 1c97115..e4e65ba 100644 --- a/src/stmt.rs +++ b/src/stmt.rs @@ -1,19 +1,15 @@ use crate::decl::VarDecl; use crate::expr::Expr; use crate::pat::Pat; -use crate::{VarKind, IntoAllocated}; use crate::{Ident, ProgramPart}; - +use crate::{IntoAllocated, VarKind}; #[cfg(feature = "serde")] -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; /// A slightly more granular part of an es program than ProgramPart #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum Stmt { /// Any expression Expr(Expr), @@ -180,7 +176,10 @@ pub enum Stmt { Var(Vec>), } -impl IntoAllocated for Stmt where T: ToString { +impl IntoAllocated for Stmt +where + T: ToString, +{ type Allocated = Stmt; fn into_allocated(self) -> Self::Allocated { @@ -222,16 +221,16 @@ impl IntoAllocated for Stmt where T: ToString { /// //rand !== 0 /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct WithStmt { pub object: Expr, pub body: Box>, } -impl IntoAllocated for WithStmt where T: ToString { +impl IntoAllocated for WithStmt +where + T: ToString, +{ type Allocated = WithStmt; fn into_allocated(self) -> Self::Allocated { @@ -252,15 +251,16 @@ impl IntoAllocated for WithStmt where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct LabeledStmt { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct LabeledStmt { pub label: Ident, pub body: Box>, } -impl IntoAllocated for LabeledStmt where T: ToString { +impl IntoAllocated for LabeledStmt +where + T: ToString, +{ type Allocated = LabeledStmt; fn into_allocated(self) -> Self::Allocated { @@ -280,16 +280,17 @@ impl IntoAllocated for LabeledStmt where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct IfStmt { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct IfStmt { pub test: Expr, pub consequent: Box>, pub alternate: Option>>, } -impl IntoAllocated for IfStmt where T: ToString { +impl IntoAllocated for IfStmt +where + T: ToString, +{ type Allocated = IfStmt; fn into_allocated(self) -> Self::Allocated { @@ -316,15 +317,16 @@ impl IntoAllocated for IfStmt where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct SwitchStmt { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct SwitchStmt { pub discriminant: Expr, pub cases: Vec>, } -impl IntoAllocated for SwitchStmt where T: ToString { +impl IntoAllocated for SwitchStmt +where + T: ToString, +{ type Allocated = SwitchStmt; fn into_allocated(self) -> Self::Allocated { @@ -337,35 +339,39 @@ impl IntoAllocated for SwitchStmt where T: ToString { /// A single case part of a switch statement #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct SwitchCase { pub test: Option>, pub consequent: Vec>, } -impl IntoAllocated for SwitchCase where T: ToString { +impl IntoAllocated for SwitchCase +where + T: ToString, +{ type Allocated = SwitchCase; fn into_allocated(self) -> Self::Allocated { SwitchCase { test: self.test.map(IntoAllocated::into_allocated), - consequent: self.consequent.into_iter().map(|c| c.into_allocated()).collect(), + consequent: self + .consequent + .into_iter() + .map(|c| c.into_allocated()) + .collect(), } } } /// A collection of program parts wrapped in curly braces #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct BlockStmt(pub Vec>); -impl IntoAllocated for BlockStmt where T: ToString { +impl IntoAllocated for BlockStmt +where + T: ToString, +{ type Allocated = BlockStmt; fn into_allocated(self) -> Self::Allocated { @@ -384,16 +390,17 @@ impl IntoAllocated for BlockStmt where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct TryStmt { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct TryStmt { pub block: BlockStmt, pub handler: Option>, pub finalizer: Option>, } -impl IntoAllocated for TryStmt where T: ToString { +impl IntoAllocated for TryStmt +where + T: ToString, +{ type Allocated = TryStmt; fn into_allocated(self) -> Self::Allocated { @@ -407,16 +414,16 @@ impl IntoAllocated for TryStmt where T: ToString { /// The error handling part of a `TryStmt` #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct CatchClause { pub param: Option>, pub body: BlockStmt, } -impl IntoAllocated for CatchClause where T: ToString { +impl IntoAllocated for CatchClause +where + T: ToString, +{ type Allocated = CatchClause; fn into_allocated(self) -> Self::Allocated { @@ -442,15 +449,16 @@ impl IntoAllocated for CatchClause where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct WhileStmt { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct WhileStmt { pub test: Expr, pub body: Box>, } -impl IntoAllocated for WhileStmt where T: ToString { +impl IntoAllocated for WhileStmt +where + T: ToString, +{ type Allocated = WhileStmt; fn into_allocated(self) -> Self::Allocated { @@ -468,21 +476,22 @@ impl IntoAllocated for WhileStmt where T: ToString { /// } while (Math.floor(Math.random() * 100) < 75) /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct DoWhileStmt { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct DoWhileStmt { pub test: Expr, pub body: Box>, } -impl IntoAllocated for DoWhileStmt where T: ToString { +impl IntoAllocated for DoWhileStmt +where + T: ToString, +{ type Allocated = DoWhileStmt; fn into_allocated(self) -> Self::Allocated { DoWhileStmt { test: self.test.into_allocated(), - body: self.body.into_allocated() + body: self.body.into_allocated(), } } } @@ -495,17 +504,18 @@ impl IntoAllocated for DoWhileStmt where T: ToString { /// } /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct ForStmt { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct ForStmt { pub init: Option>, pub test: Option>, pub update: Option>, pub body: Box>, } -impl IntoAllocated for ForStmt where T: ToString { +impl IntoAllocated for ForStmt +where + T: ToString, +{ type Allocated = ForStmt; fn into_allocated(self) -> Self::Allocated { @@ -523,21 +533,23 @@ impl IntoAllocated for ForStmt where T: ToString { /// // vvvvvvvvv /// for (var i = 0;i < 100; i++) #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum LoopInit { Variable(VarKind, Vec>), Expr(Expr), } -impl IntoAllocated for LoopInit where T: ToString { +impl IntoAllocated for LoopInit +where + T: ToString, +{ type Allocated = LoopInit; fn into_allocated(self) -> Self::Allocated { match self { - LoopInit::Variable(k, v) => LoopInit::Variable(k, v.into_iter().map(|v| v.into_allocated()).collect()), + LoopInit::Variable(k, v) => { + LoopInit::Variable(k, v.into_iter().map(|v| v.into_allocated()).collect()) + } LoopInit::Expr(inner) => LoopInit::Expr(inner.into_allocated()), } } @@ -556,16 +568,17 @@ impl IntoAllocated for LoopInit where T: ToString { /// //prints a, b /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct ForInStmt { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct ForInStmt { pub left: LoopLeft, pub right: Expr, pub body: Box>, } -impl IntoAllocated for ForInStmt where T: ToString { +impl IntoAllocated for ForInStmt +where + T: ToString, +{ type Allocated = ForInStmt; fn into_allocated(self) -> Self::Allocated { @@ -586,17 +599,18 @@ impl IntoAllocated for ForInStmt where T: ToString { /// //prints 2, 3, 4, 5, 6 /// ``` #[derive(PartialEq, Debug, Clone)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)]pub struct ForOfStmt { +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +pub struct ForOfStmt { pub left: LoopLeft, pub right: Expr, pub body: Box>, pub is_await: bool, } -impl IntoAllocated for ForOfStmt where T: ToString { +impl IntoAllocated for ForOfStmt +where + T: ToString, +{ type Allocated = ForOfStmt; fn into_allocated(self) -> Self::Allocated { @@ -612,17 +626,17 @@ impl IntoAllocated for ForOfStmt where T: ToString { /// The values on the left hand side of the keyword /// in a for in or for of loop #[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize) -)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum LoopLeft { Expr(Expr), Variable(VarKind, VarDecl), Pat(Pat), } -impl IntoAllocated for LoopLeft where T: ToString { +impl IntoAllocated for LoopLeft +where + T: ToString, +{ type Allocated = LoopLeft; fn into_allocated(self) -> Self::Allocated { From af7550073be44f795b49d7c02bc7b3af879f824f Mon Sep 17 00:00:00 2001 From: Robert Masen Date: Sun, 11 Jun 2023 10:41:48 -0500 Subject: [PATCH 8/8] bump version to 0.6.0-alpha.5 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index fc9ac9d..4f1a51a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "resast" -version = "0.6.0-alpha.4" +version = "0.6.0-alpha.5" authors = ["rfm "] edition = "2021" description = "Rusty-ECMAScript Abstract Syntax Tree"