{"record":{"id":"08ea31251f14ae84","repo":"swc-project/swc","slug":"expected-fr-unit","errorCode":null,"errorMessage":"Expected 'fr' unit","messagePattern":"Expected 'fr' unit","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/swc_css_parser/src/parser/values_and_units/mod.rs","lineNumber":2275,"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_flex_unit(&unit) {\n                    return Err(Error::new(span, ErrorKind::Expected(\"'fr' unit\")));\n                }\n\n                let unit_len = raw_unit.len() as u32;\n\n                Ok(Flex {\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":2257,"sourceCodeEnd":2293,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_css_parser/src/parser/values_and_units/mod.rs#L2257-L2293","documentation":"Thrown by Parse<Flex> in swc_css_parser when the token is a dimension but its unit is not the flex unit. After bumping Token::Dimension it checks is_flex_unit(&unit), which accepts only fr (ASCII case-insensitive). A track like '2fx', '1fraction', or '1f' fails with this error, the span covering the whole '1fx' token.","triggerScenarios":"Grid track values such as 'grid-template-columns: repeat(3, 1fx)', '2fraction', or a suffix constant typo'd as 'rf'. Any dimension in a typed flex position whose unit is not exactly fr triggers it.","commonSituations":"Typos in the fr suffix, DSL/config code that spells the unit in full ('fraction'), and copy-paste from tutorials using pseudo-syntax.","solutions":["Use exactly fr: '1fx' -> '1fr'.","Fix the suffix constant in the code that generates tracks (fraction->fr).","Whitelist the track grammar before parsing if tracks come from external input."],"exampleFix":"/* before */\na { grid-template-columns: 1fx 2fx; }\n\n/* after */\na { grid-template-columns: 1fr 2fr; }","handlingStrategy":"validation","validationCode":"function isValidFlexUnit(v: string): boolean {\n  return /^[+-]?(\\d+\\.?\\d*|\\.\\d+)fr$/i.test(v.trim());\n}","typeGuard":"fn valid_flex_unit(unit: &str) -> bool {\n    unit.eq_ignore_ascii_case(\"fr\")\n}","tryCatchPattern":"use swc_css_parser::error::ErrorKind;\nif let Err(e) = parse_input::<Stylesheet>(src, opts) {\n    if matches!(e.kind(), ErrorKind::Expected(\"'fr' unit\")) {\n        return Err(format!(\"bad flex unit at {:?}\", e.span()).into());\n    }\n    return Err(e.into());\n}","preventionTips":["Use the exact suffix 'fr'; do not accept 'fraction' from config.","Centralize the track-unit constant where grid CSS is generated.","Fixture-test repeat() and mixed track lists for unit typos."],"tags":["css","swc","parser","flex","grid","units"],"backgroundTag":"css-invalid-flex-unit","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}