diff options
author | Luca Casonato <hello@lcas.dev> | 2022-08-24 19:36:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-24 19:36:05 +0200 |
commit | 33c4d45328166d103e99a8c243727eead626080c (patch) | |
tree | 70331a9964f8faf780942696c34aab867accf7bf /cli | |
parent | 6bb72a80863ac3913d32ea21aae32dd327ce6b71 (diff) |
fix: resolve `jsxImportSource` relative to module (#15561)
Previously `jsxImportSource` was resolved relative to the config file
during graph building, and relative to the emitted module during
runtime.
This is now fixed so that the JSX import source is resolved relative to
the module both during graph building and at runtime.
Diffstat (limited to 'cli')
-rw-r--r-- | cli/Cargo.toml | 8 | ||||
-rw-r--r-- | cli/args/config_file.rs | 31 | ||||
-rw-r--r-- | cli/args/mod.rs | 9 | ||||
-rw-r--r-- | cli/lsp/documents.rs | 4 | ||||
-rw-r--r-- | cli/main.rs | 4 | ||||
-rw-r--r-- | cli/proc_state.rs | 8 | ||||
-rw-r--r-- | cli/resolver.rs | 12 | ||||
-rw-r--r-- | cli/tests/integration/run_tests.rs | 12 | ||||
-rw-r--r-- | cli/tests/testdata/jsx/import-map-scoped.json | 8 | ||||
-rw-r--r-- | cli/tests/testdata/jsx_import_source_error.out | 2 | ||||
-rw-r--r-- | cli/tests/testdata/subdir/jsx_import_source_no_pragma.tsx | 7 |
11 files changed, 71 insertions, 34 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 5bfc4663a..ef4dc6aff 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -48,9 +48,9 @@ winres = "=0.1.12" [dependencies] deno_ast = { version = "0.17.0", features = ["bundler", "cjs", "codegen", "dep_graph", "module_specifier", "proposal", "react", "sourcemap", "transforms", "transpiling", "typescript", "view", "visit"] } deno_core = { version = "0.147.0", path = "../core" } -deno_doc = "0.42.0" -deno_emit = "0.6.0" -deno_graph = "0.31.0" +deno_doc = "0.43.0" +deno_emit = "0.7.0" +deno_graph = "0.32.0" deno_lint = { version = "0.32.0", features = ["docs"] } deno_runtime = { version = "0.73.0", path = "../runtime" } deno_task_shell = "0.5.0" @@ -69,7 +69,7 @@ dprint-plugin-markdown = "=0.14.0" dprint-plugin-typescript = "=0.71.2" encoding_rs = "=0.8.31" env_logger = "=0.9.0" -eszip = "=0.24.0" +eszip = "=0.25.0" fancy-regex = "=0.10.0" flate2 = "=1.0.24" http = "=0.2.6" diff --git a/cli/args/config_file.rs b/cli/args/config_file.rs index 1739309ff..ab6e57cb1 100644 --- a/cli/args/config_file.rs +++ b/cli/args/config_file.rs @@ -11,7 +11,6 @@ use crate::fs_util::specifier_to_file_path; use deno_core::anyhow::anyhow; use deno_core::anyhow::bail; use deno_core::anyhow::Context; -use deno_core::error::custom_error; use deno_core::error::AnyError; use deno_core::serde::Deserialize; use deno_core::serde::Serialize; @@ -30,6 +29,11 @@ use std::path::PathBuf; pub type MaybeImportsResult = Result<Option<Vec<(ModuleSpecifier, Vec<String>)>>, AnyError>; +pub struct JsxImportSourceConfig { + pub default_specifier: Option<String>, + pub module: String, +} + /// The transpile options that are significant out of a user provided tsconfig /// file, that we want to deserialize out of the final config for a transpile. #[derive(Debug, Deserialize)] @@ -680,17 +684,6 @@ impl ConfigFile { if let Some(types) = compiler_options.types { imports.extend(types); } - if compiler_options.jsx == Some("react-jsx".to_string()) { - imports.push(format!( - "{}/jsx-runtime", - compiler_options.jsx_import_source.ok_or_else(|| custom_error("TypeError", "Compiler option 'jsx' set to 'react-jsx', but no 'jsxImportSource' defined."))? - )); - } else if compiler_options.jsx == Some("react-jsxdev".to_string()) { - imports.push(format!( - "{}/jsx-dev-runtime", - compiler_options.jsx_import_source.ok_or_else(|| custom_error("TypeError", "Compiler option 'jsx' set to 'react-jsxdev', but no 'jsxImportSource' defined."))? - )); - } if !imports.is_empty() { let referrer = self.specifier.clone(); Ok(Some(vec![(referrer, imports)])) @@ -700,16 +693,22 @@ impl ConfigFile { } /// Based on the compiler options in the configuration file, return the - /// implied JSX import source module. - pub fn to_maybe_jsx_import_source_module(&self) -> Option<String> { + /// 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()?; - match compiler_options.jsx.as_deref() { + 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 { + default_specifier: compiler_options.jsx_import_source, + module, + }) } pub fn to_fmt_config(&self) -> Result<Option<FmtConfig>, AnyError> { diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 85e44aca2..90fa5220b 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -34,6 +34,7 @@ use std::env; use std::net::SocketAddr; use std::path::PathBuf; +use crate::args::config_file::JsxImportSourceConfig; use crate::compat; use crate::deno_dir::DenoDir; use crate::emit::get_ts_config_for_emit; @@ -210,12 +211,14 @@ impl CliOptions { } } - /// Return the implied JSX import source module. - pub fn to_maybe_jsx_import_source_module(&self) -> Option<String> { + /// 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_module()) + .and_then(|c| c.to_maybe_jsx_import_source_config()) } /// Return any imports that should be brought into the scope of the module diff --git a/cli/lsp/documents.rs b/cli/lsp/documents.rs index 18c3710fb..0fa85c095 100644 --- a/cli/lsp/documents.rs +++ b/cli/lsp/documents.rs @@ -998,8 +998,8 @@ impl Documents { // TODO(@kitsonk) update resolved dependencies? self.maybe_import_map = maybe_import_map.map(ImportMapResolver::new); self.maybe_jsx_resolver = maybe_config_file.and_then(|cf| { - cf.to_maybe_jsx_import_source_module() - .map(|im| JsxResolver::new(im, self.maybe_import_map.clone())) + cf.to_maybe_jsx_import_source_config() + .map(|cfg| JsxResolver::new(cfg, self.maybe_import_map.clone())) }); self.imports = Arc::new( if let Some(Ok(Some(imports))) = diff --git a/cli/main.rs b/cli/main.rs index b784ed3d5..ead113697 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -457,8 +457,8 @@ async fn create_graph_and_maybe_check( ps.maybe_import_map.clone().map(ImportMapResolver::new); let maybe_jsx_resolver = ps .options - .to_maybe_jsx_import_source_module() - .map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone())); + .to_maybe_jsx_import_source_config() + .map(|cfg| JsxResolver::new(cfg, maybe_import_map_resolver.clone())); let maybe_resolver = if maybe_jsx_resolver.is_some() { maybe_jsx_resolver.as_ref().map(|jr| jr.as_resolver()) } else { diff --git a/cli/proc_state.rs b/cli/proc_state.rs index ee9098c76..bc5c36e13 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -192,8 +192,8 @@ impl ProcState { let maybe_import_map_resolver = maybe_import_map.clone().map(ImportMapResolver::new); let maybe_jsx_resolver = cli_options - .to_maybe_jsx_import_source_module() - .map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone())); + .to_maybe_jsx_import_source_config() + .map(|cfg| JsxResolver::new(cfg, maybe_import_map_resolver.clone())); let maybe_resolver: Option< Arc<dyn deno_graph::source::Resolver + Send + Sync>, > = if cli_options.compat() { @@ -643,8 +643,8 @@ impl ProcState { let maybe_imports = self.options.to_maybe_imports()?; let maybe_jsx_resolver = self .options - .to_maybe_jsx_import_source_module() - .map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone())); + .to_maybe_jsx_import_source_config() + .map(|cfg| JsxResolver::new(cfg, maybe_import_map_resolver.clone())); let maybe_resolver = if maybe_jsx_resolver.is_some() { maybe_jsx_resolver.as_ref().map(|jr| jr.as_resolver()) } else { diff --git a/cli/resolver.rs b/cli/resolver.rs index 30149278c..2e97cfe1f 100644 --- a/cli/resolver.rs +++ b/cli/resolver.rs @@ -7,6 +7,8 @@ use deno_graph::source::Resolver; use import_map::ImportMap; use std::sync::Arc; +use crate::args::config_file::JsxImportSourceConfig; + /// Wraps an import map to be used when building a deno_graph module graph. /// This is done to avoid having `import_map` be a direct dependency of /// `deno_graph`. @@ -38,17 +40,19 @@ impl Resolver for ImportMapResolver { #[derive(Debug, Default, Clone)] pub struct JsxResolver { + default_jsx_import_source: Option<String>, jsx_import_source_module: String, maybe_import_map_resolver: Option<ImportMapResolver>, } impl JsxResolver { pub fn new( - jsx_import_source_module: String, + jsx_import_source_config: JsxImportSourceConfig, maybe_import_map_resolver: Option<ImportMapResolver>, ) -> Self { Self { - jsx_import_source_module, + default_jsx_import_source: jsx_import_source_config.default_specifier, + jsx_import_source_module: jsx_import_source_config.module, maybe_import_map_resolver, } } @@ -59,6 +63,10 @@ impl JsxResolver { } impl Resolver for JsxResolver { + fn default_jsx_import_source(&self) -> Option<String> { + self.default_jsx_import_source.clone() + } + fn jsx_import_source_module(&self) -> &str { self.jsx_import_source_module.as_str() } diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index 8998eecc9..20661f27f 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -1337,6 +1337,18 @@ itest!(jsx_import_source_import_map_dev { http_server: true, }); +itest!(jsx_import_source_import_map_scoped { + args: "run --reload --import-map jsx/import-map-scoped.json --config jsx/deno-jsx-import-map.jsonc subdir/jsx_import_source_no_pragma.tsx", + output: "jsx_import_source_import_map.out", + http_server: true, +}); + +itest!(jsx_import_source_import_map_scoped_dev { + args: "run --reload --import-map jsx/import-map-scoped.json --config jsx/deno-jsxdev-import-map.jsonc subdir/jsx_import_source_no_pragma.tsx", + output: "jsx_import_source_import_map_dev.out", + http_server: true, +}); + itest!(jsx_import_source_pragma_no_check { args: "run --reload --no-check jsx_import_source_pragma.tsx", output: "jsx_import_source.out", diff --git a/cli/tests/testdata/jsx/import-map-scoped.json b/cli/tests/testdata/jsx/import-map-scoped.json new file mode 100644 index 000000000..9b2005128 --- /dev/null +++ b/cli/tests/testdata/jsx/import-map-scoped.json @@ -0,0 +1,8 @@ +{ + "scopes": { + "../subdir/": { + "jsx/jsx-runtime": "http://localhost:4545/jsx/jsx-runtime/index.ts", + "jsx/jsx-dev-runtime": "http://localhost:4545/jsx/jsx-dev-runtime/index.ts" + } + } +} diff --git a/cli/tests/testdata/jsx_import_source_error.out b/cli/tests/testdata/jsx_import_source_error.out index 957fa4a97..634a5b09b 100644 --- a/cli/tests/testdata/jsx_import_source_error.out +++ b/cli/tests/testdata/jsx_import_source_error.out @@ -1,2 +1,2 @@ error: Module not found "file:///[WILDCARD]/nonexistent/jsx-runtime". - at file:///[WILDCARD]/deno-jsx-error.jsonc:1:1 + at file:///[WILDCARD]/jsx_import_source_no_pragma.tsx:1:1 diff --git a/cli/tests/testdata/subdir/jsx_import_source_no_pragma.tsx b/cli/tests/testdata/subdir/jsx_import_source_no_pragma.tsx new file mode 100644 index 000000000..2c756054f --- /dev/null +++ b/cli/tests/testdata/subdir/jsx_import_source_no_pragma.tsx @@ -0,0 +1,7 @@ +function A() { + return "hello"; +} + +export function B() { + return <A></A>; +} |