{"record":{"id":"793a0b96fc6eb4c9","repo":"swc-project/swc","slug":"expected-hz-or-khz-units","errorCode":null,"errorMessage":"Expected 'Hz' or 'kHz' units","messagePattern":"Expected 'Hz' or 'kHz' units","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/swc_css_parser/src/parser/values_and_units/mod.rs","lineNumber":2172,"sourceCode":"\n        if !is!(self, Dimension) {\n            return Err(Error::new(span, ErrorKind::Expected(\"dimension token\")));\n        }\n\n        match bump!(self) {\n            Token::Dimension {\n                dimension: dimension_token,\n            } => {\n                let DimensionToken {\n                    value,\n                    unit,\n                    raw_value,\n                    raw_unit,\n                    ..\n                } = *dimension_token;\n\n                if !is_frequency_unit(&unit) {\n                    return Err(Error::new(span, ErrorKind::Expected(\"'Hz' or 'kHz' units\")));\n                }\n\n                let unit_len = raw_unit.len() as u32;\n\n                Ok(Frequency {\n                    span,\n                    value: Number {\n                        span: Span::new(span.lo, span.hi - BytePos(unit_len)),\n                        value,\n                        raw: Some(raw_value),\n                    },\n                    unit: Ident {\n                        span: Span::new(span.hi - BytePos(unit_len), span.hi),\n                        value: unit,\n                        raw: Some(raw_unit),\n                    },\n                })\n            }","sourceCodeStart":2154,"sourceCodeEnd":2190,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_css_parser/src/parser/values_and_units/mod.rs#L2154-L2190","documentation":"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.","triggerScenarios":"Values like '440mhz', '2k' (missing hz), '440sec', or a unit built by string concatenation that yields 'k' + 'Hz' variants outside the two allowed strings.","commonSituations":"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.","solutions":["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).","Fix typos: '2k' -> '2khz'.","Whitelist-validate units to exactly hz/khz before emitting CSS."],"exampleFix":"/* before */\na { pitch: 2k; }\n\n/* after */\na { pitch: 2khz; }","handlingStrategy":"validation","validationCode":"const FREQ_UNITS = new Set([\"hz\", \"khz\"]);\nfunction hasValidFrequencyUnit(v: string): boolean {\n  const i = [...v].findIndex(c => /[a-zA-Z]/.test(c));\n  return i > 0 && FREQ_UNITS.has(v.slice(i).toLowerCase());\n}","typeGuard":"fn valid_frequency_unit(unit: &str) -> bool {\n    matches!(unit.to_ascii_lowercase().as_str(), \"hz\" | \"khz\")\n}","tryCatchPattern":"use swc_css_parser::error::ErrorKind;\nif let Err(e) = parse_input::<Stylesheet>(src, opts) {\n    if matches!(e.kind(), ErrorKind::Expected(\"'Hz' or 'kHz' units\")) {\n        return Err(format!(\"bad frequency unit at {:?}\", e.span()).into());\n    }\n    return Err(e.into());\n}","preventionTips":["Convert physical units (MHz+) before emitting; CSS only defines hz/khz.","Whitelist units at config load time and reject unknown ones early.","Add parser fixture tests for hz, Hz, khz, kHZ casings."],"tags":["css","swc","parser","frequency","units"],"backgroundTag":"css-invalid-frequency-unit","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}