1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

fix: error on invalid & unsupported jsx compiler options (#19954)

This commit is contained in:
David Sherret 2023-07-27 12:15:39 -04:00 committed by GitHub
parent 806137bb96
commit 02d6bbff2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 18 deletions

View file

@ -1033,20 +1033,37 @@ impl ConfigFile {
/// JSX import source configuration. /// JSX import source configuration.
pub fn to_maybe_jsx_import_source_config( pub fn to_maybe_jsx_import_source_config(
&self, &self,
) -> Option<JsxImportSourceConfig> { ) -> Result<Option<JsxImportSourceConfig>, AnyError> {
let compiler_options_value = self.json.compiler_options.as_ref()?; let Some(compiler_options_value) = self.json.compiler_options.as_ref() else {
let compiler_options: CompilerOptions = return Ok(None);
serde_json::from_value(compiler_options_value.clone()).ok()?;
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,
}; };
module.map(|module| JsxImportSourceConfig { 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") => "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,
),
};
Ok(Some(JsxImportSourceConfig {
default_specifier: compiler_options.jsx_import_source, default_specifier: compiler_options.jsx_import_source,
module, module,
base_url: self.specifier.clone(), base_url: self.specifier.clone(),
}) }))
} }
pub fn resolve_tasks_config( pub fn resolve_tasks_config(
@ -1609,6 +1626,59 @@ mod tests {
assert!(ConfigFile::new(config_text, config_specifier).is_err()); assert!(ConfigFile::new(config_text, config_specifier).is_err());
} }
#[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] #[test]
fn test_tsconfig_as_bytes() { fn test_tsconfig_as_bytes() {
let mut tsconfig1 = TsConfig::new(json!({ let mut tsconfig1 = TsConfig::new(json!({

View file

@ -914,11 +914,11 @@ impl CliOptions {
/// Return the JSX import source configuration. /// Return the JSX import source configuration.
pub fn to_maybe_jsx_import_source_config( pub fn to_maybe_jsx_import_source_config(
&self, &self,
) -> Option<JsxImportSourceConfig> { ) -> Result<Option<JsxImportSourceConfig>, AnyError> {
self match self.maybe_config_file.as_ref() {
.maybe_config_file Some(config) => config.to_maybe_jsx_import_source_config(),
.as_ref() None => Ok(None),
.and_then(|c| c.to_maybe_jsx_import_source_config()) }
} }
/// Return any imports that should be brought into the scope of the module /// Return any imports that should be brought into the scope of the module

View file

@ -398,7 +398,7 @@ impl CliFactory {
.resolver .resolver
.get_or_try_init_async(async { .get_or_try_init_async(async {
Ok(Arc::new(CliGraphResolver::new( Ok(Arc::new(CliGraphResolver::new(
self.options.to_maybe_jsx_import_source_config(), self.options.to_maybe_jsx_import_source_config()?,
self.maybe_import_map().await?.clone(), self.maybe_import_map().await?.clone(),
self.options.no_npm(), self.options.no_npm(),
self.npm_api()?.clone(), self.npm_api()?.clone(),

View file

@ -1219,7 +1219,7 @@ impl Documents {
}); });
let maybe_jsx_config = options let maybe_jsx_config = options
.maybe_config_file .maybe_config_file
.and_then(|cf| cf.to_maybe_jsx_import_source_config()); .and_then(|cf| cf.to_maybe_jsx_import_source_config().ok().flatten());
let new_resolver_config_hash = calculate_resolver_config_hash( let new_resolver_config_hash = calculate_resolver_config_hash(
&options.enabled_urls, &options.enabled_urls,
options.document_preload_limit, options.document_preload_limit,

View file

@ -50,7 +50,7 @@ pub async fn vendor(
let cli_options = factory.cli_options(); let cli_options = factory.cli_options();
let entry_points = let entry_points =
resolve_entry_points(&vendor_flags, cli_options.initial_cwd())?; resolve_entry_points(&vendor_flags, cli_options.initial_cwd())?;
let jsx_import_source = cli_options.to_maybe_jsx_import_source_config(); let jsx_import_source = cli_options.to_maybe_jsx_import_source_config()?;
let module_graph_builder = factory.module_graph_builder().await?.clone(); let module_graph_builder = factory.module_graph_builder().await?.clone();
let output = build::build(build::BuildInput { let output = build::build(build::BuildInput {
entry_points, entry_points,