From 87a0f97fb9360abe373658557156d9ea70be0823 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 31 Jan 2024 09:36:03 -0500 Subject: [PATCH] Update, but probably not going to do this. --- src/graph.rs | 28 +++++++++++++++++++++------- src/lib.rs | 2 +- src/symbols/analyzer.rs | 4 ++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/graph.rs b/src/graph.rs index c20f39704..5aaa68d5e 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -789,8 +789,8 @@ pub struct JsonModule { pub specifier: ModuleSpecifier, #[serde(flatten, skip_serializing_if = "Option::is_none")] pub maybe_cache_info: Option, - #[serde(rename = "size", serialize_with = "serialize_text")] - pub text: Arc, + #[serde(rename = "size", serialize_with = "serialize_source")] + pub source: Arc, // todo(#240): This will always be MediaType::Json, but it's currently // used in the --json output. It's redundant though. pub media_type: MediaType, @@ -799,7 +799,7 @@ pub struct JsonModule { impl JsonModule { /// Return the size in bytes of the content of the JSON module. pub fn size(&self) -> usize { - self.text.as_bytes().len() + self.source.as_bytes().len() } } @@ -829,7 +829,21 @@ impl EsModuleSource { text_encoding::arc_bytes_to_text(bytes).map(EsModuleSource::Text) } - pub fn text(&self) -> Option<&Arc> { + pub fn as_bytes(&self) -> &[u8] { + match self { + Self::Bytes(bytes) => bytes, + Self::Text(text) => text.as_bytes(), + } + } + + pub fn bytes(&self) -> Arc<[u8]> { + match self { + Self::Bytes(bytes) => bytes.clone(), + Self::Text(text) => Arc::from(text.clone()), + } + } + + pub fn maybe_text(&self) -> Option<&Arc> { match self { Self::Bytes(_) => None, Self::Text(text) => Some(text), @@ -1865,7 +1879,7 @@ pub(crate) fn parse_module( })?; return Ok(Module::Json(JsonModule { maybe_cache_info: None, - text, + source: text, media_type: MediaType::Json, specifier: specifier.clone(), })); @@ -3196,7 +3210,7 @@ impl<'a, 'graph> Builder<'a, 'graph> { Module::Json(module) => { match new_source_with_text(&module.specifier, content) { Ok(source) => { - module.text = source; + module.source = source; } Err(err) => *slot = ModuleSlot::Err(err), } @@ -4344,7 +4358,7 @@ where serializer.serialize_u32(source.len() as u32) } -fn serialize_text( +fn serialize_source( source: &Arc, serializer: S, ) -> Result diff --git a/src/lib.rs b/src/lib.rs index f7d79b0fc..8fc77b6c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1769,7 +1769,7 @@ export function a(a) { assert_eq!(graph.module_slots.len(), 3); let data_specifier = ModuleSpecifier::parse("data:application/typescript,export%20*%20from%20%22https://example.com/c.ts%22;").unwrap(); let module = graph.get(&data_specifier).unwrap().esm().unwrap(); - let source = module.source.text().clone().unwrap(); + let source = module.source.maybe_text().unwrap(); assert_eq!( source.as_ref(), r#"export * from "https://example.com/c.ts";"#, diff --git a/src/symbols/analyzer.rs b/src/symbols/analyzer.rs index 209b188c9..8033bb528 100644 --- a/src/symbols/analyzer.rs +++ b/src/symbols/analyzer.rs @@ -184,7 +184,7 @@ impl<'a> RootSymbol<'a> { let specifier = &json_module.specifier; // it's not ideal having to use SourceTextInfo here, but it makes // it easier to interop with ParsedSource - let source_text_info = SourceTextInfo::new(json_module.text.clone()); + let source_text_info = SourceTextInfo::new(json_module.source.clone()); let range = source_text_info.range(); let module_id = ModuleId(self.ids_to_modules.len() as u32); let decls = { @@ -242,7 +242,7 @@ impl<'a> RootSymbol<'a> { .parser .parse_module(ParseOptions { specifier: &graph_module.specifier, - source: graph_module.source.text()?.clone(), + source: graph_module.source.maybe_text()?.clone(), media_type: graph_module.media_type, scope_analysis: true, })