Skip to content

Commit b4eef53

Browse files
authored
feat(css/ast): Make AST intuitive (#6606)
1 parent fef8fc9 commit b4eef53

File tree

366 files changed

+193
-13175
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

366 files changed

+193
-13175
lines changed

crates/swc_css_ast/src/base.rs

+53-7
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,15 @@ pub enum ComponentValue {
126126
#[tag("SimpleBlock")]
127127
SimpleBlock(Box<SimpleBlock>),
128128

129-
// Block Contents grammar
130-
#[tag("DeclarationOrAtRule")]
131-
DeclarationOrAtRule(Box<DeclarationOrAtRule>),
132-
#[tag("Rule")]
133-
Rule(Box<Rule>),
134-
#[tag("StyleBlock")]
135-
StyleBlock(Box<StyleBlock>),
129+
#[tag("AtRule")]
130+
AtRule(Box<AtRule>),
131+
132+
#[tag("QualifiedRule")]
133+
QualifiedRule(Box<QualifiedRule>),
134+
135+
#[tag("ListOfComponentValues")]
136+
ListOfComponentValues(Box<ListOfComponentValues>),
137+
136138
#[tag("KeyframeBlock")]
137139
KeyframeBlock(Box<KeyframeBlock>),
138140

@@ -181,6 +183,50 @@ pub enum ComponentValue {
181183
Declaration(Box<Declaration>),
182184
}
183185

186+
impl From<StyleBlock> for ComponentValue {
187+
#[inline]
188+
fn from(block: StyleBlock) -> Self {
189+
match block {
190+
StyleBlock::AtRule(at_rule) => ComponentValue::AtRule(at_rule),
191+
StyleBlock::Declaration(declaration) => ComponentValue::Declaration(declaration),
192+
StyleBlock::QualifiedRule(qualified_rule) => {
193+
ComponentValue::QualifiedRule(qualified_rule)
194+
}
195+
StyleBlock::ListOfComponentValues(list_of_component_values) => {
196+
ComponentValue::ListOfComponentValues(list_of_component_values)
197+
}
198+
}
199+
}
200+
}
201+
202+
impl From<DeclarationOrAtRule> for ComponentValue {
203+
#[inline]
204+
fn from(rule: DeclarationOrAtRule) -> Self {
205+
match rule {
206+
DeclarationOrAtRule::Declaration(declaration) => {
207+
ComponentValue::Declaration(declaration)
208+
}
209+
DeclarationOrAtRule::AtRule(at_rule) => ComponentValue::AtRule(at_rule),
210+
DeclarationOrAtRule::ListOfComponentValues(list_of_component_values) => {
211+
ComponentValue::ListOfComponentValues(list_of_component_values)
212+
}
213+
}
214+
}
215+
}
216+
217+
impl From<Rule> for ComponentValue {
218+
#[inline]
219+
fn from(rule: Rule) -> Self {
220+
match rule {
221+
Rule::AtRule(at_rule) => ComponentValue::AtRule(at_rule),
222+
Rule::QualifiedRule(qualified_rule) => ComponentValue::QualifiedRule(qualified_rule),
223+
Rule::ListOfComponentValues(list_of_component_values) => {
224+
ComponentValue::ListOfComponentValues(list_of_component_values)
225+
}
226+
}
227+
}
228+
}
229+
184230
#[ast_node]
185231
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
186232
pub enum DeclarationOrAtRule {

crates/swc_css_codegen/src/lib.rs

+33-47
Original file line numberDiff line numberDiff line change
@@ -1327,79 +1327,65 @@ where
13271327

13281328
for (idx, node) in n.value.iter().enumerate() {
13291329
match node {
1330-
ComponentValue::StyleBlock(_) => {
1330+
ComponentValue::ListOfComponentValues(_) | ComponentValue::Declaration(_) => {
13311331
if idx == 0 {
13321332
formatting_newline!(self);
13331333
}
13341334

13351335
increase_indent!(self);
13361336
}
1337-
ComponentValue::Rule(_) | ComponentValue::KeyframeBlock(_) => {
1337+
ComponentValue::AtRule(_)
1338+
| ComponentValue::QualifiedRule(_)
1339+
| ComponentValue::KeyframeBlock(_) => {
13381340
formatting_newline!(self);
13391341
increase_indent!(self);
13401342
}
1341-
ComponentValue::DeclarationOrAtRule(_) => {
1342-
if idx == 0 {
1343-
formatting_newline!(self);
1344-
}
13451343

1346-
increase_indent!(self);
1347-
}
13481344
_ => {}
13491345
}
13501346

1351-
emit!(self, node);
1347+
match node {
1348+
ComponentValue::ListOfComponentValues(node) => {
1349+
emit!(
1350+
&mut *self.with_ctx(Ctx {
1351+
in_list_of_component_values: true,
1352+
..self.ctx
1353+
}),
1354+
node
1355+
);
1356+
}
1357+
_ => {
1358+
emit!(self, node);
1359+
}
1360+
}
13521361

13531362
match node {
1354-
ComponentValue::Rule(_) => {
1363+
ComponentValue::AtRule(_) | ComponentValue::QualifiedRule(_) => {
13551364
formatting_newline!(self);
13561365
decrease_indent!(self);
13571366
}
1358-
ComponentValue::StyleBlock(node) => {
1359-
match &**node {
1360-
StyleBlock::AtRule(_) | StyleBlock::QualifiedRule(_) => {
1361-
formatting_newline!(self);
1362-
}
1363-
StyleBlock::Declaration(_) => {
1364-
if idx != len - 1 {
1365-
semi!(self);
1366-
} else {
1367-
formatting_semi!(self);
1368-
}
1369-
1370-
formatting_newline!(self);
1371-
}
1372-
StyleBlock::ListOfComponentValues(_) => {}
1367+
ComponentValue::Declaration(_) => {
1368+
if idx != len - 1 {
1369+
semi!(self);
1370+
} else {
1371+
formatting_semi!(self);
13731372
}
13741373

1374+
formatting_newline!(self);
1375+
decrease_indent!(self);
1376+
}
1377+
ComponentValue::ListOfComponentValues(_) => {
13751378
decrease_indent!(self);
13761379
}
1380+
13771381
ComponentValue::KeyframeBlock(_) => {
13781382
if idx == len - 1 {
13791383
formatting_newline!(self);
13801384
}
13811385

13821386
decrease_indent!(self);
13831387
}
1384-
ComponentValue::DeclarationOrAtRule(node) => {
1385-
match &**node {
1386-
DeclarationOrAtRule::AtRule(_) => {
1387-
formatting_newline!(self);
1388-
}
1389-
DeclarationOrAtRule::Declaration(_) => {
1390-
if idx != len - 1 {
1391-
semi!(self);
1392-
} else {
1393-
formatting_semi!(self);
1394-
}
1395-
1396-
formatting_newline!(self);
1397-
}
1398-
DeclarationOrAtRule::ListOfComponentValues(_) => {}
1399-
}
14001388

1401-
decrease_indent!(self);
1402-
}
14031389
_ => {
14041390
if !self.ctx.in_list_of_component_values && ending == "]" && idx != len - 1 {
14051391
space!(self);
@@ -1418,9 +1404,9 @@ where
14181404
ComponentValue::Function(n) => emit!(self, n),
14191405
ComponentValue::SimpleBlock(n) => emit!(self, n),
14201406

1421-
ComponentValue::StyleBlock(n) => emit!(self, n),
1422-
ComponentValue::DeclarationOrAtRule(n) => emit!(self, n),
1423-
ComponentValue::Rule(n) => emit!(self, n),
1407+
ComponentValue::ListOfComponentValues(n) => emit!(self, n),
1408+
ComponentValue::QualifiedRule(n) => emit!(self, n),
1409+
ComponentValue::AtRule(n) => emit!(self, n),
14241410
ComponentValue::KeyframeBlock(n) => emit!(self, n),
14251411

14261412
ComponentValue::Ident(n) => emit!(self, n),
@@ -1669,7 +1655,7 @@ where
16691655
}
16701656

16711657
#[emitter]
1672-
fn emit_frequency_cercentage(&mut self, n: &FrequencyPercentage) -> Result {
1658+
fn emit_frequency_percentage(&mut self, n: &FrequencyPercentage) -> Result {
16731659
match n {
16741660
FrequencyPercentage::Frequency(n) => emit!(self, n),
16751661
FrequencyPercentage::Percentage(n) => emit!(self, n),

crates/swc_css_compat/src/nesting.rs

+7-22
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,14 @@ impl NestingHandler {
231231

232232
for value in rule.block.value.take() {
233233
match value {
234-
ComponentValue::StyleBlock(box StyleBlock::QualifiedRule(mut nested)) => {
234+
ComponentValue::QualifiedRule(mut nested) => {
235235
self.process_prelude(&rule.prelude, &mut nested.prelude);
236236

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

239239
continue;
240240
}
241-
ComponentValue::StyleBlock(box StyleBlock::AtRule(ref at_rule)) => {
241+
ComponentValue::AtRule(ref at_rule) => {
242242
if let Some(
243243
AtRulePrelude::MediaPrelude(..)
244244
| AtRulePrelude::SupportsPrelude(..)
@@ -252,9 +252,7 @@ impl NestingHandler {
252252

253253
for n in &block.value {
254254
match n {
255-
ComponentValue::StyleBlock(box StyleBlock::QualifiedRule(
256-
n,
257-
)) => {
255+
ComponentValue::QualifiedRule(n) => {
258256
let mut q = n.clone();
259257

260258
self.process_prelude(&rule.prelude, &mut q.prelude);
@@ -284,12 +282,7 @@ impl NestingHandler {
284282
},
285283
});
286284

287-
nested_of_media.insert(
288-
0,
289-
ComponentValue::StyleBlock(Box::new(
290-
StyleBlock::QualifiedRule(rule),
291-
)),
292-
);
285+
nested_of_media.insert(0, ComponentValue::QualifiedRule(rule));
293286
}
294287

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

345338
for n in n.take() {
346339
match n {
347-
ComponentValue::StyleBlock(box StyleBlock::QualifiedRule(mut n)) => {
340+
ComponentValue::QualifiedRule(mut n) => {
348341
let mut rules = self.extract_nested_rules(&mut n);
349342

350343
rules.visit_mut_with(self);
351344

352-
new.push(ComponentValue::StyleBlock(Box::new(
353-
StyleBlock::QualifiedRule(n),
354-
)));
345+
new.push(ComponentValue::QualifiedRule(n));
355346
new.extend(rules.into_iter().map(rule_to_component_value));
356347
}
357348

@@ -366,11 +357,5 @@ impl VisitMut for NestingHandler {
366357
}
367358

368359
fn rule_to_component_value(rule: Rule) -> ComponentValue {
369-
match rule {
370-
Rule::QualifiedRule(q) => {
371-
ComponentValue::StyleBlock(Box::new(StyleBlock::QualifiedRule(q)))
372-
}
373-
Rule::AtRule(r) => ComponentValue::StyleBlock(Box::new(StyleBlock::AtRule(r))),
374-
Rule::ListOfComponentValues(..) => ComponentValue::Rule(Box::new(rule)),
375-
}
360+
rule.into()
376361
}

0 commit comments

Comments
 (0)