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>,
316}
317
318#[must_use]
328pub fn scan_theme_blocks(source: &str) -> ThemeScan {
329 if !source.contains("@theme") {
331 return ThemeScan::default();
332 }
333 let masked = mask_with_whitespace(&mask_css_comments(source, false), &CSS_NON_SELECTOR_RE);
338 let bytes = masked.as_bytes();
339 let mut out = ThemeScan::default();
340 let mut seen: FxHashSet<String> = FxHashSet::default();
341 for open in CSS_THEME_OPEN_RE.find_iter(&masked) {
342 let body_start = open.end();
343 let mut depth = 1usize;
345 let mut i = body_start;
346 while i < bytes.len() {
347 match bytes[i] {
348 b'{' => depth += 1,
349 b'}' => {
350 depth -= 1;
351 if depth == 0 {
352 break;
353 }
354 }
355 _ => {}
356 }
357 i += 1;
358 }
359 let body_end = i.min(bytes.len());
360 collect_theme_declarations(
361 source,
362 &masked,
363 body_start,
364 body_end,
365 &mut out.tokens,
366 &mut seen,
367 );
368 if let Some(body) = masked.get(body_start..body_end) {
369 for cap in CSS_VAR_REF_RE.captures_iter(body) {
370 if let Some(name) = cap.get(1) {
371 out.theme_var_reads.push(name.as_str().to_owned());
372 }
373 }
374 }
375 }
376 out
377}
378
379fn collect_theme_declarations(
385 source: &str,
386 masked: &str,
387 start: usize,
388 end: usize,
389 out: &mut Vec<ThemeTokenDef>,
390 seen: &mut FxHashSet<String>,
391) {
392 let bytes = masked.as_bytes();
393 let mut depth = 0usize;
394 let mut expect_decl = true;
395 let mut i = start;
396 while i < end {
397 let b = bytes[i];
398 match b {
399 b'{' => {
400 depth += 1;
401 expect_decl = false;
402 i += 1;
403 }
404 b'}' => {
405 depth = depth.saturating_sub(1);
406 if depth == 0 {
407 expect_decl = true;
408 }
409 i += 1;
410 }
411 b';' => {
412 if depth == 0 {
413 expect_decl = true;
414 }
415 i += 1;
416 }
417 _ if b.is_ascii_whitespace() => i += 1,
418 _ => {
419 if depth == 0 && expect_decl {
420 expect_decl = false;
421 if b == b'-' && bytes.get(i + 1) == Some(&b'-') {
422 let id_start = i;
423 let mut j = i;
424 while j < end {
425 let c = bytes[j];
426 if c == b'-' || c == b'_' || c.is_ascii_alphanumeric() {
427 j += 1;
428 } else {
429 break;
430 }
431 }
432 let mut k = j;
433 while k < end && bytes[k].is_ascii_whitespace() {
434 k += 1;
435 }
436 if k < end && bytes[k] == b':' {
438 let name = &masked[id_start + 2..j];
439 if !name.is_empty() && seen.insert(name.to_owned()) {
440 let line = 1 + source
441 .get(..id_start)
442 .map_or(0, |s| s.bytes().filter(|&x| x == b'\n').count());
443 out.push(ThemeTokenDef {
444 name: name.to_owned(),
445 line: u32::try_from(line).unwrap_or(u32::MAX),
446 });
447 }
448 }
449 i = j;
450 } else {
451 i += 1;
452 }
453 } else {
454 i += 1;
455 }
456 }
457 }
458 }
459}
460
461#[must_use]
467pub fn extract_apply_tokens(source: &str) -> Vec<String> {
468 if !source.contains("@apply") {
470 return Vec::new();
471 }
472 let masked = mask_with_whitespace(&mask_css_comments(source, false), &CSS_NON_SELECTOR_RE);
473 let mut out = Vec::new();
474 for m in CSS_APPLY_RE.find_iter(&masked) {
475 let body = m.as_str().trim_start_matches("@apply");
476 for token in body.split_whitespace() {
477 let token = token.trim_matches('!');
478 if token.is_empty() || token == "important" {
479 continue;
480 }
481 out.push(token.to_owned());
482 }
483 }
484 out
485}
486
487fn mask_with_whitespace(src: &str, re: ®ex::Regex) -> String {
497 let mut out = String::with_capacity(src.len());
498 let mut cursor = 0;
499 for m in re.find_iter(src) {
500 out.push_str(&src[cursor..m.start()]);
501 for _ in m.start()..m.end() {
502 out.push(' ');
503 }
504 cursor = m.end();
505 }
506 out.push_str(&src[cursor..]);
507 out
508}
509
510fn lightningcss_class_set(source: &str) -> Option<FxHashSet<String>> {
527 let options = ParserOptions {
528 error_recovery: true,
531 css_modules: Some(lightningcss::css_modules::Config::default()),
537 ..ParserOptions::default()
538 };
539 let stylesheet = StyleSheet::parse(source, options).ok()?;
540 let mut classes = FxHashSet::default();
541 collect_classes_from_rules(&stylesheet.rules.0, &mut classes);
542 Some(classes)
543}
544
545fn collect_classes_from_rules(rules: &[CssRule<'_>], classes: &mut FxHashSet<String>) {
550 for rule in rules {
551 match rule {
552 CssRule::Style(style) => {
553 collect_classes_from_selector_list(&style.selectors, classes);
554 collect_classes_from_rules(&style.rules.0, classes);
555 }
556 CssRule::Media(rule) => collect_classes_from_rules(&rule.rules.0, classes),
557 CssRule::Supports(rule) => collect_classes_from_rules(&rule.rules.0, classes),
558 CssRule::Container(rule) => collect_classes_from_rules(&rule.rules.0, classes),
559 CssRule::LayerBlock(rule) => collect_classes_from_rules(&rule.rules.0, classes),
560 CssRule::MozDocument(rule) => collect_classes_from_rules(&rule.rules.0, classes),
561 CssRule::StartingStyle(rule) => collect_classes_from_rules(&rule.rules.0, classes),
562 CssRule::Nesting(rule) => {
563 collect_classes_from_selector_list(&rule.style.selectors, classes);
564 collect_classes_from_rules(&rule.style.rules.0, classes);
565 }
566 CssRule::Scope(rule) => {
567 if let Some(scope_start) = &rule.scope_start {
568 collect_classes_from_selector_list(scope_start, classes);
569 }
570 if let Some(scope_end) = &rule.scope_end {
571 collect_classes_from_selector_list(scope_end, classes);
572 }
573 collect_classes_from_rules(&rule.rules.0, classes);
574 }
575 _ => {}
576 }
577 }
578}
579
580fn collect_classes_from_selector_list(list: &SelectorList<'_>, classes: &mut FxHashSet<String>) {
581 for selector in &list.0 {
582 collect_classes_from_selector(selector, classes);
583 }
584}
585
586fn collect_classes_from_selector(selector: &Selector<'_>, classes: &mut FxHashSet<String>) {
587 for component in selector.iter_raw_match_order() {
588 match component {
589 Component::Class(name) => {
590 classes.insert(name.0.to_string());
591 }
592 Component::Is(list)
593 | Component::Where(list)
594 | Component::Has(list)
595 | Component::Negation(list)
596 | Component::Any(_, list) => {
597 for nested in list.as_ref() {
598 collect_classes_from_selector(nested, classes);
599 }
600 }
601 Component::Slotted(nested) | Component::Host(Some(nested)) => {
602 collect_classes_from_selector(nested, classes);
603 }
604 Component::NthOf(data) => {
605 for nested in data.selectors() {
606 collect_classes_from_selector(nested, classes);
607 }
608 }
609 Component::NonTSPseudoClass(
611 PseudoClass::Local { selector } | PseudoClass::Global { selector },
612 ) => collect_classes_from_selector(selector, classes),
613 _ => {}
614 }
615 }
616}
617
618pub fn extract_css_module_exports(source: &str, is_scss: bool) -> Vec<ExportInfo> {
628 if !is_scss && let Some(class_set) = lightningcss_class_set(source) {
629 return scan_css_module_exports(source, is_scss, Some(&class_set));
630 }
631 scan_css_module_exports(source, is_scss, None)
632}
633
634fn scan_css_module_exports(
644 source: &str,
645 is_scss: bool,
646 class_filter: Option<&FxHashSet<String>>,
647) -> Vec<ExportInfo> {
648 let mut masked = mask_with_whitespace(source, &CSS_COMMENT_RE);
649 if is_scss {
650 masked = mask_with_whitespace(&masked, &SCSS_LINE_COMMENT_RE);
651 }
652 masked = mask_with_whitespace(&masked, &CSS_NON_SELECTOR_RE);
653 if class_filter.is_none() {
654 masked = mask_with_whitespace(&masked, &CSS_AT_RULE_PRELUDE_RE);
655 }
656
657 let mut seen = FxHashSet::default();
658 let mut exports = Vec::new();
659 for cap in CSS_CLASS_RE.captures_iter(&masked) {
660 if let Some(m) = cap.get(1) {
661 let class_name = m.as_str().to_string();
662 if class_filter.is_some_and(|filter| !filter.contains(&class_name)) {
663 continue;
664 }
665 if seen.insert(class_name.clone()) {
666 #[expect(
667 clippy::cast_possible_truncation,
668 reason = "CSS files exceeding u32::MAX bytes are not a realistic input"
669 )]
670 let span = Span::new(m.start() as u32, m.end() as u32);
671 exports.push(ExportInfo {
672 name: ExportName::Named(class_name),
673 local_name: None,
674 is_type_only: false,
675 visibility: VisibilityTag::None,
676 expected_unused_reason: None,
677 span,
678 members: Vec::new(),
679 is_side_effect_used: false,
680 super_class: None,
681 });
682 }
683 }
684 }
685 exports
686}
687
688pub(crate) fn parse_css_to_module(
690 file_id: FileId,
691 path: &Path,
692 source: &str,
693 content_hash: u64,
694) -> ModuleInfo {
695 let parsed_suppressions = crate::suppress::parse_suppressions_from_source(source);
696 let is_scss = path
697 .extension()
698 .and_then(|e| e.to_str())
699 .is_some_and(|ext| matches!(ext, "scss" | "sass" | "less"));
700
701 let stripped = mask_css_comments(source, is_scss);
702
703 let mut imports = Vec::new();
704
705 for source in extract_css_import_sources(source, is_scss) {
706 imports.push(ImportInfo {
707 source: source.normalized,
708 imported_name: if source.is_plugin {
709 ImportedName::Default
710 } else {
711 ImportedName::SideEffect
712 },
713 local_name: String::new(),
714 is_type_only: false,
715 from_style: false,
716 span: source.span,
717 source_span: source.span,
718 });
719 }
720
721 let has_apply = CSS_APPLY_RE.is_match(&stripped);
722 let has_tailwind = CSS_TAILWIND_RE.is_match(&stripped);
723 if has_apply || has_tailwind {
724 imports.push(ImportInfo {
725 source: "tailwindcss".to_string(),
726 imported_name: ImportedName::SideEffect,
727 local_name: String::new(),
728 is_type_only: false,
729 from_style: false,
730 span: Span::default(),
731 source_span: Span::default(),
732 });
733 }
734
735 let exports = if is_css_module_file(path) {
736 extract_css_module_exports(source, is_scss)
737 } else {
738 Vec::new()
739 };
740
741 ModuleInfo {
742 file_id,
743 exports,
744 imports,
745 re_exports: Vec::new(),
746 dynamic_imports: Vec::new(),
747 dynamic_import_patterns: Vec::new(),
748 require_calls: Vec::new(),
749 package_path_references: Vec::new(),
750 member_accesses: Vec::new(),
751 whole_object_uses: Vec::new(),
752 has_cjs_exports: false,
753 has_angular_component_template_url: false,
754 content_hash,
755 suppressions: parsed_suppressions.suppressions,
756 unknown_suppression_kinds: parsed_suppressions.unknown_kinds,
757 unused_import_bindings: Vec::new(),
758 type_referenced_import_bindings: Vec::new(),
759 value_referenced_import_bindings: Vec::new(),
760 line_offsets: fallow_types::extract::compute_line_offsets(source),
761 complexity: Vec::new(),
762 flag_uses: Vec::new(),
763 class_heritage: vec![],
764 injection_tokens: vec![],
765 local_type_declarations: Vec::new(),
766 public_signature_type_references: Vec::new(),
767 namespace_object_aliases: Vec::new(),
768 iconify_prefixes: Vec::new(),
769 iconify_icon_names: Vec::new(),
770 auto_import_candidates: Vec::new(),
771 directives: Vec::new(),
772 client_only_dynamic_import_spans: Vec::new(),
773 security_sinks: Vec::new(),
774 security_sinks_skipped: 0,
775 security_unresolved_callee_sites: Vec::new(),
776 tainted_bindings: Vec::new(),
777 sanitized_sink_args: Vec::new(),
778 security_control_sites: Vec::new(),
779 callee_uses: Vec::new(),
780 misplaced_directives: Vec::new(),
781 inline_server_action_exports: Vec::new(),
782 di_key_sites: Vec::new(),
783 has_dynamic_provide: false,
784 referenced_import_bindings: Vec::new(),
785 component_props: Vec::new(),
786 has_props_attrs_fallthrough: false,
787 has_define_expose: false,
788 has_define_model: false,
789 has_unharvestable_props: false,
790 component_emits: Vec::new(),
791 angular_inputs: Vec::new(),
792 angular_outputs: Vec::new(),
793 angular_component_selectors: Vec::new(),
794 angular_used_selectors: Vec::new(),
795 angular_entry_component_refs: Vec::new(),
796 has_dynamic_component_render: false,
797 has_unharvestable_emits: false,
798 has_dynamic_emit: false,
799 has_emit_whole_object_use: false,
800 load_return_keys: Vec::new(),
801 has_unharvestable_load: false,
802 has_load_data_whole_use: false,
803 has_page_data_store_whole_use: false,
804 component_functions: Vec::new(),
805 react_props: Vec::new(),
806 hook_uses: Vec::new(),
807 render_edges: Vec::new(),
808 svelte_dispatched_events: Vec::new(),
809 svelte_listened_events: Vec::new(),
810 has_dynamic_dispatch: false,
811 }
812}
813
814#[cfg(test)]
815mod tests {
816 use super::*;
817
818 fn export_names(source: &str) -> Vec<String> {
820 extract_css_module_exports(source, false)
821 .into_iter()
822 .filter_map(|e| match e.name {
823 ExportName::Named(n) => Some(n),
824 ExportName::Default => None,
825 })
826 .collect()
827 }
828
829 #[test]
830 fn is_css_file_css() {
831 assert!(is_css_file(Path::new("styles.css")));
832 }
833
834 #[test]
835 fn is_css_file_scss() {
836 assert!(is_css_file(Path::new("styles.scss")));
837 }
838
839 #[test]
840 fn is_css_file_sass() {
841 assert!(is_css_file(Path::new("styles.sass")));
842 }
843
844 #[test]
845 fn is_css_file_less() {
846 assert!(is_css_file(Path::new("styles.less")));
847 }
848
849 #[test]
850 fn is_css_file_rejects_js() {
851 assert!(!is_css_file(Path::new("app.js")));
852 }
853
854 #[test]
855 fn is_css_file_rejects_ts() {
856 assert!(!is_css_file(Path::new("app.ts")));
857 }
858
859 #[test]
860 fn is_css_file_rejects_no_extension() {
861 assert!(!is_css_file(Path::new("Makefile")));
862 }
863
864 #[test]
865 fn is_css_module_file_module_css() {
866 assert!(is_css_module_file(Path::new("Component.module.css")));
867 }
868
869 #[test]
870 fn is_css_module_file_module_scss() {
871 assert!(is_css_module_file(Path::new("Component.module.scss")));
872 }
873
874 #[test]
875 fn is_css_module_file_rejects_plain_css() {
876 assert!(!is_css_module_file(Path::new("styles.css")));
877 }
878
879 #[test]
880 fn is_css_module_file_rejects_plain_scss() {
881 assert!(!is_css_module_file(Path::new("styles.scss")));
882 }
883
884 #[test]
885 fn is_css_module_file_rejects_module_js() {
886 assert!(!is_css_module_file(Path::new("utils.module.js")));
887 }
888
889 #[test]
890 fn extracts_single_class() {
891 let names = export_names(".foo { color: red; }");
892 assert_eq!(names, vec!["foo"]);
893 }
894
895 #[test]
896 fn extracts_multiple_classes() {
897 let names = export_names(".foo { } .bar { }");
898 assert_eq!(names, vec!["foo", "bar"]);
899 }
900
901 #[test]
902 fn extracts_nested_classes() {
903 let names = export_names(".foo .bar { color: red; }");
904 assert!(names.contains(&"foo".to_string()));
905 assert!(names.contains(&"bar".to_string()));
906 }
907
908 #[test]
909 fn extracts_hyphenated_class() {
910 let names = export_names(".my-class { }");
911 assert_eq!(names, vec!["my-class"]);
912 }
913
914 #[test]
915 fn extracts_camel_case_class() {
916 let names = export_names(".myClass { }");
917 assert_eq!(names, vec!["myClass"]);
918 }
919
920 #[test]
921 fn extracts_class_inside_global_pseudo() {
922 let names = export_names(":global(.globalClass) { color: red; }");
925 assert_eq!(names, vec!["globalClass"]);
926 }
927
928 #[test]
929 fn extracts_class_inside_local_pseudo() {
930 let names = export_names(":local(.localClass) { color: red; }");
931 assert_eq!(names, vec!["localClass"]);
932 }
933
934 #[test]
935 fn extracts_classes_inside_negation() {
936 let names = export_names(".btn:not(.disabled) { }");
937 assert!(names.contains(&"btn".to_string()), "got {names:?}");
938 assert!(names.contains(&"disabled".to_string()), "got {names:?}");
939 }
940
941 #[test]
942 fn extracts_classes_inside_is_and_where() {
943 let names = export_names(":is(.a, .b) :where(.c) { }");
944 for expected in ["a", "b", "c"] {
945 assert!(
946 names.contains(&expected.to_string()),
947 "missing {expected} in {names:?}"
948 );
949 }
950 }
951
952 #[test]
953 fn extracts_underscore_class() {
954 let names = export_names("._hidden { } .__wrapper { }");
955 assert!(names.contains(&"_hidden".to_string()));
956 assert!(names.contains(&"__wrapper".to_string()));
957 }
958
959 #[test]
960 fn pseudo_selector_hover() {
961 let names = export_names(".foo:hover { color: blue; }");
962 assert_eq!(names, vec!["foo"]);
963 }
964
965 #[test]
966 fn pseudo_selector_focus() {
967 let names = export_names(".input:focus { outline: none; }");
968 assert_eq!(names, vec!["input"]);
969 }
970
971 #[test]
972 fn pseudo_element_before() {
973 let names = export_names(".icon::before { content: ''; }");
974 assert_eq!(names, vec!["icon"]);
975 }
976
977 #[test]
978 fn combined_pseudo_selectors() {
979 let names = export_names(".btn:hover, .btn:active, .btn:focus { }");
980 assert_eq!(names, vec!["btn"]);
981 }
982
983 #[test]
984 fn classes_inside_media_query() {
985 let names = export_names(
986 "@media (max-width: 768px) { .mobile-nav { display: block; } .desktop-nav { display: none; } }",
987 );
988 assert!(names.contains(&"mobile-nav".to_string()));
989 assert!(names.contains(&"desktop-nav".to_string()));
990 }
991
992 #[test]
993 fn classes_inside_multi_line_media_query() {
994 let names =
995 export_names("@media\n screen and (min-width: 600px)\n{\n .real { color: red; }\n}");
996 assert_eq!(names, vec!["real"]);
997 }
998
999 #[test]
1000 fn at_layer_statement_does_not_export() {
1001 let names = export_names("@layer foo.bar;");
1002 assert!(names.is_empty(), "got {names:?}");
1003 let names = export_names("@layer foo.bar, foo.baz;");
1004 assert!(names.is_empty(), "got {names:?}");
1005 }
1006
1007 #[test]
1008 fn at_layer_block_keeps_body_classes() {
1009 let names = export_names("@layer foo.bar { .root { color: red; } }");
1010 assert_eq!(names, vec!["root"]);
1011 }
1012
1013 #[test]
1014 fn at_layer_multiline_prelude_keeps_body_classes() {
1015 let names = export_names("@layer\n foo.bar\n{ .root { color: red; } }");
1016 assert_eq!(names, vec!["root"]);
1017 }
1018
1019 #[test]
1020 fn at_layer_with_nested_media_keeps_body() {
1021 let names =
1022 export_names("@layer foo.bar { @media (max-width: 768px) { .real { color: red; } } }");
1023 assert_eq!(names, vec!["real"]);
1024 }
1025
1026 #[test]
1027 fn at_import_with_layer_attribute_does_not_export() {
1028 let names = export_names(r#"@import url("x.css") layer(theme.button);"#);
1029 assert!(names.is_empty(), "got {names:?}");
1030 }
1031
1032 #[test]
1033 fn class_then_at_layer_does_not_leak_prelude() {
1034 let names =
1035 export_names(".outer { color: blue; } @layer foo.bar { .inner { color: red; } }");
1036 assert_eq!(names, vec!["outer", "inner"]);
1037 }
1038
1039 #[test]
1040 fn at_scope_keeps_selector_list_classes() {
1041 let names = export_names("@scope (.parent) to (.child) { .title { color: red; } }");
1042 assert!(names.contains(&"parent".to_string()), "got {names:?}");
1043 assert!(names.contains(&"child".to_string()), "got {names:?}");
1044 assert!(names.contains(&"title".to_string()), "got {names:?}");
1045 }
1046
1047 #[test]
1048 fn at_keyframes_numeric_step_is_not_class() {
1049 let names = export_names(
1050 "@keyframes slide { 0% { transform: scale(.5); } 100% { transform: scale(1); } }",
1051 );
1052 assert!(names.is_empty(), "got {names:?}");
1053 }
1054
1055 #[test]
1056 fn at_webkit_keyframes_keeps_body_classes() {
1057 let names = export_names("@-webkit-keyframes slide { 0% { } 100% { } } .real { }");
1058 assert_eq!(names, vec!["real"]);
1059 }
1060
1061 #[test]
1062 fn deduplicates_repeated_class() {
1063 let names = export_names(".btn { color: red; } .btn { font-size: 14px; }");
1064 assert_eq!(names.iter().filter(|n| *n == "btn").count(), 1);
1065 }
1066
1067 #[test]
1068 fn empty_source() {
1069 let names = export_names("");
1070 assert!(names.is_empty());
1071 }
1072
1073 #[test]
1074 fn no_classes() {
1075 let names = export_names("body { margin: 0; } * { box-sizing: border-box; }");
1076 assert!(names.is_empty());
1077 }
1078
1079 #[test]
1080 fn ignores_classes_in_block_comments() {
1081 let names = export_names("/* .fake { } */ .real { }");
1082 assert!(!names.contains(&"fake".to_string()));
1083 assert!(names.contains(&"real".to_string()));
1084 }
1085
1086 #[test]
1087 fn ignores_classes_in_scss_line_comments() {
1088 let exports = extract_css_module_exports("// .fake\n.real { }", true);
1089 let names: Vec<_> = exports
1090 .iter()
1091 .filter_map(|e| match &e.name {
1092 ExportName::Named(n) => Some(n.as_str()),
1093 ExportName::Default => None,
1094 })
1095 .collect();
1096 assert_eq!(names, vec!["real"]);
1097 }
1098
1099 #[test]
1100 fn ignores_classes_in_strings() {
1101 let names = export_names(r#".real { content: ".fake"; }"#);
1102 assert!(names.contains(&"real".to_string()));
1103 assert!(!names.contains(&"fake".to_string()));
1104 }
1105
1106 #[test]
1107 fn ignores_classes_in_url() {
1108 let names = export_names(".real { background: url(./images/hero.png); }");
1109 assert!(names.contains(&"real".to_string()));
1110 assert!(!names.contains(&"png".to_string()));
1111 }
1112
1113 #[test]
1114 fn strip_css_block_comment() {
1115 let result = strip_css_comments("/* removed */ .kept { }", false);
1116 assert!(!result.contains("removed"));
1117 assert!(result.contains(".kept"));
1118 }
1119
1120 #[test]
1121 fn strip_scss_line_comment() {
1122 let result = strip_css_comments("// removed\n.kept { }", true);
1123 assert!(!result.contains("removed"));
1124 assert!(result.contains(".kept"));
1125 }
1126
1127 #[test]
1128 fn strip_scss_preserves_css_outside_comments() {
1129 let source = "// line comment\n/* block comment */\n.visible { color: red; }";
1130 let result = strip_css_comments(source, true);
1131 assert!(result.contains(".visible"));
1132 }
1133
1134 #[test]
1135 fn url_import_http() {
1136 assert!(is_css_url_import("http://example.com/style.css"));
1137 }
1138
1139 #[test]
1140 fn url_import_https() {
1141 assert!(is_css_url_import("https://fonts.googleapis.com/css"));
1142 }
1143
1144 #[test]
1145 fn url_import_data() {
1146 assert!(is_css_url_import("data:text/css;base64,abc"));
1147 }
1148
1149 #[test]
1150 fn url_import_local_not_skipped() {
1151 assert!(!is_css_url_import("./local.css"));
1152 }
1153
1154 #[test]
1155 fn url_import_bare_specifier_not_skipped() {
1156 assert!(!is_css_url_import("tailwindcss"));
1157 }
1158
1159 #[test]
1160 fn normalize_relative_dot_path_unchanged() {
1161 assert_eq!(
1162 normalize_css_import_path("./reset.css".to_string(), false),
1163 "./reset.css"
1164 );
1165 }
1166
1167 #[test]
1168 fn normalize_parent_relative_path_unchanged() {
1169 assert_eq!(
1170 normalize_css_import_path("../shared.scss".to_string(), false),
1171 "../shared.scss"
1172 );
1173 }
1174
1175 #[test]
1176 fn normalize_absolute_path_unchanged() {
1177 assert_eq!(
1178 normalize_css_import_path("/styles/main.css".to_string(), false),
1179 "/styles/main.css"
1180 );
1181 }
1182
1183 #[test]
1184 fn normalize_url_unchanged() {
1185 assert_eq!(
1186 normalize_css_import_path("https://example.com/style.css".to_string(), false),
1187 "https://example.com/style.css"
1188 );
1189 }
1190
1191 #[test]
1192 fn normalize_bare_css_gets_dot_slash() {
1193 assert_eq!(
1194 normalize_css_import_path("app.css".to_string(), false),
1195 "./app.css"
1196 );
1197 }
1198
1199 #[test]
1200 fn normalize_css_package_subpath_stays_bare() {
1201 assert_eq!(
1202 normalize_css_import_path("tailwindcss/theme.css".to_string(), false),
1203 "tailwindcss/theme.css"
1204 );
1205 }
1206
1207 #[test]
1208 fn normalize_css_package_subpath_with_dotted_name_stays_bare() {
1209 assert_eq!(
1210 normalize_css_import_path("highlight.js/styles/github.css".to_string(), false),
1211 "highlight.js/styles/github.css"
1212 );
1213 }
1214
1215 #[test]
1216 fn normalize_bare_scss_gets_dot_slash() {
1217 assert_eq!(
1218 normalize_css_import_path("vars.scss".to_string(), false),
1219 "./vars.scss"
1220 );
1221 }
1222
1223 #[test]
1224 fn normalize_bare_sass_gets_dot_slash() {
1225 assert_eq!(
1226 normalize_css_import_path("main.sass".to_string(), false),
1227 "./main.sass"
1228 );
1229 }
1230
1231 #[test]
1232 fn normalize_bare_less_gets_dot_slash() {
1233 assert_eq!(
1234 normalize_css_import_path("theme.less".to_string(), false),
1235 "./theme.less"
1236 );
1237 }
1238
1239 #[test]
1240 fn normalize_bare_js_extension_stays_bare() {
1241 assert_eq!(
1242 normalize_css_import_path("module.js".to_string(), false),
1243 "module.js"
1244 );
1245 }
1246
1247 #[test]
1248 fn normalize_scss_bare_partial_gets_dot_slash() {
1249 assert_eq!(
1250 normalize_css_import_path("variables".to_string(), true),
1251 "./variables"
1252 );
1253 }
1254
1255 #[test]
1256 fn normalize_scss_bare_partial_with_subdir_gets_dot_slash() {
1257 assert_eq!(
1258 normalize_css_import_path("base/reset".to_string(), true),
1259 "./base/reset"
1260 );
1261 }
1262
1263 #[test]
1264 fn normalize_scss_builtin_stays_bare() {
1265 assert_eq!(
1266 normalize_css_import_path("sass:math".to_string(), true),
1267 "sass:math"
1268 );
1269 }
1270
1271 #[test]
1272 fn normalize_scss_relative_path_unchanged() {
1273 assert_eq!(
1274 normalize_css_import_path("../styles/variables".to_string(), true),
1275 "../styles/variables"
1276 );
1277 }
1278
1279 #[test]
1280 fn normalize_css_bare_extensionless_stays_bare() {
1281 assert_eq!(
1282 normalize_css_import_path("tailwindcss".to_string(), false),
1283 "tailwindcss"
1284 );
1285 }
1286
1287 #[test]
1288 fn normalize_scoped_package_with_css_extension_stays_bare() {
1289 assert_eq!(
1290 normalize_css_import_path("@fontsource/monaspace-neon/400.css".to_string(), false),
1291 "@fontsource/monaspace-neon/400.css"
1292 );
1293 }
1294
1295 #[test]
1296 fn normalize_scoped_package_with_scss_extension_stays_bare() {
1297 assert_eq!(
1298 normalize_css_import_path("@company/design-system/tokens.scss".to_string(), true),
1299 "@company/design-system/tokens.scss"
1300 );
1301 }
1302
1303 #[test]
1304 fn normalize_scoped_package_without_extension_stays_bare() {
1305 assert_eq!(
1306 normalize_css_import_path("@fallow/design-system/styles".to_string(), false),
1307 "@fallow/design-system/styles"
1308 );
1309 }
1310
1311 #[test]
1312 fn normalize_scoped_package_extensionless_scss_stays_bare() {
1313 assert_eq!(
1314 normalize_css_import_path("@company/tokens".to_string(), true),
1315 "@company/tokens"
1316 );
1317 }
1318
1319 #[test]
1320 fn normalize_path_alias_with_css_extension_stays_bare() {
1321 assert_eq!(
1322 normalize_css_import_path("@/components/Button.css".to_string(), false),
1323 "@/components/Button.css"
1324 );
1325 }
1326
1327 #[test]
1328 fn normalize_path_alias_extensionless_stays_bare() {
1329 assert_eq!(
1330 normalize_css_import_path("@/styles/variables".to_string(), false),
1331 "@/styles/variables"
1332 );
1333 }
1334
1335 #[test]
1336 fn strip_css_no_comments() {
1337 let source = ".foo { color: red; }";
1338 assert_eq!(strip_css_comments(source, false), source);
1339 }
1340
1341 #[test]
1342 fn strip_css_multiple_block_comments() {
1343 let source = "/* comment-one */ .foo { } /* comment-two */ .bar { }";
1344 let result = strip_css_comments(source, false);
1345 assert!(!result.contains("comment-one"));
1346 assert!(!result.contains("comment-two"));
1347 assert!(result.contains(".foo"));
1348 assert!(result.contains(".bar"));
1349 }
1350
1351 #[test]
1352 fn strip_scss_does_not_affect_non_scss() {
1353 let source = "// this stays\n.foo { }";
1354 let result = strip_css_comments(source, false);
1355 assert!(result.contains("// this stays"));
1356 }
1357
1358 #[test]
1359 fn css_module_parses_suppressions() {
1360 let info = parse_css_to_module(
1361 fallow_types::discover::FileId(0),
1362 Path::new("Component.module.css"),
1363 "/* fallow-ignore-file */\n.btn { color: red; }",
1364 0,
1365 );
1366 assert!(!info.suppressions.is_empty());
1367 assert_eq!(info.suppressions[0].line, 0);
1368 }
1369
1370 #[test]
1371 fn extracts_class_starting_with_underscore() {
1372 let names = export_names("._private { } .__dunder { }");
1373 assert!(names.contains(&"_private".to_string()));
1374 assert!(names.contains(&"__dunder".to_string()));
1375 }
1376
1377 #[test]
1378 fn ignores_id_selectors() {
1379 let names = export_names("#myId { color: red; }");
1380 assert!(!names.contains(&"myId".to_string()));
1381 }
1382
1383 #[test]
1384 fn ignores_element_selectors() {
1385 let names = export_names("div { color: red; } span { }");
1386 assert!(names.is_empty());
1387 }
1388
1389 #[test]
1390 fn extract_css_imports_at_import_quoted() {
1391 let imports = extract_css_imports(r#"@import "./reset.css";"#, false);
1392 assert_eq!(imports, vec!["./reset.css"]);
1393 }
1394
1395 #[test]
1396 fn extract_css_imports_package_subpath_stays_bare() {
1397 let imports =
1398 extract_css_imports(r#"@import "tailwindcss/theme.css" layer(theme);"#, false);
1399 assert_eq!(imports, vec!["tailwindcss/theme.css"]);
1400 }
1401
1402 #[test]
1403 fn extract_css_imports_at_import_url() {
1404 let imports = extract_css_imports(r#"@import url("./reset.css");"#, false);
1405 assert_eq!(imports, vec!["./reset.css"]);
1406 }
1407
1408 #[test]
1409 fn extract_css_imports_skips_remote_urls() {
1410 let imports =
1411 extract_css_imports(r#"@import "https://fonts.example.com/font.css";"#, false);
1412 assert!(imports.is_empty());
1413 }
1414
1415 #[test]
1416 fn extract_css_imports_scss_use_normalizes_partial() {
1417 let imports = extract_css_imports(r#"@use "variables";"#, true);
1418 assert_eq!(imports, vec!["./variables"]);
1419 }
1420
1421 #[test]
1422 fn extract_css_imports_scss_forward_normalizes_partial() {
1423 let imports = extract_css_imports(r#"@forward "tokens";"#, true);
1424 assert_eq!(imports, vec!["./tokens"]);
1425 }
1426
1427 #[test]
1428 fn extract_css_imports_skips_comments() {
1429 let imports = extract_css_imports(
1430 r#"/* @import "./hidden.scss"; */
1431@use "real";"#,
1432 true,
1433 );
1434 assert_eq!(imports, vec!["./real"]);
1435 }
1436
1437 #[test]
1438 fn extract_css_imports_at_plugin_keeps_package_bare() {
1439 let imports = extract_css_imports(r#"@plugin "daisyui";"#, true);
1440 assert_eq!(imports, vec!["daisyui"]);
1441 }
1442
1443 #[test]
1444 fn extract_css_imports_at_plugin_tracks_relative_file() {
1445 let imports = extract_css_imports(r#"@plugin "./tailwind-plugin.js";"#, false);
1446 assert_eq!(imports, vec!["./tailwind-plugin.js"]);
1447 }
1448
1449 #[test]
1450 fn extract_css_imports_scss_at_import_kept_relative() {
1451 let imports = extract_css_imports(r"@import 'Foo';", true);
1452 assert_eq!(imports, vec!["./Foo"]);
1453 }
1454
1455 #[test]
1456 fn extract_css_imports_additional_data_string_body() {
1457 let body = r#"@use "./src/styles/global.scss";"#;
1458 let imports = extract_css_imports(body, true);
1459 assert_eq!(imports, vec!["./src/styles/global.scss"]);
1460 }
1461
1462 #[test]
1463 fn mask_with_whitespace_preserves_byte_length() {
1464 let src = "/* hello */ .foo { }";
1465 let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
1466 assert_eq!(masked.len(), src.len());
1467 assert!(masked.is_char_boundary(src.len()));
1468 }
1469
1470 #[test]
1471 fn mask_with_whitespace_preserves_offsets_around_multibyte() {
1472 let src = "/* \u{2713} */ .foo { }";
1473 let foo_offset = src.find(".foo").expect("`.foo` present");
1474 let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
1475 assert_eq!(masked.len(), src.len());
1476 assert_eq!(masked.find(".foo"), Some(foo_offset));
1477 }
1478
1479 fn span_line_col(source: &str, start: u32) -> (u32, u32) {
1482 let offsets = fallow_types::extract::compute_line_offsets(source);
1483 fallow_types::extract::byte_offset_to_line_col(&offsets, start)
1484 }
1485
1486 #[test]
1487 fn span_points_at_real_class_declaration_line() {
1488 let source = "\n\n\n\n.foo { color: red; }\n";
1489 let exports = extract_css_module_exports(source, false);
1490 assert_eq!(exports.len(), 1);
1491 let span = exports[0].span;
1492 let (line, col) = span_line_col(source, span.start);
1493 assert_eq!(line, 5, "`.foo` on line 5 must produce line 5, not line 1");
1494 assert_eq!(
1495 col, 1,
1496 "column points at `f` in `.foo` (post-dot identifier)"
1497 );
1498 assert_eq!(
1499 &source[span.start as usize..span.end as usize],
1500 "foo",
1501 "span range must slice to the class identifier in the original source"
1502 );
1503 }
1504
1505 #[test]
1506 fn span_survives_multibyte_comment_prefix() {
1507 let source = "/* \u{2713} */\n.foo { }";
1508 let exports = extract_css_module_exports(source, false);
1509 assert_eq!(exports.len(), 1);
1510 let span = exports[0].span;
1511 assert!(
1512 source.is_char_boundary(span.start as usize),
1513 "span.start must lie on a UTF-8 char boundary"
1514 );
1515 assert_eq!(&source[span.start as usize..span.end as usize], "foo");
1516 }
1517
1518 #[test]
1519 fn span_skips_at_layer_prelude_dot_segments() {
1520 let source = "@layer foo.bar { }\n.root { }\n";
1521 let exports = extract_css_module_exports(source, false);
1522 let names: Vec<_> = exports
1523 .iter()
1524 .filter_map(|e| match &e.name {
1525 ExportName::Named(n) => Some(n.as_str()),
1526 ExportName::Default => None,
1527 })
1528 .collect();
1529 assert_eq!(names, vec!["root"], "@layer sub-segments must not export");
1530 let span = exports[0].span;
1531 let (line, _col) = span_line_col(source, span.start);
1532 assert_eq!(line, 2, "`.root` lives on line 2 of the original source");
1533 assert_eq!(&source[span.start as usize..span.end as usize], "root");
1534 }
1535
1536 #[test]
1537 fn span_skips_classes_in_strings() {
1538 let source = ".real { content: \".fake\"; }\n.also-real { }\n";
1539 let exports = extract_css_module_exports(source, false);
1540 let names: Vec<_> = exports
1541 .iter()
1542 .filter_map(|e| match &e.name {
1543 ExportName::Named(n) => Some(n.as_str()),
1544 ExportName::Default => None,
1545 })
1546 .collect();
1547 assert_eq!(names, vec!["real", "also-real"]);
1548 for export in &exports {
1549 let span = export.span;
1550 let slice = &source[span.start as usize..span.end as usize];
1551 match &export.name {
1552 ExportName::Named(n) => assert_eq!(slice, n.as_str()),
1553 ExportName::Default => unreachable!("CSS modules emit only named exports"),
1554 }
1555 }
1556 }
1557
1558 #[test]
1559 fn span_deduplicates_to_first_occurrence() {
1560 let source = ".btn { color: red; }\n.btn { color: blue; }\n";
1561 let exports = extract_css_module_exports(source, false);
1562 assert_eq!(exports.len(), 1);
1563 let (line, _col) = span_line_col(source, exports[0].span.start);
1564 assert_eq!(
1565 line, 1,
1566 "first occurrence wins for deduplicated class names"
1567 );
1568 }
1569
1570 #[test]
1571 fn span_inside_media_query() {
1572 let source =
1573 "@media (max-width: 768px) {\n .mobile { display: block; }\n .desktop { }\n}\n";
1574 let exports = extract_css_module_exports(source, false);
1575 let by_name: rustc_hash::FxHashMap<&str, oxc_span::Span> = exports
1576 .iter()
1577 .filter_map(|e| match &e.name {
1578 ExportName::Named(n) => Some((n.as_str(), e.span)),
1579 ExportName::Default => None,
1580 })
1581 .collect();
1582 let mobile_line = span_line_col(source, by_name["mobile"].start).0;
1583 let desktop_line = span_line_col(source, by_name["desktop"].start).0;
1584 assert_eq!(mobile_line, 2);
1585 assert_eq!(desktop_line, 3);
1586 }
1587
1588 #[test]
1589 fn at_layer_only_module_emits_no_exports() {
1590 let exports = extract_css_module_exports("@layer foo.bar, foo.baz;\n", false);
1591 assert!(exports.is_empty());
1592 }
1593
1594 #[test]
1595 fn parse_css_to_module_resolves_real_line_offsets() {
1596 let source = "\n\n\n\n.foo { color: red; }\n";
1597 let info = parse_css_to_module(
1598 fallow_types::discover::FileId(0),
1599 Path::new("Component.module.css"),
1600 source,
1601 0,
1602 );
1603 assert_eq!(info.exports.len(), 1);
1604 let (line, _col) = fallow_types::extract::byte_offset_to_line_col(
1605 &info.line_offsets,
1606 info.exports[0].span.start,
1607 );
1608 assert_eq!(line, 5, "downstream line must equal the source line");
1609 }
1610
1611 fn theme_token_names(source: &str) -> Vec<String> {
1612 scan_theme_blocks(source)
1613 .tokens
1614 .into_iter()
1615 .map(|t| t.name)
1616 .collect()
1617 }
1618
1619 #[test]
1620 fn theme_single_block_collects_tokens() {
1621 let names = theme_token_names("@theme { --color-brand: #f00; --radius-card: 8px; }");
1622 assert_eq!(names, vec!["color-brand", "radius-card"]);
1623 }
1624
1625 #[test]
1626 fn theme_dashed_multi_segment_names() {
1627 let names = theme_token_names(
1628 "@theme {\n --font-weight-heavy: 900;\n --inset-shadow-glow: 0 0 4px red;\n}",
1629 );
1630 assert_eq!(names, vec!["font-weight-heavy", "inset-shadow-glow"]);
1631 }
1632
1633 #[test]
1634 fn theme_inline_and_static_modifiers() {
1635 assert_eq!(
1636 theme_token_names("@theme inline { --color-a: red; }"),
1637 vec!["color-a"]
1638 );
1639 assert_eq!(
1640 theme_token_names("@theme static { --color-b: red; }"),
1641 vec!["color-b"]
1642 );
1643 }
1644
1645 #[test]
1646 fn theme_multiple_blocks_union() {
1647 let names = theme_token_names(
1648 "@theme { --color-a: red; }\n.x { color: blue; }\n@theme { --spacing-gutter: 1rem; }",
1649 );
1650 assert_eq!(names, vec!["color-a", "spacing-gutter"]);
1651 }
1652
1653 #[test]
1654 fn theme_reset_form_excluded() {
1655 let names = theme_token_names("@theme { --color-*: initial; --color-brand: red; }");
1657 assert_eq!(names, vec!["color-brand"]);
1658 }
1659
1660 #[test]
1661 fn theme_no_block_yields_nothing() {
1662 assert!(theme_token_names(".x { --color-brand: red; }").is_empty());
1663 }
1664
1665 #[test]
1666 fn theme_line_numbers() {
1667 let scan = scan_theme_blocks("@theme {\n --color-a: red;\n --radius-b: 4px;\n}");
1668 assert_eq!(scan.tokens[0].line, 2);
1669 assert_eq!(scan.tokens[1].line, 3);
1670 }
1671
1672 #[test]
1673 fn theme_token_backs_token_via_var() {
1674 let scan = scan_theme_blocks(
1675 "@theme {\n --color-brand: #f00;\n --color-button: var(--color-brand);\n}",
1676 );
1677 assert!(scan.theme_var_reads.contains(&"color-brand".to_string()));
1678 }
1679
1680 #[test]
1681 fn theme_nested_keyframes_body_not_collected() {
1682 let names = theme_token_names(
1685 "@theme {\n --animate-spin: spin 1s linear infinite;\n @keyframes spin { from { --x: 0; } to { --y: 1; } }\n}",
1686 );
1687 assert_eq!(names, vec!["animate-spin"]);
1688 }
1689
1690 #[test]
1691 fn theme_comment_block_ignored() {
1692 let names = theme_token_names("/* @theme { --color-fake: red; } */ .x { color: blue; }");
1693 assert!(names.is_empty(), "got {names:?}");
1694 }
1695
1696 #[test]
1697 fn theme_deduplicates_repeated_token() {
1698 let names = theme_token_names("@theme { --color-a: red; --color-a: blue; }");
1699 assert_eq!(names, vec!["color-a"]);
1700 }
1701
1702 #[test]
1703 fn apply_tokens_basic() {
1704 let tokens = extract_apply_tokens(".panel { @apply rounded-card font-bold; }");
1705 assert_eq!(tokens, vec!["rounded-card", "font-bold"]);
1706 }
1707
1708 #[test]
1709 fn apply_tokens_strips_important() {
1710 let tokens = extract_apply_tokens(".x { @apply text-brand! font-bold !important; }");
1711 assert_eq!(tokens, vec!["text-brand", "font-bold"]);
1712 }
1713
1714 #[test]
1715 fn apply_tokens_ignored_in_comments() {
1716 let tokens = extract_apply_tokens("/* @apply hidden-token; */ .x { color: red; }");
1717 assert!(tokens.is_empty(), "got {tokens:?}");
1718 }
1719}