Skip to main content

fallow_extract/
css.rs

1//! CSS/SCSS file parsing and CSS Module class name extraction.
2//!
3//! Handles `@import`, `@use`, `@forward`, `@plugin`, `@apply`, `@tailwind` directives,
4//! and extracts class names as named exports from `.module.css`/`.module.scss` files.
5
6use 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
14/// Regex to extract CSS @import sources.
15/// Matches: @import "path"; @import 'path'; @import url("path"); @import url('path'); @import url(path);
16static CSS_IMPORT_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
17    regex::Regex::new(r#"@import\s+(?:url\(\s*(?:["']([^"']+)["']|([^)]+))\s*\)|["']([^"']+)["'])"#)
18        .expect("valid regex")
19});
20
21/// Regex to extract SCSS @use and @forward sources.
22/// Matches: @use "path"; @use 'path'; @forward "path"; @forward 'path';
23static SCSS_USE_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
24    regex::Regex::new(r#"@(?:use|forward)\s+["']([^"']+)["']"#).expect("valid regex")
25});
26
27/// Regex to extract Tailwind CSS @plugin sources.
28/// Matches: @plugin "package"; @plugin 'package'; @plugin "./local-plugin.js";
29static CSS_PLUGIN_RE: LazyLock<regex::Regex> =
30    LazyLock::new(|| regex::Regex::new(r#"@plugin\s+["']([^"']+)["']"#).expect("valid regex"));
31
32/// Regex to extract @apply class references.
33/// Matches: @apply class1 class2 class3;
34static CSS_APPLY_RE: LazyLock<regex::Regex> =
35    LazyLock::new(|| regex::Regex::new(r"@apply\s+[^;}\n]+").expect("valid regex"));
36
37/// Regex to extract @tailwind directives.
38/// Matches: @tailwind base; @tailwind components; @tailwind utilities;
39static CSS_TAILWIND_RE: LazyLock<regex::Regex> =
40    LazyLock::new(|| regex::Regex::new(r"@tailwind\s+\w+").expect("valid regex"));
41
42/// Regex to match CSS block comments (`/* ... */`) for stripping before extraction.
43static CSS_COMMENT_RE: LazyLock<regex::Regex> =
44    LazyLock::new(|| regex::Regex::new(r"(?s)/\*.*?\*/").expect("valid regex"));
45
46/// Regex to match SCSS single-line comments (`// ...`) for stripping before extraction.
47static SCSS_LINE_COMMENT_RE: LazyLock<regex::Regex> =
48    LazyLock::new(|| regex::Regex::new(r"//[^\n]*").expect("valid regex"));
49
50/// Regex to extract CSS class names from selectors.
51/// Matches `.className` in selectors. Applied after stripping comments, strings, and URLs.
52static CSS_CLASS_RE: LazyLock<regex::Regex> =
53    LazyLock::new(|| regex::Regex::new(r"\.([a-zA-Z_][a-zA-Z0-9_-]*)").expect("valid regex"));
54
55/// Regex to strip quoted strings and `url(...)` content from CSS before class extraction.
56/// Prevents false positives from `content: ".foo"` and `url(./path/file.ext)`.
57static CSS_NON_SELECTOR_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
58    regex::Regex::new(r#"(?s)"[^"]*"|'[^']*'|url\([^)]*\)"#).expect("valid regex")
59});
60
61pub(crate) fn is_css_file(path: &Path) -> bool {
62    path.extension()
63        .and_then(|e| e.to_str())
64        .is_some_and(|ext| ext == "css" || ext == "scss")
65}
66
67/// A CSS import source with both the literal source and fallow's resolver-normalized form.
68#[derive(Debug, Clone, PartialEq, Eq)]
69pub struct CssImportSource {
70    /// The import source exactly as it appeared in `@import` / `@use` / `@forward` / `@plugin`.
71    pub raw: String,
72    /// The source normalized for fallow's resolver (`variables` -> `./variables` in SCSS).
73    pub normalized: String,
74    /// Whether this source came from Tailwind CSS `@plugin`.
75    pub is_plugin: bool,
76}
77
78fn is_css_module_file(path: &Path) -> bool {
79    is_css_file(path)
80        && path
81            .file_stem()
82            .and_then(|s| s.to_str())
83            .is_some_and(|stem| stem.ends_with(".module"))
84}
85
86/// Returns true if a CSS import source is a remote URL or data URI that should be skipped.
87fn is_css_url_import(source: &str) -> bool {
88    source.starts_with("http://") || source.starts_with("https://") || source.starts_with("data:")
89}
90
91/// Normalize a CSS/SCSS import path to use `./` prefix for relative paths.
92/// Bare file names such as `reset.css` stay relative for CSS ergonomics, while
93/// package subpaths such as `tailwindcss/theme.css` stay bare so bundler-style
94/// package CSS imports resolve through `node_modules`.
95///
96/// When `is_scss` is true, extensionless specifiers that are not SCSS built-in
97/// modules (`sass:*`) are treated as relative imports (SCSS partial convention).
98/// This handles `@use 'variables'` resolving to `./_variables.scss`.
99///
100/// Scoped npm packages (`@scope/pkg`) are always kept bare, even when they have
101/// CSS extensions (e.g., `@fontsource/monaspace-neon/400.css`). Bundlers like
102/// Vite resolve these from node_modules, not as relative paths.
103fn normalize_css_import_path(path: String, is_scss: bool) -> String {
104    if path.starts_with('.') || path.starts_with('/') || path.contains("://") {
105        return path;
106    }
107    // Scoped npm packages (`@scope/...`) are always bare specifiers resolved
108    // from node_modules, regardless of file extension.
109    if path.starts_with('@') && path.contains('/') {
110        return path;
111    }
112    let path_ref = std::path::Path::new(&path);
113    if !is_scss
114        && path.contains('/')
115        && path_ref
116            .extension()
117            .and_then(|e| e.to_str())
118            .is_some_and(is_style_extension)
119    {
120        return path;
121    }
122    // Bare filenames with CSS/SCSS extensions are relative file imports.
123    let ext = std::path::Path::new(&path)
124        .extension()
125        .and_then(|e| e.to_str());
126    match ext {
127        Some(e) if is_style_extension(e) => format!("./{path}"),
128        _ => {
129            // In SCSS, extensionless bare specifiers like `@use 'variables'` are
130            // local partials, not npm packages. SCSS built-in modules (`sass:math`,
131            // `sass:color`) use a colon prefix and should stay bare.
132            if is_scss && !path.contains(':') {
133                format!("./{path}")
134            } else {
135                path
136            }
137        }
138    }
139}
140
141fn is_style_extension(ext: &str) -> bool {
142    ext.eq_ignore_ascii_case("css")
143        || ext.eq_ignore_ascii_case("scss")
144        || ext.eq_ignore_ascii_case("sass")
145        || ext.eq_ignore_ascii_case("less")
146}
147
148/// Strip comments from CSS/SCSS source to avoid matching directives inside comments.
149fn strip_css_comments(source: &str, is_scss: bool) -> String {
150    let stripped = CSS_COMMENT_RE.replace_all(source, "");
151    if is_scss {
152        SCSS_LINE_COMMENT_RE.replace_all(&stripped, "").into_owned()
153    } else {
154        stripped.into_owned()
155    }
156}
157
158/// Normalize a Tailwind CSS `@plugin` target.
159///
160/// Unlike SCSS `@use`, extensionless targets such as `daisyui` are package
161/// specifiers, not local partials. Keep bare specifiers bare and only preserve
162/// explicit relative/root-relative paths.
163fn normalize_css_plugin_path(path: String) -> String {
164    path
165}
166
167/// Extract `@import` / `@use` / `@forward` / `@plugin` source paths from a CSS/SCSS string.
168///
169/// Returns both the raw source and the normalized source. URL imports
170/// (`http://`, `https://`, `data:`) are skipped. Use [`extract_css_imports`]
171/// when only the normalized form is needed.
172#[must_use]
173pub fn extract_css_import_sources(source: &str, is_scss: bool) -> Vec<CssImportSource> {
174    let stripped = strip_css_comments(source, is_scss);
175    let mut out = Vec::new();
176
177    for cap in CSS_IMPORT_RE.captures_iter(&stripped) {
178        let raw = cap
179            .get(1)
180            .or_else(|| cap.get(2))
181            .or_else(|| cap.get(3))
182            .map(|m| m.as_str().trim().to_string());
183        if let Some(src) = raw
184            && !src.is_empty()
185            && !is_css_url_import(&src)
186        {
187            out.push(CssImportSource {
188                normalized: normalize_css_import_path(src.clone(), is_scss),
189                raw: src,
190                is_plugin: false,
191            });
192        }
193    }
194
195    if is_scss {
196        for cap in SCSS_USE_RE.captures_iter(&stripped) {
197            if let Some(m) = cap.get(1) {
198                let raw = m.as_str().to_string();
199                out.push(CssImportSource {
200                    normalized: normalize_css_import_path(raw.clone(), true),
201                    raw,
202                    is_plugin: false,
203                });
204            }
205        }
206    }
207
208    for cap in CSS_PLUGIN_RE.captures_iter(&stripped) {
209        if let Some(m) = cap.get(1) {
210            let raw = m.as_str().trim().to_string();
211            if !raw.is_empty() && !is_css_url_import(&raw) {
212                out.push(CssImportSource {
213                    normalized: normalize_css_plugin_path(raw.clone()),
214                    raw,
215                    is_plugin: true,
216                });
217            }
218        }
219    }
220
221    out
222}
223
224/// Extract normalized `@import` / `@use` / `@forward` / `@plugin` source paths from a CSS/SCSS string.
225///
226/// Returns specifiers normalized via `normalize_css_import_path`. URL imports
227/// (`http://`, `https://`, `data:`) are skipped. Used by callers that only need
228/// entry/dependency source paths; callers that need import kind information
229/// should use [`extract_css_import_sources`].
230#[must_use]
231pub fn extract_css_imports(source: &str, is_scss: bool) -> Vec<String> {
232    extract_css_import_sources(source, is_scss)
233        .into_iter()
234        .map(|source| source.normalized)
235        .collect()
236}
237
238/// Extract class names from a CSS module file as named exports.
239pub fn extract_css_module_exports(source: &str) -> Vec<ExportInfo> {
240    let cleaned = CSS_NON_SELECTOR_RE.replace_all(source, "");
241    let mut seen = rustc_hash::FxHashSet::default();
242    let mut exports = Vec::new();
243    for cap in CSS_CLASS_RE.captures_iter(&cleaned) {
244        if let Some(m) = cap.get(1) {
245            let class_name = m.as_str().to_string();
246            if seen.insert(class_name.clone()) {
247                exports.push(ExportInfo {
248                    name: ExportName::Named(class_name),
249                    local_name: None,
250                    is_type_only: false,
251                    visibility: VisibilityTag::None,
252                    span: Span::default(),
253                    members: Vec::new(),
254                    is_side_effect_used: false,
255                    super_class: None,
256                });
257            }
258        }
259    }
260    exports
261}
262
263/// Parse a CSS/SCSS file, extracting @import, @use, @forward, @plugin, @apply, and @tailwind directives.
264pub(crate) fn parse_css_to_module(
265    file_id: FileId,
266    path: &Path,
267    source: &str,
268    content_hash: u64,
269) -> ModuleInfo {
270    let parsed_suppressions = crate::suppress::parse_suppressions_from_source(source);
271    let is_scss = path
272        .extension()
273        .and_then(|e| e.to_str())
274        .is_some_and(|ext| ext == "scss");
275
276    // Strip comments before matching to avoid false positives from commented-out code.
277    let stripped = strip_css_comments(source, is_scss);
278
279    let mut imports = Vec::new();
280
281    // Extract @import statements
282    for cap in CSS_IMPORT_RE.captures_iter(&stripped) {
283        let source_path = cap
284            .get(1)
285            .or_else(|| cap.get(2))
286            .or_else(|| cap.get(3))
287            .map(|m| m.as_str().trim().to_string());
288        if let Some(src) = source_path
289            && !src.is_empty()
290            && !is_css_url_import(&src)
291        {
292            // CSS/SCSS @import resolves relative paths without ./ prefix,
293            // so normalize to ./ to avoid bare-specifier misclassification
294            let src = normalize_css_import_path(src, is_scss);
295            imports.push(ImportInfo {
296                source: src,
297                imported_name: ImportedName::SideEffect,
298                local_name: String::new(),
299                is_type_only: false,
300                from_style: false,
301                span: Span::default(),
302                source_span: Span::default(),
303            });
304        }
305    }
306
307    // Extract SCSS @use/@forward statements
308    if is_scss {
309        for cap in SCSS_USE_RE.captures_iter(&stripped) {
310            if let Some(m) = cap.get(1) {
311                imports.push(ImportInfo {
312                    source: normalize_css_import_path(m.as_str().to_string(), true),
313                    imported_name: ImportedName::SideEffect,
314                    local_name: String::new(),
315                    is_type_only: false,
316                    from_style: false,
317                    span: Span::default(),
318                    source_span: Span::default(),
319                });
320            }
321        }
322    }
323
324    // Extract Tailwind CSS @plugin directives. These can reference npm packages
325    // (e.g. `@plugin "daisyui"`) or explicit local plugin files.
326    for cap in CSS_PLUGIN_RE.captures_iter(&stripped) {
327        if let Some(m) = cap.get(1) {
328            let source = m.as_str().trim().to_string();
329            if !source.is_empty() && !is_css_url_import(&source) {
330                imports.push(ImportInfo {
331                    source: normalize_css_plugin_path(source),
332                    imported_name: ImportedName::Default,
333                    local_name: String::new(),
334                    is_type_only: false,
335                    from_style: false,
336                    span: Span::default(),
337                    source_span: Span::default(),
338                });
339            }
340        }
341    }
342
343    // If @apply or @tailwind directives exist, create a synthetic import to tailwindcss
344    // to mark the dependency as used
345    let has_apply = CSS_APPLY_RE.is_match(&stripped);
346    let has_tailwind = CSS_TAILWIND_RE.is_match(&stripped);
347    if has_apply || has_tailwind {
348        imports.push(ImportInfo {
349            source: "tailwindcss".to_string(),
350            imported_name: ImportedName::SideEffect,
351            local_name: String::new(),
352            is_type_only: false,
353            from_style: false,
354            span: Span::default(),
355            source_span: Span::default(),
356        });
357    }
358
359    // For CSS module files, extract class names as named exports
360    let exports = if is_css_module_file(path) {
361        extract_css_module_exports(&stripped)
362    } else {
363        Vec::new()
364    };
365
366    ModuleInfo {
367        file_id,
368        exports,
369        imports,
370        re_exports: Vec::new(),
371        dynamic_imports: Vec::new(),
372        dynamic_import_patterns: Vec::new(),
373        require_calls: Vec::new(),
374        member_accesses: Vec::new(),
375        whole_object_uses: Vec::new(),
376        has_cjs_exports: false,
377        has_angular_component_template_url: false,
378        content_hash,
379        suppressions: parsed_suppressions.suppressions,
380        unknown_suppression_kinds: parsed_suppressions.unknown_kinds,
381        unused_import_bindings: Vec::new(),
382        type_referenced_import_bindings: Vec::new(),
383        value_referenced_import_bindings: Vec::new(),
384        line_offsets: fallow_types::extract::compute_line_offsets(source),
385        complexity: Vec::new(),
386        flag_uses: Vec::new(),
387        class_heritage: vec![],
388        local_type_declarations: Vec::new(),
389        public_signature_type_references: Vec::new(),
390        namespace_object_aliases: Vec::new(),
391    }
392}
393
394#[cfg(test)]
395mod tests {
396    use super::*;
397
398    /// Helper to collect export names as strings from `extract_css_module_exports`.
399    fn export_names(source: &str) -> Vec<String> {
400        extract_css_module_exports(source)
401            .into_iter()
402            .filter_map(|e| match e.name {
403                ExportName::Named(n) => Some(n),
404                ExportName::Default => None,
405            })
406            .collect()
407    }
408
409    // ── is_css_file ──────────────────────────────────────────────
410
411    #[test]
412    fn is_css_file_css() {
413        assert!(is_css_file(Path::new("styles.css")));
414    }
415
416    #[test]
417    fn is_css_file_scss() {
418        assert!(is_css_file(Path::new("styles.scss")));
419    }
420
421    #[test]
422    fn is_css_file_rejects_js() {
423        assert!(!is_css_file(Path::new("app.js")));
424    }
425
426    #[test]
427    fn is_css_file_rejects_ts() {
428        assert!(!is_css_file(Path::new("app.ts")));
429    }
430
431    #[test]
432    fn is_css_file_rejects_less() {
433        assert!(!is_css_file(Path::new("styles.less")));
434    }
435
436    #[test]
437    fn is_css_file_rejects_no_extension() {
438        assert!(!is_css_file(Path::new("Makefile")));
439    }
440
441    // ── is_css_module_file ───────────────────────────────────────
442
443    #[test]
444    fn is_css_module_file_module_css() {
445        assert!(is_css_module_file(Path::new("Component.module.css")));
446    }
447
448    #[test]
449    fn is_css_module_file_module_scss() {
450        assert!(is_css_module_file(Path::new("Component.module.scss")));
451    }
452
453    #[test]
454    fn is_css_module_file_rejects_plain_css() {
455        assert!(!is_css_module_file(Path::new("styles.css")));
456    }
457
458    #[test]
459    fn is_css_module_file_rejects_plain_scss() {
460        assert!(!is_css_module_file(Path::new("styles.scss")));
461    }
462
463    #[test]
464    fn is_css_module_file_rejects_module_js() {
465        assert!(!is_css_module_file(Path::new("utils.module.js")));
466    }
467
468    // ── extract_css_module_exports: basic class extraction ───────
469
470    #[test]
471    fn extracts_single_class() {
472        let names = export_names(".foo { color: red; }");
473        assert_eq!(names, vec!["foo"]);
474    }
475
476    #[test]
477    fn extracts_multiple_classes() {
478        let names = export_names(".foo { } .bar { }");
479        assert_eq!(names, vec!["foo", "bar"]);
480    }
481
482    #[test]
483    fn extracts_nested_classes() {
484        let names = export_names(".foo .bar { color: red; }");
485        assert!(names.contains(&"foo".to_string()));
486        assert!(names.contains(&"bar".to_string()));
487    }
488
489    #[test]
490    fn extracts_hyphenated_class() {
491        let names = export_names(".my-class { }");
492        assert_eq!(names, vec!["my-class"]);
493    }
494
495    #[test]
496    fn extracts_camel_case_class() {
497        let names = export_names(".myClass { }");
498        assert_eq!(names, vec!["myClass"]);
499    }
500
501    #[test]
502    fn extracts_underscore_class() {
503        let names = export_names("._hidden { } .__wrapper { }");
504        assert!(names.contains(&"_hidden".to_string()));
505        assert!(names.contains(&"__wrapper".to_string()));
506    }
507
508    // ── Pseudo-selectors ─────────────────────────────────────────
509
510    #[test]
511    fn pseudo_selector_hover() {
512        let names = export_names(".foo:hover { color: blue; }");
513        assert_eq!(names, vec!["foo"]);
514    }
515
516    #[test]
517    fn pseudo_selector_focus() {
518        let names = export_names(".input:focus { outline: none; }");
519        assert_eq!(names, vec!["input"]);
520    }
521
522    #[test]
523    fn pseudo_element_before() {
524        let names = export_names(".icon::before { content: ''; }");
525        assert_eq!(names, vec!["icon"]);
526    }
527
528    #[test]
529    fn combined_pseudo_selectors() {
530        let names = export_names(".btn:hover, .btn:active, .btn:focus { }");
531        // "btn" should be deduplicated
532        assert_eq!(names, vec!["btn"]);
533    }
534
535    // ── Media queries ────────────────────────────────────────────
536
537    #[test]
538    fn classes_inside_media_query() {
539        let names = export_names(
540            "@media (max-width: 768px) { .mobile-nav { display: block; } .desktop-nav { display: none; } }",
541        );
542        assert!(names.contains(&"mobile-nav".to_string()));
543        assert!(names.contains(&"desktop-nav".to_string()));
544    }
545
546    // ── Deduplication ────────────────────────────────────────────
547
548    #[test]
549    fn deduplicates_repeated_class() {
550        let names = export_names(".btn { color: red; } .btn { font-size: 14px; }");
551        assert_eq!(names.iter().filter(|n| *n == "btn").count(), 1);
552    }
553
554    // ── Edge cases ───────────────────────────────────────────────
555
556    #[test]
557    fn empty_source() {
558        let names = export_names("");
559        assert!(names.is_empty());
560    }
561
562    #[test]
563    fn no_classes() {
564        let names = export_names("body { margin: 0; } * { box-sizing: border-box; }");
565        assert!(names.is_empty());
566    }
567
568    #[test]
569    fn ignores_classes_in_block_comments() {
570        // Note: extract_css_module_exports itself does NOT strip comments;
571        // comments are stripped in parse_css_to_module before calling it.
572        // But CSS_NON_SELECTOR_RE strips quoted strings. Testing the
573        // strip_css_comments + extract pipeline via the stripped source:
574        let stripped = strip_css_comments("/* .fake { } */ .real { }", false);
575        let names = export_names(&stripped);
576        assert!(!names.contains(&"fake".to_string()));
577        assert!(names.contains(&"real".to_string()));
578    }
579
580    #[test]
581    fn ignores_classes_in_strings() {
582        let names = export_names(r#".real { content: ".fake"; }"#);
583        assert!(names.contains(&"real".to_string()));
584        assert!(!names.contains(&"fake".to_string()));
585    }
586
587    #[test]
588    fn ignores_classes_in_url() {
589        let names = export_names(".real { background: url(./images/hero.png); }");
590        assert!(names.contains(&"real".to_string()));
591        // "png" from "hero.png" should not be extracted
592        assert!(!names.contains(&"png".to_string()));
593    }
594
595    // ── strip_css_comments ───────────────────────────────────────
596
597    #[test]
598    fn strip_css_block_comment() {
599        let result = strip_css_comments("/* removed */ .kept { }", false);
600        assert!(!result.contains("removed"));
601        assert!(result.contains(".kept"));
602    }
603
604    #[test]
605    fn strip_scss_line_comment() {
606        let result = strip_css_comments("// removed\n.kept { }", true);
607        assert!(!result.contains("removed"));
608        assert!(result.contains(".kept"));
609    }
610
611    #[test]
612    fn strip_scss_preserves_css_outside_comments() {
613        let source = "// line comment\n/* block comment */\n.visible { color: red; }";
614        let result = strip_css_comments(source, true);
615        assert!(result.contains(".visible"));
616    }
617
618    // ── is_css_url_import ────────────────────────────────────────
619
620    #[test]
621    fn url_import_http() {
622        assert!(is_css_url_import("http://example.com/style.css"));
623    }
624
625    #[test]
626    fn url_import_https() {
627        assert!(is_css_url_import("https://fonts.googleapis.com/css"));
628    }
629
630    #[test]
631    fn url_import_data() {
632        assert!(is_css_url_import("data:text/css;base64,abc"));
633    }
634
635    #[test]
636    fn url_import_local_not_skipped() {
637        assert!(!is_css_url_import("./local.css"));
638    }
639
640    #[test]
641    fn url_import_bare_specifier_not_skipped() {
642        assert!(!is_css_url_import("tailwindcss"));
643    }
644
645    // ── normalize_css_import_path ─────────────────────────────────
646
647    #[test]
648    fn normalize_relative_dot_path_unchanged() {
649        assert_eq!(
650            normalize_css_import_path("./reset.css".to_string(), false),
651            "./reset.css"
652        );
653    }
654
655    #[test]
656    fn normalize_parent_relative_path_unchanged() {
657        assert_eq!(
658            normalize_css_import_path("../shared.scss".to_string(), false),
659            "../shared.scss"
660        );
661    }
662
663    #[test]
664    fn normalize_absolute_path_unchanged() {
665        assert_eq!(
666            normalize_css_import_path("/styles/main.css".to_string(), false),
667            "/styles/main.css"
668        );
669    }
670
671    #[test]
672    fn normalize_url_unchanged() {
673        assert_eq!(
674            normalize_css_import_path("https://example.com/style.css".to_string(), false),
675            "https://example.com/style.css"
676        );
677    }
678
679    #[test]
680    fn normalize_bare_css_gets_dot_slash() {
681        assert_eq!(
682            normalize_css_import_path("app.css".to_string(), false),
683            "./app.css"
684        );
685    }
686
687    #[test]
688    fn normalize_css_package_subpath_stays_bare() {
689        assert_eq!(
690            normalize_css_import_path("tailwindcss/theme.css".to_string(), false),
691            "tailwindcss/theme.css"
692        );
693    }
694
695    #[test]
696    fn normalize_css_package_subpath_with_dotted_name_stays_bare() {
697        assert_eq!(
698            normalize_css_import_path("highlight.js/styles/github.css".to_string(), false),
699            "highlight.js/styles/github.css"
700        );
701    }
702
703    #[test]
704    fn normalize_bare_scss_gets_dot_slash() {
705        assert_eq!(
706            normalize_css_import_path("vars.scss".to_string(), false),
707            "./vars.scss"
708        );
709    }
710
711    #[test]
712    fn normalize_bare_sass_gets_dot_slash() {
713        assert_eq!(
714            normalize_css_import_path("main.sass".to_string(), false),
715            "./main.sass"
716        );
717    }
718
719    #[test]
720    fn normalize_bare_less_gets_dot_slash() {
721        assert_eq!(
722            normalize_css_import_path("theme.less".to_string(), false),
723            "./theme.less"
724        );
725    }
726
727    #[test]
728    fn normalize_bare_js_extension_stays_bare() {
729        assert_eq!(
730            normalize_css_import_path("module.js".to_string(), false),
731            "module.js"
732        );
733    }
734
735    // ── SCSS partial normalization ───────────────────────────────
736
737    #[test]
738    fn normalize_scss_bare_partial_gets_dot_slash() {
739        assert_eq!(
740            normalize_css_import_path("variables".to_string(), true),
741            "./variables"
742        );
743    }
744
745    #[test]
746    fn normalize_scss_bare_partial_with_subdir_gets_dot_slash() {
747        assert_eq!(
748            normalize_css_import_path("base/reset".to_string(), true),
749            "./base/reset"
750        );
751    }
752
753    #[test]
754    fn normalize_scss_builtin_stays_bare() {
755        assert_eq!(
756            normalize_css_import_path("sass:math".to_string(), true),
757            "sass:math"
758        );
759    }
760
761    #[test]
762    fn normalize_scss_relative_path_unchanged() {
763        assert_eq!(
764            normalize_css_import_path("../styles/variables".to_string(), true),
765            "../styles/variables"
766        );
767    }
768
769    #[test]
770    fn normalize_css_bare_extensionless_stays_bare() {
771        // In CSS context (not SCSS), extensionless imports are npm packages
772        assert_eq!(
773            normalize_css_import_path("tailwindcss".to_string(), false),
774            "tailwindcss"
775        );
776    }
777
778    // ── Scoped npm packages stay bare ───────────────────────────
779
780    #[test]
781    fn normalize_scoped_package_with_css_extension_stays_bare() {
782        assert_eq!(
783            normalize_css_import_path("@fontsource/monaspace-neon/400.css".to_string(), false),
784            "@fontsource/monaspace-neon/400.css"
785        );
786    }
787
788    #[test]
789    fn normalize_scoped_package_with_scss_extension_stays_bare() {
790        assert_eq!(
791            normalize_css_import_path("@company/design-system/tokens.scss".to_string(), true),
792            "@company/design-system/tokens.scss"
793        );
794    }
795
796    #[test]
797    fn normalize_scoped_package_without_extension_stays_bare() {
798        assert_eq!(
799            normalize_css_import_path("@fallow/design-system/styles".to_string(), false),
800            "@fallow/design-system/styles"
801        );
802    }
803
804    #[test]
805    fn normalize_scoped_package_extensionless_scss_stays_bare() {
806        assert_eq!(
807            normalize_css_import_path("@company/tokens".to_string(), true),
808            "@company/tokens"
809        );
810    }
811
812    #[test]
813    fn normalize_path_alias_with_css_extension_stays_bare() {
814        // Path aliases like `@/components/Button.css` (configured via tsconfig paths
815        // or Vite alias) share the `@` prefix with scoped packages. They must stay
816        // bare so the resolver's path-alias path can handle them; prepending `./`
817        // would break resolution.
818        assert_eq!(
819            normalize_css_import_path("@/components/Button.css".to_string(), false),
820            "@/components/Button.css"
821        );
822    }
823
824    #[test]
825    fn normalize_path_alias_extensionless_stays_bare() {
826        assert_eq!(
827            normalize_css_import_path("@/styles/variables".to_string(), false),
828            "@/styles/variables"
829        );
830    }
831
832    // ── strip_css_comments edge cases ─────────────────────────────
833
834    #[test]
835    fn strip_css_no_comments() {
836        let source = ".foo { color: red; }";
837        assert_eq!(strip_css_comments(source, false), source);
838    }
839
840    #[test]
841    fn strip_css_multiple_block_comments() {
842        let source = "/* comment-one */ .foo { } /* comment-two */ .bar { }";
843        let result = strip_css_comments(source, false);
844        assert!(!result.contains("comment-one"));
845        assert!(!result.contains("comment-two"));
846        assert!(result.contains(".foo"));
847        assert!(result.contains(".bar"));
848    }
849
850    #[test]
851    fn strip_scss_does_not_affect_non_scss() {
852        // When is_scss=false, line comments should NOT be stripped
853        let source = "// this stays\n.foo { }";
854        let result = strip_css_comments(source, false);
855        assert!(result.contains("// this stays"));
856    }
857
858    // ── parse_css_to_module: suppression integration ──────────────
859
860    #[test]
861    fn css_module_parses_suppressions() {
862        let info = parse_css_to_module(
863            fallow_types::discover::FileId(0),
864            Path::new("Component.module.css"),
865            "/* fallow-ignore-file */\n.btn { color: red; }",
866            0,
867        );
868        assert!(!info.suppressions.is_empty());
869        assert_eq!(info.suppressions[0].line, 0);
870    }
871
872    // ── CSS class name edge cases ─────────────────────────────────
873
874    #[test]
875    fn extracts_class_starting_with_underscore() {
876        let names = export_names("._private { } .__dunder { }");
877        assert!(names.contains(&"_private".to_string()));
878        assert!(names.contains(&"__dunder".to_string()));
879    }
880
881    #[test]
882    fn ignores_id_selectors() {
883        let names = export_names("#myId { color: red; }");
884        assert!(!names.contains(&"myId".to_string()));
885    }
886
887    #[test]
888    fn ignores_element_selectors() {
889        let names = export_names("div { color: red; } span { }");
890        assert!(names.is_empty());
891    }
892
893    // ── extract_css_imports (issue #195: vite additionalData / SFC styles) ──
894
895    #[test]
896    fn extract_css_imports_at_import_quoted() {
897        let imports = extract_css_imports(r#"@import "./reset.css";"#, false);
898        assert_eq!(imports, vec!["./reset.css"]);
899    }
900
901    #[test]
902    fn extract_css_imports_package_subpath_stays_bare() {
903        let imports =
904            extract_css_imports(r#"@import "tailwindcss/theme.css" layer(theme);"#, false);
905        assert_eq!(imports, vec!["tailwindcss/theme.css"]);
906    }
907
908    #[test]
909    fn extract_css_imports_at_import_url() {
910        let imports = extract_css_imports(r#"@import url("./reset.css");"#, false);
911        assert_eq!(imports, vec!["./reset.css"]);
912    }
913
914    #[test]
915    fn extract_css_imports_skips_remote_urls() {
916        let imports =
917            extract_css_imports(r#"@import "https://fonts.example.com/font.css";"#, false);
918        assert!(imports.is_empty());
919    }
920
921    #[test]
922    fn extract_css_imports_scss_use_normalizes_partial() {
923        let imports = extract_css_imports(r#"@use "variables";"#, true);
924        assert_eq!(imports, vec!["./variables"]);
925    }
926
927    #[test]
928    fn extract_css_imports_scss_forward_normalizes_partial() {
929        let imports = extract_css_imports(r#"@forward "tokens";"#, true);
930        assert_eq!(imports, vec!["./tokens"]);
931    }
932
933    #[test]
934    fn extract_css_imports_skips_comments() {
935        let imports = extract_css_imports(
936            r#"/* @import "./hidden.scss"; */
937@use "real";"#,
938            true,
939        );
940        assert_eq!(imports, vec!["./real"]);
941    }
942
943    #[test]
944    fn extract_css_imports_at_plugin_keeps_package_bare() {
945        let imports = extract_css_imports(r#"@plugin "daisyui";"#, true);
946        assert_eq!(imports, vec!["daisyui"]);
947    }
948
949    #[test]
950    fn extract_css_imports_at_plugin_tracks_relative_file() {
951        let imports = extract_css_imports(r#"@plugin "./tailwind-plugin.js";"#, false);
952        assert_eq!(imports, vec!["./tailwind-plugin.js"]);
953    }
954
955    #[test]
956    fn extract_css_imports_scss_at_import_kept_relative() {
957        let imports = extract_css_imports(r"@import 'Foo';", true);
958        // Bare specifier in SCSS context is normalized to ./
959        assert_eq!(imports, vec!["./Foo"]);
960    }
961
962    #[test]
963    fn extract_css_imports_additional_data_string_body() {
964        // Mimics what Vite's css.preprocessorOptions.scss.additionalData ships
965        let body = r#"@use "./src/styles/global.scss";"#;
966        let imports = extract_css_imports(body, true);
967        assert_eq!(imports, vec!["./src/styles/global.scss"]);
968    }
969}