1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

fix: do not panic running .d.cts and .d.mts files (#14917)

This commit is contained in:
David Sherret 2022-06-20 11:20:52 -04:00 committed by GitHub
parent a0fc43c2c5
commit a7a64438e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 8 deletions

View file

@ -609,8 +609,8 @@ impl ProcState {
); );
let graph_data = self.graph_data.read(); let graph_data = self.graph_data.read();
let found = graph_data.follow_redirect(&specifier); let found_url = graph_data.follow_redirect(&specifier);
match graph_data.get(&found) { match graph_data.get(&found_url) {
Some(ModuleEntry::Module { Some(ModuleEntry::Module {
code, media_type, .. code, media_type, ..
}) => { }) => {
@ -627,25 +627,32 @@ impl ProcState {
code.to_string() code.to_string()
} }
} }
MediaType::Dts => "".to_string(), MediaType::Dts | MediaType::Dcts | MediaType::Dmts => "".to_string(),
_ => { MediaType::TypeScript
| MediaType::Mts
| MediaType::Cts
| MediaType::Jsx
| MediaType::Tsx => {
let emit_path = self let emit_path = self
.dir .dir
.gen_cache .gen_cache
.get_cache_filename_with_extension(&found, "js") .get_cache_filename_with_extension(&found_url, "js")
.unwrap_or_else(|| { .unwrap_or_else(|| {
unreachable!("Unable to get cache filename: {}", &found) unreachable!("Unable to get cache filename: {}", &found_url)
}); });
match self.dir.gen_cache.get(&emit_path) { match self.dir.gen_cache.get(&emit_path) {
Ok(b) => String::from_utf8(b).unwrap(), Ok(b) => String::from_utf8(b).unwrap(),
Err(_) => unreachable!("Unexpected missing emit: {}", found), Err(_) => unreachable!("Unexpected missing emit: {}\n\nTry reloading with the --reload CLI flag or deleting your DENO_DIR.", found_url),
} }
} }
MediaType::TsBuildInfo | MediaType::Wasm | MediaType::SourceMap => {
panic!("Unexpected media type {} for {}", media_type, found_url)
}
}; };
Ok(ModuleSource { Ok(ModuleSource {
code: code.into_bytes().into_boxed_slice(), code: code.into_bytes().into_boxed_slice(),
module_url_specified: specifier.to_string(), module_url_specified: specifier.to_string(),
module_url_found: found.to_string(), module_url_found: found_url.to_string(),
module_type: match media_type { module_type: match media_type {
MediaType::Json => ModuleType::Json, MediaType::Json => ModuleType::Json,
_ => ModuleType::JavaScript, _ => ModuleType::JavaScript,

View file

@ -2709,3 +2709,22 @@ itest!(custom_inspect_url {
args: "run custom_inspect_url.js", args: "run custom_inspect_url.js",
output: "custom_inspect_url.js.out", output: "custom_inspect_url.js.out",
}); });
#[test]
fn running_declaration_files() {
let temp_dir = TempDir::new();
let files = vec!["file.d.ts", "file.d.cts", "file.d.mts"];
for file in files {
temp_dir.write(file, "");
let mut deno_cmd = util::deno_cmd_with_deno_dir(&temp_dir);
let output = deno_cmd
.current_dir(temp_dir.path())
.args(["run", file])
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
}
}