2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-12-16 05:45:41 -05:00
|
|
|
|
2022-12-06 14:12:51 -05:00
|
|
|
use crate::args::Lockfile;
|
2022-12-09 09:40:48 -05:00
|
|
|
use crate::args::TsConfigType;
|
2022-11-25 18:29:48 -05:00
|
|
|
use crate::args::TsTypeLib;
|
2022-12-09 09:40:48 -05:00
|
|
|
use crate::args::TypeCheckMode;
|
|
|
|
use crate::cache;
|
|
|
|
use crate::cache::TypeCheckCache;
|
2021-12-16 05:45:41 -05:00
|
|
|
use crate::colors;
|
|
|
|
use crate::errors::get_error_class_name;
|
2022-11-11 21:26:14 -05:00
|
|
|
use crate::npm::resolve_npm_package_reqs;
|
2022-08-20 11:31:33 -04:00
|
|
|
use crate::npm::NpmPackageReference;
|
|
|
|
use crate::npm::NpmPackageReq;
|
2022-12-09 09:40:48 -05:00
|
|
|
use crate::proc_state::ProcState;
|
|
|
|
use crate::resolver::CliResolver;
|
|
|
|
use crate::tools::check;
|
2022-06-28 16:45:55 -04:00
|
|
|
|
2022-12-09 09:40:48 -05:00
|
|
|
use deno_core::anyhow::bail;
|
2021-12-16 05:45:41 -05:00
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::error::AnyError;
|
2022-12-09 09:40:48 -05:00
|
|
|
use deno_core::parking_lot::RwLock;
|
2021-12-16 05:45:41 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_graph::Dependency;
|
2022-08-10 17:33:42 -04:00
|
|
|
use deno_graph::GraphImport;
|
2021-12-16 05:45:41 -05:00
|
|
|
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;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
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-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 {
|
2022-05-20 16:40:55 -04:00
|
|
|
code: Arc<str>,
|
2021-12-16 05:45:41 -05:00
|
|
|
dependencies: BTreeMap<String, Dependency>,
|
|
|
|
media_type: MediaType,
|
|
|
|
/// A set of type libs that the module has passed a type check with this
|
|
|
|
/// session. This would consist of window, worker or both.
|
2022-06-28 16:45:55 -04:00
|
|
|
checked_libs: HashSet<TsTypeLib>,
|
2022-01-31 17:33:57 -05:00
|
|
|
maybe_types: Option<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>,
|
2022-11-11 21:26:14 -05:00
|
|
|
npm_packages: Vec<NpmPackageReq>,
|
2021-12-16 05:45:41 -05:00
|
|
|
/// Map of first known referrer locations for each module. Used to enhance
|
|
|
|
/// error messages.
|
2022-11-10 11:20:46 -05:00
|
|
|
referrer_map: HashMap<ModuleSpecifier, Box<Range>>,
|
2022-08-10 17:33:42 -04:00
|
|
|
graph_imports: Vec<GraphImport>,
|
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`.
|
2023-01-10 10:28:10 -05:00
|
|
|
pub fn add_graph(&mut self, graph: &ModuleGraph) {
|
2022-08-09 17:27:22 -04:00
|
|
|
for graph_import in &graph.imports {
|
2022-08-10 17:33:42 -04:00
|
|
|
for dep in graph_import.dependencies.values() {
|
|
|
|
for resolved in [&dep.maybe_code, &dep.maybe_type] {
|
2022-08-09 17:27:22 -04:00
|
|
|
if let Resolved::Ok {
|
|
|
|
specifier, range, ..
|
2022-08-10 17:33:42 -04:00
|
|
|
} = resolved
|
2022-08-09 17:27:22 -04:00
|
|
|
{
|
|
|
|
let entry = self.referrer_map.entry(specifier.clone());
|
|
|
|
entry.or_insert_with(|| range.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-22 12:14:59 -04:00
|
|
|
self.graph_imports.push(graph_import.clone())
|
2022-08-09 17:27:22 -04:00
|
|
|
}
|
|
|
|
|
2022-11-11 21:26:14 -05:00
|
|
|
let mut has_npm_specifier_in_graph = false;
|
|
|
|
|
2021-12-16 05:45:41 -05:00
|
|
|
for (specifier, result) in graph.specifiers() {
|
2022-12-06 14:12:51 -05:00
|
|
|
if NpmPackageReference::from_specifier(specifier).is_ok() {
|
2022-11-11 21:26:14 -05:00
|
|
|
has_npm_specifier_in_graph = true;
|
2021-12-16 05:45:41 -05:00
|
|
|
continue;
|
|
|
|
}
|
2022-11-11 21:26:14 -05:00
|
|
|
|
2023-01-10 10:28:10 -05:00
|
|
|
if self.modules.contains_key(specifier) {
|
2022-11-11 21:26:14 -05:00
|
|
|
continue;
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
2022-11-11 21:26:14 -05:00
|
|
|
|
2022-12-06 14:12:51 -05:00
|
|
|
if let Some(found) = graph.redirects.get(specifier) {
|
2021-12-16 05:45:41 -05:00
|
|
|
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)) => {
|
2022-12-06 14:12:51 -05:00
|
|
|
let module = graph.get(specifier).unwrap();
|
2022-01-31 17:33:57 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let module_entry = ModuleEntry::Module {
|
|
|
|
code,
|
2022-01-31 17:33:57 -05:00
|
|
|
dependencies: module.dependencies.clone(),
|
2021-12-16 05:45:41 -05:00
|
|
|
media_type,
|
|
|
|
checked_libs: Default::default(),
|
|
|
|
maybe_types,
|
|
|
|
};
|
2022-12-06 14:12:51 -05:00
|
|
|
self.modules.insert(specifier.clone(), module_entry);
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
Err(error) => {
|
2022-12-06 14:12:51 -05:00
|
|
|
let module_entry = ModuleEntry::Error(error.clone());
|
|
|
|
self.modules.insert(specifier.clone(), module_entry);
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-11 21:26:14 -05:00
|
|
|
|
|
|
|
if has_npm_specifier_in_graph {
|
|
|
|
self.npm_packages.extend(resolve_npm_package_reqs(graph));
|
|
|
|
}
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
pub fn entries(
|
|
|
|
&self,
|
|
|
|
) -> impl Iterator<Item = (&ModuleSpecifier, &ModuleEntry)> {
|
|
|
|
self.modules.iter()
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
|
2022-11-11 21:26:14 -05:00
|
|
|
/// Gets the npm package requirements from all the encountered graphs
|
|
|
|
/// in the order that they should be resolved.
|
|
|
|
pub fn npm_package_reqs(&self) -> &Vec<NpmPackageReq> {
|
|
|
|
&self.npm_packages
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2021-12-16 05:45:41 -05:00
|
|
|
/// 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);
|
|
|
|
}
|
2022-08-10 17:33:42 -04:00
|
|
|
for (_, dep) in self.graph_imports.iter().flat_map(|i| &i.dependencies) {
|
|
|
|
let mut resolutions = vec![&dep.maybe_code];
|
|
|
|
if follow_type_only {
|
|
|
|
resolutions.push(&dep.maybe_type);
|
|
|
|
}
|
|
|
|
#[allow(clippy::manual_flatten)]
|
|
|
|
for resolved in resolutions {
|
|
|
|
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
|
|
|
}
|
|
|
|
while let Some(specifier) = visiting.pop_front() {
|
2022-08-20 11:31:33 -04:00
|
|
|
if NpmPackageReference::from_specifier(specifier).is_ok() {
|
|
|
|
continue; // skip analyzing npm specifiers
|
|
|
|
}
|
|
|
|
|
2021-12-16 05:45:41 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
for (dep_specifier, dep) in dependencies.iter().rev() {
|
|
|
|
// todo(dsherret): ideally there would be a way to skip external dependencies
|
|
|
|
// in the graph here rather than specifically npm package references
|
2022-11-13 10:42:15 -05:00
|
|
|
if NpmPackageReference::from_str(dep_specifier).is_ok() {
|
2022-08-20 11:31:33 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-16 05:45:41 -05:00
|
|
|
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::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,
|
2022-08-20 11:31:33 -04:00
|
|
|
npm_packages: self.npm_packages.clone(),
|
2021-12-16 05:45:41 -05:00
|
|
|
referrer_map,
|
2022-08-22 12:14:59 -04:00
|
|
|
graph_imports: self.graph_imports.to_vec(),
|
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::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)],
|
2022-06-28 16:45:55 -04:00
|
|
|
lib: TsTypeLib,
|
2021-12-16 05:45:41 -05:00
|
|
|
) {
|
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()
|
|
|
|
{
|
2022-06-28 16:45:55 -04:00
|
|
|
checked_libs.insert(lib);
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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)],
|
2022-06-28 16:45:55 -04:00
|
|
|
lib: &TsTypeLib,
|
2021-12-16 05:45:41 -05:00
|
|
|
) -> 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
|
|
|
|
2022-08-10 17:33:42 -04:00
|
|
|
/// Get the dependencies of a module or graph import.
|
|
|
|
pub fn get_dependencies<'a>(
|
|
|
|
&'a self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<&'a BTreeMap<String, Dependency>> {
|
|
|
|
let specifier = self.follow_redirect(specifier);
|
|
|
|
if let Some(ModuleEntry::Module { dependencies, .. }) = self.get(&specifier)
|
|
|
|
{
|
|
|
|
return Some(dependencies);
|
|
|
|
}
|
|
|
|
if let Some(graph_import) =
|
|
|
|
self.graph_imports.iter().find(|i| i.referrer == specifier)
|
|
|
|
{
|
|
|
|
return Some(&graph_import.dependencies);
|
|
|
|
}
|
|
|
|
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();
|
2023-01-10 10:28:10 -05:00
|
|
|
graph_data.add_graph(graph);
|
2021-12-16 05:45:41 -05:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-12-06 14:12:51 -05:00
|
|
|
/// Checks the lockfile against the graph and and exits on errors.
|
|
|
|
pub fn graph_lock_or_exit(graph: &ModuleGraph, lockfile: &mut Lockfile) {
|
|
|
|
for module in graph.modules() {
|
|
|
|
if let Some(source) = &module.maybe_source {
|
|
|
|
if !lockfile.check_or_insert_remote(module.specifier.as_str(), source) {
|
|
|
|
let err = format!(
|
|
|
|
concat!(
|
|
|
|
"The source code is invalid, as it does not match the expected hash in the lock file.\n",
|
|
|
|
" Specifier: {}\n",
|
|
|
|
" Lock file: {}",
|
|
|
|
),
|
|
|
|
module.specifier,
|
|
|
|
lockfile.filename.display(),
|
|
|
|
);
|
|
|
|
log::error!("{} {}", colors::red("error:"), err);
|
|
|
|
std::process::exit(10);
|
|
|
|
}
|
|
|
|
}
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
2022-12-09 09:40:48 -05:00
|
|
|
|
|
|
|
pub async fn create_graph_and_maybe_check(
|
|
|
|
root: ModuleSpecifier,
|
|
|
|
ps: &ProcState,
|
|
|
|
) -> Result<Arc<deno_graph::ModuleGraph>, AnyError> {
|
|
|
|
let mut cache = cache::FetchCacher::new(
|
|
|
|
ps.emit_cache.clone(),
|
|
|
|
ps.file_fetcher.clone(),
|
2023-01-07 11:25:34 -05:00
|
|
|
PermissionsContainer::allow_all(),
|
|
|
|
PermissionsContainer::allow_all(),
|
2022-12-09 09:40:48 -05:00
|
|
|
);
|
|
|
|
let maybe_imports = ps.options.to_maybe_imports()?;
|
|
|
|
let maybe_cli_resolver = CliResolver::maybe_new(
|
|
|
|
ps.options.to_maybe_jsx_import_source_config(),
|
|
|
|
ps.maybe_import_map.clone(),
|
|
|
|
);
|
|
|
|
let maybe_graph_resolver =
|
|
|
|
maybe_cli_resolver.as_ref().map(|r| r.as_graph_resolver());
|
|
|
|
let analyzer = ps.parsed_source_cache.as_analyzer();
|
|
|
|
let graph = Arc::new(
|
|
|
|
deno_graph::create_graph(
|
|
|
|
vec![(root, deno_graph::ModuleKind::Esm)],
|
|
|
|
&mut cache,
|
|
|
|
deno_graph::GraphOptions {
|
|
|
|
is_dynamic: false,
|
|
|
|
imports: maybe_imports,
|
|
|
|
resolver: maybe_graph_resolver,
|
|
|
|
module_analyzer: Some(&*analyzer),
|
|
|
|
reporter: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await,
|
|
|
|
);
|
|
|
|
|
|
|
|
let check_js = ps.options.check_js();
|
|
|
|
let mut graph_data = GraphData::default();
|
2023-01-10 10:28:10 -05:00
|
|
|
graph_data.add_graph(&graph);
|
2022-12-09 09:40:48 -05:00
|
|
|
graph_data
|
|
|
|
.check(
|
|
|
|
&graph.roots,
|
|
|
|
ps.options.type_check_mode() != TypeCheckMode::None,
|
|
|
|
check_js,
|
|
|
|
)
|
|
|
|
.unwrap()?;
|
|
|
|
ps.npm_resolver
|
|
|
|
.add_package_reqs(graph_data.npm_package_reqs().clone())
|
|
|
|
.await?;
|
|
|
|
if let Some(lockfile) = &ps.lockfile {
|
|
|
|
graph_lock_or_exit(&graph, &mut lockfile.lock());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ps.options.type_check_mode() != TypeCheckMode::None {
|
|
|
|
let ts_config_result =
|
|
|
|
ps.options.resolve_ts_config_for_emit(TsConfigType::Check {
|
|
|
|
lib: ps.options.ts_type_lib_window(),
|
|
|
|
})?;
|
|
|
|
if let Some(ignored_options) = ts_config_result.maybe_ignored_options {
|
2022-12-09 10:54:24 -05:00
|
|
|
log::warn!("{}", ignored_options);
|
2022-12-09 09:40:48 -05:00
|
|
|
}
|
|
|
|
let maybe_config_specifier = ps.options.maybe_config_file_specifier();
|
|
|
|
let cache = TypeCheckCache::new(&ps.dir.type_checking_cache_db_file_path());
|
|
|
|
let check_result = check::check(
|
|
|
|
&graph.roots,
|
|
|
|
Arc::new(RwLock::new(graph_data)),
|
|
|
|
&cache,
|
2023-01-05 14:29:50 -05:00
|
|
|
&ps.npm_resolver,
|
2022-12-09 09:40:48 -05:00
|
|
|
check::CheckOptions {
|
|
|
|
type_check_mode: ps.options.type_check_mode(),
|
|
|
|
debug: ps.options.log_level() == Some(log::Level::Debug),
|
|
|
|
maybe_config_specifier,
|
|
|
|
ts_config: ts_config_result.ts_config,
|
|
|
|
log_checks: true,
|
|
|
|
reload: ps.options.reload_flag(),
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
log::debug!("{}", check_result.stats);
|
|
|
|
if !check_result.diagnostics.is_empty() {
|
|
|
|
return Err(check_result.diagnostics.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(graph)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn error_for_any_npm_specifier(
|
|
|
|
graph: &deno_graph::ModuleGraph,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let first_npm_specifier = graph
|
|
|
|
.specifiers()
|
|
|
|
.filter_map(|(_, r)| match r {
|
|
|
|
Ok((specifier, kind, _)) if kind == deno_graph::ModuleKind::External => {
|
2023-01-05 12:33:09 -05:00
|
|
|
Some(specifier)
|
2022-12-09 09:40:48 -05:00
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.next();
|
|
|
|
if let Some(npm_specifier) = first_npm_specifier {
|
|
|
|
bail!("npm specifiers have not yet been implemented for this sub command (https://github.com/denoland/deno/issues/15960). Found: {}", npm_specifier)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|