Skip to content

Commit

Permalink
Switch to just storing Arc<str>
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jan 31, 2024
1 parent 22da8ea commit 95264e5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/fast_check/range_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ impl<'a> PublicRangeFinder<'a> {
return true; // just analyze it
};
match module {
crate::Module::Script(m) => is_typed_media_type(m.media_type),
crate::Module::Js(m) => is_typed_media_type(m.media_type),
crate::Module::Json(_) => true,
crate::Module::Npm(_)
| crate::Module::Node(_)
Expand Down
75 changes: 28 additions & 47 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ pub struct WorkspaceMember {
pub enum Module {
// todo(#239): remove this when updating the --json output for 2.0
#[serde(rename = "esm")]
Script(ScriptModule),
Js(JsModule),
// todo(#239): remove this when updating the --json output for 2.0
#[serde(rename = "asserted")]
Json(JsonModule),
Expand All @@ -708,7 +708,7 @@ pub enum Module {
impl Module {
pub fn specifier(&self) -> &ModuleSpecifier {
match self {
Module::Script(module) => &module.specifier,
Module::Js(module) => &module.specifier,
Module::Json(module) => &module.specifier,
Module::Npm(module) => &module.specifier,
Module::Node(module) => &module.specifier,
Expand All @@ -724,8 +724,8 @@ impl Module {
}
}

pub fn esm(&self) -> Option<&ScriptModule> {
if let Module::Script(module) = &self {
pub fn js(&self) -> Option<&JsModule> {
if let Module::Js(module) = &self {
Some(module)
} else {
None
Expand Down Expand Up @@ -820,7 +820,7 @@ pub struct FastCheckTypeModule {

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ScriptModule {
pub struct JsModule {
#[serde(
skip_serializing_if = "IndexMap::is_empty",
serialize_with = "serialize_dependencies"
Expand All @@ -839,7 +839,7 @@ pub struct ScriptModule {
pub fast_check: Option<FastCheckTypeModuleSlot>,
}

impl ScriptModule {
impl JsModule {
fn new(specifier: ModuleSpecifier, source: Arc<str>) -> Self {
Self {
dependencies: Default::default(),
Expand Down Expand Up @@ -1093,7 +1093,7 @@ impl<'a> Iterator for ModuleEntryIterator<'a> {
fn next(&mut self) -> Option<Self::Item> {
match self.previous_module.take() {
Some(ModuleEntryRef::Module(module)) => match module {
Module::Script(module) => {
Module::Js(module) => {
let check_types = (self.check_js
|| !matches!(
module.media_type,
Expand Down Expand Up @@ -1199,7 +1199,7 @@ impl<'a> ModuleGraphErrorIterator<'a> {

fn check_resolution(
&self,
module: &ScriptModule,
module: &JsModule,
mode: ResolutionMode,
specifier_text: &str,
resolution: &Resolution,
Expand Down Expand Up @@ -1276,7 +1276,7 @@ impl<'a> Iterator for ModuleGraphErrorIterator<'a> {

if let Some((_, module_entry)) = self.iterator.next() {
match module_entry {
ModuleEntryRef::Module(Module::Script(module)) => {
ModuleEntryRef::Module(Module::Js(module)) => {
let check_types = (check_js
|| !matches!(
module.media_type,
Expand Down Expand Up @@ -1581,7 +1581,7 @@ impl ModuleGraph {
prefer_types: bool,
) -> Option<ModuleSpecifier> {
match referring_module {
Module::Script(referring_module) => {
Module::Js(referring_module) => {
let dependency = referring_module.dependencies.get(specifier)?;
self.resolve_dependency_from_dep(dependency, prefer_types)
}
Expand Down Expand Up @@ -1609,7 +1609,7 @@ impl ModuleGraph {
// Even if we resolved the specifier, it doesn't mean the module is actually
// there, so check in the module slots
match self.module_slots.get(&resolved_specifier) {
Some(ModuleSlot::Module(Module::Script(module))) if prefer_types => {
Some(ModuleSlot::Module(Module::Js(module))) if prefer_types => {
// check for if this module has a types dependency
if let Some(Resolution::Ok(resolved)) = module
.maybe_types_dependency
Expand Down Expand Up @@ -1674,7 +1674,7 @@ impl ModuleGraph {
return Ok(None);
};

if let Some(specifier) = module.esm().and_then(|m| {
if let Some(specifier) = module.js().and_then(|m| {
m.maybe_types_dependency
.as_ref()
.and_then(|d| d.dependency.ok())
Expand Down Expand Up @@ -1879,7 +1879,7 @@ pub(crate) fn parse_module(
match module_analyzer.analyze(specifier, source.clone(), media_type) {
Ok(module_info) => {
// Return the module as a valid module
Ok(Module::Script(parse_es_module_from_module_info(
Ok(Module::Js(parse_js_module_from_module_info(
graph_kind,
specifier,
media_type,
Expand All @@ -1905,7 +1905,7 @@ pub(crate) fn parse_module(
) {
Ok(module_info) => {
// Return the module as a valid module
Ok(Module::Script(parse_es_module_from_module_info(
Ok(Module::Js(parse_js_module_from_module_info(
graph_kind,
specifier,
media_type,
Expand Down Expand Up @@ -1935,7 +1935,7 @@ pub(crate) fn parse_module(
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn parse_es_module_from_module_info(
pub(crate) fn parse_js_module_from_module_info(
graph_kind: GraphKind,
specifier: &ModuleSpecifier,
media_type: MediaType,
Expand All @@ -1945,8 +1945,8 @@ pub(crate) fn parse_es_module_from_module_info(
file_system: &dyn FileSystem,
maybe_resolver: Option<&dyn Resolver>,
maybe_npm_resolver: Option<&dyn NpmResolver>,
) -> ScriptModule {
let mut module = ScriptModule::new(specifier.clone(), source);
) -> JsModule {
let mut module = JsModule::new(specifier.clone(), source);
module.media_type = media_type;

// Analyze the TypeScript triple-slash references
Expand Down Expand Up @@ -3141,32 +3141,14 @@ impl<'a, 'graph> Builder<'a, 'graph> {
match slot {
ModuleSlot::Module(module) => {
match module {
Module::Script(module) => match module.media_type {
MediaType::JavaScript
| MediaType::Jsx
| MediaType::Mjs
| MediaType::Cjs
| MediaType::TypeScript
| MediaType::Mts
| MediaType::Cts
| MediaType::Dts
| MediaType::Dmts
| MediaType::Dcts
| MediaType::Tsx
| MediaType::Json => {
match new_source_with_text(&module.specifier, content)
{
Ok(source) => {
module.source = source;
}
Err(err) => *slot = ModuleSlot::Err(err),
Module::Js(module) => {
match new_source_with_text(&module.specifier, content) {
Ok(source) => {
module.source = source;
}
Err(err) => *slot = ModuleSlot::Err(err),
}
MediaType::Wasm
| MediaType::TsBuildInfo
| MediaType::SourceMap
| MediaType::Unknown => todo!(),
},
}
Module::Json(module) => {
match new_source_with_text(&module.specifier, content) {
Ok(source) => {
Expand Down Expand Up @@ -3236,7 +3218,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
module.maybe_cache_info =
self.loader.get_cache_info(&module.specifier);
}
Module::Script(module) => {
Module::Js(module) => {
module.maybe_cache_info =
self.loader.get_cache_info(&module.specifier);
}
Expand Down Expand Up @@ -3841,8 +3823,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
Err(err) => ModuleSlot::Err(err),
};

if let ModuleSlot::Module(Module::Script(module)) = module_slot.borrow_mut()
{
if let ModuleSlot::Module(Module::Js(module)) = module_slot.borrow_mut() {
if matches!(self.graph.graph_kind, GraphKind::All | GraphKind::CodeOnly)
|| module.maybe_types_dependency.is_none()
{
Expand Down Expand Up @@ -3973,7 +3954,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
let module_slot = self.graph.module_slots.get_mut(&specifier).unwrap();
let module = match module_slot {
ModuleSlot::Module(m) => match m {
Module::Script(m) => m,
Module::Js(m) => m,
_ => continue,
},
ModuleSlot::Err(_) | ModuleSlot::Pending => continue,
Expand Down Expand Up @@ -4385,7 +4366,7 @@ mod tests {
None,
)
.unwrap();
let module = module.esm().unwrap();
let module = module.js().unwrap();
assert_eq!(module.dependencies.len(), 1);
let dependency = module.dependencies.first().unwrap().1;
assert_eq!(
Expand Down Expand Up @@ -4915,7 +4896,7 @@ mod tests {
.await;
graph.valid().unwrap();
let module = graph.get(&Url::parse("file:///foo.ts").unwrap()).unwrap();
let module = module.esm().unwrap();
let module = module.js().unwrap();
let dependency_a = module.dependencies.get("file:///bar.ts").unwrap();
let dependency_b = module.dependencies.get("file:///baz.json").unwrap();
assert_eq!(
Expand Down
26 changes: 12 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub use graph::FastCheckTypeModule;
pub use graph::FastCheckTypeModuleSlot;
pub use graph::GraphImport;
pub use graph::GraphKind;
pub use graph::JsModule;
pub use graph::JsonModule;
pub use graph::Module;
pub use graph::ModuleEntryRef;
Expand All @@ -66,7 +67,6 @@ pub use graph::Range;
pub use graph::Resolution;
pub use graph::ResolutionError;
pub use graph::ResolutionResolved;
pub use graph::ScriptModule;
pub use graph::TypesDependency;
pub use graph::WalkOptions;
pub use graph::WorkspaceMember;
Expand Down Expand Up @@ -135,10 +135,8 @@ pub struct ParseModuleFromAstOptions<'a> {
}

/// Parse an individual module from an AST, returning the module.
pub fn parse_module_from_ast(
options: ParseModuleFromAstOptions,
) -> ScriptModule {
graph::parse_es_module_from_module_info(
pub fn parse_module_from_ast(options: ParseModuleFromAstOptions) -> JsModule {
graph::parse_js_module_from_module_info(
options.graph_kind,
options.specifier,
options.parsed_source.media_type(),
Expand Down Expand Up @@ -221,7 +219,7 @@ mod tests {
.unwrap()
.module()
.unwrap()
.esm()
.js()
.unwrap();
assert_eq!(module.dependencies.len(), 1);
let maybe_dependency = module.dependencies.get("./test02.ts");
Expand Down Expand Up @@ -1008,7 +1006,7 @@ console.log(a);
.unwrap()
.module()
.unwrap()
.esm()
.js()
.unwrap();
assert_eq!(module.media_type, MediaType::TypeScript);
}
Expand Down Expand Up @@ -1770,7 +1768,7 @@ export function a(a) {
.await;
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 module = graph.get(&data_specifier).unwrap().js().unwrap();
assert_eq!(
module.source.as_ref(),
r#"export * from "https://example.com/c.ts";"#,
Expand Down Expand Up @@ -1817,7 +1815,7 @@ export function a(a) {
},
)
.await;
let module = graph.get(&graph.roots[0]).unwrap().esm().unwrap();
let module = graph.get(&graph.roots[0]).unwrap().js().unwrap();
let maybe_dep = module.dependencies.get("b");
assert!(maybe_dep.is_some());
let dep = maybe_dep.unwrap();
Expand Down Expand Up @@ -1877,7 +1875,7 @@ export function a(a) {
},
)
.await;
let module = graph.get(&graph.roots[0]).unwrap().esm().unwrap();
let module = graph.get(&graph.roots[0]).unwrap().js().unwrap();
let types_dep = module.maybe_types_dependency.as_ref().unwrap();
assert_eq!(types_dep.specifier, "file:///a.js");
assert_eq!(
Expand Down Expand Up @@ -3030,7 +3028,7 @@ export function a(a) {
maybe_npm_resolver: None,
})
.unwrap();
let actual = actual.esm().unwrap();
let actual = actual.js().unwrap();
assert_eq!(actual.dependencies.len(), 7);
assert_eq!(actual.specifier, specifier);
assert_eq!(actual.media_type, MediaType::TypeScript);
Expand All @@ -3047,7 +3045,7 @@ export function a(a) {
maybe_npm_resolver: None,
})
.unwrap();
let actual = actual.esm().unwrap();
let actual = actual.js().unwrap();
assert_eq!(actual.dependencies.len(), 4);
}

Expand Down Expand Up @@ -3140,7 +3138,7 @@ export function a(a) {
maybe_npm_resolver: None,
})
.unwrap();
let actual = actual.esm().unwrap();
let actual = actual.js().unwrap();
assert_eq!(actual.dependencies.len(), 1);
let dep = actual
.dependencies
Expand Down Expand Up @@ -3183,7 +3181,7 @@ export function a(a) {
maybe_npm_resolver: None,
})
.unwrap();
let actual = actual.esm().unwrap();
let actual = actual.js().unwrap();
assert_eq!(actual.dependencies.len(), 1);
let dep = actual
.dependencies
Expand Down
Loading

0 comments on commit 95264e5

Please sign in to comment.