swc-project/swc · error · Error

Expected 'Hz' or 'kHz' units

Error message

Expected 'Hz' or 'kHz' units

What it means

Thrown by Parse<Frequency> in swc_css_parser when the token is a dimension but its unit is not a frequency unit. After bumping Token::Dimension it checks is_frequency_unit(&unit), which accepts only hz and khz (ASCII case-insensitive, so 'Hz'/'kHZ' are fine). Units like 'mhz', 'khz '.trim failures, or misapplied units (s, db) produce this error covering the whole dimension token span.

Source

Thrown at crates/swc_css_parser/src/parser/values_and_units/mod.rs:2172

        if !is!(self, Dimension) {
            return Err(Error::new(span, ErrorKind::Expected("dimension token")));
        }

        match bump!(self) {
            Token::Dimension {
                dimension: dimension_token,
            } => {
                let DimensionToken {
                    value,
                    unit,
                    raw_value,
                    raw_unit,
                    ..
                } = *dimension_token;

                if !is_frequency_unit(&unit) {
                    return Err(Error::new(span, ErrorKind::Expected("'Hz' or 'kHz' units")));
                }

                let unit_len = raw_unit.len() as u32;

                Ok(Frequency {
                    span,
                    value: Number {
                        span: Span::new(span.lo, span.hi - BytePos(unit_len)),
                        value,
                        raw: Some(raw_value),
                    },
                    unit: Ident {
                        span: Span::new(span.hi - BytePos(unit_len), span.hi),
                        value: unit,
                        raw: Some(raw_unit),
                    },
                })
            }

View on GitHub (pinned to 5176682b65)

Solutions

  1. Use only hz or khz: '440mhz' -> '440hz' is not a semantic fix, so convert the value if you really meant megahertz (440MHz is out of CSS range; emit '440000khz' if a value is required).
  2. Fix typos: '2k' -> '2khz'.
  3. Whitelist-validate units to exactly hz/khz before emitting CSS.

Example fix

/* before */
a { pitch: 2k; }

/* after */
a { pitch: 2khz; }
Defensive patterns

Strategy: validation

Validate before calling

const FREQ_UNITS = new Set(["hz", "khz"]);
function hasValidFrequencyUnit(v: string): boolean {
  const i = [...v].findIndex(c => /[a-zA-Z]/.test(c));
  return i > 0 && FREQ_UNITS.has(v.slice(i).toLowerCase());
}

Type guard

fn valid_frequency_unit(unit: &str) -> bool {
    matches!(unit.to_ascii_lowercase().as_str(), "hz" | "khz")
}

Try / catch

use swc_css_parser::error::ErrorKind;
if let Err(e) = parse_input::<Stylesheet>(src, opts) {
    if matches!(e.kind(), ErrorKind::Expected("'Hz' or 'kHz' units")) {
        return Err(format!("bad frequency unit at {:?}", e.span()).into());
    }
    return Err(e.into());
}

Prevention

When it happens

Trigger: Values like '440mhz', '2k' (missing hz), '440sec', or a unit built by string concatenation that yields 'k' + 'Hz' variants outside the two allowed strings.

Common situations: Unit-typos in audio CSS, code translating physical units (MHz, GHz) that have no CSS equivalent, and configuration maps passing through unsupported frequency units verbatim.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/793a0b96fc6eb4c9. Report an issue: GitHub.