Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add support for jsxImportSourceTypes #23419

Merged
merged 12 commits into from
Apr 30, 2024
30 changes: 13 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ license = "MIT"
repository = "https://github.com/denoland/deno"

[workspace.dependencies]
deno_ast = { version = "=0.36.2", features = ["transpiling"] }
deno_ast = { version = "0.37.0", features = ["transpiling"] }
dsherret marked this conversation as resolved.
Show resolved Hide resolved
deno_core = { version = "0.275.0" }

deno_bench_util = { version = "0.141.0", path = "./bench_util" }
Expand Down Expand Up @@ -370,3 +370,7 @@ opt-level = 3
opt-level = 3
[profile.release.package.base64-simd]
opt-level = 3

[patch.crates-io]
deno_graph = { path = "../deno_graph" }
deno_config = { path = "../deno_config" }
12 changes: 6 additions & 6 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ deno_ast = { workspace = true, features = ["bundler", "cjs", "codegen", "proposa
deno_cache_dir = { workspace = true }
deno_config = "=0.15.0"
deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_doc = { version = "=0.123.1", features = ["html"] }
deno_emit = "=0.39.0"
deno_graph = { version = "=0.71.5", features = ["tokio_executor"] }
deno_lint = { version = "=0.58.2", features = ["docs"] }
deno_doc = { version = "0.124.0", features = ["html"] }
deno_emit = "0.39.1"
deno_graph = { version = "0.72.0", features = ["tokio_executor"] }
deno_lint = { version = "0.58.3", features = ["docs"] }
deno_lockfile.workspace = true
deno_npm = "=0.17.0"
deno_runtime = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_semver = "=0.5.4"
deno_task_shell = "=0.16.0"
deno_terminal.workspace = true
eszip = "=0.66.0"
eszip = "0.67.0"
napi_sym.workspace = true

async-trait.workspace = true
Expand All @@ -98,7 +98,7 @@ dotenvy = "0.15.7"
dprint-plugin-json = "=0.19.2"
dprint-plugin-jupyter = "=0.1.3"
dprint-plugin-markdown = "=0.16.4"
dprint-plugin-typescript = "=0.90.1"
dprint-plugin-typescript = "0.90.2"
env_logger = "=0.10.0"
fancy-regex = "=0.10.0"
faster-hex.workspace = true
Expand Down
18 changes: 9 additions & 9 deletions cli/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::cache::FastInsecureHasher;
use crate::cache::ParsedSourceCache;

use deno_ast::SourceMapOption;
use deno_ast::TranspileResult;
use deno_core::error::AnyError;
use deno_core::ModuleCodeString;
use deno_core::ModuleSpecifier;
Expand Down Expand Up @@ -32,7 +33,7 @@ impl Emitter {
let transpile_and_emit_options_hash = {
let mut hasher = FastInsecureHasher::default();
hasher.write_hashable(&transpile_options);
hasher.write_hashable(emit_options);
hasher.write_hashable(&emit_options);
hasher.finish()
};
Self {
Expand Down Expand Up @@ -101,14 +102,12 @@ impl Emitter {
media_type,
)?;
let transpiled_source = match parsed_source
.transpile_owned(&self.transpile_options, &self.emit_options)
.transpile(&self.transpile_options, &self.emit_options)?
{
Ok(result) => result?,
Err(parsed_source) => {
// transpile_owned is more efficient and should be preferred
TranspileResult::Owned(source) => source,
TranspileResult::Cloned(source) => {
debug_assert!(false, "Transpile owned failed.");
parsed_source
.transpile(&self.transpile_options, &self.emit_options)?
source
}
};
debug_assert!(transpiled_source.source_map.is_none());
Expand All @@ -135,10 +134,11 @@ impl Emitter {
let parsed_source = self
.parsed_source_cache
.remove_or_parse_module(specifier, source_arc, media_type)?;
let mut options = self.emit_options;
let mut options = self.emit_options.clone();
options.source_map = SourceMapOption::None;
let transpiled_source = parsed_source
.transpile_owned_with_fallback(&self.transpile_options, &options)?;
.transpile(&self.transpile_options, &options)?
.into_source();
Ok(transpiled_source.text)
}

Expand Down
10 changes: 8 additions & 2 deletions cli/lsp/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,14 @@ impl LspTsConfig {
let import_map = import_map?;
let referrer = &config_file?.specifier;
let compiler_options = ts_config.inner.0.as_object_mut()?;
let jsx_import_source =
compiler_options.get("jsxImportSource")?.as_str()?;
let jsx_import_source = compiler_options
.get("jsxImportSourceTypes")
.and_then(|v| v.as_str())
.or_else(|| {
compiler_options
.get("jsxImportSource")
.and_then(|v| v.as_str())
})?;
let jsx_import_source =
import_map.resolve(jsx_import_source, referrer).ok()?;
compiler_options
Expand Down
9 changes: 9 additions & 0 deletions cli/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ pub struct CliGraphResolver {
sloppy_imports_resolver: Option<SloppyImportsResolver>,
mapped_specifier_resolver: MappedSpecifierResolver,
maybe_default_jsx_import_source: Option<String>,
maybe_default_jsx_import_source_types: Option<String>,
maybe_jsx_import_source_module: Option<String>,
maybe_vendor_specifier: Option<ModuleSpecifier>,
node_resolver: Option<Arc<CliNodeResolver>>,
Expand Down Expand Up @@ -488,6 +489,10 @@ impl CliGraphResolver {
.maybe_jsx_import_source_config
.as_ref()
.and_then(|c| c.default_specifier.clone()),
maybe_default_jsx_import_source_types: options
.maybe_jsx_import_source_config
.as_ref()
.and_then(|c| c.default_types_specifier.clone()),
maybe_jsx_import_source_module: options
.maybe_jsx_import_source_config
.map(|c| c.module),
Expand Down Expand Up @@ -554,6 +559,10 @@ impl Resolver for CliGraphResolver {
self.maybe_default_jsx_import_source.clone()
}

fn default_jsx_import_source_types(&self) -> Option<String> {
self.maybe_default_jsx_import_source_types.clone()
}

fn jsx_import_source_module(&self) -> &str {
self
.maybe_jsx_import_source_module
Expand Down
6 changes: 6 additions & 0 deletions cli/schemas/config-file.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
"default": "react",
"markdownDescription": "Specify module specifier used to import the JSX factory functions when using jsx: `react-jsx*`.\n\nSee more: https://www.typescriptlang.org/tsconfig/#jsxImportSource"
},
"jsxImportSourceTypes": {
"description": "Specify module specifier used to import the types for the JSX factory functions when using jsx: 'react-jsx*'. This is the logical equivalent of prefixing an import to the jsxImportSource with `// @deno-types=\"...\"`.",
"type": "string",
"default": "@types/react",
"markdownDescription": "Specify module specifier used to import the types for the JSX factory functions when using jsx: `react-jsx*`. This is the logical equivalent of prefixing an import to the jsxImportSource with `// @deno-types=\"...\"`."
},
"keyofStringsOnly": {
"description": "Make keyof only return strings instead of string, numbers or symbols. Legacy option.",
"type": "boolean",
Expand Down
1 change: 1 addition & 0 deletions cli/tools/repl/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ impl ReplSession {
keep_comments: false,
},
)?
.into_source()
.text;

let value = self
Expand Down
6 changes: 3 additions & 3 deletions runtime/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn maybe_transpile_source(
maybe_syntax: None,
})?;
let transpiled_source = parsed
.transpile_owned(
.transpile(
&deno_ast::TranspileOptions {
imports_not_used_as_values: deno_ast::ImportsNotUsedAsValues::Remove,
..Default::default()
Expand All @@ -109,8 +109,8 @@ pub fn maybe_transpile_source(
},
..Default::default()
},
)
.unwrap()?;
)?
.into_source();

let maybe_source_map: Option<SourceMapData> = transpiled_source
.source_map
Expand Down