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_PLUGIN_RE: LazyLock<regex::Regex> =
LazyLock::new(|| regex::Regex::new(r#"@plugin\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")
});
static CSS_AT_RULE_PRELUDE_RE: LazyLock<regex::Regex> =
LazyLock::new(|| regex::Regex::new(r"@(?:layer|import)\b[^;{]*").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")
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CssImportSource {
pub raw: String,
pub normalized: String,
pub is_plugin: bool,
}
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 path_ref = std::path::Path::new(&path);
if !is_scss
&& path.contains('/')
&& path_ref
.extension()
.and_then(|e| e.to_str())
.is_some_and(is_style_extension)
{
return path;
}
let ext = std::path::Path::new(&path)
.extension()
.and_then(|e| e.to_str());
match ext {
Some(e) if is_style_extension(e) => format!("./{path}"),
_ => {
if is_scss && !path.contains(':') {
format!("./{path}")
} else {
path
}
}
}
}
fn is_style_extension(ext: &str) -> bool {
ext.eq_ignore_ascii_case("css")
|| ext.eq_ignore_ascii_case("scss")
|| ext.eq_ignore_ascii_case("sass")
|| ext.eq_ignore_ascii_case("less")
}
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()
}
}
fn normalize_css_plugin_path(path: String) -> String {
path
}
#[must_use]
pub fn extract_css_import_sources(source: &str, is_scss: bool) -> Vec<CssImportSource> {
let stripped = strip_css_comments(source, is_scss);
let mut out = Vec::new();
for cap in CSS_IMPORT_RE.captures_iter(&stripped) {
let raw = 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) = raw
&& !src.is_empty()
&& !is_css_url_import(&src)
{
out.push(CssImportSource {
normalized: normalize_css_import_path(src.clone(), is_scss),
raw: src,
is_plugin: false,
});
}
}
if is_scss {
for cap in SCSS_USE_RE.captures_iter(&stripped) {
if let Some(m) = cap.get(1) {
let raw = m.as_str().to_string();
out.push(CssImportSource {
normalized: normalize_css_import_path(raw.clone(), true),
raw,
is_plugin: false,
});
}
}
}
for cap in CSS_PLUGIN_RE.captures_iter(&stripped) {
if let Some(m) = cap.get(1) {
let raw = m.as_str().trim().to_string();
if !raw.is_empty() && !is_css_url_import(&raw) {
out.push(CssImportSource {
normalized: normalize_css_plugin_path(raw.clone()),
raw,
is_plugin: true,
});
}
}
}
out
}
#[must_use]
pub fn extract_css_imports(source: &str, is_scss: bool) -> Vec<String> {
extract_css_import_sources(source, is_scss)
.into_iter()
.map(|source| source.normalized)
.collect()
}
fn mask_with_whitespace(src: &str, re: ®ex::Regex) -> String {
let mut out = String::with_capacity(src.len());
let mut cursor = 0;
for m in re.find_iter(src) {
out.push_str(&src[cursor..m.start()]);
for _ in m.start()..m.end() {
out.push(' ');
}
cursor = m.end();
}
out.push_str(&src[cursor..]);
out
}
pub fn extract_css_module_exports(source: &str, is_scss: bool) -> Vec<ExportInfo> {
let mut masked = mask_with_whitespace(source, &CSS_COMMENT_RE);
if is_scss {
masked = mask_with_whitespace(&masked, &SCSS_LINE_COMMENT_RE);
}
masked = mask_with_whitespace(&masked, &CSS_NON_SELECTOR_RE);
masked = mask_with_whitespace(&masked, &CSS_AT_RULE_PRELUDE_RE);
let mut seen = rustc_hash::FxHashSet::default();
let mut exports = Vec::new();
for cap in CSS_CLASS_RE.captures_iter(&masked) {
if let Some(m) = cap.get(1) {
let class_name = m.as_str().to_string();
if seen.insert(class_name.clone()) {
#[expect(
clippy::cast_possible_truncation,
reason = "CSS files exceeding u32::MAX bytes are not a realistic input"
)]
let span = Span::new(m.start() as u32, m.end() as u32);
exports.push(ExportInfo {
name: ExportName::Named(class_name),
local_name: None,
is_type_only: false,
visibility: VisibilityTag::None,
span,
members: Vec::new(),
is_side_effect_used: false,
super_class: None,
});
}
}
}
exports
}
pub(crate) fn parse_css_to_module(
file_id: FileId,
path: &Path,
source: &str,
content_hash: u64,
) -> ModuleInfo {
let parsed_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,
from_style: 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,
from_style: false,
span: Span::default(),
source_span: Span::default(),
});
}
}
}
for cap in CSS_PLUGIN_RE.captures_iter(&stripped) {
if let Some(m) = cap.get(1) {
let source = m.as_str().trim().to_string();
if !source.is_empty() && !is_css_url_import(&source) {
imports.push(ImportInfo {
source: normalize_css_plugin_path(source),
imported_name: ImportedName::Default,
local_name: String::new(),
is_type_only: false,
from_style: 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,
from_style: false,
span: Span::default(),
source_span: Span::default(),
});
}
let exports = if is_css_module_file(path) {
extract_css_module_exports(source, is_scss)
} 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,
has_angular_component_template_url: false,
content_hash,
suppressions: parsed_suppressions.suppressions,
unknown_suppression_kinds: parsed_suppressions.unknown_kinds,
unused_import_bindings: Vec::new(),
type_referenced_import_bindings: Vec::new(),
value_referenced_import_bindings: Vec::new(),
line_offsets: fallow_types::extract::compute_line_offsets(source),
complexity: Vec::new(),
flag_uses: Vec::new(),
class_heritage: vec![],
local_type_declarations: Vec::new(),
public_signature_type_references: Vec::new(),
namespace_object_aliases: Vec::new(),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn export_names(source: &str) -> Vec<String> {
extract_css_module_exports(source, false)
.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 classes_inside_multi_line_media_query() {
let names =
export_names("@media\n screen and (min-width: 600px)\n{\n .real { color: red; }\n}");
assert_eq!(names, vec!["real"]);
}
#[test]
fn at_layer_statement_does_not_export() {
let names = export_names("@layer foo.bar;");
assert!(names.is_empty(), "got {names:?}");
let names = export_names("@layer foo.bar, foo.baz;");
assert!(names.is_empty(), "got {names:?}");
}
#[test]
fn at_layer_block_keeps_body_classes() {
let names = export_names("@layer foo.bar { .root { color: red; } }");
assert_eq!(names, vec!["root"]);
}
#[test]
fn at_layer_multiline_prelude_keeps_body_classes() {
let names = export_names("@layer\n foo.bar\n{ .root { color: red; } }");
assert_eq!(names, vec!["root"]);
}
#[test]
fn at_layer_with_nested_media_keeps_body() {
let names =
export_names("@layer foo.bar { @media (max-width: 768px) { .real { color: red; } } }");
assert_eq!(names, vec!["real"]);
}
#[test]
fn at_import_with_layer_attribute_does_not_export() {
let names = export_names(r#"@import url("x.css") layer(theme.button);"#);
assert!(names.is_empty(), "got {names:?}");
}
#[test]
fn class_then_at_layer_does_not_leak_prelude() {
let names =
export_names(".outer { color: blue; } @layer foo.bar { .inner { color: red; } }");
assert_eq!(names, vec!["outer", "inner"]);
}
#[test]
fn at_scope_keeps_selector_list_classes() {
let names = export_names("@scope (.parent) to (.child) { .title { color: red; } }");
assert!(names.contains(&"parent".to_string()), "got {names:?}");
assert!(names.contains(&"child".to_string()), "got {names:?}");
assert!(names.contains(&"title".to_string()), "got {names:?}");
}
#[test]
fn at_keyframes_numeric_step_is_not_class() {
let names = export_names(
"@keyframes slide { 0% { transform: scale(.5); } 100% { transform: scale(1); } }",
);
assert!(names.is_empty(), "got {names:?}");
}
#[test]
fn at_webkit_keyframes_keeps_body_classes() {
let names = export_names("@-webkit-keyframes slide { 0% { } 100% { } } .real { }");
assert_eq!(names, vec!["real"]);
}
#[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 names = export_names("/* .fake { } */ .real { }");
assert!(!names.contains(&"fake".to_string()));
assert!(names.contains(&"real".to_string()));
}
#[test]
fn ignores_classes_in_scss_line_comments() {
let exports = extract_css_module_exports("// .fake\n.real { }", true);
let names: Vec<_> = exports
.iter()
.filter_map(|e| match &e.name {
ExportName::Named(n) => Some(n.as_str()),
ExportName::Default => None,
})
.collect();
assert_eq!(names, vec!["real"]);
}
#[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_css_package_subpath_stays_bare() {
assert_eq!(
normalize_css_import_path("tailwindcss/theme.css".to_string(), false),
"tailwindcss/theme.css"
);
}
#[test]
fn normalize_css_package_subpath_with_dotted_name_stays_bare() {
assert_eq!(
normalize_css_import_path("highlight.js/styles/github.css".to_string(), false),
"highlight.js/styles/github.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());
}
#[test]
fn extract_css_imports_at_import_quoted() {
let imports = extract_css_imports(r#"@import "./reset.css";"#, false);
assert_eq!(imports, vec!["./reset.css"]);
}
#[test]
fn extract_css_imports_package_subpath_stays_bare() {
let imports =
extract_css_imports(r#"@import "tailwindcss/theme.css" layer(theme);"#, false);
assert_eq!(imports, vec!["tailwindcss/theme.css"]);
}
#[test]
fn extract_css_imports_at_import_url() {
let imports = extract_css_imports(r#"@import url("./reset.css");"#, false);
assert_eq!(imports, vec!["./reset.css"]);
}
#[test]
fn extract_css_imports_skips_remote_urls() {
let imports =
extract_css_imports(r#"@import "https://fonts.example.com/font.css";"#, false);
assert!(imports.is_empty());
}
#[test]
fn extract_css_imports_scss_use_normalizes_partial() {
let imports = extract_css_imports(r#"@use "variables";"#, true);
assert_eq!(imports, vec!["./variables"]);
}
#[test]
fn extract_css_imports_scss_forward_normalizes_partial() {
let imports = extract_css_imports(r#"@forward "tokens";"#, true);
assert_eq!(imports, vec!["./tokens"]);
}
#[test]
fn extract_css_imports_skips_comments() {
let imports = extract_css_imports(
r#"/* @import "./hidden.scss"; */
@use "real";"#,
true,
);
assert_eq!(imports, vec!["./real"]);
}
#[test]
fn extract_css_imports_at_plugin_keeps_package_bare() {
let imports = extract_css_imports(r#"@plugin "daisyui";"#, true);
assert_eq!(imports, vec!["daisyui"]);
}
#[test]
fn extract_css_imports_at_plugin_tracks_relative_file() {
let imports = extract_css_imports(r#"@plugin "./tailwind-plugin.js";"#, false);
assert_eq!(imports, vec!["./tailwind-plugin.js"]);
}
#[test]
fn extract_css_imports_scss_at_import_kept_relative() {
let imports = extract_css_imports(r"@import 'Foo';", true);
assert_eq!(imports, vec!["./Foo"]);
}
#[test]
fn extract_css_imports_additional_data_string_body() {
let body = r#"@use "./src/styles/global.scss";"#;
let imports = extract_css_imports(body, true);
assert_eq!(imports, vec!["./src/styles/global.scss"]);
}
#[test]
fn mask_with_whitespace_preserves_byte_length() {
let src = "/* hello */ .foo { }";
let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
assert_eq!(masked.len(), src.len());
assert!(masked.is_char_boundary(src.len()));
}
#[test]
fn mask_with_whitespace_preserves_offsets_around_multibyte() {
let src = "/* \u{2713} */ .foo { }";
let foo_offset = src.find(".foo").expect("`.foo` present");
let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
assert_eq!(masked.len(), src.len());
assert_eq!(masked.find(".foo"), Some(foo_offset));
}
fn span_line_col(source: &str, start: u32) -> (u32, u32) {
let offsets = fallow_types::extract::compute_line_offsets(source);
fallow_types::extract::byte_offset_to_line_col(&offsets, start)
}
#[test]
fn span_points_at_real_class_declaration_line() {
let source = "\n\n\n\n.foo { color: red; }\n";
let exports = extract_css_module_exports(source, false);
assert_eq!(exports.len(), 1);
let span = exports[0].span;
let (line, col) = span_line_col(source, span.start);
assert_eq!(line, 5, "`.foo` on line 5 must produce line 5, not line 1");
assert_eq!(
col, 1,
"column points at `f` in `.foo` (post-dot identifier)"
);
assert_eq!(
&source[span.start as usize..span.end as usize],
"foo",
"span range must slice to the class identifier in the original source"
);
}
#[test]
fn span_survives_multibyte_comment_prefix() {
let source = "/* \u{2713} */\n.foo { }";
let exports = extract_css_module_exports(source, false);
assert_eq!(exports.len(), 1);
let span = exports[0].span;
assert!(
source.is_char_boundary(span.start as usize),
"span.start must lie on a UTF-8 char boundary"
);
assert_eq!(&source[span.start as usize..span.end as usize], "foo");
}
#[test]
fn span_skips_at_layer_prelude_dot_segments() {
let source = "@layer foo.bar { }\n.root { }\n";
let exports = extract_css_module_exports(source, false);
let names: Vec<_> = exports
.iter()
.filter_map(|e| match &e.name {
ExportName::Named(n) => Some(n.as_str()),
ExportName::Default => None,
})
.collect();
assert_eq!(names, vec!["root"], "@layer sub-segments must not export");
let span = exports[0].span;
let (line, _col) = span_line_col(source, span.start);
assert_eq!(line, 2, "`.root` lives on line 2 of the original source");
assert_eq!(&source[span.start as usize..span.end as usize], "root");
}
#[test]
fn span_skips_classes_in_strings() {
let source = ".real { content: \".fake\"; }\n.also-real { }\n";
let exports = extract_css_module_exports(source, false);
let names: Vec<_> = exports
.iter()
.filter_map(|e| match &e.name {
ExportName::Named(n) => Some(n.as_str()),
ExportName::Default => None,
})
.collect();
assert_eq!(names, vec!["real", "also-real"]);
for export in &exports {
let span = export.span;
let slice = &source[span.start as usize..span.end as usize];
match &export.name {
ExportName::Named(n) => assert_eq!(slice, n.as_str()),
ExportName::Default => unreachable!("CSS modules emit only named exports"),
}
}
}
#[test]
fn span_deduplicates_to_first_occurrence() {
let source = ".btn { color: red; }\n.btn { color: blue; }\n";
let exports = extract_css_module_exports(source, false);
assert_eq!(exports.len(), 1);
let (line, _col) = span_line_col(source, exports[0].span.start);
assert_eq!(
line, 1,
"first occurrence wins for deduplicated class names"
);
}
#[test]
fn span_inside_media_query() {
let source =
"@media (max-width: 768px) {\n .mobile { display: block; }\n .desktop { }\n}\n";
let exports = extract_css_module_exports(source, false);
let by_name: rustc_hash::FxHashMap<&str, oxc_span::Span> = exports
.iter()
.filter_map(|e| match &e.name {
ExportName::Named(n) => Some((n.as_str(), e.span)),
ExportName::Default => None,
})
.collect();
let mobile_line = span_line_col(source, by_name["mobile"].start).0;
let desktop_line = span_line_col(source, by_name["desktop"].start).0;
assert_eq!(mobile_line, 2);
assert_eq!(desktop_line, 3);
}
#[test]
fn at_layer_only_module_emits_no_exports() {
let exports = extract_css_module_exports("@layer foo.bar, foo.baz;\n", false);
assert!(exports.is_empty());
}
#[test]
fn parse_css_to_module_resolves_real_line_offsets() {
let source = "\n\n\n\n.foo { color: red; }\n";
let info = parse_css_to_module(
fallow_types::discover::FileId(0),
Path::new("Component.module.css"),
source,
0,
);
assert_eq!(info.exports.len(), 1);
let (line, _col) = fallow_types::extract::byte_offset_to_line_col(
&info.line_offsets,
info.exports[0].span.start,
);
assert_eq!(line, 5, "downstream line must equal the source line");
}
}