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    crate::static_regex(
18        r#"@import\s+(?:url\(\s*(?:["']([^"']+)["']|([^)]+))\s*\)|["']([^"']+)["'])"#,
19    )
20});
21
22/// Regex to extract SCSS @use and @forward sources.
23/// Matches: @use "path"; @use 'path'; @forward "path"; @forward 'path';
24static SCSS_USE_RE: LazyLock<regex::Regex> =
25    LazyLock::new(|| crate::static_regex(r#"@(?:use|forward)\s+["']([^"']+)["']"#));
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(|| crate::static_regex(r#"@plugin\s+["']([^"']+)["']"#));
31
32/// Regex to extract @apply class references.
33/// Matches: @apply class1 class2 class3;
34static CSS_APPLY_RE: LazyLock<regex::Regex> =
35    LazyLock::new(|| crate::static_regex(r"@apply\s+[^;}\n]+"));
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(|| crate::static_regex(r"@tailwind\s+\w+"));
41
42/// Regex to match CSS block comments (`/* ... */`) for stripping before extraction.
43static CSS_COMMENT_RE: LazyLock<regex::Regex> =
44    LazyLock::new(|| crate::static_regex(r"(?s)/\*.*?\*/"));
45
46/// Regex to match SCSS single-line comments (`// ...`) for stripping before extraction.
47static SCSS_LINE_COMMENT_RE: LazyLock<regex::Regex> =
48    LazyLock::new(|| crate::static_regex(r"//[^\n]*"));
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(|| crate::static_regex(r"\.([a-zA-Z_][a-zA-Z0-9_-]*)"));
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> =
58    LazyLock::new(|| crate::static_regex(r#"(?s)"[^"]*"|'[^']*'|url\([^)]*\)"#));
59
60/// Regex to strip the prelude of `@layer` and `@import` at-rules before
61/// CSS-Modules class extraction. Matches the `@keyword` plus everything up to
62/// (but not including) the next `;` or `{`, so block bodies are preserved.
63///
64/// Narrow allowlist by design (issue #540): only at-rules whose preludes
65/// legitimately carry dot-separated identifiers without selector semantics are
66/// stripped. `@layer foo.bar` (CSS Cascading & Inheritance L5) lists layer
67/// names; `@import url("x.css") layer(theme.button)` carries a parenthesised
68/// layer reference. `@scope (.foo) to (.bar)` keeps its existing behavior
69/// because the prelude IS a selector list and `.foo` / `.bar` are real class
70/// references that the user may want to surface as exports.
71static 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/// A CSS import source with both the literal source and fallow's resolver-normalized form.
81#[derive(Debug, Clone, PartialEq, Eq)]
82pub struct CssImportSource {
83    /// The import source exactly as it appeared in `@import` / `@use` / `@forward` / `@plugin`.
84    pub raw: String,
85    /// The source normalized for fallow's resolver (`variables` -> `./variables` in SCSS).
86    pub normalized: String,
87    /// Whether this source came from Tailwind CSS `@plugin`.
88    pub is_plugin: bool,
89    /// Span of the source specifier in the original CSS/SCSS input.
90    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
101/// Returns true if a CSS import source is a remote URL or data URI that should be skipped.
102fn is_css_url_import(source: &str) -> bool {
103    source.starts_with("http://") || source.starts_with("https://") || source.starts_with("data:")
104}
105
106/// Normalize a CSS/SCSS import path to use `./` prefix for relative paths.
107/// Bare file names such as `reset.css` stay relative for CSS ergonomics, while
108/// package subpaths such as `tailwindcss/theme.css` stay bare so bundler-style
109/// package CSS imports resolve through `node_modules`.
110///
111/// When `is_scss` is true, extensionless specifiers that are not SCSS built-in
112/// modules (`sass:*`) are treated as relative imports (SCSS partial convention).
113/// This handles `@use 'variables'` resolving to `./_variables.scss`.
114///
115/// Scoped npm packages (`@scope/pkg`) are always kept bare, even when they have
116/// CSS extensions (e.g., `@fontsource/monaspace-neon/400.css`). Bundlers like
117/// Vite resolve these from node_modules, not as relative paths.
118fn 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/// Strip comments from CSS/SCSS source to avoid matching directives inside comments.
158#[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
176/// Normalize a Tailwind CSS `@plugin` target.
177///
178/// Unlike SCSS `@use`, extensionless targets such as `daisyui` are package
179/// specifiers, not local partials. Keep bare specifiers bare and only preserve
180/// explicit relative/root-relative paths.
181fn normalize_css_plugin_path(path: String) -> String {
182    path
183}
184
185/// Extract `@import` / `@use` / `@forward` / `@plugin` source paths from a CSS/SCSS string.
186///
187/// Returns both the raw source and the normalized source. URL imports
188/// (`http://`, `https://`, `data:`) are skipped. Use [`extract_css_imports`]
189/// when only the normalized form is needed.
190#[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/// Extract normalized `@import` / `@use` / `@forward` / `@plugin` source paths from a CSS/SCSS string.
251///
252/// Returns specifiers normalized via `normalize_css_import_path`. URL imports
253/// (`http://`, `https://`, `data:`) are skipped. Used by callers that only need
254/// entry/dependency source paths; callers that need import kind information
255/// should use [`extract_css_import_sources`].
256#[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
264/// Mask every regex match in `src` with ASCII spaces (`0x20`) of equal byte
265/// length, so byte offsets in the returned string correspond 1:1 to byte
266/// offsets in the original.
267///
268/// Used to neutralise CSS comments, quoted strings, `url(...)`, and at-rule
269/// preludes before scanning for `.class` selectors, while preserving the
270/// original-source positions that callers need to populate `ExportInfo.span`
271/// (issue #549). The `regex` crate guarantees match boundaries respect UTF-8
272/// char boundaries, so the masked buffer is always valid UTF-8.
273fn mask_with_whitespace(src: &str, re: &regex::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
287/// Extract class names from a CSS module file as named exports.
288///
289/// Each emitted [`ExportInfo`] carries a [`Span`] pointing at the bare class
290/// name in the ORIGINAL `source` (no leading dot), so downstream
291/// `compute_line_offsets` resolves the real declaration line and column
292/// instead of falling back to line:1 col:0 (issue #549).
293pub 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
328/// Parse a CSS/SCSS file, extracting @import, @use, @forward, @plugin, @apply, and @tailwind directives.
329pub(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        security_unresolved_callee_sites: Vec::new(),
415        tainted_bindings: Vec::new(),
416        sanitized_sink_args: Vec::new(),
417        security_control_sites: Vec::new(),
418        callee_uses: Vec::new(),
419    }
420}
421
422#[cfg(test)]
423mod tests {
424    use super::*;
425
426    /// Helper to collect export names as strings from `extract_css_module_exports`.
427    fn export_names(source: &str) -> Vec<String> {
428        extract_css_module_exports(source, false)
429            .into_iter()
430            .filter_map(|e| match e.name {
431                ExportName::Named(n) => Some(n),
432                ExportName::Default => None,
433            })
434            .collect()
435    }
436
437    #[test]
438    fn is_css_file_css() {
439        assert!(is_css_file(Path::new("styles.css")));
440    }
441
442    #[test]
443    fn is_css_file_scss() {
444        assert!(is_css_file(Path::new("styles.scss")));
445    }
446
447    #[test]
448    fn is_css_file_rejects_js() {
449        assert!(!is_css_file(Path::new("app.js")));
450    }
451
452    #[test]
453    fn is_css_file_rejects_ts() {
454        assert!(!is_css_file(Path::new("app.ts")));
455    }
456
457    #[test]
458    fn is_css_file_rejects_less() {
459        assert!(!is_css_file(Path::new("styles.less")));
460    }
461
462    #[test]
463    fn is_css_file_rejects_no_extension() {
464        assert!(!is_css_file(Path::new("Makefile")));
465    }
466
467    #[test]
468    fn is_css_module_file_module_css() {
469        assert!(is_css_module_file(Path::new("Component.module.css")));
470    }
471
472    #[test]
473    fn is_css_module_file_module_scss() {
474        assert!(is_css_module_file(Path::new("Component.module.scss")));
475    }
476
477    #[test]
478    fn is_css_module_file_rejects_plain_css() {
479        assert!(!is_css_module_file(Path::new("styles.css")));
480    }
481
482    #[test]
483    fn is_css_module_file_rejects_plain_scss() {
484        assert!(!is_css_module_file(Path::new("styles.scss")));
485    }
486
487    #[test]
488    fn is_css_module_file_rejects_module_js() {
489        assert!(!is_css_module_file(Path::new("utils.module.js")));
490    }
491
492    #[test]
493    fn extracts_single_class() {
494        let names = export_names(".foo { color: red; }");
495        assert_eq!(names, vec!["foo"]);
496    }
497
498    #[test]
499    fn extracts_multiple_classes() {
500        let names = export_names(".foo { } .bar { }");
501        assert_eq!(names, vec!["foo", "bar"]);
502    }
503
504    #[test]
505    fn extracts_nested_classes() {
506        let names = export_names(".foo .bar { color: red; }");
507        assert!(names.contains(&"foo".to_string()));
508        assert!(names.contains(&"bar".to_string()));
509    }
510
511    #[test]
512    fn extracts_hyphenated_class() {
513        let names = export_names(".my-class { }");
514        assert_eq!(names, vec!["my-class"]);
515    }
516
517    #[test]
518    fn extracts_camel_case_class() {
519        let names = export_names(".myClass { }");
520        assert_eq!(names, vec!["myClass"]);
521    }
522
523    #[test]
524    fn extracts_underscore_class() {
525        let names = export_names("._hidden { } .__wrapper { }");
526        assert!(names.contains(&"_hidden".to_string()));
527        assert!(names.contains(&"__wrapper".to_string()));
528    }
529
530    #[test]
531    fn pseudo_selector_hover() {
532        let names = export_names(".foo:hover { color: blue; }");
533        assert_eq!(names, vec!["foo"]);
534    }
535
536    #[test]
537    fn pseudo_selector_focus() {
538        let names = export_names(".input:focus { outline: none; }");
539        assert_eq!(names, vec!["input"]);
540    }
541
542    #[test]
543    fn pseudo_element_before() {
544        let names = export_names(".icon::before { content: ''; }");
545        assert_eq!(names, vec!["icon"]);
546    }
547
548    #[test]
549    fn combined_pseudo_selectors() {
550        let names = export_names(".btn:hover, .btn:active, .btn:focus { }");
551        assert_eq!(names, vec!["btn"]);
552    }
553
554    #[test]
555    fn classes_inside_media_query() {
556        let names = export_names(
557            "@media (max-width: 768px) { .mobile-nav { display: block; } .desktop-nav { display: none; } }",
558        );
559        assert!(names.contains(&"mobile-nav".to_string()));
560        assert!(names.contains(&"desktop-nav".to_string()));
561    }
562
563    #[test]
564    fn classes_inside_multi_line_media_query() {
565        let names =
566            export_names("@media\n  screen and (min-width: 600px)\n{\n  .real { color: red; }\n}");
567        assert_eq!(names, vec!["real"]);
568    }
569
570    #[test]
571    fn at_layer_statement_does_not_export() {
572        let names = export_names("@layer foo.bar;");
573        assert!(names.is_empty(), "got {names:?}");
574        let names = export_names("@layer foo.bar, foo.baz;");
575        assert!(names.is_empty(), "got {names:?}");
576    }
577
578    #[test]
579    fn at_layer_block_keeps_body_classes() {
580        let names = export_names("@layer foo.bar { .root { color: red; } }");
581        assert_eq!(names, vec!["root"]);
582    }
583
584    #[test]
585    fn at_layer_multiline_prelude_keeps_body_classes() {
586        let names = export_names("@layer\n  foo.bar\n{ .root { color: red; } }");
587        assert_eq!(names, vec!["root"]);
588    }
589
590    #[test]
591    fn at_layer_with_nested_media_keeps_body() {
592        let names =
593            export_names("@layer foo.bar { @media (max-width: 768px) { .real { color: red; } } }");
594        assert_eq!(names, vec!["real"]);
595    }
596
597    #[test]
598    fn at_import_with_layer_attribute_does_not_export() {
599        let names = export_names(r#"@import url("x.css") layer(theme.button);"#);
600        assert!(names.is_empty(), "got {names:?}");
601    }
602
603    #[test]
604    fn class_then_at_layer_does_not_leak_prelude() {
605        let names =
606            export_names(".outer { color: blue; } @layer foo.bar { .inner { color: red; } }");
607        assert_eq!(names, vec!["outer", "inner"]);
608    }
609
610    #[test]
611    fn at_scope_keeps_selector_list_classes() {
612        let names = export_names("@scope (.parent) to (.child) { .title { color: red; } }");
613        assert!(names.contains(&"parent".to_string()), "got {names:?}");
614        assert!(names.contains(&"child".to_string()), "got {names:?}");
615        assert!(names.contains(&"title".to_string()), "got {names:?}");
616    }
617
618    #[test]
619    fn at_keyframes_numeric_step_is_not_class() {
620        let names = export_names(
621            "@keyframes slide { 0% { transform: scale(.5); } 100% { transform: scale(1); } }",
622        );
623        assert!(names.is_empty(), "got {names:?}");
624    }
625
626    #[test]
627    fn at_webkit_keyframes_keeps_body_classes() {
628        let names = export_names("@-webkit-keyframes slide { 0% { } 100% { } } .real { }");
629        assert_eq!(names, vec!["real"]);
630    }
631
632    #[test]
633    fn deduplicates_repeated_class() {
634        let names = export_names(".btn { color: red; } .btn { font-size: 14px; }");
635        assert_eq!(names.iter().filter(|n| *n == "btn").count(), 1);
636    }
637
638    #[test]
639    fn empty_source() {
640        let names = export_names("");
641        assert!(names.is_empty());
642    }
643
644    #[test]
645    fn no_classes() {
646        let names = export_names("body { margin: 0; } * { box-sizing: border-box; }");
647        assert!(names.is_empty());
648    }
649
650    #[test]
651    fn ignores_classes_in_block_comments() {
652        let names = export_names("/* .fake { } */ .real { }");
653        assert!(!names.contains(&"fake".to_string()));
654        assert!(names.contains(&"real".to_string()));
655    }
656
657    #[test]
658    fn ignores_classes_in_scss_line_comments() {
659        let exports = extract_css_module_exports("// .fake\n.real { }", true);
660        let names: Vec<_> = exports
661            .iter()
662            .filter_map(|e| match &e.name {
663                ExportName::Named(n) => Some(n.as_str()),
664                ExportName::Default => None,
665            })
666            .collect();
667        assert_eq!(names, vec!["real"]);
668    }
669
670    #[test]
671    fn ignores_classes_in_strings() {
672        let names = export_names(r#".real { content: ".fake"; }"#);
673        assert!(names.contains(&"real".to_string()));
674        assert!(!names.contains(&"fake".to_string()));
675    }
676
677    #[test]
678    fn ignores_classes_in_url() {
679        let names = export_names(".real { background: url(./images/hero.png); }");
680        assert!(names.contains(&"real".to_string()));
681        assert!(!names.contains(&"png".to_string()));
682    }
683
684    #[test]
685    fn strip_css_block_comment() {
686        let result = strip_css_comments("/* removed */ .kept { }", false);
687        assert!(!result.contains("removed"));
688        assert!(result.contains(".kept"));
689    }
690
691    #[test]
692    fn strip_scss_line_comment() {
693        let result = strip_css_comments("// removed\n.kept { }", true);
694        assert!(!result.contains("removed"));
695        assert!(result.contains(".kept"));
696    }
697
698    #[test]
699    fn strip_scss_preserves_css_outside_comments() {
700        let source = "// line comment\n/* block comment */\n.visible { color: red; }";
701        let result = strip_css_comments(source, true);
702        assert!(result.contains(".visible"));
703    }
704
705    #[test]
706    fn url_import_http() {
707        assert!(is_css_url_import("http://example.com/style.css"));
708    }
709
710    #[test]
711    fn url_import_https() {
712        assert!(is_css_url_import("https://fonts.googleapis.com/css"));
713    }
714
715    #[test]
716    fn url_import_data() {
717        assert!(is_css_url_import("data:text/css;base64,abc"));
718    }
719
720    #[test]
721    fn url_import_local_not_skipped() {
722        assert!(!is_css_url_import("./local.css"));
723    }
724
725    #[test]
726    fn url_import_bare_specifier_not_skipped() {
727        assert!(!is_css_url_import("tailwindcss"));
728    }
729
730    #[test]
731    fn normalize_relative_dot_path_unchanged() {
732        assert_eq!(
733            normalize_css_import_path("./reset.css".to_string(), false),
734            "./reset.css"
735        );
736    }
737
738    #[test]
739    fn normalize_parent_relative_path_unchanged() {
740        assert_eq!(
741            normalize_css_import_path("../shared.scss".to_string(), false),
742            "../shared.scss"
743        );
744    }
745
746    #[test]
747    fn normalize_absolute_path_unchanged() {
748        assert_eq!(
749            normalize_css_import_path("/styles/main.css".to_string(), false),
750            "/styles/main.css"
751        );
752    }
753
754    #[test]
755    fn normalize_url_unchanged() {
756        assert_eq!(
757            normalize_css_import_path("https://example.com/style.css".to_string(), false),
758            "https://example.com/style.css"
759        );
760    }
761
762    #[test]
763    fn normalize_bare_css_gets_dot_slash() {
764        assert_eq!(
765            normalize_css_import_path("app.css".to_string(), false),
766            "./app.css"
767        );
768    }
769
770    #[test]
771    fn normalize_css_package_subpath_stays_bare() {
772        assert_eq!(
773            normalize_css_import_path("tailwindcss/theme.css".to_string(), false),
774            "tailwindcss/theme.css"
775        );
776    }
777
778    #[test]
779    fn normalize_css_package_subpath_with_dotted_name_stays_bare() {
780        assert_eq!(
781            normalize_css_import_path("highlight.js/styles/github.css".to_string(), false),
782            "highlight.js/styles/github.css"
783        );
784    }
785
786    #[test]
787    fn normalize_bare_scss_gets_dot_slash() {
788        assert_eq!(
789            normalize_css_import_path("vars.scss".to_string(), false),
790            "./vars.scss"
791        );
792    }
793
794    #[test]
795    fn normalize_bare_sass_gets_dot_slash() {
796        assert_eq!(
797            normalize_css_import_path("main.sass".to_string(), false),
798            "./main.sass"
799        );
800    }
801
802    #[test]
803    fn normalize_bare_less_gets_dot_slash() {
804        assert_eq!(
805            normalize_css_import_path("theme.less".to_string(), false),
806            "./theme.less"
807        );
808    }
809
810    #[test]
811    fn normalize_bare_js_extension_stays_bare() {
812        assert_eq!(
813            normalize_css_import_path("module.js".to_string(), false),
814            "module.js"
815        );
816    }
817
818    #[test]
819    fn normalize_scss_bare_partial_gets_dot_slash() {
820        assert_eq!(
821            normalize_css_import_path("variables".to_string(), true),
822            "./variables"
823        );
824    }
825
826    #[test]
827    fn normalize_scss_bare_partial_with_subdir_gets_dot_slash() {
828        assert_eq!(
829            normalize_css_import_path("base/reset".to_string(), true),
830            "./base/reset"
831        );
832    }
833
834    #[test]
835    fn normalize_scss_builtin_stays_bare() {
836        assert_eq!(
837            normalize_css_import_path("sass:math".to_string(), true),
838            "sass:math"
839        );
840    }
841
842    #[test]
843    fn normalize_scss_relative_path_unchanged() {
844        assert_eq!(
845            normalize_css_import_path("../styles/variables".to_string(), true),
846            "../styles/variables"
847        );
848    }
849
850    #[test]
851    fn normalize_css_bare_extensionless_stays_bare() {
852        assert_eq!(
853            normalize_css_import_path("tailwindcss".to_string(), false),
854            "tailwindcss"
855        );
856    }
857
858    #[test]
859    fn normalize_scoped_package_with_css_extension_stays_bare() {
860        assert_eq!(
861            normalize_css_import_path("@fontsource/monaspace-neon/400.css".to_string(), false),
862            "@fontsource/monaspace-neon/400.css"
863        );
864    }
865
866    #[test]
867    fn normalize_scoped_package_with_scss_extension_stays_bare() {
868        assert_eq!(
869            normalize_css_import_path("@company/design-system/tokens.scss".to_string(), true),
870            "@company/design-system/tokens.scss"
871        );
872    }
873
874    #[test]
875    fn normalize_scoped_package_without_extension_stays_bare() {
876        assert_eq!(
877            normalize_css_import_path("@fallow/design-system/styles".to_string(), false),
878            "@fallow/design-system/styles"
879        );
880    }
881
882    #[test]
883    fn normalize_scoped_package_extensionless_scss_stays_bare() {
884        assert_eq!(
885            normalize_css_import_path("@company/tokens".to_string(), true),
886            "@company/tokens"
887        );
888    }
889
890    #[test]
891    fn normalize_path_alias_with_css_extension_stays_bare() {
892        assert_eq!(
893            normalize_css_import_path("@/components/Button.css".to_string(), false),
894            "@/components/Button.css"
895        );
896    }
897
898    #[test]
899    fn normalize_path_alias_extensionless_stays_bare() {
900        assert_eq!(
901            normalize_css_import_path("@/styles/variables".to_string(), false),
902            "@/styles/variables"
903        );
904    }
905
906    #[test]
907    fn strip_css_no_comments() {
908        let source = ".foo { color: red; }";
909        assert_eq!(strip_css_comments(source, false), source);
910    }
911
912    #[test]
913    fn strip_css_multiple_block_comments() {
914        let source = "/* comment-one */ .foo { } /* comment-two */ .bar { }";
915        let result = strip_css_comments(source, false);
916        assert!(!result.contains("comment-one"));
917        assert!(!result.contains("comment-two"));
918        assert!(result.contains(".foo"));
919        assert!(result.contains(".bar"));
920    }
921
922    #[test]
923    fn strip_scss_does_not_affect_non_scss() {
924        let source = "// this stays\n.foo { }";
925        let result = strip_css_comments(source, false);
926        assert!(result.contains("// this stays"));
927    }
928
929    #[test]
930    fn css_module_parses_suppressions() {
931        let info = parse_css_to_module(
932            fallow_types::discover::FileId(0),
933            Path::new("Component.module.css"),
934            "/* fallow-ignore-file */\n.btn { color: red; }",
935            0,
936        );
937        assert!(!info.suppressions.is_empty());
938        assert_eq!(info.suppressions[0].line, 0);
939    }
940
941    #[test]
942    fn extracts_class_starting_with_underscore() {
943        let names = export_names("._private { } .__dunder { }");
944        assert!(names.contains(&"_private".to_string()));
945        assert!(names.contains(&"__dunder".to_string()));
946    }
947
948    #[test]
949    fn ignores_id_selectors() {
950        let names = export_names("#myId { color: red; }");
951        assert!(!names.contains(&"myId".to_string()));
952    }
953
954    #[test]
955    fn ignores_element_selectors() {
956        let names = export_names("div { color: red; } span { }");
957        assert!(names.is_empty());
958    }
959
960    #[test]
961    fn extract_css_imports_at_import_quoted() {
962        let imports = extract_css_imports(r#"@import "./reset.css";"#, false);
963        assert_eq!(imports, vec!["./reset.css"]);
964    }
965
966    #[test]
967    fn extract_css_imports_package_subpath_stays_bare() {
968        let imports =
969            extract_css_imports(r#"@import "tailwindcss/theme.css" layer(theme);"#, false);
970        assert_eq!(imports, vec!["tailwindcss/theme.css"]);
971    }
972
973    #[test]
974    fn extract_css_imports_at_import_url() {
975        let imports = extract_css_imports(r#"@import url("./reset.css");"#, false);
976        assert_eq!(imports, vec!["./reset.css"]);
977    }
978
979    #[test]
980    fn extract_css_imports_skips_remote_urls() {
981        let imports =
982            extract_css_imports(r#"@import "https://fonts.example.com/font.css";"#, false);
983        assert!(imports.is_empty());
984    }
985
986    #[test]
987    fn extract_css_imports_scss_use_normalizes_partial() {
988        let imports = extract_css_imports(r#"@use "variables";"#, true);
989        assert_eq!(imports, vec!["./variables"]);
990    }
991
992    #[test]
993    fn extract_css_imports_scss_forward_normalizes_partial() {
994        let imports = extract_css_imports(r#"@forward "tokens";"#, true);
995        assert_eq!(imports, vec!["./tokens"]);
996    }
997
998    #[test]
999    fn extract_css_imports_skips_comments() {
1000        let imports = extract_css_imports(
1001            r#"/* @import "./hidden.scss"; */
1002@use "real";"#,
1003            true,
1004        );
1005        assert_eq!(imports, vec!["./real"]);
1006    }
1007
1008    #[test]
1009    fn extract_css_imports_at_plugin_keeps_package_bare() {
1010        let imports = extract_css_imports(r#"@plugin "daisyui";"#, true);
1011        assert_eq!(imports, vec!["daisyui"]);
1012    }
1013
1014    #[test]
1015    fn extract_css_imports_at_plugin_tracks_relative_file() {
1016        let imports = extract_css_imports(r#"@plugin "./tailwind-plugin.js";"#, false);
1017        assert_eq!(imports, vec!["./tailwind-plugin.js"]);
1018    }
1019
1020    #[test]
1021    fn extract_css_imports_scss_at_import_kept_relative() {
1022        let imports = extract_css_imports(r"@import 'Foo';", true);
1023        assert_eq!(imports, vec!["./Foo"]);
1024    }
1025
1026    #[test]
1027    fn extract_css_imports_additional_data_string_body() {
1028        let body = r#"@use "./src/styles/global.scss";"#;
1029        let imports = extract_css_imports(body, true);
1030        assert_eq!(imports, vec!["./src/styles/global.scss"]);
1031    }
1032
1033    #[test]
1034    fn mask_with_whitespace_preserves_byte_length() {
1035        let src = "/* hello */ .foo { }";
1036        let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
1037        assert_eq!(masked.len(), src.len());
1038        assert!(masked.is_char_boundary(src.len()));
1039    }
1040
1041    #[test]
1042    fn mask_with_whitespace_preserves_offsets_around_multibyte() {
1043        let src = "/* \u{2713} */ .foo { }";
1044        let foo_offset = src.find(".foo").expect("`.foo` present");
1045        let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
1046        assert_eq!(masked.len(), src.len());
1047        assert_eq!(masked.find(".foo"), Some(foo_offset));
1048    }
1049
1050    /// Resolve a span's start to (line, col) using the same primitives the
1051    /// downstream pipeline uses in `crates/core/src/analyze/unused_exports.rs`.
1052    fn span_line_col(source: &str, start: u32) -> (u32, u32) {
1053        let offsets = fallow_types::extract::compute_line_offsets(source);
1054        fallow_types::extract::byte_offset_to_line_col(&offsets, start)
1055    }
1056
1057    #[test]
1058    fn span_points_at_real_class_declaration_line() {
1059        let source = "\n\n\n\n.foo { color: red; }\n";
1060        let exports = extract_css_module_exports(source, false);
1061        assert_eq!(exports.len(), 1);
1062        let span = exports[0].span;
1063        let (line, col) = span_line_col(source, span.start);
1064        assert_eq!(line, 5, "`.foo` on line 5 must produce line 5, not line 1");
1065        assert_eq!(
1066            col, 1,
1067            "column points at `f` in `.foo` (post-dot identifier)"
1068        );
1069        assert_eq!(
1070            &source[span.start as usize..span.end as usize],
1071            "foo",
1072            "span range must slice to the class identifier in the original source"
1073        );
1074    }
1075
1076    #[test]
1077    fn span_survives_multibyte_comment_prefix() {
1078        let source = "/* \u{2713} */\n.foo { }";
1079        let exports = extract_css_module_exports(source, false);
1080        assert_eq!(exports.len(), 1);
1081        let span = exports[0].span;
1082        assert!(
1083            source.is_char_boundary(span.start as usize),
1084            "span.start must lie on a UTF-8 char boundary"
1085        );
1086        assert_eq!(&source[span.start as usize..span.end as usize], "foo");
1087    }
1088
1089    #[test]
1090    fn span_skips_at_layer_prelude_dot_segments() {
1091        let source = "@layer foo.bar { }\n.root { }\n";
1092        let exports = extract_css_module_exports(source, false);
1093        let names: Vec<_> = exports
1094            .iter()
1095            .filter_map(|e| match &e.name {
1096                ExportName::Named(n) => Some(n.as_str()),
1097                ExportName::Default => None,
1098            })
1099            .collect();
1100        assert_eq!(names, vec!["root"], "@layer sub-segments must not export");
1101        let span = exports[0].span;
1102        let (line, _col) = span_line_col(source, span.start);
1103        assert_eq!(line, 2, "`.root` lives on line 2 of the original source");
1104        assert_eq!(&source[span.start as usize..span.end as usize], "root");
1105    }
1106
1107    #[test]
1108    fn span_skips_classes_in_strings() {
1109        let source = ".real { content: \".fake\"; }\n.also-real { }\n";
1110        let exports = extract_css_module_exports(source, false);
1111        let names: Vec<_> = exports
1112            .iter()
1113            .filter_map(|e| match &e.name {
1114                ExportName::Named(n) => Some(n.as_str()),
1115                ExportName::Default => None,
1116            })
1117            .collect();
1118        assert_eq!(names, vec!["real", "also-real"]);
1119        for export in &exports {
1120            let span = export.span;
1121            let slice = &source[span.start as usize..span.end as usize];
1122            match &export.name {
1123                ExportName::Named(n) => assert_eq!(slice, n.as_str()),
1124                ExportName::Default => unreachable!("CSS modules emit only named exports"),
1125            }
1126        }
1127    }
1128
1129    #[test]
1130    fn span_deduplicates_to_first_occurrence() {
1131        let source = ".btn { color: red; }\n.btn { color: blue; }\n";
1132        let exports = extract_css_module_exports(source, false);
1133        assert_eq!(exports.len(), 1);
1134        let (line, _col) = span_line_col(source, exports[0].span.start);
1135        assert_eq!(
1136            line, 1,
1137            "first occurrence wins for deduplicated class names"
1138        );
1139    }
1140
1141    #[test]
1142    fn span_inside_media_query() {
1143        let source =
1144            "@media (max-width: 768px) {\n  .mobile { display: block; }\n  .desktop { }\n}\n";
1145        let exports = extract_css_module_exports(source, false);
1146        let by_name: rustc_hash::FxHashMap<&str, oxc_span::Span> = exports
1147            .iter()
1148            .filter_map(|e| match &e.name {
1149                ExportName::Named(n) => Some((n.as_str(), e.span)),
1150                ExportName::Default => None,
1151            })
1152            .collect();
1153        let mobile_line = span_line_col(source, by_name["mobile"].start).0;
1154        let desktop_line = span_line_col(source, by_name["desktop"].start).0;
1155        assert_eq!(mobile_line, 2);
1156        assert_eq!(desktop_line, 3);
1157    }
1158
1159    #[test]
1160    fn at_layer_only_module_emits_no_exports() {
1161        let exports = extract_css_module_exports("@layer foo.bar, foo.baz;\n", false);
1162        assert!(exports.is_empty());
1163    }
1164
1165    #[test]
1166    fn parse_css_to_module_resolves_real_line_offsets() {
1167        let source = "\n\n\n\n.foo { color: red; }\n";
1168        let info = parse_css_to_module(
1169            fallow_types::discover::FileId(0),
1170            Path::new("Component.module.css"),
1171            source,
1172            0,
1173        );
1174        assert_eq!(info.exports.len(), 1);
1175        let (line, _col) = fallow_types::extract::byte_offset_to_line_col(
1176            &info.line_offsets,
1177            info.exports[0].span.start,
1178        );
1179        assert_eq!(line, 5, "downstream line must equal the source line");
1180    }
1181}