Don't omit empty values while serializing

This commit is contained in:
강동윤 2019-11-28 17:49:08 +09:00
parent 5532e628b2
commit 95a59338d3
13 changed files with 81 additions and 145 deletions

View File

@ -18,26 +18,26 @@ use swc_common::{ast_node, Span};
pub struct Class {
pub span: Span,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub decorators: Vec<Decorator>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub body: Vec<ClassMember>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub super_class: Option<Box<Expr>>,
#[serde(default)]
pub is_abstract: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub type_params: Option<TsTypeParamDecl>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub super_type_params: Option<TsTypeParamInstantiation>,
/// Typescript extension.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub implements: Vec<TsExprWithTypeArgs>,
}
@ -68,27 +68,23 @@ macro_rules! property {
pub key: $KEY,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub value: Option<Box<Expr>>,
#[serde(
default,
rename = "typeAnnotation",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<TsTypeAnn>,
#[serde(default)]
pub is_static: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub decorators: Vec<Decorator>,
#[serde(default)]
pub computed: bool,
/// Typescript extension.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub accessibility: Option<Accessibility>,
/// Typescript extension.
@ -128,7 +124,7 @@ macro_rules! method {
pub is_static: bool,
/// Typescript extension.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub accessibility: Option<Accessibility>,
/// Typescript extension.
@ -152,10 +148,10 @@ pub struct Constructor {
pub params: Vec<PatOrTsParamProp>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub body: Option<BlockStmt>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub accessibility: Option<Accessibility>,
#[serde(default)]

View File

@ -85,7 +85,7 @@ pub struct VarDeclarator {
pub name: Pat,
/// Initialization expresion.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub init: Option<Box<Expr>>,
/// Typescript only

View File

@ -161,7 +161,7 @@ pub struct ThisExpr {
pub struct ArrayLit {
pub span: Span,
#[serde(default, rename = "elements", skip_serializing_if = "Vec::is_empty")]
#[serde(default, rename = "elements")]
pub elems: Vec<Option<ExprOrSpread>>,
}
@ -170,7 +170,7 @@ pub struct ArrayLit {
pub struct ObjectLit {
pub span: Span,
#[serde(default, rename = "properties", skip_serializing_if = "Vec::is_empty")]
#[serde(default, rename = "properties"]
pub props: Vec<PropOrSpread>,
}
@ -236,8 +236,7 @@ pub struct BinExpr {
pub struct FnExpr {
#[serde(
default,
rename = "identifier",
skip_serializing_if = "Option::is_none"
rename = "identifier"
)]
pub ident: Option<Ident>,
@ -251,8 +250,7 @@ pub struct FnExpr {
pub struct ClassExpr {
#[serde(
default,
rename = "identifier",
skip_serializing_if = "Option::is_none"
rename = "identifier"
)]
pub ident: Option<Ident>,
@ -351,12 +349,11 @@ pub struct ArrowExpr {
#[serde(
default,
rename = "typeParameters",
skip_serializing_if = "Option::is_none"
rename = "typeParameters"
)]
pub type_params: Option<TsTypeParamDecl>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub return_type: Option<TsTypeAnn>,
}
@ -364,7 +361,7 @@ pub struct ArrowExpr {
pub struct YieldExpr {
pub span: Span,
#[serde(default, rename = "argument", skip_serializing_if = "Option::is_none")]
#[serde(default, rename = "argument")]
pub arg: Option<Box<Expr>>,
#[serde(default)]
@ -411,8 +408,7 @@ pub struct TaggedTpl {
#[serde(
default,
rename = "typeParameters",
skip_serializing_if = "Option::is_none"
rename = "typeParameters"
)]
pub type_params: Option<TsTypeParamInstantiation>,
}
@ -452,7 +448,7 @@ pub struct Super {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "fold", derive(Fold))]
pub struct ExprOrSpread {
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub spread: Option<Span>,
pub expr: Box<Expr>,

View File

@ -11,7 +11,7 @@ use swc_common::{ast_node, Span};
pub struct Function {
pub params: Vec<Pat>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub decorators: Vec<Decorator>,
pub span: Span,
@ -27,14 +27,10 @@ pub struct Function {
#[serde(default, rename = "async")]
pub is_async: bool,
#[serde(
default,
rename = "typeParameters",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "typeParameters")]
pub type_params: Option<TsTypeParamDecl>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub return_type: Option<TsTypeAnn>,
}

View File

@ -9,11 +9,7 @@ pub struct Ident {
#[serde(rename = "value")]
#[cfg_attr(feature = "fold", fold(ignore))]
pub sym: JsWord,
#[serde(
default,
rename = "typeAnnotation",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<TsTypeAnn>,
/// TypeScript only. Used in case of an optional parameter.
#[serde(default)]

View File

@ -83,7 +83,7 @@ pub struct JSXOpeningElement {
pub span: Span,
#[serde(default, rename = "attributes", skip_serializing_if = "Vec::is_empty")]
#[serde(default, rename = "attributes")]
pub attrs: Vec<JSXAttrOrSpread>,
#[serde(rename = "selfClosing")]
@ -91,11 +91,7 @@ pub struct JSXOpeningElement {
/// Note: This field's name is differrent from one from babel because it is
/// misleading
#[serde(
default,
rename = "typeArguments",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "typeArguments")]
pub type_args: Option<TsTypeParamInstantiation>,
}
@ -119,7 +115,7 @@ pub struct JSXAttr {
pub span: Span,
pub name: JSXAttrName,
/// Babel uses Expr instead of JSXAttrValue
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub value: Option<Box<Expr>>,
}
@ -190,7 +186,7 @@ pub struct JSXFragment {
pub opening: JSXOpeningFragment,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub children: Vec<JSXElementChild>,
pub closing: JSXClosingFragment,

View File

@ -61,7 +61,7 @@ pub struct Regex {
#[serde(rename = "pattern")]
pub exp: Str,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub flags: Option<RegexFlags>,
}

View File

@ -8,11 +8,7 @@ pub struct Module {
pub body: Vec<ModuleItem>,
#[serde(
default,
rename = "interpreter",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "interpreter")]
pub shebang: Option<JsWord>,
}
@ -22,11 +18,7 @@ pub struct Script {
pub body: Vec<Stmt>,
#[serde(
default,
rename = "interpreter",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "interpreter")]
pub shebang: Option<JsWord>,
}

View File

@ -57,7 +57,7 @@ pub struct ExportDecl {
pub struct ImportDecl {
pub span: Span,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub specifiers: Vec<ImportSpecifier>,
#[serde(rename = "source")]
@ -137,7 +137,7 @@ pub struct ImportSpecific {
pub local: Ident,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub imported: Option<Ident>,
}
@ -173,6 +173,6 @@ pub struct NamedExportSpecifier {
/// `foo` in `export { foo as bar }`
pub orig: Ident,
/// `Some(bar)` in `export { foo as bar }`
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub exported: Option<Ident>,
}

View File

@ -33,11 +33,7 @@ pub struct ArrayPat {
#[serde(rename = "elements")]
pub elems: Vec<Option<Pat>>,
#[serde(
default,
rename = "typeAnnotation",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<TsTypeAnn>,
}
@ -48,11 +44,7 @@ pub struct ObjectPat {
#[serde(rename = "properties")]
pub props: Vec<ObjectPatProp>,
#[serde(
default,
rename = "typeAnnotation",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<TsTypeAnn>,
}
@ -64,11 +56,7 @@ pub struct AssignPat {
pub right: Box<Expr>,
#[serde(
default,
rename = "typeAnnotation",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<TsTypeAnn>,
}
@ -83,11 +71,7 @@ pub struct RestPat {
#[span(hi)]
pub arg: Box<Pat>,
#[serde(
default,
rename = "typeAnnotation",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<TsTypeAnn>,
}
@ -118,6 +102,6 @@ pub struct AssignPatProp {
pub span: Span,
pub key: Ident,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub value: Option<Box<Expr>>,
}

View File

@ -52,7 +52,7 @@ pub struct AssignProp {
pub struct GetterProp {
pub span: Span,
pub key: PropName,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub body: Option<BlockStmt>,
}
#[ast_node("SetterProperty")]
@ -60,7 +60,7 @@ pub struct SetterProp {
pub span: Span,
pub key: PropName,
pub param: Pat,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub body: Option<BlockStmt>,
}
#[ast_node("MethodProperty")]

View File

@ -106,7 +106,7 @@ pub struct WithStmt {
#[ast_node("ReturnStatement")]
pub struct ReturnStmt {
pub span: Span,
#[serde(default, rename = "argument", skip_serializing_if = "Option::is_none")]
#[serde(default, rename = "argument")]
pub arg: Option<Box<Expr>>,
}
@ -120,14 +120,14 @@ pub struct LabeledStmt {
#[ast_node("BreakStatement")]
pub struct BreakStmt {
pub span: Span,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub label: Option<Ident>,
}
#[ast_node("ContinueStatement")]
pub struct ContinueStmt {
pub span: Span,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub label: Option<Ident>,
}
@ -139,7 +139,7 @@ pub struct IfStmt {
#[serde(rename = "consequent")]
pub cons: Box<Stmt>,
#[serde(default, rename = "alternate", skip_serializing_if = "Option::is_none")]
#[serde(default, rename = "alternate")]
pub alt: Option<Box<Stmt>>,
}
@ -163,10 +163,10 @@ pub struct TryStmt {
pub block: BlockStmt,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub handler: Option<CatchClause>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub finalizer: Option<BlockStmt>,
}
@ -188,13 +188,13 @@ pub struct DoWhileStmt {
pub struct ForStmt {
pub span: Span,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub init: Option<VarDeclOrExpr>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub test: Option<Box<Expr>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub update: Option<Box<Expr>>,
pub body: Box<Stmt>,
@ -216,7 +216,7 @@ pub struct ForOfStmt {
/// es2018
///
/// for-await-of statements, e.g., `for await (const x of xs) {`
#[serde(default, rename = "await", skip_serializing_if = "Option::is_none")]
#[serde(default, rename = "await")]
pub await_token: Option<Span>,
pub left: VarDeclOrPat,
pub right: Box<Expr>,
@ -228,7 +228,7 @@ pub struct SwitchCase {
pub span: Span,
/// None for `default:`
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub test: Option<Box<Expr>>,
#[serde(rename = "consequent")]
@ -242,7 +242,7 @@ pub struct CatchClause {
///
/// The param is null if the catch binding is omitted. E.g., try { foo() }
/// catch { bar() }
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub param: Option<Pat>,
pub body: BlockStmt,

View File

@ -36,10 +36,10 @@ pub struct TsTypeParam {
pub span: Span,
pub name: Ident,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub constraint: Option<Box<TsType>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub default: Option<Box<TsType>>,
}
@ -61,10 +61,10 @@ pub struct TsTypeCastExpr {
#[ast_node("TsParameterProperty")]
pub struct TsParamProp {
pub span: Span,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub decorators: Vec<Decorator>,
/// At least one of `accessibility` or `readonly` must be set.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub accessibility: Option<Accessibility>,
pub readonly: bool,
pub param: TsParamPropParam,
@ -141,13 +141,9 @@ pub enum TsTypeElement {
pub struct TsCallSignatureDecl {
pub span: Span,
pub params: Vec<TsFnParam>,
#[serde(
default,
rename = "typeAnnotation",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<TsTypeAnn>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub type_params: Option<TsTypeParamDecl>,
}
@ -155,13 +151,9 @@ pub struct TsCallSignatureDecl {
pub struct TsConstructSignatureDecl {
pub span: Span,
pub params: Vec<TsFnParam>,
#[serde(
default,
rename = "typeAnnotation",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<TsTypeAnn>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub type_params: Option<TsTypeParamDecl>,
}
@ -172,16 +164,12 @@ pub struct TsPropertySignature {
pub key: Box<Expr>,
pub computed: bool,
pub optional: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub init: Option<Box<Expr>>,
pub params: Vec<TsFnParam>,
#[serde(
default,
rename = "typeAnnotation",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<TsTypeAnn>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub type_params: Option<TsTypeParamDecl>,
}
@ -193,20 +181,16 @@ pub struct TsMethodSignature {
pub computed: bool,
pub optional: bool,
pub params: Vec<TsFnParam>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub type_ann: Option<TsTypeAnn>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub type_params: Option<TsTypeParamDecl>,
}
#[ast_node("TsIndexSignature")]
pub struct TsIndexSignature {
pub params: Vec<TsFnParam>,
#[serde(
default,
rename = "typeAnnotation",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<TsTypeAnn>,
pub readonly: bool,
@ -382,7 +366,7 @@ pub struct TsFnType {
pub span: Span,
pub params: Vec<TsFnParam>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub type_params: Option<TsTypeParamDecl>,
#[serde(rename = "typeAnnotation")]
pub type_ann: TsTypeAnn,
@ -392,7 +376,7 @@ pub struct TsFnType {
pub struct TsConstructorType {
pub span: Span,
pub params: Vec<TsFnParam>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub type_params: Option<TsTypeParamDecl>,
#[serde(rename = "typeAnnotation")]
pub type_ann: TsTypeAnn,
@ -402,7 +386,7 @@ pub struct TsConstructorType {
pub struct TsTypeRef {
pub span: Span,
pub type_name: TsEntityName,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub type_params: Option<TsTypeParamInstantiation>,
}
@ -599,16 +583,12 @@ impl<'de> Deserialize<'de> for TruePlusMinus {
#[ast_node("TsMappedType")]
pub struct TsMappedType {
pub span: Span,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub readonly: Option<TruePlusMinus>,
pub type_param: TsTypeParam,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub optional: Option<TruePlusMinus>,
#[serde(
default,
rename = "typeAnnotation",
skip_serializing_if = "Option::is_none"
)]
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<Box<TsType>>,
}
@ -640,7 +620,7 @@ pub struct TsInterfaceDecl {
pub span: Span,
pub id: Ident,
pub declare: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub type_params: Option<TsTypeParamDecl>,
pub extends: Vec<TsExprWithTypeArgs>,
pub body: TsInterfaceBody,
@ -657,7 +637,7 @@ pub struct TsExprWithTypeArgs {
pub span: Span,
#[serde(rename = "expression")]
pub expr: TsEntityName,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub type_params: Option<TsTypeParamInstantiation>,
}
@ -666,7 +646,7 @@ pub struct TsTypeAliasDecl {
pub span: Span,
pub declare: bool,
pub id: Ident,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub type_params: Option<TsTypeParamDecl>,
#[serde(rename = "typeAnnotation")]
pub type_ann: Box<TsType>,
@ -685,7 +665,7 @@ pub struct TsEnumDecl {
pub struct TsEnumMember {
pub span: Span,
pub id: TsEnumMemberId,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub init: Option<Box<Expr>>,
}
@ -707,7 +687,7 @@ pub struct TsModuleDecl {
/// In TypeScript, this is only available through`node.flags`.
pub global: bool,
pub id: TsModuleName,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub body: Option<TsNamespaceBody>,
}