summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2023-07-27 12:15:39 -0400
committerGitHub <noreply@github.com>2023-07-27 12:15:39 -0400
commit02d6bbff2c72817c9a9d6346b4bfd8ed25379ea3 (patch)
tree1eca00e8410da689286f6f60cfaae381dfbb2e4e
parent806137bb96c6f7e4b359a6e979c4e22d3a04a55c (diff)
fix: error on invalid & unsupported jsx compiler options (#19954)
-rw-r--r--cli/args/config_file.rs88
-rw-r--r--cli/args/mod.rs10
-rw-r--r--cli/factory.rs2
-rw-r--r--cli/lsp/documents.rs2
-rw-r--r--cli/tools/vendor/mod.rs2
5 files changed, 87 insertions, 17 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,
diff --git a/cli/args/mod.rs b/cli/args/mod.rs
index a979aa10c..68cd3faa7 100644
--- a/cli/args/mod.rs
+++ b/cli/args/mod.rs
@@ -914,11 +914,11 @@ impl CliOptions {
/// Return the JSX import source configuration.
pub fn to_maybe_jsx_import_source_config(
&self,
- ) -> Option<JsxImportSourceConfig> {
- self
- .maybe_config_file
- .as_ref()
- .and_then(|c| c.to_maybe_jsx_import_source_config())
+ ) -> Result<Option<JsxImportSourceConfig>, AnyError> {
+ match self.maybe_config_file.as_ref() {
+ Some(config) => config.to_maybe_jsx_import_source_config(),
+ None => Ok(None),
+ }
}
/// Return any imports that should be brought into the scope of the module
diff --git a/cli/factory.rs b/cli/factory.rs
index 330865744..28c436e35 100644
--- a/cli/factory.rs
+++ b/cli/factory.rs
@@ -398,7 +398,7 @@ impl CliFactory {
.resolver
.get_or_try_init_async(async {
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.options.no_npm(),
self.npm_api()?.clone(),
diff --git a/cli/lsp/documents.rs b/cli/lsp/documents.rs
index d987279de..2d3bed57a 100644
--- a/cli/lsp/documents.rs
+++ b/cli/lsp/documents.rs
@@ -1219,7 +1219,7 @@ impl Documents {
});
let maybe_jsx_config = options
.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(
&options.enabled_urls,
options.document_preload_limit,
diff --git a/cli/tools/vendor/mod.rs b/cli/tools/vendor/mod.rs
index 49a984c87..fdea5fc26 100644
--- a/cli/tools/vendor/mod.rs
+++ b/cli/tools/vendor/mod.rs
@@ -50,7 +50,7 @@ pub async fn vendor(
let cli_options = factory.cli_options();
let entry_points =
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 output = build::build(build::BuildInput {
entry_points,