2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-12-16 05:45:41 -05:00
|
|
|
|
|
|
|
use crate::colors;
|
|
|
|
use crate::emit::TypeLib;
|
|
|
|
use crate::errors::get_error_class_name;
|
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_graph::Dependency;
|
|
|
|
use deno_graph::MediaType;
|
|
|
|
use deno_graph::ModuleGraph;
|
|
|
|
use deno_graph::ModuleGraphError;
|
2022-01-31 17:33:57 -05:00
|
|
|
use deno_graph::ModuleKind;
|
2021-12-16 05:45:41 -05:00
|
|
|
use deno_graph::Range;
|
2022-01-31 17:33:57 -05:00
|
|
|
use deno_graph::Resolved;
|
2022-04-18 10:52:26 -04:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use regex::Regex;
|
2021-12-16 05:45:41 -05:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-04-18 10:52:26 -04:00
|
|
|
/// Matches the `@ts-check` pragma.
|
|
|
|
static TS_CHECK_RE: Lazy<Regex> =
|
|
|
|
Lazy::new(|| Regex::new(r#"(?i)^\s*@ts-check(?:\s+|$)"#).unwrap());
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn contains_specifier(
|
2022-01-31 17:33:57 -05:00
|
|
|
v: &[(ModuleSpecifier, ModuleKind)],
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> bool {
|
|
|
|
v.iter().any(|(s, _)| s == specifier)
|
|
|
|
}
|
|
|
|
|
2021-12-16 05:45:41 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
#[allow(clippy::large_enum_variant)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub enum ModuleEntry {
|
2021-12-16 05:45:41 -05:00
|
|
|
Module {
|
|
|
|
code: Arc<String>,
|
|
|
|
dependencies: BTreeMap<String, Dependency>,
|
|
|
|
media_type: MediaType,
|
2022-04-18 10:52:26 -04:00
|
|
|
/// Whether or not this is a JS/JSX module with a `@ts-check` directive.
|
|
|
|
ts_check: bool,
|
2021-12-16 05:45:41 -05:00
|
|
|
/// A set of type libs that the module has passed a type check with this
|
|
|
|
/// session. This would consist of window, worker or both.
|
|
|
|
checked_libs: HashSet<TypeLib>,
|
2022-01-31 17:33:57 -05:00
|
|
|
maybe_types: Option<Resolved>,
|
2021-12-16 05:45:41 -05:00
|
|
|
},
|
|
|
|
Configuration {
|
2022-01-31 17:33:57 -05:00
|
|
|
dependencies: BTreeMap<String, Resolved>,
|
2021-12-16 05:45:41 -05:00
|
|
|
},
|
|
|
|
Error(ModuleGraphError),
|
|
|
|
Redirect(ModuleSpecifier),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Composes data from potentially many `ModuleGraph`s.
|
|
|
|
#[derive(Debug, Default)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct GraphData {
|
2021-12-16 05:45:41 -05:00
|
|
|
modules: HashMap<ModuleSpecifier, ModuleEntry>,
|
|
|
|
/// Map of first known referrer locations for each module. Used to enhance
|
|
|
|
/// error messages.
|
|
|
|
referrer_map: HashMap<ModuleSpecifier, Range>,
|
|
|
|
configurations: HashSet<ModuleSpecifier>,
|
2022-02-27 08:38:45 -05:00
|
|
|
cjs_esm_translations: HashMap<ModuleSpecifier, String>,
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GraphData {
|
|
|
|
/// Store data from `graph` into `self`.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn add_graph(&mut self, graph: &ModuleGraph, reload: bool) {
|
2021-12-16 05:45:41 -05:00
|
|
|
for (specifier, result) in graph.specifiers() {
|
|
|
|
if !reload && self.modules.contains_key(&specifier) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Some(found) = graph.redirects.get(&specifier) {
|
|
|
|
let module_entry = ModuleEntry::Redirect(found.clone());
|
|
|
|
self.modules.insert(specifier.clone(), module_entry);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
match result {
|
2022-01-31 17:33:57 -05:00
|
|
|
Ok((_, _, media_type)) => {
|
2021-12-16 05:45:41 -05:00
|
|
|
let module = graph.get(&specifier).unwrap();
|
2022-01-31 17:33:57 -05:00
|
|
|
if module.kind == ModuleKind::Synthetic {
|
|
|
|
let mut dependencies = BTreeMap::new();
|
|
|
|
for (specifier, dependency) in &module.dependencies {
|
|
|
|
if !matches!(dependency.maybe_type, Resolved::None) {
|
|
|
|
dependencies
|
|
|
|
.insert(specifier.clone(), dependency.maybe_type.clone());
|
|
|
|
if let Resolved::Ok {
|
|
|
|
specifier, range, ..
|
|
|
|
} = &dependency.maybe_type
|
|
|
|
{
|
|
|
|
let entry = self.referrer_map.entry(specifier.clone());
|
|
|
|
entry.or_insert_with(|| range.clone());
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
2022-01-31 17:33:57 -05:00
|
|
|
}
|
|
|
|
self.modules.insert(
|
|
|
|
module.specifier.clone(),
|
|
|
|
ModuleEntry::Configuration { dependencies },
|
|
|
|
);
|
|
|
|
self.configurations.insert(module.specifier.clone());
|
|
|
|
}
|
|
|
|
let code = match &module.maybe_source {
|
|
|
|
Some(source) => source.clone(),
|
|
|
|
None => continue,
|
2021-12-16 05:45:41 -05:00
|
|
|
};
|
2022-01-31 17:33:57 -05:00
|
|
|
let maybe_types = module
|
|
|
|
.maybe_types_dependency
|
|
|
|
.as_ref()
|
|
|
|
.map(|(_, r)| r.clone());
|
|
|
|
if let Some(Resolved::Ok {
|
|
|
|
specifier, range, ..
|
|
|
|
}) = &maybe_types
|
|
|
|
{
|
2021-12-16 05:45:41 -05:00
|
|
|
let specifier = graph.redirects.get(specifier).unwrap_or(specifier);
|
|
|
|
let entry = self.referrer_map.entry(specifier.clone());
|
2022-01-31 17:33:57 -05:00
|
|
|
entry.or_insert_with(|| range.clone());
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
2022-01-31 17:33:57 -05:00
|
|
|
for dep in module.dependencies.values() {
|
2021-12-16 05:45:41 -05:00
|
|
|
#[allow(clippy::manual_flatten)]
|
|
|
|
for resolved in [&dep.maybe_code, &dep.maybe_type] {
|
2022-01-31 17:33:57 -05:00
|
|
|
if let Resolved::Ok {
|
|
|
|
specifier, range, ..
|
|
|
|
} = resolved
|
|
|
|
{
|
2021-12-16 05:45:41 -05:00
|
|
|
let specifier =
|
|
|
|
graph.redirects.get(specifier).unwrap_or(specifier);
|
|
|
|
let entry = self.referrer_map.entry(specifier.clone());
|
2022-01-31 17:33:57 -05:00
|
|
|
entry.or_insert_with(|| range.clone());
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 10:52:26 -04:00
|
|
|
let ts_check = match &media_type {
|
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Mjs
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Jsx => {
|
|
|
|
let parsed_source = module.maybe_parsed_source.as_ref().unwrap();
|
|
|
|
parsed_source
|
|
|
|
.get_leading_comments()
|
|
|
|
.iter()
|
|
|
|
.any(|c| TS_CHECK_RE.is_match(&c.text))
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
};
|
2021-12-16 05:45:41 -05:00
|
|
|
let module_entry = ModuleEntry::Module {
|
|
|
|
code,
|
2022-01-31 17:33:57 -05:00
|
|
|
dependencies: module.dependencies.clone(),
|
2022-04-18 10:52:26 -04:00
|
|
|
ts_check,
|
2021-12-16 05:45:41 -05:00
|
|
|
media_type,
|
|
|
|
checked_libs: Default::default(),
|
|
|
|
maybe_types,
|
|
|
|
};
|
|
|
|
self.modules.insert(specifier, module_entry);
|
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
let module_entry = ModuleEntry::Error(error);
|
|
|
|
self.modules.insert(specifier, module_entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn entries(&self) -> HashMap<&ModuleSpecifier, &ModuleEntry> {
|
2021-12-16 05:45:41 -05:00
|
|
|
self.modules.iter().collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Walk dependencies from `roots` and return every encountered specifier.
|
|
|
|
/// Return `None` if any modules are not known.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn walk<'a>(
|
2021-12-16 05:45:41 -05:00
|
|
|
&'a self,
|
2022-01-31 17:33:57 -05:00
|
|
|
roots: &[(ModuleSpecifier, ModuleKind)],
|
2021-12-16 05:45:41 -05:00
|
|
|
follow_dynamic: bool,
|
|
|
|
follow_type_only: bool,
|
2021-12-22 08:25:06 -05:00
|
|
|
check_js: bool,
|
2021-12-16 05:45:41 -05:00
|
|
|
) -> Option<HashMap<&'a ModuleSpecifier, &'a ModuleEntry>> {
|
|
|
|
let mut result = HashMap::<&'a ModuleSpecifier, &'a ModuleEntry>::new();
|
|
|
|
let mut seen = HashSet::<&ModuleSpecifier>::new();
|
|
|
|
let mut visiting = VecDeque::<&ModuleSpecifier>::new();
|
2022-01-31 17:33:57 -05:00
|
|
|
for (root, _) in roots {
|
2021-12-16 05:45:41 -05:00
|
|
|
seen.insert(root);
|
|
|
|
visiting.push_back(root);
|
|
|
|
}
|
|
|
|
for root in &self.configurations {
|
|
|
|
seen.insert(root);
|
|
|
|
visiting.push_back(root);
|
|
|
|
}
|
|
|
|
while let Some(specifier) = visiting.pop_front() {
|
|
|
|
let (specifier, entry) = match self.modules.get_key_value(specifier) {
|
|
|
|
Some(pair) => pair,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
result.insert(specifier, entry);
|
|
|
|
match entry {
|
|
|
|
ModuleEntry::Module {
|
|
|
|
dependencies,
|
|
|
|
maybe_types,
|
2021-12-22 08:25:06 -05:00
|
|
|
media_type,
|
2021-12-16 05:45:41 -05:00
|
|
|
..
|
|
|
|
} => {
|
2021-12-22 08:25:06 -05:00
|
|
|
let check_types = (check_js
|
|
|
|
|| !matches!(
|
|
|
|
media_type,
|
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Mjs
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Jsx
|
|
|
|
))
|
|
|
|
&& follow_type_only;
|
|
|
|
if check_types {
|
2022-01-31 17:33:57 -05:00
|
|
|
if let Some(Resolved::Ok { specifier, .. }) = maybe_types {
|
|
|
|
if !seen.contains(specifier) {
|
|
|
|
seen.insert(specifier);
|
|
|
|
visiting.push_front(specifier);
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (_, dep) in dependencies.iter().rev() {
|
|
|
|
if !dep.is_dynamic || follow_dynamic {
|
|
|
|
let mut resolutions = vec![&dep.maybe_code];
|
2021-12-22 08:25:06 -05:00
|
|
|
if check_types {
|
2021-12-16 05:45:41 -05:00
|
|
|
resolutions.push(&dep.maybe_type);
|
|
|
|
}
|
|
|
|
#[allow(clippy::manual_flatten)]
|
|
|
|
for resolved in resolutions {
|
2022-01-31 17:33:57 -05:00
|
|
|
if let Resolved::Ok { specifier, .. } = resolved {
|
|
|
|
if !seen.contains(specifier) {
|
|
|
|
seen.insert(specifier);
|
|
|
|
visiting.push_front(specifier);
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ModuleEntry::Configuration { dependencies } => {
|
2022-01-31 17:33:57 -05:00
|
|
|
for resolved in dependencies.values() {
|
|
|
|
if let Resolved::Ok { specifier, .. } = resolved {
|
|
|
|
if !seen.contains(specifier) {
|
|
|
|
seen.insert(specifier);
|
|
|
|
visiting.push_front(specifier);
|
|
|
|
}
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ModuleEntry::Error(_) => {}
|
|
|
|
ModuleEntry::Redirect(specifier) => {
|
|
|
|
if !seen.contains(specifier) {
|
|
|
|
seen.insert(specifier);
|
|
|
|
visiting.push_front(specifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clone part of `self`, containing only modules which are dependencies of
|
|
|
|
/// `roots`. Returns `None` if any roots are not known.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn graph_segment(
|
2021-12-16 05:45:41 -05:00
|
|
|
&self,
|
2022-01-31 17:33:57 -05:00
|
|
|
roots: &[(ModuleSpecifier, ModuleKind)],
|
2021-12-16 05:45:41 -05:00
|
|
|
) -> Option<Self> {
|
|
|
|
let mut modules = HashMap::new();
|
|
|
|
let mut referrer_map = HashMap::new();
|
2021-12-22 08:25:06 -05:00
|
|
|
let entries = match self.walk(roots, true, true, true) {
|
2021-12-16 05:45:41 -05:00
|
|
|
Some(entries) => entries,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
for (specifier, module_entry) in entries {
|
|
|
|
modules.insert(specifier.clone(), module_entry.clone());
|
|
|
|
if let Some(referrer) = self.referrer_map.get(specifier) {
|
|
|
|
referrer_map.insert(specifier.clone(), referrer.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(Self {
|
|
|
|
modules,
|
|
|
|
referrer_map,
|
|
|
|
configurations: self.configurations.clone(),
|
2022-02-27 08:38:45 -05:00
|
|
|
cjs_esm_translations: Default::default(),
|
2021-12-16 05:45:41 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if `roots` and their deps are available. Returns `Some(Ok(()))` if
|
|
|
|
/// so. Returns `Some(Err(_))` if there is a known module graph or resolution
|
|
|
|
/// error statically reachable from `roots`. Returns `None` if any modules are
|
|
|
|
/// not known.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn check(
|
2021-12-16 05:45:41 -05:00
|
|
|
&self,
|
2022-01-31 17:33:57 -05:00
|
|
|
roots: &[(ModuleSpecifier, ModuleKind)],
|
2021-12-16 05:45:41 -05:00
|
|
|
follow_type_only: bool,
|
2021-12-22 08:25:06 -05:00
|
|
|
check_js: bool,
|
2021-12-16 05:45:41 -05:00
|
|
|
) -> Option<Result<(), AnyError>> {
|
2021-12-22 08:25:06 -05:00
|
|
|
let entries = match self.walk(roots, false, follow_type_only, check_js) {
|
2021-12-16 05:45:41 -05:00
|
|
|
Some(entries) => entries,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
for (specifier, module_entry) in entries {
|
|
|
|
match module_entry {
|
|
|
|
ModuleEntry::Module {
|
|
|
|
dependencies,
|
|
|
|
maybe_types,
|
2021-12-22 08:25:06 -05:00
|
|
|
media_type,
|
2021-12-16 05:45:41 -05:00
|
|
|
..
|
|
|
|
} => {
|
2021-12-22 08:25:06 -05:00
|
|
|
let check_types = (check_js
|
|
|
|
|| !matches!(
|
|
|
|
media_type,
|
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Mjs
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Jsx
|
|
|
|
))
|
|
|
|
&& follow_type_only;
|
|
|
|
if check_types {
|
2022-01-31 17:33:57 -05:00
|
|
|
if let Some(Resolved::Err(error)) = maybe_types {
|
2021-12-16 05:45:41 -05:00
|
|
|
let range = error.range();
|
|
|
|
if !range.specifier.as_str().contains("$deno") {
|
|
|
|
return Some(Err(custom_error(
|
|
|
|
get_error_class_name(&error.clone().into()),
|
2022-01-15 01:10:12 -05:00
|
|
|
format!("{}\n at {}", error, range),
|
2021-12-16 05:45:41 -05:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
return Some(Err(error.clone().into()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (_, dep) in dependencies.iter() {
|
|
|
|
if !dep.is_dynamic {
|
|
|
|
let mut resolutions = vec![&dep.maybe_code];
|
2021-12-22 08:25:06 -05:00
|
|
|
if check_types {
|
2021-12-16 05:45:41 -05:00
|
|
|
resolutions.push(&dep.maybe_type);
|
|
|
|
}
|
|
|
|
#[allow(clippy::manual_flatten)]
|
|
|
|
for resolved in resolutions {
|
2022-01-31 17:33:57 -05:00
|
|
|
if let Resolved::Err(error) = resolved {
|
2021-12-16 05:45:41 -05:00
|
|
|
let range = error.range();
|
|
|
|
if !range.specifier.as_str().contains("$deno") {
|
|
|
|
return Some(Err(custom_error(
|
|
|
|
get_error_class_name(&error.clone().into()),
|
2022-01-15 01:10:12 -05:00
|
|
|
format!("{}\n at {}", error, range),
|
2021-12-16 05:45:41 -05:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
return Some(Err(error.clone().into()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ModuleEntry::Configuration { dependencies } => {
|
|
|
|
for resolved_result in dependencies.values() {
|
2022-01-31 17:33:57 -05:00
|
|
|
if let Resolved::Err(error) = resolved_result {
|
2021-12-16 05:45:41 -05:00
|
|
|
let range = error.range();
|
|
|
|
if !range.specifier.as_str().contains("$deno") {
|
|
|
|
return Some(Err(custom_error(
|
|
|
|
get_error_class_name(&error.clone().into()),
|
2022-01-15 01:10:12 -05:00
|
|
|
format!("{}\n at {}", error, range),
|
2021-12-16 05:45:41 -05:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
return Some(Err(error.clone().into()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ModuleEntry::Error(error) => {
|
2022-01-31 17:33:57 -05:00
|
|
|
if !contains_specifier(roots, specifier) {
|
2021-12-16 05:45:41 -05:00
|
|
|
if let Some(range) = self.referrer_map.get(specifier) {
|
|
|
|
if !range.specifier.as_str().contains("$deno") {
|
|
|
|
let message = error.to_string();
|
|
|
|
return Some(Err(custom_error(
|
|
|
|
get_error_class_name(&error.clone().into()),
|
|
|
|
format!("{}\n at {}", message, range),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Some(Err(error.clone().into()));
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(Ok(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mark `roots` and all of their dependencies as type checked under `lib`.
|
|
|
|
/// Assumes that all of those modules are known.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn set_type_checked(
|
2021-12-16 05:45:41 -05:00
|
|
|
&mut self,
|
2022-01-31 17:33:57 -05:00
|
|
|
roots: &[(ModuleSpecifier, ModuleKind)],
|
2021-12-16 05:45:41 -05:00
|
|
|
lib: &TypeLib,
|
|
|
|
) {
|
2021-12-22 08:25:06 -05:00
|
|
|
let specifiers: Vec<ModuleSpecifier> =
|
|
|
|
match self.walk(roots, true, true, true) {
|
|
|
|
Some(entries) => entries.into_keys().cloned().collect(),
|
|
|
|
None => unreachable!("contains module not in graph data"),
|
|
|
|
};
|
2021-12-16 05:45:41 -05:00
|
|
|
for specifier in specifiers {
|
|
|
|
if let ModuleEntry::Module { checked_libs, .. } =
|
|
|
|
self.modules.get_mut(&specifier).unwrap()
|
|
|
|
{
|
|
|
|
checked_libs.insert(lib.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if `roots` are all marked as type checked under `lib`.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn is_type_checked(
|
2021-12-16 05:45:41 -05:00
|
|
|
&self,
|
2022-01-31 17:33:57 -05:00
|
|
|
roots: &[(ModuleSpecifier, ModuleKind)],
|
2021-12-16 05:45:41 -05:00
|
|
|
lib: &TypeLib,
|
|
|
|
) -> bool {
|
2022-01-31 17:33:57 -05:00
|
|
|
roots.iter().all(|(r, _)| {
|
2021-12-16 05:45:41 -05:00
|
|
|
let found = self.follow_redirect(r);
|
|
|
|
match self.modules.get(&found) {
|
|
|
|
Some(ModuleEntry::Module { checked_libs, .. }) => {
|
|
|
|
checked_libs.contains(lib)
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If `specifier` is known and a redirect, return the found specifier.
|
|
|
|
/// Otherwise return `specifier`.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn follow_redirect(
|
2021-12-16 05:45:41 -05:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> ModuleSpecifier {
|
|
|
|
match self.modules.get(specifier) {
|
|
|
|
Some(ModuleEntry::Redirect(s)) => s.clone(),
|
|
|
|
_ => specifier.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn get<'a>(
|
2021-12-16 05:45:41 -05:00
|
|
|
&'a self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<&'a ModuleEntry> {
|
|
|
|
self.modules.get(specifier)
|
|
|
|
}
|
2022-02-27 08:38:45 -05:00
|
|
|
|
|
|
|
// TODO(bartlomieju): after saving translated source
|
|
|
|
// it's never removed, potentially leading to excessive
|
|
|
|
// memory consumption
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn add_cjs_esm_translation(
|
2022-02-27 08:38:45 -05:00
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
source: String,
|
|
|
|
) {
|
|
|
|
let prev = self
|
|
|
|
.cjs_esm_translations
|
|
|
|
.insert(specifier.to_owned(), source);
|
|
|
|
assert!(prev.is_none());
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn get_cjs_esm_translation<'a>(
|
2022-02-27 08:38:45 -05:00
|
|
|
&'a self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<&'a String> {
|
|
|
|
self.cjs_esm_translations.get(specifier)
|
|
|
|
}
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&ModuleGraph> for GraphData {
|
|
|
|
fn from(graph: &ModuleGraph) -> Self {
|
|
|
|
let mut graph_data = GraphData::default();
|
|
|
|
graph_data.add_graph(graph, false);
|
|
|
|
graph_data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like `graph.valid()`, but enhanced with referrer info.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn graph_valid(
|
2021-12-16 05:45:41 -05:00
|
|
|
graph: &ModuleGraph,
|
|
|
|
follow_type_only: bool,
|
2021-12-22 08:25:06 -05:00
|
|
|
check_js: bool,
|
2021-12-16 05:45:41 -05:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
GraphData::from(graph)
|
2021-12-22 08:25:06 -05:00
|
|
|
.check(&graph.roots, follow_type_only, check_js)
|
2021-12-16 05:45:41 -05:00
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calls `graph.lock()` and exits on errors.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn graph_lock_or_exit(graph: &ModuleGraph) {
|
2021-12-16 05:45:41 -05:00
|
|
|
if let Err(err) = graph.lock() {
|
|
|
|
log::error!("{} {}", colors::red("error:"), err);
|
|
|
|
std::process::exit(10);
|
|
|
|
}
|
|
|
|
}
|