From 05ca19b84489ae47f69d8228b3c0097164c7ec6e Mon Sep 17 00:00:00 2001 From: Brandon Dail Date: Wed, 13 Sep 2023 21:14:58 -0500 Subject: [PATCH] fix(css/modules): Support composes when using multiple subclass selectors (#7949) --- crates/swc_css_modules/src/lib.rs | 14 +++++----- .../issue-7948/issue-7948.compiled.css | 11 ++++++++ .../fixture/modules/issue-7948/issue-7948.css | 16 +++++++++++ .../issue-7948/issue-7948.transform.json | 28 +++++++++++++++++++ 4 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 crates/swc_css_modules/tests/fixture/modules/issue-7948/issue-7948.compiled.css create mode 100644 crates/swc_css_modules/tests/fixture/modules/issue-7948/issue-7948.css create mode 100644 crates/swc_css_modules/tests/fixture/modules/issue-7948/issue-7948.transform.json diff --git a/crates/swc_css_modules/src/lib.rs b/crates/swc_css_modules/src/lib.rs index cf84afd8415..49678021c42 100644 --- a/crates/swc_css_modules/src/lib.rs +++ b/crates/swc_css_modules/src/lib.rs @@ -204,13 +204,13 @@ where n.visit_mut_children_with(self); if let QualifiedRulePrelude::SelectorList(sel) = &n.prelude { - // - if sel.children.len() == 1 && sel.children[0].children.len() == 1 { - if let ComplexSelectorChildren::CompoundSelector(sel) = &sel.children[0].children[0] - { - if sel.subclass_selectors.len() == 1 { - if let SubclassSelector::Class(class_sel) = &sel.subclass_selectors[0] { - if let Some(composes) = self.data.composes_for_current.take() { + let composes = self.data.composes_for_current.take(); + + for child in &sel.children { + if let ComplexSelectorChildren::CompoundSelector(sel) = &child.children[0] { + for subclass_sel in &sel.subclass_selectors { + if let SubclassSelector::Class(class_sel) = &subclass_sel { + if let Some(composes) = &composes { let key = self .data .renamed_to_orig diff --git a/crates/swc_css_modules/tests/fixture/modules/issue-7948/issue-7948.compiled.css b/crates/swc_css_modules/tests/fixture/modules/issue-7948/issue-7948.compiled.css new file mode 100644 index 00000000000..17561d7af9e --- /dev/null +++ b/crates/swc_css_modules/tests/fixture/modules/issue-7948/issue-7948.compiled.css @@ -0,0 +1,11 @@ +.__local__root-class { + background-color: red; +} +.__local__chain-1, +.__local__chain-2 {} +.__local__chain-1 { + color: blue; +} +.__local__chain-2 { + color: green; +} diff --git a/crates/swc_css_modules/tests/fixture/modules/issue-7948/issue-7948.css b/crates/swc_css_modules/tests/fixture/modules/issue-7948/issue-7948.css new file mode 100644 index 00000000000..93a74fda23f --- /dev/null +++ b/crates/swc_css_modules/tests/fixture/modules/issue-7948/issue-7948.css @@ -0,0 +1,16 @@ +.root-class { + background-color: red; +} + +.chain-1, +.chain-2 { + composes: root-class; +} + +.chain-1 { + color: blue; +} + +.chain-2 { + color: green; +} diff --git a/crates/swc_css_modules/tests/fixture/modules/issue-7948/issue-7948.transform.json b/crates/swc_css_modules/tests/fixture/modules/issue-7948/issue-7948.transform.json new file mode 100644 index 00000000000..9a965a943e0 --- /dev/null +++ b/crates/swc_css_modules/tests/fixture/modules/issue-7948/issue-7948.transform.json @@ -0,0 +1,28 @@ +{ + "root-class": [ + { + "type": "local", + "name": "__local__root-class" + } + ], + "chain-1": [ + { + "type": "local", + "name": "__local__chain-1" + }, + { + "type": "local", + "name": "__local__root-class" + } + ], + "chain-2": [ + { + "type": "local", + "name": "__local__chain-2" + }, + { + "type": "local", + "name": "__local__root-class" + } + ] +}