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