From 34c30b12adaac9f071b9a4ad082d0bb42dd74728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 4 Nov 2023 08:53:13 +0900 Subject: [PATCH 1/8] Add a test --- crates/swc_css_modules/tests/fixture/issue-7910.css | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 crates/swc_css_modules/tests/fixture/issue-7910.css diff --git a/crates/swc_css_modules/tests/fixture/issue-7910.css b/crates/swc_css_modules/tests/fixture/issue-7910.css new file mode 100644 index 000000000000..3e75c80ecd94 --- /dev/null +++ b/crates/swc_css_modules/tests/fixture/issue-7910.css @@ -0,0 +1,10 @@ +.child-class1 { + color: red; +} +.root-class { + composes: child-class1; + composes: child-class2; +} +.child-class2 { + color: green; +} From f68f5ac792a69224ebd92943e8e875e017d72946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 4 Nov 2023 09:05:24 +0900 Subject: [PATCH 2/8] fix --- crates/swc_css_modules/src/lib.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/swc_css_modules/src/lib.rs b/crates/swc_css_modules/src/lib.rs index 7c113213d5dd..72975ca5f468 100644 --- a/crates/swc_css_modules/src/lib.rs +++ b/crates/swc_css_modules/src/lib.rs @@ -307,9 +307,19 @@ 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(value) = self.data.orig_to_renamed.get(&orig) { composes_for_current.push(CssClassName::Local { name: Ident { span: *span, From 4ac0652afc3b629337c55199f93444d32652c229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 4 Nov 2023 09:05:41 +0900 Subject: [PATCH 3/8] Rename --- crates/swc_css_modules/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/swc_css_modules/src/lib.rs b/crates/swc_css_modules/src/lib.rs index 72975ca5f468..7263b17a2dfc 100644 --- a/crates/swc_css_modules/src/lib.rs +++ b/crates/swc_css_modules/src/lib.rs @@ -319,11 +319,11 @@ where value, ); - if let Some(value) = self.data.orig_to_renamed.get(&orig) { + 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, }, }); From fa3b1efd74da7d12a93088c8c741beb2618d4413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 4 Nov 2023 09:05:49 +0900 Subject: [PATCH 4/8] Update test refs --- .../tests/fixture/issue-7910.compiled.css | 7 +++++ .../tests/fixture/issue-7910.transform.json | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 crates/swc_css_modules/tests/fixture/issue-7910.compiled.css create mode 100644 crates/swc_css_modules/tests/fixture/issue-7910.transform.json diff --git a/crates/swc_css_modules/tests/fixture/issue-7910.compiled.css b/crates/swc_css_modules/tests/fixture/issue-7910.compiled.css new file mode 100644 index 000000000000..41c534fbb4a8 --- /dev/null +++ b/crates/swc_css_modules/tests/fixture/issue-7910.compiled.css @@ -0,0 +1,7 @@ +.__local__child-class1 { + color: red; +} +.__local__root-class {} +.__local__child-class2 { + color: green; +} diff --git a/crates/swc_css_modules/tests/fixture/issue-7910.transform.json b/crates/swc_css_modules/tests/fixture/issue-7910.transform.json new file mode 100644 index 000000000000..a2c0330fb363 --- /dev/null +++ b/crates/swc_css_modules/tests/fixture/issue-7910.transform.json @@ -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" + } + ] +} From bb334b42a85b189b1f7e348cdff774e2fea83d09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 4 Nov 2023 09:18:08 +0900 Subject: [PATCH 5/8] Dep --- crates/testing/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/testing/Cargo.toml b/crates/testing/Cargo.toml index a1ca9653576e..a717809ca515 100644 --- a/crates/testing/Cargo.toml +++ b/crates/testing/Cargo.toml @@ -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"] } From d76291deb8b21c460cc5f192fb287970e8f9dd04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 4 Nov 2023 09:18:14 +0900 Subject: [PATCH 6/8] cargo lockfile --- Cargo.lock | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e2a3700d73cd..b34b61120994 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3250,9 +3250,9 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.188" +version = "1.0.190" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" +checksum = "91d3c334ca1ee894a2c6f6ad698fe8c435b76d504b13d436f0685d648d6d96f7" dependencies = [ "serde_derive", ] @@ -3280,9 +3280,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.188" +version = "1.0.190" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" +checksum = "67c5609f394e5c2bd7fc51efda478004ea80ef42fee983d5c67a65e34f32c0e3" dependencies = [ "proc-macro2", "quote", @@ -5451,6 +5451,7 @@ dependencies = [ "once_cell", "pretty_assertions", "regex", + "serde", "serde_json", "swc_common", "swc_error_reporters", From c174e3893b9b31fe729d373b41678b937e2f286e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 4 Nov 2023 09:23:24 +0900 Subject: [PATCH 7/8] `compare_json_to_file` --- crates/testing/src/output.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/testing/src/output.rs b/crates/testing/src/output.rs index 1618ec779cd0..a95018358d85 100644 --- a/crates/testing/src/output.rs +++ b/crates/testing/src/output.rs @@ -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; @@ -102,6 +103,28 @@ impl NormalizedOutput { NormalizedOutput(normalize_input(s, true)) } + pub fn compare_json_to_file(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::(&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)` From 76fb4193a1dba5aac72946c40bc17a83e0040616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 4 Nov 2023 09:25:24 +0900 Subject: [PATCH 8/8] use it --- crates/swc_css_modules/tests/fixture.rs | 72 ++++++++++++------------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/crates/swc_css_modules/tests/fixture.rs b/crates/swc_css_modules/tests/fixture.rs index 2c52e4870f9f..051ee818062e 100644 --- a/crates/swc_css_modules/tests/fixture.rs +++ b/crates/swc_css_modules/tests/fixture.rs @@ -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(()) }) @@ -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::>(), - ) - }) - .collect::>(), - ) - .unwrap(); - - NormalizedOutput::from(transformed_classes) - .compare_to_file(input.with_file_name(format!( + } + }) + .collect::>(), + ) + }) + .collect::>(); + + NormalizedOutput::compare_json_to_file( + &transformed_classes, + &input.with_file_name(format!( "{}.transform.json", input.file_stem().unwrap().to_string_lossy() - ))) - .unwrap(); + )), + ); } Ok(()) })