Skip to content

Commit 5cee3a3

Browse files
authored
fix(check): support text and bytes imports with --unstable-tsgo (#31084)
Fixes #31034.
1 parent c20ef4c commit 5cee3a3

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

cli/tsc/go.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn synthetic_config(
6060
{
6161
obj.insert("jsx".to_string(), json!("react-jsx"));
6262
}
63+
obj.insert("allowArbitraryExtensions".to_string(), json!(true));
6364
let config = serde_json::to_string(&json!({
6465
"compilerOptions": config,
6566
"files": root_names,
@@ -334,6 +335,22 @@ fn get_package_json_scope_if_applicable(
334335
Ok(jsons!(None::<String>)?)
335336
}
336337

338+
fn append_raw_import_fragment(specifier: &mut String, raw_kind: &str) {
339+
let fragment_index = specifier.find('#');
340+
match fragment_index {
341+
Some(index) => {
342+
if specifier[index..].contains(&format!("denoRawImport={}.ts", raw_kind))
343+
{
344+
return;
345+
}
346+
specifier.push_str(&format!("&denoRawImport={}.ts", raw_kind));
347+
}
348+
None => {
349+
specifier.push_str(&format!("#denoRawImport={}.ts", raw_kind));
350+
}
351+
}
352+
}
353+
337354
struct HandlerState {
338355
config_path: String,
339356
synthetic_config: String,
@@ -407,7 +424,17 @@ impl deno_typescript_go_client_rust::CallbackHandler for Handler {
407424
}
408425
"resolveModuleName" => {
409426
let payload = deser::<ResolveModuleNamePayload>(payload)?;
410-
let (out_name, extension) = resolve_name(&mut state, payload)?;
427+
let import_attribute_type = payload.import_attribute_type.clone();
428+
let (mut out_name, mut extension) = resolve_name(&mut state, payload)?;
429+
if let Some(import_attribute_type) = &import_attribute_type
430+
&& matches!(import_attribute_type.as_str(), "text" | "bytes")
431+
{
432+
append_raw_import_fragment(
433+
&mut out_name,
434+
import_attribute_type.as_str(),
435+
);
436+
extension = Some("ts");
437+
}
411438

412439
Ok(jsons!({
413440
"resolvedFileName": out_name,
@@ -432,6 +459,7 @@ impl deno_typescript_go_client_rust::CallbackHandler for Handler {
432459
module_name: payload.type_reference_directive_name,
433460
containing_file: payload.containing_file,
434461
resolution_mode: payload.resolution_mode,
462+
import_attribute_type: None,
435463
};
436464
let (out_name, extension) = resolve_name(&mut state, payload)?;
437465
log::debug!(
@@ -507,6 +535,7 @@ fn resolve_name(
507535
payload: ResolveModuleNamePayload,
508536
) -> Result<(String, Option<&'static str>), deno_typescript_go_client_rust::Error>
509537
{
538+
log::debug!("resolve_name({payload:?})");
510539
let graph = &handler.graph;
511540
let maybe_npm = handler.maybe_npm.as_ref();
512541
let referrer = if let Some(remapped_specifier) =
@@ -627,6 +656,7 @@ fn load_inner(
627656
state,
628657
)?;
629658
let Some(result) = result else {
659+
log::debug!("load_inner {load_specifier} -> None");
630660
return Ok(None);
631661
};
632662
let is_cjs = result.is_cjs;
@@ -635,6 +665,7 @@ fn load_inner(
635665
let module_kind = get_resolution_mode(is_cjs, media_type);
636666
let script_kind = super::as_ts_script_kind(media_type);
637667
log::debug!("load_inner {load_specifier} -> {:?}", module_kind);
668+
log::trace!("loaded contents ({load_specifier}) -> {:?}", result.data);
638669
state
639670
.module_kind_map
640671
.insert(load_specifier.to_string(), module_kind);

cli/tsc/go/tsgo_version.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ impl Hashes {
2323
}
2424
}
2525

26-
pub const VERSION: &str = "0.1.6";
26+
pub const VERSION: &str = "0.1.8";
2727
pub const DOWNLOAD_BASE_URL: &str =
28-
"https://github.com/denoland/typescript-go/releases/download/v0.1.6";
28+
"https://github.com/denoland/typescript-go/releases/download/v0.1.8";
2929
pub const HASHES: Hashes = Hashes {
30-
windows_x64: "sha256:04f85a9a64807437471cd45ed569b1ee9910dbe9751c1a5085028ae5eb09db56",
31-
macos_x64: "sha256:84619ab4a6ac3dc1c78a62c209ea853cf077871fe086657bc861e268b9c0412c",
32-
macos_arm64: "sha256:cef3d3f60abe9f2947f2e30f8075d860f9bf176a5545c651eabfcaa9e791e0f9",
33-
linux_x64: "sha256:ea0ae7f3782a8372a03ec2c1f1bbb415e0c10a43ce917b0564084f896e0df127",
34-
linux_arm64: "sha256:67b4f4d9982ff5c5c14105b37c48b582e3dc806c8a504f1a1b5416e29de68198",
30+
windows_x64: "sha256:3e5c7102c387d1b85795e0baf402e93a1c3bd82d75b5ad90089ea72f0525bc22",
31+
macos_x64: "sha256:5f31471910b585dfbffdd370ffd0d0dc9f408e1b7482e293a84246d7c8bf980b",
32+
macos_arm64: "sha256:73d88c842a5276d721f38c6b749a923417f1c42f237fafe06ed4c50d772dda71",
33+
linux_x64: "sha256:e83798203401f8fd31f2539ef23c1bc76a9bf02cfe6902dc854310ed8d994454",
34+
linux_arm64: "sha256:f82e11a474bb9e69e0397c614ab30a08eb09c93c69bb3de19364ef660a851ede",
3535
};
3636

3737
const _: () = {

libs/typescript_go_client/src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub struct ResolveModuleNamePayload {
4848
pub module_name: String,
4949
pub containing_file: String,
5050
pub resolution_mode: ResolutionMode,
51+
pub import_attribute_type: Option<String>,
5152
// redirected_reference: Handle<Project>,
5253
}
5354

tests/specs/check/bytes_and_text_imports/__test__.jsonc

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33
"output": "main.out",
44
"exitCode": 1,
55
"variants": {
6-
// TODO(nathanwhit): not implemented yet, needs tsgo changes
7-
// see appendRawImportFragment in cli/tsc/97_ts_host.js and the code that calls it.
8-
// it will need tsgo changes because the import kind is not easily available on the tsgo side
9-
// during resolution.
10-
//
11-
// "tsgo": {
12-
// "use_tsgo": "1"
13-
// },
6+
"tsgo": {
7+
"use_tsgo": "1"
8+
},
149
"tsc": {
1510
"use_tsgo": ""
1611
}

0 commit comments

Comments
 (0)