css_class_extractor.ts

AST-based CSS class extraction for Svelte and TypeScript files.

Replaces regex-based extraction with proper parsing to handle: - class="display:flex" - string attributes - class={{ active, disabled: !enabled }} - object attributes (Svelte 5.16+) - class={[cond && 'box', 'display:flex']} - array attributes (Svelte 5.16+) - class:active={cond} - class directives - clsx('foo', { bar: true }) - class utility function calls - Variables with class-related names - // @fuz-classes class1 class2 - comment hints for dynamic classes

Declarations
#

8 declarations

view source

AcornPlugin
#

extract_css_classes
#

css_class_extractor.ts view source

(source: string, options?: ExtractCssClassesOptions): Set<string>

Unified extraction function that auto-detects file type. Returns just the class names as a Set.

source

- The file source code

type string

options

- Extraction options

default {}

returns

Set<string>

Set of class names

extract_css_classes_with_locations
#

css_class_extractor.ts view source

(source: string, options?: ExtractCssClassesOptions): ExtractionResult

Unified extraction function that auto-detects file type. Returns full extraction result with locations and diagnostics.

source

- The file source code

type string

options

- Extraction options

default {}

returns

ExtractionResult

Full extraction result with classes, tracked variables, and diagnostics

extract_from_svelte
#

css_class_extractor.ts view source

(source: string, file?: string): ExtractionResult

Extracts CSS classes from a Svelte file using AST parsing.

source

- The Svelte file source code

type string

file

- File path for location tracking

type string
default '<unknown>'

returns

ExtractionResult

Extraction result with classes, tracked variables, and diagnostics

extract_from_ts
#

css_class_extractor.ts view source

(source: string, file?: string, acorn_plugins?: AcornPlugin[] | undefined): ExtractionResult

Extracts CSS classes from a TypeScript/JS file using AST parsing.

source

- The TS/JS file source code

type string

file

- File path for location tracking

type string
default '<unknown>'

acorn_plugins?

- Additional acorn plugins (e.g., acorn-jsx for React)

type AcornPlugin[] | undefined
optional

returns

ExtractionResult

Extraction result with classes, tracked variables, and diagnostics

ExtractCssClassesOptions
#

css_class_extractor.ts view source

ExtractCssClassesOptions

Options for CSS class extraction.

filename

File path used to determine extraction method (Svelte vs TS) and for location tracking in diagnostics.

type string

acorn_plugins

Additional acorn plugins to use when parsing TS/JS files. Useful for adding JSX support via acorn-jsx for React projects.

type Array<AcornPlugin>

ExtractionResult
#

css_class_extractor.ts view source

ExtractionResult

Extraction result with classes mapped to their source locations. Uses null instead of empty collections to avoid allocation overhead.

Uses embedded diagnostics (rather than a Result type) because file extraction can partially succeed: some classes may be extracted while others produce errors. This differs from which uses a discriminated union because single-class parsing is binary success/failure.

classes

Map from class name to locations where it was used, or null if none. Keys = unique classes, values = locations for diagnostics/IDE integration.

type Map<string, Array<SourceLocation>> | null

explicit_classes

Classes explicitly annotated via @fuz-classes comments, or null if none. These should produce warnings if they can't be resolved during generation.

type Set<string> | null

tracked_vars

Variables that were used in class contexts, or null if none

type Set<string> | null

diagnostics

Diagnostics from the extraction phase, or null if none

type Array<ExtractionDiagnostic> | null

SourceIndex
#

css_class_extractor.ts view source

Helper class for converting character offsets to line/column positions. Svelte template nodes (Comment, Text, ExpressionTag) only have char offsets, so this class enables efficient conversion.

Build: O(n) where n = source length Lookup: O(log m) where m = number of lines (binary search)

line_starts

type Array<number>

private

constructor

type new (source: string): SourceIndex

source
type string

get_location

Converts a character offset to a source location.

type (offset: number, file: string): SourceLocation

offset

- 0-based character offset in the source

type number
file

- File path for the location

type string

SourceLocation with 1-based line and column

Imported by
#