Skip to content

Commit

Permalink
feat(css/ast): Add types for An+b syntax (#2759)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Nov 16, 2021
1 parent 2e01876 commit 6ce437d
Show file tree
Hide file tree
Showing 64 changed files with 12,988 additions and 416 deletions.
39 changes: 38 additions & 1 deletion crates/swc_css_ast/src/selector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{Ident, Str, Tokens};
use is_macro::Is;
use string_enum::StringEnum;
use swc_atoms::JsWord;
use swc_common::{ast_node, EqIgnoreSpan, Span};

#[ast_node("SelectorList")]
Expand Down Expand Up @@ -130,12 +131,48 @@ pub struct AttrSelector {
pub modifier: Option<char>,
}

#[ast_node]
#[derive(Is)]
pub enum PseudoSelectorChildren {
#[tag("Nth")]
Nth(Nth),

#[tag("Tokens")]
Tokens(Tokens),
}

#[ast_node("Nth")]
pub struct Nth {
pub span: Span,
pub nth: NthValue,
pub selector_list: Option<SelectorList>,
}

#[ast_node("AnPlusB")]
pub struct AnPlusB {
pub span: Span,
pub a: Option<i32>,
pub a_raw: Option<JsWord>,
pub b: Option<i32>,
pub b_raw: Option<JsWord>,
}

#[ast_node]
#[derive(Is)]
pub enum NthValue {
#[tag("AnPlusB")]
AnPlusB(AnPlusB),

#[tag("Ident")]
Ident(Ident),
}

#[ast_node("PseudoSelector")]
pub struct PseudoSelector {
pub span: Span,
pub is_element: bool,
pub name: Ident,
pub args: Tokens,
pub children: Option<PseudoSelectorChildren>,
}

#[ast_node("IdSelector")]
Expand Down
42 changes: 40 additions & 2 deletions crates/swc_css_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,18 +788,56 @@ where
punct!(self, "]");
}

#[emitter]
fn emit_nth(&mut self, n: &Nth) -> Result {
emit!(self, n.nth);

if n.selector_list.is_some() {
emit!(self, n.selector_list);
}
}

#[emitter]
fn emit_an_plus_b(&mut self, n: &AnPlusB) -> Result {
if let Some(a_raw) = &n.a_raw {
self.wr.write_raw(Some(n.span), a_raw)?;
punct!(self, "n");
}

if let Some(b_raw) = &n.b_raw {
self.wr.write_raw(Some(n.span), b_raw)?;
}
}

#[emitter]
fn emit_nth_value(&mut self, n: &NthValue) -> Result {
match n {
NthValue::AnPlusB(n) => emit!(self, n),
NthValue::Ident(n) => emit!(self, n),
}
}

#[emitter]
fn emit_pseudo_selector_children(&mut self, n: &PseudoSelectorChildren) -> Result {
match n {
PseudoSelectorChildren::Nth(n) => emit!(self, n),
PseudoSelectorChildren::Tokens(n) => emit!(self, n),
}
}

#[emitter]
fn emit_pseudo_selector(&mut self, n: &PseudoSelector) -> Result {
punct!(self, ":");

if n.is_element {
punct!(self, ":");
}

emit!(self, n.name);

if !n.args.tokens.is_empty() {
if n.children.is_some() {
punct!(self, "(");
emit!(self, n.args);
emit!(self, n.children);
punct!(self, ")");
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_css_parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl Error {
ErrorKind::InvalidMediaQuery => "Invalid media query".into(),
ErrorKind::UnknownAtRuleNotTerminated => "Unknown @rule is not terminated".into(),
ErrorKind::InvalidDeclarationValue => "Expected a property value".into(),
ErrorKind::InvalidAnPlusBMicrosyntax => "Invalid An+B microsyntax".into(),
};
handler.struct_span_err(self.inner.0, &msg)
}
Expand Down Expand Up @@ -80,6 +81,7 @@ pub enum ErrorKind {
InvalidSupportQuery,
InvalidKeyframeSelector,
InvalidMediaQuery,
InvalidAnPlusBMicrosyntax,

UnknownAtRuleNotTerminated,
}
Loading

1 comment on commit 6ce437d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 6ce437d Previous: 2e01876 Ratio
base_tr_fixer 35378 ns/iter (± 5383) 23584 ns/iter (± 241) 1.50
base_tr_resolver_and_hygiene 194961 ns/iter (± 27788) 129925 ns/iter (± 25887) 1.50
codegen_es2015 68396 ns/iter (± 6819) 52025 ns/iter (± 318) 1.31
codegen_es2016 68338 ns/iter (± 7033) 51768 ns/iter (± 508) 1.32
codegen_es2017 69216 ns/iter (± 7510) 51947 ns/iter (± 293) 1.33
codegen_es2018 69694 ns/iter (± 6656) 51743 ns/iter (± 426) 1.35
codegen_es2019 67891 ns/iter (± 5842) 51895 ns/iter (± 371) 1.31
codegen_es2020 69289 ns/iter (± 5179) 51865 ns/iter (± 318) 1.34
codegen_es3 69361 ns/iter (± 10621) 51870 ns/iter (± 474) 1.34
codegen_es5 69015 ns/iter (± 15115) 51182 ns/iter (± 300) 1.35
full_es2015 235518605 ns/iter (± 17310459) 185347500 ns/iter (± 11184579) 1.27
full_es2016 184979341 ns/iter (± 16284344) 142319920 ns/iter (± 9839120) 1.30
full_es2017 193977540 ns/iter (± 14590208) 155265742 ns/iter (± 10729736) 1.25
full_es2018 195894555 ns/iter (± 13809066) 155028002 ns/iter (± 6598055) 1.26
full_es2019 186785963 ns/iter (± 14867645) 149853142 ns/iter (± 6227741) 1.25
full_es2020 190738579 ns/iter (± 17098400) 147956685 ns/iter (± 5900431) 1.29
full_es3 284791411 ns/iter (± 31959208) 214530692 ns/iter (± 24802045) 1.33
full_es5 259355180 ns/iter (± 29886583) 184134465 ns/iter (± 28606824) 1.41
parser 889097 ns/iter (± 117341) 589322 ns/iter (± 15902) 1.51
ser_ast_node 197 ns/iter (± 14) 127 ns/iter (± 5) 1.55
ser_serde 206 ns/iter (± 18) 140 ns/iter (± 2) 1.47
emit_colors 14517344 ns/iter (± 20061908) 17141108 ns/iter (± 23816472) 0.85
emit_large 83824398 ns/iter (± 109478499) 105422541 ns/iter (± 161410161) 0.80
base_clone 2970085 ns/iter (± 340892) 2232375 ns/iter (± 204711) 1.33
fold_span 5259444 ns/iter (± 997480) 3765301 ns/iter (± 460965) 1.40
fold_span_panic 5422115 ns/iter (± 660872) 3999169 ns/iter (± 351638) 1.36
visit_mut_span 3546297 ns/iter (± 579227) 2731112 ns/iter (± 194655) 1.30
visit_mut_span_panic 3619833 ns/iter (± 503362) 2783070 ns/iter (± 254466) 1.30
ast_clone 24238 ns/iter (± 1825) 16624 ns/iter (± 148) 1.46
ast_clone_to_stable 79513 ns/iter (± 10026) 51351 ns/iter (± 471) 1.55
ast_clone_to_stable_then_to_unstable 141641 ns/iter (± 11793) 92351 ns/iter (± 669) 1.53
json_deserialize 2828142 ns/iter (± 372694) 1926511 ns/iter (± 125771) 1.47
json_serialize 119158 ns/iter (± 12145) 92776 ns/iter (± 281) 1.28
boxing_boxed 188 ns/iter (± 16) 126 ns/iter (± 1) 1.49
boxing_boxed_clone 83 ns/iter (± 9) 59 ns/iter (± 1) 1.41
boxing_unboxed 200 ns/iter (± 20) 120 ns/iter (± 0) 1.67
boxing_unboxed_clone 84 ns/iter (± 7) 56 ns/iter (± 1) 1.50

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.