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//!
6//! Extraction is a deliberate hybrid, not a half-finished migration. lightningcss
7//! owns the membership decision for standard CSS (which `.token` occurrences are
8//! genuine class selectors, via `lightningcss_class_set`); the regex scanners own
9//! span location and the entire SCSS path. lightningcss parses standard CSS only,
10//! not SCSS syntax (`@use`, `@forward`, `//` line comments, `$variables`), so SCSS
11//! files are gated away from the parser and the regex chain stays as permanent
12//! infrastructure rather than a transitional step toward an all-parser tokenizer.
13
14use std::path::Path;
15use std::sync::LazyLock;
16
17use lightningcss::rules::CssRule;
18use lightningcss::selector::{Component, PseudoClass, Selector, SelectorList};
19use lightningcss::stylesheet::{ParserOptions, StyleSheet};
20use oxc_span::Span;
21use rustc_hash::FxHashSet;
22
23use crate::{ExportInfo, ExportName, ImportInfo, ImportedName, ModuleInfo, VisibilityTag};
24use fallow_types::discover::FileId;
25
26/// Regex to extract CSS @import sources.
27/// Matches: @import "path"; @import 'path'; @import url("path"); @import url('path'); @import url(path);
28static CSS_IMPORT_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
29    crate::static_regex(
30        r#"@import\s+(?:url\(\s*(?:["']([^"']+)["']|([^)]+))\s*\)|["']([^"']+)["'])"#,
31    )
32});
33
34/// Regex to extract SCSS @use and @forward sources.
35/// Matches: @use "path"; @use 'path'; @forward "path"; @forward 'path';
36static SCSS_USE_RE: LazyLock<regex::Regex> =
37    LazyLock::new(|| crate::static_regex(r#"@(?:use|forward)\s+["']([^"']+)["']"#));
38
39/// Regex to extract Tailwind CSS @plugin sources.
40/// Matches: @plugin "package"; @plugin 'package'; @plugin "./local-plugin.js";
41static CSS_PLUGIN_RE: LazyLock<regex::Regex> =
42    LazyLock::new(|| crate::static_regex(r#"@plugin\s+["']([^"']+)["']"#));
43
44/// Regex to extract @apply class references.
45/// Matches: @apply class1 class2 class3;
46static CSS_APPLY_RE: LazyLock<regex::Regex> =
47    LazyLock::new(|| crate::static_regex(r"@apply\s+[^;}\n]+"));
48
49/// Regex to extract @tailwind directives.
50/// Matches: @tailwind base; @tailwind components; @tailwind utilities;
51static CSS_TAILWIND_RE: LazyLock<regex::Regex> =
52    LazyLock::new(|| crate::static_regex(r"@tailwind\s+\w+"));
53
54/// Regex to match CSS block comments (`/* ... */`) for stripping before extraction.
55static CSS_COMMENT_RE: LazyLock<regex::Regex> =
56    LazyLock::new(|| crate::static_regex(r"(?s)/\*.*?\*/"));
57
58/// Regex to match SCSS single-line comments (`// ...`) for stripping before extraction.
59static SCSS_LINE_COMMENT_RE: LazyLock<regex::Regex> =
60    LazyLock::new(|| crate::static_regex(r"//[^\n]*"));
61
62/// Regex to extract CSS class names from selectors.
63/// Matches `.className` in selectors. Applied after stripping comments, strings, and URLs.
64static CSS_CLASS_RE: LazyLock<regex::Regex> =
65    LazyLock::new(|| crate::static_regex(r"\.([a-zA-Z_][a-zA-Z0-9_-]*)"));
66
67/// Regex to strip quoted strings and `url(...)` content from CSS before class extraction.
68/// Prevents false positives from `content: ".foo"` and `url(./path/file.ext)`.
69static CSS_NON_SELECTOR_RE: LazyLock<regex::Regex> =
70    LazyLock::new(|| crate::static_regex(r#"(?s)"[^"]*"|'[^']*'|url\([^)]*\)"#));
71
72/// Regex to strip the prelude of `@layer` and `@import` at-rules before
73/// CSS-Modules class extraction. Matches the `@keyword` plus everything up to
74/// (but not including) the next `;` or `{`, so block bodies are preserved.
75///
76/// Narrow allowlist by design (issue #540): only at-rules whose preludes
77/// legitimately carry dot-separated identifiers without selector semantics are
78/// stripped. `@layer foo.bar` (CSS Cascading & Inheritance L5) lists layer
79/// names; `@import url("x.css") layer(theme.button)` carries a parenthesised
80/// layer reference. `@scope (.foo) to (.bar)` keeps its existing behavior
81/// because the prelude IS a selector list and `.foo` / `.bar` are real class
82/// references that the user may want to surface as exports.
83static CSS_AT_RULE_PRELUDE_RE: LazyLock<regex::Regex> =
84    LazyLock::new(|| crate::static_regex(r"@(?:layer|import)\b[^;{]*"));
85
86pub(crate) fn is_css_file(path: &Path) -> bool {
87    path.extension()
88        .and_then(|e| e.to_str())
89        .is_some_and(|ext| matches!(ext, "css" | "scss" | "sass" | "less"))
90}
91
92/// A CSS import source with both the literal source and fallow's resolver-normalized form.
93#[derive(Debug, Clone, PartialEq, Eq)]
94pub struct CssImportSource {
95    /// The import source exactly as it appeared in `@import` / `@use` / `@forward` / `@plugin`.
96    pub raw: String,
97    /// The source normalized for fallow's resolver (`variables` -> `./variables` in SCSS).
98    pub normalized: String,
99    /// Whether this source came from Tailwind CSS `@plugin`.
100    pub is_plugin: bool,
101    /// Span of the source specifier in the original CSS/SCSS input.
102    pub span: Span,
103}
104
105fn is_css_module_file(path: &Path) -> bool {
106    is_css_file(path)
107        && path
108            .file_stem()
109            .and_then(|s| s.to_str())
110            .is_some_and(|stem| stem.ends_with(".module"))
111}
112
113/// Returns true if a CSS import source is a remote URL or data URI that should be skipped.
114fn is_css_url_import(source: &str) -> bool {
115    source.starts_with("http://") || source.starts_with("https://") || source.starts_with("data:")
116}
117
118/// Normalize a CSS/SCSS import path to use `./` prefix for relative paths.
119/// Bare file names such as `reset.css` stay relative for CSS ergonomics, while
120/// package subpaths such as `tailwindcss/theme.css` stay bare so bundler-style
121/// package CSS imports resolve through `node_modules`.
122///
123/// When `is_scss` is true, extensionless specifiers that are not SCSS built-in
124/// modules (`sass:*`) are treated as relative imports (SCSS partial convention).
125/// This handles `@use 'variables'` resolving to `./_variables.scss`.
126///
127/// Scoped npm packages (`@scope/pkg`) are always kept bare, even when they have
128/// CSS extensions (e.g., `@fontsource/monaspace-neon/400.css`). Bundlers like
129/// Vite resolve these from node_modules, not as relative paths.
130fn normalize_css_import_path(path: String, is_scss: bool) -> String {
131    if path.starts_with('.') || path.starts_with('/') || path.contains("://") {
132        return path;
133    }
134    if path.starts_with('@') && path.contains('/') {
135        return path;
136    }
137    let path_ref = std::path::Path::new(&path);
138    if !is_scss
139        && path.contains('/')
140        && path_ref
141            .extension()
142            .and_then(|e| e.to_str())
143            .is_some_and(is_style_extension)
144    {
145        return path;
146    }
147    let ext = std::path::Path::new(&path)
148        .extension()
149        .and_then(|e| e.to_str());
150    match ext {
151        Some(e) if is_style_extension(e) => format!("./{path}"),
152        _ => {
153            if is_scss && !path.contains(':') {
154                format!("./{path}")
155            } else {
156                path
157            }
158        }
159    }
160}
161
162fn is_style_extension(ext: &str) -> bool {
163    ext.eq_ignore_ascii_case("css")
164        || ext.eq_ignore_ascii_case("scss")
165        || ext.eq_ignore_ascii_case("sass")
166        || ext.eq_ignore_ascii_case("less")
167}
168
169/// Strip comments from CSS/SCSS source to avoid matching directives inside comments.
170#[cfg(test)]
171fn strip_css_comments(source: &str, is_scss: bool) -> String {
172    let stripped = CSS_COMMENT_RE.replace_all(source, "");
173    if is_scss {
174        SCSS_LINE_COMMENT_RE.replace_all(&stripped, "").into_owned()
175    } else {
176        stripped.into_owned()
177    }
178}
179
180fn mask_css_comments(source: &str, is_scss: bool) -> String {
181    let mut masked = mask_with_whitespace(source, &CSS_COMMENT_RE);
182    if is_scss {
183        masked = mask_with_whitespace(&masked, &SCSS_LINE_COMMENT_RE);
184    }
185    masked
186}
187
188/// Normalize a Tailwind CSS `@plugin` target.
189///
190/// Unlike SCSS `@use`, extensionless targets such as `daisyui` are package
191/// specifiers, not local partials. Keep bare specifiers bare and only preserve
192/// explicit relative/root-relative paths.
193fn normalize_css_plugin_path(path: String) -> String {
194    path
195}
196
197/// Extract `@import` / `@use` / `@forward` / `@plugin` source paths from a CSS/SCSS string.
198///
199/// Returns both the raw source and the normalized source. URL imports
200/// (`http://`, `https://`, `data:`) are skipped. Use [`extract_css_imports`]
201/// when only the normalized form is needed.
202///
203/// Regex-based by design: this path also handles the SCSS `@use` / `@forward`
204/// forms, which lightningcss does not parse, so unlike class extraction there is
205/// no parser-backed set to defer the membership decision to.
206#[must_use]
207pub fn extract_css_import_sources(source: &str, is_scss: bool) -> Vec<CssImportSource> {
208    let stripped = mask_css_comments(source, is_scss);
209    let mut out = Vec::new();
210
211    for cap in CSS_IMPORT_RE.captures_iter(&stripped) {
212        let raw = cap.get(1).or_else(|| cap.get(2)).or_else(|| cap.get(3));
213        if let Some(m) = raw {
214            let (src, span) = trimmed_match_with_span(m);
215            if !src.is_empty() && !is_css_url_import(&src) {
216                out.push(CssImportSource {
217                    normalized: normalize_css_import_path(src.clone(), is_scss),
218                    raw: src,
219                    is_plugin: false,
220                    span,
221                });
222            }
223        }
224    }
225
226    if is_scss {
227        for cap in SCSS_USE_RE.captures_iter(&stripped) {
228            if let Some(m) = cap.get(1) {
229                let (raw, span) = trimmed_match_with_span(m);
230                out.push(CssImportSource {
231                    normalized: normalize_css_import_path(raw.clone(), true),
232                    raw,
233                    is_plugin: false,
234                    span,
235                });
236            }
237        }
238    }
239
240    for cap in CSS_PLUGIN_RE.captures_iter(&stripped) {
241        if let Some(m) = cap.get(1) {
242            let (raw, span) = trimmed_match_with_span(m);
243            if !raw.is_empty() && !is_css_url_import(&raw) {
244                out.push(CssImportSource {
245                    normalized: normalize_css_plugin_path(raw.clone()),
246                    raw,
247                    is_plugin: true,
248                    span,
249                });
250            }
251        }
252    }
253
254    out
255}
256
257fn trimmed_match_with_span(m: regex::Match<'_>) -> (String, Span) {
258    let raw = m.as_str();
259    let trimmed_start = raw.len() - raw.trim_start().len();
260    let trimmed_end = raw.trim_end().len();
261    let start = m.start() + trimmed_start;
262    let end = m.start() + trimmed_end;
263    (raw.trim().to_string(), Span::new(start as u32, end as u32))
264}
265
266/// Extract normalized `@import` / `@use` / `@forward` / `@plugin` source paths from a CSS/SCSS string.
267///
268/// Returns specifiers normalized via `normalize_css_import_path`. URL imports
269/// (`http://`, `https://`, `data:`) are skipped. Used by callers that only need
270/// entry/dependency source paths; callers that need import kind information
271/// should use [`extract_css_import_sources`].
272#[must_use]
273pub fn extract_css_imports(source: &str, is_scss: bool) -> Vec<String> {
274    extract_css_import_sources(source, is_scss)
275        .into_iter()
276        .map(|source| source.normalized)
277        .collect()
278}
279
280/// Opening of a Tailwind v4 `@theme` block: `@theme`, optional modifier keywords
281/// (`inline` / `static` / `reference` / `default`), then the `{`. Matches up to
282/// and including the brace so the caller can brace-match the body from `end()`.
283static CSS_THEME_OPEN_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
284    crate::static_regex(r"@theme(?:\s+(?:inline|static|reference|default))*\s*\{")
285});
286
287/// A `var(--custom-property)` reference, capturing the dashed-ident name without
288/// the leading `--`. Used only to credit a theme token read by another theme
289/// token inside a `@theme` interior (lightningcss skips the unknown at-rule).
290static CSS_VAR_REF_RE: LazyLock<regex::Regex> =
291    LazyLock::new(|| crate::static_regex(r"var\(\s*--([A-Za-z0-9_-]+)"));
292
293/// A Tailwind v4 `@theme` token definition: the custom-property name WITHOUT the
294/// leading `--` (e.g. `color-brand`) and its 1-based line in the source.
295#[derive(Debug, Clone, PartialEq, Eq)]
296pub struct ThemeTokenDef {
297    /// The custom-property name with the `--` prefix stripped (`color-brand`).
298    pub name: String,
299    /// The normalized top-level declaration value, with internal whitespace
300    /// collapsed. Empty only when the value could not be recovered.
301    pub value: String,
302    /// 1-based line of the declaration in the original source.
303    pub line: u32,
304}
305
306/// Result of scanning a CSS source for Tailwind v4 `@theme` blocks.
307#[derive(Debug, Clone, Default, PartialEq, Eq)]
308pub struct ThemeScan {
309    /// Custom-property tokens DEFINED at the top level of a `@theme` block, with
310    /// the `*`-reset form (`--color-*: initial`) and bare-namespace declarations
311    /// excluded. Deduped by name (first definition wins for the line).
312    pub tokens: Vec<ThemeTokenDef>,
313    /// Custom-property names (without `--`) READ via `var()` anywhere inside a
314    /// `@theme` block interior, each paired with the 1-based source line of the
315    /// `var(` token. lightningcss does not descend into the unknown `@theme`
316    /// at-rule, so these reads are invisible to `CssAnalytics`; a token backing
317    /// another token (`--color-button: var(--color-brand)`) keeps the backing
318    /// token live.
319    pub theme_var_reads: Vec<(String, u32)>,
320}
321
322/// Scan a CSS source for Tailwind v4 `@theme` blocks, returning the defined
323/// design tokens plus the custom properties read via `var()` inside those blocks.
324///
325/// Tailwind v4 is CSS-first, so `@theme { --color-brand: #f00; }` is the unit of
326/// a user-authored design token. lightningcss treats `@theme` as an unknown
327/// at-rule and skips it, so this is a separate brace-matching pass (comments and
328/// strings masked first so braces / semicolons inside them never break the block
329/// boundary). Only top-level `--ident: value` declarations are tokens; declarations
330/// inside a nested block (e.g. `@keyframes` for `--animate-*`) are not.
331#[must_use]
332pub fn scan_theme_blocks(source: &str) -> ThemeScan {
333    // Fast path: skip the masking allocation for the common no-`@theme` file.
334    if !source.contains("@theme") {
335        return ThemeScan::default();
336    }
337    let masked = mask_theme_source(source);
338    let mut out = ThemeScan::default();
339    let mut seen: FxHashSet<String> = FxHashSet::default();
340    for open in CSS_THEME_OPEN_RE.find_iter(&masked) {
341        let body_start = open.end();
342        let body_end = find_theme_body_end(&masked, body_start);
343        collect_theme_declarations(
344            source,
345            &masked,
346            body_start,
347            body_end,
348            &mut out.tokens,
349            &mut seen,
350        );
351        collect_theme_var_reads(
352            source,
353            &masked,
354            body_start,
355            body_end,
356            &mut out.theme_var_reads,
357        );
358    }
359    out
360}
361
362/// Located regular-CSS `var(--token)` reads OUTSIDE any `@theme` block interior:
363/// `(name, line)` per read, with the `--` stripped from the name. `@theme`-
364/// interior reads are deliberately excluded here (they are located separately by
365/// [`scan_theme_blocks`] as the distinct `theme-var` surface), so the two read
366/// kinds never double-count. Comments / strings / `url()` are masked first, so a
367/// `var()` inside those regions is never matched.
368#[must_use]
369pub fn extract_css_var_reads_located(source: &str) -> Vec<(String, u32)> {
370    if !source.contains("var(") {
371        return Vec::new();
372    }
373    let masked = mask_theme_source(source);
374    // Byte ranges of every `@theme { ... }` interior, so reads inside them are
375    // skipped (they are the `theme-var` surface, located elsewhere).
376    let mut theme_bodies: Vec<(usize, usize)> = Vec::new();
377    if masked.contains("@theme") {
378        for open in CSS_THEME_OPEN_RE.find_iter(&masked) {
379            let body_start = open.end();
380            let body_end = find_theme_body_end(&masked, body_start);
381            theme_bodies.push((body_start, body_end));
382        }
383    }
384    let in_theme = |offset: usize| theme_bodies.iter().any(|&(s, e)| offset >= s && offset < e);
385    let mut out = Vec::new();
386    for cap in CSS_VAR_REF_RE.captures_iter(&masked) {
387        let (Some(whole), Some(name)) = (cap.get(0), cap.get(1)) else {
388            continue;
389        };
390        if in_theme(whole.start()) {
391            continue;
392        }
393        out.push((
394            name.as_str().to_owned(),
395            line_at_offset(source, whole.start()),
396        ));
397    }
398    out
399}
400
401/// Mask comments, strings, and `url(...)` while preserving byte offsets so
402/// braces inside those regions never affect `@theme` block matching.
403fn mask_theme_source(source: &str) -> String {
404    mask_with_whitespace(&mask_css_comments(source, false), &CSS_NON_SELECTOR_RE)
405}
406
407/// Brace-match from just after a `@theme {` opener to its partner.
408fn find_theme_body_end(masked: &str, body_start: usize) -> usize {
409    let bytes = masked.as_bytes();
410    let mut depth = 1usize;
411    let mut i = body_start;
412    while i < bytes.len() {
413        match bytes[i] {
414            b'{' => depth += 1,
415            b'}' => {
416                depth -= 1;
417                if depth == 0 {
418                    break;
419                }
420            }
421            _ => {}
422        }
423        i += 1;
424    }
425    i.min(bytes.len())
426}
427
428fn collect_theme_var_reads(
429    source: &str,
430    masked: &str,
431    body_start: usize,
432    body_end: usize,
433    out: &mut Vec<(String, u32)>,
434) {
435    let Some(body) = masked.get(body_start..body_end) else {
436        return;
437    };
438    for cap in CSS_VAR_REF_RE.captures_iter(body) {
439        let (Some(whole), Some(name)) = (cap.get(0), cap.get(1)) else {
440            continue;
441        };
442        // Absolute byte offset of the `var(` token start in the original source
443        // (masking preserves byte offsets 1:1).
444        let offset = body_start + whole.start();
445        out.push((name.as_str().to_owned(), line_at_offset(source, offset)));
446    }
447}
448
449/// 1-based line number of `offset` in `source`, counting `\n` up to (but not
450/// including) the byte at `offset`. Out-of-range offsets clamp to line 1.
451fn line_at_offset(source: &str, offset: usize) -> u32 {
452    let count = source
453        .get(..offset)
454        .map_or(0, |s| s.bytes().filter(|&b| b == b'\n').count());
455    u32::try_from(1 + count).unwrap_or(u32::MAX)
456}
457
458/// Walk a masked `@theme` body collecting top-level `--ident: value` declarations
459/// as tokens. Tracks brace depth so declarations inside a nested block (e.g. an
460/// `@keyframes` for `--animate-*`) are skipped, and statement position so only a
461/// `--ident` at a declaration start counts. The `*`-reset form (`--color-*`) is
462/// excluded because the `*` breaks the ident scan before the `:`.
463fn collect_theme_declarations(
464    source: &str,
465    masked: &str,
466    start: usize,
467    end: usize,
468    out: &mut Vec<ThemeTokenDef>,
469    seen: &mut FxHashSet<String>,
470) {
471    let bytes = masked.as_bytes();
472    let mut depth = 0usize;
473    let mut expect_decl = true;
474    let mut i = start;
475    while i < end {
476        let b = bytes[i];
477        match b {
478            b'{' => {
479                depth += 1;
480                expect_decl = false;
481                i += 1;
482            }
483            b'}' => {
484                depth = depth.saturating_sub(1);
485                if depth == 0 {
486                    expect_decl = true;
487                }
488                i += 1;
489            }
490            b';' => {
491                if depth == 0 {
492                    expect_decl = true;
493                }
494                i += 1;
495            }
496            _ if b.is_ascii_whitespace() => i += 1,
497            _ => {
498                if depth == 0 && expect_decl {
499                    expect_decl = false;
500                    i = scan_theme_declaration(
501                        &mut ThemeDeclarationScan {
502                            source,
503                            masked,
504                            end,
505                            out,
506                            seen,
507                        },
508                        b,
509                        i,
510                    );
511                } else {
512                    i += 1;
513                }
514            }
515        }
516    }
517}
518
519struct ThemeDeclarationScan<'a, 'b> {
520    source: &'a str,
521    masked: &'a str,
522    end: usize,
523    out: &'b mut Vec<ThemeTokenDef>,
524    seen: &'b mut FxHashSet<String>,
525}
526
527/// At a declaration start, harvest a `--ident:` custom-property name and return
528/// the cursor advanced past the scanned ident. Returns `i + 1` for any non-`--`
529/// declaration start.
530fn scan_theme_declaration(scan: &mut ThemeDeclarationScan<'_, '_>, b: u8, i: usize) -> usize {
531    let bytes = scan.masked.as_bytes();
532    if !(b == b'-' && bytes.get(i + 1) == Some(&b'-')) {
533        return i + 1;
534    }
535    let id_start = i;
536    let mut j = i;
537    while j < scan.end {
538        let c = bytes[j];
539        if c == b'-' || c == b'_' || c.is_ascii_alphanumeric() {
540            j += 1;
541        } else {
542            break;
543        }
544    }
545    let mut k = j;
546    while k < scan.end && bytes[k].is_ascii_whitespace() {
547        k += 1;
548    }
549    // Only a `--ident:` (no `*` before the colon) is a token.
550    if k < scan.end && bytes[k] == b':' {
551        let name = &scan.masked[id_start + 2..j];
552        if !name.is_empty() && scan.seen.insert(name.to_owned()) {
553            let value = theme_declaration_value(scan.source, scan.masked, k + 1, scan.end);
554            let line = 1 + scan
555                .source
556                .get(..id_start)
557                .map_or(0, |s| s.bytes().filter(|&x| x == b'\n').count());
558            scan.out.push(ThemeTokenDef {
559                name: name.to_owned(),
560                value,
561                line: u32::try_from(line).unwrap_or(u32::MAX),
562            });
563        }
564    }
565    j
566}
567
568fn theme_declaration_value(source: &str, masked: &str, start: usize, end: usize) -> String {
569    let bytes = masked.as_bytes();
570    let mut depth = 0usize;
571    let mut i = start;
572    while i < end {
573        match bytes[i] {
574            b'{' => depth += 1,
575            b'}' => {
576                if depth == 0 {
577                    break;
578                }
579                depth -= 1;
580            }
581            b';' if depth == 0 => break,
582            _ => {}
583        }
584        i += 1;
585    }
586    source
587        .get(start..i)
588        .unwrap_or_default()
589        .split_whitespace()
590        .collect::<Vec<_>>()
591        .join(" ")
592}
593
594/// Extract the utility tokens referenced in `@apply` directive bodies across a
595/// CSS source (comment / string masked). `@apply rounded-card font-bold;` yields
596/// `["rounded-card", "font-bold"]`. The leading-`!` and trailing-`!` important
597/// modifiers and a bare `!important` token are stripped, so a theme token whose
598/// utility is applied only via `@apply` is credited as used.
599#[must_use]
600pub fn extract_apply_tokens(source: &str) -> Vec<String> {
601    // Fast path: skip the masking allocation for the common no-`@apply` file.
602    if !source.contains("@apply") {
603        return Vec::new();
604    }
605    let masked = mask_with_whitespace(&mask_css_comments(source, false), &CSS_NON_SELECTOR_RE);
606    let mut out = Vec::new();
607    for m in CSS_APPLY_RE.find_iter(&masked) {
608        let body = m.as_str().trim_start_matches("@apply");
609        for token in body.split_whitespace() {
610            let token = token.trim_matches('!');
611            if token.is_empty() || token == "important" {
612                continue;
613            }
614            out.push(token.to_owned());
615        }
616    }
617    out
618}
619
620/// Like [`extract_apply_tokens`], but pairs each class-shaped token with the
621/// 1-based source line of its `@apply` directive. Used by the token-consumer
622/// reverse index to locate `@apply`-surface consumers; masking preserves byte
623/// offsets so the directive line is recoverable from the match start.
624#[must_use]
625pub fn extract_apply_tokens_located(source: &str) -> Vec<(String, u32)> {
626    if !source.contains("@apply") {
627        return Vec::new();
628    }
629    let masked = mask_with_whitespace(&mask_css_comments(source, false), &CSS_NON_SELECTOR_RE);
630    let mut out = Vec::new();
631    for m in CSS_APPLY_RE.find_iter(&masked) {
632        let line = line_at_offset(source, m.start());
633        let body = m.as_str().trim_start_matches("@apply");
634        for token in body.split_whitespace() {
635            let token = token.trim_matches('!');
636            if token.is_empty() || token == "important" {
637                continue;
638            }
639            out.push((token.to_owned(), line));
640        }
641    }
642    out
643}
644
645/// Mask every regex match in `src` with ASCII spaces (`0x20`) of equal byte
646/// length, so byte offsets in the returned string correspond 1:1 to byte
647/// offsets in the original.
648///
649/// Used to neutralise CSS comments, quoted strings, `url(...)`, and at-rule
650/// preludes before scanning for `.class` selectors, while preserving the
651/// original-source positions that callers need to populate `ExportInfo.span`
652/// (issue #549). The `regex` crate guarantees match boundaries respect UTF-8
653/// char boundaries, so the masked buffer is always valid UTF-8.
654fn mask_with_whitespace(src: &str, re: &regex::Regex) -> String {
655    let mut out = String::with_capacity(src.len());
656    let mut cursor = 0;
657    for m in re.find_iter(src) {
658        out.push_str(&src[cursor..m.start()]);
659        for _ in m.start()..m.end() {
660            out.push(' ');
661        }
662        cursor = m.end();
663    }
664    out.push_str(&src[cursor..]);
665    out
666}
667
668/// Collect the authoritative set of class-selector names from a CSS source by
669/// parsing it into a real AST (lightningcss). Returns `None` only on a
670/// catastrophic parse failure (Sass syntax that is not standard CSS), in which
671/// case the caller falls back to the regex scanner. With `error_recovery` on,
672/// individual malformed rules are recovered silently and contribute a partial
673/// set rather than triggering the fallback, so a broken rule drops only its own
674/// classes (a conservative miss) instead of returning `None`.
675///
676/// This is the source of truth for which `.token` occurrences are genuine class
677/// selectors. It natively excludes `@layer foo.bar` layer names, `@import ...
678/// layer(theme.button)` layer references, `@keyframes` step selectors, id and
679/// element selectors, and the contents of comments / strings / `url()`, which
680/// the older regex-only scanner had to approximate with a stack of masking
681/// passes. Classes nested inside `:is()` / `:where()` / `:not()` / `:has()` /
682/// `:any()` / `::slotted()` / `:host()` / `:nth-child(... of ...)` are
683/// collected too, matching the regex scanner's "every `.class` token" behavior.
684fn lightningcss_class_set(source: &str) -> Option<FxHashSet<String>> {
685    let options = ParserOptions {
686        // Recover from individual malformed rules so a single bad rule does not
687        // discard class names from the rest of the file.
688        error_recovery: true,
689        // These files are `.module.css` / `.module.scss`, so parse in CSS Modules
690        // mode. That makes the `:local()` / `:global()` pseudo-classes parse as
691        // real selectors rather than erroring, so classes wrapped in them are
692        // collected (matching the regex scanner). Renaming is a print-time
693        // concern, so the AST class names stay the original author-written names.
694        css_modules: Some(lightningcss::css_modules::Config::default()),
695        ..ParserOptions::default()
696    };
697    let stylesheet = StyleSheet::parse(source, options).ok()?;
698    let mut classes = FxHashSet::default();
699    collect_classes_from_rules(&stylesheet.rules.0, &mut classes);
700    Some(classes)
701}
702
703/// Recursively collect class-selector names from a list of CSS rules, descending
704/// into every grouping rule (`@media`, `@supports`, `@container`, `@layer {}`,
705/// `@document`, `@starting-style`, `@scope`, nested style rules) so a class
706/// declared anywhere contributes to the set.
707fn collect_classes_from_rules(rules: &[CssRule<'_>], classes: &mut FxHashSet<String>) {
708    for rule in rules {
709        match rule {
710            CssRule::Style(style) => {
711                collect_classes_from_selector_list(&style.selectors, classes);
712                collect_classes_from_rules(&style.rules.0, classes);
713            }
714            CssRule::Media(rule) => collect_classes_from_rules(&rule.rules.0, classes),
715            CssRule::Supports(rule) => collect_classes_from_rules(&rule.rules.0, classes),
716            CssRule::Container(rule) => collect_classes_from_rules(&rule.rules.0, classes),
717            CssRule::LayerBlock(rule) => collect_classes_from_rules(&rule.rules.0, classes),
718            CssRule::MozDocument(rule) => collect_classes_from_rules(&rule.rules.0, classes),
719            CssRule::StartingStyle(rule) => collect_classes_from_rules(&rule.rules.0, classes),
720            CssRule::Nesting(rule) => {
721                collect_classes_from_selector_list(&rule.style.selectors, classes);
722                collect_classes_from_rules(&rule.style.rules.0, classes);
723            }
724            CssRule::Scope(rule) => {
725                if let Some(scope_start) = &rule.scope_start {
726                    collect_classes_from_selector_list(scope_start, classes);
727                }
728                if let Some(scope_end) = &rule.scope_end {
729                    collect_classes_from_selector_list(scope_end, classes);
730                }
731                collect_classes_from_rules(&rule.rules.0, classes);
732            }
733            _ => {}
734        }
735    }
736}
737
738fn collect_classes_from_selector_list(list: &SelectorList<'_>, classes: &mut FxHashSet<String>) {
739    for selector in &list.0 {
740        collect_classes_from_selector(selector, classes);
741    }
742}
743
744fn collect_classes_from_selector(selector: &Selector<'_>, classes: &mut FxHashSet<String>) {
745    for component in selector.iter_raw_match_order() {
746        match component {
747            Component::Class(name) => {
748                classes.insert(name.0.to_string());
749            }
750            Component::Is(list)
751            | Component::Where(list)
752            | Component::Has(list)
753            | Component::Negation(list)
754            | Component::Any(_, list) => {
755                for nested in list.as_ref() {
756                    collect_classes_from_selector(nested, classes);
757                }
758            }
759            Component::Slotted(nested) | Component::Host(Some(nested)) => {
760                collect_classes_from_selector(nested, classes);
761            }
762            Component::NthOf(data) => {
763                for nested in data.selectors() {
764                    collect_classes_from_selector(nested, classes);
765                }
766            }
767            // CSS Modules `:local(.foo)` / `:global(.foo)` wrap a real selector.
768            Component::NonTSPseudoClass(
769                PseudoClass::Local { selector } | PseudoClass::Global { selector },
770            ) => collect_classes_from_selector(selector, classes),
771            _ => {}
772        }
773    }
774}
775
776/// Extract class names from a CSS module file as named exports.
777///
778/// For standard CSS, lightningcss parses the source into an AST and supplies the
779/// authoritative set of class-selector names; the byte-offset scanner then
780/// locates each name's [`Span`] in the ORIGINAL `source` (pointing at the bare
781/// class name, no leading dot) so downstream `compute_line_offsets` resolves the
782/// real declaration line and column instead of falling back to line:1 col:0
783/// (issue #549). For SCSS (Sass syntax lightningcss does not parse) and for any
784/// CSS that fails to parse outright, the regex-only scanner is used unchanged.
785pub fn extract_css_module_exports(source: &str, is_scss: bool) -> Vec<ExportInfo> {
786    if !is_scss && let Some(class_set) = lightningcss_class_set(source) {
787        return scan_css_module_exports(source, is_scss, Some(&class_set));
788    }
789    scan_css_module_exports(source, is_scss, None)
790}
791
792/// Scan `source` for `.class` tokens and emit one [`ExportInfo`] per distinct
793/// class (first occurrence wins), with a [`Span`] pointing at the post-dot
794/// identifier in the original source.
795///
796/// When `class_filter` is `Some`, only tokens present in the AST-derived set are
797/// emitted, so the parser owns the membership decision and the scanner owns only
798/// span location. When `class_filter` is `None` (SCSS / parse-failure fallback),
799/// the at-rule prelude is masked to keep `@layer foo.bar` / `@import ...
800/// layer(...)` segments from being mistaken for classes.
801fn scan_css_module_exports(
802    source: &str,
803    is_scss: bool,
804    class_filter: Option<&FxHashSet<String>>,
805) -> Vec<ExportInfo> {
806    let masked = mask_css_module_class_candidates(source, is_scss, class_filter.is_some());
807    let mut seen = FxHashSet::default();
808    let mut exports = Vec::new();
809    for cap in CSS_CLASS_RE.captures_iter(&masked) {
810        if let Some(m) = cap.get(1) {
811            push_css_class_export(m, class_filter, &mut seen, &mut exports);
812        }
813    }
814    exports
815}
816
817fn mask_css_module_class_candidates(source: &str, is_scss: bool, has_class_filter: bool) -> String {
818    let mut masked = mask_with_whitespace(source, &CSS_COMMENT_RE);
819    if is_scss {
820        masked = mask_with_whitespace(&masked, &SCSS_LINE_COMMENT_RE);
821    }
822    masked = mask_with_whitespace(&masked, &CSS_NON_SELECTOR_RE);
823    if !has_class_filter {
824        masked = mask_with_whitespace(&masked, &CSS_AT_RULE_PRELUDE_RE);
825    }
826    masked
827}
828
829fn push_css_class_export(
830    class_match: regex::Match<'_>,
831    class_filter: Option<&FxHashSet<String>>,
832    seen: &mut FxHashSet<String>,
833    exports: &mut Vec<ExportInfo>,
834) {
835    let class_name = class_match.as_str().to_string();
836    if class_filter.is_some_and(|filter| !filter.contains(&class_name)) {
837        return;
838    }
839    if seen.insert(class_name.clone()) {
840        exports.push(css_class_export(class_name, class_match));
841    }
842}
843
844fn css_class_export(class_name: String, class_match: regex::Match<'_>) -> ExportInfo {
845    #[expect(
846        clippy::cast_possible_truncation,
847        reason = "CSS files exceeding u32::MAX bytes are not a realistic input"
848    )]
849    let span = Span::new(class_match.start() as u32, class_match.end() as u32);
850    ExportInfo {
851        name: ExportName::Named(class_name),
852        local_name: None,
853        is_type_only: false,
854        visibility: VisibilityTag::None,
855        expected_unused_reason: None,
856        span,
857        members: Vec::new(),
858        is_side_effect_used: false,
859        super_class: None,
860    }
861}
862
863/// Build the import edges for a CSS/SCSS source: every `@import`/`@use`/etc.
864/// directive plus a synthetic `tailwindcss` side-effect import when `@apply` or
865/// `@tailwind` is present.
866fn build_css_imports(source: &str, stripped: &str, is_scss: bool) -> Vec<ImportInfo> {
867    let mut imports = Vec::new();
868
869    for css_source in extract_css_import_sources(source, is_scss) {
870        imports.push(ImportInfo {
871            source: css_source.normalized,
872            imported_name: if css_source.is_plugin {
873                ImportedName::Default
874            } else {
875                ImportedName::SideEffect
876            },
877            local_name: String::new(),
878            is_type_only: false,
879            from_style: false,
880            span: css_source.span,
881            source_span: css_source.span,
882        });
883    }
884
885    let has_apply = CSS_APPLY_RE.is_match(stripped);
886    let has_tailwind = CSS_TAILWIND_RE.is_match(stripped);
887    if has_apply || has_tailwind {
888        imports.push(ImportInfo {
889            source: "tailwindcss".to_string(),
890            imported_name: ImportedName::SideEffect,
891            local_name: String::new(),
892            is_type_only: false,
893            from_style: false,
894            span: Span::default(),
895            source_span: Span::default(),
896        });
897    }
898
899    imports
900}
901
902/// Parse a CSS/SCSS file, extracting @import, @use, @forward, @plugin, @apply, and @tailwind directives.
903pub(crate) fn parse_css_to_module(
904    file_id: FileId,
905    path: &Path,
906    source: &str,
907    content_hash: u64,
908) -> ModuleInfo {
909    let parsed_suppressions = crate::suppress::parse_suppressions_from_source(source);
910    let is_scss = path
911        .extension()
912        .and_then(|e| e.to_str())
913        .is_some_and(|ext| matches!(ext, "scss" | "sass" | "less"));
914
915    let stripped = mask_css_comments(source, is_scss);
916    let imports = build_css_imports(source, &stripped, is_scss);
917
918    let exports = if is_css_module_file(path) {
919        extract_css_module_exports(source, is_scss)
920    } else {
921        Vec::new()
922    };
923
924    css_module_info(
925        file_id,
926        content_hash,
927        source,
928        parsed_suppressions,
929        imports,
930        exports,
931    )
932}
933
934/// Assemble the `ModuleInfo` for a CSS/SCSS file: the import/export edges plus
935/// the line offsets and suppressions; all AST-derived fields stay empty since
936/// CSS carries no JS-level structure. Pure plumbing struct literal.
937fn css_module_info(
938    file_id: FileId,
939    content_hash: u64,
940    source: &str,
941    parsed_suppressions: crate::suppress::ParsedSuppressions,
942    imports: Vec<ImportInfo>,
943    exports: Vec<ExportInfo>,
944) -> ModuleInfo {
945    crate::module_info::non_js_module_info(
946        file_id,
947        content_hash,
948        source,
949        parsed_suppressions,
950        imports,
951        exports,
952    )
953}
954
955#[cfg(all(test, not(miri)))]
956mod tests {
957    use super::*;
958
959    /// Helper to collect export names as strings from `extract_css_module_exports`.
960    fn export_names(source: &str) -> Vec<String> {
961        extract_css_module_exports(source, false)
962            .into_iter()
963            .filter_map(|e| match e.name {
964                ExportName::Named(n) => Some(n),
965                ExportName::Default => None,
966            })
967            .collect()
968    }
969
970    #[test]
971    fn is_css_file_css() {
972        assert!(is_css_file(Path::new("styles.css")));
973    }
974
975    #[test]
976    fn is_css_file_scss() {
977        assert!(is_css_file(Path::new("styles.scss")));
978    }
979
980    #[test]
981    fn is_css_file_sass() {
982        assert!(is_css_file(Path::new("styles.sass")));
983    }
984
985    #[test]
986    fn is_css_file_less() {
987        assert!(is_css_file(Path::new("styles.less")));
988    }
989
990    #[test]
991    fn is_css_file_rejects_js() {
992        assert!(!is_css_file(Path::new("app.js")));
993    }
994
995    #[test]
996    fn is_css_file_rejects_ts() {
997        assert!(!is_css_file(Path::new("app.ts")));
998    }
999
1000    #[test]
1001    fn is_css_file_rejects_no_extension() {
1002        assert!(!is_css_file(Path::new("Makefile")));
1003    }
1004
1005    #[test]
1006    fn is_css_module_file_module_css() {
1007        assert!(is_css_module_file(Path::new("Component.module.css")));
1008    }
1009
1010    #[test]
1011    fn is_css_module_file_module_scss() {
1012        assert!(is_css_module_file(Path::new("Component.module.scss")));
1013    }
1014
1015    #[test]
1016    fn is_css_module_file_rejects_plain_css() {
1017        assert!(!is_css_module_file(Path::new("styles.css")));
1018    }
1019
1020    #[test]
1021    fn is_css_module_file_rejects_plain_scss() {
1022        assert!(!is_css_module_file(Path::new("styles.scss")));
1023    }
1024
1025    #[test]
1026    fn is_css_module_file_rejects_module_js() {
1027        assert!(!is_css_module_file(Path::new("utils.module.js")));
1028    }
1029
1030    #[test]
1031    fn extracts_single_class() {
1032        let names = export_names(".foo { color: red; }");
1033        assert_eq!(names, vec!["foo"]);
1034    }
1035
1036    #[test]
1037    fn extracts_multiple_classes() {
1038        let names = export_names(".foo { } .bar { }");
1039        assert_eq!(names, vec!["foo", "bar"]);
1040    }
1041
1042    #[test]
1043    fn extracts_nested_classes() {
1044        let names = export_names(".foo .bar { color: red; }");
1045        assert!(names.contains(&"foo".to_string()));
1046        assert!(names.contains(&"bar".to_string()));
1047    }
1048
1049    #[test]
1050    fn extracts_hyphenated_class() {
1051        let names = export_names(".my-class { }");
1052        assert_eq!(names, vec!["my-class"]);
1053    }
1054
1055    #[test]
1056    fn extracts_camel_case_class() {
1057        let names = export_names(".myClass { }");
1058        assert_eq!(names, vec!["myClass"]);
1059    }
1060
1061    #[test]
1062    fn extracts_class_inside_global_pseudo() {
1063        // CSS Modules `:global(.foo)` must surface `foo`: the parser understands
1064        // the wrapped selector, which the regex scanner could not on its own.
1065        let names = export_names(":global(.globalClass) { color: red; }");
1066        assert_eq!(names, vec!["globalClass"]);
1067    }
1068
1069    #[test]
1070    fn extracts_class_inside_local_pseudo() {
1071        let names = export_names(":local(.localClass) { color: red; }");
1072        assert_eq!(names, vec!["localClass"]);
1073    }
1074
1075    #[test]
1076    fn extracts_classes_inside_negation() {
1077        let names = export_names(".btn:not(.disabled) { }");
1078        assert!(names.contains(&"btn".to_string()), "got {names:?}");
1079        assert!(names.contains(&"disabled".to_string()), "got {names:?}");
1080    }
1081
1082    #[test]
1083    fn extracts_classes_inside_is_and_where() {
1084        let names = export_names(":is(.a, .b) :where(.c) { }");
1085        for expected in ["a", "b", "c"] {
1086            assert!(
1087                names.contains(&expected.to_string()),
1088                "missing {expected} in {names:?}"
1089            );
1090        }
1091    }
1092
1093    #[test]
1094    fn extracts_underscore_class() {
1095        let names = export_names("._hidden { } .__wrapper { }");
1096        assert!(names.contains(&"_hidden".to_string()));
1097        assert!(names.contains(&"__wrapper".to_string()));
1098    }
1099
1100    #[test]
1101    fn pseudo_selector_hover() {
1102        let names = export_names(".foo:hover { color: blue; }");
1103        assert_eq!(names, vec!["foo"]);
1104    }
1105
1106    #[test]
1107    fn pseudo_selector_focus() {
1108        let names = export_names(".input:focus { outline: none; }");
1109        assert_eq!(names, vec!["input"]);
1110    }
1111
1112    #[test]
1113    fn pseudo_element_before() {
1114        let names = export_names(".icon::before { content: ''; }");
1115        assert_eq!(names, vec!["icon"]);
1116    }
1117
1118    #[test]
1119    fn combined_pseudo_selectors() {
1120        let names = export_names(".btn:hover, .btn:active, .btn:focus { }");
1121        assert_eq!(names, vec!["btn"]);
1122    }
1123
1124    #[test]
1125    fn classes_inside_media_query() {
1126        let names = export_names(
1127            "@media (max-width: 768px) { .mobile-nav { display: block; } .desktop-nav { display: none; } }",
1128        );
1129        assert!(names.contains(&"mobile-nav".to_string()));
1130        assert!(names.contains(&"desktop-nav".to_string()));
1131    }
1132
1133    #[test]
1134    fn classes_inside_multi_line_media_query() {
1135        let names =
1136            export_names("@media\n  screen and (min-width: 600px)\n{\n  .real { color: red; }\n}");
1137        assert_eq!(names, vec!["real"]);
1138    }
1139
1140    #[test]
1141    fn at_layer_statement_does_not_export() {
1142        let names = export_names("@layer foo.bar;");
1143        assert!(names.is_empty(), "got {names:?}");
1144        let names = export_names("@layer foo.bar, foo.baz;");
1145        assert!(names.is_empty(), "got {names:?}");
1146    }
1147
1148    #[test]
1149    fn at_layer_block_keeps_body_classes() {
1150        let names = export_names("@layer foo.bar { .root { color: red; } }");
1151        assert_eq!(names, vec!["root"]);
1152    }
1153
1154    #[test]
1155    fn at_layer_multiline_prelude_keeps_body_classes() {
1156        let names = export_names("@layer\n  foo.bar\n{ .root { color: red; } }");
1157        assert_eq!(names, vec!["root"]);
1158    }
1159
1160    #[test]
1161    fn at_layer_with_nested_media_keeps_body() {
1162        let names =
1163            export_names("@layer foo.bar { @media (max-width: 768px) { .real { color: red; } } }");
1164        assert_eq!(names, vec!["real"]);
1165    }
1166
1167    #[test]
1168    fn at_import_with_layer_attribute_does_not_export() {
1169        let names = export_names(r#"@import url("x.css") layer(theme.button);"#);
1170        assert!(names.is_empty(), "got {names:?}");
1171    }
1172
1173    #[test]
1174    fn class_then_at_layer_does_not_leak_prelude() {
1175        let names =
1176            export_names(".outer { color: blue; } @layer foo.bar { .inner { color: red; } }");
1177        assert_eq!(names, vec!["outer", "inner"]);
1178    }
1179
1180    #[test]
1181    fn at_scope_keeps_selector_list_classes() {
1182        let names = export_names("@scope (.parent) to (.child) { .title { color: red; } }");
1183        assert!(names.contains(&"parent".to_string()), "got {names:?}");
1184        assert!(names.contains(&"child".to_string()), "got {names:?}");
1185        assert!(names.contains(&"title".to_string()), "got {names:?}");
1186    }
1187
1188    #[test]
1189    fn at_keyframes_numeric_step_is_not_class() {
1190        let names = export_names(
1191            "@keyframes slide { 0% { transform: scale(.5); } 100% { transform: scale(1); } }",
1192        );
1193        assert!(names.is_empty(), "got {names:?}");
1194    }
1195
1196    #[test]
1197    fn at_webkit_keyframes_keeps_body_classes() {
1198        let names = export_names("@-webkit-keyframes slide { 0% { } 100% { } } .real { }");
1199        assert_eq!(names, vec!["real"]);
1200    }
1201
1202    #[test]
1203    fn deduplicates_repeated_class() {
1204        let names = export_names(".btn { color: red; } .btn { font-size: 14px; }");
1205        assert_eq!(names.iter().filter(|n| *n == "btn").count(), 1);
1206    }
1207
1208    #[test]
1209    fn empty_source() {
1210        let names = export_names("");
1211        assert!(names.is_empty());
1212    }
1213
1214    #[test]
1215    fn no_classes() {
1216        let names = export_names("body { margin: 0; } * { box-sizing: border-box; }");
1217        assert!(names.is_empty());
1218    }
1219
1220    #[test]
1221    fn ignores_classes_in_block_comments() {
1222        let names = export_names("/* .fake { } */ .real { }");
1223        assert!(!names.contains(&"fake".to_string()));
1224        assert!(names.contains(&"real".to_string()));
1225    }
1226
1227    #[test]
1228    fn ignores_classes_in_scss_line_comments() {
1229        let exports = extract_css_module_exports("// .fake\n.real { }", true);
1230        let names: Vec<_> = exports
1231            .iter()
1232            .filter_map(|e| match &e.name {
1233                ExportName::Named(n) => Some(n.as_str()),
1234                ExportName::Default => None,
1235            })
1236            .collect();
1237        assert_eq!(names, vec!["real"]);
1238    }
1239
1240    #[test]
1241    fn ignores_classes_in_strings() {
1242        let names = export_names(r#".real { content: ".fake"; }"#);
1243        assert!(names.contains(&"real".to_string()));
1244        assert!(!names.contains(&"fake".to_string()));
1245    }
1246
1247    #[test]
1248    fn ignores_classes_in_url() {
1249        let names = export_names(".real { background: url(./images/hero.png); }");
1250        assert!(names.contains(&"real".to_string()));
1251        assert!(!names.contains(&"png".to_string()));
1252    }
1253
1254    #[test]
1255    fn strip_css_block_comment() {
1256        let result = strip_css_comments("/* removed */ .kept { }", false);
1257        assert!(!result.contains("removed"));
1258        assert!(result.contains(".kept"));
1259    }
1260
1261    #[test]
1262    fn strip_scss_line_comment() {
1263        let result = strip_css_comments("// removed\n.kept { }", true);
1264        assert!(!result.contains("removed"));
1265        assert!(result.contains(".kept"));
1266    }
1267
1268    #[test]
1269    fn strip_scss_preserves_css_outside_comments() {
1270        let source = "// line comment\n/* block comment */\n.visible { color: red; }";
1271        let result = strip_css_comments(source, true);
1272        assert!(result.contains(".visible"));
1273    }
1274
1275    #[test]
1276    fn url_import_http() {
1277        assert!(is_css_url_import("http://example.com/style.css"));
1278    }
1279
1280    #[test]
1281    fn url_import_https() {
1282        assert!(is_css_url_import("https://fonts.googleapis.com/css"));
1283    }
1284
1285    #[test]
1286    fn url_import_data() {
1287        assert!(is_css_url_import("data:text/css;base64,abc"));
1288    }
1289
1290    #[test]
1291    fn url_import_local_not_skipped() {
1292        assert!(!is_css_url_import("./local.css"));
1293    }
1294
1295    #[test]
1296    fn url_import_bare_specifier_not_skipped() {
1297        assert!(!is_css_url_import("tailwindcss"));
1298    }
1299
1300    #[test]
1301    fn normalize_relative_dot_path_unchanged() {
1302        assert_eq!(
1303            normalize_css_import_path("./reset.css".to_string(), false),
1304            "./reset.css"
1305        );
1306    }
1307
1308    #[test]
1309    fn normalize_parent_relative_path_unchanged() {
1310        assert_eq!(
1311            normalize_css_import_path("../shared.scss".to_string(), false),
1312            "../shared.scss"
1313        );
1314    }
1315
1316    #[test]
1317    fn normalize_absolute_path_unchanged() {
1318        assert_eq!(
1319            normalize_css_import_path("/styles/main.css".to_string(), false),
1320            "/styles/main.css"
1321        );
1322    }
1323
1324    #[test]
1325    fn normalize_url_unchanged() {
1326        assert_eq!(
1327            normalize_css_import_path("https://example.com/style.css".to_string(), false),
1328            "https://example.com/style.css"
1329        );
1330    }
1331
1332    #[test]
1333    fn normalize_bare_css_gets_dot_slash() {
1334        assert_eq!(
1335            normalize_css_import_path("app.css".to_string(), false),
1336            "./app.css"
1337        );
1338    }
1339
1340    #[test]
1341    fn normalize_css_package_subpath_stays_bare() {
1342        assert_eq!(
1343            normalize_css_import_path("tailwindcss/theme.css".to_string(), false),
1344            "tailwindcss/theme.css"
1345        );
1346    }
1347
1348    #[test]
1349    fn normalize_css_package_subpath_with_dotted_name_stays_bare() {
1350        assert_eq!(
1351            normalize_css_import_path("highlight.js/styles/github.css".to_string(), false),
1352            "highlight.js/styles/github.css"
1353        );
1354    }
1355
1356    #[test]
1357    fn normalize_bare_scss_gets_dot_slash() {
1358        assert_eq!(
1359            normalize_css_import_path("vars.scss".to_string(), false),
1360            "./vars.scss"
1361        );
1362    }
1363
1364    #[test]
1365    fn normalize_bare_sass_gets_dot_slash() {
1366        assert_eq!(
1367            normalize_css_import_path("main.sass".to_string(), false),
1368            "./main.sass"
1369        );
1370    }
1371
1372    #[test]
1373    fn normalize_bare_less_gets_dot_slash() {
1374        assert_eq!(
1375            normalize_css_import_path("theme.less".to_string(), false),
1376            "./theme.less"
1377        );
1378    }
1379
1380    #[test]
1381    fn normalize_bare_js_extension_stays_bare() {
1382        assert_eq!(
1383            normalize_css_import_path("module.js".to_string(), false),
1384            "module.js"
1385        );
1386    }
1387
1388    #[test]
1389    fn normalize_scss_bare_partial_gets_dot_slash() {
1390        assert_eq!(
1391            normalize_css_import_path("variables".to_string(), true),
1392            "./variables"
1393        );
1394    }
1395
1396    #[test]
1397    fn normalize_scss_bare_partial_with_subdir_gets_dot_slash() {
1398        assert_eq!(
1399            normalize_css_import_path("base/reset".to_string(), true),
1400            "./base/reset"
1401        );
1402    }
1403
1404    #[test]
1405    fn normalize_scss_builtin_stays_bare() {
1406        assert_eq!(
1407            normalize_css_import_path("sass:math".to_string(), true),
1408            "sass:math"
1409        );
1410    }
1411
1412    #[test]
1413    fn normalize_scss_relative_path_unchanged() {
1414        assert_eq!(
1415            normalize_css_import_path("../styles/variables".to_string(), true),
1416            "../styles/variables"
1417        );
1418    }
1419
1420    #[test]
1421    fn normalize_css_bare_extensionless_stays_bare() {
1422        assert_eq!(
1423            normalize_css_import_path("tailwindcss".to_string(), false),
1424            "tailwindcss"
1425        );
1426    }
1427
1428    #[test]
1429    fn normalize_scoped_package_with_css_extension_stays_bare() {
1430        assert_eq!(
1431            normalize_css_import_path("@fontsource/monaspace-neon/400.css".to_string(), false),
1432            "@fontsource/monaspace-neon/400.css"
1433        );
1434    }
1435
1436    #[test]
1437    fn normalize_scoped_package_with_scss_extension_stays_bare() {
1438        assert_eq!(
1439            normalize_css_import_path("@company/design-system/tokens.scss".to_string(), true),
1440            "@company/design-system/tokens.scss"
1441        );
1442    }
1443
1444    #[test]
1445    fn normalize_scoped_package_without_extension_stays_bare() {
1446        assert_eq!(
1447            normalize_css_import_path("@fallow/design-system/styles".to_string(), false),
1448            "@fallow/design-system/styles"
1449        );
1450    }
1451
1452    #[test]
1453    fn normalize_scoped_package_extensionless_scss_stays_bare() {
1454        assert_eq!(
1455            normalize_css_import_path("@company/tokens".to_string(), true),
1456            "@company/tokens"
1457        );
1458    }
1459
1460    #[test]
1461    fn normalize_path_alias_with_css_extension_stays_bare() {
1462        assert_eq!(
1463            normalize_css_import_path("@/components/Button.css".to_string(), false),
1464            "@/components/Button.css"
1465        );
1466    }
1467
1468    #[test]
1469    fn normalize_path_alias_extensionless_stays_bare() {
1470        assert_eq!(
1471            normalize_css_import_path("@/styles/variables".to_string(), false),
1472            "@/styles/variables"
1473        );
1474    }
1475
1476    #[test]
1477    fn strip_css_no_comments() {
1478        let source = ".foo { color: red; }";
1479        assert_eq!(strip_css_comments(source, false), source);
1480    }
1481
1482    #[test]
1483    fn strip_css_multiple_block_comments() {
1484        let source = "/* comment-one */ .foo { } /* comment-two */ .bar { }";
1485        let result = strip_css_comments(source, false);
1486        assert!(!result.contains("comment-one"));
1487        assert!(!result.contains("comment-two"));
1488        assert!(result.contains(".foo"));
1489        assert!(result.contains(".bar"));
1490    }
1491
1492    #[test]
1493    fn strip_scss_does_not_affect_non_scss() {
1494        let source = "// this stays\n.foo { }";
1495        let result = strip_css_comments(source, false);
1496        assert!(result.contains("// this stays"));
1497    }
1498
1499    #[test]
1500    fn css_module_parses_suppressions() {
1501        let info = parse_css_to_module(
1502            fallow_types::discover::FileId(0),
1503            Path::new("Component.module.css"),
1504            "/* fallow-ignore-file */\n.btn { color: red; }",
1505            0,
1506        );
1507        assert!(!info.suppressions.is_empty());
1508        assert_eq!(info.suppressions[0].line, 0);
1509    }
1510
1511    #[test]
1512    fn extracts_class_starting_with_underscore() {
1513        let names = export_names("._private { } .__dunder { }");
1514        assert!(names.contains(&"_private".to_string()));
1515        assert!(names.contains(&"__dunder".to_string()));
1516    }
1517
1518    #[test]
1519    fn ignores_id_selectors() {
1520        let names = export_names("#myId { color: red; }");
1521        assert!(!names.contains(&"myId".to_string()));
1522    }
1523
1524    #[test]
1525    fn ignores_element_selectors() {
1526        let names = export_names("div { color: red; } span { }");
1527        assert!(names.is_empty());
1528    }
1529
1530    #[test]
1531    fn extract_css_imports_at_import_quoted() {
1532        let imports = extract_css_imports(r#"@import "./reset.css";"#, false);
1533        assert_eq!(imports, vec!["./reset.css"]);
1534    }
1535
1536    #[test]
1537    fn extract_css_imports_package_subpath_stays_bare() {
1538        let imports =
1539            extract_css_imports(r#"@import "tailwindcss/theme.css" layer(theme);"#, false);
1540        assert_eq!(imports, vec!["tailwindcss/theme.css"]);
1541    }
1542
1543    #[test]
1544    fn extract_css_imports_at_import_url() {
1545        let imports = extract_css_imports(r#"@import url("./reset.css");"#, false);
1546        assert_eq!(imports, vec!["./reset.css"]);
1547    }
1548
1549    #[test]
1550    fn extract_css_imports_skips_remote_urls() {
1551        let imports =
1552            extract_css_imports(r#"@import "https://fonts.example.com/font.css";"#, false);
1553        assert!(imports.is_empty());
1554    }
1555
1556    #[test]
1557    fn extract_css_imports_scss_use_normalizes_partial() {
1558        let imports = extract_css_imports(r#"@use "variables";"#, true);
1559        assert_eq!(imports, vec!["./variables"]);
1560    }
1561
1562    #[test]
1563    fn extract_css_imports_scss_forward_normalizes_partial() {
1564        let imports = extract_css_imports(r#"@forward "tokens";"#, true);
1565        assert_eq!(imports, vec!["./tokens"]);
1566    }
1567
1568    #[test]
1569    fn extract_css_imports_skips_comments() {
1570        let imports = extract_css_imports(
1571            r#"/* @import "./hidden.scss"; */
1572@use "real";"#,
1573            true,
1574        );
1575        assert_eq!(imports, vec!["./real"]);
1576    }
1577
1578    #[test]
1579    fn extract_css_imports_at_plugin_keeps_package_bare() {
1580        let imports = extract_css_imports(r#"@plugin "daisyui";"#, true);
1581        assert_eq!(imports, vec!["daisyui"]);
1582    }
1583
1584    #[test]
1585    fn extract_css_imports_at_plugin_tracks_relative_file() {
1586        let imports = extract_css_imports(r#"@plugin "./tailwind-plugin.js";"#, false);
1587        assert_eq!(imports, vec!["./tailwind-plugin.js"]);
1588    }
1589
1590    #[test]
1591    fn extract_css_imports_scss_at_import_kept_relative() {
1592        let imports = extract_css_imports(r"@import 'Foo';", true);
1593        assert_eq!(imports, vec!["./Foo"]);
1594    }
1595
1596    #[test]
1597    fn extract_css_imports_additional_data_string_body() {
1598        let body = r#"@use "./src/styles/global.scss";"#;
1599        let imports = extract_css_imports(body, true);
1600        assert_eq!(imports, vec!["./src/styles/global.scss"]);
1601    }
1602
1603    #[test]
1604    fn mask_with_whitespace_preserves_byte_length() {
1605        let src = "/* hello */ .foo { }";
1606        let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
1607        assert_eq!(masked.len(), src.len());
1608        assert!(masked.is_char_boundary(src.len()));
1609    }
1610
1611    #[test]
1612    fn mask_with_whitespace_preserves_offsets_around_multibyte() {
1613        let src = "/* \u{2713} */ .foo { }";
1614        let foo_offset = src.find(".foo").expect("`.foo` present");
1615        let masked = mask_with_whitespace(src, &CSS_COMMENT_RE);
1616        assert_eq!(masked.len(), src.len());
1617        assert_eq!(masked.find(".foo"), Some(foo_offset));
1618    }
1619
1620    /// Resolve a span's start to (line, col) using the same primitives the
1621    /// downstream pipeline uses in `crates/core/src/analyze/unused_exports.rs`.
1622    fn span_line_col(source: &str, start: u32) -> (u32, u32) {
1623        let offsets = fallow_types::extract::compute_line_offsets(source);
1624        fallow_types::extract::byte_offset_to_line_col(&offsets, start)
1625    }
1626
1627    #[test]
1628    fn span_points_at_real_class_declaration_line() {
1629        let source = "\n\n\n\n.foo { color: red; }\n";
1630        let exports = extract_css_module_exports(source, false);
1631        assert_eq!(exports.len(), 1);
1632        let span = exports[0].span;
1633        let (line, col) = span_line_col(source, span.start);
1634        assert_eq!(line, 5, "`.foo` on line 5 must produce line 5, not line 1");
1635        assert_eq!(
1636            col, 1,
1637            "column points at `f` in `.foo` (post-dot identifier)"
1638        );
1639        assert_eq!(
1640            &source[span.start as usize..span.end as usize],
1641            "foo",
1642            "span range must slice to the class identifier in the original source"
1643        );
1644    }
1645
1646    #[test]
1647    fn span_survives_multibyte_comment_prefix() {
1648        let source = "/* \u{2713} */\n.foo { }";
1649        let exports = extract_css_module_exports(source, false);
1650        assert_eq!(exports.len(), 1);
1651        let span = exports[0].span;
1652        assert!(
1653            source.is_char_boundary(span.start as usize),
1654            "span.start must lie on a UTF-8 char boundary"
1655        );
1656        assert_eq!(&source[span.start as usize..span.end as usize], "foo");
1657    }
1658
1659    #[test]
1660    fn span_skips_at_layer_prelude_dot_segments() {
1661        let source = "@layer foo.bar { }\n.root { }\n";
1662        let exports = extract_css_module_exports(source, false);
1663        let names: Vec<_> = exports
1664            .iter()
1665            .filter_map(|e| match &e.name {
1666                ExportName::Named(n) => Some(n.as_str()),
1667                ExportName::Default => None,
1668            })
1669            .collect();
1670        assert_eq!(names, vec!["root"], "@layer sub-segments must not export");
1671        let span = exports[0].span;
1672        let (line, _col) = span_line_col(source, span.start);
1673        assert_eq!(line, 2, "`.root` lives on line 2 of the original source");
1674        assert_eq!(&source[span.start as usize..span.end as usize], "root");
1675    }
1676
1677    #[test]
1678    fn span_skips_classes_in_strings() {
1679        let source = ".real { content: \".fake\"; }\n.also-real { }\n";
1680        let exports = extract_css_module_exports(source, false);
1681        let names: Vec<_> = exports
1682            .iter()
1683            .filter_map(|e| match &e.name {
1684                ExportName::Named(n) => Some(n.as_str()),
1685                ExportName::Default => None,
1686            })
1687            .collect();
1688        assert_eq!(names, vec!["real", "also-real"]);
1689        for export in &exports {
1690            let span = export.span;
1691            let slice = &source[span.start as usize..span.end as usize];
1692            match &export.name {
1693                ExportName::Named(n) => assert_eq!(slice, n.as_str()),
1694                ExportName::Default => unreachable!("CSS modules emit only named exports"),
1695            }
1696        }
1697    }
1698
1699    #[test]
1700    fn span_deduplicates_to_first_occurrence() {
1701        let source = ".btn { color: red; }\n.btn { color: blue; }\n";
1702        let exports = extract_css_module_exports(source, false);
1703        assert_eq!(exports.len(), 1);
1704        let (line, _col) = span_line_col(source, exports[0].span.start);
1705        assert_eq!(
1706            line, 1,
1707            "first occurrence wins for deduplicated class names"
1708        );
1709    }
1710
1711    #[test]
1712    fn span_inside_media_query() {
1713        let source =
1714            "@media (max-width: 768px) {\n  .mobile { display: block; }\n  .desktop { }\n}\n";
1715        let exports = extract_css_module_exports(source, false);
1716        let by_name: rustc_hash::FxHashMap<&str, oxc_span::Span> = exports
1717            .iter()
1718            .filter_map(|e| match &e.name {
1719                ExportName::Named(n) => Some((n.as_str(), e.span)),
1720                ExportName::Default => None,
1721            })
1722            .collect();
1723        let mobile_line = span_line_col(source, by_name["mobile"].start).0;
1724        let desktop_line = span_line_col(source, by_name["desktop"].start).0;
1725        assert_eq!(mobile_line, 2);
1726        assert_eq!(desktop_line, 3);
1727    }
1728
1729    #[test]
1730    fn at_layer_only_module_emits_no_exports() {
1731        let exports = extract_css_module_exports("@layer foo.bar, foo.baz;\n", false);
1732        assert!(exports.is_empty());
1733    }
1734
1735    #[test]
1736    fn parse_css_to_module_resolves_real_line_offsets() {
1737        let source = "\n\n\n\n.foo { color: red; }\n";
1738        let info = parse_css_to_module(
1739            fallow_types::discover::FileId(0),
1740            Path::new("Component.module.css"),
1741            source,
1742            0,
1743        );
1744        assert_eq!(info.exports.len(), 1);
1745        let (line, _col) = fallow_types::extract::byte_offset_to_line_col(
1746            &info.line_offsets,
1747            info.exports[0].span.start,
1748        );
1749        assert_eq!(line, 5, "downstream line must equal the source line");
1750    }
1751
1752    fn theme_token_names(source: &str) -> Vec<String> {
1753        scan_theme_blocks(source)
1754            .tokens
1755            .into_iter()
1756            .map(|t| t.name)
1757            .collect()
1758    }
1759
1760    #[test]
1761    fn theme_single_block_collects_tokens() {
1762        let names = theme_token_names("@theme { --color-brand: #f00; --radius-card: 8px; }");
1763        assert_eq!(names, vec!["color-brand", "radius-card"]);
1764    }
1765
1766    #[test]
1767    fn theme_token_values_are_normalized() {
1768        let scan = scan_theme_blocks("@theme {\n  --color-brand: rgb( 255 0 0 );\n}");
1769        assert_eq!(scan.tokens[0].name, "color-brand");
1770        assert_eq!(scan.tokens[0].value, "rgb( 255 0 0 )");
1771    }
1772
1773    #[test]
1774    fn theme_dashed_multi_segment_names() {
1775        let names = theme_token_names(
1776            "@theme {\n  --font-weight-heavy: 900;\n  --inset-shadow-glow: 0 0 4px red;\n}",
1777        );
1778        assert_eq!(names, vec!["font-weight-heavy", "inset-shadow-glow"]);
1779    }
1780
1781    #[test]
1782    fn theme_inline_and_static_modifiers() {
1783        assert_eq!(
1784            theme_token_names("@theme inline { --color-a: red; }"),
1785            vec!["color-a"]
1786        );
1787        assert_eq!(
1788            theme_token_names("@theme static { --color-b: red; }"),
1789            vec!["color-b"]
1790        );
1791    }
1792
1793    #[test]
1794    fn theme_multiple_blocks_union() {
1795        let names = theme_token_names(
1796            "@theme { --color-a: red; }\n.x { color: blue; }\n@theme { --spacing-gutter: 1rem; }",
1797        );
1798        assert_eq!(names, vec!["color-a", "spacing-gutter"]);
1799    }
1800
1801    #[test]
1802    fn theme_reset_form_excluded() {
1803        // `--color-*: initial` is a namespace reset directive, not a token.
1804        let names = theme_token_names("@theme { --color-*: initial; --color-brand: red; }");
1805        assert_eq!(names, vec!["color-brand"]);
1806    }
1807
1808    #[test]
1809    fn theme_no_block_yields_nothing() {
1810        assert!(theme_token_names(".x { --color-brand: red; }").is_empty());
1811    }
1812
1813    #[test]
1814    fn theme_line_numbers() {
1815        let scan = scan_theme_blocks("@theme {\n  --color-a: red;\n  --radius-b: 4px;\n}");
1816        assert_eq!(scan.tokens[0].line, 2);
1817        assert_eq!(scan.tokens[1].line, 3);
1818    }
1819
1820    #[test]
1821    fn theme_token_backs_token_via_var() {
1822        let scan = scan_theme_blocks(
1823            "@theme {\n  --color-brand: #f00;\n  --color-button: var(--color-brand);\n}",
1824        );
1825        assert!(
1826            scan.theme_var_reads
1827                .iter()
1828                .any(|(name, _)| name == "color-brand")
1829        );
1830    }
1831
1832    #[test]
1833    fn theme_var_read_carries_line() {
1834        // The `var(--color-brand)` read sits on line 3 of the source; the located
1835        // theme-var read must carry that 1-based line for the reverse index.
1836        let scan = scan_theme_blocks(
1837            "@theme {\n  --color-brand: #f00;\n  --color-button: var(--color-brand);\n}",
1838        );
1839        assert_eq!(
1840            scan.theme_var_reads,
1841            vec![("color-brand".to_string(), 3u32)]
1842        );
1843    }
1844
1845    #[test]
1846    fn css_var_reads_locate_outside_theme_and_exclude_interior() {
1847        // A regular-CSS `var(--color-brand)` read is located (css-var surface);
1848        // a read inside the `@theme` interior is the distinct theme-var surface
1849        // and MUST be excluded here so the two kinds never double-count.
1850        let source = "@theme {\n  --color-brand: #f00;\n  --color-button: var(--color-brand);\n}\n\n.btn {\n  color: var(--color-brand);\n}\n";
1851        assert_eq!(
1852            extract_css_var_reads_located(source),
1853            vec![("color-brand".to_string(), 7u32)],
1854            "only the .btn read (line 7) is a css-var; the @theme-interior read is excluded"
1855        );
1856
1857        // A source whose only `var()` read is inside `@theme` yields no css-var.
1858        assert!(
1859            extract_css_var_reads_located("@theme {\n  --a: #fff;\n  --b: var(--a);\n}",)
1860                .is_empty(),
1861            "a @theme-interior-only var() read is not a css-var consumer"
1862        );
1863    }
1864
1865    #[test]
1866    fn theme_string_braces_do_not_truncate_block() {
1867        let scan = scan_theme_blocks(
1868            "@theme {\n  --font-label: \"}\";\n  --color-brand: #f00;\n  --color-button: var(--color-brand);\n}",
1869        );
1870        assert_eq!(
1871            scan.tokens
1872                .iter()
1873                .map(|token| token.name.as_str())
1874                .collect::<Vec<_>>(),
1875            vec!["font-label", "color-brand", "color-button"]
1876        );
1877        assert!(
1878            scan.theme_var_reads
1879                .iter()
1880                .any(|(name, _)| name == "color-brand")
1881        );
1882    }
1883
1884    #[test]
1885    fn theme_nested_keyframes_body_not_collected() {
1886        // `@keyframes` inside `@theme` (for `--animate-*`) must not surface its
1887        // step selectors or interior as theme tokens.
1888        let names = theme_token_names(
1889            "@theme {\n  --animate-spin: spin 1s linear infinite;\n  @keyframes spin { from { --x: 0; } to { --y: 1; } }\n}",
1890        );
1891        assert_eq!(names, vec!["animate-spin"]);
1892    }
1893
1894    #[test]
1895    fn theme_comment_block_ignored() {
1896        let names = theme_token_names("/* @theme { --color-fake: red; } */ .x { color: blue; }");
1897        assert!(names.is_empty(), "got {names:?}");
1898    }
1899
1900    #[test]
1901    fn theme_deduplicates_repeated_token() {
1902        let names = theme_token_names("@theme { --color-a: red; --color-a: blue; }");
1903        assert_eq!(names, vec!["color-a"]);
1904    }
1905
1906    #[test]
1907    fn apply_tokens_basic() {
1908        let tokens = extract_apply_tokens(".panel { @apply rounded-card font-bold; }");
1909        assert_eq!(tokens, vec!["rounded-card", "font-bold"]);
1910    }
1911
1912    #[test]
1913    fn apply_tokens_strips_important() {
1914        let tokens = extract_apply_tokens(".x { @apply text-brand! font-bold !important; }");
1915        assert_eq!(tokens, vec!["text-brand", "font-bold"]);
1916    }
1917
1918    #[test]
1919    fn apply_tokens_ignored_in_comments() {
1920        let tokens = extract_apply_tokens("/* @apply hidden-token; */ .x { color: red; }");
1921        assert!(tokens.is_empty(), "got {tokens:?}");
1922    }
1923}