From 56d12bc6f6c46f01ca9f49a0f3a80a759e64c8ac Mon Sep 17 00:00:00 2001 From: Robert Masen Date: Sun, 11 Jun 2023 10:32:50 -0500 Subject: [PATCH] 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 {