1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -05:00

fix(#10733): empty tsconfig.json file does not cause error (#10734)

Fixes #10733
This commit is contained in:
Romain Prignon 2021-05-27 08:33:33 +02:00 committed by GitHub
parent b21fa78a1e
commit 3468e1fcf8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,7 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use crate::fs_util::canonicalize_path;
use deno_core::error::anyhow;
use deno_core::error::AnyError;
use deno_core::error::Context;
use deno_core::serde::Deserialize;
@ -293,7 +294,23 @@ impl ConfigFile {
}
pub fn new(text: &str, path: &Path) -> Result<Self, AnyError> {
let jsonc = jsonc_parser::parse_to_serde_value(text)?.unwrap();
let jsonc = match jsonc_parser::parse_to_serde_value(text) {
Ok(None) => json!({}),
Ok(Some(value)) if value.is_object() => value,
Ok(Some(_)) => {
return Err(anyhow!(
"config file JSON {:?} should be an object",
path.to_str().unwrap()
))
}
Err(e) => {
return Err(anyhow!(
"Unable to parse config file JSON {:?} because of {}",
path.to_str().unwrap(),
e.to_string()
))
}
};
let json: ConfigFileJson = serde_json::from_value(jsonc)?;
Ok(Self {
@ -393,6 +410,42 @@ mod tests {
);
}
#[test]
fn test_parse_config_with_empty_file() {
let config_text = "";
let config_path = PathBuf::from("/deno/tsconfig.json");
let config_file = ConfigFile::new(config_text, &config_path).unwrap();
let (options_value, _) =
config_file.as_compiler_options().expect("error parsing");
assert!(options_value.is_object());
}
#[test]
fn test_parse_config_with_commented_file() {
let config_text = r#"//{"foo":"bar"}"#;
let config_path = PathBuf::from("/deno/tsconfig.json");
let config_file = ConfigFile::new(config_text, &config_path).unwrap();
let (options_value, _) =
config_file.as_compiler_options().expect("error parsing");
assert!(options_value.is_object());
}
#[test]
fn test_parse_config_with_invalid_file() {
let config_text = "{foo:bar}";
let config_path = PathBuf::from("/deno/tsconfig.json");
// Emit error: Unable to parse config file JSON "<config_path>" because of Unexpected token on line 1 column 6.
assert!(ConfigFile::new(config_text, &config_path).is_err());
}
#[test]
fn test_parse_config_with_not_object_file() {
let config_text = "[]";
let config_path = PathBuf::from("/deno/tsconfig.json");
// Emit error: config file JSON "<config_path>" should be an object
assert!(ConfigFile::new(config_text, &config_path).is_err());
}
#[test]
fn test_tsconfig_merge_user_options() {
let mut tsconfig = TsConfig::new(json!({