use std::path::Path;
use std::sync::LazyLock;
use oxc_span::Span;
use crate::{ExportInfo, ExportName, ImportInfo, ImportedName, ModuleInfo, VisibilityTag};
use fallow_types::discover::FileId;
static CSS_IMPORT_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
regex::Regex::new(r#"@import\s+(?:url\(\s*(?:["']([^"']+)["']|([^)]+))\s*\)|["']([^"']+)["'])"#)
.expect("valid regex")
});
static SCSS_USE_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
regex::Regex::new(r#"@(?:use|forward)\s+["']([^"']+)["']"#).expect("valid regex")
});
static CSS_APPLY_RE: LazyLock<regex::Regex> =
LazyLock::new(|| regex::Regex::new(r"@apply\s+[^;}\n]+").expect("valid regex"));
static CSS_TAILWIND_RE: LazyLock<regex::Regex> =
LazyLock::new(|| regex::Regex::new(r"@tailwind\s+\w+").expect("valid regex"));
static CSS_COMMENT_RE: LazyLock<regex::Regex> =
LazyLock::new(|| regex::Regex::new(r"(?s)/\*.*?\*/").expect("valid regex"));
static SCSS_LINE_COMMENT_RE: LazyLock<regex::Regex> =
LazyLock::new(|| regex::Regex::new(r"//[^\n]*").expect("valid regex"));
static CSS_CLASS_RE: LazyLock<regex::Regex> =
LazyLock::new(|| regex::Regex::new(r"\.([a-zA-Z_][a-zA-Z0-9_-]*)").expect("valid regex"));
static CSS_NON_SELECTOR_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
regex::Regex::new(r#"(?s)"[^"]*"|'[^']*'|url\([^)]*\)"#).expect("valid regex")
});
pub(crate) fn is_css_file(path: &Path) -> bool {
path.extension()
.and_then(|e| e.to_str())
.is_some_and(|ext| ext == "css" || ext == "scss")
}
fn is_css_module_file(path: &Path) -> bool {
is_css_file(path)
&& path
.file_stem()
.and_then(|s| s.to_str())
.is_some_and(|stem| stem.ends_with(".module"))
}
fn is_css_url_import(source: &str) -> bool {
source.starts_with("http://") || source.starts_with("https://") || source.starts_with("data:")
}
fn normalize_css_import_path(path: String, is_scss: bool) -> String {
if path.starts_with('.') || path.starts_with('/') || path.contains("://") {
return path;
}
if path.starts_with('@') && path.contains('/') {
return path;
}
let ext = std::path::Path::new(&path)
.extension()
.and_then(|e| e.to_str());
match ext {
Some(e)
if e.eq_ignore_ascii_case("css")
|| e.eq_ignore_ascii_case("scss")
|| e.eq_ignore_ascii_case("sass")
|| e.eq_ignore_ascii_case("less") =>
{
format!("./{path}")
}
_ => {
if is_scss && !path.contains(':') {
format!("./{path}")
} else {
path
}
}
}
}
fn strip_css_comments(source: &str, is_scss: bool) -> String {
let stripped = CSS_COMMENT_RE.replace_all(source, "");
if is_scss {
SCSS_LINE_COMMENT_RE.replace_all(&stripped, "").into_owned()
} else {
stripped.into_owned()
}
}
pub fn extract_css_module_exports(source: &str) -> Vec<ExportInfo> {
let cleaned = CSS_NON_SELECTOR_RE.replace_all(source, "");
let mut seen = rustc_hash::FxHashSet::default();
let mut exports = Vec::new();
for cap in CSS_CLASS_RE.captures_iter(&cleaned) {
if let Some(m) = cap.get(1) {
let class_name = m.as_str().to_string();
if seen.insert(class_name.clone()) {
exports.push(ExportInfo {
name: ExportName::Named(class_name),
local_name: None,
is_type_only: false,
visibility: VisibilityTag::None,
span: Span::default(),
members: Vec::new(),
super_class: None,
});
}
}
}
exports
}
pub(crate) fn parse_css_to_module(
file_id: FileId,
path: &Path,
source: &str,
content_hash: u64,
) -> ModuleInfo {
let suppressions = crate::suppress::parse_suppressions_from_source(source);
let is_scss = path
.extension()
.and_then(|e| e.to_str())
.is_some_and(|ext| ext == "scss");
let stripped = strip_css_comments(source, is_scss);
let mut imports = Vec::new();
for cap in CSS_IMPORT_RE.captures_iter(&stripped) {
let source_path = cap
.get(1)
.or_else(|| cap.get(2))
.or_else(|| cap.get(3))
.map(|m| m.as_str().trim().to_string());
if let Some(src) = source_path
&& !src.is_empty()
&& !is_css_url_import(&src)
{
let src = normalize_css_import_path(src, is_scss);
imports.push(ImportInfo {
source: src,
imported_name: ImportedName::SideEffect,
local_name: String::new(),
is_type_only: false,
span: Span::default(),
source_span: Span::default(),
});
}
}
if is_scss {
for cap in SCSS_USE_RE.captures_iter(&stripped) {
if let Some(m) = cap.get(1) {
imports.push(ImportInfo {
source: normalize_css_import_path(m.as_str().to_string(), true),
imported_name: ImportedName::SideEffect,
local_name: String::new(),
is_type_only: false,
span: Span::default(),
source_span: Span::default(),
});
}
}
}
let has_apply = CSS_APPLY_RE.is_match(&stripped);
let has_tailwind = CSS_TAILWIND_RE.is_match(&stripped);
if has_apply || has_tailwind {
imports.push(ImportInfo {
source: "tailwindcss".to_string(),
imported_name: ImportedName::SideEffect,
local_name: String::new(),
is_type_only: false,
span: Span::default(),
source_span: Span::default(),
});
}
let exports = if is_css_module_file(path) {
extract_css_module_exports(&stripped)
} else {
Vec::new()
};
ModuleInfo {
file_id,
exports,
imports,
re_exports: Vec::new(),
dynamic_imports: Vec::new(),
dynamic_import_patterns: Vec::new(),
require_calls: Vec::new(),
member_accesses: Vec::new(),
whole_object_uses: Vec::new(),
has_cjs_exports: false,
content_hash,
suppressions,
unused_import_bindings: Vec::new(),
line_offsets: fallow_types::extract::compute_line_offsets(source),
complexity: Vec::new(),
flag_uses: Vec::new(),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn export_names(source: &str) -> Vec<String> {
extract_css_module_exports(source)
.into_iter()
.filter_map(|e| match e.name {
ExportName::Named(n) => Some(n),
ExportName::Default => None,
})
.collect()
}
#[test]
fn is_css_file_css() {
assert!(is_css_file(Path::new("styles.css")));
}
#[test]
fn is_css_file_scss() {
assert!(is_css_file(Path::new("styles.scss")));
}
#[test]
fn is_css_file_rejects_js() {
assert!(!is_css_file(Path::new("app.js")));
}
#[test]
fn is_css_file_rejects_ts() {
assert!(!is_css_file(Path::new("app.ts")));
}
#[test]
fn is_css_file_rejects_less() {
assert!(!is_css_file(Path::new("styles.less")));
}
#[test]
fn is_css_file_rejects_no_extension() {
assert!(!is_css_file(Path::new("Makefile")));
}
#[test]
fn is_css_module_file_module_css() {
assert!(is_css_module_file(Path::new("Component.module.css")));
}
#[test]
fn is_css_module_file_module_scss() {
assert!(is_css_module_file(Path::new("Component.module.scss")));
}
#[test]
fn is_css_module_file_rejects_plain_css() {
assert!(!is_css_module_file(Path::new("styles.css")));
}
#[test]
fn is_css_module_file_rejects_plain_scss() {
assert!(!is_css_module_file(Path::new("styles.scss")));
}
#[test]
fn is_css_module_file_rejects_module_js() {
assert!(!is_css_module_file(Path::new("utils.module.js")));
}
#[test]
fn extracts_single_class() {
let names = export_names(".foo { color: red; }");
assert_eq!(names, vec!["foo"]);
}
#[test]
fn extracts_multiple_classes() {
let names = export_names(".foo { } .bar { }");
assert_eq!(names, vec!["foo", "bar"]);
}
#[test]
fn extracts_nested_classes() {
let names = export_names(".foo .bar { color: red; }");
assert!(names.contains(&"foo".to_string()));
assert!(names.contains(&"bar".to_string()));
}
#[test]
fn extracts_hyphenated_class() {
let names = export_names(".my-class { }");
assert_eq!(names, vec!["my-class"]);
}
#[test]
fn extracts_camel_case_class() {
let names = export_names(".myClass { }");
assert_eq!(names, vec!["myClass"]);
}
#[test]
fn extracts_underscore_class() {
let names = export_names("._hidden { } .__wrapper { }");
assert!(names.contains(&"_hidden".to_string()));
assert!(names.contains(&"__wrapper".to_string()));
}
#[test]
fn pseudo_selector_hover() {
let names = export_names(".foo:hover { color: blue; }");
assert_eq!(names, vec!["foo"]);
}
#[test]
fn pseudo_selector_focus() {
let names = export_names(".input:focus { outline: none; }");
assert_eq!(names, vec!["input"]);
}
#[test]
fn pseudo_element_before() {
let names = export_names(".icon::before { content: ''; }");
assert_eq!(names, vec!["icon"]);
}
#[test]
fn combined_pseudo_selectors() {
let names = export_names(".btn:hover, .btn:active, .btn:focus { }");
assert_eq!(names, vec!["btn"]);
}
#[test]
fn classes_inside_media_query() {
let names = export_names(
"@media (max-width: 768px) { .mobile-nav { display: block; } .desktop-nav { display: none; } }",
);
assert!(names.contains(&"mobile-nav".to_string()));
assert!(names.contains(&"desktop-nav".to_string()));
}
#[test]
fn deduplicates_repeated_class() {
let names = export_names(".btn { color: red; } .btn { font-size: 14px; }");
assert_eq!(names.iter().filter(|n| *n == "btn").count(), 1);
}
#[test]
fn empty_source() {
let names = export_names("");
assert!(names.is_empty());
}
#[test]
fn no_classes() {
let names = export_names("body { margin: 0; } * { box-sizing: border-box; }");
assert!(names.is_empty());
}
#[test]
fn ignores_classes_in_block_comments() {
let stripped = strip_css_comments("/* .fake { } */ .real { }", false);
let names = export_names(&stripped);
assert!(!names.contains(&"fake".to_string()));
assert!(names.contains(&"real".to_string()));
}
#[test]
fn ignores_classes_in_strings() {
let names = export_names(r#".real { content: ".fake"; }"#);
assert!(names.contains(&"real".to_string()));
assert!(!names.contains(&"fake".to_string()));
}
#[test]
fn ignores_classes_in_url() {
let names = export_names(".real { background: url(./images/hero.png); }");
assert!(names.contains(&"real".to_string()));
assert!(!names.contains(&"png".to_string()));
}
#[test]
fn strip_css_block_comment() {
let result = strip_css_comments("/* removed */ .kept { }", false);
assert!(!result.contains("removed"));
assert!(result.contains(".kept"));
}
#[test]
fn strip_scss_line_comment() {
let result = strip_css_comments("// removed\n.kept { }", true);
assert!(!result.contains("removed"));
assert!(result.contains(".kept"));
}
#[test]
fn strip_scss_preserves_css_outside_comments() {
let source = "// line comment\n/* block comment */\n.visible { color: red; }";
let result = strip_css_comments(source, true);
assert!(result.contains(".visible"));
}
#[test]
fn url_import_http() {
assert!(is_css_url_import("http://example.com/style.css"));
}
#[test]
fn url_import_https() {
assert!(is_css_url_import("https://fonts.googleapis.com/css"));
}
#[test]
fn url_import_data() {
assert!(is_css_url_import("data:text/css;base64,abc"));
}
#[test]
fn url_import_local_not_skipped() {
assert!(!is_css_url_import("./local.css"));
}
#[test]
fn url_import_bare_specifier_not_skipped() {
assert!(!is_css_url_import("tailwindcss"));
}
#[test]
fn normalize_relative_dot_path_unchanged() {
assert_eq!(
normalize_css_import_path("./reset.css".to_string(), false),
"./reset.css"
);
}
#[test]
fn normalize_parent_relative_path_unchanged() {
assert_eq!(
normalize_css_import_path("../shared.scss".to_string(), false),
"../shared.scss"
);
}
#[test]
fn normalize_absolute_path_unchanged() {
assert_eq!(
normalize_css_import_path("/styles/main.css".to_string(), false),
"/styles/main.css"
);
}
#[test]
fn normalize_url_unchanged() {
assert_eq!(
normalize_css_import_path("https://example.com/style.css".to_string(), false),
"https://example.com/style.css"
);
}
#[test]
fn normalize_bare_css_gets_dot_slash() {
assert_eq!(
normalize_css_import_path("app.css".to_string(), false),
"./app.css"
);
}
#[test]
fn normalize_bare_scss_gets_dot_slash() {
assert_eq!(
normalize_css_import_path("vars.scss".to_string(), false),
"./vars.scss"
);
}
#[test]
fn normalize_bare_sass_gets_dot_slash() {
assert_eq!(
normalize_css_import_path("main.sass".to_string(), false),
"./main.sass"
);
}
#[test]
fn normalize_bare_less_gets_dot_slash() {
assert_eq!(
normalize_css_import_path("theme.less".to_string(), false),
"./theme.less"
);
}
#[test]
fn normalize_bare_js_extension_stays_bare() {
assert_eq!(
normalize_css_import_path("module.js".to_string(), false),
"module.js"
);
}
#[test]
fn normalize_scss_bare_partial_gets_dot_slash() {
assert_eq!(
normalize_css_import_path("variables".to_string(), true),
"./variables"
);
}
#[test]
fn normalize_scss_bare_partial_with_subdir_gets_dot_slash() {
assert_eq!(
normalize_css_import_path("base/reset".to_string(), true),
"./base/reset"
);
}
#[test]
fn normalize_scss_builtin_stays_bare() {
assert_eq!(
normalize_css_import_path("sass:math".to_string(), true),
"sass:math"
);
}
#[test]
fn normalize_scss_relative_path_unchanged() {
assert_eq!(
normalize_css_import_path("../styles/variables".to_string(), true),
"../styles/variables"
);
}
#[test]
fn normalize_css_bare_extensionless_stays_bare() {
assert_eq!(
normalize_css_import_path("tailwindcss".to_string(), false),
"tailwindcss"
);
}
#[test]
fn normalize_scoped_package_with_css_extension_stays_bare() {
assert_eq!(
normalize_css_import_path("@fontsource/monaspace-neon/400.css".to_string(), false),
"@fontsource/monaspace-neon/400.css"
);
}
#[test]
fn normalize_scoped_package_with_scss_extension_stays_bare() {
assert_eq!(
normalize_css_import_path("@company/design-system/tokens.scss".to_string(), true),
"@company/design-system/tokens.scss"
);
}
#[test]
fn normalize_scoped_package_without_extension_stays_bare() {
assert_eq!(
normalize_css_import_path("@fallow/design-system/styles".to_string(), false),
"@fallow/design-system/styles"
);
}
#[test]
fn normalize_scoped_package_extensionless_scss_stays_bare() {
assert_eq!(
normalize_css_import_path("@company/tokens".to_string(), true),
"@company/tokens"
);
}
#[test]
fn normalize_path_alias_with_css_extension_stays_bare() {
assert_eq!(
normalize_css_import_path("@/components/Button.css".to_string(), false),
"@/components/Button.css"
);
}
#[test]
fn normalize_path_alias_extensionless_stays_bare() {
assert_eq!(
normalize_css_import_path("@/styles/variables".to_string(), false),
"@/styles/variables"
);
}
#[test]
fn strip_css_no_comments() {
let source = ".foo { color: red; }";
assert_eq!(strip_css_comments(source, false), source);
}
#[test]
fn strip_css_multiple_block_comments() {
let source = "/* comment-one */ .foo { } /* comment-two */ .bar { }";
let result = strip_css_comments(source, false);
assert!(!result.contains("comment-one"));
assert!(!result.contains("comment-two"));
assert!(result.contains(".foo"));
assert!(result.contains(".bar"));
}
#[test]
fn strip_scss_does_not_affect_non_scss() {
let source = "// this stays\n.foo { }";
let result = strip_css_comments(source, false);
assert!(result.contains("// this stays"));
}
#[test]
fn css_module_parses_suppressions() {
let info = parse_css_to_module(
fallow_types::discover::FileId(0),
Path::new("Component.module.css"),
"/* fallow-ignore-file */\n.btn { color: red; }",
0,
);
assert!(!info.suppressions.is_empty());
assert_eq!(info.suppressions[0].line, 0);
}
#[test]
fn extracts_class_starting_with_underscore() {
let names = export_names("._private { } .__dunder { }");
assert!(names.contains(&"_private".to_string()));
assert!(names.contains(&"__dunder".to_string()));
}
#[test]
fn ignores_id_selectors() {
let names = export_names("#myId { color: red; }");
assert!(!names.contains(&"myId".to_string()));
}
#[test]
fn ignores_element_selectors() {
let names = export_names("div { color: red; } span { }");
assert!(names.is_empty());
}
}