From 15da05d7d5be0623dc84fd337bdb24cb3816f2c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Mar 2023 13:08:49 +0900 Subject: [PATCH 01/13] Add a test --- .../tests/nesting/vercel/1/input.css | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 crates/swc_css_compat/tests/nesting/vercel/1/input.css diff --git a/crates/swc_css_compat/tests/nesting/vercel/1/input.css b/crates/swc_css_compat/tests/nesting/vercel/1/input.css new file mode 100644 index 000000000000..1a820907978f --- /dev/null +++ b/crates/swc_css_compat/tests/nesting/vercel/1/input.css @@ -0,0 +1,35 @@ +.plan-redirect { + --geist-gap: 0; + + border-radius: var(--geist-marketing-radius); + box-shadow: var(--shadow-smallest); + + > * { + padding: var(--geist-space-gap); + } +} + +.plan-redirect-title > *, +.plan-redirect-content { + margin: 0; +} + +.plan-redirect-title { + box-shadow: 0px 1px 0px var(--accents-2); + > * { + font-size: 1rem; + } +} + +.plan-redirect-content { + align-items: stretch; + line-height: 1.5; + + > * { + margin: 0; + } + + > * + * { + margin-top: var(--geist-space-gap); + } +} From 46e9df69023f85ef915f39a54b1efbcaf69b57d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Mar 2023 14:56:51 +0900 Subject: [PATCH 02/13] panic --- crates/swc_css_compat/tests/fixture.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/swc_css_compat/tests/fixture.rs b/crates/swc_css_compat/tests/fixture.rs index 97ff7c4b2327..28586c4d6669 100644 --- a/crates/swc_css_compat/tests/fixture.rs +++ b/crates/swc_css_compat/tests/fixture.rs @@ -32,12 +32,17 @@ fn parse_stylesheet(fm: &Lrc) -> Stylesheet { &mut errors, ) .unwrap(); + let has_errors = !errors.is_empty(); for err in errors { HANDLER.with(|handler| { err.to_diagnostics(handler).emit(); }); } + if has_errors { + panic!("failed to parse"); + } + ss } From 245964334c1dbd86f91fd328aade6c554deb5779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Mar 2023 14:59:59 +0900 Subject: [PATCH 03/13] panic --- crates/swc_css_modules/tests/fixture.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/swc_css_modules/tests/fixture.rs b/crates/swc_css_modules/tests/fixture.rs index 80d3a29adb3a..ac06dfc60db6 100644 --- a/crates/swc_css_modules/tests/fixture.rs +++ b/crates/swc_css_modules/tests/fixture.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use swc_atoms::JsWord; +use swc_common::errors::HANDLER; use swc_css_codegen::{ writer::basic::{BasicCssWriter, BasicCssWriterConfig, IndentType}, CodeGenerator, CodegenConfig, Emit, @@ -55,6 +56,16 @@ fn compile(input: PathBuf) { &mut errors, ) .unwrap(); + + if !errors.is_empty() { + HANDLER.with(|handler| { + for err in errors { + err.to_diagnostics(handler).emit(); + } + }); + panic!("failed to parse") + } + let _result = swc_css_modules::imports::analyze_imports(&ss); let transform_result = swc_css_modules::compile(&mut ss, TestConfig {}); From 7db9313bf8a1828dd4c14b876b0a8df0be526751 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Mar 2023 15:00:26 +0900 Subject: [PATCH 04/13] Add a test --- crates/swc_css_modules/tests/fixture/vercel/1.css | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 crates/swc_css_modules/tests/fixture/vercel/1.css diff --git a/crates/swc_css_modules/tests/fixture/vercel/1.css b/crates/swc_css_modules/tests/fixture/vercel/1.css new file mode 100644 index 000000000000..230cea04e34b --- /dev/null +++ b/crates/swc_css_modules/tests/fixture/vercel/1.css @@ -0,0 +1,3 @@ +.quickstart .buttons :global(> *) { + color: red +} \ No newline at end of file From 56d8469040bf944824328a4631913fd30d90f932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Mar 2023 15:21:11 +0900 Subject: [PATCH 05/13] Remove --- .../tests/nesting/vercel/1/input.css | 35 ------------------- 1 file changed, 35 deletions(-) delete mode 100644 crates/swc_css_compat/tests/nesting/vercel/1/input.css diff --git a/crates/swc_css_compat/tests/nesting/vercel/1/input.css b/crates/swc_css_compat/tests/nesting/vercel/1/input.css deleted file mode 100644 index 1a820907978f..000000000000 --- a/crates/swc_css_compat/tests/nesting/vercel/1/input.css +++ /dev/null @@ -1,35 +0,0 @@ -.plan-redirect { - --geist-gap: 0; - - border-radius: var(--geist-marketing-radius); - box-shadow: var(--shadow-smallest); - - > * { - padding: var(--geist-space-gap); - } -} - -.plan-redirect-title > *, -.plan-redirect-content { - margin: 0; -} - -.plan-redirect-title { - box-shadow: 0px 1px 0px var(--accents-2); - > * { - font-size: 1rem; - } -} - -.plan-redirect-content { - align-items: stretch; - line-height: 1.5; - - > * { - margin: 0; - } - - > * + * { - margin-top: var(--geist-space-gap); - } -} From 674c36f74e9175a0cea548c0fcf2cb89dfb842fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Mar 2023 15:22:39 +0900 Subject: [PATCH 06/13] Add a test --- .../tests/with-compat/vercel/009/input.css | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 crates/swc_css_modules/tests/with-compat/vercel/009/input.css diff --git a/crates/swc_css_modules/tests/with-compat/vercel/009/input.css b/crates/swc_css_modules/tests/with-compat/vercel/009/input.css new file mode 100644 index 000000000000..b42b97cabb90 --- /dev/null +++ b/crates/swc_css_modules/tests/with-compat/vercel/009/input.css @@ -0,0 +1,41 @@ +@media (max-width: 700px) { + --padding-x: 20px; +} + +@media (min-width: 60em) { + .form-wrapper { + margin-top: var(--geist-space-medium); + width: calc(760px + var(--geist-space-medium) / 2); + } +} + +.plan-redirect { + --geist-gap: 0; + + border-radius: var(--geist-marketing-radius); + box-shadow: var(--shadow-smallest); + + > * { + padding: var(--geist-space-gap); + } +} + +.plan-redirect-title { + box-shadow: 0px 1px 0px var(--accents-2); + > * { + font-size: 1rem; + } +} + +.plan-redirect-content { + align-items: stretch; + line-height: 1.5; + + > * { + margin: 0; + } + + > * + * { + margin-top: var(--geist-space-gap); + } +} From c6fd81627be40abeb73e693b232a37545dcae65c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Mar 2023 15:23:36 +0900 Subject: [PATCH 07/13] Move a test --- .../{fixture/vercel/1.css => with-compat/vercel/010/input.css} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/swc_css_modules/tests/{fixture/vercel/1.css => with-compat/vercel/010/input.css} (100%) diff --git a/crates/swc_css_modules/tests/fixture/vercel/1.css b/crates/swc_css_modules/tests/with-compat/vercel/010/input.css similarity index 100% rename from crates/swc_css_modules/tests/fixture/vercel/1.css rename to crates/swc_css_modules/tests/with-compat/vercel/010/input.css From afb8ea0aaaef81ab55577ffff5c099b952c66beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Mar 2023 15:25:25 +0900 Subject: [PATCH 08/13] Revert --- crates/swc_css_modules/tests/fixture.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/crates/swc_css_modules/tests/fixture.rs b/crates/swc_css_modules/tests/fixture.rs index ac06dfc60db6..96d164b1c603 100644 --- a/crates/swc_css_modules/tests/fixture.rs +++ b/crates/swc_css_modules/tests/fixture.rs @@ -1,7 +1,6 @@ use std::path::PathBuf; use swc_atoms::JsWord; -use swc_common::errors::HANDLER; use swc_css_codegen::{ writer::basic::{BasicCssWriter, BasicCssWriterConfig, IndentType}, CodeGenerator, CodegenConfig, Emit, @@ -57,15 +56,6 @@ fn compile(input: PathBuf) { ) .unwrap(); - if !errors.is_empty() { - HANDLER.with(|handler| { - for err in errors { - err.to_diagnostics(handler).emit(); - } - }); - panic!("failed to parse") - } - let _result = swc_css_modules::imports::analyze_imports(&ss); let transform_result = swc_css_modules::compile(&mut ss, TestConfig {}); From a962ddcd55740b76117218f229407047da20aa49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Mar 2023 15:33:20 +0900 Subject: [PATCH 09/13] Fix global issue --- crates/swc_css_parser/src/parser/mod.rs | 3 +++ crates/swc_css_parser/src/parser/selectors/mod.rs | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/swc_css_parser/src/parser/mod.rs b/crates/swc_css_parser/src/parser/mod.rs index cca37a26f2c0..720913f41fa6 100644 --- a/crates/swc_css_parser/src/parser/mod.rs +++ b/crates/swc_css_parser/src/parser/mod.rs @@ -76,6 +76,8 @@ struct Ctx { in_page_at_rule: bool, in_container_at_rule: bool, in_font_feature_values_at_rule: bool, + + in_global_or_local_selector: bool, } impl Default for Ctx { @@ -92,6 +94,7 @@ impl Default for Ctx { in_page_at_rule: false, in_container_at_rule: false, in_font_feature_values_at_rule: false, + in_global_or_local_selector: false, } } } diff --git a/crates/swc_css_parser/src/parser/selectors/mod.rs b/crates/swc_css_parser/src/parser/selectors/mod.rs index 814638208e35..b3ee3e8a359e 100644 --- a/crates/swc_css_parser/src/parser/selectors/mod.rs +++ b/crates/swc_css_parser/src/parser/selectors/mod.rs @@ -5,6 +5,7 @@ use swc_css_ast::*; use super::{input::ParserInput, PResult, Parser}; use crate::{ error::{Error, ErrorKind}, + parser::Ctx, Parse, }; @@ -456,7 +457,11 @@ where } } - if nesting_selector.is_none() && type_selector.is_none() && subclass_selectors.is_empty() { + if !self.ctx.in_global_or_local_selector + && nesting_selector.is_none() + && type_selector.is_none() + && subclass_selectors.is_empty() + { return Err(Error::new(start_span, ErrorKind::InvalidSelector)); } @@ -862,7 +867,11 @@ where js_word!("local") | js_word!("global") if self.config.css_modules => { self.input.skip_ws(); - let selector_list = self.parse()?; + let ctx = Ctx { + in_global_or_local_selector: true, + ..self.ctx + }; + let selector_list = self.with_ctx(ctx).parse_as::()?; self.input.skip_ws(); From 2a3086ba5f59425ca0a964f1c206c09af7344bd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Mar 2023 15:33:31 +0900 Subject: [PATCH 10/13] Update test refs --- crates/swc_css_modules/tests/with-compat/vercel/010/output.css | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 crates/swc_css_modules/tests/with-compat/vercel/010/output.css diff --git a/crates/swc_css_modules/tests/with-compat/vercel/010/output.css b/crates/swc_css_modules/tests/with-compat/vercel/010/output.css new file mode 100644 index 000000000000..775d0a9b3599 --- /dev/null +++ b/crates/swc_css_modules/tests/with-compat/vercel/010/output.css @@ -0,0 +1,3 @@ +.__local__quickstart .__local__buttons > * { + color: red; +} From 92d817666e24816170d489dfd54591ad008705ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Mar 2023 15:33:55 +0900 Subject: [PATCH 11/13] Reduce --- .../tests/with-compat/vercel/009/input.css | 38 ------------------- 1 file changed, 38 deletions(-) diff --git a/crates/swc_css_modules/tests/with-compat/vercel/009/input.css b/crates/swc_css_modules/tests/with-compat/vercel/009/input.css index b42b97cabb90..8da4b95c82ad 100644 --- a/crates/swc_css_modules/tests/with-compat/vercel/009/input.css +++ b/crates/swc_css_modules/tests/with-compat/vercel/009/input.css @@ -1,41 +1,3 @@ @media (max-width: 700px) { --padding-x: 20px; } - -@media (min-width: 60em) { - .form-wrapper { - margin-top: var(--geist-space-medium); - width: calc(760px + var(--geist-space-medium) / 2); - } -} - -.plan-redirect { - --geist-gap: 0; - - border-radius: var(--geist-marketing-radius); - box-shadow: var(--shadow-smallest); - - > * { - padding: var(--geist-space-gap); - } -} - -.plan-redirect-title { - box-shadow: 0px 1px 0px var(--accents-2); - > * { - font-size: 1rem; - } -} - -.plan-redirect-content { - align-items: stretch; - line-height: 1.5; - - > * { - margin: 0; - } - - > * + * { - margin-top: var(--geist-space-gap); - } -} From 9b02c03073741623af63752ef6084013c4b2237b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Mar 2023 16:12:14 +0900 Subject: [PATCH 12/13] Revert --- crates/swc_css_compat/tests/fixture.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/crates/swc_css_compat/tests/fixture.rs b/crates/swc_css_compat/tests/fixture.rs index 28586c4d6669..97ff7c4b2327 100644 --- a/crates/swc_css_compat/tests/fixture.rs +++ b/crates/swc_css_compat/tests/fixture.rs @@ -32,17 +32,12 @@ fn parse_stylesheet(fm: &Lrc) -> Stylesheet { &mut errors, ) .unwrap(); - let has_errors = !errors.is_empty(); for err in errors { HANDLER.with(|handler| { err.to_diagnostics(handler).emit(); }); } - if has_errors { - panic!("failed to parse"); - } - ss } From 3028cea3235ab26c18e65b7d79dc325040f5fcd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Mar 2023 16:12:27 +0900 Subject: [PATCH 13/13] Remove invalid --- crates/swc_css_modules/tests/with-compat/vercel/009/input.css | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 crates/swc_css_modules/tests/with-compat/vercel/009/input.css diff --git a/crates/swc_css_modules/tests/with-compat/vercel/009/input.css b/crates/swc_css_modules/tests/with-compat/vercel/009/input.css deleted file mode 100644 index 8da4b95c82ad..000000000000 --- a/crates/swc_css_modules/tests/with-compat/vercel/009/input.css +++ /dev/null @@ -1,3 +0,0 @@ -@media (max-width: 700px) { - --padding-x: 20px; -}