Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(css/ast): Make AST intuitive #6606

Merged
merged 39 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
60 changes: 53 additions & 7 deletions crates/swc_css_ast/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ pub enum ComponentValue {
#[tag("SimpleBlock")]
SimpleBlock(Box<SimpleBlock>),

// Block Contents grammar
#[tag("DeclarationOrAtRule")]
DeclarationOrAtRule(Box<DeclarationOrAtRule>),
#[tag("Rule")]
Rule(Box<Rule>),
#[tag("StyleBlock")]
StyleBlock(Box<StyleBlock>),
#[tag("AtRule")]
AtRule(Box<AtRule>),

#[tag("QualifiedRule")]
QualifiedRule(Box<QualifiedRule>),

#[tag("ListOfComponentValues")]
ListOfComponentValues(Box<ListOfComponentValues>),

#[tag("KeyframeBlock")]
KeyframeBlock(Box<KeyframeBlock>),

Expand Down Expand Up @@ -181,6 +183,50 @@ pub enum ComponentValue {
Declaration(Box<Declaration>),
}

impl From<StyleBlock> for ComponentValue {
#[inline]
fn from(block: StyleBlock) -> Self {
match block {
StyleBlock::AtRule(at_rule) => ComponentValue::AtRule(at_rule),
StyleBlock::Declaration(declaration) => ComponentValue::Declaration(declaration),
StyleBlock::QualifiedRule(qualified_rule) => {
ComponentValue::QualifiedRule(qualified_rule)
}
StyleBlock::ListOfComponentValues(list_of_component_values) => {
ComponentValue::ListOfComponentValues(list_of_component_values)
}
}
}
}

impl From<DeclarationOrAtRule> for ComponentValue {
#[inline]
fn from(rule: DeclarationOrAtRule) -> Self {
match rule {
DeclarationOrAtRule::Declaration(declaration) => {
ComponentValue::Declaration(declaration)
}
DeclarationOrAtRule::AtRule(at_rule) => ComponentValue::AtRule(at_rule),
DeclarationOrAtRule::ListOfComponentValues(list_of_component_values) => {
ComponentValue::ListOfComponentValues(list_of_component_values)
}
}
}
}

impl From<Rule> for ComponentValue {
#[inline]
fn from(rule: Rule) -> Self {
match rule {
Rule::AtRule(at_rule) => ComponentValue::AtRule(at_rule),
Rule::QualifiedRule(qualified_rule) => ComponentValue::QualifiedRule(qualified_rule),
Rule::ListOfComponentValues(list_of_component_values) => {
ComponentValue::ListOfComponentValues(list_of_component_values)
}
}
}
}

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum DeclarationOrAtRule {
Expand Down
80 changes: 33 additions & 47 deletions crates/swc_css_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,79 +1327,65 @@ where

for (idx, node) in n.value.iter().enumerate() {
match node {
ComponentValue::StyleBlock(_) => {
ComponentValue::ListOfComponentValues(_) | ComponentValue::Declaration(_) => {
if idx == 0 {
formatting_newline!(self);
}

increase_indent!(self);
}
ComponentValue::Rule(_) | ComponentValue::KeyframeBlock(_) => {
ComponentValue::AtRule(_)
| ComponentValue::QualifiedRule(_)
| ComponentValue::KeyframeBlock(_) => {
formatting_newline!(self);
increase_indent!(self);
}
ComponentValue::DeclarationOrAtRule(_) => {
if idx == 0 {
formatting_newline!(self);
}

increase_indent!(self);
}
_ => {}
}

emit!(self, node);
match node {
ComponentValue::ListOfComponentValues(node) => {
emit!(
&mut *self.with_ctx(Ctx {
in_list_of_component_values: true,
..self.ctx
}),
node
);
}
_ => {
emit!(self, node);
}
}

match node {
ComponentValue::Rule(_) => {
ComponentValue::AtRule(_) | ComponentValue::QualifiedRule(_) => {
formatting_newline!(self);
decrease_indent!(self);
}
ComponentValue::StyleBlock(node) => {
match &**node {
StyleBlock::AtRule(_) | StyleBlock::QualifiedRule(_) => {
formatting_newline!(self);
}
StyleBlock::Declaration(_) => {
if idx != len - 1 {
semi!(self);
} else {
formatting_semi!(self);
}

formatting_newline!(self);
}
StyleBlock::ListOfComponentValues(_) => {}
ComponentValue::Declaration(_) => {
if idx != len - 1 {
semi!(self);
} else {
formatting_semi!(self);
}

formatting_newline!(self);
decrease_indent!(self);
}
ComponentValue::ListOfComponentValues(_) => {
decrease_indent!(self);
}

ComponentValue::KeyframeBlock(_) => {
if idx == len - 1 {
formatting_newline!(self);
}

decrease_indent!(self);
}
ComponentValue::DeclarationOrAtRule(node) => {
match &**node {
DeclarationOrAtRule::AtRule(_) => {
formatting_newline!(self);
}
DeclarationOrAtRule::Declaration(_) => {
if idx != len - 1 {
semi!(self);
} else {
formatting_semi!(self);
}

formatting_newline!(self);
}
DeclarationOrAtRule::ListOfComponentValues(_) => {}
}

decrease_indent!(self);
}
_ => {
if !self.ctx.in_list_of_component_values && ending == "]" && idx != len - 1 {
space!(self);
Expand All @@ -1418,9 +1404,9 @@ where
ComponentValue::Function(n) => emit!(self, n),
ComponentValue::SimpleBlock(n) => emit!(self, n),

ComponentValue::StyleBlock(n) => emit!(self, n),
ComponentValue::DeclarationOrAtRule(n) => emit!(self, n),
ComponentValue::Rule(n) => emit!(self, n),
ComponentValue::ListOfComponentValues(n) => emit!(self, n),
ComponentValue::QualifiedRule(n) => emit!(self, n),
ComponentValue::AtRule(n) => emit!(self, n),
ComponentValue::KeyframeBlock(n) => emit!(self, n),

ComponentValue::Ident(n) => emit!(self, n),
Expand Down Expand Up @@ -1669,7 +1655,7 @@ where
}

#[emitter]
fn emit_frequency_cercentage(&mut self, n: &FrequencyPercentage) -> Result {
fn emit_frequency_percentage(&mut self, n: &FrequencyPercentage) -> Result {
match n {
FrequencyPercentage::Frequency(n) => emit!(self, n),
FrequencyPercentage::Percentage(n) => emit!(self, n),
Expand Down
29 changes: 7 additions & 22 deletions crates/swc_css_compat/src/nesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,14 @@ impl NestingHandler {

for value in rule.block.value.take() {
match value {
ComponentValue::StyleBlock(box StyleBlock::QualifiedRule(mut nested)) => {
ComponentValue::QualifiedRule(mut nested) => {
self.process_prelude(&rule.prelude, &mut nested.prelude);

nested_rules.push(Rule::QualifiedRule(nested));

continue;
}
ComponentValue::StyleBlock(box StyleBlock::AtRule(ref at_rule)) => {
ComponentValue::AtRule(ref at_rule) => {
if let Some(
AtRulePrelude::MediaPrelude(..)
| AtRulePrelude::SupportsPrelude(..)
Expand All @@ -252,9 +252,7 @@ impl NestingHandler {

for n in &block.value {
match n {
ComponentValue::StyleBlock(box StyleBlock::QualifiedRule(
n,
)) => {
ComponentValue::QualifiedRule(n) => {
let mut q = n.clone();

self.process_prelude(&rule.prelude, &mut q.prelude);
Expand Down Expand Up @@ -284,12 +282,7 @@ impl NestingHandler {
},
});

nested_of_media.insert(
0,
ComponentValue::StyleBlock(Box::new(
StyleBlock::QualifiedRule(rule),
)),
);
nested_of_media.insert(0, ComponentValue::QualifiedRule(rule));
}

nested_rules.push(Rule::AtRule(Box::new(AtRule {
Expand Down Expand Up @@ -344,14 +337,12 @@ impl VisitMut for NestingHandler {

for n in n.take() {
match n {
ComponentValue::StyleBlock(box StyleBlock::QualifiedRule(mut n)) => {
ComponentValue::QualifiedRule(mut n) => {
let mut rules = self.extract_nested_rules(&mut n);

rules.visit_mut_with(self);

new.push(ComponentValue::StyleBlock(Box::new(
StyleBlock::QualifiedRule(n),
)));
new.push(ComponentValue::QualifiedRule(n));
new.extend(rules.into_iter().map(rule_to_component_value));
}

Expand All @@ -366,11 +357,5 @@ impl VisitMut for NestingHandler {
}

fn rule_to_component_value(rule: Rule) -> ComponentValue {
match rule {
Rule::QualifiedRule(q) => {
ComponentValue::StyleBlock(Box::new(StyleBlock::QualifiedRule(q)))
}
Rule::AtRule(r) => ComponentValue::StyleBlock(Box::new(StyleBlock::AtRule(r))),
Rule::ListOfComponentValues(..) => ComponentValue::Rule(Box::new(rule)),
}
rule.into()
}