Skip to content

Commit 382e8e8

Browse files
committed
fix: better jsx config resolution
1 parent e1f3fd6 commit 382e8e8

File tree

3 files changed

+286
-194
lines changed

3 files changed

+286
-194
lines changed

src/deno_json/mod.rs

+12-151
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ mod ts;
3535

3636
pub use ts::EmitConfigOptions;
3737
pub use ts::IgnoredCompilerOptions;
38-
pub use ts::JsxImportSourceConfig;
3938
pub use ts::ParsedTsConfigOptions;
39+
pub use ts::RawJsxCompilerOptions;
4040
pub use ts::TsConfig;
4141

4242
#[derive(Clone, Debug, Default, Deserialize, Hash, PartialEq)]
@@ -834,17 +834,6 @@ pub enum ToInvalidConfigError {
834834
},
835835
}
836836

837-
#[derive(Debug, Error, JsError)]
838-
#[class(type)]
839-
pub enum ToMaybeJsxImportSourceConfigError {
840-
#[error("'jsxImportSource' is only supported when 'jsx' is set to 'react-jsx' or 'react-jsxdev'.\n at {0}")]
841-
InvalidJsxImportSourceJsxValue(Url),
842-
#[error("'jsxImportSourceTypes' is only supported when 'jsx' is set to 'react-jsx' or 'react-jsxdev'.\n at {0}")]
843-
InvalidJjsxImportSourceTypessxValue(Url),
844-
#[error("Unsupported 'jsx' compiler option value '{value}'. Supported: 'react-jsx', 'react-jsxdev', 'react', 'precompile'\n at {specifier}")]
845-
InvalidJsxCompilerOption { value: String, specifier: Url },
846-
}
847-
848837
#[derive(Debug, Error, JsError)]
849838
#[class(type)]
850839
pub enum ResolveTaskConfigError {
@@ -1626,60 +1615,18 @@ impl ConfigFile {
16261615

16271616
/// Based on the compiler options in the configuration file, return the
16281617
/// JSX import source configuration.
1629-
pub fn to_maybe_jsx_import_source_config(
1630-
&self,
1631-
) -> Result<Option<JsxImportSourceConfig>, ToMaybeJsxImportSourceConfigError>
1632-
{
1633-
#[derive(Debug, Deserialize)]
1634-
#[serde(rename_all = "camelCase")]
1635-
struct JsxCompilerOptions {
1636-
pub jsx: Option<String>,
1637-
pub jsx_import_source: Option<String>,
1638-
pub jsx_import_source_types: Option<String>,
1639-
}
1640-
1641-
let Some(compiler_options_value) = self.json.compiler_options.as_ref()
1642-
else {
1643-
return Ok(None);
1644-
};
1645-
let Some(compiler_options) = serde_json::from_value::<JsxCompilerOptions>(
1646-
compiler_options_value.clone(),
1647-
)
1648-
.ok() else {
1649-
return Ok(None);
1650-
};
1651-
let module = match compiler_options.jsx.as_deref() {
1652-
Some("react-jsx") => "jsx-runtime".to_string(),
1653-
Some("react-jsxdev") => "jsx-dev-runtime".to_string(),
1654-
Some("react") | None => {
1655-
if compiler_options.jsx_import_source.is_some() {
1656-
return Err(
1657-
ToMaybeJsxImportSourceConfigError::InvalidJsxImportSourceJsxValue(
1658-
self.specifier.clone(),
1659-
),
1660-
);
1661-
}
1662-
if compiler_options.jsx_import_source_types.is_some() {
1663-
return Err(ToMaybeJsxImportSourceConfigError::InvalidJjsxImportSourceTypessxValue(self.specifier.clone()));
1664-
}
1665-
return Ok(None);
1666-
}
1667-
Some("precompile") => "jsx-runtime".to_string(),
1668-
Some(setting) => {
1669-
return Err(
1670-
ToMaybeJsxImportSourceConfigError::InvalidJsxCompilerOption {
1671-
value: setting.to_string(),
1672-
specifier: self.specifier.clone(),
1673-
},
1618+
pub fn to_raw_jsx_compiler_options(&self) -> RawJsxCompilerOptions {
1619+
self
1620+
.json
1621+
.compiler_options
1622+
.as_ref()
1623+
.and_then(|compiler_options_value| {
1624+
serde_json::from_value::<RawJsxCompilerOptions>(
1625+
compiler_options_value.clone(),
16741626
)
1675-
}
1676-
};
1677-
Ok(Some(JsxImportSourceConfig {
1678-
default_specifier: compiler_options.jsx_import_source,
1679-
default_types_specifier: compiler_options.jsx_import_source_types,
1680-
module,
1681-
base_url: self.specifier.clone(),
1682-
}))
1627+
.ok()
1628+
})
1629+
.unwrap_or_default()
16831630
}
16841631

16851632
pub fn resolve_tasks_config(
@@ -2238,92 +2185,6 @@ mod tests {
22382185
assert!(ConfigFile::new(config_text, config_specifier,).is_err());
22392186
}
22402187

2241-
#[test]
2242-
fn test_jsx_invalid_setting() {
2243-
let config_text = r#"{ "compilerOptions": { "jsx": "preserve" } }"#;
2244-
let config_specifier = Url::parse("file:///deno/tsconfig.json").unwrap();
2245-
let config = ConfigFile::new(config_text, config_specifier).unwrap();
2246-
assert_eq!(
2247-
config.to_maybe_jsx_import_source_config().err().unwrap().to_string(),
2248-
concat!(
2249-
"Unsupported 'jsx' compiler option value 'preserve'. Supported: 'react-jsx', 'react-jsxdev', 'react', 'precompile'\n",
2250-
" at file:///deno/tsconfig.json",
2251-
),
2252-
);
2253-
}
2254-
2255-
#[test]
2256-
fn test_jsx_import_source_only() {
2257-
let config_specifier = Url::parse("file:///deno/tsconfig.json").unwrap();
2258-
{
2259-
let config_text =
2260-
r#"{ "compilerOptions": { "jsxImportSource": "test" } }"#;
2261-
let config =
2262-
ConfigFile::new(config_text, config_specifier.clone()).unwrap();
2263-
assert_eq!(
2264-
config.to_maybe_jsx_import_source_config().err().unwrap().to_string(),
2265-
concat!(
2266-
"'jsxImportSource' is only supported when 'jsx' is set to 'react-jsx' or 'react-jsxdev'.\n",
2267-
" at file:///deno/tsconfig.json",
2268-
),
2269-
);
2270-
}
2271-
{
2272-
let config_text = r#"{ "compilerOptions": { "jsx": "react", "jsxImportSource": "test" } }"#;
2273-
let config = ConfigFile::new(config_text, config_specifier).unwrap();
2274-
assert_eq!(
2275-
config.to_maybe_jsx_import_source_config().err().unwrap().to_string(),
2276-
concat!(
2277-
"'jsxImportSource' is only supported when 'jsx' is set to 'react-jsx' or 'react-jsxdev'.\n",
2278-
" at file:///deno/tsconfig.json",
2279-
),
2280-
);
2281-
}
2282-
}
2283-
2284-
#[test]
2285-
fn test_jsx_import_source_types_only() {
2286-
let config_specifier = Url::parse("file:///deno/tsconfig.json").unwrap();
2287-
{
2288-
let config_text =
2289-
r#"{ "compilerOptions": { "jsxImportSourceTypes": "test" } }"#;
2290-
let config =
2291-
ConfigFile::new(config_text, config_specifier.clone()).unwrap();
2292-
assert_eq!(
2293-
config.to_maybe_jsx_import_source_config().err().unwrap().to_string(),
2294-
concat!(
2295-
"'jsxImportSourceTypes' is only supported when 'jsx' is set to 'react-jsx' or 'react-jsxdev'.\n",
2296-
" at file:///deno/tsconfig.json",
2297-
),
2298-
);
2299-
}
2300-
{
2301-
let config_text = r#"{ "compilerOptions": { "jsx": "react", "jsxImportSourceTypes": "test" } }"#;
2302-
let config = ConfigFile::new(config_text, config_specifier).unwrap();
2303-
assert_eq!(
2304-
config.to_maybe_jsx_import_source_config().err().unwrap().to_string(),
2305-
concat!(
2306-
"'jsxImportSourceTypes' is only supported when 'jsx' is set to 'react-jsx' or 'react-jsxdev'.\n",
2307-
" at file:///deno/tsconfig.json",
2308-
),
2309-
);
2310-
}
2311-
}
2312-
2313-
#[test]
2314-
fn test_jsx_import_source_valid() {
2315-
let config_text = r#"{ "compilerOptions": { "jsx": "react" } }"#;
2316-
let config_specifier = Url::parse("file:///deno/tsconfig.json").unwrap();
2317-
assert!(ConfigFile::new(config_text, config_specifier,).is_ok());
2318-
}
2319-
2320-
#[test]
2321-
fn test_jsx_precompile_skip_setting() {
2322-
let config_text = r#"{ "compilerOptions": { "jsx": "precompile", "jsxPrecompileSkipElements": ["a", "p"] } }"#;
2323-
let config_specifier = Url::parse("file:///deno/tsconfig.json").unwrap();
2324-
assert!(ConfigFile::new(config_text, config_specifier,).is_ok());
2325-
}
2326-
23272188
#[test]
23282189
fn task_name_invalid_chars() {
23292190
run_task_error_test(

src/deno_json/ts.rs

+6-24
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,12 @@ use serde_json::Value;
77
use std::fmt;
88
use url::Url;
99

10-
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
11-
pub struct JsxImportSourceConfig {
12-
pub default_specifier: Option<String>,
13-
pub default_types_specifier: Option<String>,
14-
pub module: String,
15-
pub base_url: Url,
16-
}
17-
18-
impl JsxImportSourceConfig {
19-
pub fn maybe_specifier_text(&self) -> Option<String> {
20-
self
21-
.default_specifier
22-
.as_ref()
23-
.map(|default_specifier| format!("{}/{}", default_specifier, self.module))
24-
}
25-
26-
pub fn maybe_types_specifier_text(&self) -> Option<String> {
27-
self
28-
.default_types_specifier
29-
.as_ref()
30-
.map(|default_types_specifier| {
31-
format!("{}/{}", default_types_specifier, self.module)
32-
})
33-
}
10+
#[derive(Debug, Default, Deserialize)]
11+
#[serde(rename_all = "camelCase")]
12+
pub struct RawJsxCompilerOptions {
13+
pub jsx: Option<String>,
14+
pub jsx_import_source: Option<String>,
15+
pub jsx_import_source_types: Option<String>,
3416
}
3517

3618
/// The transpile options that are significant out of a user provided tsconfig

0 commit comments

Comments
 (0)