Skip to content

Commit

Permalink
feat: Add a lint for unsupported CSS Module selector (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Aug 2, 2024
1 parent 3e62347 commit be53018
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions selectors/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ pub enum SelectorParseErrorKind<'i> {
UnexpectedTokenInAttributeSelector(Token<'i>),
PseudoElementExpectedIdent(Token<'i>),
UnsupportedPseudoClassOrElement(CowRcStr<'i>),
AmbiguousCssModuleClass(CowRcStr<'i>),
UnexpectedIdent(CowRcStr<'i>),
ExpectedNamespace(CowRcStr<'i>),
ExpectedBarInAttr(Token<'i>),
Expand Down
5 changes: 5 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ pub enum SelectorError<'i> {
),
/// An unsupported pseudo class or pseudo element was encountered.
UnsupportedPseudoClassOrElement(CowArcStr<'i>),

/// Ambiguous CSS module class.
AmbiguousCssModuleClass(CowArcStr<'i>),
}

impl<'i> fmt::Display for SelectorError<'i> {
Expand All @@ -257,6 +260,7 @@ impl<'i> fmt::Display for SelectorError<'i> {
UnexpectedIdent(name) => write!(f, "Unexpected identifier: {}", name),
UnexpectedTokenInAttributeSelector(token) => write!(f, "Unexpected token in attribute selector: {:?}", token),
UnsupportedPseudoClassOrElement(name) => write!(f, "Unsupported pseudo class or element: {}", name),
AmbiguousCssModuleClass(_) => write!(f, "Ambiguous CSS module class not supported"),
}
}
}
Expand Down Expand Up @@ -297,6 +301,7 @@ impl<'i> From<SelectorParseErrorKind<'i>> for SelectorError<'i> {
SelectorError::ExplicitNamespaceUnexpectedToken(t.into())
}
SelectorParseErrorKind::ClassNeedsIdent(t) => SelectorError::ClassNeedsIdent(t.into()),
SelectorParseErrorKind::AmbiguousCssModuleClass(name) => SelectorError::AmbiguousCssModuleClass(name.into()),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ impl<'a, 'o, 'i> parcel_selectors::parser::Parser<'i> for SelectorParser<'a, 'o,
"corner-present" => WebKitScrollbar(WebKitScrollbarPseudoClass::CornerPresent),
"window-inactive" => WebKitScrollbar(WebKitScrollbarPseudoClass::WindowInactive),

"local" | "global" if self.options.css_modules.is_some() => {
return Err(loc.new_custom_error(SelectorParseErrorKind::AmbiguousCssModuleClass(name.clone())))
},

_ => {
if !name.starts_with('-') {
self.options.warn(loc.new_custom_error(SelectorParseErrorKind::UnsupportedPseudoClassOrElement(name.clone())));
Expand Down
31 changes: 31 additions & 0 deletions tests/cli_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,37 @@ fn css_modules_pattern() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[test]
fn css_modules_next_64299() -> Result<(), Box<dyn std::error::Error>> {
let file = assert_fs::NamedTempFile::new("test.css")?;
file.write_str(
"
.blue {
background: blue;
:global {
.red {
background: red;
}
}
&:global {
&.green {
background: green;
}
}
}
",
)?;

let mut cmd = Command::cargo_bin("lightningcss")?;
cmd.arg(file.path());
cmd.arg("--css-modules");
cmd.assert().failure();

Ok(())
}

#[test]
fn sourcemap() -> Result<(), Box<dyn std::error::Error>> {
let (input, _, _) = css_module_test_vals();
Expand Down

0 comments on commit be53018

Please sign in to comment.