2020-08-31 14:12:24 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2020-11-16 14:48:50 -05:00
|
|
|
use crate::fs_util::canonicalize_path;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
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;
|
2020-08-31 14:12:24 -04:00
|
|
|
use jsonc_parser::JsonValue;
|
|
|
|
use serde::Deserialize;
|
2020-09-29 03:16:12 -04:00
|
|
|
use serde::Serialize;
|
|
|
|
use serde::Serializer;
|
2020-10-29 06:18:18 -04:00
|
|
|
use std::collections::BTreeMap;
|
2020-08-31 14:12:24 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fmt;
|
2020-09-29 03:16:12 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2020-08-31 14:12:24 -04:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
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,
|
2020-10-26 09:03:03 -04:00
|
|
|
pub inline_source_map: bool,
|
2020-09-29 03:16:12 -04:00
|
|
|
pub jsx: String,
|
|
|
|
pub jsx_factory: String,
|
|
|
|
pub jsx_fragment_factory: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A structure that represents a set of options that were ignored and the
|
|
|
|
/// path those options came from.
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct IgnoredCompilerOptions {
|
|
|
|
pub items: Vec<String>,
|
2020-10-26 09:03:03 -04:00
|
|
|
pub maybe_path: Option<PathBuf>,
|
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();
|
2020-08-31 14:12:24 -04:00
|
|
|
codes.sort();
|
2020-10-26 09:03:03 -04:00
|
|
|
if let Some(path) = &self.maybe_path {
|
|
|
|
write!(f, "Unsupported compiler options in \"{}\".\n The following options were ignored:\n {}", path.to_string_lossy(), codes.join(", "))
|
|
|
|
} else {
|
|
|
|
write!(f, "Unsupported compiler options provided.\n The following options were ignored:\n {}", codes.join(", "))
|
|
|
|
}
|
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] = &[
|
2020-08-31 14:12:24 -04:00
|
|
|
"allowSyntheticDefaultImports",
|
2020-11-01 21:51:56 -05:00
|
|
|
"allowUmdGlobalAccess",
|
|
|
|
"baseUrl",
|
|
|
|
"declaration",
|
|
|
|
"declarationMap",
|
|
|
|
"downlevelIteration",
|
2020-10-26 09:03:03 -04:00
|
|
|
"esModuleInterop",
|
2020-11-01 21:51:56 -05:00
|
|
|
"emitDeclarationOnly",
|
|
|
|
"importHelpers",
|
2020-10-26 09:03:03 -04:00
|
|
|
"inlineSourceMap",
|
|
|
|
"inlineSources",
|
2020-11-25 07:06:54 -05:00
|
|
|
"isolatedModules",
|
2020-10-26 09:03:03 -04:00
|
|
|
"module",
|
2020-11-01 21:51:56 -05:00
|
|
|
"noEmitHelpers",
|
2020-10-26 09:03:03 -04:00
|
|
|
"noLib",
|
2020-11-01 21:51:56 -05:00
|
|
|
"noResolve",
|
|
|
|
"outDir",
|
|
|
|
"paths",
|
2020-10-26 09:03:03 -04:00
|
|
|
"preserveConstEnums",
|
|
|
|
"reactNamespace",
|
2020-11-01 21:51:56 -05:00
|
|
|
"rootDir",
|
|
|
|
"rootDirs",
|
|
|
|
"skipLibCheck",
|
2020-10-26 09:03:03 -04:00
|
|
|
"sourceMap",
|
2020-11-01 21:51:56 -05:00
|
|
|
"sourceRoot",
|
2020-10-26 09:03:03 -04:00
|
|
|
"target",
|
2020-11-01 21:51:56 -05:00
|
|
|
"types",
|
|
|
|
"useDefineForClassFields",
|
2020-10-26 09:03:03 -04:00
|
|
|
];
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
pub const IGNORED_RUNTIME_COMPILER_OPTIONS: &[&str] = &[
|
2020-08-31 14:12:24 -04:00
|
|
|
"assumeChangesOnlyAffectDirectDependencies",
|
|
|
|
"build",
|
2020-11-01 21:51:56 -05:00
|
|
|
"charset",
|
2020-08-31 14:12:24 -04:00
|
|
|
"composite",
|
|
|
|
"diagnostics",
|
2020-11-01 21:51:56 -05:00
|
|
|
"disableSizeLimit",
|
2020-08-31 14:12:24 -04:00
|
|
|
"emitBOM",
|
|
|
|
"extendedDiagnostics",
|
|
|
|
"forceConsistentCasingInFileNames",
|
|
|
|
"generateCpuProfile",
|
|
|
|
"help",
|
|
|
|
"incremental",
|
|
|
|
"init",
|
|
|
|
"listEmittedFiles",
|
|
|
|
"listFiles",
|
|
|
|
"mapRoot",
|
|
|
|
"maxNodeModuleJsDepth",
|
|
|
|
"moduleResolution",
|
|
|
|
"newLine",
|
|
|
|
"noEmit",
|
|
|
|
"noEmitOnError",
|
|
|
|
"out",
|
|
|
|
"outDir",
|
|
|
|
"outFile",
|
|
|
|
"preserveSymlinks",
|
|
|
|
"preserveWatchOutput",
|
|
|
|
"pretty",
|
2020-11-01 21:51:56 -05:00
|
|
|
"project",
|
2020-08-31 14:12:24 -04:00
|
|
|
"resolveJsonModule",
|
|
|
|
"showConfig",
|
|
|
|
"skipDefaultLibCheck",
|
|
|
|
"stripInternal",
|
|
|
|
"traceResolution",
|
|
|
|
"tsBuildInfoFile",
|
|
|
|
"typeRoots",
|
|
|
|
"version",
|
|
|
|
"watch",
|
|
|
|
];
|
|
|
|
|
|
|
|
/// A function that works like JavaScript's `Object.assign()`.
|
|
|
|
pub fn json_merge(a: &mut Value, b: &Value) {
|
|
|
|
match (a, b) {
|
|
|
|
(&mut Value::Object(ref mut a), &Value::Object(ref b)) => {
|
|
|
|
for (k, v) in b {
|
|
|
|
json_merge(a.entry(k.clone()).or_insert(Value::Null), v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(a, b) => {
|
|
|
|
*a = b.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a jsonc libraries `JsonValue` to a serde `Value`.
|
|
|
|
fn jsonc_to_serde(j: JsonValue) -> Value {
|
|
|
|
match j {
|
|
|
|
JsonValue::Array(arr) => {
|
|
|
|
let vec = arr.into_iter().map(jsonc_to_serde).collect();
|
|
|
|
Value::Array(vec)
|
|
|
|
}
|
|
|
|
JsonValue::Boolean(bool) => Value::Bool(bool),
|
|
|
|
JsonValue::Null => Value::Null,
|
|
|
|
JsonValue::Number(num) => {
|
|
|
|
let number =
|
|
|
|
serde_json::Number::from_str(&num).expect("could not parse number");
|
|
|
|
Value::Number(number)
|
|
|
|
}
|
|
|
|
JsonValue::Object(obj) => {
|
|
|
|
let mut map = serde_json::map::Map::new();
|
|
|
|
for (key, json_value) in obj.into_iter() {
|
|
|
|
map.insert(key, jsonc_to_serde(json_value));
|
|
|
|
}
|
|
|
|
Value::Object(map)
|
|
|
|
}
|
|
|
|
JsonValue::String(str) => Value::String(str),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct TSConfigJson {
|
|
|
|
compiler_options: Option<HashMap<String, Value>>,
|
|
|
|
exclude: Option<Vec<String>>,
|
|
|
|
extends: Option<String>,
|
|
|
|
files: Option<Vec<String>>,
|
|
|
|
include: Option<Vec<String>>,
|
|
|
|
references: Option<Value>,
|
|
|
|
type_acquisition: Option<Value>,
|
|
|
|
}
|
|
|
|
|
2020-10-26 09:03:03 -04:00
|
|
|
fn parse_compiler_options(
|
|
|
|
compiler_options: &HashMap<String, Value>,
|
|
|
|
maybe_path: Option<PathBuf>,
|
|
|
|
is_runtime: bool,
|
|
|
|
) -> 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();
|
|
|
|
if (!is_runtime && IGNORED_COMPILER_OPTIONS.contains(&key))
|
|
|
|
|| IGNORED_RUNTIME_COMPILER_OPTIONS.contains(&key)
|
|
|
|
{
|
|
|
|
items.push(key.to_string());
|
|
|
|
} else {
|
|
|
|
filtered.insert(key.to_string(), value.to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let value = serde_json::to_value(filtered)?;
|
|
|
|
let maybe_ignored_options = if !items.is_empty() {
|
|
|
|
Some(IgnoredCompilerOptions { items, maybe_path })
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((value, maybe_ignored_options))
|
|
|
|
}
|
|
|
|
|
2020-08-31 14:12:24 -04:00
|
|
|
/// Take a string of JSONC, parse it and return a serde `Value` of the text.
|
|
|
|
/// The result also contains any options that were ignored.
|
|
|
|
pub fn parse_config(
|
|
|
|
config_text: &str,
|
2020-09-29 03:16:12 -04:00
|
|
|
path: &Path,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(Value, Option<IgnoredCompilerOptions>), AnyError> {
|
2020-08-31 14:12:24 -04:00
|
|
|
assert!(!config_text.is_empty());
|
|
|
|
let jsonc = jsonc_parser::parse_to_value(config_text)?.unwrap();
|
|
|
|
let config: TSConfigJson = serde_json::from_value(jsonc_to_serde(jsonc))?;
|
|
|
|
|
2020-10-26 09:03:03 -04:00
|
|
|
if let Some(compiler_options) = config.compiler_options {
|
|
|
|
parse_compiler_options(&compiler_options, Some(path.to_owned()), false)
|
2020-08-31 14:12:24 -04:00
|
|
|
} else {
|
2020-10-26 09:03:03 -04:00
|
|
|
Ok((json!({}), None))
|
|
|
|
}
|
2020-08-31 14:12:24 -04:00
|
|
|
}
|
|
|
|
|
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> {
|
2020-10-29 06:18:18 -04:00
|
|
|
let map = self.0.as_object().unwrap();
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Merge a serde_json value into the configuration.
|
|
|
|
pub fn merge(&mut self, value: &Value) {
|
|
|
|
json_merge(&mut self.0, value);
|
|
|
|
}
|
|
|
|
|
2020-09-29 03:16:12 -04:00
|
|
|
/// Take an optional string representing a user provided TypeScript config file
|
|
|
|
/// which was passed in via the `--config` compiler option and merge it with
|
|
|
|
/// the configuration. Returning the result which optionally contains any
|
|
|
|
/// compiler options that were ignored.
|
|
|
|
///
|
|
|
|
/// When there are options ignored out of the file, a warning will be written
|
|
|
|
/// to stderr regarding the options that were ignored.
|
2020-10-26 09:03:03 -04:00
|
|
|
pub fn merge_tsconfig(
|
2020-09-29 03:16:12 -04:00
|
|
|
&mut self,
|
|
|
|
maybe_path: Option<String>,
|
|
|
|
) -> Result<Option<IgnoredCompilerOptions>, AnyError> {
|
|
|
|
if let Some(path) = maybe_path {
|
|
|
|
let cwd = std::env::current_dir()?;
|
|
|
|
let config_file = cwd.join(path);
|
2020-10-29 22:19:03 -04:00
|
|
|
let config_path = canonicalize_path(&config_file).map_err(|_| {
|
2020-09-29 03:16:12 -04:00
|
|
|
std::io::Error::new(
|
|
|
|
std::io::ErrorKind::InvalidInput,
|
|
|
|
format!(
|
|
|
|
"Could not find the config file: {}",
|
|
|
|
config_file.to_string_lossy()
|
|
|
|
),
|
|
|
|
)
|
|
|
|
})?;
|
2020-10-01 06:32:05 -04:00
|
|
|
let config_text = std::fs::read_to_string(config_path.clone())?;
|
2020-09-29 03:16:12 -04:00
|
|
|
let (value, maybe_ignored_options) =
|
|
|
|
parse_config(&config_text, &config_path)?;
|
|
|
|
json_merge(&mut self.0, &value);
|
|
|
|
|
|
|
|
Ok(maybe_ignored_options)
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
2020-10-26 09:03:03 -04:00
|
|
|
|
|
|
|
/// Take a map of compiler options, filtering out any that are ignored, then
|
|
|
|
/// merge it with the current configuration, returning any options that might
|
|
|
|
/// have been ignored.
|
|
|
|
pub fn merge_user_config(
|
|
|
|
&mut self,
|
|
|
|
user_options: &HashMap<String, Value>,
|
|
|
|
) -> Result<Option<IgnoredCompilerOptions>, AnyError> {
|
|
|
|
let (value, maybe_ignored_options) =
|
|
|
|
parse_compiler_options(user_options, None, true)?;
|
|
|
|
json_merge(&mut self.0, &value);
|
|
|
|
Ok(maybe_ignored_options)
|
|
|
|
}
|
2020-09-29 03:16:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
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
|
|
|
|
}
|
|
|
|
}"#;
|
2020-09-29 03:16:12 -04:00
|
|
|
let config_path = PathBuf::from("/deno/tsconfig.json");
|
2020-08-31 14:12:24 -04:00
|
|
|
let (options_value, ignored) =
|
2020-09-29 03:16:12 -04:00
|
|
|
parse_config(config_text, &config_path).expect("error parsing");
|
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()],
|
2020-10-26 09:03:03 -04:00
|
|
|
maybe_path: Some(config_path),
|
2020-09-29 03:16:12 -04:00
|
|
|
}),
|
2020-08-31 14:12:24 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-26 09:03:03 -04:00
|
|
|
#[test]
|
|
|
|
fn test_tsconfig_merge_user_options() {
|
|
|
|
let mut tsconfig = TsConfig::new(json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
}));
|
|
|
|
let user_options = serde_json::from_value(json!({
|
|
|
|
"target": "es6",
|
|
|
|
"build": true,
|
|
|
|
"strict": false,
|
|
|
|
}))
|
|
|
|
.expect("could not convert to hashmap");
|
|
|
|
let maybe_ignored_options = tsconfig
|
|
|
|
.merge_user_config(&user_options)
|
|
|
|
.expect("could not merge options");
|
|
|
|
assert_eq!(
|
|
|
|
tsconfig.0,
|
|
|
|
json!({
|
|
|
|
"module": "esnext",
|
|
|
|
"target": "es6",
|
|
|
|
"strict": false,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
maybe_ignored_options,
|
|
|
|
Some(IgnoredCompilerOptions {
|
|
|
|
items: vec!["build".to_string()],
|
|
|
|
maybe_path: None
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
2020-08-31 14:12:24 -04:00
|
|
|
}
|