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

fix(css/modules): Allow out-of-order class names for composes #8218

Merged
merged 9 commits into from
Nov 4, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions crates/swc_css_modules/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,23 @@ where
}
}

for class_name in n.value.iter() {
for class_name in n.value.iter_mut() {
if let ComponentValue::Ident(box Ident { span, value, .. }) = class_name {
if let Some(value) = self.data.orig_to_renamed.get(value) {
let orig = value.clone();
rename(
*span,
&mut self.config,
&mut self.result,
&mut self.data.orig_to_renamed,
&mut self.data.renamed_to_orig,
value,
);

if let Some(new_name) = self.data.orig_to_renamed.get(&orig) {
composes_for_current.push(CssClassName::Local {
name: Ident {
span: *span,
value: value.clone(),
value: new_name.clone(),
raw: None,
},
});
Expand Down
72 changes: 35 additions & 37 deletions crates/swc_css_modules/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ fn imports(input: PathBuf) {
return Ok(());
}

let s = serde_json::to_string_pretty(&result).unwrap();
NormalizedOutput::from(s)
.compare_to_file(input.with_file_name(format!(
NormalizedOutput::compare_json_to_file(
&result,
&input.with_file_name(format!(
"{}.imports.json",
input.file_stem().unwrap().to_string_lossy()
)))
.unwrap();
)),
);

Ok(())
})
Expand Down Expand Up @@ -89,41 +89,39 @@ fn compile(input: PathBuf) {
.unwrap();

if !transform_result.renamed.is_empty() {
let transformed_classes = serde_json::to_string_pretty(
&transform_result
.renamed
.into_iter()
.map(|(k, v)| {
(
k,
v.into_iter()
.map(|v| match v {
CssClassName::Global { name } => {
CssClassNameForTest::Global { name: name.value }
}
CssClassName::Local { name } => {
CssClassNameForTest::Local { name: name.value }
let transformed_classes = &transform_result
.renamed
.into_iter()
.map(|(k, v)| {
(
k,
v.into_iter()
.map(|v| match v {
CssClassName::Global { name } => {
CssClassNameForTest::Global { name: name.value }
}
CssClassName::Local { name } => {
CssClassNameForTest::Local { name: name.value }
}
CssClassName::Import { name, from } => {
CssClassNameForTest::Import {
name: name.value,
from,
}
CssClassName::Import { name, from } => {
CssClassNameForTest::Import {
name: name.value,
from,
}
}
})
.collect::<Vec<_>>(),
)
})
.collect::<FxHashMap<_, _>>(),
)
.unwrap();

NormalizedOutput::from(transformed_classes)
.compare_to_file(input.with_file_name(format!(
}
})
.collect::<Vec<_>>(),
)
})
.collect::<FxHashMap<_, _>>();

NormalizedOutput::compare_json_to_file(
&transformed_classes,
&input.with_file_name(format!(
"{}.transform.json",
input.file_stem().unwrap().to_string_lossy()
)))
.unwrap();
)),
);
}
Ok(())
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.__local__child-class1 {
color: red;
}
.__local__root-class {}
.__local__child-class2 {
color: green;
}
10 changes: 10 additions & 0 deletions crates/swc_css_modules/tests/fixture/issue-7910.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.child-class1 {
color: red;
}
.root-class {
composes: child-class1;
composes: child-class2;
}
.child-class2 {
color: green;
}
28 changes: 28 additions & 0 deletions crates/swc_css_modules/tests/fixture/issue-7910.transform.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"child-class1": [
{
"type": "local",
"name": "__local__child-class1"
}
],
"root-class": [
{
"type": "local",
"name": "__local__root-class"
},
{
"type": "local",
"name": "__local__child-class1"
},
{
"type": "local",
"name": "__local__child-class2"
}
],
"child-class2": [
{
"type": "local",
"name": "__local__child-class2"
}
]
}
3 changes: 2 additions & 1 deletion crates/testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ difference = "2"
once_cell = "1.18.0"
pretty_assertions = "1.3"
regex = "1"
serde_json = "1.0.71"
serde = "1"
serde_json = "1"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }

Expand Down
25 changes: 24 additions & 1 deletion crates/testing/src/output.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::{
env, fmt,
fs::{create_dir_all, File},
fs::{self, create_dir_all, File},
io::Read,
ops::Deref,
path::Path,
};

use serde::Serialize;
use tracing::debug;

use crate::paths;
Expand Down Expand Up @@ -102,6 +103,28 @@ impl NormalizedOutput {
NormalizedOutput(normalize_input(s, true))
}

pub fn compare_json_to_file<T>(actual: &T, path: &Path)
where
T: Serialize,
{
let actual_value =
serde_json::to_value(actual).expect("failed to serialize the actual value to json");

if let Ok(expected) = fs::read_to_string(path) {
let expected_value = serde_json::from_str::<serde_json::Value>(&expected)
.expect("failed to deserialize the expected value from json");

if expected_value == actual_value {
return;
}
}

let actual_json_string = serde_json::to_string_pretty(&actual_value)
.expect("failed to serialize the actual value to json");

let _ = NormalizedOutput::from(actual_json_string).compare_to_file(path);
}

/// If output differs, prints actual stdout/stderr to
/// `CARGO_MANIFEST_DIR/target/swc-test-results/ui/$rel_path` where
/// `$rel_path`: `path.strip_prefix(CARGO_MANIFEST_DIR)`
Expand Down
Loading