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

refactor(css/compat): union nesting and compiler #6686

Merged
merged 5 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 25 additions & 3 deletions crates/swc_css_compat/src/compiler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use swc_common::{Spanned, DUMMY_SP};
use swc_common::{util::take::Take, Spanned, DUMMY_SP};
use swc_css_ast::{
AbsoluteColorBase, AtRule, ComponentValue, CompoundSelector, MediaAnd, MediaCondition,
MediaConditionAllType, MediaConditionWithoutOr, MediaInParens, MediaQuery, Rule,
Expand All @@ -16,8 +16,8 @@ mod color_space_separated_parameters;
mod custom_media;
mod legacy_rgb_and_hsl;
mod media_query_ranges;
mod nesting;
mod selector_not;
mod utils;

/// Compiles a modern CSS file to a CSS file which works with old browsers.
#[derive(Debug)]
Expand Down Expand Up @@ -88,7 +88,29 @@ impl VisitMut for Compiler {
}

fn visit_mut_rules(&mut self, n: &mut Vec<Rule>) {
n.visit_mut_children_with(self);
if self.c.process.contains(Features::NESTING) {
let mut new = vec![];

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

rules.visit_mut_with(self);

new.push(Rule::QualifiedRule(n));
new.extend(rules);
}
_ => {
new.push(n);
}
}
}

*n = new;
} else {
n.visit_mut_children_with(self);
}

if self.c.process.contains(Features::CUSTOM_MEDIA) {
self.custom_media.process_rules(n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ use std::iter::once;

use swc_common::{util::take::Take, DUMMY_SP};
use swc_css_ast::*;
use swc_css_visit::{VisitMut, VisitMutWith};

pub fn nesting() -> impl VisitMut {
NestingHandler {}
}

struct NestingHandler {}
use crate::compiler::Compiler;

impl NestingHandler {
impl Compiler {
fn process_subclass_selectors(
&mut self,
prelude: &SelectorList,
Expand Down Expand Up @@ -225,7 +220,7 @@ impl NestingHandler {
}
}

fn extract_nested_rules(&mut self, rule: &mut QualifiedRule) -> Vec<Rule> {
pub(crate) fn extract_nested_rules(&mut self, rule: &mut QualifiedRule) -> Vec<Rule> {
let mut nested_rules = vec![];
let mut block_values = vec![];

Expand Down Expand Up @@ -262,7 +257,7 @@ impl NestingHandler {
nested_of_media.extend(
once(Rule::QualifiedRule(q))
.chain(rules.into_iter())
.map(rule_to_component_value),
.map(From::from),
);
}

Expand Down Expand Up @@ -308,54 +303,3 @@ impl NestingHandler {
nested_rules
}
}

impl VisitMut for NestingHandler {
fn visit_mut_rules(&mut self, n: &mut Vec<Rule>) {
let mut new = vec![];

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

rules.visit_mut_with(self);

new.push(Rule::QualifiedRule(n));
new.extend(rules);
}
_ => {
new.push(n);
}
}
}

*n = new;
}

fn visit_mut_component_values(&mut self, n: &mut Vec<ComponentValue>) {
let mut new = vec![];

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

rules.visit_mut_with(self);

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

_ => {
new.push(n);
}
}
}

*n = new;
}
}

fn rule_to_component_value(rule: Rule) -> ComponentValue {
rule.into()
}
1 change: 0 additions & 1 deletion crates/swc_css_compat/src/compiler/utils.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/swc_css_compat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@

pub mod compiler;
pub mod feature;
pub mod nesting;
19 changes: 6 additions & 13 deletions crates/swc_css_compat/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use swc_css_codegen::{
use swc_css_compat::{
compiler::{Compiler, Config},
feature::Features,
nesting::nesting,
};
use swc_css_parser::{parse_file, parser::ParserConfig};
use swc_css_visit::VisitMutWith;
Expand Down Expand Up @@ -54,18 +53,17 @@ fn print_stylesheet(ss: &Stylesheet) -> String {
s
}

fn test_nesting(input: PathBuf, suffix: Option<&str>) {
let parent = input.parent().unwrap();
let output = match suffix {
Some(suffix) => parent.join("output.".to_owned() + suffix + ".css"),
_ => parent.join("output.css"),
};
#[testing::fixture("tests/nesting/**/*.css", exclude("expect.css"))]
fn test_nesting(input: PathBuf) {
let output = input.with_extension("expect.css");

testing::run_test(false, |cm, _| {
let fm = cm.load_file(&input).unwrap();
let mut ss = parse_stylesheet(&fm);

ss.visit_mut_with(&mut nesting());
ss.visit_mut_with(&mut Compiler::new(Config {
process: Features::NESTING,
}));

let s = print_stylesheet(&ss);

Expand All @@ -76,11 +74,6 @@ fn test_nesting(input: PathBuf, suffix: Option<&str>) {
.unwrap();
}

#[testing::fixture("tests/nesting/**/input.css")]
fn test_nesting_without_env(input: PathBuf) {
test_nesting(input, None)
}

#[testing::fixture("tests/custom-media-query/**/*.css", exclude("expect.css"))]
fn test_custom_media_query(input: PathBuf) {
let output = input.with_extension("expect.css");
Expand Down
10 changes: 7 additions & 3 deletions crates/swc_css_modules/tests/with_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use swc_css_codegen::{
writer::basic::{BasicCssWriter, BasicCssWriterConfig},
CodegenConfig, Emit,
};
use swc_css_compat::nesting::nesting;
use swc_css_compat::{
compiler::{Compiler, Config},
feature::Features,
};
use swc_css_parser::{parse_file, parser::ParserConfig};
use swc_css_visit::VisitMutWith;
use testing::NormalizedOutput;
Expand Down Expand Up @@ -48,8 +51,9 @@ fn test_full(input: PathBuf, suffix: Option<&str>) {
}

swc_css_modules::compile(&mut ss, TestConfig {});

ss.visit_mut_with(&mut nesting());
ss.visit_mut_with(&mut Compiler::new(Config {
process: Features::NESTING,
}));

let mut s = String::new();
{
Expand Down