2023-01-02 21:00:42 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-08-31 18:12:24 +00:00
|
|
|
|
2022-06-27 20:54:09 +00:00
|
|
|
use crate::args::ConfigFlag;
|
|
|
|
use crate::args::Flags;
|
2022-11-28 22:28:54 +00:00
|
|
|
use crate::util::fs::canonicalize_path;
|
|
|
|
use crate::util::path::specifier_parent;
|
|
|
|
use crate::util::path::specifier_to_file_path;
|
2021-11-09 01:26:39 +00:00
|
|
|
|
2021-11-16 14:02:28 +00:00
|
|
|
use deno_core::anyhow::anyhow;
|
2021-11-24 20:14:19 +00:00
|
|
|
use deno_core::anyhow::bail;
|
2021-11-16 14:02:28 +00:00
|
|
|
use deno_core::anyhow::Context;
|
2020-09-14 16:48:57 +00:00
|
|
|
use deno_core::error::AnyError;
|
2021-02-25 20:18:35 +00:00
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde::Serialize;
|
|
|
|
use deno_core::serde::Serializer;
|
2020-09-21 16:36:37 +00:00
|
|
|
use deno_core::serde_json;
|
2020-10-26 13:03:03 +00:00
|
|
|
use deno_core::serde_json::json;
|
2020-09-21 16:36:37 +00:00
|
|
|
use deno_core::serde_json::Value;
|
2021-10-10 21:26:22 +00:00
|
|
|
use deno_core::ModuleSpecifier;
|
2023-02-23 03:45:35 +00:00
|
|
|
use indexmap::IndexMap;
|
2023-02-23 01:16:16 +00:00
|
|
|
use std::borrow::Cow;
|
2020-10-29 10:18:18 +00:00
|
|
|
use std::collections::BTreeMap;
|
2020-08-31 18:12:24 +00:00
|
|
|
use std::collections::HashMap;
|
2022-01-18 01:10:17 +00:00
|
|
|
use std::collections::HashSet;
|
2020-08-31 18:12:24 +00:00
|
|
|
use std::fmt;
|
2020-09-29 07:16:12 +00:00
|
|
|
use std::path::Path;
|
2022-01-18 01:10:17 +00:00
|
|
|
use std::path::PathBuf;
|
2020-08-31 18:12:24 +00:00
|
|
|
|
2022-03-23 13:54:22 +00:00
|
|
|
pub type MaybeImportsResult =
|
2023-02-10 03:00:23 +00:00
|
|
|
Result<Vec<deno_graph::ReferrerImports>, AnyError>;
|
2021-11-09 01:26:39 +00:00
|
|
|
|
2023-01-28 15:18:32 +00:00
|
|
|
#[derive(Hash)]
|
2022-08-24 17:36:05 +00:00
|
|
|
pub struct JsxImportSourceConfig {
|
|
|
|
pub default_specifier: Option<String>,
|
|
|
|
pub module: String,
|
|
|
|
}
|
|
|
|
|
2020-09-29 07:16:12 +00:00
|
|
|
/// The transpile options that are significant out of a user provided tsconfig
|
|
|
|
/// file, that we want to deserialize out of the final config for a transpile.
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-10-20 03:10:42 +00:00
|
|
|
pub struct EmitConfigOptions {
|
2020-09-29 07:16:12 +00:00
|
|
|
pub check_js: bool,
|
|
|
|
pub emit_decorator_metadata: bool,
|
2021-03-07 20:40:11 +00:00
|
|
|
pub imports_not_used_as_values: String,
|
2020-10-26 13:03:03 +00:00
|
|
|
pub inline_source_map: bool,
|
2021-09-08 04:05:34 +00:00
|
|
|
pub inline_sources: bool,
|
2021-05-19 04:18:01 +00:00
|
|
|
pub source_map: bool,
|
2020-09-29 07:16:12 +00:00
|
|
|
pub jsx: String,
|
|
|
|
pub jsx_factory: String,
|
|
|
|
pub jsx_fragment_factory: String,
|
2021-11-09 01:26:39 +00:00
|
|
|
pub jsx_import_source: Option<String>,
|
2020-09-29 07:16:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 21:18:32 +00:00
|
|
|
/// There are certain compiler options that can impact what modules are part of
|
|
|
|
/// a module graph, which need to be deserialized into a structure for analysis.
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CompilerOptions {
|
2021-11-09 01:26:39 +00:00
|
|
|
pub jsx: Option<String>,
|
|
|
|
pub jsx_import_source: Option<String>,
|
2021-06-21 21:18:32 +00:00
|
|
|
pub types: Option<Vec<String>>,
|
|
|
|
}
|
|
|
|
|
2020-09-29 07:16:12 +00:00
|
|
|
/// A structure that represents a set of options that were ignored and the
|
|
|
|
/// path those options came from.
|
2022-09-19 08:25:03 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-09-29 07:16:12 +00:00
|
|
|
pub struct IgnoredCompilerOptions {
|
|
|
|
pub items: Vec<String>,
|
2021-11-24 20:14:19 +00:00
|
|
|
pub maybe_specifier: Option<ModuleSpecifier>,
|
2020-09-29 07:16:12 +00:00
|
|
|
}
|
2020-08-31 18:12:24 +00:00
|
|
|
|
|
|
|
impl fmt::Display for IgnoredCompilerOptions {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2020-09-29 07:16:12 +00:00
|
|
|
let mut codes = self.items.clone();
|
2023-01-05 19:29:50 +00:00
|
|
|
codes.sort_unstable();
|
2021-11-24 20:14:19 +00:00
|
|
|
if let Some(specifier) = &self.maybe_specifier {
|
|
|
|
write!(f, "Unsupported compiler options in \"{}\".\n The following options were ignored:\n {}", specifier, codes.join(", "))
|
2020-10-26 13:03:03 +00:00
|
|
|
} else {
|
|
|
|
write!(f, "Unsupported compiler options provided.\n The following options were ignored:\n {}", codes.join(", "))
|
|
|
|
}
|
2020-08-31 18:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 21:43:54 +00:00
|
|
|
impl Serialize for IgnoredCompilerOptions {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
Serialize::serialize(&self.items, serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 18:12:24 +00:00
|
|
|
/// A static slice of all the compiler options that should be ignored that
|
|
|
|
/// either have no effect on the compilation or would cause the emit to not work
|
|
|
|
/// in Deno.
|
2020-12-07 10:46:39 +00:00
|
|
|
pub const IGNORED_COMPILER_OPTIONS: &[&str] = &[
|
2023-03-21 15:46:40 +00:00
|
|
|
"allowImportingTsExtensions",
|
2020-08-31 18:12:24 +00:00
|
|
|
"allowSyntheticDefaultImports",
|
2020-11-02 02:51:56 +00:00
|
|
|
"allowUmdGlobalAccess",
|
2020-08-31 18:12:24 +00:00
|
|
|
"assumeChangesOnlyAffectDirectDependencies",
|
2022-07-13 19:38:36 +00:00
|
|
|
"baseUrl",
|
2020-08-31 18:12:24 +00:00
|
|
|
"build",
|
2020-11-02 02:51:56 +00:00
|
|
|
"charset",
|
2020-08-31 18:12:24 +00:00
|
|
|
"composite",
|
2022-07-13 19:38:36 +00:00
|
|
|
"declaration",
|
|
|
|
"declarationMap",
|
2020-08-31 18:12:24 +00:00
|
|
|
"diagnostics",
|
2020-11-02 02:51:56 +00:00
|
|
|
"disableSizeLimit",
|
2022-07-13 19:38:36 +00:00
|
|
|
"downlevelIteration",
|
2020-08-31 18:12:24 +00:00
|
|
|
"emitBOM",
|
2022-07-13 19:38:36 +00:00
|
|
|
"emitDeclarationOnly",
|
|
|
|
"esModuleInterop",
|
2022-07-20 14:59:56 +00:00
|
|
|
"experimentalDecorators",
|
2020-08-31 18:12:24 +00:00
|
|
|
"extendedDiagnostics",
|
|
|
|
"forceConsistentCasingInFileNames",
|
|
|
|
"generateCpuProfile",
|
|
|
|
"help",
|
2022-07-13 19:38:36 +00:00
|
|
|
"importHelpers",
|
2020-08-31 18:12:24 +00:00
|
|
|
"incremental",
|
|
|
|
"init",
|
2022-07-13 19:38:36 +00:00
|
|
|
"inlineSourceMap",
|
|
|
|
"inlineSources",
|
2020-12-31 21:43:54 +00:00
|
|
|
"isolatedModules",
|
2020-08-31 18:12:24 +00:00
|
|
|
"listEmittedFiles",
|
|
|
|
"listFiles",
|
|
|
|
"mapRoot",
|
|
|
|
"maxNodeModuleJsDepth",
|
2022-07-13 19:38:36 +00:00
|
|
|
"module",
|
2022-07-20 14:59:56 +00:00
|
|
|
"moduleDetection",
|
2020-08-31 18:12:24 +00:00
|
|
|
"moduleResolution",
|
|
|
|
"newLine",
|
|
|
|
"noEmit",
|
2022-07-13 19:38:36 +00:00
|
|
|
"noEmitHelpers",
|
2020-08-31 18:12:24 +00:00
|
|
|
"noEmitOnError",
|
2022-07-13 19:38:36 +00:00
|
|
|
"noLib",
|
|
|
|
"noResolve",
|
2020-08-31 18:12:24 +00:00
|
|
|
"out",
|
|
|
|
"outDir",
|
|
|
|
"outFile",
|
2022-07-13 19:38:36 +00:00
|
|
|
"paths",
|
|
|
|
"preserveConstEnums",
|
2020-08-31 18:12:24 +00:00
|
|
|
"preserveSymlinks",
|
|
|
|
"preserveWatchOutput",
|
|
|
|
"pretty",
|
2020-11-02 02:51:56 +00:00
|
|
|
"project",
|
2022-07-13 19:38:36 +00:00
|
|
|
"reactNamespace",
|
2020-08-31 18:12:24 +00:00
|
|
|
"resolveJsonModule",
|
2022-07-13 19:38:36 +00:00
|
|
|
"rootDir",
|
|
|
|
"rootDirs",
|
2020-08-31 18:12:24 +00:00
|
|
|
"showConfig",
|
|
|
|
"skipDefaultLibCheck",
|
2022-07-13 19:38:36 +00:00
|
|
|
"skipLibCheck",
|
|
|
|
"sourceMap",
|
|
|
|
"sourceRoot",
|
2020-08-31 18:12:24 +00:00
|
|
|
"stripInternal",
|
2022-07-13 19:38:36 +00:00
|
|
|
"target",
|
2020-08-31 18:12:24 +00:00
|
|
|
"traceResolution",
|
|
|
|
"tsBuildInfoFile",
|
|
|
|
"typeRoots",
|
2021-04-10 21:56:40 +00:00
|
|
|
"useDefineForClassFields",
|
2020-08-31 18:12:24 +00:00
|
|
|
"version",
|
|
|
|
"watch",
|
|
|
|
];
|
|
|
|
|
|
|
|
/// A function that works like JavaScript's `Object.assign()`.
|
|
|
|
pub fn json_merge(a: &mut Value, b: &Value) {
|
|
|
|
match (a, b) {
|
2023-01-27 15:43:16 +00:00
|
|
|
(&mut Value::Object(ref mut a), Value::Object(b)) => {
|
2020-08-31 18:12:24 +00:00
|
|
|
for (k, v) in b {
|
|
|
|
json_merge(a.entry(k.clone()).or_insert(Value::Null), v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(a, b) => {
|
|
|
|
*a = b.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:03:03 +00:00
|
|
|
fn parse_compiler_options(
|
|
|
|
compiler_options: &HashMap<String, Value>,
|
2021-11-24 20:14:19 +00:00
|
|
|
maybe_specifier: Option<ModuleSpecifier>,
|
2020-10-26 13:03:03 +00:00
|
|
|
) -> Result<(Value, Option<IgnoredCompilerOptions>), AnyError> {
|
|
|
|
let mut filtered: HashMap<String, Value> = HashMap::new();
|
|
|
|
let mut items: Vec<String> = Vec::new();
|
|
|
|
|
|
|
|
for (key, value) in compiler_options.iter() {
|
|
|
|
let key = key.as_str();
|
2023-03-11 16:43:45 +00:00
|
|
|
// We don't pass "types" entries to typescript via the compiler
|
|
|
|
// options and instead provide those to tsc as "roots". This is
|
|
|
|
// because our "types" behavior is at odds with how TypeScript's
|
|
|
|
// "types" works.
|
|
|
|
if key != "types" {
|
|
|
|
if IGNORED_COMPILER_OPTIONS.contains(&key) {
|
|
|
|
items.push(key.to_string());
|
|
|
|
} else {
|
|
|
|
filtered.insert(key.to_string(), value.to_owned());
|
|
|
|
}
|
2020-10-26 13:03:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let value = serde_json::to_value(filtered)?;
|
|
|
|
let maybe_ignored_options = if !items.is_empty() {
|
2021-11-24 20:14:19 +00:00
|
|
|
Some(IgnoredCompilerOptions {
|
|
|
|
items,
|
|
|
|
maybe_specifier,
|
|
|
|
})
|
2020-10-26 13:03:03 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((value, maybe_ignored_options))
|
|
|
|
}
|
|
|
|
|
2020-09-29 07:16:12 +00:00
|
|
|
/// A structure for managing the configuration of TypeScript
|
|
|
|
#[derive(Debug, Clone)]
|
2020-10-20 03:10:42 +00:00
|
|
|
pub struct TsConfig(pub Value);
|
2020-09-29 07:16:12 +00:00
|
|
|
|
|
|
|
impl TsConfig {
|
|
|
|
/// Create a new `TsConfig` with the base being the `value` supplied.
|
|
|
|
pub fn new(value: Value) -> Self {
|
|
|
|
TsConfig(value)
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:46:42 +00:00
|
|
|
pub fn as_bytes(&self) -> Vec<u8> {
|
2023-01-15 04:06:46 +00:00
|
|
|
let map = self.0.as_object().expect("invalid tsconfig");
|
2020-10-29 10:18:18 +00:00
|
|
|
let ordered: BTreeMap<_, _> = map.iter().collect();
|
|
|
|
let value = json!(ordered);
|
|
|
|
value.to_string().as_bytes().to_owned()
|
2020-09-30 11:46:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 00:50:15 +00:00
|
|
|
/// Return the value of the `checkJs` compiler option, defaulting to `false`
|
|
|
|
/// if not present.
|
|
|
|
pub fn get_check_js(&self) -> bool {
|
|
|
|
if let Some(check_js) = self.0.get("checkJs") {
|
|
|
|
check_js.as_bool().unwrap_or(false)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 21:43:54 +00:00
|
|
|
pub fn get_declaration(&self) -> bool {
|
|
|
|
if let Some(declaration) = self.0.get("declaration") {
|
|
|
|
declaration.as_bool().unwrap_or(false)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 00:50:15 +00:00
|
|
|
/// Merge a serde_json value into the configuration.
|
|
|
|
pub fn merge(&mut self, value: &Value) {
|
|
|
|
json_merge(&mut self.0, value);
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:16:39 +00:00
|
|
|
/// Take an optional user provided config file
|
|
|
|
/// which was passed in via the `--config` flag and merge `compilerOptions` with
|
2020-09-29 07:16:12 +00:00
|
|
|
/// the configuration. Returning the result which optionally contains any
|
|
|
|
/// compiler options that were ignored.
|
2021-05-10 16:16:39 +00:00
|
|
|
pub fn merge_tsconfig_from_config_file(
|
2020-09-29 07:16:12 +00:00
|
|
|
&mut self,
|
2021-05-10 16:16:39 +00:00
|
|
|
maybe_config_file: Option<&ConfigFile>,
|
2020-09-29 07:16:12 +00:00
|
|
|
) -> Result<Option<IgnoredCompilerOptions>, AnyError> {
|
2021-05-10 16:16:39 +00:00
|
|
|
if let Some(config_file) = maybe_config_file {
|
2021-09-03 15:01:58 +00:00
|
|
|
let (value, maybe_ignored_options) = config_file.to_compiler_options()?;
|
2021-05-10 16:16:39 +00:00
|
|
|
self.merge(&value);
|
2020-09-29 07:16:12 +00:00
|
|
|
Ok(maybe_ignored_options)
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for TsConfig {
|
|
|
|
/// Serializes inner hash map which is ordered by the key
|
|
|
|
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
Serialize::serialize(&self.0, serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 15:01:58 +00:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize)]
|
|
|
|
#[serde(default, deny_unknown_fields)]
|
|
|
|
pub struct LintRulesConfig {
|
|
|
|
pub tags: Option<Vec<String>>,
|
|
|
|
pub include: Option<Vec<String>>,
|
|
|
|
pub exclude: Option<Vec<String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, Deserialize)]
|
|
|
|
#[serde(default, deny_unknown_fields)]
|
2021-11-23 15:38:11 +00:00
|
|
|
struct SerializedFilesConfig {
|
2021-09-03 15:01:58 +00:00
|
|
|
pub include: Vec<String>,
|
|
|
|
pub exclude: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2021-11-23 15:38:11 +00:00
|
|
|
impl SerializedFilesConfig {
|
2021-11-24 20:14:19 +00:00
|
|
|
pub fn into_resolved(
|
|
|
|
self,
|
|
|
|
config_file_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<FilesConfig, AnyError> {
|
|
|
|
let config_dir = specifier_parent(config_file_specifier);
|
|
|
|
Ok(FilesConfig {
|
2021-11-23 15:38:11 +00:00
|
|
|
include: self
|
|
|
|
.include
|
|
|
|
.into_iter()
|
2023-01-07 20:22:09 +00:00
|
|
|
.map(|p| {
|
|
|
|
let url = config_dir.join(&p)?;
|
|
|
|
specifier_to_file_path(&url)
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?,
|
2021-11-23 15:38:11 +00:00
|
|
|
exclude: self
|
|
|
|
.exclude
|
|
|
|
.into_iter()
|
2023-01-07 20:22:09 +00:00
|
|
|
.map(|p| {
|
|
|
|
let url = config_dir.join(&p)?;
|
|
|
|
specifier_to_file_path(&url)
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?,
|
2021-11-24 20:14:19 +00:00
|
|
|
})
|
2021-11-23 15:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-07 20:22:09 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
2021-11-23 15:38:11 +00:00
|
|
|
pub struct FilesConfig {
|
2023-01-07 20:22:09 +00:00
|
|
|
pub include: Vec<PathBuf>,
|
|
|
|
pub exclude: Vec<PathBuf>,
|
2021-11-24 20:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FilesConfig {
|
|
|
|
/// Gets if the provided specifier is allowed based on the includes
|
|
|
|
/// and excludes in the configuration file.
|
|
|
|
pub fn matches_specifier(&self, specifier: &ModuleSpecifier) -> bool {
|
2023-01-07 20:22:09 +00:00
|
|
|
let file_path = match specifier_to_file_path(specifier) {
|
|
|
|
Ok(file_path) => file_path,
|
|
|
|
Err(_) => return false,
|
|
|
|
};
|
2021-11-24 20:14:19 +00:00
|
|
|
// Skip files which is in the exclude list.
|
2023-01-07 20:22:09 +00:00
|
|
|
if self.exclude.iter().any(|i| file_path.starts_with(i)) {
|
2021-11-24 20:14:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore files not in the include list if it's not empty.
|
|
|
|
self.include.is_empty()
|
2023-01-07 20:22:09 +00:00
|
|
|
|| self.include.iter().any(|i| file_path.starts_with(i))
|
2021-11-24 20:14:19 +00:00
|
|
|
}
|
2021-11-23 15:38:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 15:01:58 +00:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize)]
|
|
|
|
#[serde(default, deny_unknown_fields)]
|
2021-11-23 15:38:11 +00:00
|
|
|
struct SerializedLintConfig {
|
|
|
|
pub rules: LintRulesConfig,
|
|
|
|
pub files: SerializedFilesConfig,
|
2022-10-25 12:21:20 +00:00
|
|
|
pub report: Option<String>,
|
2021-11-23 15:38:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializedLintConfig {
|
2021-11-24 20:14:19 +00:00
|
|
|
pub fn into_resolved(
|
|
|
|
self,
|
|
|
|
config_file_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<LintConfig, AnyError> {
|
|
|
|
Ok(LintConfig {
|
2021-11-23 15:38:11 +00:00
|
|
|
rules: self.rules,
|
2021-11-24 20:14:19 +00:00
|
|
|
files: self.files.into_resolved(config_file_specifier)?,
|
2022-10-25 12:21:20 +00:00
|
|
|
report: self.report,
|
2021-11-24 20:14:19 +00:00
|
|
|
})
|
2021-11-23 15:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
2021-09-03 15:01:58 +00:00
|
|
|
pub struct LintConfig {
|
|
|
|
pub rules: LintRulesConfig,
|
2021-09-13 18:19:10 +00:00
|
|
|
pub files: FilesConfig,
|
2022-10-25 12:21:20 +00:00
|
|
|
pub report: Option<String>,
|
2021-09-13 18:19:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 02:14:00 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
2021-09-13 18:19:10 +00:00
|
|
|
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
|
|
|
pub enum ProseWrap {
|
|
|
|
Always,
|
|
|
|
Never,
|
|
|
|
Preserve,
|
|
|
|
}
|
|
|
|
|
2022-04-20 02:14:00 +00:00
|
|
|
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
2021-09-13 18:19:10 +00:00
|
|
|
#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
|
|
|
|
pub struct FmtOptionsConfig {
|
|
|
|
pub use_tabs: Option<bool>,
|
|
|
|
pub line_width: Option<u32>,
|
|
|
|
pub indent_width: Option<u8>,
|
|
|
|
pub single_quote: Option<bool>,
|
|
|
|
pub prose_wrap: Option<ProseWrap>,
|
2023-01-25 20:06:00 +00:00
|
|
|
pub semi_colons: Option<bool>,
|
2021-09-13 18:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, Deserialize)]
|
|
|
|
#[serde(default, deny_unknown_fields)]
|
2021-11-23 15:38:11 +00:00
|
|
|
struct SerializedFmtConfig {
|
|
|
|
pub options: FmtOptionsConfig,
|
|
|
|
pub files: SerializedFilesConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializedFmtConfig {
|
2021-11-24 20:14:19 +00:00
|
|
|
pub fn into_resolved(
|
|
|
|
self,
|
|
|
|
config_file_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<FmtConfig, AnyError> {
|
|
|
|
Ok(FmtConfig {
|
2021-11-23 15:38:11 +00:00
|
|
|
options: self.options,
|
2021-11-24 20:14:19 +00:00
|
|
|
files: self.files.into_resolved(config_file_specifier)?,
|
|
|
|
})
|
2021-11-23 15:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
2021-09-13 18:19:10 +00:00
|
|
|
pub struct FmtConfig {
|
|
|
|
pub options: FmtOptionsConfig,
|
|
|
|
pub files: FilesConfig,
|
2021-09-03 15:01:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-18 19:12:19 +00:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize)]
|
|
|
|
#[serde(default, deny_unknown_fields)]
|
|
|
|
struct SerializedTestConfig {
|
|
|
|
pub files: SerializedFilesConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializedTestConfig {
|
|
|
|
pub fn into_resolved(
|
|
|
|
self,
|
|
|
|
config_file_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<TestConfig, AnyError> {
|
|
|
|
Ok(TestConfig {
|
|
|
|
files: self.files.into_resolved(config_file_specifier)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct TestConfig {
|
|
|
|
pub files: FilesConfig,
|
|
|
|
}
|
|
|
|
|
2022-12-10 01:30:47 +00:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize)]
|
|
|
|
#[serde(default, deny_unknown_fields)]
|
|
|
|
struct SerializedBenchConfig {
|
|
|
|
pub files: SerializedFilesConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializedBenchConfig {
|
|
|
|
pub fn into_resolved(
|
|
|
|
self,
|
|
|
|
config_file_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<BenchConfig, AnyError> {
|
|
|
|
Ok(BenchConfig {
|
|
|
|
files: self.files.into_resolved(config_file_specifier)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct BenchConfig {
|
|
|
|
pub files: FilesConfig,
|
|
|
|
}
|
|
|
|
|
2022-12-07 23:13:45 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub enum LockConfig {
|
|
|
|
Bool(bool),
|
|
|
|
PathBuf(PathBuf),
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:16:39 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ConfigFileJson {
|
|
|
|
pub compiler_options: Option<Value>,
|
2022-02-22 23:51:14 +00:00
|
|
|
pub import_map: Option<String>,
|
2023-01-25 20:13:40 +00:00
|
|
|
pub imports: Option<Value>,
|
|
|
|
pub scopes: Option<Value>,
|
2021-09-03 15:01:58 +00:00
|
|
|
pub lint: Option<Value>,
|
2021-09-13 18:19:10 +00:00
|
|
|
pub fmt: Option<Value>,
|
2022-03-11 01:56:14 +00:00
|
|
|
pub tasks: Option<Value>,
|
2022-07-18 19:12:19 +00:00
|
|
|
pub test: Option<Value>,
|
2022-12-10 01:30:47 +00:00
|
|
|
pub bench: Option<Value>,
|
2022-12-07 23:13:45 +00:00
|
|
|
pub lock: Option<Value>,
|
2021-05-10 16:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct ConfigFile {
|
2021-11-24 20:14:19 +00:00
|
|
|
pub specifier: ModuleSpecifier,
|
2021-05-10 16:16:39 +00:00
|
|
|
pub json: ConfigFileJson,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfigFile {
|
2023-02-23 01:16:16 +00:00
|
|
|
pub fn discover(
|
|
|
|
flags: &Flags,
|
|
|
|
cwd: &Path,
|
|
|
|
) -> Result<Option<ConfigFile>, AnyError> {
|
2022-06-28 20:45:55 +00:00
|
|
|
match &flags.config_flag {
|
|
|
|
ConfigFlag::Disabled => Ok(None),
|
2023-02-23 01:16:16 +00:00
|
|
|
ConfigFlag::Path(config_path) => {
|
|
|
|
let config_path = PathBuf::from(config_path);
|
|
|
|
let config_path = if config_path.is_absolute() {
|
|
|
|
config_path
|
|
|
|
} else {
|
|
|
|
cwd.join(config_path)
|
|
|
|
};
|
|
|
|
Ok(Some(ConfigFile::read(&config_path)?))
|
|
|
|
}
|
2022-06-28 20:45:55 +00:00
|
|
|
ConfigFlag::Discover => {
|
2023-03-14 01:12:09 +00:00
|
|
|
if let Some(config_path_args) = flags.config_path_args(cwd) {
|
2022-06-28 20:45:55 +00:00
|
|
|
let mut checked = HashSet::new();
|
|
|
|
for f in config_path_args {
|
|
|
|
if let Some(cf) = Self::discover_from(&f, &mut checked)? {
|
|
|
|
return Ok(Some(cf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// From CWD walk up to root looking for deno.json or deno.jsonc
|
2023-02-23 01:16:16 +00:00
|
|
|
Self::discover_from(cwd, &mut checked)
|
2022-06-28 20:45:55 +00:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn discover_from(
|
|
|
|
start: &Path,
|
|
|
|
checked: &mut HashSet<PathBuf>,
|
|
|
|
) -> Result<Option<ConfigFile>, AnyError> {
|
|
|
|
/// Filenames that Deno will recognize when discovering config.
|
|
|
|
const CONFIG_FILE_NAMES: [&str; 2] = ["deno.json", "deno.jsonc"];
|
|
|
|
|
2023-02-23 01:16:16 +00:00
|
|
|
// todo(dsherret): in the future, we should force all callers
|
|
|
|
// to provide a resolved path
|
|
|
|
let start = if start.is_absolute() {
|
|
|
|
Cow::Borrowed(start)
|
|
|
|
} else {
|
|
|
|
Cow::Owned(std::env::current_dir()?.join(start))
|
|
|
|
};
|
|
|
|
|
2022-06-28 20:45:55 +00:00
|
|
|
for ancestor in start.ancestors() {
|
|
|
|
if checked.insert(ancestor.to_path_buf()) {
|
|
|
|
for config_filename in CONFIG_FILE_NAMES {
|
|
|
|
let f = ancestor.join(config_filename);
|
2023-01-24 14:41:22 +00:00
|
|
|
match ConfigFile::read(&f) {
|
2022-06-28 20:45:55 +00:00
|
|
|
Ok(cf) => {
|
2023-01-24 14:41:22 +00:00
|
|
|
log::debug!("Config file found at '{}'", f.display());
|
2022-06-28 20:45:55 +00:00
|
|
|
return Ok(Some(cf));
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
if let Some(ioerr) = e.downcast_ref::<std::io::Error>() {
|
|
|
|
use std::io::ErrorKind::*;
|
|
|
|
match ioerr.kind() {
|
|
|
|
InvalidInput | PermissionDenied | NotFound => {
|
|
|
|
// ok keep going
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(e); // Unknown error. Stop.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(e); // Parse error or something else. Stop.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No config file found.
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2023-02-23 01:16:16 +00:00
|
|
|
pub fn read(config_path: &Path) -> Result<Self, AnyError> {
|
|
|
|
debug_assert!(config_path.is_absolute());
|
2021-05-13 21:56:30 +00:00
|
|
|
|
2022-09-19 14:01:47 +00:00
|
|
|
// perf: Check if the config file exists before canonicalizing path.
|
2023-02-23 01:16:16 +00:00
|
|
|
if !config_path.exists() {
|
2022-09-19 14:01:47 +00:00
|
|
|
return Err(
|
|
|
|
std::io::Error::new(
|
|
|
|
std::io::ErrorKind::InvalidInput,
|
|
|
|
format!(
|
|
|
|
"Could not find the config file: {}",
|
2023-02-23 01:16:16 +00:00
|
|
|
config_path.to_string_lossy()
|
2022-09-19 14:01:47 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-23 01:16:16 +00:00
|
|
|
let config_path = canonicalize_path(config_path).map_err(|_| {
|
2021-05-10 16:16:39 +00:00
|
|
|
std::io::Error::new(
|
|
|
|
std::io::ErrorKind::InvalidInput,
|
|
|
|
format!(
|
|
|
|
"Could not find the config file: {}",
|
2023-02-23 01:16:16 +00:00
|
|
|
config_path.to_string_lossy()
|
2021-05-10 16:16:39 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
})?;
|
2021-11-24 20:14:19 +00:00
|
|
|
let config_specifier = ModuleSpecifier::from_file_path(&config_path)
|
|
|
|
.map_err(|_| {
|
|
|
|
anyhow!(
|
|
|
|
"Could not convert path to specifier. Path: {}",
|
|
|
|
config_path.display()
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
Self::from_specifier(&config_specifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_specifier(specifier: &ModuleSpecifier) -> Result<Self, AnyError> {
|
|
|
|
let config_path = specifier_to_file_path(specifier)?;
|
2022-12-17 22:20:15 +00:00
|
|
|
let config_text = match std::fs::read_to_string(config_path) {
|
2021-11-24 20:14:19 +00:00
|
|
|
Ok(text) => text,
|
|
|
|
Err(err) => bail!(
|
|
|
|
"Error reading config file {}: {}",
|
|
|
|
specifier,
|
|
|
|
err.to_string()
|
|
|
|
),
|
|
|
|
};
|
|
|
|
Self::new(&config_text, specifier)
|
2021-05-10 16:16:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-24 20:14:19 +00:00
|
|
|
pub fn new(
|
|
|
|
text: &str,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<Self, AnyError> {
|
2022-07-27 01:24:56 +00:00
|
|
|
let jsonc =
|
|
|
|
match jsonc_parser::parse_to_serde_value(text, &Default::default()) {
|
|
|
|
Ok(None) => json!({}),
|
|
|
|
Ok(Some(value)) if value.is_object() => value,
|
|
|
|
Ok(Some(_)) => {
|
|
|
|
return Err(anyhow!(
|
2022-08-29 17:13:39 +00:00
|
|
|
"config file JSON {} should be an object",
|
2022-07-27 01:24:56 +00:00
|
|
|
specifier,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
return Err(anyhow!(
|
2022-08-29 17:13:39 +00:00
|
|
|
"Unable to parse config file JSON {} because of {}",
|
2022-07-27 01:24:56 +00:00
|
|
|
specifier,
|
|
|
|
e.to_string()
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2021-05-10 16:16:39 +00:00
|
|
|
let json: ConfigFileJson = serde_json::from_value(jsonc)?;
|
|
|
|
|
|
|
|
Ok(Self {
|
2021-11-24 20:14:19 +00:00
|
|
|
specifier: specifier.to_owned(),
|
2021-05-10 16:16:39 +00:00
|
|
|
json,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-22 13:25:06 +00:00
|
|
|
/// Returns true if the configuration indicates that JavaScript should be
|
|
|
|
/// type checked, otherwise false.
|
|
|
|
pub fn get_check_js(&self) -> bool {
|
|
|
|
self
|
|
|
|
.json
|
|
|
|
.compiler_options
|
|
|
|
.as_ref()
|
2022-02-25 01:03:12 +00:00
|
|
|
.and_then(|co| co.get("checkJs").and_then(|v| v.as_bool()))
|
2021-12-22 13:25:06 +00:00
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:16:39 +00:00
|
|
|
/// Parse `compilerOptions` and return a serde `Value`.
|
|
|
|
/// The result also contains any options that were ignored.
|
2021-09-03 15:01:58 +00:00
|
|
|
pub fn to_compiler_options(
|
2021-05-10 16:16:39 +00:00
|
|
|
&self,
|
|
|
|
) -> Result<(Value, Option<IgnoredCompilerOptions>), AnyError> {
|
|
|
|
if let Some(compiler_options) = self.json.compiler_options.clone() {
|
|
|
|
let options: HashMap<String, Value> =
|
|
|
|
serde_json::from_value(compiler_options)
|
|
|
|
.context("compilerOptions should be an object")?;
|
2022-07-13 19:38:36 +00:00
|
|
|
parse_compiler_options(&options, Some(self.specifier.to_owned()))
|
2021-05-10 16:16:39 +00:00
|
|
|
} else {
|
|
|
|
Ok((json!({}), None))
|
|
|
|
}
|
|
|
|
}
|
2021-09-03 15:01:58 +00:00
|
|
|
|
2022-02-22 23:51:14 +00:00
|
|
|
pub fn to_import_map_path(&self) -> Option<String> {
|
|
|
|
self.json.import_map.clone()
|
|
|
|
}
|
|
|
|
|
2023-01-25 20:13:40 +00:00
|
|
|
pub fn to_import_map_value(&self) -> Value {
|
|
|
|
let mut value = serde_json::Map::with_capacity(2);
|
|
|
|
if let Some(imports) = &self.json.imports {
|
|
|
|
value.insert("imports".to_string(), imports.clone());
|
|
|
|
}
|
|
|
|
if let Some(scopes) = &self.json.scopes {
|
|
|
|
value.insert("scopes".to_string(), scopes.clone());
|
|
|
|
}
|
|
|
|
value.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_an_import_map(&self) -> bool {
|
|
|
|
self.json.imports.is_some() || self.json.scopes.is_some()
|
|
|
|
}
|
|
|
|
|
2023-01-07 20:22:09 +00:00
|
|
|
pub fn to_fmt_config(&self) -> Result<Option<FmtConfig>, AnyError> {
|
|
|
|
if let Some(config) = self.json.fmt.clone() {
|
|
|
|
let fmt_config: SerializedFmtConfig = serde_json::from_value(config)
|
|
|
|
.context("Failed to parse \"fmt\" configuration")?;
|
|
|
|
Ok(Some(fmt_config.into_resolved(&self.specifier)?))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 15:01:58 +00:00
|
|
|
pub fn to_lint_config(&self) -> Result<Option<LintConfig>, AnyError> {
|
|
|
|
if let Some(config) = self.json.lint.clone() {
|
2021-11-23 15:38:11 +00:00
|
|
|
let lint_config: SerializedLintConfig = serde_json::from_value(config)
|
2021-09-03 15:01:58 +00:00
|
|
|
.context("Failed to parse \"lint\" configuration")?;
|
2021-11-24 20:14:19 +00:00
|
|
|
Ok(Some(lint_config.into_resolved(&self.specifier)?))
|
2021-09-03 15:01:58 +00:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
2021-09-13 18:19:10 +00:00
|
|
|
|
2022-07-18 19:12:19 +00:00
|
|
|
pub fn to_test_config(&self) -> Result<Option<TestConfig>, AnyError> {
|
|
|
|
if let Some(config) = self.json.test.clone() {
|
2022-12-10 01:30:47 +00:00
|
|
|
let test_config: SerializedTestConfig = serde_json::from_value(config)
|
2022-07-18 19:12:19 +00:00
|
|
|
.context("Failed to parse \"test\" configuration")?;
|
2022-12-10 01:30:47 +00:00
|
|
|
Ok(Some(test_config.into_resolved(&self.specifier)?))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_bench_config(&self) -> Result<Option<BenchConfig>, AnyError> {
|
|
|
|
if let Some(config) = self.json.bench.clone() {
|
|
|
|
let bench_config: SerializedBenchConfig = serde_json::from_value(config)
|
|
|
|
.context("Failed to parse \"bench\" configuration")?;
|
|
|
|
Ok(Some(bench_config.into_resolved(&self.specifier)?))
|
2022-07-18 19:12:19 +00:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 00:27:43 +00:00
|
|
|
/// Return any tasks that are defined in the configuration file as a sequence
|
|
|
|
/// of JSON objects providing the name of the task and the arguments of the
|
|
|
|
/// task in a detail field.
|
|
|
|
pub fn to_lsp_tasks(&self) -> Option<Value> {
|
|
|
|
let value = self.json.tasks.clone()?;
|
|
|
|
let tasks: BTreeMap<String, String> = serde_json::from_value(value).ok()?;
|
|
|
|
Some(
|
|
|
|
tasks
|
|
|
|
.into_iter()
|
|
|
|
.map(|(key, value)| {
|
|
|
|
json!({
|
|
|
|
"name": key,
|
|
|
|
"detail": value,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-11 01:56:14 +00:00
|
|
|
pub fn to_tasks_config(
|
|
|
|
&self,
|
2023-02-23 03:45:35 +00:00
|
|
|
) -> Result<Option<IndexMap<String, String>>, AnyError> {
|
2022-03-11 01:56:14 +00:00
|
|
|
if let Some(config) = self.json.tasks.clone() {
|
2023-02-23 03:45:35 +00:00
|
|
|
let tasks_config: IndexMap<String, String> =
|
2022-03-11 01:56:14 +00:00
|
|
|
serde_json::from_value(config)
|
|
|
|
.context("Failed to parse \"tasks\" configuration")?;
|
|
|
|
Ok(Some(tasks_config))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 21:26:22 +00:00
|
|
|
/// If the configuration file contains "extra" modules (like TypeScript
|
|
|
|
/// `"types"`) options, return them as imports to be added to a module graph.
|
2021-11-09 01:26:39 +00:00
|
|
|
pub fn to_maybe_imports(&self) -> MaybeImportsResult {
|
|
|
|
let mut imports = Vec::new();
|
|
|
|
let compiler_options_value =
|
|
|
|
if let Some(value) = self.json.compiler_options.as_ref() {
|
|
|
|
value
|
|
|
|
} else {
|
2023-02-10 03:00:23 +00:00
|
|
|
return Ok(Vec::new());
|
2021-11-09 01:26:39 +00:00
|
|
|
};
|
|
|
|
let compiler_options: CompilerOptions =
|
|
|
|
serde_json::from_value(compiler_options_value.clone())?;
|
|
|
|
if let Some(types) = compiler_options.types {
|
|
|
|
imports.extend(types);
|
|
|
|
}
|
|
|
|
if !imports.is_empty() {
|
2021-11-24 20:14:19 +00:00
|
|
|
let referrer = self.specifier.clone();
|
2023-02-10 03:00:23 +00:00
|
|
|
Ok(vec![deno_graph::ReferrerImports { referrer, imports }])
|
2021-11-09 01:26:39 +00:00
|
|
|
} else {
|
2023-02-10 03:00:23 +00:00
|
|
|
Ok(Vec::new())
|
2021-11-09 01:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Based on the compiler options in the configuration file, return the
|
2022-08-24 17:36:05 +00:00
|
|
|
/// JSX import source configuration.
|
|
|
|
pub fn to_maybe_jsx_import_source_config(
|
|
|
|
&self,
|
|
|
|
) -> Option<JsxImportSourceConfig> {
|
2021-10-10 21:26:22 +00:00
|
|
|
let compiler_options_value = self.json.compiler_options.as_ref()?;
|
|
|
|
let compiler_options: CompilerOptions =
|
|
|
|
serde_json::from_value(compiler_options_value.clone()).ok()?;
|
2022-08-24 17:36:05 +00:00
|
|
|
let module = match compiler_options.jsx.as_deref() {
|
2021-11-09 01:26:39 +00:00
|
|
|
Some("react-jsx") => Some("jsx-runtime".to_string()),
|
|
|
|
Some("react-jsxdev") => Some("jsx-dev-runtime".to_string()),
|
|
|
|
_ => None,
|
2022-08-24 17:36:05 +00:00
|
|
|
};
|
|
|
|
module.map(|module| JsxImportSourceConfig {
|
|
|
|
default_specifier: compiler_options.jsx_import_source,
|
|
|
|
module,
|
|
|
|
})
|
2021-10-10 21:26:22 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 20:45:55 +00:00
|
|
|
pub fn resolve_tasks_config(
|
|
|
|
&self,
|
2023-02-23 03:45:35 +00:00
|
|
|
) -> Result<IndexMap<String, String>, AnyError> {
|
2022-06-28 20:45:55 +00:00
|
|
|
let maybe_tasks_config = self.to_tasks_config()?;
|
2023-02-23 03:45:35 +00:00
|
|
|
let tasks_config = maybe_tasks_config.unwrap_or_default();
|
|
|
|
for key in tasks_config.keys() {
|
|
|
|
if key.is_empty() {
|
|
|
|
bail!("Configuration file task names cannot be empty");
|
|
|
|
} else if !key
|
|
|
|
.chars()
|
|
|
|
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | ':'))
|
|
|
|
{
|
|
|
|
bail!("Configuration file task names must only contain alpha-numeric characters, colons (:), underscores (_), or dashes (-). Task: {}", key);
|
|
|
|
} else if !key.chars().next().unwrap().is_ascii_alphabetic() {
|
|
|
|
bail!("Configuration file task names must start with an alphabetic character. Task: {}", key);
|
2022-06-28 20:45:55 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-23 03:45:35 +00:00
|
|
|
Ok(tasks_config)
|
2022-06-28 20:45:55 +00:00
|
|
|
}
|
2022-12-07 23:13:45 +00:00
|
|
|
|
|
|
|
pub fn to_lock_config(&self) -> Result<Option<LockConfig>, AnyError> {
|
|
|
|
if let Some(config) = self.json.lock.clone() {
|
|
|
|
let lock_config: LockConfig = serde_json::from_value(config)
|
|
|
|
.context("Failed to parse \"lock\" configuration")?;
|
|
|
|
Ok(Some(lock_config))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
2021-05-10 16:16:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 23:29:48 +00:00
|
|
|
/// Represents the "default" type library that should be used when type
|
|
|
|
/// checking the code in the module graph. Note that a user provided config
|
|
|
|
/// of `"lib"` would override this value.
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
|
|
|
|
pub enum TsTypeLib {
|
|
|
|
DenoWindow,
|
|
|
|
DenoWorker,
|
|
|
|
UnstableDenoWindow,
|
|
|
|
UnstableDenoWorker,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TsTypeLib {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::DenoWindow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for TsTypeLib {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
let value = match self {
|
|
|
|
Self::DenoWindow => vec!["deno.window".to_string()],
|
|
|
|
Self::DenoWorker => vec!["deno.worker".to_string()],
|
|
|
|
Self::UnstableDenoWindow => {
|
|
|
|
vec!["deno.window".to_string(), "deno.unstable".to_string()]
|
|
|
|
}
|
|
|
|
Self::UnstableDenoWorker => {
|
|
|
|
vec!["deno.worker".to_string(), "deno.unstable".to_string()]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Serialize::serialize(&value, serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An enum that represents the base tsc configuration to return.
|
|
|
|
pub enum TsConfigType {
|
|
|
|
/// Return a configuration for bundling, using swc to emit the bundle. This is
|
|
|
|
/// independent of type checking.
|
|
|
|
Bundle,
|
|
|
|
/// Return a configuration to use tsc to type check. This
|
|
|
|
/// is independent of either bundling or emitting via swc.
|
|
|
|
Check { lib: TsTypeLib },
|
|
|
|
/// Return a configuration to use swc to emit single module files.
|
|
|
|
Emit,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TsConfigForEmit {
|
|
|
|
pub ts_config: TsConfig,
|
|
|
|
pub maybe_ignored_options: Option<IgnoredCompilerOptions>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For a given configuration type and optionally a configuration file,
|
|
|
|
/// return a `TsConfig` struct and optionally any user configuration
|
|
|
|
/// options that were ignored.
|
|
|
|
pub fn get_ts_config_for_emit(
|
|
|
|
config_type: TsConfigType,
|
|
|
|
maybe_config_file: Option<&ConfigFile>,
|
|
|
|
) -> Result<TsConfigForEmit, AnyError> {
|
|
|
|
let mut ts_config = match config_type {
|
|
|
|
TsConfigType::Bundle => TsConfig::new(json!({
|
2023-03-21 15:46:40 +00:00
|
|
|
"allowImportingTsExtensions": true,
|
2022-11-25 23:29:48 +00:00
|
|
|
"checkJs": false,
|
|
|
|
"emitDecoratorMetadata": false,
|
|
|
|
"importsNotUsedAsValues": "remove",
|
|
|
|
"inlineSourceMap": false,
|
|
|
|
"inlineSources": false,
|
|
|
|
"sourceMap": false,
|
|
|
|
"jsx": "react",
|
|
|
|
"jsxFactory": "React.createElement",
|
|
|
|
"jsxFragmentFactory": "React.Fragment",
|
|
|
|
})),
|
|
|
|
TsConfigType::Check { lib } => TsConfig::new(json!({
|
|
|
|
"allowJs": true,
|
2023-03-21 15:46:40 +00:00
|
|
|
"allowImportingTsExtensions": true,
|
2022-11-25 23:29:48 +00:00
|
|
|
"allowSyntheticDefaultImports": true,
|
|
|
|
"checkJs": false,
|
|
|
|
"emitDecoratorMetadata": false,
|
|
|
|
"experimentalDecorators": true,
|
|
|
|
"incremental": true,
|
|
|
|
"jsx": "react",
|
|
|
|
"importsNotUsedAsValues": "remove",
|
|
|
|
"inlineSourceMap": true,
|
|
|
|
"inlineSources": true,
|
|
|
|
"isolatedModules": true,
|
|
|
|
"lib": lib,
|
|
|
|
"module": "esnext",
|
|
|
|
"moduleDetection": "force",
|
|
|
|
"noEmit": true,
|
|
|
|
"resolveJsonModule": true,
|
|
|
|
"sourceMap": false,
|
|
|
|
"strict": true,
|
|
|
|
"target": "esnext",
|
2023-02-05 16:49:20 +00:00
|
|
|
"tsBuildInfoFile": "internal:///.tsbuildinfo",
|
2022-11-25 23:29:48 +00:00
|
|
|
"useDefineForClassFields": true,
|
|
|
|
// TODO(@kitsonk) remove for Deno 2.0
|
|
|
|
"useUnknownInCatchVariables": false,
|
|
|
|
})),
|
|
|
|
TsConfigType::Emit => TsConfig::new(json!({
|
2023-03-21 15:46:40 +00:00
|
|
|
"allowImportingTsExtensions": true,
|
2022-11-25 23:29:48 +00:00
|
|
|
"checkJs": false,
|
|
|
|
"emitDecoratorMetadata": false,
|
|
|
|
"importsNotUsedAsValues": "remove",
|
|
|
|
"inlineSourceMap": true,
|
|
|
|
"inlineSources": true,
|
|
|
|
"sourceMap": false,
|
|
|
|
"jsx": "react",
|
|
|
|
"jsxFactory": "React.createElement",
|
|
|
|
"jsxFragmentFactory": "React.Fragment",
|
|
|
|
"resolveJsonModule": true,
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
let maybe_ignored_options =
|
|
|
|
ts_config.merge_tsconfig_from_config_file(maybe_config_file)?;
|
|
|
|
Ok(TsConfigForEmit {
|
|
|
|
ts_config,
|
|
|
|
maybe_ignored_options,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<TsConfig> for deno_ast::EmitOptions {
|
|
|
|
fn from(config: TsConfig) -> Self {
|
|
|
|
let options: EmitConfigOptions = serde_json::from_value(config.0).unwrap();
|
|
|
|
let imports_not_used_as_values =
|
|
|
|
match options.imports_not_used_as_values.as_str() {
|
|
|
|
"preserve" => deno_ast::ImportsNotUsedAsValues::Preserve,
|
|
|
|
"error" => deno_ast::ImportsNotUsedAsValues::Error,
|
|
|
|
_ => deno_ast::ImportsNotUsedAsValues::Remove,
|
|
|
|
};
|
|
|
|
let (transform_jsx, jsx_automatic, jsx_development) =
|
|
|
|
match options.jsx.as_str() {
|
|
|
|
"react" => (true, false, false),
|
|
|
|
"react-jsx" => (true, true, false),
|
|
|
|
"react-jsxdev" => (true, true, true),
|
|
|
|
_ => (false, false, false),
|
|
|
|
};
|
|
|
|
deno_ast::EmitOptions {
|
|
|
|
emit_metadata: options.emit_decorator_metadata,
|
|
|
|
imports_not_used_as_values,
|
|
|
|
inline_source_map: options.inline_source_map,
|
|
|
|
inline_sources: options.inline_sources,
|
|
|
|
source_map: options.source_map,
|
|
|
|
jsx_automatic,
|
|
|
|
jsx_development,
|
|
|
|
jsx_factory: options.jsx_factory,
|
|
|
|
jsx_fragment_factory: options.jsx_fragment_factory,
|
|
|
|
jsx_import_source: options.jsx_import_source,
|
|
|
|
transform_jsx,
|
|
|
|
var_decl_imports: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 18:12:24 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-09-21 16:36:37 +00:00
|
|
|
use deno_core::serde_json::json;
|
2022-06-28 20:45:55 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2020-08-31 18:12:24 +00:00
|
|
|
|
2021-05-13 21:56:30 +00:00
|
|
|
#[test]
|
|
|
|
fn read_config_file_absolute() {
|
2021-08-11 14:20:47 +00:00
|
|
|
let path = test_util::testdata_path().join("module_graph/tsconfig.json");
|
2023-02-23 01:16:16 +00:00
|
|
|
let config_file = ConfigFile::read(&path).unwrap();
|
2021-05-13 21:56:30 +00:00
|
|
|
assert!(config_file.json.compiler_options.is_some());
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:48:11 +00:00
|
|
|
#[test]
|
|
|
|
fn include_config_path_on_error() {
|
2023-02-23 01:16:16 +00:00
|
|
|
let path = test_util::testdata_path().join("404.json");
|
|
|
|
let error = ConfigFile::read(&path).err().unwrap();
|
2021-05-18 20:48:11 +00:00
|
|
|
assert!(error.to_string().contains("404.json"));
|
|
|
|
}
|
|
|
|
|
2020-08-31 18:12:24 +00:00
|
|
|
#[test]
|
|
|
|
fn test_json_merge() {
|
|
|
|
let mut value_a = json!({
|
|
|
|
"a": true,
|
|
|
|
"b": "c"
|
|
|
|
});
|
|
|
|
let value_b = json!({
|
|
|
|
"b": "d",
|
|
|
|
"e": false,
|
|
|
|
});
|
|
|
|
json_merge(&mut value_a, &value_b);
|
|
|
|
assert_eq!(
|
|
|
|
value_a,
|
|
|
|
json!({
|
|
|
|
"a": true,
|
|
|
|
"b": "d",
|
|
|
|
"e": false,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_config() {
|
|
|
|
let config_text = r#"{
|
|
|
|
"compilerOptions": {
|
|
|
|
"build": true,
|
|
|
|
// comments are allowed
|
|
|
|
"strict": true
|
2021-09-03 15:01:58 +00:00
|
|
|
},
|
|
|
|
"lint": {
|
|
|
|
"files": {
|
|
|
|
"include": ["src/"],
|
|
|
|
"exclude": ["src/testdata/"]
|
|
|
|
},
|
|
|
|
"rules": {
|
|
|
|
"tags": ["recommended"],
|
|
|
|
"include": ["ban-untagged-todo"]
|
|
|
|
}
|
2021-09-13 18:19:10 +00:00
|
|
|
},
|
|
|
|
"fmt": {
|
|
|
|
"files": {
|
|
|
|
"include": ["src/"],
|
|
|
|
"exclude": ["src/testdata/"]
|
|
|
|
},
|
|
|
|
"options": {
|
|
|
|
"useTabs": true,
|
|
|
|
"lineWidth": 80,
|
|
|
|
"indentWidth": 4,
|
|
|
|
"singleQuote": true,
|
|
|
|
"proseWrap": "preserve"
|
|
|
|
}
|
2022-03-11 01:56:14 +00:00
|
|
|
},
|
|
|
|
"tasks": {
|
|
|
|
"build": "deno run --allow-read --allow-write build.ts",
|
|
|
|
"server": "deno run --allow-net --allow-read server.ts"
|
2020-08-31 18:12:24 +00:00
|
|
|
}
|
|
|
|
}"#;
|
2021-11-24 20:14:19 +00:00
|
|
|
let config_dir = ModuleSpecifier::parse("file:///deno/").unwrap();
|
|
|
|
let config_specifier = config_dir.join("tsconfig.json").unwrap();
|
|
|
|
let config_file = ConfigFile::new(config_text, &config_specifier).unwrap();
|
2020-08-31 18:12:24 +00:00
|
|
|
let (options_value, ignored) =
|
2021-09-03 15:01:58 +00:00
|
|
|
config_file.to_compiler_options().expect("error parsing");
|
2020-08-31 18:12:24 +00:00
|
|
|
assert!(options_value.is_object());
|
|
|
|
let options = options_value.as_object().unwrap();
|
|
|
|
assert!(options.contains_key("strict"));
|
|
|
|
assert_eq!(options.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
ignored,
|
2020-09-29 07:16:12 +00:00
|
|
|
Some(IgnoredCompilerOptions {
|
|
|
|
items: vec!["build".to_string()],
|
2021-11-24 20:14:19 +00:00
|
|
|
maybe_specifier: Some(config_specifier),
|
2020-09-29 07:16:12 +00:00
|
|
|
}),
|
2020-08-31 18:12:24 +00:00
|
|
|
);
|
2021-09-03 15:01:58 +00:00
|
|
|
|
|
|
|
let lint_config = config_file
|
|
|
|
.to_lint_config()
|
|
|
|
.expect("error parsing lint object")
|
|
|
|
.expect("lint object should be defined");
|
2023-01-07 20:22:09 +00:00
|
|
|
assert_eq!(lint_config.files.include, vec![PathBuf::from("/deno/src/")]);
|
2021-11-23 15:38:11 +00:00
|
|
|
assert_eq!(
|
|
|
|
lint_config.files.exclude,
|
2023-01-07 20:22:09 +00:00
|
|
|
vec![PathBuf::from("/deno/src/testdata/")]
|
2021-11-23 15:38:11 +00:00
|
|
|
);
|
2021-09-03 15:01:58 +00:00
|
|
|
assert_eq!(
|
|
|
|
lint_config.rules.include,
|
|
|
|
Some(vec!["ban-untagged-todo".to_string()])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
lint_config.rules.tags,
|
|
|
|
Some(vec!["recommended".to_string()])
|
|
|
|
);
|
|
|
|
assert!(lint_config.rules.exclude.is_none());
|
2021-09-13 18:19:10 +00:00
|
|
|
|
|
|
|
let fmt_config = config_file
|
|
|
|
.to_fmt_config()
|
|
|
|
.expect("error parsing fmt object")
|
|
|
|
.expect("fmt object should be defined");
|
2023-01-07 20:22:09 +00:00
|
|
|
assert_eq!(fmt_config.files.include, vec![PathBuf::from("/deno/src/")]);
|
2021-11-23 15:38:11 +00:00
|
|
|
assert_eq!(
|
|
|
|
fmt_config.files.exclude,
|
2023-01-07 20:22:09 +00:00
|
|
|
vec![PathBuf::from("/deno/src/testdata/")],
|
2021-11-23 15:38:11 +00:00
|
|
|
);
|
2021-09-13 18:19:10 +00:00
|
|
|
assert_eq!(fmt_config.options.use_tabs, Some(true));
|
|
|
|
assert_eq!(fmt_config.options.line_width, Some(80));
|
|
|
|
assert_eq!(fmt_config.options.indent_width, Some(4));
|
|
|
|
assert_eq!(fmt_config.options.single_quote, Some(true));
|
2022-03-11 01:56:14 +00:00
|
|
|
|
|
|
|
let tasks_config = config_file.to_tasks_config().unwrap().unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
tasks_config["build"],
|
|
|
|
"deno run --allow-read --allow-write build.ts",
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
tasks_config["server"],
|
|
|
|
"deno run --allow-net --allow-read server.ts"
|
|
|
|
);
|
2020-08-31 18:12:24 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 06:33:33 +00:00
|
|
|
#[test]
|
|
|
|
fn test_parse_config_with_empty_file() {
|
|
|
|
let config_text = "";
|
2021-11-24 20:14:19 +00:00
|
|
|
let config_specifier =
|
|
|
|
ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap();
|
|
|
|
let config_file = ConfigFile::new(config_text, &config_specifier).unwrap();
|
2021-05-27 06:33:33 +00:00
|
|
|
let (options_value, _) =
|
2021-09-03 15:01:58 +00:00
|
|
|
config_file.to_compiler_options().expect("error parsing");
|
2021-05-27 06:33:33 +00:00
|
|
|
assert!(options_value.is_object());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_config_with_commented_file() {
|
|
|
|
let config_text = r#"//{"foo":"bar"}"#;
|
2021-11-24 20:14:19 +00:00
|
|
|
let config_specifier =
|
|
|
|
ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap();
|
|
|
|
let config_file = ConfigFile::new(config_text, &config_specifier).unwrap();
|
2021-05-27 06:33:33 +00:00
|
|
|
let (options_value, _) =
|
2021-09-03 15:01:58 +00:00
|
|
|
config_file.to_compiler_options().expect("error parsing");
|
2021-05-27 06:33:33 +00:00
|
|
|
assert!(options_value.is_object());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_config_with_invalid_file() {
|
|
|
|
let config_text = "{foo:bar}";
|
2021-11-24 20:14:19 +00:00
|
|
|
let config_specifier =
|
|
|
|
ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap();
|
2021-05-27 06:33:33 +00:00
|
|
|
// Emit error: Unable to parse config file JSON "<config_path>" because of Unexpected token on line 1 column 6.
|
2021-11-24 20:14:19 +00:00
|
|
|
assert!(ConfigFile::new(config_text, &config_specifier).is_err());
|
2021-05-27 06:33:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_config_with_not_object_file() {
|
|
|
|
let config_text = "[]";
|
2021-11-24 20:14:19 +00:00
|
|
|
let config_specifier =
|
|
|
|
ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap();
|
2021-05-27 06:33:33 +00:00
|
|
|
// Emit error: config file JSON "<config_path>" should be an object
|
2021-11-24 20:14:19 +00:00
|
|
|
assert!(ConfigFile::new(config_text, &config_specifier).is_err());
|
2021-05-27 06:33:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 10:18:18 +00:00
|
|
|
#[test]
|
|
|
|
fn test_tsconfig_as_bytes() {
|
|
|
|
let mut tsconfig1 = TsConfig::new(json!({
|
|
|
|
"strict": true,
|
|
|
|
"target": "esnext",
|
|
|
|
}));
|
|
|
|
tsconfig1.merge(&json!({
|
|
|
|
"target": "es5",
|
|
|
|
"module": "amd",
|
|
|
|
}));
|
|
|
|
let mut tsconfig2 = TsConfig::new(json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"strict": true,
|
|
|
|
}));
|
|
|
|
tsconfig2.merge(&json!({
|
|
|
|
"module": "amd",
|
|
|
|
"target": "es5",
|
|
|
|
}));
|
|
|
|
assert_eq!(tsconfig1.as_bytes(), tsconfig2.as_bytes());
|
|
|
|
}
|
2022-01-18 01:10:17 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn discover_from_success() {
|
|
|
|
// testdata/fmt/deno.jsonc exists
|
|
|
|
let testdata = test_util::testdata_path();
|
|
|
|
let c_md = testdata.join("fmt/with_config/subdir/c.md");
|
|
|
|
let mut checked = HashSet::new();
|
2022-06-28 20:45:55 +00:00
|
|
|
let config_file = ConfigFile::discover_from(&c_md, &mut checked)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2022-01-18 01:10:17 +00:00
|
|
|
assert!(checked.contains(c_md.parent().unwrap()));
|
|
|
|
assert!(!checked.contains(&testdata));
|
|
|
|
let fmt_config = config_file.to_fmt_config().unwrap().unwrap();
|
|
|
|
let expected_exclude = ModuleSpecifier::from_file_path(
|
|
|
|
testdata.join("fmt/with_config/subdir/b.ts"),
|
|
|
|
)
|
2023-01-07 20:22:09 +00:00
|
|
|
.unwrap()
|
|
|
|
.to_file_path()
|
2022-01-18 01:10:17 +00:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(fmt_config.files.exclude, vec![expected_exclude]);
|
|
|
|
|
|
|
|
// Now add all ancestors of testdata to checked.
|
|
|
|
for a in testdata.ancestors() {
|
|
|
|
checked.insert(a.to_path_buf());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we call discover_from again starting at testdata, we ought to get None.
|
2022-06-28 20:45:55 +00:00
|
|
|
assert!(ConfigFile::discover_from(&testdata, &mut checked)
|
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
2022-01-18 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn discover_from_malformed() {
|
|
|
|
let testdata = test_util::testdata_path();
|
|
|
|
let d = testdata.join("malformed_config/");
|
|
|
|
let mut checked = HashSet::new();
|
2022-06-28 20:45:55 +00:00
|
|
|
let err = ConfigFile::discover_from(&d, &mut checked).unwrap_err();
|
2022-01-18 01:10:17 +00:00
|
|
|
assert!(err.to_string().contains("Unable to parse config file"));
|
|
|
|
}
|
2022-02-22 23:51:14 +00:00
|
|
|
|
|
|
|
#[test]
|
2022-06-28 20:45:55 +00:00
|
|
|
fn task_name_invalid_chars() {
|
|
|
|
run_task_error_test(
|
|
|
|
r#"{
|
|
|
|
"tasks": {
|
|
|
|
"build": "deno test",
|
|
|
|
"some%test": "deno bundle mod.ts"
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
concat!(
|
|
|
|
"Configuration file task names must only contain alpha-numeric ",
|
|
|
|
"characters, colons (:), underscores (_), or dashes (-). Task: some%test",
|
|
|
|
),
|
2022-02-22 23:51:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-06-28 20:45:55 +00:00
|
|
|
fn task_name_non_alpha_starting_char() {
|
|
|
|
run_task_error_test(
|
|
|
|
r#"{
|
|
|
|
"tasks": {
|
|
|
|
"build": "deno test",
|
|
|
|
"1test": "deno bundle mod.ts"
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
concat!(
|
|
|
|
"Configuration file task names must start with an ",
|
|
|
|
"alphabetic character. Task: 1test",
|
|
|
|
),
|
|
|
|
);
|
2022-02-22 23:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-06-28 20:45:55 +00:00
|
|
|
fn task_name_empty() {
|
|
|
|
run_task_error_test(
|
|
|
|
r#"{
|
|
|
|
"tasks": {
|
|
|
|
"build": "deno test",
|
|
|
|
"": "deno bundle mod.ts"
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
"Configuration file task names cannot be empty",
|
|
|
|
);
|
2022-02-22 23:51:14 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 20:45:55 +00:00
|
|
|
fn run_task_error_test(config_text: &str, expected_error: &str) {
|
|
|
|
let config_dir = ModuleSpecifier::parse("file:///deno/").unwrap();
|
|
|
|
let config_specifier = config_dir.join("tsconfig.json").unwrap();
|
|
|
|
let config_file = ConfigFile::new(config_text, &config_specifier).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
config_file
|
|
|
|
.resolve_tasks_config()
|
|
|
|
.err()
|
|
|
|
.unwrap()
|
|
|
|
.to_string(),
|
|
|
|
expected_error,
|
|
|
|
);
|
2022-02-22 23:51:14 +00:00
|
|
|
}
|
2020-08-31 18:12:24 +00:00
|
|
|
}
|