2024-12-31 14:12:39 -05:00
|
|
|
// Copyright 2018-2025 the Deno authors. MIT license.
|
2024-03-05 15:45:44 -05:00
|
|
|
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2024-09-04 11:39:30 -04:00
|
|
|
use deno_config::deno_json::TsConfigForEmit;
|
2024-03-05 15:45:44 -05:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_semver::jsr::JsrDepPackageReq;
|
|
|
|
use deno_semver::jsr::JsrPackageReqReference;
|
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
|
|
|
|
2024-12-13 13:19:37 -05:00
|
|
|
pub fn import_map_deps(
|
|
|
|
import_map: &serde_json::Value,
|
|
|
|
) -> HashSet<JsrDepPackageReq> {
|
|
|
|
let values = imports_values(import_map.get("imports"))
|
|
|
|
.into_iter()
|
|
|
|
.chain(scope_values(import_map.get("scopes")));
|
|
|
|
values_to_set(values)
|
|
|
|
}
|
|
|
|
|
2024-03-05 15:45:44 -05:00
|
|
|
pub fn deno_json_deps(
|
2024-07-19 15:56:07 -04:00
|
|
|
config: &deno_config::deno_json::ConfigFile,
|
2024-03-05 15:45:44 -05:00
|
|
|
) -> HashSet<JsrDepPackageReq> {
|
|
|
|
let values = imports_values(config.json.imports.as_ref())
|
|
|
|
.into_iter()
|
|
|
|
.chain(scope_values(config.json.scopes.as_ref()));
|
2024-11-19 19:49:19 -05:00
|
|
|
let mut set = values_to_set(values);
|
|
|
|
|
|
|
|
if let Some(serde_json::Value::Object(compiler_options)) =
|
|
|
|
&config.json.compiler_options
|
|
|
|
{
|
|
|
|
// add jsxImportSource
|
|
|
|
if let Some(serde_json::Value::String(value)) =
|
|
|
|
compiler_options.get("jsxImportSource")
|
|
|
|
{
|
|
|
|
if let Some(dep_req) = value_to_dep_req(value) {
|
|
|
|
set.insert(dep_req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add jsxImportSourceTypes
|
|
|
|
if let Some(serde_json::Value::String(value)) =
|
|
|
|
compiler_options.get("jsxImportSourceTypes")
|
|
|
|
{
|
|
|
|
if let Some(dep_req) = value_to_dep_req(value) {
|
|
|
|
set.insert(dep_req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add the dependencies in the types array
|
|
|
|
if let Some(serde_json::Value::Array(types)) = compiler_options.get("types")
|
|
|
|
{
|
|
|
|
for value in types {
|
|
|
|
if let serde_json::Value::String(value) = value {
|
|
|
|
if let Some(dep_req) = value_to_dep_req(value) {
|
|
|
|
set.insert(dep_req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
2024-03-05 15:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn imports_values(value: Option<&serde_json::Value>) -> Vec<&String> {
|
|
|
|
let Some(obj) = value.and_then(|v| v.as_object()) else {
|
|
|
|
return Vec::new();
|
|
|
|
};
|
|
|
|
let mut items = Vec::with_capacity(obj.len());
|
|
|
|
for value in obj.values() {
|
|
|
|
if let serde_json::Value::String(value) = value {
|
|
|
|
items.push(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
items
|
|
|
|
}
|
|
|
|
|
|
|
|
fn scope_values(value: Option<&serde_json::Value>) -> Vec<&String> {
|
|
|
|
let Some(obj) = value.and_then(|v| v.as_object()) else {
|
|
|
|
return Vec::new();
|
|
|
|
};
|
|
|
|
obj.values().flat_map(|v| imports_values(Some(v))).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn values_to_set<'a>(
|
|
|
|
values: impl Iterator<Item = &'a String>,
|
|
|
|
) -> HashSet<JsrDepPackageReq> {
|
|
|
|
let mut entries = HashSet::new();
|
|
|
|
for value in values {
|
2024-11-19 19:49:19 -05:00
|
|
|
if let Some(dep_req) = value_to_dep_req(value) {
|
|
|
|
entries.insert(dep_req);
|
2024-03-05 15:45:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
entries
|
|
|
|
}
|
2024-09-04 11:39:30 -04:00
|
|
|
|
2024-11-19 19:49:19 -05:00
|
|
|
fn value_to_dep_req(value: &str) -> Option<JsrDepPackageReq> {
|
|
|
|
if let Ok(req_ref) = JsrPackageReqReference::from_str(value) {
|
|
|
|
Some(JsrDepPackageReq::jsr(req_ref.into_inner().req))
|
|
|
|
} else if let Ok(req_ref) = NpmPackageReqReference::from_str(value) {
|
|
|
|
Some(JsrDepPackageReq::npm(req_ref.into_inner().req))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 11:39:30 -04:00
|
|
|
pub fn check_warn_tsconfig(ts_config: &TsConfigForEmit) {
|
|
|
|
if let Some(ignored_options) = &ts_config.maybe_ignored_options {
|
|
|
|
log::warn!("{}", ignored_options);
|
|
|
|
}
|
|
|
|
let serde_json::Value::Object(obj) = &ts_config.ts_config.0 else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
if obj.get("experimentalDecorators") == Some(&serde_json::Value::Bool(true)) {
|
|
|
|
log::warn!(
|
|
|
|
"{} experimentalDecorators compiler option is deprecated and may be removed at any time",
|
|
|
|
deno_runtime::colors::yellow("Warning"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|