diff options
Diffstat (limited to 'cli/args/config_file.rs')
-rw-r--r-- | cli/args/config_file.rs | 88 |
1 files changed, 79 insertions, 9 deletions
diff --git a/cli/args/config_file.rs b/cli/args/config_file.rs index 1928d02fb..66b80b9d4 100644 --- a/cli/args/config_file.rs +++ b/cli/args/config_file.rs @@ -1033,20 +1033,37 @@ impl ConfigFile { /// JSX import source configuration. pub fn to_maybe_jsx_import_source_config( &self, - ) -> Option<JsxImportSourceConfig> { - let compiler_options_value = self.json.compiler_options.as_ref()?; - let compiler_options: CompilerOptions = - serde_json::from_value(compiler_options_value.clone()).ok()?; + ) -> Result<Option<JsxImportSourceConfig>, AnyError> { + let Some(compiler_options_value) = self.json.compiler_options.as_ref() else { + return Ok(None); + }; + let Some(compiler_options) = + serde_json::from_value::<CompilerOptions>(compiler_options_value.clone()).ok() else { + return Ok(None); + }; let module = match compiler_options.jsx.as_deref() { - Some("react-jsx") => Some("jsx-runtime".to_string()), - Some("react-jsxdev") => Some("jsx-dev-runtime".to_string()), - _ => None, + Some("react-jsx") => "jsx-runtime".to_string(), + Some("react-jsxdev") => "jsx-dev-runtime".to_string(), + Some("react") | None => { + if compiler_options.jsx_import_source.is_some() { + bail!( + "'jsxImportSource' is only supported when 'jsx' is set to 'react-jsx' or 'react-jsxdev'.\n at {}", + self.specifier, + ); + } + return Ok(None); + } + Some(setting) => bail!( + "Unsupported 'jsx' compiler option value '{}'. Supported: 'react-jsx', 'react-jsxdev', 'react'\n at {}", + setting, + self.specifier, + ), }; - module.map(|module| JsxImportSourceConfig { + Ok(Some(JsxImportSourceConfig { default_specifier: compiler_options.jsx_import_source, module, base_url: self.specifier.clone(), - }) + })) } pub fn resolve_tasks_config( @@ -1610,6 +1627,59 @@ mod tests { } #[test] + fn test_jsx_invalid_setting() { + let config_text = r#"{ "compilerOptions": { "jsx": "preserve" } }"#; + let config_specifier = + ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap(); + let config = ConfigFile::new(config_text, config_specifier).unwrap(); + assert_eq!( + config.to_maybe_jsx_import_source_config().err().unwrap().to_string(), + concat!( + "Unsupported 'jsx' compiler option value 'preserve'. Supported: 'react-jsx', 'react-jsxdev', 'react'\n", + " at file:///deno/tsconfig.json", + ), + ); + } + + #[test] + fn test_jsx_import_source_only() { + let config_specifier = + ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap(); + { + let config_text = + r#"{ "compilerOptions": { "jsxImportSource": "test" } }"#; + let config = + ConfigFile::new(config_text, config_specifier.clone()).unwrap(); + assert_eq!( + config.to_maybe_jsx_import_source_config().err().unwrap().to_string(), + concat!( + "'jsxImportSource' is only supported when 'jsx' is set to 'react-jsx' or 'react-jsxdev'.\n", + " at file:///deno/tsconfig.json", + ), + ); + } + { + let config_text = r#"{ "compilerOptions": { "jsx": "react", "jsxImportSource": "test" } }"#; + let config = ConfigFile::new(config_text, config_specifier).unwrap(); + assert_eq!( + config.to_maybe_jsx_import_source_config().err().unwrap().to_string(), + concat!( + "'jsxImportSource' is only supported when 'jsx' is set to 'react-jsx' or 'react-jsxdev'.\n", + " at file:///deno/tsconfig.json", + ), + ); + } + } + + #[test] + fn test_jsx_import_source_valid() { + let config_text = r#"{ "compilerOptions": { "jsx": "react" } }"#; + let config_specifier = + ModuleSpecifier::parse("file:///deno/tsconfig.json").unwrap(); + assert!(ConfigFile::new(config_text, config_specifier).is_ok()); + } + + #[test] fn test_tsconfig_as_bytes() { let mut tsconfig1 = TsConfig::new(json!({ "strict": true, |