swc_core::common

Attribute Macro ast_serde

source
#[ast_serde]
Available on crate feature __common only.
Expand description

Derives serde::Serialize and serde::Deserialize.

§Struct attributes

#[ast_serde("A")] adds "type": "A" to json when serialized, and deserializes as the type only if type field of json string is A.

§Enum attributes

§Type-level attributes

This macro does not accept arguments if used on enum.

§Variant attributes

§#[tag("Expr")]

You can tell “Use this variant if type is Expr”.

This attribute can be applied multiple time, if a variant consumes multiple types.

For example, Lit of swc_ecma_ast is an enum, but Expr, which contains Lit as a variant, is also an enum. So the Lit variant has multiple #[tag]-s like

enum Expr {
  #[tag("StringLiteral")]
  #[tag("NumericLiteral")]
  #[tag("BooleanLiteral")]
  Lit(Lit),
}

so the deserializer can decide which variant to use.

#[tag] also supports wildcard like #[tag("*")]. You can use this if there are two many variants.