2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-08-31 14:12:24 -04:00
|
|
|
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::ConfigFlag;
|
|
|
|
use crate::args::Flags;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::path::specifier_parent;
|
|
|
|
use crate::util::path::specifier_to_file_path;
|
2021-11-08 20:26:39 -05:00
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::anyhow;
|
2021-11-24 15:14:19 -05:00
|
|
|
use deno_core::anyhow::bail;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::Context;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2021-02-25 15:18:35 -05:00
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde::Serialize;
|
|
|
|
use deno_core::serde::Serializer;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
2020-10-26 09:03:03 -04:00
|
|
|
use deno_core::serde_json::json;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json::Value;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2023-02-22 22:45:35 -05:00
|
|
|
use indexmap::IndexMap;
|
2023-02-22 20:16:16 -05:00
|
|
|
use std::borrow::Cow;
|
2020-10-29 06:18:18 -04:00
|
|
|
use std::collections::BTreeMap;
|
2020-08-31 14:12:24 -04:00
|
|
|
use std::collections::HashMap;
|
2022-01-17 20:10:17 -05:00
|
|
|
use std::collections::HashSet;
|
2020-08-31 14:12:24 -04:00
|
|
|
use std::fmt;
|
2020-09-29 03:16:12 -04:00
|
|
|
use std::path::Path;
|
2022-01-17 20:10:17 -05:00
|
|
|
use std::path::PathBuf;
|
2020-08-31 14:12:24 -04:00
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub type MaybeImportsResult =
|
2023-02-09 22:00:23 -05:00
|
|
|
Result<Vec<deno_graph::ReferrerImports>, AnyError>;
|
2021-11-08 20:26:39 -05:00
|
|
|
|
2023-07-05 12:43:22 -04:00
|
|
|
#[derive(Debug, Clone, Hash)]
|
2022-08-24 13:36:05 -04:00
|
|
|
pub struct JsxImportSourceConfig {
|
|
|
|
pub default_specifier: Option<String>,
|
|
|
|
pub module: String,
|
2023-07-14 18:10:42 -04:00
|
|
|
pub base_url: ModuleSpecifier,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl JsxImportSourceConfig {
|
|
|
|
pub fn maybe_specifier_text(&self) -> Option<String> {
|
|
|
|
self
|
|
|
|
.default_specifier
|
|
|
|
.as_ref()
|
|
|
|
.map(|default_specifier| format!("{}/{}", default_specifier, self.module))
|
|
|
|
}
|
2022-08-24 13:36:05 -04:00
|
|
|
}
|
|
|
|
|
2020-09-29 03:16:12 -04: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-19 23:10:42 -04:00
|
|
|
pub struct EmitConfigOptions {
|
2020-09-29 03:16:12 -04:00
|
|
|
pub check_js: bool,
|
|
|
|
pub emit_decorator_metadata: bool,
|
2021-03-07 15:40:11 -05:00
|
|
|
pub imports_not_used_as_values: String,
|
2020-10-26 09:03:03 -04:00
|
|
|
pub inline_source_map: bool,
|
2021-09-08 00:05:34 -04:00
|
|
|
pub inline_sources: bool,
|
2021-05-19 00:18:01 -04:00
|
|
|
pub source_map: bool,
|
2020-09-29 03:16:12 -04:00
|
|
|
pub jsx: String,
|
|
|
|
pub jsx_factory: String,
|
|
|
|
pub jsx_fragment_factory: String,
|
2021-11-08 20:26:39 -05:00
|
|
|
pub jsx_import_source: Option<String>,
|
2020-09-29 03:16:12 -04:00
|
|
|
}
|
|
|
|
|
2021-06-21 17:18:32 -04: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-08 20:26:39 -05:00
|
|
|
pub jsx: Option<String>,
|
|
|
|
pub jsx_import_source: Option<String>,
|
2021-06-21 17:18:32 -04:00
|
|
|
pub types: Option<Vec<String>>,
|
|
|
|
}
|
|
|
|
|
2020-09-29 03:16:12 -04:00
|
|
|
/// A structure that represents a set of options that were ignored and the
|
|
|
|
/// path those options came from.
|
2022-09-19 04:25:03 -04:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-09-29 03:16:12 -04:00
|
|
|
pub struct IgnoredCompilerOptions {
|
|
|
|
pub items: Vec<String>,
|
2021-11-24 15:14:19 -05:00
|
|
|
pub maybe_specifier: Option<ModuleSpecifier>,
|
2020-09-29 03:16:12 -04:00
|
|
|
}
|
2020-08-31 14:12:24 -04:00
|
|
|
|
|
|
|
impl fmt::Display for IgnoredCompilerOptions {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2020-09-29 03:16:12 -04:00
|
|
|
let mut codes = self.items.clone();
|
2023-01-05 14:29:50 -05:00
|
|
|
codes.sort_unstable();
|
2021-11-24 15:14:19 -05: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 09:03:03 -04:00
|
|
|
} else {
|
|
|
|
write!(f, "Unsupported compiler options provided.\n The following options were ignored:\n {}", codes.join(", "))
|
|
|
|
}
|
2020-08-31 14:12:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 16:43:54 -05: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 14:12:24 -04: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 05:46:39 -05:00
|
|
|
pub const IGNORED_COMPILER_OPTIONS: &[&str] = &[
|
2023-03-21 11:46:40 -04:00
|
|
|
"allowImportingTsExtensions",
|
2020-08-31 14:12:24 -04:00
|
|
|
"allowSyntheticDefaultImports",
|
2020-11-01 21:51:56 -05:00
|
|
|
"allowUmdGlobalAccess",
|
2020-08-31 14:12:24 -04:00
|
|
|
"assumeChangesOnlyAffectDirectDependencies",
|
2022-07-13 15:38:36 -04:00
|
|
|
"baseUrl",
|
2020-08-31 14:12:24 -04:00
|
|
|
"build",
|
2020-11-01 21:51:56 -05:00
|
|
|
"charset",
|
2020-08-31 14:12:24 -04:00
|
|
|
"composite",
|
2022-07-13 15:38:36 -04:00
|
|
|
"declaration",
|
|
|
|
"declarationMap",
|
2020-08-31 14:12:24 -04:00
|
|
|
"diagnostics",
|
2020-11-01 21:51:56 -05:00
|
|
|
"disableSizeLimit",
|
2022-07-13 15:38:36 -04:00
|
|
|
"downlevelIteration",
|
2020-08-31 14:12:24 -04:00
|
|
|
"emitBOM",
|
2022-07-13 15:38:36 -04:00
|
|
|
"emitDeclarationOnly",
|
|
|
|
"esModuleInterop",
|
2022-07-20 10:59:56 -04:00
|
|
|
"experimentalDecorators",
|
2020-08-31 14:12:24 -04:00
|
|
|
"extendedDiagnostics",
|
|
|
|
"forceConsistentCasingInFileNames",
|
|
|
|
"generateCpuProfile",
|
|
|
|
"help",
|
2022-07-13 15:38:36 -04:00
|
|
|
"importHelpers",
|
2020-08-31 14:12:24 -04:00
|
|
|
"incremental",
|
|
|
|
"init",
|
2022-07-13 15:38:36 -04:00
|
|
|
"inlineSourceMap",
|
|
|
|
"inlineSources",
|
2020-12-31 16:43:54 -05:00
|
|
|
"isolatedModules",
|
2020-08-31 14:12:24 -04:00
|
|
|
"listEmittedFiles",
|
|
|
|
"listFiles",
|
|
|
|
"mapRoot",
|
|
|
|
"maxNodeModuleJsDepth",
|
2022-07-13 15:38:36 -04:00
|
|
|
"module",
|
2022-07-20 10:59:56 -04:00
|
|
|
"moduleDetection",
|
2020-08-31 14:12:24 -04:00
|
|
|
"moduleResolution",
|
|
|
|
"newLine",
|
|
|
|
"noEmit",
|
2022-07-13 15:38:36 -04:00
|
|
|
"noEmitHelpers",
|
2020-08-31 14:12:24 -04:00
|
|
|
"noEmitOnError",
|
2022-07-13 15:38:36 -04:00
|
|
|
"noLib",
|
|
|
|
"noResolve",
|
2020-08-31 14:12:24 -04:00
|
|
|
"out",
|
|
|
|
"outDir",
|
|
|
|
"outFile",
|
2022-07-13 15:38:36 -04:00
|
|
|
"paths",
|
|
|
|
"preserveConstEnums",
|
2020-08-31 14:12:24 -04:00
|
|
|
"preserveSymlinks",
|
|
|
|
"preserveWatchOutput",
|
|
|
|
"pretty",
|
2020-11-01 21:51:56 -05:00
|
|
|
"project",
|
2022-07-13 15:38:36 -04:00
|
|
|
"reactNamespace",
|
2020-08-31 14:12:24 -04:00
|
|
|
"resolveJsonModule",
|
2022-07-13 15:38:36 -04:00
|
|
|
"rootDir",
|
|
|
|
"rootDirs",
|
2020-08-31 14:12:24 -04:00
|
|
|
"showConfig",
|
|
|
|
"skipDefaultLibCheck",
|
2022-07-13 15:38:36 -04:00
|
|
|
"skipLibCheck",
|
|
|
|
"sourceMap",
|
|
|
|
"sourceRoot",
|
2020-08-31 14:12:24 -04:00
|
|
|
"stripInternal",
|
2022-07-13 15:38:36 -04:00
|
|
|
"target",
|
2020-08-31 14:12:24 -04:00
|
|
|
"traceResolution",
|
|
|
|
"tsBuildInfoFile",
|
|
|
|
"typeRoots",
|
2021-04-10 17:56:40 -04:00
|
|
|
"useDefineForClassFields",
|
2020-08-31 14:12:24 -04: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 10:43:16 -05:00
|
|
|
(&mut Value::Object(ref mut a), Value::Object(b)) => {
|
2020-08-31 14:12:24 -04: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 09:03:03 -04:00
|
|
|
fn parse_compiler_options(
|
|
|
|
compiler_options: &HashMap<String, Value>,
|
2021-11-24 15:14:19 -05:00
|
|
|
maybe_specifier: Option<ModuleSpecifier>,
|
2020-10-26 09:03:03 -04: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 11:43:45 -05: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 09:03:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let value = serde_json::to_value(filtered)?;
|
|
|
|
let maybe_ignored_options = if !items.is_empty() {
|
2021-11-24 15:14:19 -05:00
|
|
|
Some(IgnoredCompilerOptions {
|
|
|
|
items,
|
|
|
|
maybe_specifier,
|
|
|
|
})
|
2020-10-26 09:03:03 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((value, maybe_ignored_options))
|
|
|
|
}
|
|
|
|
|
2020-09-29 03:16:12 -04:00
|
|
|
/// A structure for managing the configuration of TypeScript
|
|
|
|
#[derive(Debug, Clone)]
|
2020-10-19 23:10:42 -04:00
|
|
|
pub struct TsConfig(pub Value);
|
2020-09-29 03:16:12 -04: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 07:46:42 -04:00
|
|
|
pub fn as_bytes(&self) -> Vec<u8> {
|
2023-01-14 23:06:46 -05:00
|
|
|
let map = self.0.as_object().expect("invalid tsconfig");
|
2020-10-29 06:18:18 -04:00
|
|
|
let ordered: BTreeMap<_, _> = map.iter().collect();
|
|
|
|
let value = json!(ordered);
|
|
|
|
value.to_string().as_bytes().to_owned()
|
2020-09-30 07:46:42 -04:00
|
|
|
}
|
|
|
|
|
2020-10-22 20:50:15 -04: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 16:43:54 -05: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-22 20:50:15 -04: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 12:16:39 -04:00
|
|
|
/// Take an optional user provided config file
|
|
|
|
/// which was passed in via the `--config` flag and merge `compilerOptions` with
|
2020-09-29 03:16:12 -04:00
|
|
|
/// the configuration. Returning the result which optionally contains any
|
|
|
|
/// compiler options that were ignored.
|
2021-05-10 12:16:39 -04:00
|
|
|
pub fn merge_tsconfig_from_config_file(
|
2020-09-29 03:16:12 -04:00
|
|
|
&mut self,
|
2021-05-10 12:16:39 -04:00
|
|
|
maybe_config_file: Option<&ConfigFile>,
|
2020-09-29 03:16:12 -04:00
|
|
|
) -> Result<Option<IgnoredCompilerOptions>, AnyError> {
|
2021-05-10 12:16:39 -04:00
|
|
|
if let Some(config_file) = maybe_config_file {
|
2021-09-03 11:01:58 -04:00
|
|
|
let (value, maybe_ignored_options) = config_file.to_compiler_options()?;
|
2021-05-10 12:16:39 -04:00
|
|
|
self.merge(&value);
|
2020-09-29 03:16:12 -04: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
2021-09-03 11:01:58 -04:00
|
|
|
#[serde(default, deny_unknown_fields)]
|
|
|
|
pub struct LintRulesConfig {
|
|
|
|
pub tags: Option<Vec<String>>,
|
|
|
|
pub include: Option<Vec<String>>,
|
|
|
|
pub exclude: Option<Vec<String>>,
|
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
2021-09-03 11:01:58 -04:00
|
|
|
#[serde(default, deny_unknown_fields)]
|
2021-11-23 10:38:11 -05:00
|
|
|
struct SerializedFilesConfig {
|
2021-09-03 11:01:58 -04:00
|
|
|
pub include: Vec<String>,
|
|
|
|
pub exclude: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2021-11-23 10:38:11 -05:00
|
|
|
impl SerializedFilesConfig {
|
2021-11-24 15:14:19 -05:00
|
|
|
pub fn into_resolved(
|
|
|
|
self,
|
|
|
|
config_file_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<FilesConfig, AnyError> {
|
2023-05-22 21:39:59 -04:00
|
|
|
let config_dir =
|
|
|
|
specifier_to_file_path(&specifier_parent(config_file_specifier))?;
|
2021-11-24 15:14:19 -05:00
|
|
|
Ok(FilesConfig {
|
2021-11-23 10:38:11 -05:00
|
|
|
include: self
|
|
|
|
.include
|
|
|
|
.into_iter()
|
2023-05-22 21:39:59 -04:00
|
|
|
.map(|p| config_dir.join(p))
|
|
|
|
.collect::<Vec<_>>(),
|
2021-11-23 10:38:11 -05:00
|
|
|
exclude: self
|
|
|
|
.exclude
|
|
|
|
.into_iter()
|
2023-05-22 21:39:59 -04:00
|
|
|
.map(|p| config_dir.join(p))
|
|
|
|
.collect::<Vec<_>>(),
|
2021-11-24 15:14:19 -05:00
|
|
|
})
|
2021-11-23 10:38:11 -05:00
|
|
|
}
|
2023-04-26 23:02:36 -04:00
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.include.is_empty() && self.exclude.is_empty()
|
|
|
|
}
|
2021-11-23 10:38:11 -05:00
|
|
|
}
|
|
|
|
|
2023-01-07 15:22:09 -05:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
2021-11-23 10:38:11 -05:00
|
|
|
pub struct FilesConfig {
|
2023-01-07 15:22:09 -05:00
|
|
|
pub include: Vec<PathBuf>,
|
|
|
|
pub exclude: Vec<PathBuf>,
|
2021-11-24 15:14:19 -05: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 15:22:09 -05:00
|
|
|
let file_path = match specifier_to_file_path(specifier) {
|
|
|
|
Ok(file_path) => file_path,
|
|
|
|
Err(_) => return false,
|
|
|
|
};
|
2021-11-24 15:14:19 -05:00
|
|
|
// Skip files which is in the exclude list.
|
2023-01-07 15:22:09 -05:00
|
|
|
if self.exclude.iter().any(|i| file_path.starts_with(i)) {
|
2021-11-24 15:14:19 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore files not in the include list if it's not empty.
|
|
|
|
self.include.is_empty()
|
2023-01-07 15:22:09 -05:00
|
|
|
|| self.include.iter().any(|i| file_path.starts_with(i))
|
2021-11-24 15:14:19 -05:00
|
|
|
}
|
2023-05-18 16:55:10 -04:00
|
|
|
|
|
|
|
fn extend(self, rhs: Self) -> Self {
|
|
|
|
Self {
|
|
|
|
include: [self.include, rhs.include].concat(),
|
|
|
|
exclude: [self.exclude, rhs.exclude].concat(),
|
|
|
|
}
|
|
|
|
}
|
2021-11-23 10:38:11 -05:00
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
/// Choose between flat and nested files configuration.
|
|
|
|
///
|
|
|
|
/// `files` has precedence over `deprecated_files`.
|
|
|
|
/// when `deprecated_files` is present, a warning is logged.
|
|
|
|
///
|
|
|
|
/// caveat: due to default values, it's not possible to distinguish between
|
|
|
|
/// an empty configuration and a configuration with default values.
|
|
|
|
/// `{ "files": {} }` is equivalent to `{ "files": { "include": [], "exclude": [] } }`
|
|
|
|
/// and it wouldn't be able to emit warning for `{ "files": {}, "exclude": [] }`.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `files` - Flat configuration.
|
|
|
|
/// * `deprecated_files` - Nested configuration. ("Files")
|
|
|
|
fn choose_files(
|
|
|
|
files: SerializedFilesConfig,
|
|
|
|
deprecated_files: SerializedFilesConfig,
|
|
|
|
) -> SerializedFilesConfig {
|
|
|
|
const DEPRECATED_FILES: &str =
|
|
|
|
"Warning: \"files\" configuration is deprecated";
|
|
|
|
const FLAT_CONFIG: &str = "\"include\" and \"exclude\"";
|
|
|
|
|
|
|
|
let (files_nonempty, deprecated_files_nonempty) =
|
|
|
|
(!files.is_empty(), !deprecated_files.is_empty());
|
|
|
|
|
|
|
|
match (files_nonempty, deprecated_files_nonempty) {
|
|
|
|
(true, true) => {
|
|
|
|
log::warn!("{DEPRECATED_FILES} and ignored by {FLAT_CONFIG}.");
|
|
|
|
files
|
|
|
|
}
|
|
|
|
(true, false) => files,
|
|
|
|
(false, true) => {
|
|
|
|
log::warn!("{DEPRECATED_FILES}. Please use {FLAT_CONFIG} instead.");
|
|
|
|
deprecated_files
|
|
|
|
}
|
|
|
|
(false, false) => SerializedFilesConfig::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `lint` config representation for serde
|
|
|
|
///
|
|
|
|
/// fields `include` and `exclude` are expanded from [SerializedFilesConfig].
|
|
|
|
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
2021-09-03 11:01:58 -04:00
|
|
|
#[serde(default, deny_unknown_fields)]
|
2021-11-23 10:38:11 -05:00
|
|
|
struct SerializedLintConfig {
|
|
|
|
pub rules: LintRulesConfig,
|
2023-04-26 23:02:36 -04:00
|
|
|
pub include: Vec<String>,
|
|
|
|
pub exclude: Vec<String>,
|
|
|
|
|
|
|
|
#[serde(rename = "files")]
|
|
|
|
pub deprecated_files: SerializedFilesConfig,
|
2022-10-25 08:21:20 -04:00
|
|
|
pub report: Option<String>,
|
2021-11-23 10:38:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializedLintConfig {
|
2021-11-24 15:14:19 -05:00
|
|
|
pub fn into_resolved(
|
|
|
|
self,
|
|
|
|
config_file_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<LintConfig, AnyError> {
|
2023-04-26 23:02:36 -04:00
|
|
|
let (include, exclude) = (self.include, self.exclude);
|
|
|
|
let files = SerializedFilesConfig { include, exclude };
|
|
|
|
|
2021-11-24 15:14:19 -05:00
|
|
|
Ok(LintConfig {
|
2021-11-23 10:38:11 -05:00
|
|
|
rules: self.rules,
|
2023-04-26 23:02:36 -04:00
|
|
|
files: choose_files(files, self.deprecated_files)
|
|
|
|
.into_resolved(config_file_specifier)?,
|
2022-10-25 08:21:20 -04:00
|
|
|
report: self.report,
|
2021-11-24 15:14:19 -05:00
|
|
|
})
|
2021-11-23 10:38:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
2021-09-03 11:01:58 -04:00
|
|
|
pub struct LintConfig {
|
|
|
|
pub rules: LintRulesConfig,
|
2021-09-13 14:19:10 -04:00
|
|
|
pub files: FilesConfig,
|
2022-10-25 08:21:20 -04:00
|
|
|
pub report: Option<String>,
|
2021-09-13 14:19:10 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 16:55:10 -04:00
|
|
|
impl LintConfig {
|
|
|
|
pub fn with_files(self, files: FilesConfig) -> Self {
|
|
|
|
let files = self.files.extend(files);
|
|
|
|
Self { files, ..self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
|
2021-09-13 14:19:10 -04:00
|
|
|
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
|
|
|
pub enum ProseWrap {
|
|
|
|
Always,
|
|
|
|
Never,
|
|
|
|
Preserve,
|
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
|
2021-09-13 14:19:10 -04: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 15:06:00 -05:00
|
|
|
pub semi_colons: Option<bool>,
|
2021-09-13 14:19:10 -04:00
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
impl FmtOptionsConfig {
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.use_tabs.is_none()
|
|
|
|
&& self.line_width.is_none()
|
|
|
|
&& self.indent_width.is_none()
|
|
|
|
&& self.single_quote.is_none()
|
|
|
|
&& self.prose_wrap.is_none()
|
|
|
|
&& self.semi_colons.is_none()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Choose between flat and nested fmt options.
|
|
|
|
///
|
|
|
|
/// `options` has precedence over `deprecated_options`.
|
|
|
|
/// when `deprecated_options` is present, a warning is logged.
|
|
|
|
///
|
|
|
|
/// caveat: due to default values, it's not possible to distinguish between
|
|
|
|
/// an empty configuration and a configuration with default values.
|
|
|
|
/// `{ "fmt": {} } is equivalent to `{ "fmt": { "options": {} } }`
|
|
|
|
/// and it wouldn't be able to emit warning for `{ "fmt": { "options": {}, "semiColons": "false" } }`.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `options` - Flat options.
|
|
|
|
/// * `deprecated_options` - Nested files configuration ("option").
|
|
|
|
fn choose_fmt_options(
|
|
|
|
options: FmtOptionsConfig,
|
|
|
|
deprecated_options: FmtOptionsConfig,
|
|
|
|
) -> FmtOptionsConfig {
|
|
|
|
const DEPRECATED_OPTIONS: &str =
|
|
|
|
"Warning: \"options\" configuration is deprecated";
|
|
|
|
const FLAT_OPTION: &str = "\"flat\" options";
|
|
|
|
|
|
|
|
let (options_nonempty, deprecated_options_nonempty) =
|
|
|
|
(!options.is_empty(), !deprecated_options.is_empty());
|
|
|
|
|
|
|
|
match (options_nonempty, deprecated_options_nonempty) {
|
|
|
|
(true, true) => {
|
|
|
|
log::warn!("{DEPRECATED_OPTIONS} and ignored by {FLAT_OPTION}.");
|
|
|
|
options
|
|
|
|
}
|
|
|
|
(true, false) => options,
|
|
|
|
(false, true) => {
|
|
|
|
log::warn!("{DEPRECATED_OPTIONS}. Please use {FLAT_OPTION} instead.");
|
|
|
|
deprecated_options
|
|
|
|
}
|
|
|
|
(false, false) => FmtOptionsConfig::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `fmt` config representation for serde
|
|
|
|
///
|
|
|
|
/// fields from `use_tabs`..`semi_colons` are expanded from [FmtOptionsConfig].
|
|
|
|
/// fields `include` and `exclude` are expanded from [SerializedFilesConfig].
|
|
|
|
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
|
|
|
#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
|
2021-11-23 10:38:11 -05:00
|
|
|
struct SerializedFmtConfig {
|
2023-04-26 23:02:36 -04:00
|
|
|
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>,
|
|
|
|
pub semi_colons: Option<bool>,
|
|
|
|
#[serde(rename = "options")]
|
|
|
|
pub deprecated_options: FmtOptionsConfig,
|
|
|
|
pub include: Vec<String>,
|
|
|
|
pub exclude: Vec<String>,
|
|
|
|
#[serde(rename = "files")]
|
|
|
|
pub deprecated_files: SerializedFilesConfig,
|
2021-11-23 10:38:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializedFmtConfig {
|
2021-11-24 15:14:19 -05:00
|
|
|
pub fn into_resolved(
|
|
|
|
self,
|
|
|
|
config_file_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<FmtConfig, AnyError> {
|
2023-04-26 23:02:36 -04:00
|
|
|
let (include, exclude) = (self.include, self.exclude);
|
|
|
|
let files = SerializedFilesConfig { include, exclude };
|
|
|
|
let options = FmtOptionsConfig {
|
|
|
|
use_tabs: self.use_tabs,
|
|
|
|
line_width: self.line_width,
|
|
|
|
indent_width: self.indent_width,
|
|
|
|
single_quote: self.single_quote,
|
|
|
|
prose_wrap: self.prose_wrap,
|
|
|
|
semi_colons: self.semi_colons,
|
|
|
|
};
|
|
|
|
|
2021-11-24 15:14:19 -05:00
|
|
|
Ok(FmtConfig {
|
2023-04-26 23:02:36 -04:00
|
|
|
options: choose_fmt_options(options, self.deprecated_options),
|
|
|
|
files: choose_files(files, self.deprecated_files)
|
|
|
|
.into_resolved(config_file_specifier)?,
|
2021-11-24 15:14:19 -05:00
|
|
|
})
|
2021-11-23 10:38:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
2021-09-13 14:19:10 -04:00
|
|
|
pub struct FmtConfig {
|
|
|
|
pub options: FmtOptionsConfig,
|
|
|
|
pub files: FilesConfig,
|
2021-09-03 11:01:58 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 16:55:10 -04:00
|
|
|
impl FmtConfig {
|
|
|
|
pub fn with_files(self, files: FilesConfig) -> Self {
|
|
|
|
let files = self.files.extend(files);
|
|
|
|
Self { files, ..self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
/// `test` config representation for serde
|
|
|
|
///
|
|
|
|
/// fields `include` and `exclude` are expanded from [SerializedFilesConfig].
|
|
|
|
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
2022-07-18 15:12:19 -04:00
|
|
|
#[serde(default, deny_unknown_fields)]
|
|
|
|
struct SerializedTestConfig {
|
2023-04-26 23:02:36 -04:00
|
|
|
pub include: Vec<String>,
|
|
|
|
pub exclude: Vec<String>,
|
|
|
|
#[serde(rename = "files")]
|
|
|
|
pub deprecated_files: SerializedFilesConfig,
|
2022-07-18 15:12:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializedTestConfig {
|
|
|
|
pub fn into_resolved(
|
|
|
|
self,
|
|
|
|
config_file_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<TestConfig, AnyError> {
|
2023-04-26 23:02:36 -04:00
|
|
|
let (include, exclude) = (self.include, self.exclude);
|
|
|
|
let files = SerializedFilesConfig { include, exclude };
|
|
|
|
|
2022-07-18 15:12:19 -04:00
|
|
|
Ok(TestConfig {
|
2023-04-26 23:02:36 -04:00
|
|
|
files: choose_files(files, self.deprecated_files)
|
|
|
|
.into_resolved(config_file_specifier)?,
|
2022-07-18 15:12:19 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
2022-07-18 15:12:19 -04:00
|
|
|
pub struct TestConfig {
|
|
|
|
pub files: FilesConfig,
|
|
|
|
}
|
|
|
|
|
2023-05-18 16:55:10 -04:00
|
|
|
impl TestConfig {
|
|
|
|
pub fn with_files(self, files: FilesConfig) -> Self {
|
|
|
|
let files = self.files.extend(files);
|
|
|
|
Self { files }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
/// `bench` config representation for serde
|
|
|
|
///
|
|
|
|
/// fields `include` and `exclude` are expanded from [SerializedFilesConfig].
|
|
|
|
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
2022-12-09 20:30:47 -05:00
|
|
|
#[serde(default, deny_unknown_fields)]
|
|
|
|
struct SerializedBenchConfig {
|
2023-04-26 23:02:36 -04:00
|
|
|
pub include: Vec<String>,
|
|
|
|
pub exclude: Vec<String>,
|
|
|
|
#[serde(rename = "files")]
|
|
|
|
pub deprecated_files: SerializedFilesConfig,
|
2022-12-09 20:30:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializedBenchConfig {
|
|
|
|
pub fn into_resolved(
|
|
|
|
self,
|
|
|
|
config_file_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<BenchConfig, AnyError> {
|
2023-04-26 23:02:36 -04:00
|
|
|
let (include, exclude) = (self.include, self.exclude);
|
|
|
|
let files = SerializedFilesConfig { include, exclude };
|
|
|
|
|
2022-12-09 20:30:47 -05:00
|
|
|
Ok(BenchConfig {
|
2023-04-26 23:02:36 -04:00
|
|
|
files: choose_files(files, self.deprecated_files)
|
|
|
|
.into_resolved(config_file_specifier)?,
|
2022-12-09 20:30:47 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
2022-12-09 20:30:47 -05:00
|
|
|
pub struct BenchConfig {
|
|
|
|
pub files: FilesConfig,
|
|
|
|
}
|
|
|
|
|
2023-05-18 16:55:10 -04:00
|
|
|
impl BenchConfig {
|
|
|
|
pub fn with_files(self, files: FilesConfig) -> Self {
|
|
|
|
let files = self.files.extend(files);
|
|
|
|
Self { files }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq)]
|
2022-12-07 18:13:45 -05:00
|
|
|
#[serde(untagged)]
|
|
|
|
pub enum LockConfig {
|
|
|
|
Bool(bool),
|
|
|
|
PathBuf(PathBuf),
|
|
|
|
}
|
|
|
|
|
2021-05-10 12:16:39 -04:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ConfigFileJson {
|
|
|
|
pub compiler_options: Option<Value>,
|
2022-02-22 18:51:14 -05:00
|
|
|
pub import_map: Option<String>,
|
2023-01-25 15:13:40 -05:00
|
|
|
pub imports: Option<Value>,
|
|
|
|
pub scopes: Option<Value>,
|
2021-09-03 11:01:58 -04:00
|
|
|
pub lint: Option<Value>,
|
2021-09-13 14:19:10 -04:00
|
|
|
pub fmt: Option<Value>,
|
2022-03-10 20:56:14 -05:00
|
|
|
pub tasks: Option<Value>,
|
2022-07-18 15:12:19 -04:00
|
|
|
pub test: Option<Value>,
|
2022-12-09 20:30:47 -05:00
|
|
|
pub bench: Option<Value>,
|
2022-12-07 18:13:45 -05:00
|
|
|
pub lock: Option<Value>,
|
2023-05-18 16:55:10 -04:00
|
|
|
pub exclude: Option<Value>,
|
2023-05-18 18:10:44 -04:00
|
|
|
pub node_modules_dir: Option<bool>,
|
2021-05-10 12:16:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct ConfigFile {
|
2021-11-24 15:14:19 -05:00
|
|
|
pub specifier: ModuleSpecifier,
|
2023-05-18 18:10:44 -04:00
|
|
|
json: ConfigFileJson,
|
2021-05-10 12:16:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfigFile {
|
2023-02-22 20:16:16 -05:00
|
|
|
pub fn discover(
|
|
|
|
flags: &Flags,
|
|
|
|
cwd: &Path,
|
|
|
|
) -> Result<Option<ConfigFile>, AnyError> {
|
2022-06-28 16:45:55 -04:00
|
|
|
match &flags.config_flag {
|
|
|
|
ConfigFlag::Disabled => Ok(None),
|
2023-02-22 20:16:16 -05: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 16:45:55 -04:00
|
|
|
ConfigFlag::Discover => {
|
2023-03-13 21:12:09 -04:00
|
|
|
if let Some(config_path_args) = flags.config_path_args(cwd) {
|
2022-06-28 16:45:55 -04: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-22 20:16:16 -05:00
|
|
|
Self::discover_from(cwd, &mut checked)
|
2022-06-28 16:45:55 -04:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn discover_from(
|
|
|
|
start: &Path,
|
|
|
|
checked: &mut HashSet<PathBuf>,
|
|
|
|
) -> Result<Option<ConfigFile>, AnyError> {
|
2023-06-09 08:56:11 -04:00
|
|
|
fn is_skippable_err(e: &AnyError) -> bool {
|
|
|
|
if let Some(ioerr) = e.downcast_ref::<std::io::Error>() {
|
|
|
|
use std::io::ErrorKind::*;
|
|
|
|
match ioerr.kind() {
|
|
|
|
InvalidInput | PermissionDenied | NotFound => {
|
|
|
|
// ok keep going
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
const NOT_A_DIRECTORY: i32 = 20;
|
|
|
|
cfg!(unix) && ioerr.raw_os_error() == Some(NOT_A_DIRECTORY)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 16:45:55 -04:00
|
|
|
/// Filenames that Deno will recognize when discovering config.
|
|
|
|
const CONFIG_FILE_NAMES: [&str; 2] = ["deno.json", "deno.jsonc"];
|
|
|
|
|
2023-02-22 20:16:16 -05: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 16:45:55 -04: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 09:41:22 -05:00
|
|
|
match ConfigFile::read(&f) {
|
2022-06-28 16:45:55 -04:00
|
|
|
Ok(cf) => {
|
2023-01-24 09:41:22 -05:00
|
|
|
log::debug!("Config file found at '{}'", f.display());
|
2022-06-28 16:45:55 -04:00
|
|
|
return Ok(Some(cf));
|
|
|
|
}
|
2023-06-09 08:56:11 -04:00
|
|
|
Err(e) if is_skippable_err(&e) => {
|
|
|
|
// ok, keep going
|
|
|
|
}
|
2022-06-28 16:45:55 -04:00
|
|
|
Err(e) => {
|
2023-06-09 08:56:11 -04:00
|
|
|
return Err(e);
|
2022-06-28 16:45:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No config file found.
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2023-02-22 20:16:16 -05:00
|
|
|
pub fn read(config_path: &Path) -> Result<Self, AnyError> {
|
|
|
|
debug_assert!(config_path.is_absolute());
|
2021-05-13 17:56:30 -04:00
|
|
|
|
2023-06-09 08:56:11 -04:00
|
|
|
let specifier =
|
|
|
|
ModuleSpecifier::from_file_path(config_path).map_err(|_| {
|
2021-11-24 15:14:19 -05:00
|
|
|
anyhow!(
|
2023-06-09 08:56:11 -04:00
|
|
|
"Could not convert config file path to specifier. Path: {}",
|
2021-11-24 15:14:19 -05:00
|
|
|
config_path.display()
|
|
|
|
)
|
|
|
|
})?;
|
2023-06-09 08:56:11 -04:00
|
|
|
Self::from_specifier_and_path(specifier, config_path)
|
2021-11-24 15:14:19 -05:00
|
|
|
}
|
|
|
|
|
2023-05-22 21:28:36 -04:00
|
|
|
pub fn from_specifier(specifier: ModuleSpecifier) -> Result<Self, AnyError> {
|
2023-06-09 08:56:11 -04:00
|
|
|
let config_path =
|
|
|
|
specifier_to_file_path(&specifier).with_context(|| {
|
|
|
|
format!("Invalid config file path for '{}'.", specifier)
|
|
|
|
})?;
|
|
|
|
Self::from_specifier_and_path(specifier, &config_path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_specifier_and_path(
|
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
config_path: &Path,
|
|
|
|
) -> Result<Self, AnyError> {
|
|
|
|
let text = std::fs::read_to_string(config_path)
|
|
|
|
.with_context(|| format!("Error reading config file '{}'.", specifier))?;
|
|
|
|
Self::new(&text, specifier)
|
2021-05-10 12:16:39 -04:00
|
|
|
}
|
|
|
|
|
2023-05-22 21:28:36 -04:00
|
|
|
pub fn new(text: &str, specifier: ModuleSpecifier) -> Result<Self, AnyError> {
|
2022-07-26 21:24:56 -04: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 13:13:39 -04:00
|
|
|
"config file JSON {} should be an object",
|
2022-07-26 21:24:56 -04:00
|
|
|
specifier,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
return Err(anyhow!(
|
2022-08-29 13:13:39 -04:00
|
|
|
"Unable to parse config file JSON {} because of {}",
|
2022-07-26 21:24:56 -04:00
|
|
|
specifier,
|
|
|
|
e.to_string()
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2021-05-10 12:16:39 -04:00
|
|
|
let json: ConfigFileJson = serde_json::from_value(jsonc)?;
|
|
|
|
|
2023-05-22 21:28:36 -04:00
|
|
|
Ok(Self { specifier, json })
|
2021-05-10 12:16:39 -04:00
|
|
|
}
|
|
|
|
|
2021-12-22 08:25:06 -05: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-24 20:03:12 -05:00
|
|
|
.and_then(|co| co.get("checkJs").and_then(|v| v.as_bool()))
|
2021-12-22 08:25:06 -05:00
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2021-05-10 12:16:39 -04:00
|
|
|
/// Parse `compilerOptions` and return a serde `Value`.
|
|
|
|
/// The result also contains any options that were ignored.
|
2021-09-03 11:01:58 -04:00
|
|
|
pub fn to_compiler_options(
|
2021-05-10 12:16:39 -04: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 15:38:36 -04:00
|
|
|
parse_compiler_options(&options, Some(self.specifier.to_owned()))
|
2021-05-10 12:16:39 -04:00
|
|
|
} else {
|
|
|
|
Ok((json!({}), None))
|
|
|
|
}
|
|
|
|
}
|
2021-09-03 11:01:58 -04:00
|
|
|
|
2022-02-22 18:51:14 -05:00
|
|
|
pub fn to_import_map_path(&self) -> Option<String> {
|
|
|
|
self.json.import_map.clone()
|
|
|
|
}
|
|
|
|
|
2023-05-18 18:10:44 -04:00
|
|
|
pub fn node_modules_dir(&self) -> Option<bool> {
|
|
|
|
self.json.node_modules_dir
|
|
|
|
}
|
|
|
|
|
2023-01-25 15:13:40 -05: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-05-18 16:55:10 -04:00
|
|
|
pub fn to_files_config(&self) -> Result<Option<FilesConfig>, AnyError> {
|
|
|
|
let exclude: Vec<String> = if let Some(exclude) = self.json.exclude.clone()
|
|
|
|
{
|
|
|
|
serde_json::from_value(exclude)
|
|
|
|
.context("Failed to parse \"exclude\" configuration")?
|
2023-01-07 15:22:09 -05:00
|
|
|
} else {
|
2023-05-18 16:55:10 -04:00
|
|
|
Vec::new()
|
|
|
|
};
|
|
|
|
|
|
|
|
let raw_files_config = SerializedFilesConfig {
|
|
|
|
exclude,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
Ok(Some(raw_files_config.into_resolved(&self.specifier)?))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_fmt_config(&self) -> Result<Option<FmtConfig>, AnyError> {
|
|
|
|
let files_config = self.to_files_config()?;
|
|
|
|
let fmt_config = match self.json.fmt.clone() {
|
|
|
|
Some(config) => {
|
|
|
|
let fmt_config: SerializedFmtConfig = serde_json::from_value(config)
|
|
|
|
.context("Failed to parse \"fmt\" configuration")?;
|
|
|
|
Some(fmt_config.into_resolved(&self.specifier)?)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if files_config.is_none() && fmt_config.is_none() {
|
|
|
|
return Ok(None);
|
2023-01-07 15:22:09 -05:00
|
|
|
}
|
2023-05-18 16:55:10 -04:00
|
|
|
|
|
|
|
let fmt_config = fmt_config.unwrap_or_default();
|
|
|
|
let files_config = files_config.unwrap_or_default();
|
|
|
|
|
|
|
|
Ok(Some(fmt_config.with_files(files_config)))
|
2023-01-07 15:22:09 -05:00
|
|
|
}
|
|
|
|
|
2021-09-03 11:01:58 -04:00
|
|
|
pub fn to_lint_config(&self) -> Result<Option<LintConfig>, AnyError> {
|
2023-05-18 16:55:10 -04:00
|
|
|
let files_config = self.to_files_config()?;
|
|
|
|
let lint_config = match self.json.lint.clone() {
|
|
|
|
Some(config) => {
|
|
|
|
let lint_config: SerializedLintConfig = serde_json::from_value(config)
|
|
|
|
.context("Failed to parse \"lint\" configuration")?;
|
|
|
|
Some(lint_config.into_resolved(&self.specifier)?)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if files_config.is_none() && lint_config.is_none() {
|
|
|
|
return Ok(None);
|
2021-09-03 11:01:58 -04:00
|
|
|
}
|
2023-05-18 16:55:10 -04:00
|
|
|
|
|
|
|
let lint_config = lint_config.unwrap_or_default();
|
|
|
|
let files_config = files_config.unwrap_or_default();
|
|
|
|
|
|
|
|
Ok(Some(lint_config.with_files(files_config)))
|
2021-09-03 11:01:58 -04:00
|
|
|
}
|
2021-09-13 14:19:10 -04:00
|
|
|
|
2022-07-18 15:12:19 -04:00
|
|
|
pub fn to_test_config(&self) -> Result<Option<TestConfig>, AnyError> {
|
2023-05-18 16:55:10 -04:00
|
|
|
let files_config = self.to_files_config()?;
|
|
|
|
let test_config = match self.json.test.clone() {
|
|
|
|
Some(config) => {
|
|
|
|
let test_config: SerializedTestConfig = serde_json::from_value(config)
|
|
|
|
.context("Failed to parse \"test\" configuration")?;
|
|
|
|
Some(test_config.into_resolved(&self.specifier)?)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if files_config.is_none() && test_config.is_none() {
|
|
|
|
return Ok(None);
|
2022-12-09 20:30:47 -05:00
|
|
|
}
|
2023-05-18 16:55:10 -04:00
|
|
|
|
|
|
|
let test_config = test_config.unwrap_or_default();
|
|
|
|
let files_config = files_config.unwrap_or_default();
|
|
|
|
|
|
|
|
Ok(Some(test_config.with_files(files_config)))
|
2022-12-09 20:30:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_bench_config(&self) -> Result<Option<BenchConfig>, AnyError> {
|
2023-05-18 16:55:10 -04:00
|
|
|
let files_config = self.to_files_config()?;
|
|
|
|
let bench_config = match self.json.bench.clone() {
|
|
|
|
Some(config) => {
|
|
|
|
let bench_config: SerializedBenchConfig =
|
|
|
|
serde_json::from_value(config)
|
|
|
|
.context("Failed to parse \"bench\" configuration")?;
|
|
|
|
Some(bench_config.into_resolved(&self.specifier)?)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if files_config.is_none() && bench_config.is_none() {
|
|
|
|
return Ok(None);
|
2022-07-18 15:12:19 -04:00
|
|
|
}
|
2023-05-18 16:55:10 -04:00
|
|
|
|
|
|
|
let bench_config = bench_config.unwrap_or_default();
|
|
|
|
let files_config = files_config.unwrap_or_default();
|
|
|
|
|
|
|
|
Ok(Some(bench_config.with_files(files_config)))
|
2022-07-18 15:12:19 -04:00
|
|
|
}
|
|
|
|
|
2022-03-28 20:27:43 -04: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-10 20:56:14 -05:00
|
|
|
pub fn to_tasks_config(
|
|
|
|
&self,
|
2023-02-22 22:45:35 -05:00
|
|
|
) -> Result<Option<IndexMap<String, String>>, AnyError> {
|
2022-03-10 20:56:14 -05:00
|
|
|
if let Some(config) = self.json.tasks.clone() {
|
2023-02-22 22:45:35 -05:00
|
|
|
let tasks_config: IndexMap<String, String> =
|
2022-03-10 20:56:14 -05:00
|
|
|
serde_json::from_value(config)
|
|
|
|
.context("Failed to parse \"tasks\" configuration")?;
|
|
|
|
Ok(Some(tasks_config))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:26:22 -04: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-08 20:26:39 -05: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-09 22:00:23 -05:00
|
|
|
return Ok(Vec::new());
|
2021-11-08 20:26:39 -05: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 15:14:19 -05:00
|
|
|
let referrer = self.specifier.clone();
|
2023-02-09 22:00:23 -05:00
|
|
|
Ok(vec![deno_graph::ReferrerImports { referrer, imports }])
|
2021-11-08 20:26:39 -05:00
|
|
|
} else {
|
2023-02-09 22:00:23 -05:00
|
|
|
Ok(Vec::new())
|
2021-11-08 20:26:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Based on the compiler options in the configuration file, return the
|
2022-08-24 13:36:05 -04:00
|
|
|
/// JSX import source configuration.
|
|
|
|
pub fn to_maybe_jsx_import_source_config(
|
|
|
|
&self,
|
|
|
|
) -> Option<JsxImportSourceConfig> {
|
2021-10-10 17:26:22 -04: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 13:36:05 -04:00
|
|
|
let module = match compiler_options.jsx.as_deref() {
|
2021-11-08 20:26:39 -05:00
|
|
|
Some("react-jsx") => Some("jsx-runtime".to_string()),
|
|
|
|
Some("react-jsxdev") => Some("jsx-dev-runtime".to_string()),
|
|
|
|
_ => None,
|
2022-08-24 13:36:05 -04:00
|
|
|
};
|
|
|
|
module.map(|module| JsxImportSourceConfig {
|
|
|
|
default_specifier: compiler_options.jsx_import_source,
|
|
|
|
module,
|
2023-07-14 18:10:42 -04:00
|
|
|
base_url: self.specifier.clone(),
|
2022-08-24 13:36:05 -04:00
|
|
|
})
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 16:45:55 -04:00
|
|
|
pub fn resolve_tasks_config(
|
|
|
|
&self,
|
2023-02-22 22:45:35 -05:00
|
|
|
) -> Result<IndexMap<String, String>, AnyError> {
|
2022-06-28 16:45:55 -04:00
|
|
|
let maybe_tasks_config = self.to_tasks_config()?;
|
2023-02-22 22:45:35 -05: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 16:45:55 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-22 22:45:35 -05:00
|
|
|
Ok(tasks_config)
|
2022-06-28 16:45:55 -04:00
|
|
|
}
|
2022-12-07 18:13:45 -05: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)
|
|
|
|
}
|
|
|
|
}
|
2023-05-22 21:28:36 -04:00
|
|
|
|
|
|
|
pub fn resolve_lockfile_path(&self) -> Result<Option<PathBuf>, AnyError> {
|
|
|
|
match self.to_lock_config()? {
|
|
|
|
Some(LockConfig::Bool(lock)) if !lock => Ok(None),
|
|
|
|
Some(LockConfig::PathBuf(lock)) => Ok(Some(
|
|
|
|
self
|
|
|
|
.specifier
|
|
|
|
.to_file_path()
|
|
|
|
.unwrap()
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join(lock),
|
|
|
|
)),
|
|
|
|
_ => {
|
|
|
|
let mut path = self.specifier.to_file_path().unwrap();
|
|
|
|
path.set_file_name("deno.lock");
|
|
|
|
Ok(Some(path))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-10 12:16:39 -04:00
|
|
|
}
|
|
|
|
|
2022-11-25 18:29:48 -05: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 11:46:40 -04:00
|
|
|
"allowImportingTsExtensions": true,
|
2022-11-25 18:29:48 -05: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 11:46:40 -04:00
|
|
|
"allowImportingTsExtensions": true,
|
2022-11-25 18:29:48 -05: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 11:49:20 -05:00
|
|
|
"tsBuildInfoFile": "internal:///.tsbuildinfo",
|
2022-11-25 18:29:48 -05:00
|
|
|
"useDefineForClassFields": true,
|
|
|
|
// TODO(@kitsonk) remove for Deno 2.0
|
|
|
|
"useUnknownInCatchVariables": false,
|
|
|
|
})),
|
|
|
|
TsConfigType::Emit => TsConfig::new(json!({
|
2023-03-21 11:46:40 -04:00
|
|
|
"allowImportingTsExtensions": true,
|
2022-11-25 18:29:48 -05: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 14:12:24 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json::json;
|
2022-06-28 16:45:55 -04:00
|
|
|
use pretty_assertions::assert_eq;
|
2020-08-31 14:12:24 -04:00
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
fn unpack_object<T>(result: Result<Option<T>, AnyError>, name: &str) -> T {
|
|
|
|
result
|
|
|
|
.unwrap_or_else(|err| panic!("error parsing {name} object but got {err}"))
|
|
|
|
.unwrap_or_else(|| panic!("{name} object should be defined"))
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:56:30 -04:00
|
|
|
#[test]
|
|
|
|
fn read_config_file_absolute() {
|
2021-08-11 10:20:47 -04:00
|
|
|
let path = test_util::testdata_path().join("module_graph/tsconfig.json");
|
2023-06-10 11:09:45 -04:00
|
|
|
let config_file = ConfigFile::read(path.as_path()).unwrap();
|
2021-05-13 17:56:30 -04:00
|
|
|
assert!(config_file.json.compiler_options.is_some());
|
|
|
|
}
|
|
|
|
|
2021-05-18 16:48:11 -04:00
|
|
|
#[test]
|
|
|
|
fn include_config_path_on_error() {
|
2023-02-22 20:16:16 -05:00
|
|
|
let path = test_util::testdata_path().join("404.json");
|
2023-06-10 11:09:45 -04:00
|
|
|
let error = ConfigFile::read(path.as_path()).err().unwrap();
|
2021-05-18 16:48:11 -04:00
|
|
|
assert!(error.to_string().contains("404.json"));
|
|
|
|
}
|
|
|
|
|
2020-08-31 14:12:24 -04: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 11:01:58 -04:00
|
|
|
},
|
|
|
|
"lint": {
|
2023-04-26 23:02:36 -04:00
|
|
|
"include": ["src/"],
|
|
|
|
"exclude": ["src/testdata/"],
|
2021-09-03 11:01:58 -04:00
|
|
|
"rules": {
|
|
|
|
"tags": ["recommended"],
|
|
|
|
"include": ["ban-untagged-todo"]
|
|
|
|
}
|
2021-09-13 14:19:10 -04:00
|
|
|
},
|
|
|
|
"fmt": {
|
2023-04-26 23:02:36 -04:00
|
|
|
"include": ["src/"],
|
|
|
|
"exclude": ["src/testdata/"],
|
|
|
|
"useTabs": true,
|
|
|
|
"lineWidth": 80,
|
|
|
|
"indentWidth": 4,
|
|
|
|
"singleQuote": true,
|
|
|
|
"proseWrap": "preserve"
|
2022-03-10 20:56:14 -05:00
|
|
|
},
|
|
|
|
"tasks": {
|
|
|
|
"build": "deno run --allow-read --allow-write build.ts",
|
|
|
|
"server": "deno run --allow-net --allow-read server.ts"
|
2020-08-31 14:12:24 -04:00
|
|
|
}
|
|
|
|
}"#;
|
2021-11-24 15:14:19 -05:00
|
|
|
let config_dir = ModuleSpecifier::parse("file:///deno/").unwrap();
|
|
|
|
let config_specifier = config_dir.join("tsconfig.json").unwrap();
|
2023-05-22 21:28:36 -04:00
|
|
|
let config_file =
|
|
|
|
ConfigFile::new(config_text, config_specifier.clone()).unwrap();
|
2023-05-18 16:55:10 -04:00
|
|
|
let (options_value, ignored) = config_file.to_compiler_options().unwrap();
|
2020-08-31 14:12:24 -04: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 03:16:12 -04:00
|
|
|
Some(IgnoredCompilerOptions {
|
|
|
|
items: vec!["build".to_string()],
|
2021-11-24 15:14:19 -05:00
|
|
|
maybe_specifier: Some(config_specifier),
|
2020-09-29 03:16:12 -04:00
|
|
|
}),
|
2020-08-31 14:12:24 -04:00
|
|
|
);
|
2021-09-03 11:01:58 -04:00
|
|
|
|
2021-11-23 10:38:11 -05:00
|
|
|
assert_eq!(
|
2023-04-26 23:02:36 -04:00
|
|
|
unpack_object(config_file.to_lint_config(), "lint"),
|
|
|
|
LintConfig {
|
|
|
|
files: FilesConfig {
|
|
|
|
include: vec![PathBuf::from("/deno/src/")],
|
|
|
|
exclude: vec![PathBuf::from("/deno/src/testdata/")],
|
|
|
|
},
|
|
|
|
rules: LintRulesConfig {
|
|
|
|
include: Some(vec!["ban-untagged-todo".to_string()]),
|
|
|
|
exclude: None,
|
|
|
|
tags: Some(vec!["recommended".to_string()]),
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
}
|
2021-09-03 11:01:58 -04:00
|
|
|
);
|
2021-11-23 10:38:11 -05:00
|
|
|
assert_eq!(
|
2023-04-26 23:02:36 -04:00
|
|
|
unpack_object(config_file.to_fmt_config(), "fmt"),
|
|
|
|
FmtConfig {
|
|
|
|
files: FilesConfig {
|
|
|
|
include: vec![PathBuf::from("/deno/src/")],
|
|
|
|
exclude: vec![PathBuf::from("/deno/src/testdata/")],
|
|
|
|
},
|
|
|
|
options: FmtOptionsConfig {
|
|
|
|
use_tabs: Some(true),
|
|
|
|
line_width: Some(80),
|
|
|
|
indent_width: Some(4),
|
|
|
|
single_quote: Some(true),
|
|
|
|
prose_wrap: Some(ProseWrap::Preserve),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
}
|
2021-11-23 10:38:11 -05:00
|
|
|
);
|
2022-03-10 20:56:14 -05: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 14:12:24 -04:00
|
|
|
}
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
/// if either "include" or "exclude" is specified, "files" is ignored
|
|
|
|
#[test]
|
|
|
|
fn test_parse_config_with_deprecated_files_field() {
|
|
|
|
let config_text = r#"{
|
|
|
|
"lint": {
|
|
|
|
"files": { "include": ["foo/"], "exclude": ["bar/"] },
|
|
|
|
"include": ["src/"]
|
|
|
|
},
|
|
|
|
"fmt": {
|
|
|
|
"files": { "include": ["foo/"], "exclude": ["bar/"] },
|
|
|
|
"exclude": ["dist/"]
|
|
|
|
},
|
|
|
|
"bench": {
|
|
|
|
"files": { "include": ["foo/"] },
|
|
|
|
"include": ["src/"]
|
|
|
|
},
|
|
|
|
"test": {
|
|
|
|
"files": { "include": ["foo/"] },
|
|
|
|
"include": ["src/"]
|
|
|
|
}
|
|
|
|
}"#;
|
|
|
|
let config_dir = ModuleSpecifier::parse("file:///deno/").unwrap();
|
|
|
|
let config_specifier = config_dir.join("tsconfig.json").unwrap();
|
2023-05-22 21:28:36 -04:00
|
|
|
let config_file = ConfigFile::new(config_text, config_specifier).unwrap();
|
2023-04-26 23:02:36 -04:00
|
|
|
|
|
|
|
let lint_files = unpack_object(config_file.to_lint_config(), "lint").files;
|
|
|
|
assert_eq!(
|
|
|
|
lint_files,
|
|
|
|
FilesConfig {
|
|
|
|
include: vec![PathBuf::from("/deno/src/")],
|
|
|
|
exclude: vec![],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
let fmt_files = unpack_object(config_file.to_fmt_config(), "fmt").files;
|
|
|
|
assert_eq!(
|
|
|
|
fmt_files,
|
|
|
|
FilesConfig {
|
|
|
|
exclude: vec![PathBuf::from("/deno/dist/")],
|
|
|
|
include: vec![],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
let test_include = unpack_object(config_file.to_test_config(), "test")
|
|
|
|
.files
|
|
|
|
.include;
|
|
|
|
assert_eq!(test_include, vec![PathBuf::from("/deno/src/")]);
|
|
|
|
|
|
|
|
let bench_include = unpack_object(config_file.to_bench_config(), "bench")
|
|
|
|
.files
|
|
|
|
.include;
|
|
|
|
assert_eq!(bench_include, vec![PathBuf::from("/deno/src/")]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_config_with_deprecated_files_field_only() {
|
|
|
|
let config_text = r#"{
|
|
|
|
"lint": { "files": { "include": ["src/"] } },
|
|
|
|
"fmt": { "files": { "include": ["src/"] } },
|
|
|
|
"test": { "files": { "exclude": ["dist/"] } },
|
|
|
|
"bench": { "files": { "exclude": ["dist/"] } }
|
|
|
|
}"#;
|
|
|
|
let config_dir = ModuleSpecifier::parse("file:///deno/").unwrap();
|
|
|
|
let config_specifier = config_dir.join("tsconfig.json").unwrap();
|
2023-05-22 21:28:36 -04:00
|
|
|
let config_file = ConfigFile::new(config_text, config_specifier).unwrap();
|
2023-04-26 23:02:36 -04:00
|
|
|
|
|
|
|
let lint_include = unpack_object(config_file.to_lint_config(), "lint")
|
|
|
|
.files
|
|
|
|
.include;
|
|
|
|
assert_eq!(lint_include, vec![PathBuf::from("/deno/src/")]);
|
|
|
|
|
|
|
|
let fmt_include = unpack_object(config_file.to_fmt_config(), "fmt")
|
|
|
|
.files
|
|
|
|
.include;
|
|
|
|
assert_eq!(fmt_include, vec![PathBuf::from("/deno/src/")]);
|
|
|
|
|
|
|
|
let test_exclude = unpack_object(config_file.to_test_config(), "test")
|
|
|
|
.files
|
|
|
|
.exclude;
|
|
|
|
assert_eq!(test_exclude, vec![PathBuf::from("/deno/dist/")]);
|
|
|
|
|
|
|
|
let bench_exclude = unpack_object(config_file.to_bench_config(), "bench")
|
|
|
|
.files
|
|
|
|
.exclude;
|
|
|
|
assert_eq!(bench_exclude, vec![PathBuf::from("/deno/dist/")]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_config_with_deprecated_fmt_options() {
|
|
|
|
let config_text_both = r#"{
|
|
|
|
"fmt": {
|
|
|
|
"options": {
|
|
|
|
"semiColons": true
|
|
|
|
},
|
|
|
|
"semiColons": false
|
|
|
|
}
|
|
|
|
}"#;
|
|
|
|
let config_text_deprecated = r#"{
|
|
|
|
"fmt": {
|
|
|
|
"options": {
|
|
|
|
"semiColons": true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#;
|
|
|
|
let config_specifier =
|
|
|
|
ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap();
|
|
|
|
let config_file_both =
|
2023-05-22 21:28:36 -04:00
|
|
|
ConfigFile::new(config_text_both, config_specifier.clone()).unwrap();
|
2023-04-26 23:02:36 -04:00
|
|
|
let config_file_deprecated =
|
2023-05-22 21:28:36 -04:00
|
|
|
ConfigFile::new(config_text_deprecated, config_specifier).unwrap();
|
2023-04-26 23:02:36 -04:00
|
|
|
|
|
|
|
fn unpack_options(config_file: ConfigFile) -> FmtOptionsConfig {
|
|
|
|
unpack_object(config_file.to_fmt_config(), "fmt").options
|
|
|
|
}
|
|
|
|
|
|
|
|
let fmt_options_both = unpack_options(config_file_both);
|
|
|
|
assert_eq!(fmt_options_both.semi_colons, Some(false));
|
|
|
|
|
|
|
|
let fmt_options_deprecated = unpack_options(config_file_deprecated);
|
|
|
|
assert_eq!(fmt_options_deprecated.semi_colons, Some(true));
|
|
|
|
}
|
|
|
|
|
2021-05-27 02:33:33 -04:00
|
|
|
#[test]
|
|
|
|
fn test_parse_config_with_empty_file() {
|
|
|
|
let config_text = "";
|
2021-11-24 15:14:19 -05:00
|
|
|
let config_specifier =
|
|
|
|
ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap();
|
2023-05-22 21:28:36 -04:00
|
|
|
let config_file = ConfigFile::new(config_text, config_specifier).unwrap();
|
2023-05-18 16:55:10 -04:00
|
|
|
let (options_value, _) = config_file.to_compiler_options().unwrap();
|
2021-05-27 02:33:33 -04:00
|
|
|
assert!(options_value.is_object());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_config_with_commented_file() {
|
|
|
|
let config_text = r#"//{"foo":"bar"}"#;
|
2021-11-24 15:14:19 -05:00
|
|
|
let config_specifier =
|
|
|
|
ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap();
|
2023-05-22 21:28:36 -04:00
|
|
|
let config_file = ConfigFile::new(config_text, config_specifier).unwrap();
|
2023-05-18 16:55:10 -04:00
|
|
|
let (options_value, _) = config_file.to_compiler_options().unwrap();
|
|
|
|
assert!(options_value.is_object());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_config_with_global_files() {
|
|
|
|
let config_text = r#"{
|
|
|
|
"exclude": ["foo/"],
|
|
|
|
"test": {
|
|
|
|
"exclude": ["npm/"],
|
|
|
|
},
|
|
|
|
"bench": {}
|
|
|
|
}"#;
|
|
|
|
let config_specifier =
|
|
|
|
ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap();
|
2023-05-22 21:28:36 -04:00
|
|
|
let config_file = ConfigFile::new(config_text, config_specifier).unwrap();
|
2023-05-18 16:55:10 -04:00
|
|
|
|
|
|
|
let (options_value, _) = config_file.to_compiler_options().unwrap();
|
|
|
|
assert!(options_value.is_object());
|
|
|
|
|
|
|
|
let test_config = config_file.to_test_config().unwrap().unwrap();
|
|
|
|
assert_eq!(test_config.files.include, Vec::<PathBuf>::new());
|
|
|
|
assert_eq!(
|
|
|
|
test_config.files.exclude,
|
|
|
|
vec![PathBuf::from("/deno/npm/"), PathBuf::from("/deno/foo/")]
|
|
|
|
);
|
|
|
|
|
|
|
|
let bench_config = config_file.to_bench_config().unwrap().unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
bench_config.files.exclude,
|
|
|
|
vec![PathBuf::from("/deno/foo/")]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_config_with_global_files_only() {
|
|
|
|
let config_text = r#"{
|
|
|
|
"exclude": ["npm/"]
|
|
|
|
}"#;
|
|
|
|
let config_specifier =
|
|
|
|
ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap();
|
2023-05-22 21:28:36 -04:00
|
|
|
let config_file = ConfigFile::new(config_text, config_specifier).unwrap();
|
2023-05-18 16:55:10 -04:00
|
|
|
|
|
|
|
let (options_value, _) = config_file.to_compiler_options().unwrap();
|
2021-05-27 02:33:33 -04:00
|
|
|
assert!(options_value.is_object());
|
2023-05-18 16:55:10 -04:00
|
|
|
|
|
|
|
let empty_include = Vec::<PathBuf>::new();
|
|
|
|
|
|
|
|
let files_config = config_file.to_files_config().unwrap().unwrap();
|
|
|
|
assert_eq!(files_config.include, empty_include);
|
|
|
|
assert_eq!(files_config.exclude, vec![PathBuf::from("/deno/npm/")]);
|
|
|
|
|
|
|
|
let lint_config = config_file.to_lint_config().unwrap().unwrap();
|
|
|
|
assert_eq!(lint_config.files.include, empty_include);
|
|
|
|
assert_eq!(lint_config.files.exclude, vec![PathBuf::from("/deno/npm/")]);
|
|
|
|
|
|
|
|
let fmt_config = config_file.to_fmt_config().unwrap().unwrap();
|
|
|
|
assert_eq!(fmt_config.files.include, empty_include);
|
|
|
|
assert_eq!(fmt_config.files.exclude, vec![PathBuf::from("/deno/npm/")],);
|
2021-05-27 02:33:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_config_with_invalid_file() {
|
|
|
|
let config_text = "{foo:bar}";
|
2021-11-24 15:14:19 -05:00
|
|
|
let config_specifier =
|
|
|
|
ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap();
|
2021-05-27 02:33:33 -04:00
|
|
|
// Emit error: Unable to parse config file JSON "<config_path>" because of Unexpected token on line 1 column 6.
|
2023-05-22 21:28:36 -04:00
|
|
|
assert!(ConfigFile::new(config_text, config_specifier).is_err());
|
2021-05-27 02:33:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_config_with_not_object_file() {
|
|
|
|
let config_text = "[]";
|
2021-11-24 15:14:19 -05:00
|
|
|
let config_specifier =
|
|
|
|
ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap();
|
2021-05-27 02:33:33 -04:00
|
|
|
// Emit error: config file JSON "<config_path>" should be an object
|
2023-05-22 21:28:36 -04:00
|
|
|
assert!(ConfigFile::new(config_text, config_specifier).is_err());
|
2021-05-27 02:33:33 -04:00
|
|
|
}
|
|
|
|
|
2020-10-29 06:18:18 -04: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-17 20:10:17 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn discover_from_success() {
|
|
|
|
// testdata/fmt/deno.jsonc exists
|
|
|
|
let testdata = test_util::testdata_path();
|
2023-06-10 11:09:45 -04:00
|
|
|
let c_md = testdata.join("fmt/with_config/subdir/c.md").to_path_buf();
|
2022-01-17 20:10:17 -05:00
|
|
|
let mut checked = HashSet::new();
|
2022-06-28 16:45:55 -04:00
|
|
|
let config_file = ConfigFile::discover_from(&c_md, &mut checked)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2022-01-17 20:10:17 -05:00
|
|
|
assert!(checked.contains(c_md.parent().unwrap()));
|
2023-06-10 11:09:45 -04:00
|
|
|
assert!(!checked.contains(testdata.as_path()));
|
2022-01-17 20:10:17 -05:00
|
|
|
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 15:22:09 -05:00
|
|
|
.unwrap()
|
|
|
|
.to_file_path()
|
2022-01-17 20:10:17 -05:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(fmt_config.files.exclude, vec![expected_exclude]);
|
|
|
|
|
|
|
|
// Now add all ancestors of testdata to checked.
|
2023-06-10 11:09:45 -04:00
|
|
|
for a in testdata.as_path().ancestors() {
|
2022-01-17 20:10:17 -05:00
|
|
|
checked.insert(a.to_path_buf());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we call discover_from again starting at testdata, we ought to get None.
|
2023-06-10 11:09:45 -04:00
|
|
|
assert!(ConfigFile::discover_from(testdata.as_path(), &mut checked)
|
2022-06-28 16:45:55 -04:00
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
2022-01-17 20:10:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn discover_from_malformed() {
|
|
|
|
let testdata = test_util::testdata_path();
|
|
|
|
let d = testdata.join("malformed_config/");
|
|
|
|
let mut checked = HashSet::new();
|
2023-06-10 11:09:45 -04:00
|
|
|
let err = ConfigFile::discover_from(d.as_path(), &mut checked).unwrap_err();
|
2022-01-17 20:10:17 -05:00
|
|
|
assert!(err.to_string().contains("Unable to parse config file"));
|
|
|
|
}
|
2022-02-22 18:51:14 -05:00
|
|
|
|
|
|
|
#[test]
|
2022-06-28 16:45:55 -04: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 18:51:14 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-06-28 16:45:55 -04: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 18:51:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-06-28 16:45:55 -04: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 18:51:14 -05:00
|
|
|
}
|
|
|
|
|
2022-06-28 16:45:55 -04: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();
|
2023-05-22 21:28:36 -04:00
|
|
|
let config_file = ConfigFile::new(config_text, config_specifier).unwrap();
|
2022-06-28 16:45:55 -04:00
|
|
|
assert_eq!(
|
|
|
|
config_file
|
|
|
|
.resolve_tasks_config()
|
|
|
|
.err()
|
|
|
|
.unwrap()
|
|
|
|
.to_string(),
|
|
|
|
expected_error,
|
|
|
|
);
|
2022-02-22 18:51:14 -05:00
|
|
|
}
|
2020-08-31 14:12:24 -04:00
|
|
|
}
|