Skip to content

Commit

Permalink
Update, but probably not going to do this.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jan 31, 2024
1 parent d736a34 commit 87a0f97
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
28 changes: 21 additions & 7 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,8 @@ pub struct JsonModule {
pub specifier: ModuleSpecifier,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub maybe_cache_info: Option<CacheInfo>,
#[serde(rename = "size", serialize_with = "serialize_text")]
pub text: Arc<str>,
#[serde(rename = "size", serialize_with = "serialize_source")]
pub source: Arc<str>,
// 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,
Expand All @@ -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()
}
}

Expand Down Expand Up @@ -829,7 +829,21 @@ impl EsModuleSource {
text_encoding::arc_bytes_to_text(bytes).map(EsModuleSource::Text)
}

pub fn text(&self) -> Option<&Arc<str>> {
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<str>> {
match self {
Self::Bytes(_) => None,
Self::Text(text) => Some(text),
Expand Down Expand Up @@ -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(),
}));
Expand Down Expand Up @@ -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),
}
Expand Down Expand Up @@ -4344,7 +4358,7 @@ where
serializer.serialize_u32(source.len() as u32)
}

fn serialize_text<S>(
fn serialize_source<S>(
source: &Arc<str>,
serializer: S,
) -> Result<S::Ok, S::Error>
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";"#,
Expand Down
4 changes: 2 additions & 2 deletions src/symbols/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
})
Expand Down

0 comments on commit 87a0f97

Please sign in to comment.