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/parser): Fix parsing of :global(> *) #7082

Merged
merged 13 commits into from
Mar 16, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/swc_css_modules/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn compile(input: PathBuf) {
&mut errors,
)
.unwrap();

let _result = swc_css_modules::imports::analyze_imports(&ss);

let transform_result = swc_css_modules::compile(&mut ss, TestConfig {});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.quickstart .buttons :global(> *) {
color: red
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.__local__quickstart .__local__buttons > * {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is extra space and looks like we can cut them more, but it is not critical, I will refactor the whole logic on better perf and more readable logic and will fix it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thank you!

color: red;
}
3 changes: 3 additions & 0 deletions crates/swc_css_parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions crates/swc_css_parser/src/parser/selectors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use swc_css_ast::*;
use super::{input::ParserInput, PResult, Parser};
use crate::{
error::{Error, ErrorKind},
parser::Ctx,
Parse,
};

Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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::<ComplexSelector>()?;

self.input.skip_ws();

Expand Down