1use std::path::Path;
7use std::sync::LazyLock;
8
9use oxc_span::Span;
10
11use crate::{ExportInfo, ExportName, ImportInfo, ImportedName, ModuleInfo, VisibilityTag};
12use fallow_types::discover::FileId;
13
14static CSS_IMPORT_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
17 regex::Regex::new(r#"@import\s+(?:url\(\s*(?:["']([^"']+)["']|([^)]+))\s*\)|["']([^"']+)["'])"#)
18 .expect("valid regex")
19});
20
21static SCSS_USE_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
24 regex::Regex::new(r#"@(?:use|forward)\s+["']([^"']+)["']"#).expect("valid regex")
25});
26
27static CSS_PLUGIN_RE: LazyLock<regex::Regex> =
30 LazyLock::new(|| regex::Regex::new(r#"@plugin\s+["']([^"']+)["']"#).expect("valid regex"));
31
32static CSS_APPLY_RE: LazyLock<regex::Regex> =
35 LazyLock::new(|| regex::Regex::new(r"@apply\s+[^;}\n]+").expect("valid regex"));
36
37static CSS_TAILWIND_RE: LazyLock<regex::Regex> =
40 LazyLock::new(|| regex::Regex::new(r"@tailwind\s+\w+").expect("valid regex"));
41
42static CSS_COMMENT_RE: LazyLock<regex::Regex> =
44 LazyLock::new(|| regex::Regex::new(r"(?s)/\*.*?\*/").expect("valid regex"));
45
46static SCSS_LINE_COMMENT_RE: LazyLock<regex::Regex> =
48 LazyLock::new(|| regex::Regex::new(r"//[^\n]*").expect("valid regex"));
49
50static CSS_CLASS_RE: LazyLock<regex::Regex> =
53 LazyLock::new(|| regex::Regex::new(r"\.([a-zA-Z_][a-zA-Z0-9_-]*)").expect("valid regex"));
54
55static CSS_NON_SELECTOR_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
58 regex::Regex::new(r#"(?s)"[^"]*"|'[^']*'|url\([^)]*\)"#).expect("valid regex")
59});
60
61static CSS_AT_RULE_PRELUDE_RE: LazyLock<regex::Regex> =
73 LazyLock::new(|| regex::Regex::new(r"@(?:layer|import)\b[^;{]*").expect("valid regex"));
74
75pub(crate) fn is_css_file(path: &Path) -> bool {
76 path.extension()
77 .and_then(|e| e.to_str())
78 .is_some_and(|ext| ext == "css" || ext == "scss")
79}
80
81#[derive(Debug, Clone, PartialEq, Eq)]
83pub struct CssImportSource {
84 pub raw: String,
86 pub normalized: String,
88 pub is_plugin: bool,
90 pub span: Span,
92}
93
94fn is_css_module_file(path: &Path) -> bool {
95 is_css_file(path)
96 && path
97 .file_stem()
98 .and_then(|s| s.to_str())
99 .is_some_and(|stem| stem.ends_with(".module"))
100}
101
102fn is_css_url_import(source: &str) -> bool {
104 source.starts_with("http://") || source.starts_with("https://") || source.starts_with("data:")
105}
106
107fn normalize_css_import_path(path: String, is_scss: bool) -> String {
120 if path.starts_with('.') || path.starts_with('/') || path.contains("://") {
121 return path;
122 }
123 if path.starts_with('@') && path.contains('/') {
126 return path;
127 }
128 let path_ref = std::path::Path::new(&path);
129 if !is_scss
130 && path.contains('/')
131 && path_ref
132 .extension()
133 .and_then(|e| e.to_str())
134 .is_some_and(is_style_extension)
135 {
136 return path;
137 }
138 let ext = std::path::Path::new(&path)
140 .extension()
141 .and_then(|e| e.to_str());
142 match ext {
143 Some(e) if is_style_extension(e) => format!("./{path}"),
144 _ => {
145 if is_scss && !path.contains(':') {
149 format!("./{path}")
150 } else {
151 path
152 }
153 }
154 }
155}
156
157fn is_style_extension(ext: &str) -> bool {
158 ext.eq_ignore_ascii_case("css")
159 || ext.eq_ignore_ascii_case("scss")
160 || ext.eq_ignore_ascii_case("sass")
161 || ext.eq_ignore_ascii_case("less")
162}
163
164#[cfg(test)]
166fn strip_css_comments(source: &str, is_scss: bool) -> String {
167 let stripped = CSS_COMMENT_RE.replace_all(source, "");
168 if is_scss {
169 SCSS_LINE_COMMENT_RE.replace_all(&stripped, "").into_owned()
170 } else {
171 stripped.into_owned()
172 }
173}
174
175fn mask_css_comments(source: &str, is_scss: bool) -> String {
176 let mut masked = mask_with_whitespace(source, &CSS_COMMENT_RE);
177 if is_scss {
178 masked = mask_with_whitespace(&masked, &SCSS_LINE_COMMENT_RE);
179 }
180 masked
181}
182
183fn normalize_css_plugin_path(path: String) -> String {
189 path
190}
191
192#[must_use]
198pub fn extract_css_import_sources(source: &str, is_scss: bool) -> Vec<CssImportSource> {
199 let stripped = mask_css_comments(source, is_scss);
200 let mut out = Vec::new();
201
202 for cap in CSS_IMPORT_RE.captures_iter(&stripped) {
203 let raw = cap.get(1).or_else(|| cap.get(2)).or_else(|| cap.get(3));
204 if let Some(m) = raw {
205 let (src, span) = trimmed_match_with_span(m);
206 if !src.is_empty() && !is_css_url_import(&src) {
207 out.push(CssImportSource {
208 normalized: normalize_css_import_path(src.clone(), is_scss),
209 raw: src,
210 is_plugin: false,
211 span,
212 });
213 }
214 }
215 }
216
217 if is_scss {
218 for cap in SCSS_USE_RE.captures_iter(&stripped) {
219 if let Some(m) = cap.get(1) {
220 let (raw, span) = trimmed_match_with_span(m);
221 out.push(CssImportSource {
222 normalized: normalize_css_import_path(raw.clone(), true),
223 raw,
224 is_plugin: false,
225 span,
226 });
227 }
228 }
229 }
230
231 for cap in CSS_PLUGIN_RE.captures_iter(&stripped) {
232 if let Some(m) = cap.get(1) {
233 let (raw, span) = trimmed_match_with_span(m);
234 if !raw.is_empty() && !is_css_url_import(&raw) {
235 out.push(CssImportSource {
236 normalized: normalize_css_plugin_path(raw.clone()),
237 raw,
238 is_plugin: true,
239 span,
240 });
241 }
242 }
243 }
244
245 out
246}
247
248fn trimmed_match_with_span(m: regex::Match<'_>) -> (String, Span) {
249 let raw = m.as_str();
250 let trimmed_start = raw.len() - raw.trim_start().len();
251 let trimmed_end = raw.trim_end().len();
252 let start = m.start() + trimmed_start;
253 let end = m.start() + trimmed_end;
254 (raw.trim().to_string(), Span::new(start as u32, end as u32))
255}
256
257#[must_use]
264pub fn extract_css_imports(source: &str, is_scss: bool) -> Vec<String> {
265 extract_css_import_sources(source, is_scss)
266 .into_iter()
267 .map(|source| source.normalized)
268 .collect()
269}
270
271fn mask_with_whitespace(src: &str, re: ®ex::Regex) -> String {
281 let mut out = String::with_capacity(src.len());
282 let mut cursor = 0;
283 for m in re.find_iter(src) {
284 out.push_str(&src[cursor..m.start()]);
285 for _ in m.start()..m.end() {
286 out.push(' ');
287 }
288 cursor = m.end();
289 }
290 out.push_str(&src[cursor..]);
291 out
292}
293
294pub fn extract_css_module_exports(source: &str, is_scss: bool) -> Vec<ExportInfo> {
301 let mut masked = mask_with_whitespace(source, &CSS_COMMENT_RE);
306 if is_scss {
307 masked = mask_with_whitespace(&masked, &SCSS_LINE_COMMENT_RE);
308 }
309 masked = mask_with_whitespace(&masked, &CSS_NON_SELECTOR_RE);
310 masked = mask_with_whitespace(&masked, &CSS_AT_RULE_PRELUDE_RE);
315
316 let mut seen = rustc_hash::FxHashSet::default();
317 let mut exports = Vec::new();
318 for cap in CSS_CLASS_RE.captures_iter(&masked) {
319 if let Some(m) = cap.get(1) {
320 let class_name = m.as_str().to_string();
321 if seen.insert(class_name.clone()) {
322 #[expect(
323 clippy::cast_possible_truncation,
324 reason = "CSS files exceeding u32::MAX bytes are not a realistic input"
325 )]
326 let span = Span::new(m.start() as u32, m.end() as u32);
327 exports.push(ExportInfo {
328 name: ExportName::Named(class_name),
329 local_name: None,
330 is_type_only: false,
331 visibility: VisibilityTag::None,
332 span,
333 members: Vec::new(),
334 is_side_effect_used: false,
335 super_class: None,
336 });
337 }
338 }
339 }
340 exports
341}
342
343pub(crate) fn parse_css_to_module(
345 file_id: FileId,
346 path: &Path,
347 source: &str,
348 content_hash: u64,
349) -> ModuleInfo {
350 let parsed_suppressions = crate::suppress::parse_suppressions_from_source(source);
351 let is_scss = path
352 .extension()
353 .and_then(|e| e.to_str())
354 .is_some_and(|ext| ext == "scss");
355
356 let stripped = mask_css_comments(source, is_scss);
359
360 let mut imports = Vec::new();
361
362 for source in extract_css_import_sources(source, is_scss) {
363 imports.push(ImportInfo {
364 source: source.normalized,
365 imported_name: if source.is_plugin {
366 ImportedName::Default
367 } else {
368 ImportedName::SideEffect
369 },
370 local_name: String::new(),
371 is_type_only: false,
372 from_style: false,
373 span: source.span,
374 source_span: source.span,
375 });
376 }
377
378 let has_apply = CSS_APPLY_RE.is_match(&stripped);
381 let has_tailwind = CSS_TAILWIND_RE.is_match(&stripped);
382 if has_apply || has_tailwind {
383 imports.push(ImportInfo {
384 source: "tailwindcss".to_string(),
385 imported_name: ImportedName::SideEffect,
386 local_name: String::new(),
387 is_type_only: false,
388 from_style: false,
389 span: Span::default(),
390 source_span: Span::default(),
391 });
392 }
393
394 let exports = if is_css_module_file(path) {
399 extract_css_module_exports(source, is_scss)
400 } else {
401 Vec::new()
402 };
403
404 ModuleInfo {
405 file_id,
406 exports,
407 imports,
408 re_exports: Vec::new(),
409 dynamic_imports: Vec::new(),
410 dynamic_import_patterns: Vec::new(),
411 require_calls: Vec::new(),
412 member_accesses: Vec::new(),
413 whole_object_uses: Vec::new(),
414 has_cjs_exports: false,
415 has_angular_component_template_url: false,
416 content_hash,
417 suppressions: parsed_suppressions.suppressions,
418 unknown_suppression_kinds: parsed_suppressions.unknown_kinds,
419 unused_import_bindings: Vec::new(),
420 type_referenced_import_bindings: Vec::new(),
421 value_referenced_import_bindings: Vec::new(),
422 line_offsets: fallow_types::extract::compute_line_offsets(source),
423 complexity: Vec::new(),
424 flag_uses: Vec::new(),
425 class_heritage: vec![],
426 local_type_declarations: Vec::new(),
427 public_signature_type_references: Vec::new(),
428 namespace_object_aliases: Vec::new(),
429 iconify_prefixes: Vec::new(),
430 auto_import_candidates: Vec::new(),
431 }
432}
433
434#[cfg(test)]
435mod tests {
436 use super::*;
437
438 fn export_names(source: &str) -> Vec<String> {
440 extract_css_module_exports(source, false)
441 .into_iter()
442 .filter_map(|e| match e.name {
443 ExportName::Named(n) => Some(n),
444 ExportName::Default => None,
445 })
446 .collect()
447 }
448
449 #[test]
452 fn is_css_file_css() {
453 assert!(is_css_file(Path::new("styles.css")));
454 }
455
456 #[test]
457 fn is_css_file_scss() {
458 assert!(is_css_file(Path::new("styles.scss")));
459 }
460
461 #[test]
462 fn is_css_file_rejects_js() {
463 assert!(!is_css_file(Path::new("app.js")));
464 }
465
466 #[test]
467 fn is_css_file_rejects_ts() {
468 assert!(!is_css_file(Path::new("app.ts")));
469 }
470
471 #[test]
472 fn is_css_file_rejects_less() {
473 assert!(!is_css_file(Path::new("styles.less")));
474 }
475
476 #[test]
477 fn is_css_file_rejects_no_extension() {
478 assert!(!is_css_file(Path::new("Makefile")));
479 }
480
481 #[test]
484 fn is_css_module_file_module_css() {
485 assert!(is_css_module_file(Path::new("Component.module.css")));
486 }
487
488 #[test]
489 fn is_css_module_file_module_scss() {
490 assert!(is_css_module_file(Path::new("Component.module.scss")));
491 }
492
493 #[test]
494 fn is_css_module_file_rejects_plain_css() {
495 assert!(!is_css_module_file(Path::new("styles.css")));
496 }
497
498 #[test]
499 fn is_css_module_file_rejects_plain_scss() {
500 assert!(!is_css_module_file(Path::new("styles.scss")));
501 }
502
503 #[test]
504 fn is_css_module_file_rejects_module_js() {
505 assert!(!is_css_module_file(Path::new("utils.module.js")));
506 }
507
508 #[test]
511 fn extracts_single_class() {
512 let names = export_names(".foo { color: red; }");
513 assert_eq!(names, vec!["foo"]);
514 }
515
516 #[test]
517 fn extracts_multiple_classes() {
518 let names = export_names(".foo { } .bar { }");
519 assert_eq!(names, vec!["foo", "bar"]);
520 }
521
522 #[test]
523 fn extracts_nested_classes() {
524 let names = export_names(".foo .bar { color: red; }");
525 assert!(names.contains(&"foo".to_string()));
526 assert!(names.contains(&"bar".to_string()));
527 }
528
529 #[test]
530 fn extracts_hyphenated_class() {
531 let names = export_names(".my-class { }");
532 assert_eq!(names, vec!["my-class"]);
533 }
534
535 #[test]
536 fn extracts_camel_case_class() {
537 let names = export_names(".myClass { }");
538 assert_eq!(names, vec!["myClass"]);
539 }
540
541 #[test]
542 fn extracts_underscore_class() {
543 let names = export_names("._hidden { } .__wrapper { }");
544 assert!(names.contains(&"_hidden".to_string()));
545 assert!(names.contains(&"__wrapper".to_string()));
546 }
547
548 #[test]
551 fn pseudo_selector_hover() {
552 let names = export_names(".foo:hover { color: blue; }");
553 assert_eq!(names, vec!["foo"]);
554 }
555
556 #[test]
557 fn pseudo_selector_focus() {
558 let names = export_names(".input:focus { outline: none; }");
559 assert_eq!(names, vec!["input"]);
560 }
561
562 #[test]
563 fn pseudo_element_before() {
564 let names = export_names(".icon::before { content: ''; }");
565 assert_eq!(names, vec!["icon"]);
566 }
567
568 #[test]
569 fn combined_pseudo_selectors() {
570 let names = export_names(".btn:hover, .btn:active, .btn:focus { }");
571 assert_eq!(names, vec!["btn"]);
573 }
574
575 #[test]
578 fn classes_inside_media_query() {
579 let names = export_names(
580 "@media (max-width: 768px) { .mobile-nav { display: block; } .desktop-nav { display: none; } }",
581 );
582 assert!(names.contains(&"mobile-nav".to_string()));
583 assert!(names.contains(&"desktop-nav".to_string()));
584 }
585
586 #[test]
587 fn classes_inside_multi_line_media_query() {
588 let names =
593 export_names("@media\n screen and (min-width: 600px)\n{\n .real { color: red; }\n}");
594 assert_eq!(names, vec!["real"]);
595 }
596
597 #[test]
600 fn at_layer_statement_does_not_export() {
601 let names = export_names("@layer foo.bar;");
604 assert!(names.is_empty(), "got {names:?}");
605 let names = export_names("@layer foo.bar, foo.baz;");
606 assert!(names.is_empty(), "got {names:?}");
607 }
608
609 #[test]
610 fn at_layer_block_keeps_body_classes() {
611 let names = export_names("@layer foo.bar { .root { color: red; } }");
614 assert_eq!(names, vec!["root"]);
615 }
616
617 #[test]
618 fn at_layer_multiline_prelude_keeps_body_classes() {
619 let names = export_names("@layer\n foo.bar\n{ .root { color: red; } }");
622 assert_eq!(names, vec!["root"]);
623 }
624
625 #[test]
626 fn at_layer_with_nested_media_keeps_body() {
627 let names =
631 export_names("@layer foo.bar { @media (max-width: 768px) { .real { color: red; } } }");
632 assert_eq!(names, vec!["real"]);
633 }
634
635 #[test]
636 fn at_import_with_layer_attribute_does_not_export() {
637 let names = export_names(r#"@import url("x.css") layer(theme.button);"#);
642 assert!(names.is_empty(), "got {names:?}");
643 }
644
645 #[test]
646 fn class_then_at_layer_does_not_leak_prelude() {
647 let names =
650 export_names(".outer { color: blue; } @layer foo.bar { .inner { color: red; } }");
651 assert_eq!(names, vec!["outer", "inner"]);
652 }
653
654 #[test]
657 fn at_scope_keeps_selector_list_classes() {
658 let names = export_names("@scope (.parent) to (.child) { .title { color: red; } }");
663 assert!(names.contains(&"parent".to_string()), "got {names:?}");
664 assert!(names.contains(&"child".to_string()), "got {names:?}");
665 assert!(names.contains(&"title".to_string()), "got {names:?}");
666 }
667
668 #[test]
669 fn at_keyframes_numeric_step_is_not_class() {
670 let names = export_names(
676 "@keyframes slide { 0% { transform: scale(.5); } 100% { transform: scale(1); } }",
677 );
678 assert!(names.is_empty(), "got {names:?}");
679 }
680
681 #[test]
682 fn at_webkit_keyframes_keeps_body_classes() {
683 let names = export_names("@-webkit-keyframes slide { 0% { } 100% { } } .real { }");
687 assert_eq!(names, vec!["real"]);
688 }
689
690 #[test]
693 fn deduplicates_repeated_class() {
694 let names = export_names(".btn { color: red; } .btn { font-size: 14px; }");
695 assert_eq!(names.iter().filter(|n| *n == "btn").count(), 1);
696 }
697
698 #[test]
701 fn empty_source() {
702 let names = export_names("");
703 assert!(names.is_empty());
704 }
705
706 #[test]
707 fn no_classes() {
708 let names = export_names("body { margin: 0; } * { box-sizing: border-box; }");
709 assert!(names.is_empty());
710 }
711
712 #[test]
713 fn ignores_classes_in_block_comments() {
714 let names = export_names("/* .fake { } */ .real { }");
717 assert!(!names.contains(&"fake".to_string()));
718 assert!(names.contains(&"real".to_string()));
719 }
720
721 #[test]
722 fn ignores_classes_in_scss_line_comments() {
723 let exports = extract_css_module_exports("// .fake\n.real { }", true);
724 let names: Vec<_> = exports
725 .iter()
726 .filter_map(|e| match &e.name {
727 ExportName::Named(n) => Some(n.as_str()),
728 ExportName::Default => None,
729 })
730 .collect();
731 assert_eq!(names, vec!["real"]);
732 }
733
734 #[test]
735 fn ignores_classes_in_strings() {
736 let names = export_names(r#".real { content: ".fake"; }"#);
737 assert!(names.contains(&"real".to_string()));
738 assert!(!names.contains(&"fake".to_string()));
739 }
740
741 #[test]
742 fn ignores_classes_in_url() {
743 let names = export_names(".real { background: url(./images/hero.png); }");
744 assert!(names.contains(&"real".to_string()));
745 assert!(!names.contains(&"png".to_string()));
747 }
748
749 #[test]
752 fn strip_css_block_comment() {
753 let result = strip_css_comments("/* removed */ .kept { }", false);
754 assert!(!result.contains("removed"));
755 assert!(result.contains(".kept"));
756 }
757
758 #[test]
759 fn strip_scss_line_comment() {
760 let result = strip_css_comments("// removed\n.kept { }", true);
761 assert!(!result.contains("removed"));
762 assert!(result.contains(".kept"));
763 }
764
765 #[test]
766 fn strip_scss_preserves_css_outside_comments() {
767 let source = "// line comment\n/* block comment */\n.visible { color: red; }";
768 let result = strip_css_comments(source, true);
769 assert!(result.contains(".visible"));
770 }
771
772 #[test]
775 fn url_import_http() {
776 assert!(is_css_url_import("http://example.com/style.css"));
777 }
778
779 #[test]
780 fn url_import_https() {
781 assert!(is_css_url_import("https://fonts.googleapis.com/css"));
782 }
783
784 #[test]
785 fn url_import_data() {
786 assert!(is_css_url_import("data:text/css;base64,abc"));
787 }
788
789 #[test]
790 fn url_import_local_not_skipped() {
791 assert!(!is_css_url_import("./local.css"));
792 }
793
794 #[test]
795 fn url_import_bare_specifier_not_skipped() {
796 assert!(!is_css_url_import("tailwindcss"));
797 }
798
799 #[test]
802 fn normalize_relative_dot_path_unchanged() {
803 assert_eq!(
804 normalize_css_import_path("./reset.css".to_string(), false),
805 "./reset.css"
806 );
807 }
808
809 #[test]
810 fn normalize_parent_relative_path_unchanged() {
811 assert_eq!(
812 normalize_css_import_path("../shared.scss".to_string(), false),
813 "../shared.scss"
814 );
815 }
816
817 #[test]
818 fn normalize_absolute_path_unchanged() {
819 assert_eq!(
820 normalize_css_import_path("/styles/main.css".to_string(), false),
821 "/styles/main.css"
822 );
823 }
824
825 #[test]
826 fn normalize_url_unchanged() {
827 assert_eq!(
828 normalize_css_import_path("https://example.com/style.css".to_string(), false),
829 "https://example.com/style.css"
830 );
831 }
832
833 #[test]
834 fn normalize_bare_css_gets_dot_slash() {
835 assert_eq!(
836 normalize_css_import_path("app.css".to_string(), false),
837 "./app.css"
838 );
839 }
840
841 #[test]
842 fn normalize_css_package_subpath_stays_bare() {
843 assert_eq!(
844 normalize_css_import_path("tailwindcss/theme.css".to_string(), false),
845 "tailwindcss/theme.css"
846 );
847 }
848
849 #[test]
850 fn normalize_css_package_subpath_with_dotted_name_stays_bare() {
851 assert_eq!(
852 normalize_css_import_path("highlight.js/styles/github.css".to_string(), false),
853 "highlight.js/styles/github.css"
854 );
855 }
856
857 #[test]
858 fn normalize_bare_scss_gets_dot_slash() {
859 assert_eq!(
860 normalize_css_import_path("vars.scss".to_string(), false),
861 "./vars.scss"
862 );
863 }
864
865 #[test]
866 fn normalize_bare_sass_gets_dot_slash() {
867 assert_eq!(
868 normalize_css_import_path("main.sass".to_string(), false),
869 "./main.sass"
870 );
871 }
872
873 #[test]
874 fn normalize_bare_less_gets_dot_slash() {
875 assert_eq!(
876 normalize_css_import_path("theme.less".to_string(), false),
877 "./theme.less"
878 );
879 }
880
881 #[test]
882 fn normalize_bare_js_extension_stays_bare() {
883 assert_eq!(
884 normalize_css_import_path("module.js".to_string(), false),
885 "module.js"
886 );
887 }
888
889 #[test]
892 fn normalize_scss_bare_partial_gets_dot_slash() {
893 assert_eq!(
894 normalize_css_import_path("variables".to_string(), true),
895 "./variables"
896 );
897 }
898
899 #[test]
900 fn normalize_scss_bare_partial_with_subdir_gets_dot_slash() {
901 assert_eq!(
902 normalize_css_import_path("base/reset".to_string(), true),
903 "./base/reset"
904 );
905 }
906
907 #[test]
908 fn normalize_scss_builtin_stays_bare() {
909 assert_eq!(
910 normalize_css_import_path("sass:math".to_string(), true),
911 "sass:math"
912 );
913 }
914
915 #[test]
916 fn normalize_scss_relative_path_unchanged() {
917 assert_eq!(
918 normalize_css_import_path("../styles/variables".to_string(), true),
919 "../styles/variables"
920 );
921 }
922
923 #[test]
924 fn normalize_css_bare_extensionless_stays_bare() {
925 assert_eq!(
927 normalize_css_import_path("tailwindcss".to_string(), false),
928 "tailwindcss"
929 );
930 }
931
932 #[test]
935 fn normalize_scoped_package_with_css_extension_stays_bare() {
936 assert_eq!(
937 normalize_css_import_path("@fontsource/monaspace-neon/400.css".to_string(), false),
938 "@fontsource/monaspace-neon/400.css"
939 );
940 }
941
942 #[test]
943 fn normalize_scoped_package_with_scss_extension_stays_bare() {
944 assert_eq!(
945 normalize_css_import_path("@company/design-system/tokens.scss".to_string(), true),
946 "@company/design-system/tokens.scss"
947 );
948 }
949
950 #[test]
951 fn normalize_scoped_package_without_extension_stays_bare() {
952 assert_eq!(
953 normalize_css_import_path("@fallow/design-system/styles".to_string(), false),
954 "@fallow/design-system/styles"
955 );
956 }
957
958 #[test]
959 fn normalize_scoped_package_extensionless_scss_stays_bare() {
960 assert_eq!(
961 normalize_css_import_path("@company/tokens".to_string(), true),
962 "@company/tokens"
963 );
964 }
965
966 #[test]
967 fn normalize_path_alias_with_css_extension_stays_bare() {
968 assert_eq!(
973 normalize_css_import_path("@/components/Button.css".to_string(), false),
974 "@/components/Button.css"
975 );
976 }
977
978 #[test]
979 fn normalize_path_alias_extensionless_stays_bare() {
980 assert_eq!(
981 normalize_css_import_path("@/styles/variables".to_string(), false),
982 "@/styles/variables"
983 );
984 }
985
986 #[test]
989 fn strip_css_no_comments() {
990 let source = ".foo { color: red; }";
991 assert_eq!(strip_css_comments(source, false), source);
992 }
993
994 #[test]
995 fn strip_css_multiple_block_comments() {
996 let source = "/* comment-one */ .foo { } /* comment-two */ .bar { }";
997 let result = strip_css_comments(source, false);
998 assert!(!result.contains("comment-one"));
999 assert!(!result.contains("comment-two"));
1000 assert!(result.contains(".foo"));
1001 assert!(result.contains(".bar"));
1002 }
1003
1004 #[test]
1005 fn strip_scss_does_not_affect_non_scss() {
1006 let source = "// this stays\n.foo { }";
1008 let result = strip_css_comments(source, false);
1009 assert!(result.contains("// this stays"));
1010 }
1011
1012 #[test]
1015 fn css_module_parses_suppressions() {
1016 let info = parse_css_to_module(
1017 fallow_types::discover::FileId(0),
1018 Path::new("Component.module.css"),
1019 "/* fallow-ignore-file */\n.btn { color: red; }",
1020 0,
1021 );
1022 assert!(!info.suppressions.is_empty());
1023 assert_eq!(info.suppressions[0].line, 0);
1024 }
1025
1026 #[test]
1029 fn extracts_class_starting_with_underscore() {
1030 let names = export_names("._private { } .__dunder { }");
1031 assert!(names.contains(&"_private".to_string()));
1032 assert!(names.contains(&"__dunder".to_string()));
1033 }
1034
1035 #[test]
1036 fn ignores_id_selectors() {
1037 let names = export_names("#myId { color: red; }");
1038 assert!(!names.contains(&"myId".to_string()));
1039 }
1040
1041 #[test]
1042 fn ignores_element_selectors() {
1043 let names = export_names("div { color: red; } span { }");
1044 assert!(names.is_empty());
1045 }
1046
1047 #[test]
1050 fn extract_css_imports_at_import_quoted() {
1051 let imports = extract_css_imports(r#"@import "./reset.css";"#, false);
1052 assert_eq!(imports, vec!["./reset.css"]);
1053 }
1054
1055 #[test]
1056 fn extract_css_imports_package_subpath_stays_bare() {
1057 let imports =
1058 extract_css_imports(r#"@import "tailwindcss/theme.css" layer(theme);"#, false);
1059 assert_eq!(imports, vec!["tailwindcss/theme.css"]);
1060 }
1061
1062 #[test]
1063 fn extract_css_imports_at_import_url() {
1064 let imports = extract_css_imports(r#"@import url("./reset.css");"#, false);
1065 assert_eq!(imports, vec!["./reset.css"]);
1066 }
1067
1068 #[test]
1069 fn extract_css_imports_skips_remote_urls() {
1070 let imports =
1071 extract_css_imports(r#"@import "https://fonts.example.com/font.css";"#, false);
1072 assert!(imports.is_empty());
1073 }
1074
1075 #[test]
1076 fn extract_css_imports_scss_use_normalizes_partial() {
1077 let imports = extract_css_imports(r#"@use "variables";"#, true);
1078 assert_eq!(imports, vec!["./variables"]);
1079 }
1080
1081 #[test]
1082 fn extract_css_imports_scss_forward_normalizes_partial() {
1083 let imports = extract_css_imports(r#"@forward "tokens";"#, true);
1084 assert_eq!(imports, vec!["./tokens"]);
1085 }
1086
1087 #[test]
1088 fn extract_css_imports_skips_comments() {
1089 let imports = extract_css_imports(
1090 r#"/* @import "./hidden.scss"; */
1091@use "real";"#,
1092 true,
1093 );
1094 assert_eq!(imports, vec!["./real"]);
1095 }
1096
1097 #[test]
1098 fn extract_css_imports_at_plugin_keeps_package_bare() {
1099 let imports = extract_css_imports(r#"@plugin "daisyui";"#, true);
1100 assert_eq!(imports, vec!["daisyui"]);
1101 }
1102
1103 #[test]
1104 fn extract_css_imports_at_plugin_tracks_relative_file() {
1105 let imports = extract_css_imports(r#"@plugin "./tailwind-plugin.js";"#, false);
1106 assert_eq!(imports, vec!["./tailwind-plugin.js"]);
1107 }
1108
1109 #[test]
1110 fn extract_css_imports_scss_at_import_kept_relative() {
1111 let imports = extract_css_imports(r"@import 'Foo';", true);
1112 assert_eq!(imports, vec!["./Foo"]);
1114 }
1115
1116 #[test]
1117 fn extract_css_imports_additional_data_string_body() {
1118 let body = r#"@use "./src/styles/global.scss";"#;
1120 let imports = extract_css_imports(body, true);
1121 assert_eq!(imports, vec!["./src/styles/global.scss"]);
1122 }
1123
1124 #[test]
1127 fn mask_with_whitespace_preserves_byte_length() {
1128 let src = "/* hello */ .foo { }";
1129 let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
1130 assert_eq!(masked.len(), src.len());
1131 assert!(masked.is_char_boundary(src.len()));
1132 }
1133
1134 #[test]
1135 fn mask_with_whitespace_preserves_offsets_around_multibyte() {
1136 let src = "/* \u{2713} */ .foo { }";
1140 let foo_offset = src.find(".foo").expect("`.foo` present");
1141 let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
1142 assert_eq!(masked.len(), src.len());
1143 assert_eq!(masked.find(".foo"), Some(foo_offset));
1144 }
1145
1146 fn span_line_col(source: &str, start: u32) -> (u32, u32) {
1151 let offsets = fallow_types::extract::compute_line_offsets(source);
1152 fallow_types::extract::byte_offset_to_line_col(&offsets, start)
1153 }
1154
1155 #[test]
1156 fn span_points_at_real_class_declaration_line() {
1157 let source = "\n\n\n\n.foo { color: red; }\n";
1158 let exports = extract_css_module_exports(source, false);
1159 assert_eq!(exports.len(), 1);
1160 let span = exports[0].span;
1161 let (line, col) = span_line_col(source, span.start);
1162 assert_eq!(line, 5, "`.foo` on line 5 must produce line 5, not line 1");
1163 assert_eq!(
1166 col, 1,
1167 "column points at `f` in `.foo` (post-dot identifier)"
1168 );
1169 assert_eq!(
1172 &source[span.start as usize..span.end as usize],
1173 "foo",
1174 "span range must slice to the class identifier in the original source"
1175 );
1176 }
1177
1178 #[test]
1179 fn span_survives_multibyte_comment_prefix() {
1180 let source = "/* \u{2713} */\n.foo { }";
1184 let exports = extract_css_module_exports(source, false);
1185 assert_eq!(exports.len(), 1);
1186 let span = exports[0].span;
1187 assert!(
1188 source.is_char_boundary(span.start as usize),
1189 "span.start must lie on a UTF-8 char boundary"
1190 );
1191 assert_eq!(&source[span.start as usize..span.end as usize], "foo");
1192 }
1193
1194 #[test]
1195 fn span_skips_at_layer_prelude_dot_segments() {
1196 let source = "@layer foo.bar { }\n.root { }\n";
1200 let exports = extract_css_module_exports(source, false);
1201 let names: Vec<_> = exports
1202 .iter()
1203 .filter_map(|e| match &e.name {
1204 ExportName::Named(n) => Some(n.as_str()),
1205 ExportName::Default => None,
1206 })
1207 .collect();
1208 assert_eq!(names, vec!["root"], "@layer sub-segments must not export");
1209 let span = exports[0].span;
1210 let (line, _col) = span_line_col(source, span.start);
1211 assert_eq!(line, 2, "`.root` lives on line 2 of the original source");
1212 assert_eq!(&source[span.start as usize..span.end as usize], "root");
1213 }
1214
1215 #[test]
1216 fn span_skips_classes_in_strings() {
1217 let source = ".real { content: \".fake\"; }\n.also-real { }\n";
1218 let exports = extract_css_module_exports(source, false);
1219 let names: Vec<_> = exports
1220 .iter()
1221 .filter_map(|e| match &e.name {
1222 ExportName::Named(n) => Some(n.as_str()),
1223 ExportName::Default => None,
1224 })
1225 .collect();
1226 assert_eq!(names, vec!["real", "also-real"]);
1227 for export in &exports {
1229 let span = export.span;
1230 let slice = &source[span.start as usize..span.end as usize];
1231 match &export.name {
1232 ExportName::Named(n) => assert_eq!(slice, n.as_str()),
1233 ExportName::Default => unreachable!("CSS modules emit only named exports"),
1234 }
1235 }
1236 }
1237
1238 #[test]
1239 fn span_deduplicates_to_first_occurrence() {
1240 let source = ".btn { color: red; }\n.btn { color: blue; }\n";
1241 let exports = extract_css_module_exports(source, false);
1242 assert_eq!(exports.len(), 1);
1243 let (line, _col) = span_line_col(source, exports[0].span.start);
1244 assert_eq!(
1245 line, 1,
1246 "first occurrence wins for deduplicated class names"
1247 );
1248 }
1249
1250 #[test]
1251 fn span_inside_media_query() {
1252 let source =
1253 "@media (max-width: 768px) {\n .mobile { display: block; }\n .desktop { }\n}\n";
1254 let exports = extract_css_module_exports(source, false);
1255 let by_name: rustc_hash::FxHashMap<&str, oxc_span::Span> = exports
1256 .iter()
1257 .filter_map(|e| match &e.name {
1258 ExportName::Named(n) => Some((n.as_str(), e.span)),
1259 ExportName::Default => None,
1260 })
1261 .collect();
1262 let mobile_line = span_line_col(source, by_name["mobile"].start).0;
1263 let desktop_line = span_line_col(source, by_name["desktop"].start).0;
1264 assert_eq!(mobile_line, 2);
1265 assert_eq!(desktop_line, 3);
1266 }
1267
1268 #[test]
1269 fn at_layer_only_module_emits_no_exports() {
1270 let exports = extract_css_module_exports("@layer foo.bar, foo.baz;\n", false);
1273 assert!(exports.is_empty());
1274 }
1275
1276 #[test]
1277 fn parse_css_to_module_resolves_real_line_offsets() {
1278 let source = "\n\n\n\n.foo { color: red; }\n";
1282 let info = parse_css_to_module(
1283 fallow_types::discover::FileId(0),
1284 Path::new("Component.module.css"),
1285 source,
1286 0,
1287 );
1288 assert_eq!(info.exports.len(), 1);
1289 let (line, _col) = fallow_types::extract::byte_offset_to_line_col(
1290 &info.line_offsets,
1291 info.exports[0].span.start,
1292 );
1293 assert_eq!(line, 5, "downstream line must equal the source line");
1294 }
1295}