1use std::path::Path;
15use std::sync::LazyLock;
16
17use lightningcss::rules::CssRule;
18use lightningcss::selector::{Component, PseudoClass, Selector, SelectorList};
19use lightningcss::stylesheet::{ParserOptions, StyleSheet};
20use oxc_span::Span;
21use rustc_hash::FxHashSet;
22
23use crate::{ExportInfo, ExportName, ImportInfo, ImportedName, ModuleInfo, VisibilityTag};
24use fallow_types::discover::FileId;
25
26static CSS_IMPORT_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
29 crate::static_regex(
30 r#"@import\s+(?:url\(\s*(?:["']([^"']+)["']|([^)]+))\s*\)|["']([^"']+)["'])"#,
31 )
32});
33
34static SCSS_USE_RE: LazyLock<regex::Regex> =
37 LazyLock::new(|| crate::static_regex(r#"@(?:use|forward)\s+["']([^"']+)["']"#));
38
39static CSS_PLUGIN_RE: LazyLock<regex::Regex> =
42 LazyLock::new(|| crate::static_regex(r#"@plugin\s+["']([^"']+)["']"#));
43
44static CSS_APPLY_RE: LazyLock<regex::Regex> =
47 LazyLock::new(|| crate::static_regex(r"@apply\s+[^;}\n]+"));
48
49static CSS_TAILWIND_RE: LazyLock<regex::Regex> =
52 LazyLock::new(|| crate::static_regex(r"@tailwind\s+\w+"));
53
54static CSS_COMMENT_RE: LazyLock<regex::Regex> =
56 LazyLock::new(|| crate::static_regex(r"(?s)/\*.*?\*/"));
57
58static SCSS_LINE_COMMENT_RE: LazyLock<regex::Regex> =
60 LazyLock::new(|| crate::static_regex(r"//[^\n]*"));
61
62static CSS_CLASS_RE: LazyLock<regex::Regex> =
65 LazyLock::new(|| crate::static_regex(r"\.([a-zA-Z_][a-zA-Z0-9_-]*)"));
66
67static CSS_NON_SELECTOR_RE: LazyLock<regex::Regex> =
70 LazyLock::new(|| crate::static_regex(r#"(?s)"[^"]*"|'[^']*'|url\([^)]*\)"#));
71
72static CSS_AT_RULE_PRELUDE_RE: LazyLock<regex::Regex> =
84 LazyLock::new(|| crate::static_regex(r"@(?:layer|import)\b[^;{]*"));
85
86pub(crate) fn is_css_file(path: &Path) -> bool {
87 path.extension()
88 .and_then(|e| e.to_str())
89 .is_some_and(|ext| matches!(ext, "css" | "scss" | "sass" | "less"))
90}
91
92#[derive(Debug, Clone, PartialEq, Eq)]
94pub struct CssImportSource {
95 pub raw: String,
97 pub normalized: String,
99 pub is_plugin: bool,
101 pub span: Span,
103}
104
105fn is_css_module_file(path: &Path) -> bool {
106 is_css_file(path)
107 && path
108 .file_stem()
109 .and_then(|s| s.to_str())
110 .is_some_and(|stem| stem.ends_with(".module"))
111}
112
113fn is_css_url_import(source: &str) -> bool {
115 source.starts_with("http://") || source.starts_with("https://") || source.starts_with("data:")
116}
117
118fn normalize_css_import_path(path: String, is_scss: bool) -> String {
131 if path.starts_with('.') || path.starts_with('/') || path.contains("://") {
132 return path;
133 }
134 if path.starts_with('@') && path.contains('/') {
135 return path;
136 }
137 let path_ref = std::path::Path::new(&path);
138 if !is_scss
139 && path.contains('/')
140 && path_ref
141 .extension()
142 .and_then(|e| e.to_str())
143 .is_some_and(is_style_extension)
144 {
145 return path;
146 }
147 let ext = std::path::Path::new(&path)
148 .extension()
149 .and_then(|e| e.to_str());
150 match ext {
151 Some(e) if is_style_extension(e) => format!("./{path}"),
152 _ => {
153 if is_scss && !path.contains(':') {
154 format!("./{path}")
155 } else {
156 path
157 }
158 }
159 }
160}
161
162fn is_style_extension(ext: &str) -> bool {
163 ext.eq_ignore_ascii_case("css")
164 || ext.eq_ignore_ascii_case("scss")
165 || ext.eq_ignore_ascii_case("sass")
166 || ext.eq_ignore_ascii_case("less")
167}
168
169#[cfg(test)]
171fn strip_css_comments(source: &str, is_scss: bool) -> String {
172 let stripped = CSS_COMMENT_RE.replace_all(source, "");
173 if is_scss {
174 SCSS_LINE_COMMENT_RE.replace_all(&stripped, "").into_owned()
175 } else {
176 stripped.into_owned()
177 }
178}
179
180fn mask_css_comments(source: &str, is_scss: bool) -> String {
181 let mut masked = mask_with_whitespace(source, &CSS_COMMENT_RE);
182 if is_scss {
183 masked = mask_with_whitespace(&masked, &SCSS_LINE_COMMENT_RE);
184 }
185 masked
186}
187
188fn normalize_css_plugin_path(path: String) -> String {
194 path
195}
196
197#[must_use]
207pub fn extract_css_import_sources(source: &str, is_scss: bool) -> Vec<CssImportSource> {
208 let stripped = mask_css_comments(source, is_scss);
209 let mut out = Vec::new();
210
211 for cap in CSS_IMPORT_RE.captures_iter(&stripped) {
212 let raw = cap.get(1).or_else(|| cap.get(2)).or_else(|| cap.get(3));
213 if let Some(m) = raw {
214 let (src, span) = trimmed_match_with_span(m);
215 if !src.is_empty() && !is_css_url_import(&src) {
216 out.push(CssImportSource {
217 normalized: normalize_css_import_path(src.clone(), is_scss),
218 raw: src,
219 is_plugin: false,
220 span,
221 });
222 }
223 }
224 }
225
226 if is_scss {
227 for cap in SCSS_USE_RE.captures_iter(&stripped) {
228 if let Some(m) = cap.get(1) {
229 let (raw, span) = trimmed_match_with_span(m);
230 out.push(CssImportSource {
231 normalized: normalize_css_import_path(raw.clone(), true),
232 raw,
233 is_plugin: false,
234 span,
235 });
236 }
237 }
238 }
239
240 for cap in CSS_PLUGIN_RE.captures_iter(&stripped) {
241 if let Some(m) = cap.get(1) {
242 let (raw, span) = trimmed_match_with_span(m);
243 if !raw.is_empty() && !is_css_url_import(&raw) {
244 out.push(CssImportSource {
245 normalized: normalize_css_plugin_path(raw.clone()),
246 raw,
247 is_plugin: true,
248 span,
249 });
250 }
251 }
252 }
253
254 out
255}
256
257fn trimmed_match_with_span(m: regex::Match<'_>) -> (String, Span) {
258 let raw = m.as_str();
259 let trimmed_start = raw.len() - raw.trim_start().len();
260 let trimmed_end = raw.trim_end().len();
261 let start = m.start() + trimmed_start;
262 let end = m.start() + trimmed_end;
263 (raw.trim().to_string(), Span::new(start as u32, end as u32))
264}
265
266#[must_use]
273pub fn extract_css_imports(source: &str, is_scss: bool) -> Vec<String> {
274 extract_css_import_sources(source, is_scss)
275 .into_iter()
276 .map(|source| source.normalized)
277 .collect()
278}
279
280static CSS_THEME_OPEN_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
284 crate::static_regex(r"@theme(?:\s+(?:inline|static|reference|default))*\s*\{")
285});
286
287static CSS_VAR_REF_RE: LazyLock<regex::Regex> =
291 LazyLock::new(|| crate::static_regex(r"var\(\s*--([A-Za-z0-9_-]+)"));
292
293#[derive(Debug, Clone, PartialEq, Eq)]
296pub struct ThemeTokenDef {
297 pub name: String,
299 pub line: u32,
301}
302
303#[derive(Debug, Clone, Default, PartialEq, Eq)]
305pub struct ThemeScan {
306 pub tokens: Vec<ThemeTokenDef>,
310 pub theme_var_reads: Vec<(String, u32)>,
317}
318
319#[must_use]
329pub fn scan_theme_blocks(source: &str) -> ThemeScan {
330 if !source.contains("@theme") {
332 return ThemeScan::default();
333 }
334 let masked = mask_theme_source(source);
335 let mut out = ThemeScan::default();
336 let mut seen: FxHashSet<String> = FxHashSet::default();
337 for open in CSS_THEME_OPEN_RE.find_iter(&masked) {
338 let body_start = open.end();
339 let body_end = find_theme_body_end(&masked, body_start);
340 collect_theme_declarations(
341 source,
342 &masked,
343 body_start,
344 body_end,
345 &mut out.tokens,
346 &mut seen,
347 );
348 collect_theme_var_reads(
349 source,
350 &masked,
351 body_start,
352 body_end,
353 &mut out.theme_var_reads,
354 );
355 }
356 out
357}
358
359#[must_use]
366pub fn extract_css_var_reads_located(source: &str) -> Vec<(String, u32)> {
367 if !source.contains("var(") {
368 return Vec::new();
369 }
370 let masked = mask_theme_source(source);
371 let mut theme_bodies: Vec<(usize, usize)> = Vec::new();
374 if masked.contains("@theme") {
375 for open in CSS_THEME_OPEN_RE.find_iter(&masked) {
376 let body_start = open.end();
377 let body_end = find_theme_body_end(&masked, body_start);
378 theme_bodies.push((body_start, body_end));
379 }
380 }
381 let in_theme = |offset: usize| theme_bodies.iter().any(|&(s, e)| offset >= s && offset < e);
382 let mut out = Vec::new();
383 for cap in CSS_VAR_REF_RE.captures_iter(&masked) {
384 let (Some(whole), Some(name)) = (cap.get(0), cap.get(1)) else {
385 continue;
386 };
387 if in_theme(whole.start()) {
388 continue;
389 }
390 out.push((
391 name.as_str().to_owned(),
392 line_at_offset(source, whole.start()),
393 ));
394 }
395 out
396}
397
398fn mask_theme_source(source: &str) -> String {
401 mask_with_whitespace(&mask_css_comments(source, false), &CSS_NON_SELECTOR_RE)
402}
403
404fn find_theme_body_end(masked: &str, body_start: usize) -> usize {
406 let bytes = masked.as_bytes();
407 let mut depth = 1usize;
408 let mut i = body_start;
409 while i < bytes.len() {
410 match bytes[i] {
411 b'{' => depth += 1,
412 b'}' => {
413 depth -= 1;
414 if depth == 0 {
415 break;
416 }
417 }
418 _ => {}
419 }
420 i += 1;
421 }
422 i.min(bytes.len())
423}
424
425fn collect_theme_var_reads(
426 source: &str,
427 masked: &str,
428 body_start: usize,
429 body_end: usize,
430 out: &mut Vec<(String, u32)>,
431) {
432 let Some(body) = masked.get(body_start..body_end) else {
433 return;
434 };
435 for cap in CSS_VAR_REF_RE.captures_iter(body) {
436 let (Some(whole), Some(name)) = (cap.get(0), cap.get(1)) else {
437 continue;
438 };
439 let offset = body_start + whole.start();
442 out.push((name.as_str().to_owned(), line_at_offset(source, offset)));
443 }
444}
445
446fn line_at_offset(source: &str, offset: usize) -> u32 {
449 let count = source
450 .get(..offset)
451 .map_or(0, |s| s.bytes().filter(|&b| b == b'\n').count());
452 u32::try_from(1 + count).unwrap_or(u32::MAX)
453}
454
455fn collect_theme_declarations(
461 source: &str,
462 masked: &str,
463 start: usize,
464 end: usize,
465 out: &mut Vec<ThemeTokenDef>,
466 seen: &mut FxHashSet<String>,
467) {
468 let bytes = masked.as_bytes();
469 let mut depth = 0usize;
470 let mut expect_decl = true;
471 let mut i = start;
472 while i < end {
473 let b = bytes[i];
474 match b {
475 b'{' => {
476 depth += 1;
477 expect_decl = false;
478 i += 1;
479 }
480 b'}' => {
481 depth = depth.saturating_sub(1);
482 if depth == 0 {
483 expect_decl = true;
484 }
485 i += 1;
486 }
487 b';' => {
488 if depth == 0 {
489 expect_decl = true;
490 }
491 i += 1;
492 }
493 _ if b.is_ascii_whitespace() => i += 1,
494 _ => {
495 if depth == 0 && expect_decl {
496 expect_decl = false;
497 i = scan_theme_declaration(
498 &mut ThemeDeclarationScan {
499 source,
500 masked,
501 end,
502 out,
503 seen,
504 },
505 b,
506 i,
507 );
508 } else {
509 i += 1;
510 }
511 }
512 }
513 }
514}
515
516struct ThemeDeclarationScan<'a, 'b> {
517 source: &'a str,
518 masked: &'a str,
519 end: usize,
520 out: &'b mut Vec<ThemeTokenDef>,
521 seen: &'b mut FxHashSet<String>,
522}
523
524fn scan_theme_declaration(scan: &mut ThemeDeclarationScan<'_, '_>, b: u8, i: usize) -> usize {
528 let bytes = scan.masked.as_bytes();
529 if !(b == b'-' && bytes.get(i + 1) == Some(&b'-')) {
530 return i + 1;
531 }
532 let id_start = i;
533 let mut j = i;
534 while j < scan.end {
535 let c = bytes[j];
536 if c == b'-' || c == b'_' || c.is_ascii_alphanumeric() {
537 j += 1;
538 } else {
539 break;
540 }
541 }
542 let mut k = j;
543 while k < scan.end && bytes[k].is_ascii_whitespace() {
544 k += 1;
545 }
546 if k < scan.end && bytes[k] == b':' {
548 let name = &scan.masked[id_start + 2..j];
549 if !name.is_empty() && scan.seen.insert(name.to_owned()) {
550 let line = 1 + scan
551 .source
552 .get(..id_start)
553 .map_or(0, |s| s.bytes().filter(|&x| x == b'\n').count());
554 scan.out.push(ThemeTokenDef {
555 name: name.to_owned(),
556 line: u32::try_from(line).unwrap_or(u32::MAX),
557 });
558 }
559 }
560 j
561}
562
563#[must_use]
569pub fn extract_apply_tokens(source: &str) -> Vec<String> {
570 if !source.contains("@apply") {
572 return Vec::new();
573 }
574 let masked = mask_with_whitespace(&mask_css_comments(source, false), &CSS_NON_SELECTOR_RE);
575 let mut out = Vec::new();
576 for m in CSS_APPLY_RE.find_iter(&masked) {
577 let body = m.as_str().trim_start_matches("@apply");
578 for token in body.split_whitespace() {
579 let token = token.trim_matches('!');
580 if token.is_empty() || token == "important" {
581 continue;
582 }
583 out.push(token.to_owned());
584 }
585 }
586 out
587}
588
589#[must_use]
594pub fn extract_apply_tokens_located(source: &str) -> Vec<(String, u32)> {
595 if !source.contains("@apply") {
596 return Vec::new();
597 }
598 let masked = mask_with_whitespace(&mask_css_comments(source, false), &CSS_NON_SELECTOR_RE);
599 let mut out = Vec::new();
600 for m in CSS_APPLY_RE.find_iter(&masked) {
601 let line = line_at_offset(source, m.start());
602 let body = m.as_str().trim_start_matches("@apply");
603 for token in body.split_whitespace() {
604 let token = token.trim_matches('!');
605 if token.is_empty() || token == "important" {
606 continue;
607 }
608 out.push((token.to_owned(), line));
609 }
610 }
611 out
612}
613
614fn mask_with_whitespace(src: &str, re: ®ex::Regex) -> String {
624 let mut out = String::with_capacity(src.len());
625 let mut cursor = 0;
626 for m in re.find_iter(src) {
627 out.push_str(&src[cursor..m.start()]);
628 for _ in m.start()..m.end() {
629 out.push(' ');
630 }
631 cursor = m.end();
632 }
633 out.push_str(&src[cursor..]);
634 out
635}
636
637fn lightningcss_class_set(source: &str) -> Option<FxHashSet<String>> {
654 let options = ParserOptions {
655 error_recovery: true,
658 css_modules: Some(lightningcss::css_modules::Config::default()),
664 ..ParserOptions::default()
665 };
666 let stylesheet = StyleSheet::parse(source, options).ok()?;
667 let mut classes = FxHashSet::default();
668 collect_classes_from_rules(&stylesheet.rules.0, &mut classes);
669 Some(classes)
670}
671
672fn collect_classes_from_rules(rules: &[CssRule<'_>], classes: &mut FxHashSet<String>) {
677 for rule in rules {
678 match rule {
679 CssRule::Style(style) => {
680 collect_classes_from_selector_list(&style.selectors, classes);
681 collect_classes_from_rules(&style.rules.0, classes);
682 }
683 CssRule::Media(rule) => collect_classes_from_rules(&rule.rules.0, classes),
684 CssRule::Supports(rule) => collect_classes_from_rules(&rule.rules.0, classes),
685 CssRule::Container(rule) => collect_classes_from_rules(&rule.rules.0, classes),
686 CssRule::LayerBlock(rule) => collect_classes_from_rules(&rule.rules.0, classes),
687 CssRule::MozDocument(rule) => collect_classes_from_rules(&rule.rules.0, classes),
688 CssRule::StartingStyle(rule) => collect_classes_from_rules(&rule.rules.0, classes),
689 CssRule::Nesting(rule) => {
690 collect_classes_from_selector_list(&rule.style.selectors, classes);
691 collect_classes_from_rules(&rule.style.rules.0, classes);
692 }
693 CssRule::Scope(rule) => {
694 if let Some(scope_start) = &rule.scope_start {
695 collect_classes_from_selector_list(scope_start, classes);
696 }
697 if let Some(scope_end) = &rule.scope_end {
698 collect_classes_from_selector_list(scope_end, classes);
699 }
700 collect_classes_from_rules(&rule.rules.0, classes);
701 }
702 _ => {}
703 }
704 }
705}
706
707fn collect_classes_from_selector_list(list: &SelectorList<'_>, classes: &mut FxHashSet<String>) {
708 for selector in &list.0 {
709 collect_classes_from_selector(selector, classes);
710 }
711}
712
713fn collect_classes_from_selector(selector: &Selector<'_>, classes: &mut FxHashSet<String>) {
714 for component in selector.iter_raw_match_order() {
715 match component {
716 Component::Class(name) => {
717 classes.insert(name.0.to_string());
718 }
719 Component::Is(list)
720 | Component::Where(list)
721 | Component::Has(list)
722 | Component::Negation(list)
723 | Component::Any(_, list) => {
724 for nested in list.as_ref() {
725 collect_classes_from_selector(nested, classes);
726 }
727 }
728 Component::Slotted(nested) | Component::Host(Some(nested)) => {
729 collect_classes_from_selector(nested, classes);
730 }
731 Component::NthOf(data) => {
732 for nested in data.selectors() {
733 collect_classes_from_selector(nested, classes);
734 }
735 }
736 Component::NonTSPseudoClass(
738 PseudoClass::Local { selector } | PseudoClass::Global { selector },
739 ) => collect_classes_from_selector(selector, classes),
740 _ => {}
741 }
742 }
743}
744
745pub fn extract_css_module_exports(source: &str, is_scss: bool) -> Vec<ExportInfo> {
755 if !is_scss && let Some(class_set) = lightningcss_class_set(source) {
756 return scan_css_module_exports(source, is_scss, Some(&class_set));
757 }
758 scan_css_module_exports(source, is_scss, None)
759}
760
761fn scan_css_module_exports(
771 source: &str,
772 is_scss: bool,
773 class_filter: Option<&FxHashSet<String>>,
774) -> Vec<ExportInfo> {
775 let masked = mask_css_module_class_candidates(source, is_scss, class_filter.is_some());
776 let mut seen = FxHashSet::default();
777 let mut exports = Vec::new();
778 for cap in CSS_CLASS_RE.captures_iter(&masked) {
779 if let Some(m) = cap.get(1) {
780 push_css_class_export(m, class_filter, &mut seen, &mut exports);
781 }
782 }
783 exports
784}
785
786fn mask_css_module_class_candidates(source: &str, is_scss: bool, has_class_filter: bool) -> String {
787 let mut masked = mask_with_whitespace(source, &CSS_COMMENT_RE);
788 if is_scss {
789 masked = mask_with_whitespace(&masked, &SCSS_LINE_COMMENT_RE);
790 }
791 masked = mask_with_whitespace(&masked, &CSS_NON_SELECTOR_RE);
792 if !has_class_filter {
793 masked = mask_with_whitespace(&masked, &CSS_AT_RULE_PRELUDE_RE);
794 }
795 masked
796}
797
798fn push_css_class_export(
799 class_match: regex::Match<'_>,
800 class_filter: Option<&FxHashSet<String>>,
801 seen: &mut FxHashSet<String>,
802 exports: &mut Vec<ExportInfo>,
803) {
804 let class_name = class_match.as_str().to_string();
805 if class_filter.is_some_and(|filter| !filter.contains(&class_name)) {
806 return;
807 }
808 if seen.insert(class_name.clone()) {
809 exports.push(css_class_export(class_name, class_match));
810 }
811}
812
813fn css_class_export(class_name: String, class_match: regex::Match<'_>) -> ExportInfo {
814 #[expect(
815 clippy::cast_possible_truncation,
816 reason = "CSS files exceeding u32::MAX bytes are not a realistic input"
817 )]
818 let span = Span::new(class_match.start() as u32, class_match.end() as u32);
819 ExportInfo {
820 name: ExportName::Named(class_name),
821 local_name: None,
822 is_type_only: false,
823 visibility: VisibilityTag::None,
824 expected_unused_reason: None,
825 span,
826 members: Vec::new(),
827 is_side_effect_used: false,
828 super_class: None,
829 }
830}
831
832fn build_css_imports(source: &str, stripped: &str, is_scss: bool) -> Vec<ImportInfo> {
836 let mut imports = Vec::new();
837
838 for css_source in extract_css_import_sources(source, is_scss) {
839 imports.push(ImportInfo {
840 source: css_source.normalized,
841 imported_name: if css_source.is_plugin {
842 ImportedName::Default
843 } else {
844 ImportedName::SideEffect
845 },
846 local_name: String::new(),
847 is_type_only: false,
848 from_style: false,
849 span: css_source.span,
850 source_span: css_source.span,
851 });
852 }
853
854 let has_apply = CSS_APPLY_RE.is_match(stripped);
855 let has_tailwind = CSS_TAILWIND_RE.is_match(stripped);
856 if has_apply || has_tailwind {
857 imports.push(ImportInfo {
858 source: "tailwindcss".to_string(),
859 imported_name: ImportedName::SideEffect,
860 local_name: String::new(),
861 is_type_only: false,
862 from_style: false,
863 span: Span::default(),
864 source_span: Span::default(),
865 });
866 }
867
868 imports
869}
870
871pub(crate) fn parse_css_to_module(
873 file_id: FileId,
874 path: &Path,
875 source: &str,
876 content_hash: u64,
877) -> ModuleInfo {
878 let parsed_suppressions = crate::suppress::parse_suppressions_from_source(source);
879 let is_scss = path
880 .extension()
881 .and_then(|e| e.to_str())
882 .is_some_and(|ext| matches!(ext, "scss" | "sass" | "less"));
883
884 let stripped = mask_css_comments(source, is_scss);
885 let imports = build_css_imports(source, &stripped, is_scss);
886
887 let exports = if is_css_module_file(path) {
888 extract_css_module_exports(source, is_scss)
889 } else {
890 Vec::new()
891 };
892
893 css_module_info(
894 file_id,
895 content_hash,
896 source,
897 parsed_suppressions,
898 imports,
899 exports,
900 )
901}
902
903fn css_module_info(
907 file_id: FileId,
908 content_hash: u64,
909 source: &str,
910 parsed_suppressions: crate::suppress::ParsedSuppressions,
911 imports: Vec<ImportInfo>,
912 exports: Vec<ExportInfo>,
913) -> ModuleInfo {
914 crate::module_info::non_js_module_info(
915 file_id,
916 content_hash,
917 source,
918 parsed_suppressions,
919 imports,
920 exports,
921 )
922}
923
924#[cfg(all(test, not(miri)))]
925mod tests {
926 use super::*;
927
928 fn export_names(source: &str) -> Vec<String> {
930 extract_css_module_exports(source, false)
931 .into_iter()
932 .filter_map(|e| match e.name {
933 ExportName::Named(n) => Some(n),
934 ExportName::Default => None,
935 })
936 .collect()
937 }
938
939 #[test]
940 fn is_css_file_css() {
941 assert!(is_css_file(Path::new("styles.css")));
942 }
943
944 #[test]
945 fn is_css_file_scss() {
946 assert!(is_css_file(Path::new("styles.scss")));
947 }
948
949 #[test]
950 fn is_css_file_sass() {
951 assert!(is_css_file(Path::new("styles.sass")));
952 }
953
954 #[test]
955 fn is_css_file_less() {
956 assert!(is_css_file(Path::new("styles.less")));
957 }
958
959 #[test]
960 fn is_css_file_rejects_js() {
961 assert!(!is_css_file(Path::new("app.js")));
962 }
963
964 #[test]
965 fn is_css_file_rejects_ts() {
966 assert!(!is_css_file(Path::new("app.ts")));
967 }
968
969 #[test]
970 fn is_css_file_rejects_no_extension() {
971 assert!(!is_css_file(Path::new("Makefile")));
972 }
973
974 #[test]
975 fn is_css_module_file_module_css() {
976 assert!(is_css_module_file(Path::new("Component.module.css")));
977 }
978
979 #[test]
980 fn is_css_module_file_module_scss() {
981 assert!(is_css_module_file(Path::new("Component.module.scss")));
982 }
983
984 #[test]
985 fn is_css_module_file_rejects_plain_css() {
986 assert!(!is_css_module_file(Path::new("styles.css")));
987 }
988
989 #[test]
990 fn is_css_module_file_rejects_plain_scss() {
991 assert!(!is_css_module_file(Path::new("styles.scss")));
992 }
993
994 #[test]
995 fn is_css_module_file_rejects_module_js() {
996 assert!(!is_css_module_file(Path::new("utils.module.js")));
997 }
998
999 #[test]
1000 fn extracts_single_class() {
1001 let names = export_names(".foo { color: red; }");
1002 assert_eq!(names, vec!["foo"]);
1003 }
1004
1005 #[test]
1006 fn extracts_multiple_classes() {
1007 let names = export_names(".foo { } .bar { }");
1008 assert_eq!(names, vec!["foo", "bar"]);
1009 }
1010
1011 #[test]
1012 fn extracts_nested_classes() {
1013 let names = export_names(".foo .bar { color: red; }");
1014 assert!(names.contains(&"foo".to_string()));
1015 assert!(names.contains(&"bar".to_string()));
1016 }
1017
1018 #[test]
1019 fn extracts_hyphenated_class() {
1020 let names = export_names(".my-class { }");
1021 assert_eq!(names, vec!["my-class"]);
1022 }
1023
1024 #[test]
1025 fn extracts_camel_case_class() {
1026 let names = export_names(".myClass { }");
1027 assert_eq!(names, vec!["myClass"]);
1028 }
1029
1030 #[test]
1031 fn extracts_class_inside_global_pseudo() {
1032 let names = export_names(":global(.globalClass) { color: red; }");
1035 assert_eq!(names, vec!["globalClass"]);
1036 }
1037
1038 #[test]
1039 fn extracts_class_inside_local_pseudo() {
1040 let names = export_names(":local(.localClass) { color: red; }");
1041 assert_eq!(names, vec!["localClass"]);
1042 }
1043
1044 #[test]
1045 fn extracts_classes_inside_negation() {
1046 let names = export_names(".btn:not(.disabled) { }");
1047 assert!(names.contains(&"btn".to_string()), "got {names:?}");
1048 assert!(names.contains(&"disabled".to_string()), "got {names:?}");
1049 }
1050
1051 #[test]
1052 fn extracts_classes_inside_is_and_where() {
1053 let names = export_names(":is(.a, .b) :where(.c) { }");
1054 for expected in ["a", "b", "c"] {
1055 assert!(
1056 names.contains(&expected.to_string()),
1057 "missing {expected} in {names:?}"
1058 );
1059 }
1060 }
1061
1062 #[test]
1063 fn extracts_underscore_class() {
1064 let names = export_names("._hidden { } .__wrapper { }");
1065 assert!(names.contains(&"_hidden".to_string()));
1066 assert!(names.contains(&"__wrapper".to_string()));
1067 }
1068
1069 #[test]
1070 fn pseudo_selector_hover() {
1071 let names = export_names(".foo:hover { color: blue; }");
1072 assert_eq!(names, vec!["foo"]);
1073 }
1074
1075 #[test]
1076 fn pseudo_selector_focus() {
1077 let names = export_names(".input:focus { outline: none; }");
1078 assert_eq!(names, vec!["input"]);
1079 }
1080
1081 #[test]
1082 fn pseudo_element_before() {
1083 let names = export_names(".icon::before { content: ''; }");
1084 assert_eq!(names, vec!["icon"]);
1085 }
1086
1087 #[test]
1088 fn combined_pseudo_selectors() {
1089 let names = export_names(".btn:hover, .btn:active, .btn:focus { }");
1090 assert_eq!(names, vec!["btn"]);
1091 }
1092
1093 #[test]
1094 fn classes_inside_media_query() {
1095 let names = export_names(
1096 "@media (max-width: 768px) { .mobile-nav { display: block; } .desktop-nav { display: none; } }",
1097 );
1098 assert!(names.contains(&"mobile-nav".to_string()));
1099 assert!(names.contains(&"desktop-nav".to_string()));
1100 }
1101
1102 #[test]
1103 fn classes_inside_multi_line_media_query() {
1104 let names =
1105 export_names("@media\n screen and (min-width: 600px)\n{\n .real { color: red; }\n}");
1106 assert_eq!(names, vec!["real"]);
1107 }
1108
1109 #[test]
1110 fn at_layer_statement_does_not_export() {
1111 let names = export_names("@layer foo.bar;");
1112 assert!(names.is_empty(), "got {names:?}");
1113 let names = export_names("@layer foo.bar, foo.baz;");
1114 assert!(names.is_empty(), "got {names:?}");
1115 }
1116
1117 #[test]
1118 fn at_layer_block_keeps_body_classes() {
1119 let names = export_names("@layer foo.bar { .root { color: red; } }");
1120 assert_eq!(names, vec!["root"]);
1121 }
1122
1123 #[test]
1124 fn at_layer_multiline_prelude_keeps_body_classes() {
1125 let names = export_names("@layer\n foo.bar\n{ .root { color: red; } }");
1126 assert_eq!(names, vec!["root"]);
1127 }
1128
1129 #[test]
1130 fn at_layer_with_nested_media_keeps_body() {
1131 let names =
1132 export_names("@layer foo.bar { @media (max-width: 768px) { .real { color: red; } } }");
1133 assert_eq!(names, vec!["real"]);
1134 }
1135
1136 #[test]
1137 fn at_import_with_layer_attribute_does_not_export() {
1138 let names = export_names(r#"@import url("x.css") layer(theme.button);"#);
1139 assert!(names.is_empty(), "got {names:?}");
1140 }
1141
1142 #[test]
1143 fn class_then_at_layer_does_not_leak_prelude() {
1144 let names =
1145 export_names(".outer { color: blue; } @layer foo.bar { .inner { color: red; } }");
1146 assert_eq!(names, vec!["outer", "inner"]);
1147 }
1148
1149 #[test]
1150 fn at_scope_keeps_selector_list_classes() {
1151 let names = export_names("@scope (.parent) to (.child) { .title { color: red; } }");
1152 assert!(names.contains(&"parent".to_string()), "got {names:?}");
1153 assert!(names.contains(&"child".to_string()), "got {names:?}");
1154 assert!(names.contains(&"title".to_string()), "got {names:?}");
1155 }
1156
1157 #[test]
1158 fn at_keyframes_numeric_step_is_not_class() {
1159 let names = export_names(
1160 "@keyframes slide { 0% { transform: scale(.5); } 100% { transform: scale(1); } }",
1161 );
1162 assert!(names.is_empty(), "got {names:?}");
1163 }
1164
1165 #[test]
1166 fn at_webkit_keyframes_keeps_body_classes() {
1167 let names = export_names("@-webkit-keyframes slide { 0% { } 100% { } } .real { }");
1168 assert_eq!(names, vec!["real"]);
1169 }
1170
1171 #[test]
1172 fn deduplicates_repeated_class() {
1173 let names = export_names(".btn { color: red; } .btn { font-size: 14px; }");
1174 assert_eq!(names.iter().filter(|n| *n == "btn").count(), 1);
1175 }
1176
1177 #[test]
1178 fn empty_source() {
1179 let names = export_names("");
1180 assert!(names.is_empty());
1181 }
1182
1183 #[test]
1184 fn no_classes() {
1185 let names = export_names("body { margin: 0; } * { box-sizing: border-box; }");
1186 assert!(names.is_empty());
1187 }
1188
1189 #[test]
1190 fn ignores_classes_in_block_comments() {
1191 let names = export_names("/* .fake { } */ .real { }");
1192 assert!(!names.contains(&"fake".to_string()));
1193 assert!(names.contains(&"real".to_string()));
1194 }
1195
1196 #[test]
1197 fn ignores_classes_in_scss_line_comments() {
1198 let exports = extract_css_module_exports("// .fake\n.real { }", true);
1199 let names: Vec<_> = exports
1200 .iter()
1201 .filter_map(|e| match &e.name {
1202 ExportName::Named(n) => Some(n.as_str()),
1203 ExportName::Default => None,
1204 })
1205 .collect();
1206 assert_eq!(names, vec!["real"]);
1207 }
1208
1209 #[test]
1210 fn ignores_classes_in_strings() {
1211 let names = export_names(r#".real { content: ".fake"; }"#);
1212 assert!(names.contains(&"real".to_string()));
1213 assert!(!names.contains(&"fake".to_string()));
1214 }
1215
1216 #[test]
1217 fn ignores_classes_in_url() {
1218 let names = export_names(".real { background: url(./images/hero.png); }");
1219 assert!(names.contains(&"real".to_string()));
1220 assert!(!names.contains(&"png".to_string()));
1221 }
1222
1223 #[test]
1224 fn strip_css_block_comment() {
1225 let result = strip_css_comments("/* removed */ .kept { }", false);
1226 assert!(!result.contains("removed"));
1227 assert!(result.contains(".kept"));
1228 }
1229
1230 #[test]
1231 fn strip_scss_line_comment() {
1232 let result = strip_css_comments("// removed\n.kept { }", true);
1233 assert!(!result.contains("removed"));
1234 assert!(result.contains(".kept"));
1235 }
1236
1237 #[test]
1238 fn strip_scss_preserves_css_outside_comments() {
1239 let source = "// line comment\n/* block comment */\n.visible { color: red; }";
1240 let result = strip_css_comments(source, true);
1241 assert!(result.contains(".visible"));
1242 }
1243
1244 #[test]
1245 fn url_import_http() {
1246 assert!(is_css_url_import("http://example.com/style.css"));
1247 }
1248
1249 #[test]
1250 fn url_import_https() {
1251 assert!(is_css_url_import("https://fonts.googleapis.com/css"));
1252 }
1253
1254 #[test]
1255 fn url_import_data() {
1256 assert!(is_css_url_import("data:text/css;base64,abc"));
1257 }
1258
1259 #[test]
1260 fn url_import_local_not_skipped() {
1261 assert!(!is_css_url_import("./local.css"));
1262 }
1263
1264 #[test]
1265 fn url_import_bare_specifier_not_skipped() {
1266 assert!(!is_css_url_import("tailwindcss"));
1267 }
1268
1269 #[test]
1270 fn normalize_relative_dot_path_unchanged() {
1271 assert_eq!(
1272 normalize_css_import_path("./reset.css".to_string(), false),
1273 "./reset.css"
1274 );
1275 }
1276
1277 #[test]
1278 fn normalize_parent_relative_path_unchanged() {
1279 assert_eq!(
1280 normalize_css_import_path("../shared.scss".to_string(), false),
1281 "../shared.scss"
1282 );
1283 }
1284
1285 #[test]
1286 fn normalize_absolute_path_unchanged() {
1287 assert_eq!(
1288 normalize_css_import_path("/styles/main.css".to_string(), false),
1289 "/styles/main.css"
1290 );
1291 }
1292
1293 #[test]
1294 fn normalize_url_unchanged() {
1295 assert_eq!(
1296 normalize_css_import_path("https://example.com/style.css".to_string(), false),
1297 "https://example.com/style.css"
1298 );
1299 }
1300
1301 #[test]
1302 fn normalize_bare_css_gets_dot_slash() {
1303 assert_eq!(
1304 normalize_css_import_path("app.css".to_string(), false),
1305 "./app.css"
1306 );
1307 }
1308
1309 #[test]
1310 fn normalize_css_package_subpath_stays_bare() {
1311 assert_eq!(
1312 normalize_css_import_path("tailwindcss/theme.css".to_string(), false),
1313 "tailwindcss/theme.css"
1314 );
1315 }
1316
1317 #[test]
1318 fn normalize_css_package_subpath_with_dotted_name_stays_bare() {
1319 assert_eq!(
1320 normalize_css_import_path("highlight.js/styles/github.css".to_string(), false),
1321 "highlight.js/styles/github.css"
1322 );
1323 }
1324
1325 #[test]
1326 fn normalize_bare_scss_gets_dot_slash() {
1327 assert_eq!(
1328 normalize_css_import_path("vars.scss".to_string(), false),
1329 "./vars.scss"
1330 );
1331 }
1332
1333 #[test]
1334 fn normalize_bare_sass_gets_dot_slash() {
1335 assert_eq!(
1336 normalize_css_import_path("main.sass".to_string(), false),
1337 "./main.sass"
1338 );
1339 }
1340
1341 #[test]
1342 fn normalize_bare_less_gets_dot_slash() {
1343 assert_eq!(
1344 normalize_css_import_path("theme.less".to_string(), false),
1345 "./theme.less"
1346 );
1347 }
1348
1349 #[test]
1350 fn normalize_bare_js_extension_stays_bare() {
1351 assert_eq!(
1352 normalize_css_import_path("module.js".to_string(), false),
1353 "module.js"
1354 );
1355 }
1356
1357 #[test]
1358 fn normalize_scss_bare_partial_gets_dot_slash() {
1359 assert_eq!(
1360 normalize_css_import_path("variables".to_string(), true),
1361 "./variables"
1362 );
1363 }
1364
1365 #[test]
1366 fn normalize_scss_bare_partial_with_subdir_gets_dot_slash() {
1367 assert_eq!(
1368 normalize_css_import_path("base/reset".to_string(), true),
1369 "./base/reset"
1370 );
1371 }
1372
1373 #[test]
1374 fn normalize_scss_builtin_stays_bare() {
1375 assert_eq!(
1376 normalize_css_import_path("sass:math".to_string(), true),
1377 "sass:math"
1378 );
1379 }
1380
1381 #[test]
1382 fn normalize_scss_relative_path_unchanged() {
1383 assert_eq!(
1384 normalize_css_import_path("../styles/variables".to_string(), true),
1385 "../styles/variables"
1386 );
1387 }
1388
1389 #[test]
1390 fn normalize_css_bare_extensionless_stays_bare() {
1391 assert_eq!(
1392 normalize_css_import_path("tailwindcss".to_string(), false),
1393 "tailwindcss"
1394 );
1395 }
1396
1397 #[test]
1398 fn normalize_scoped_package_with_css_extension_stays_bare() {
1399 assert_eq!(
1400 normalize_css_import_path("@fontsource/monaspace-neon/400.css".to_string(), false),
1401 "@fontsource/monaspace-neon/400.css"
1402 );
1403 }
1404
1405 #[test]
1406 fn normalize_scoped_package_with_scss_extension_stays_bare() {
1407 assert_eq!(
1408 normalize_css_import_path("@company/design-system/tokens.scss".to_string(), true),
1409 "@company/design-system/tokens.scss"
1410 );
1411 }
1412
1413 #[test]
1414 fn normalize_scoped_package_without_extension_stays_bare() {
1415 assert_eq!(
1416 normalize_css_import_path("@fallow/design-system/styles".to_string(), false),
1417 "@fallow/design-system/styles"
1418 );
1419 }
1420
1421 #[test]
1422 fn normalize_scoped_package_extensionless_scss_stays_bare() {
1423 assert_eq!(
1424 normalize_css_import_path("@company/tokens".to_string(), true),
1425 "@company/tokens"
1426 );
1427 }
1428
1429 #[test]
1430 fn normalize_path_alias_with_css_extension_stays_bare() {
1431 assert_eq!(
1432 normalize_css_import_path("@/components/Button.css".to_string(), false),
1433 "@/components/Button.css"
1434 );
1435 }
1436
1437 #[test]
1438 fn normalize_path_alias_extensionless_stays_bare() {
1439 assert_eq!(
1440 normalize_css_import_path("@/styles/variables".to_string(), false),
1441 "@/styles/variables"
1442 );
1443 }
1444
1445 #[test]
1446 fn strip_css_no_comments() {
1447 let source = ".foo { color: red; }";
1448 assert_eq!(strip_css_comments(source, false), source);
1449 }
1450
1451 #[test]
1452 fn strip_css_multiple_block_comments() {
1453 let source = "/* comment-one */ .foo { } /* comment-two */ .bar { }";
1454 let result = strip_css_comments(source, false);
1455 assert!(!result.contains("comment-one"));
1456 assert!(!result.contains("comment-two"));
1457 assert!(result.contains(".foo"));
1458 assert!(result.contains(".bar"));
1459 }
1460
1461 #[test]
1462 fn strip_scss_does_not_affect_non_scss() {
1463 let source = "// this stays\n.foo { }";
1464 let result = strip_css_comments(source, false);
1465 assert!(result.contains("// this stays"));
1466 }
1467
1468 #[test]
1469 fn css_module_parses_suppressions() {
1470 let info = parse_css_to_module(
1471 fallow_types::discover::FileId(0),
1472 Path::new("Component.module.css"),
1473 "/* fallow-ignore-file */\n.btn { color: red; }",
1474 0,
1475 );
1476 assert!(!info.suppressions.is_empty());
1477 assert_eq!(info.suppressions[0].line, 0);
1478 }
1479
1480 #[test]
1481 fn extracts_class_starting_with_underscore() {
1482 let names = export_names("._private { } .__dunder { }");
1483 assert!(names.contains(&"_private".to_string()));
1484 assert!(names.contains(&"__dunder".to_string()));
1485 }
1486
1487 #[test]
1488 fn ignores_id_selectors() {
1489 let names = export_names("#myId { color: red; }");
1490 assert!(!names.contains(&"myId".to_string()));
1491 }
1492
1493 #[test]
1494 fn ignores_element_selectors() {
1495 let names = export_names("div { color: red; } span { }");
1496 assert!(names.is_empty());
1497 }
1498
1499 #[test]
1500 fn extract_css_imports_at_import_quoted() {
1501 let imports = extract_css_imports(r#"@import "./reset.css";"#, false);
1502 assert_eq!(imports, vec!["./reset.css"]);
1503 }
1504
1505 #[test]
1506 fn extract_css_imports_package_subpath_stays_bare() {
1507 let imports =
1508 extract_css_imports(r#"@import "tailwindcss/theme.css" layer(theme);"#, false);
1509 assert_eq!(imports, vec!["tailwindcss/theme.css"]);
1510 }
1511
1512 #[test]
1513 fn extract_css_imports_at_import_url() {
1514 let imports = extract_css_imports(r#"@import url("./reset.css");"#, false);
1515 assert_eq!(imports, vec!["./reset.css"]);
1516 }
1517
1518 #[test]
1519 fn extract_css_imports_skips_remote_urls() {
1520 let imports =
1521 extract_css_imports(r#"@import "https://fonts.example.com/font.css";"#, false);
1522 assert!(imports.is_empty());
1523 }
1524
1525 #[test]
1526 fn extract_css_imports_scss_use_normalizes_partial() {
1527 let imports = extract_css_imports(r#"@use "variables";"#, true);
1528 assert_eq!(imports, vec!["./variables"]);
1529 }
1530
1531 #[test]
1532 fn extract_css_imports_scss_forward_normalizes_partial() {
1533 let imports = extract_css_imports(r#"@forward "tokens";"#, true);
1534 assert_eq!(imports, vec!["./tokens"]);
1535 }
1536
1537 #[test]
1538 fn extract_css_imports_skips_comments() {
1539 let imports = extract_css_imports(
1540 r#"/* @import "./hidden.scss"; */
1541@use "real";"#,
1542 true,
1543 );
1544 assert_eq!(imports, vec!["./real"]);
1545 }
1546
1547 #[test]
1548 fn extract_css_imports_at_plugin_keeps_package_bare() {
1549 let imports = extract_css_imports(r#"@plugin "daisyui";"#, true);
1550 assert_eq!(imports, vec!["daisyui"]);
1551 }
1552
1553 #[test]
1554 fn extract_css_imports_at_plugin_tracks_relative_file() {
1555 let imports = extract_css_imports(r#"@plugin "./tailwind-plugin.js";"#, false);
1556 assert_eq!(imports, vec!["./tailwind-plugin.js"]);
1557 }
1558
1559 #[test]
1560 fn extract_css_imports_scss_at_import_kept_relative() {
1561 let imports = extract_css_imports(r"@import 'Foo';", true);
1562 assert_eq!(imports, vec!["./Foo"]);
1563 }
1564
1565 #[test]
1566 fn extract_css_imports_additional_data_string_body() {
1567 let body = r#"@use "./src/styles/global.scss";"#;
1568 let imports = extract_css_imports(body, true);
1569 assert_eq!(imports, vec!["./src/styles/global.scss"]);
1570 }
1571
1572 #[test]
1573 fn mask_with_whitespace_preserves_byte_length() {
1574 let src = "/* hello */ .foo { }";
1575 let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
1576 assert_eq!(masked.len(), src.len());
1577 assert!(masked.is_char_boundary(src.len()));
1578 }
1579
1580 #[test]
1581 fn mask_with_whitespace_preserves_offsets_around_multibyte() {
1582 let src = "/* \u{2713} */ .foo { }";
1583 let foo_offset = src.find(".foo").expect("`.foo` present");
1584 let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
1585 assert_eq!(masked.len(), src.len());
1586 assert_eq!(masked.find(".foo"), Some(foo_offset));
1587 }
1588
1589 fn span_line_col(source: &str, start: u32) -> (u32, u32) {
1592 let offsets = fallow_types::extract::compute_line_offsets(source);
1593 fallow_types::extract::byte_offset_to_line_col(&offsets, start)
1594 }
1595
1596 #[test]
1597 fn span_points_at_real_class_declaration_line() {
1598 let source = "\n\n\n\n.foo { color: red; }\n";
1599 let exports = extract_css_module_exports(source, false);
1600 assert_eq!(exports.len(), 1);
1601 let span = exports[0].span;
1602 let (line, col) = span_line_col(source, span.start);
1603 assert_eq!(line, 5, "`.foo` on line 5 must produce line 5, not line 1");
1604 assert_eq!(
1605 col, 1,
1606 "column points at `f` in `.foo` (post-dot identifier)"
1607 );
1608 assert_eq!(
1609 &source[span.start as usize..span.end as usize],
1610 "foo",
1611 "span range must slice to the class identifier in the original source"
1612 );
1613 }
1614
1615 #[test]
1616 fn span_survives_multibyte_comment_prefix() {
1617 let source = "/* \u{2713} */\n.foo { }";
1618 let exports = extract_css_module_exports(source, false);
1619 assert_eq!(exports.len(), 1);
1620 let span = exports[0].span;
1621 assert!(
1622 source.is_char_boundary(span.start as usize),
1623 "span.start must lie on a UTF-8 char boundary"
1624 );
1625 assert_eq!(&source[span.start as usize..span.end as usize], "foo");
1626 }
1627
1628 #[test]
1629 fn span_skips_at_layer_prelude_dot_segments() {
1630 let source = "@layer foo.bar { }\n.root { }\n";
1631 let exports = extract_css_module_exports(source, false);
1632 let names: Vec<_> = exports
1633 .iter()
1634 .filter_map(|e| match &e.name {
1635 ExportName::Named(n) => Some(n.as_str()),
1636 ExportName::Default => None,
1637 })
1638 .collect();
1639 assert_eq!(names, vec!["root"], "@layer sub-segments must not export");
1640 let span = exports[0].span;
1641 let (line, _col) = span_line_col(source, span.start);
1642 assert_eq!(line, 2, "`.root` lives on line 2 of the original source");
1643 assert_eq!(&source[span.start as usize..span.end as usize], "root");
1644 }
1645
1646 #[test]
1647 fn span_skips_classes_in_strings() {
1648 let source = ".real { content: \".fake\"; }\n.also-real { }\n";
1649 let exports = extract_css_module_exports(source, false);
1650 let names: Vec<_> = exports
1651 .iter()
1652 .filter_map(|e| match &e.name {
1653 ExportName::Named(n) => Some(n.as_str()),
1654 ExportName::Default => None,
1655 })
1656 .collect();
1657 assert_eq!(names, vec!["real", "also-real"]);
1658 for export in &exports {
1659 let span = export.span;
1660 let slice = &source[span.start as usize..span.end as usize];
1661 match &export.name {
1662 ExportName::Named(n) => assert_eq!(slice, n.as_str()),
1663 ExportName::Default => unreachable!("CSS modules emit only named exports"),
1664 }
1665 }
1666 }
1667
1668 #[test]
1669 fn span_deduplicates_to_first_occurrence() {
1670 let source = ".btn { color: red; }\n.btn { color: blue; }\n";
1671 let exports = extract_css_module_exports(source, false);
1672 assert_eq!(exports.len(), 1);
1673 let (line, _col) = span_line_col(source, exports[0].span.start);
1674 assert_eq!(
1675 line, 1,
1676 "first occurrence wins for deduplicated class names"
1677 );
1678 }
1679
1680 #[test]
1681 fn span_inside_media_query() {
1682 let source =
1683 "@media (max-width: 768px) {\n .mobile { display: block; }\n .desktop { }\n}\n";
1684 let exports = extract_css_module_exports(source, false);
1685 let by_name: rustc_hash::FxHashMap<&str, oxc_span::Span> = exports
1686 .iter()
1687 .filter_map(|e| match &e.name {
1688 ExportName::Named(n) => Some((n.as_str(), e.span)),
1689 ExportName::Default => None,
1690 })
1691 .collect();
1692 let mobile_line = span_line_col(source, by_name["mobile"].start).0;
1693 let desktop_line = span_line_col(source, by_name["desktop"].start).0;
1694 assert_eq!(mobile_line, 2);
1695 assert_eq!(desktop_line, 3);
1696 }
1697
1698 #[test]
1699 fn at_layer_only_module_emits_no_exports() {
1700 let exports = extract_css_module_exports("@layer foo.bar, foo.baz;\n", false);
1701 assert!(exports.is_empty());
1702 }
1703
1704 #[test]
1705 fn parse_css_to_module_resolves_real_line_offsets() {
1706 let source = "\n\n\n\n.foo { color: red; }\n";
1707 let info = parse_css_to_module(
1708 fallow_types::discover::FileId(0),
1709 Path::new("Component.module.css"),
1710 source,
1711 0,
1712 );
1713 assert_eq!(info.exports.len(), 1);
1714 let (line, _col) = fallow_types::extract::byte_offset_to_line_col(
1715 &info.line_offsets,
1716 info.exports[0].span.start,
1717 );
1718 assert_eq!(line, 5, "downstream line must equal the source line");
1719 }
1720
1721 fn theme_token_names(source: &str) -> Vec<String> {
1722 scan_theme_blocks(source)
1723 .tokens
1724 .into_iter()
1725 .map(|t| t.name)
1726 .collect()
1727 }
1728
1729 #[test]
1730 fn theme_single_block_collects_tokens() {
1731 let names = theme_token_names("@theme { --color-brand: #f00; --radius-card: 8px; }");
1732 assert_eq!(names, vec!["color-brand", "radius-card"]);
1733 }
1734
1735 #[test]
1736 fn theme_dashed_multi_segment_names() {
1737 let names = theme_token_names(
1738 "@theme {\n --font-weight-heavy: 900;\n --inset-shadow-glow: 0 0 4px red;\n}",
1739 );
1740 assert_eq!(names, vec!["font-weight-heavy", "inset-shadow-glow"]);
1741 }
1742
1743 #[test]
1744 fn theme_inline_and_static_modifiers() {
1745 assert_eq!(
1746 theme_token_names("@theme inline { --color-a: red; }"),
1747 vec!["color-a"]
1748 );
1749 assert_eq!(
1750 theme_token_names("@theme static { --color-b: red; }"),
1751 vec!["color-b"]
1752 );
1753 }
1754
1755 #[test]
1756 fn theme_multiple_blocks_union() {
1757 let names = theme_token_names(
1758 "@theme { --color-a: red; }\n.x { color: blue; }\n@theme { --spacing-gutter: 1rem; }",
1759 );
1760 assert_eq!(names, vec!["color-a", "spacing-gutter"]);
1761 }
1762
1763 #[test]
1764 fn theme_reset_form_excluded() {
1765 let names = theme_token_names("@theme { --color-*: initial; --color-brand: red; }");
1767 assert_eq!(names, vec!["color-brand"]);
1768 }
1769
1770 #[test]
1771 fn theme_no_block_yields_nothing() {
1772 assert!(theme_token_names(".x { --color-brand: red; }").is_empty());
1773 }
1774
1775 #[test]
1776 fn theme_line_numbers() {
1777 let scan = scan_theme_blocks("@theme {\n --color-a: red;\n --radius-b: 4px;\n}");
1778 assert_eq!(scan.tokens[0].line, 2);
1779 assert_eq!(scan.tokens[1].line, 3);
1780 }
1781
1782 #[test]
1783 fn theme_token_backs_token_via_var() {
1784 let scan = scan_theme_blocks(
1785 "@theme {\n --color-brand: #f00;\n --color-button: var(--color-brand);\n}",
1786 );
1787 assert!(
1788 scan.theme_var_reads
1789 .iter()
1790 .any(|(name, _)| name == "color-brand")
1791 );
1792 }
1793
1794 #[test]
1795 fn theme_var_read_carries_line() {
1796 let scan = scan_theme_blocks(
1799 "@theme {\n --color-brand: #f00;\n --color-button: var(--color-brand);\n}",
1800 );
1801 assert_eq!(
1802 scan.theme_var_reads,
1803 vec![("color-brand".to_string(), 3u32)]
1804 );
1805 }
1806
1807 #[test]
1808 fn css_var_reads_locate_outside_theme_and_exclude_interior() {
1809 let source = "@theme {\n --color-brand: #f00;\n --color-button: var(--color-brand);\n}\n\n.btn {\n color: var(--color-brand);\n}\n";
1813 assert_eq!(
1814 extract_css_var_reads_located(source),
1815 vec![("color-brand".to_string(), 7u32)],
1816 "only the .btn read (line 7) is a css-var; the @theme-interior read is excluded"
1817 );
1818
1819 assert!(
1821 extract_css_var_reads_located("@theme {\n --a: #fff;\n --b: var(--a);\n}",)
1822 .is_empty(),
1823 "a @theme-interior-only var() read is not a css-var consumer"
1824 );
1825 }
1826
1827 #[test]
1828 fn theme_string_braces_do_not_truncate_block() {
1829 let scan = scan_theme_blocks(
1830 "@theme {\n --font-label: \"}\";\n --color-brand: #f00;\n --color-button: var(--color-brand);\n}",
1831 );
1832 assert_eq!(
1833 scan.tokens
1834 .iter()
1835 .map(|token| token.name.as_str())
1836 .collect::<Vec<_>>(),
1837 vec!["font-label", "color-brand", "color-button"]
1838 );
1839 assert!(
1840 scan.theme_var_reads
1841 .iter()
1842 .any(|(name, _)| name == "color-brand")
1843 );
1844 }
1845
1846 #[test]
1847 fn theme_nested_keyframes_body_not_collected() {
1848 let names = theme_token_names(
1851 "@theme {\n --animate-spin: spin 1s linear infinite;\n @keyframes spin { from { --x: 0; } to { --y: 1; } }\n}",
1852 );
1853 assert_eq!(names, vec!["animate-spin"]);
1854 }
1855
1856 #[test]
1857 fn theme_comment_block_ignored() {
1858 let names = theme_token_names("/* @theme { --color-fake: red; } */ .x { color: blue; }");
1859 assert!(names.is_empty(), "got {names:?}");
1860 }
1861
1862 #[test]
1863 fn theme_deduplicates_repeated_token() {
1864 let names = theme_token_names("@theme { --color-a: red; --color-a: blue; }");
1865 assert_eq!(names, vec!["color-a"]);
1866 }
1867
1868 #[test]
1869 fn apply_tokens_basic() {
1870 let tokens = extract_apply_tokens(".panel { @apply rounded-card font-bold; }");
1871 assert_eq!(tokens, vec!["rounded-card", "font-bold"]);
1872 }
1873
1874 #[test]
1875 fn apply_tokens_strips_important() {
1876 let tokens = extract_apply_tokens(".x { @apply text-brand! font-bold !important; }");
1877 assert_eq!(tokens, vec!["text-brand", "font-bold"]);
1878 }
1879
1880 #[test]
1881 fn apply_tokens_ignored_in_comments() {
1882 let tokens = extract_apply_tokens("/* @apply hidden-token; */ .x { color: red; }");
1883 assert!(tokens.is_empty(), "got {tokens:?}");
1884 }
1885}