diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2020-10-14 10:52:49 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-14 10:52:49 +1100 |
commit | 10654fa95553866c63a56a7f84c7ec47fb7aac9c (patch) | |
tree | 011b6f67259db903cda6bf61d439fdeec588835d /cli/module_graph2.rs | |
parent | 374d433f1f4885686dc5c166be3a25c839e22959 (diff) |
refactor(cli): add tsc2 (#7942)
Ref #7225
Diffstat (limited to 'cli/module_graph2.rs')
-rw-r--r-- | cli/module_graph2.rs | 77 |
1 files changed, 74 insertions, 3 deletions
diff --git a/cli/module_graph2.rs b/cli/module_graph2.rs index 412519178..de9e3a5db 100644 --- a/cli/module_graph2.rs +++ b/cli/module_graph2.rs @@ -373,8 +373,8 @@ impl Module { } } -#[derive(Clone, Debug, PartialEq)] -pub struct Stats(Vec<(String, u128)>); +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct Stats(pub Vec<(String, u128)>); impl<'de> Deserialize<'de> for Stats { fn deserialize<D>(deserializer: D) -> result::Result<Self, D::Error> @@ -572,6 +572,27 @@ impl Graph2 { Ok(()) } + pub fn get_media_type( + &self, + specifier: &ModuleSpecifier, + ) -> Option<MediaType> { + if let Some(module) = self.modules.get(specifier) { + Some(module.media_type) + } else { + None + } + } + + /// Get the source for a given module specifier. If the module is not part + /// of the graph, the result will be `None`. + pub fn get_source(&self, specifier: &ModuleSpecifier) -> Option<String> { + if let Some(module) = self.modules.get(specifier) { + Some(module.source.clone()) + } else { + None + } + } + /// Verify the subresource integrity of the graph based upon the optional /// lockfile, updating the lockfile with any missing resources. This will /// error if any of the resources do not match their lock status. @@ -595,6 +616,56 @@ impl Graph2 { Ok(()) } + /// Given a string specifier and a referring module specifier, provide the + /// resulting module specifier and media type for the module that is part of + /// the graph. + pub fn resolve( + &self, + specifier: &str, + referrer: &ModuleSpecifier, + ) -> Result<ModuleSpecifier, AnyError> { + if !self.modules.contains_key(referrer) { + return Err(MissingSpecifier(referrer.to_owned()).into()); + } + let module = self.modules.get(referrer).unwrap(); + if !module.dependencies.contains_key(specifier) { + return Err( + MissingDependency(referrer.to_owned(), specifier.to_owned()).into(), + ); + } + let dependency = module.dependencies.get(specifier).unwrap(); + // If there is a @deno-types pragma that impacts the dependency, then the + // maybe_type property will be set with that specifier, otherwise we use the + // specifier that point to the runtime code. + let resolved_specifier = + if let Some(type_specifier) = dependency.maybe_type.clone() { + type_specifier + } else if let Some(code_specifier) = dependency.maybe_code.clone() { + code_specifier + } else { + return Err( + MissingDependency(referrer.to_owned(), specifier.to_owned()).into(), + ); + }; + if !self.modules.contains_key(&resolved_specifier) { + return Err( + MissingDependency(referrer.to_owned(), resolved_specifier.to_string()) + .into(), + ); + } + let dep_module = self.modules.get(&resolved_specifier).unwrap(); + // In the case that there is a X-TypeScript-Types or a triple-slash types, + // then the `maybe_types` specifier will be populated and we should use that + // instead. + let result = if let Some((_, types)) = dep_module.maybe_types.clone() { + types + } else { + resolved_specifier + }; + + Ok(result) + } + /// Transpile (only transform) the graph, updating any emitted modules /// with the specifier handler. The result contains any performance stats /// from the compiler and optionally any user provided configuration compiler @@ -798,7 +869,7 @@ impl GraphBuilder2 { } #[cfg(test)] -mod tests { +pub mod tests { use super::*; use deno_core::futures::future; |