diff --git a/crates/oxc_sourcemap/src/decode.rs b/crates/oxc_sourcemap/src/decode.rs index e66db3d60f26f..86e1f766019f4 100644 --- a/crates/oxc_sourcemap/src/decode.rs +++ b/crates/oxc_sourcemap/src/decode.rs @@ -7,20 +7,25 @@ use crate::{SourceMap, Token}; #[serde(rename_all = "camelCase")] struct JSONSourceMap { file: Option, - mappings: String, - sources: Vec, - sources_content: Option>, - names: Vec, + mappings: Option, + sources: Option>>, + sources_content: Option>>, + names: Option>, } pub fn decode(value: &str) -> Result { let json: JSONSourceMap = serde_json::from_str(value)?; let file = json.file.map(Into::into); - let names = json.names.into_iter().map(Into::into).collect::>(); - let sources = json.sources.into_iter().map(Into::into).collect::>(); - let source_contents = - json.sources_content.map(|v| v.into_iter().map(Into::into).collect::>()); - let tokens = decode_mapping(&json.mappings, names.len(), sources.len())?; + let names = + json.names.map(|v| v.into_iter().map(Into::into).collect::>()).unwrap_or_default(); + let sources = json + .sources + .map(|v| v.into_iter().map(Option::unwrap_or_default).map(Into::into).collect::>()) + .unwrap_or_default(); + let source_contents = json + .sources_content + .map(|v| v.into_iter().map(Option::unwrap_or_default).map(Into::into).collect::>()); + let tokens = decode_mapping(&json.mappings.unwrap_or_default(), names.len(), sources.len())?; Ok(SourceMap::new(file, names, sources, source_contents, tokens, None)) } @@ -141,3 +146,12 @@ fn test_decode_sourcemap() { assert_eq!(iter.next().unwrap().to_tuple(), (Some("coolstuff.js"), 2, 2, Some("alert"))); assert!(iter.next().is_none()); } + +#[test] +fn test_decode_sourcemap_optional_filed() { + let input = r#"{ + "sources": [null], + "sourcesContent": [null] + }"#; + SourceMap::from_json_string(input).expect("should success"); +}