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
61/// Regex to strip the prelude of `@layer` and `@import` at-rules before
62/// CSS-Modules class extraction. Matches the `@keyword` plus everything up to
63/// (but not including) the next `;` or `{`, so block bodies are preserved.
64///
65/// Narrow allowlist by design (issue #540): only at-rules whose preludes
66/// legitimately carry dot-separated identifiers without selector semantics are
67/// stripped. `@layer foo.bar` (CSS Cascading & Inheritance L5) lists layer
68/// names; `@import url("x.css") layer(theme.button)` carries a parenthesised
69/// layer reference. `@scope (.foo) to (.bar)` keeps its existing behavior
70/// because the prelude IS a selector list and `.foo` / `.bar` are real class
71/// references that the user may want to surface as exports.
72static CSS_AT_RULE_PRELUDE_RE: LazyLock<regex::Regex> =
73    LazyLock::new(|| regex::Regex::new(r"@(?:layer|import)\b[^;{]*").expect("valid regex"));
74
75pub(crate) fn is_css_file(path: &Path) -> bool {
76    path.extension()
77        .and_then(|e| e.to_str())
78        .is_some_and(|ext| ext == "css" || ext == "scss")
79}
80
81/// A CSS import source with both the literal source and fallow's resolver-normalized form.
82#[derive(Debug, Clone, PartialEq, Eq)]
83pub struct CssImportSource {
84    /// The import source exactly as it appeared in `@import` / `@use` / `@forward` / `@plugin`.
85    pub raw: String,
86    /// The source normalized for fallow's resolver (`variables` -> `./variables` in SCSS).
87    pub normalized: String,
88    /// Whether this source came from Tailwind CSS `@plugin`.
89    pub is_plugin: bool,
90}
91
92fn is_css_module_file(path: &Path) -> bool {
93    is_css_file(path)
94        && path
95            .file_stem()
96            .and_then(|s| s.to_str())
97            .is_some_and(|stem| stem.ends_with(".module"))
98}
99
100/// Returns true if a CSS import source is a remote URL or data URI that should be skipped.
101fn is_css_url_import(source: &str) -> bool {
102    source.starts_with("http://") || source.starts_with("https://") || source.starts_with("data:")
103}
104
105/// Normalize a CSS/SCSS import path to use `./` prefix for relative paths.
106/// Bare file names such as `reset.css` stay relative for CSS ergonomics, while
107/// package subpaths such as `tailwindcss/theme.css` stay bare so bundler-style
108/// package CSS imports resolve through `node_modules`.
109///
110/// When `is_scss` is true, extensionless specifiers that are not SCSS built-in
111/// modules (`sass:*`) are treated as relative imports (SCSS partial convention).
112/// This handles `@use 'variables'` resolving to `./_variables.scss`.
113///
114/// Scoped npm packages (`@scope/pkg`) are always kept bare, even when they have
115/// CSS extensions (e.g., `@fontsource/monaspace-neon/400.css`). Bundlers like
116/// Vite resolve these from node_modules, not as relative paths.
117fn normalize_css_import_path(path: String, is_scss: bool) -> String {
118    if path.starts_with('.') || path.starts_with('/') || path.contains("://") {
119        return path;
120    }
121    // Scoped npm packages (`@scope/...`) are always bare specifiers resolved
122    // from node_modules, regardless of file extension.
123    if path.starts_with('@') && path.contains('/') {
124        return path;
125    }
126    let path_ref = std::path::Path::new(&path);
127    if !is_scss
128        && path.contains('/')
129        && path_ref
130            .extension()
131            .and_then(|e| e.to_str())
132            .is_some_and(is_style_extension)
133    {
134        return path;
135    }
136    // Bare filenames with CSS/SCSS extensions are relative file imports.
137    let ext = std::path::Path::new(&path)
138        .extension()
139        .and_then(|e| e.to_str());
140    match ext {
141        Some(e) if is_style_extension(e) => format!("./{path}"),
142        _ => {
143            // In SCSS, extensionless bare specifiers like `@use 'variables'` are
144            // local partials, not npm packages. SCSS built-in modules (`sass:math`,
145            // `sass:color`) use a colon prefix and should stay bare.
146            if is_scss && !path.contains(':') {
147                format!("./{path}")
148            } else {
149                path
150            }
151        }
152    }
153}
154
155fn is_style_extension(ext: &str) -> bool {
156    ext.eq_ignore_ascii_case("css")
157        || ext.eq_ignore_ascii_case("scss")
158        || ext.eq_ignore_ascii_case("sass")
159        || ext.eq_ignore_ascii_case("less")
160}
161
162/// Strip comments from CSS/SCSS source to avoid matching directives inside comments.
163fn strip_css_comments(source: &str, is_scss: bool) -> String {
164    let stripped = CSS_COMMENT_RE.replace_all(source, "");
165    if is_scss {
166        SCSS_LINE_COMMENT_RE.replace_all(&stripped, "").into_owned()
167    } else {
168        stripped.into_owned()
169    }
170}
171
172/// Normalize a Tailwind CSS `@plugin` target.
173///
174/// Unlike SCSS `@use`, extensionless targets such as `daisyui` are package
175/// specifiers, not local partials. Keep bare specifiers bare and only preserve
176/// explicit relative/root-relative paths.
177fn normalize_css_plugin_path(path: String) -> String {
178    path
179}
180
181/// Extract `@import` / `@use` / `@forward` / `@plugin` source paths from a CSS/SCSS string.
182///
183/// Returns both the raw source and the normalized source. URL imports
184/// (`http://`, `https://`, `data:`) are skipped. Use [`extract_css_imports`]
185/// when only the normalized form is needed.
186#[must_use]
187pub fn extract_css_import_sources(source: &str, is_scss: bool) -> Vec<CssImportSource> {
188    let stripped = strip_css_comments(source, is_scss);
189    let mut out = Vec::new();
190
191    for cap in CSS_IMPORT_RE.captures_iter(&stripped) {
192        let raw = cap
193            .get(1)
194            .or_else(|| cap.get(2))
195            .or_else(|| cap.get(3))
196            .map(|m| m.as_str().trim().to_string());
197        if let Some(src) = raw
198            && !src.is_empty()
199            && !is_css_url_import(&src)
200        {
201            out.push(CssImportSource {
202                normalized: normalize_css_import_path(src.clone(), is_scss),
203                raw: src,
204                is_plugin: false,
205            });
206        }
207    }
208
209    if is_scss {
210        for cap in SCSS_USE_RE.captures_iter(&stripped) {
211            if let Some(m) = cap.get(1) {
212                let raw = m.as_str().to_string();
213                out.push(CssImportSource {
214                    normalized: normalize_css_import_path(raw.clone(), true),
215                    raw,
216                    is_plugin: false,
217                });
218            }
219        }
220    }
221
222    for cap in CSS_PLUGIN_RE.captures_iter(&stripped) {
223        if let Some(m) = cap.get(1) {
224            let raw = m.as_str().trim().to_string();
225            if !raw.is_empty() && !is_css_url_import(&raw) {
226                out.push(CssImportSource {
227                    normalized: normalize_css_plugin_path(raw.clone()),
228                    raw,
229                    is_plugin: true,
230                });
231            }
232        }
233    }
234
235    out
236}
237
238/// Extract normalized `@import` / `@use` / `@forward` / `@plugin` source paths from a CSS/SCSS string.
239///
240/// Returns specifiers normalized via `normalize_css_import_path`. URL imports
241/// (`http://`, `https://`, `data:`) are skipped. Used by callers that only need
242/// entry/dependency source paths; callers that need import kind information
243/// should use [`extract_css_import_sources`].
244#[must_use]
245pub fn extract_css_imports(source: &str, is_scss: bool) -> Vec<String> {
246    extract_css_import_sources(source, is_scss)
247        .into_iter()
248        .map(|source| source.normalized)
249        .collect()
250}
251
252/// Mask every regex match in `src` with ASCII spaces (`0x20`) of equal byte
253/// length, so byte offsets in the returned string correspond 1:1 to byte
254/// offsets in the original.
255///
256/// Used to neutralise CSS comments, quoted strings, `url(...)`, and at-rule
257/// preludes before scanning for `.class` selectors, while preserving the
258/// original-source positions that callers need to populate `ExportInfo.span`
259/// (issue #549). The `regex` crate guarantees match boundaries respect UTF-8
260/// char boundaries, so the masked buffer is always valid UTF-8.
261fn mask_with_whitespace(src: &str, re: &regex::Regex) -> String {
262    let mut out = String::with_capacity(src.len());
263    let mut cursor = 0;
264    for m in re.find_iter(src) {
265        out.push_str(&src[cursor..m.start()]);
266        for _ in m.start()..m.end() {
267            out.push(' ');
268        }
269        cursor = m.end();
270    }
271    out.push_str(&src[cursor..]);
272    out
273}
274
275/// Extract class names from a CSS module file as named exports.
276///
277/// Each emitted [`ExportInfo`] carries a [`Span`] pointing at the bare class
278/// name in the ORIGINAL `source` (no leading dot), so downstream
279/// `compute_line_offsets` resolves the real declaration line and column
280/// instead of falling back to line:1 col:0 (issue #549).
281pub fn extract_css_module_exports(source: &str, is_scss: bool) -> Vec<ExportInfo> {
282    // Offset-preserving masking pipeline: each pass blanks matched bytes with
283    // ASCII spaces of equal byte length so capture offsets in the masked
284    // buffer index back into the original source. Order mirrors the legacy
285    // strip pipeline so the SEMANTIC set of class-name candidates is unchanged.
286    let mut masked = mask_with_whitespace(source, &CSS_COMMENT_RE);
287    if is_scss {
288        masked = mask_with_whitespace(&masked, &SCSS_LINE_COMMENT_RE);
289    }
290    masked = mask_with_whitespace(&masked, &CSS_NON_SELECTOR_RE);
291    // Strip `@layer` and `@import` preludes so dot-separated layer names
292    // (`@layer foo.bar`, `@import url("x.css") layer(theme.button)`) do not
293    // leak into the class-name scan. See `CSS_AT_RULE_PRELUDE_RE` for the
294    // allowlist rationale (issue #540).
295    masked = mask_with_whitespace(&masked, &CSS_AT_RULE_PRELUDE_RE);
296
297    let mut seen = rustc_hash::FxHashSet::default();
298    let mut exports = Vec::new();
299    for cap in CSS_CLASS_RE.captures_iter(&masked) {
300        if let Some(m) = cap.get(1) {
301            let class_name = m.as_str().to_string();
302            if seen.insert(class_name.clone()) {
303                #[expect(
304                    clippy::cast_possible_truncation,
305                    reason = "CSS files exceeding u32::MAX bytes are not a realistic input"
306                )]
307                let span = Span::new(m.start() as u32, m.end() as u32);
308                exports.push(ExportInfo {
309                    name: ExportName::Named(class_name),
310                    local_name: None,
311                    is_type_only: false,
312                    visibility: VisibilityTag::None,
313                    span,
314                    members: Vec::new(),
315                    is_side_effect_used: false,
316                    super_class: None,
317                });
318            }
319        }
320    }
321    exports
322}
323
324/// Parse a CSS/SCSS file, extracting @import, @use, @forward, @plugin, @apply, and @tailwind directives.
325pub(crate) fn parse_css_to_module(
326    file_id: FileId,
327    path: &Path,
328    source: &str,
329    content_hash: u64,
330) -> ModuleInfo {
331    let parsed_suppressions = crate::suppress::parse_suppressions_from_source(source);
332    let is_scss = path
333        .extension()
334        .and_then(|e| e.to_str())
335        .is_some_and(|ext| ext == "scss");
336
337    // Strip comments before matching to avoid false positives from commented-out code.
338    let stripped = strip_css_comments(source, is_scss);
339
340    let mut imports = Vec::new();
341
342    // Extract @import statements
343    for cap in CSS_IMPORT_RE.captures_iter(&stripped) {
344        let source_path = cap
345            .get(1)
346            .or_else(|| cap.get(2))
347            .or_else(|| cap.get(3))
348            .map(|m| m.as_str().trim().to_string());
349        if let Some(src) = source_path
350            && !src.is_empty()
351            && !is_css_url_import(&src)
352        {
353            // CSS/SCSS @import resolves relative paths without ./ prefix,
354            // so normalize to ./ to avoid bare-specifier misclassification
355            let src = normalize_css_import_path(src, is_scss);
356            imports.push(ImportInfo {
357                source: src,
358                imported_name: ImportedName::SideEffect,
359                local_name: String::new(),
360                is_type_only: false,
361                from_style: false,
362                span: Span::default(),
363                source_span: Span::default(),
364            });
365        }
366    }
367
368    // Extract SCSS @use/@forward statements
369    if is_scss {
370        for cap in SCSS_USE_RE.captures_iter(&stripped) {
371            if let Some(m) = cap.get(1) {
372                imports.push(ImportInfo {
373                    source: normalize_css_import_path(m.as_str().to_string(), true),
374                    imported_name: ImportedName::SideEffect,
375                    local_name: String::new(),
376                    is_type_only: false,
377                    from_style: false,
378                    span: Span::default(),
379                    source_span: Span::default(),
380                });
381            }
382        }
383    }
384
385    // Extract Tailwind CSS @plugin directives. These can reference npm packages
386    // (e.g. `@plugin "daisyui"`) or explicit local plugin files.
387    for cap in CSS_PLUGIN_RE.captures_iter(&stripped) {
388        if let Some(m) = cap.get(1) {
389            let source = m.as_str().trim().to_string();
390            if !source.is_empty() && !is_css_url_import(&source) {
391                imports.push(ImportInfo {
392                    source: normalize_css_plugin_path(source),
393                    imported_name: ImportedName::Default,
394                    local_name: String::new(),
395                    is_type_only: false,
396                    from_style: false,
397                    span: Span::default(),
398                    source_span: Span::default(),
399                });
400            }
401        }
402    }
403
404    // If @apply or @tailwind directives exist, create a synthetic import to tailwindcss
405    // to mark the dependency as used
406    let has_apply = CSS_APPLY_RE.is_match(&stripped);
407    let has_tailwind = CSS_TAILWIND_RE.is_match(&stripped);
408    if has_apply || has_tailwind {
409        imports.push(ImportInfo {
410            source: "tailwindcss".to_string(),
411            imported_name: ImportedName::SideEffect,
412            local_name: String::new(),
413            is_type_only: false,
414            from_style: false,
415            span: Span::default(),
416            source_span: Span::default(),
417        });
418    }
419
420    // For CSS module files, extract class names as named exports. Pass the
421    // ORIGINAL source (not `stripped`); `extract_css_module_exports` runs its
422    // own offset-preserving masking so `ExportInfo.span` resolves to real
423    // line/col via `line_offsets` below.
424    let exports = if is_css_module_file(path) {
425        extract_css_module_exports(source, is_scss)
426    } else {
427        Vec::new()
428    };
429
430    ModuleInfo {
431        file_id,
432        exports,
433        imports,
434        re_exports: Vec::new(),
435        dynamic_imports: Vec::new(),
436        dynamic_import_patterns: Vec::new(),
437        require_calls: Vec::new(),
438        member_accesses: Vec::new(),
439        whole_object_uses: Vec::new(),
440        has_cjs_exports: false,
441        has_angular_component_template_url: false,
442        content_hash,
443        suppressions: parsed_suppressions.suppressions,
444        unknown_suppression_kinds: parsed_suppressions.unknown_kinds,
445        unused_import_bindings: Vec::new(),
446        type_referenced_import_bindings: Vec::new(),
447        value_referenced_import_bindings: Vec::new(),
448        line_offsets: fallow_types::extract::compute_line_offsets(source),
449        complexity: Vec::new(),
450        flag_uses: Vec::new(),
451        class_heritage: vec![],
452        local_type_declarations: Vec::new(),
453        public_signature_type_references: Vec::new(),
454        namespace_object_aliases: Vec::new(),
455        iconify_prefixes: Vec::new(),
456        auto_import_candidates: Vec::new(),
457    }
458}
459
460#[cfg(test)]
461mod tests {
462    use super::*;
463
464    /// Helper to collect export names as strings from `extract_css_module_exports`.
465    fn export_names(source: &str) -> Vec<String> {
466        extract_css_module_exports(source, false)
467            .into_iter()
468            .filter_map(|e| match e.name {
469                ExportName::Named(n) => Some(n),
470                ExportName::Default => None,
471            })
472            .collect()
473    }
474
475    // ── is_css_file ──────────────────────────────────────────────
476
477    #[test]
478    fn is_css_file_css() {
479        assert!(is_css_file(Path::new("styles.css")));
480    }
481
482    #[test]
483    fn is_css_file_scss() {
484        assert!(is_css_file(Path::new("styles.scss")));
485    }
486
487    #[test]
488    fn is_css_file_rejects_js() {
489        assert!(!is_css_file(Path::new("app.js")));
490    }
491
492    #[test]
493    fn is_css_file_rejects_ts() {
494        assert!(!is_css_file(Path::new("app.ts")));
495    }
496
497    #[test]
498    fn is_css_file_rejects_less() {
499        assert!(!is_css_file(Path::new("styles.less")));
500    }
501
502    #[test]
503    fn is_css_file_rejects_no_extension() {
504        assert!(!is_css_file(Path::new("Makefile")));
505    }
506
507    // ── is_css_module_file ───────────────────────────────────────
508
509    #[test]
510    fn is_css_module_file_module_css() {
511        assert!(is_css_module_file(Path::new("Component.module.css")));
512    }
513
514    #[test]
515    fn is_css_module_file_module_scss() {
516        assert!(is_css_module_file(Path::new("Component.module.scss")));
517    }
518
519    #[test]
520    fn is_css_module_file_rejects_plain_css() {
521        assert!(!is_css_module_file(Path::new("styles.css")));
522    }
523
524    #[test]
525    fn is_css_module_file_rejects_plain_scss() {
526        assert!(!is_css_module_file(Path::new("styles.scss")));
527    }
528
529    #[test]
530    fn is_css_module_file_rejects_module_js() {
531        assert!(!is_css_module_file(Path::new("utils.module.js")));
532    }
533
534    // ── extract_css_module_exports: basic class extraction ───────
535
536    #[test]
537    fn extracts_single_class() {
538        let names = export_names(".foo { color: red; }");
539        assert_eq!(names, vec!["foo"]);
540    }
541
542    #[test]
543    fn extracts_multiple_classes() {
544        let names = export_names(".foo { } .bar { }");
545        assert_eq!(names, vec!["foo", "bar"]);
546    }
547
548    #[test]
549    fn extracts_nested_classes() {
550        let names = export_names(".foo .bar { color: red; }");
551        assert!(names.contains(&"foo".to_string()));
552        assert!(names.contains(&"bar".to_string()));
553    }
554
555    #[test]
556    fn extracts_hyphenated_class() {
557        let names = export_names(".my-class { }");
558        assert_eq!(names, vec!["my-class"]);
559    }
560
561    #[test]
562    fn extracts_camel_case_class() {
563        let names = export_names(".myClass { }");
564        assert_eq!(names, vec!["myClass"]);
565    }
566
567    #[test]
568    fn extracts_underscore_class() {
569        let names = export_names("._hidden { } .__wrapper { }");
570        assert!(names.contains(&"_hidden".to_string()));
571        assert!(names.contains(&"__wrapper".to_string()));
572    }
573
574    // ── Pseudo-selectors ─────────────────────────────────────────
575
576    #[test]
577    fn pseudo_selector_hover() {
578        let names = export_names(".foo:hover { color: blue; }");
579        assert_eq!(names, vec!["foo"]);
580    }
581
582    #[test]
583    fn pseudo_selector_focus() {
584        let names = export_names(".input:focus { outline: none; }");
585        assert_eq!(names, vec!["input"]);
586    }
587
588    #[test]
589    fn pseudo_element_before() {
590        let names = export_names(".icon::before { content: ''; }");
591        assert_eq!(names, vec!["icon"]);
592    }
593
594    #[test]
595    fn combined_pseudo_selectors() {
596        let names = export_names(".btn:hover, .btn:active, .btn:focus { }");
597        // "btn" should be deduplicated
598        assert_eq!(names, vec!["btn"]);
599    }
600
601    // ── Media queries ────────────────────────────────────────────
602
603    #[test]
604    fn classes_inside_media_query() {
605        let names = export_names(
606            "@media (max-width: 768px) { .mobile-nav { display: block; } .desktop-nav { display: none; } }",
607        );
608        assert!(names.contains(&"mobile-nav".to_string()));
609        assert!(names.contains(&"desktop-nav".to_string()));
610    }
611
612    #[test]
613    fn classes_inside_multi_line_media_query() {
614        // Body classes still extract when the `@media` prelude spans multiple
615        // lines. `@media` is not in the at-rule prelude allowlist, so the new
616        // strip never fires here; this test guards the pre-existing scanner
617        // behavior, not the new regex.
618        let names =
619            export_names("@media\n  screen and (min-width: 600px)\n{\n  .real { color: red; }\n}");
620        assert_eq!(names, vec!["real"]);
621    }
622
623    // ── Cascade layers (issue #540) ──────────────────────────────
624
625    #[test]
626    fn at_layer_statement_does_not_export() {
627        // `@layer foo.bar;` and `@layer foo.bar, foo.baz;` declare layer names,
628        // NOT class selectors. The dot is part of the layer-name grammar.
629        let names = export_names("@layer foo.bar;");
630        assert!(names.is_empty(), "got {names:?}");
631        let names = export_names("@layer foo.bar, foo.baz;");
632        assert!(names.is_empty(), "got {names:?}");
633    }
634
635    #[test]
636    fn at_layer_block_keeps_body_classes() {
637        // The block body still scans for real classes; only the prelude is
638        // stripped.
639        let names = export_names("@layer foo.bar { .root { color: red; } }");
640        assert_eq!(names, vec!["root"]);
641    }
642
643    #[test]
644    fn at_layer_multiline_prelude_keeps_body_classes() {
645        // `[^;{]` in the at-rule prelude regex includes newlines, so layer
646        // names split across lines are stripped just like single-line ones.
647        let names = export_names("@layer\n  foo.bar\n{ .root { color: red; } }");
648        assert_eq!(names, vec!["root"]);
649    }
650
651    #[test]
652    fn at_layer_with_nested_media_keeps_body() {
653        // Nested at-rules: the @layer prelude is stripped but the @media
654        // body still scans (the @media prelude is not in the allowlist and
655        // does not contain class-like tokens anyway).
656        let names =
657            export_names("@layer foo.bar { @media (max-width: 768px) { .real { color: red; } } }");
658        assert_eq!(names, vec!["real"]);
659    }
660
661    #[test]
662    fn at_import_with_layer_attribute_does_not_export() {
663        // `@import url("x.css") layer(theme.button);` carries a parenthesised
664        // layer reference in its prelude. After url() and string stripping the
665        // remaining text still contains `.button`; the @import prelude strip
666        // wipes it.
667        let names = export_names(r#"@import url("x.css") layer(theme.button);"#);
668        assert!(names.is_empty(), "got {names:?}");
669    }
670
671    #[test]
672    fn class_then_at_layer_does_not_leak_prelude() {
673        // The @layer prelude strip must match only the at-rule's own prelude,
674        // not consume the preceding `.outer` selector.
675        let names =
676            export_names(".outer { color: blue; } @layer foo.bar { .inner { color: red; } }");
677        assert_eq!(names, vec!["outer", "inner"]);
678    }
679
680    // ── No-regression contracts ──────────────────────────────────
681
682    #[test]
683    fn at_scope_keeps_selector_list_classes() {
684        // `@scope (.parent) to (.child) { ... }` puts a selector list in its
685        // prelude. `.parent` and `.child` are GENUINE class references; the
686        // narrow at-rule allowlist intentionally does NOT strip @scope so
687        // these still extract as exports (matching pre-fix behavior).
688        let names = export_names("@scope (.parent) to (.child) { .title { color: red; } }");
689        assert!(names.contains(&"parent".to_string()), "got {names:?}");
690        assert!(names.contains(&"child".to_string()), "got {names:?}");
691        assert!(names.contains(&"title".to_string()), "got {names:?}");
692    }
693
694    #[test]
695    fn at_keyframes_numeric_step_is_not_class() {
696        // `@keyframes` percentage selectors and CSS numeric literals like
697        // `scale(.5)` start with a digit after the dot. `CSS_CLASS_RE`'s
698        // first-char anchor (`[a-zA-Z_]`) already rejects them; this test
699        // locks down that contract so a future regex relaxation does not
700        // silently start extracting `5` as a class name.
701        let names = export_names(
702            "@keyframes slide { 0% { transform: scale(.5); } 100% { transform: scale(1); } }",
703        );
704        assert!(names.is_empty(), "got {names:?}");
705    }
706
707    #[test]
708    fn at_webkit_keyframes_keeps_body_classes() {
709        // Vendor-prefixed at-rules are NOT in the prelude-strip allowlist
710        // (their preludes are simple idents without dots, so stripping is not
711        // required). Body classes still extract normally.
712        let names = export_names("@-webkit-keyframes slide { 0% { } 100% { } } .real { }");
713        assert_eq!(names, vec!["real"]);
714    }
715
716    // ── Deduplication ────────────────────────────────────────────
717
718    #[test]
719    fn deduplicates_repeated_class() {
720        let names = export_names(".btn { color: red; } .btn { font-size: 14px; }");
721        assert_eq!(names.iter().filter(|n| *n == "btn").count(), 1);
722    }
723
724    // ── Edge cases ───────────────────────────────────────────────
725
726    #[test]
727    fn empty_source() {
728        let names = export_names("");
729        assert!(names.is_empty());
730    }
731
732    #[test]
733    fn no_classes() {
734        let names = export_names("body { margin: 0; } * { box-sizing: border-box; }");
735        assert!(names.is_empty());
736    }
737
738    #[test]
739    fn ignores_classes_in_block_comments() {
740        // After issue #549, extract_css_module_exports masks comments itself
741        // (offset-preserving) so callers can pass the original source.
742        let names = export_names("/* .fake { } */ .real { }");
743        assert!(!names.contains(&"fake".to_string()));
744        assert!(names.contains(&"real".to_string()));
745    }
746
747    #[test]
748    fn ignores_classes_in_scss_line_comments() {
749        let exports = extract_css_module_exports("// .fake\n.real { }", true);
750        let names: Vec<_> = exports
751            .iter()
752            .filter_map(|e| match &e.name {
753                ExportName::Named(n) => Some(n.as_str()),
754                ExportName::Default => None,
755            })
756            .collect();
757        assert_eq!(names, vec!["real"]);
758    }
759
760    #[test]
761    fn ignores_classes_in_strings() {
762        let names = export_names(r#".real { content: ".fake"; }"#);
763        assert!(names.contains(&"real".to_string()));
764        assert!(!names.contains(&"fake".to_string()));
765    }
766
767    #[test]
768    fn ignores_classes_in_url() {
769        let names = export_names(".real { background: url(./images/hero.png); }");
770        assert!(names.contains(&"real".to_string()));
771        // "png" from "hero.png" should not be extracted
772        assert!(!names.contains(&"png".to_string()));
773    }
774
775    // ── strip_css_comments ───────────────────────────────────────
776
777    #[test]
778    fn strip_css_block_comment() {
779        let result = strip_css_comments("/* removed */ .kept { }", false);
780        assert!(!result.contains("removed"));
781        assert!(result.contains(".kept"));
782    }
783
784    #[test]
785    fn strip_scss_line_comment() {
786        let result = strip_css_comments("// removed\n.kept { }", true);
787        assert!(!result.contains("removed"));
788        assert!(result.contains(".kept"));
789    }
790
791    #[test]
792    fn strip_scss_preserves_css_outside_comments() {
793        let source = "// line comment\n/* block comment */\n.visible { color: red; }";
794        let result = strip_css_comments(source, true);
795        assert!(result.contains(".visible"));
796    }
797
798    // ── is_css_url_import ────────────────────────────────────────
799
800    #[test]
801    fn url_import_http() {
802        assert!(is_css_url_import("http://example.com/style.css"));
803    }
804
805    #[test]
806    fn url_import_https() {
807        assert!(is_css_url_import("https://fonts.googleapis.com/css"));
808    }
809
810    #[test]
811    fn url_import_data() {
812        assert!(is_css_url_import("data:text/css;base64,abc"));
813    }
814
815    #[test]
816    fn url_import_local_not_skipped() {
817        assert!(!is_css_url_import("./local.css"));
818    }
819
820    #[test]
821    fn url_import_bare_specifier_not_skipped() {
822        assert!(!is_css_url_import("tailwindcss"));
823    }
824
825    // ── normalize_css_import_path ─────────────────────────────────
826
827    #[test]
828    fn normalize_relative_dot_path_unchanged() {
829        assert_eq!(
830            normalize_css_import_path("./reset.css".to_string(), false),
831            "./reset.css"
832        );
833    }
834
835    #[test]
836    fn normalize_parent_relative_path_unchanged() {
837        assert_eq!(
838            normalize_css_import_path("../shared.scss".to_string(), false),
839            "../shared.scss"
840        );
841    }
842
843    #[test]
844    fn normalize_absolute_path_unchanged() {
845        assert_eq!(
846            normalize_css_import_path("/styles/main.css".to_string(), false),
847            "/styles/main.css"
848        );
849    }
850
851    #[test]
852    fn normalize_url_unchanged() {
853        assert_eq!(
854            normalize_css_import_path("https://example.com/style.css".to_string(), false),
855            "https://example.com/style.css"
856        );
857    }
858
859    #[test]
860    fn normalize_bare_css_gets_dot_slash() {
861        assert_eq!(
862            normalize_css_import_path("app.css".to_string(), false),
863            "./app.css"
864        );
865    }
866
867    #[test]
868    fn normalize_css_package_subpath_stays_bare() {
869        assert_eq!(
870            normalize_css_import_path("tailwindcss/theme.css".to_string(), false),
871            "tailwindcss/theme.css"
872        );
873    }
874
875    #[test]
876    fn normalize_css_package_subpath_with_dotted_name_stays_bare() {
877        assert_eq!(
878            normalize_css_import_path("highlight.js/styles/github.css".to_string(), false),
879            "highlight.js/styles/github.css"
880        );
881    }
882
883    #[test]
884    fn normalize_bare_scss_gets_dot_slash() {
885        assert_eq!(
886            normalize_css_import_path("vars.scss".to_string(), false),
887            "./vars.scss"
888        );
889    }
890
891    #[test]
892    fn normalize_bare_sass_gets_dot_slash() {
893        assert_eq!(
894            normalize_css_import_path("main.sass".to_string(), false),
895            "./main.sass"
896        );
897    }
898
899    #[test]
900    fn normalize_bare_less_gets_dot_slash() {
901        assert_eq!(
902            normalize_css_import_path("theme.less".to_string(), false),
903            "./theme.less"
904        );
905    }
906
907    #[test]
908    fn normalize_bare_js_extension_stays_bare() {
909        assert_eq!(
910            normalize_css_import_path("module.js".to_string(), false),
911            "module.js"
912        );
913    }
914
915    // ── SCSS partial normalization ───────────────────────────────
916
917    #[test]
918    fn normalize_scss_bare_partial_gets_dot_slash() {
919        assert_eq!(
920            normalize_css_import_path("variables".to_string(), true),
921            "./variables"
922        );
923    }
924
925    #[test]
926    fn normalize_scss_bare_partial_with_subdir_gets_dot_slash() {
927        assert_eq!(
928            normalize_css_import_path("base/reset".to_string(), true),
929            "./base/reset"
930        );
931    }
932
933    #[test]
934    fn normalize_scss_builtin_stays_bare() {
935        assert_eq!(
936            normalize_css_import_path("sass:math".to_string(), true),
937            "sass:math"
938        );
939    }
940
941    #[test]
942    fn normalize_scss_relative_path_unchanged() {
943        assert_eq!(
944            normalize_css_import_path("../styles/variables".to_string(), true),
945            "../styles/variables"
946        );
947    }
948
949    #[test]
950    fn normalize_css_bare_extensionless_stays_bare() {
951        // In CSS context (not SCSS), extensionless imports are npm packages
952        assert_eq!(
953            normalize_css_import_path("tailwindcss".to_string(), false),
954            "tailwindcss"
955        );
956    }
957
958    // ── Scoped npm packages stay bare ───────────────────────────
959
960    #[test]
961    fn normalize_scoped_package_with_css_extension_stays_bare() {
962        assert_eq!(
963            normalize_css_import_path("@fontsource/monaspace-neon/400.css".to_string(), false),
964            "@fontsource/monaspace-neon/400.css"
965        );
966    }
967
968    #[test]
969    fn normalize_scoped_package_with_scss_extension_stays_bare() {
970        assert_eq!(
971            normalize_css_import_path("@company/design-system/tokens.scss".to_string(), true),
972            "@company/design-system/tokens.scss"
973        );
974    }
975
976    #[test]
977    fn normalize_scoped_package_without_extension_stays_bare() {
978        assert_eq!(
979            normalize_css_import_path("@fallow/design-system/styles".to_string(), false),
980            "@fallow/design-system/styles"
981        );
982    }
983
984    #[test]
985    fn normalize_scoped_package_extensionless_scss_stays_bare() {
986        assert_eq!(
987            normalize_css_import_path("@company/tokens".to_string(), true),
988            "@company/tokens"
989        );
990    }
991
992    #[test]
993    fn normalize_path_alias_with_css_extension_stays_bare() {
994        // Path aliases like `@/components/Button.css` (configured via tsconfig paths
995        // or Vite alias) share the `@` prefix with scoped packages. They must stay
996        // bare so the resolver's path-alias path can handle them; prepending `./`
997        // would break resolution.
998        assert_eq!(
999            normalize_css_import_path("@/components/Button.css".to_string(), false),
1000            "@/components/Button.css"
1001        );
1002    }
1003
1004    #[test]
1005    fn normalize_path_alias_extensionless_stays_bare() {
1006        assert_eq!(
1007            normalize_css_import_path("@/styles/variables".to_string(), false),
1008            "@/styles/variables"
1009        );
1010    }
1011
1012    // ── strip_css_comments edge cases ─────────────────────────────
1013
1014    #[test]
1015    fn strip_css_no_comments() {
1016        let source = ".foo { color: red; }";
1017        assert_eq!(strip_css_comments(source, false), source);
1018    }
1019
1020    #[test]
1021    fn strip_css_multiple_block_comments() {
1022        let source = "/* comment-one */ .foo { } /* comment-two */ .bar { }";
1023        let result = strip_css_comments(source, false);
1024        assert!(!result.contains("comment-one"));
1025        assert!(!result.contains("comment-two"));
1026        assert!(result.contains(".foo"));
1027        assert!(result.contains(".bar"));
1028    }
1029
1030    #[test]
1031    fn strip_scss_does_not_affect_non_scss() {
1032        // When is_scss=false, line comments should NOT be stripped
1033        let source = "// this stays\n.foo { }";
1034        let result = strip_css_comments(source, false);
1035        assert!(result.contains("// this stays"));
1036    }
1037
1038    // ── parse_css_to_module: suppression integration ──────────────
1039
1040    #[test]
1041    fn css_module_parses_suppressions() {
1042        let info = parse_css_to_module(
1043            fallow_types::discover::FileId(0),
1044            Path::new("Component.module.css"),
1045            "/* fallow-ignore-file */\n.btn { color: red; }",
1046            0,
1047        );
1048        assert!(!info.suppressions.is_empty());
1049        assert_eq!(info.suppressions[0].line, 0);
1050    }
1051
1052    // ── CSS class name edge cases ─────────────────────────────────
1053
1054    #[test]
1055    fn extracts_class_starting_with_underscore() {
1056        let names = export_names("._private { } .__dunder { }");
1057        assert!(names.contains(&"_private".to_string()));
1058        assert!(names.contains(&"__dunder".to_string()));
1059    }
1060
1061    #[test]
1062    fn ignores_id_selectors() {
1063        let names = export_names("#myId { color: red; }");
1064        assert!(!names.contains(&"myId".to_string()));
1065    }
1066
1067    #[test]
1068    fn ignores_element_selectors() {
1069        let names = export_names("div { color: red; } span { }");
1070        assert!(names.is_empty());
1071    }
1072
1073    // ── extract_css_imports (issue #195: vite additionalData / SFC styles) ──
1074
1075    #[test]
1076    fn extract_css_imports_at_import_quoted() {
1077        let imports = extract_css_imports(r#"@import "./reset.css";"#, false);
1078        assert_eq!(imports, vec!["./reset.css"]);
1079    }
1080
1081    #[test]
1082    fn extract_css_imports_package_subpath_stays_bare() {
1083        let imports =
1084            extract_css_imports(r#"@import "tailwindcss/theme.css" layer(theme);"#, false);
1085        assert_eq!(imports, vec!["tailwindcss/theme.css"]);
1086    }
1087
1088    #[test]
1089    fn extract_css_imports_at_import_url() {
1090        let imports = extract_css_imports(r#"@import url("./reset.css");"#, false);
1091        assert_eq!(imports, vec!["./reset.css"]);
1092    }
1093
1094    #[test]
1095    fn extract_css_imports_skips_remote_urls() {
1096        let imports =
1097            extract_css_imports(r#"@import "https://fonts.example.com/font.css";"#, false);
1098        assert!(imports.is_empty());
1099    }
1100
1101    #[test]
1102    fn extract_css_imports_scss_use_normalizes_partial() {
1103        let imports = extract_css_imports(r#"@use "variables";"#, true);
1104        assert_eq!(imports, vec!["./variables"]);
1105    }
1106
1107    #[test]
1108    fn extract_css_imports_scss_forward_normalizes_partial() {
1109        let imports = extract_css_imports(r#"@forward "tokens";"#, true);
1110        assert_eq!(imports, vec!["./tokens"]);
1111    }
1112
1113    #[test]
1114    fn extract_css_imports_skips_comments() {
1115        let imports = extract_css_imports(
1116            r#"/* @import "./hidden.scss"; */
1117@use "real";"#,
1118            true,
1119        );
1120        assert_eq!(imports, vec!["./real"]);
1121    }
1122
1123    #[test]
1124    fn extract_css_imports_at_plugin_keeps_package_bare() {
1125        let imports = extract_css_imports(r#"@plugin "daisyui";"#, true);
1126        assert_eq!(imports, vec!["daisyui"]);
1127    }
1128
1129    #[test]
1130    fn extract_css_imports_at_plugin_tracks_relative_file() {
1131        let imports = extract_css_imports(r#"@plugin "./tailwind-plugin.js";"#, false);
1132        assert_eq!(imports, vec!["./tailwind-plugin.js"]);
1133    }
1134
1135    #[test]
1136    fn extract_css_imports_scss_at_import_kept_relative() {
1137        let imports = extract_css_imports(r"@import 'Foo';", true);
1138        // Bare specifier in SCSS context is normalized to ./
1139        assert_eq!(imports, vec!["./Foo"]);
1140    }
1141
1142    #[test]
1143    fn extract_css_imports_additional_data_string_body() {
1144        // Mimics what Vite's css.preprocessorOptions.scss.additionalData ships
1145        let body = r#"@use "./src/styles/global.scss";"#;
1146        let imports = extract_css_imports(body, true);
1147        assert_eq!(imports, vec!["./src/styles/global.scss"]);
1148    }
1149
1150    // ── mask_with_whitespace (issue #549) ─────────────────────────
1151
1152    #[test]
1153    fn mask_with_whitespace_preserves_byte_length() {
1154        let src = "/* hello */ .foo { }";
1155        let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
1156        assert_eq!(masked.len(), src.len());
1157        assert!(masked.is_char_boundary(src.len()));
1158    }
1159
1160    #[test]
1161    fn mask_with_whitespace_preserves_offsets_around_multibyte() {
1162        // The block comment contains a multi-byte UTF-8 char (U+2713 CHECK MARK,
1163        // 3 bytes). The mask replaces the 3 comment bytes with 3 ASCII spaces;
1164        // the post-comment `.foo` selector keeps its original byte offset.
1165        let src = "/* \u{2713} */ .foo { }";
1166        let foo_offset = src.find(".foo").expect("`.foo` present");
1167        let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
1168        assert_eq!(masked.len(), src.len());
1169        assert_eq!(masked.find(".foo"), Some(foo_offset));
1170    }
1171
1172    // ── extract_css_module_exports span correctness (issue #549) ──
1173
1174    /// Resolve a span's start to (line, col) using the same primitives the
1175    /// downstream pipeline uses in `crates/core/src/analyze/unused_exports.rs`.
1176    fn span_line_col(source: &str, start: u32) -> (u32, u32) {
1177        let offsets = fallow_types::extract::compute_line_offsets(source);
1178        fallow_types::extract::byte_offset_to_line_col(&offsets, start)
1179    }
1180
1181    #[test]
1182    fn span_points_at_real_class_declaration_line() {
1183        let source = "\n\n\n\n.foo { color: red; }\n";
1184        let exports = extract_css_module_exports(source, false);
1185        assert_eq!(exports.len(), 1);
1186        let span = exports[0].span;
1187        let (line, col) = span_line_col(source, span.start);
1188        assert_eq!(line, 5, "`.foo` on line 5 must produce line 5, not line 1");
1189        // col is a 0-based byte column. `.` sits at col 0; the bare identifier
1190        // begins at col 1.
1191        assert_eq!(
1192            col, 1,
1193            "column points at `f` in `.foo` (post-dot identifier)"
1194        );
1195        // Substring at the recorded span must equal the class name; otherwise
1196        // the masking pipeline shifted offsets.
1197        assert_eq!(
1198            &source[span.start as usize..span.end as usize],
1199            "foo",
1200            "span range must slice to the class identifier in the original source"
1201        );
1202    }
1203
1204    #[test]
1205    fn span_survives_multibyte_comment_prefix() {
1206        // The check mark is 3 bytes in UTF-8. Even when the mask replaces a
1207        // 3-byte char with 3 spaces, the post-comment `.foo` capture must
1208        // land on a UTF-8 char boundary in the ORIGINAL source.
1209        let source = "/* \u{2713} */\n.foo { }";
1210        let exports = extract_css_module_exports(source, false);
1211        assert_eq!(exports.len(), 1);
1212        let span = exports[0].span;
1213        assert!(
1214            source.is_char_boundary(span.start as usize),
1215            "span.start must lie on a UTF-8 char boundary"
1216        );
1217        assert_eq!(&source[span.start as usize..span.end as usize], "foo");
1218    }
1219
1220    #[test]
1221    fn span_skips_at_layer_prelude_dot_segments() {
1222        // Regression for #540 plus #549: `@layer foo.bar` must not emit `bar`
1223        // as an export, AND the body `.root` selector must land on `r` (line 2),
1224        // not on the `b` in `bar` (line 1).
1225        let source = "@layer foo.bar { }\n.root { }\n";
1226        let exports = extract_css_module_exports(source, false);
1227        let names: Vec<_> = exports
1228            .iter()
1229            .filter_map(|e| match &e.name {
1230                ExportName::Named(n) => Some(n.as_str()),
1231                ExportName::Default => None,
1232            })
1233            .collect();
1234        assert_eq!(names, vec!["root"], "@layer sub-segments must not export");
1235        let span = exports[0].span;
1236        let (line, _col) = span_line_col(source, span.start);
1237        assert_eq!(line, 2, "`.root` lives on line 2 of the original source");
1238        assert_eq!(&source[span.start as usize..span.end as usize], "root");
1239    }
1240
1241    #[test]
1242    fn span_skips_classes_in_strings() {
1243        let source = ".real { content: \".fake\"; }\n.also-real { }\n";
1244        let exports = extract_css_module_exports(source, false);
1245        let names: Vec<_> = exports
1246            .iter()
1247            .filter_map(|e| match &e.name {
1248                ExportName::Named(n) => Some(n.as_str()),
1249                ExportName::Default => None,
1250            })
1251            .collect();
1252        assert_eq!(names, vec!["real", "also-real"]);
1253        // Each surviving export's span must slice to its declared name.
1254        for export in &exports {
1255            let span = export.span;
1256            let slice = &source[span.start as usize..span.end as usize];
1257            match &export.name {
1258                ExportName::Named(n) => assert_eq!(slice, n.as_str()),
1259                ExportName::Default => unreachable!("CSS modules emit only named exports"),
1260            }
1261        }
1262    }
1263
1264    #[test]
1265    fn span_deduplicates_to_first_occurrence() {
1266        let source = ".btn { color: red; }\n.btn { color: blue; }\n";
1267        let exports = extract_css_module_exports(source, false);
1268        assert_eq!(exports.len(), 1);
1269        let (line, _col) = span_line_col(source, exports[0].span.start);
1270        assert_eq!(
1271            line, 1,
1272            "first occurrence wins for deduplicated class names"
1273        );
1274    }
1275
1276    #[test]
1277    fn span_inside_media_query() {
1278        let source =
1279            "@media (max-width: 768px) {\n  .mobile { display: block; }\n  .desktop { }\n}\n";
1280        let exports = extract_css_module_exports(source, false);
1281        let by_name: rustc_hash::FxHashMap<&str, oxc_span::Span> = exports
1282            .iter()
1283            .filter_map(|e| match &e.name {
1284                ExportName::Named(n) => Some((n.as_str(), e.span)),
1285                ExportName::Default => None,
1286            })
1287            .collect();
1288        let mobile_line = span_line_col(source, by_name["mobile"].start).0;
1289        let desktop_line = span_line_col(source, by_name["desktop"].start).0;
1290        assert_eq!(mobile_line, 2);
1291        assert_eq!(desktop_line, 3);
1292    }
1293
1294    #[test]
1295    fn at_layer_only_module_emits_no_exports() {
1296        // A `.module.css` with only a cascade-layer declaration must not emit
1297        // any exports (no body selectors, no class names).
1298        let exports = extract_css_module_exports("@layer foo.bar, foo.baz;\n", false);
1299        assert!(exports.is_empty());
1300    }
1301
1302    #[test]
1303    fn parse_css_to_module_resolves_real_line_offsets() {
1304        // Integration test through the full parse_css_to_module pipeline.
1305        // A `.module.css` finding's downstream line/col must reflect the real
1306        // declaration position, not line 1.
1307        let source = "\n\n\n\n.foo { color: red; }\n";
1308        let info = parse_css_to_module(
1309            fallow_types::discover::FileId(0),
1310            Path::new("Component.module.css"),
1311            source,
1312            0,
1313        );
1314        assert_eq!(info.exports.len(), 1);
1315        let (line, _col) = fallow_types::extract::byte_offset_to_line_col(
1316            &info.line_offsets,
1317            info.exports[0].span.start,
1318        );
1319        assert_eq!(line, 5, "downstream line must equal the source line");
1320    }
1321}