Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions crates/ast_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,29 +158,34 @@ pub fn ast_node(
let mut item = TokenStream::new();
match input.data {
Data::Enum(..) => {
struct EnumArgs {
clone: bool,
}
impl parse::Parse for EnumArgs {
fn parse(i: parse::ParseStream<'_>) -> syn::Result<Self> {
let name: Ident = i.parse()?;
if name != "no_clone" {
return Err(i.error("unknown attribute"));
}
Ok(EnumArgs { clone: false })
use syn::parse::Parser;

let attrs = <syn::punctuated::Punctuated<syn::Ident, syn::Token![,]>>::parse_terminated
.parse(args)
.expect("failed to parse #[ast_node]");

let mut has_no_clone = false;
let mut has_no_unknown = false;
for attr in &attrs {
if attr == "no_clone" {
has_no_clone = true;
} else if attr == "no_unknown" {
has_no_unknown = true;
} else {
panic!("unknown attribute: {attr:?}")
}
}
let args = if args.is_empty() {
EnumArgs { clone: true }
} else {
parse(args).expect("failed to parse args of #[ast_node]")
};

let clone = if args.clone {
let clone = if !has_no_clone {
Some(quote!(#[derive(Clone)]))
} else {
None
};
let non_exhaustive = if !has_no_unknown {
Some(quote!(#[cfg_attr(feature = "unknown", non_exhaustive)]))
} else {
None
};

item.extend(quote!(
#[allow(clippy::derive_partial_eq_without_eq)]
Expand All @@ -198,6 +203,7 @@ pub fn ast_node(
::swc_common::DeserializeEnum,
)]
#clone
#non_exhaustive
#[cfg_attr(
feature = "rkyv-impl",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
Expand Down
58 changes: 29 additions & 29 deletions crates/swc_css_ast/src/at_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct AtRule {
pub block: Option<SimpleBlock>,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum AtRuleName {
#[tag("DashedIdent")]
Expand Down Expand Up @@ -46,7 +46,7 @@ impl PartialEq<Atom> for AtRuleName {
}
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum AtRulePrelude {
#[tag("ListOfComponentValues")]
Expand Down Expand Up @@ -99,7 +99,7 @@ pub struct ScopeRange {
pub scope_end: Option<ForgivingSelectorList>,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum ColorProfileName {
#[tag("DashedIdent")]
Expand All @@ -122,7 +122,7 @@ pub struct FontFeatureValuesPrelude {
pub font_family: Vec<FamilyName>,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum DocumentPreludeMatchingFunction {
#[tag("Url")]
Expand All @@ -131,7 +131,7 @@ pub enum DocumentPreludeMatchingFunction {
Function(Function),
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum KeyframesName {
#[tag("CustomIdent")]
Expand Down Expand Up @@ -170,7 +170,7 @@ pub struct KeyframeBlock {
pub block: SimpleBlock,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum KeyframeSelector {
#[tag("Ident")]
Expand All @@ -188,7 +188,7 @@ pub struct ImportPrelude {
pub import_conditions: Option<Box<ImportConditions>>,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum ImportHref {
#[tag("Url")]
Expand All @@ -197,7 +197,7 @@ pub enum ImportHref {
Str(Str),
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum ImportLayerName {
#[tag("Ident")]
Expand All @@ -222,7 +222,7 @@ pub struct NamespacePrelude {
pub uri: Box<NamespacePreludeUri>,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum NamespacePreludeUri {
#[tag("Url")]
Expand Down Expand Up @@ -269,14 +269,14 @@ impl EqIgnoreSpan for MediaQuery {
}
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum MediaType {
#[tag("Ident")]
Ident(Ident),
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum MediaConditionType {
#[tag("MediaCondition")]
Expand All @@ -300,7 +300,7 @@ pub struct MediaConditionWithoutOr {
pub conditions: Vec<MediaConditionWithoutOrType>,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum MediaConditionAllType {
#[tag("MediaNot")]
Expand All @@ -316,7 +316,7 @@ pub enum MediaConditionAllType {
MediaInParens(MediaInParens),
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum MediaConditionWithoutOrType {
#[tag("MediaNot")]
Expand Down Expand Up @@ -371,7 +371,7 @@ impl EqIgnoreSpan for MediaOr {
}
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum MediaInParens {
#[tag("MediaCondition")]
Expand All @@ -384,7 +384,7 @@ pub enum MediaInParens {
GeneralEnclosed(GeneralEnclosed),
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum MediaFeature {
#[tag("MediaFeaturePlain")]
Expand All @@ -400,7 +400,7 @@ pub enum MediaFeature {
RangeInterval(MediaFeatureRangeInterval),
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum MediaFeatureName {
#[tag("Ident")]
Expand All @@ -410,7 +410,7 @@ pub enum MediaFeatureName {
ExtensionName(ExtensionName),
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum MediaFeatureValue {
#[tag("Number")]
Expand Down Expand Up @@ -502,7 +502,7 @@ pub struct SupportsCondition {
pub conditions: Vec<SupportsConditionType>,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum SupportsConditionType {
#[tag("SupportsNot")]
Expand Down Expand Up @@ -560,7 +560,7 @@ impl EqIgnoreSpan for SupportsOr {
}
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum SupportsInParens {
#[tag("SupportsCondition")]
Expand All @@ -573,7 +573,7 @@ pub enum SupportsInParens {
GeneralEnclosed(GeneralEnclosed),
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum SupportsFeature {
#[tag("Declaration")]
Expand All @@ -582,7 +582,7 @@ pub enum SupportsFeature {
Function(Function),
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum GeneralEnclosed {
#[tag("Function")]
Expand Down Expand Up @@ -620,7 +620,7 @@ pub struct PageSelectorPseudo {
pub value: Ident,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum LayerPrelude {
#[tag("LayerName")]
Expand Down Expand Up @@ -651,7 +651,7 @@ pub struct ContainerCondition {
pub query: ContainerQuery,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum ContainerName {
#[tag("CustomIdent")]
Expand All @@ -665,7 +665,7 @@ pub struct ContainerQuery {
pub queries: Vec<ContainerQueryType>,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum ContainerQueryType {
#[tag("ContainerQueryNot")]
Expand Down Expand Up @@ -723,7 +723,7 @@ impl EqIgnoreSpan for ContainerQueryOr {
}
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum QueryInParens {
#[tag("ContainerQuery")]
Expand All @@ -740,7 +740,7 @@ pub enum QueryInParens {
GeneralEnclosed(GeneralEnclosed),
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum SizeFeature {
#[tag("SizeFeaturePlain")]
Expand Down Expand Up @@ -822,7 +822,7 @@ pub struct SizeFeatureRangeInterval {
pub right: Box<SizeFeatureValue>,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum SizeFeatureValue {
#[tag("Number")]
Expand All @@ -841,7 +841,7 @@ pub enum SizeFeatureValue {
Function(Function),
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum SizeFeatureName {
#[tag("Ident")]
Expand Down Expand Up @@ -893,7 +893,7 @@ impl Take for CustomMediaQuery {
}
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum CustomMediaQueryMediaType {
#[tag("Ident")]
Expand Down
14 changes: 7 additions & 7 deletions crates/swc_css_ast/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Stylesheet {
pub rules: Vec<Rule>,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum Rule {
#[tag("QualifiedRule")]
Expand Down Expand Up @@ -53,7 +53,7 @@ impl Take for QualifiedRule {
}
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum QualifiedRulePrelude {
#[tag("SelectorList")]
Expand All @@ -70,7 +70,7 @@ impl Take for QualifiedRulePrelude {
}
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum StyleBlock {
#[tag("AtRule")]
Expand Down Expand Up @@ -101,7 +101,7 @@ impl Take for SimpleBlock {
}
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum FunctionName {
#[tag("Ident")]
Expand Down Expand Up @@ -162,7 +162,7 @@ pub struct ListOfComponentValues {
pub children: Vec<ComponentValue>,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum ComponentValue {
// No grammar
Expand Down Expand Up @@ -284,7 +284,7 @@ impl From<Rule> for ComponentValue {
}
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum DeclarationOrAtRule {
#[tag("Declaration")]
Expand All @@ -306,7 +306,7 @@ pub struct Declaration {
pub important: Option<ImportantFlag>,
}

#[ast_node]
#[ast_node(no_unknown)]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum DeclarationName {
#[tag("Ident")]
Expand Down
Loading
Loading