mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
refactor: migrate last tsc op to op2 macro (#20816)
This commit is contained in:
parent
8706a20d1f
commit
d41d3b8e2f
1 changed files with 40 additions and 32 deletions
|
@ -14,16 +14,13 @@ use deno_core::anyhow::Context;
|
||||||
use deno_core::ascii_str;
|
use deno_core::ascii_str;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::located_script_name;
|
use deno_core::located_script_name;
|
||||||
use deno_core::op;
|
|
||||||
use deno_core::op2;
|
use deno_core::op2;
|
||||||
use deno_core::resolve_url_or_path;
|
use deno_core::resolve_url_or_path;
|
||||||
use deno_core::serde::Deserialize;
|
use deno_core::serde::Deserialize;
|
||||||
use deno_core::serde::Deserializer;
|
use deno_core::serde::Deserializer;
|
||||||
use deno_core::serde::Serialize;
|
use deno_core::serde::Serialize;
|
||||||
use deno_core::serde::Serializer;
|
use deno_core::serde::Serializer;
|
||||||
use deno_core::serde_json;
|
|
||||||
use deno_core::serde_json::json;
|
use deno_core::serde_json::json;
|
||||||
use deno_core::serde_json::Value;
|
|
||||||
use deno_core::serde_v8;
|
use deno_core::serde_v8;
|
||||||
use deno_core::JsRuntime;
|
use deno_core::JsRuntime;
|
||||||
use deno_core::ModuleSpecifier;
|
use deno_core::ModuleSpecifier;
|
||||||
|
@ -444,17 +441,29 @@ pub fn as_ts_script_kind(media_type: MediaType) -> i32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(bartlomieju): `op2` doesn't support `serde_json::Value`
|
#[derive(Debug, Serialize)]
|
||||||
#[op]
|
#[serde(rename_all = "camelCase")]
|
||||||
fn op_load(state: &mut OpState, args: Value) -> Result<Value, AnyError> {
|
struct LoadResponse {
|
||||||
|
data: Option<String>,
|
||||||
|
version: Option<String>,
|
||||||
|
script_kind: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[op2]
|
||||||
|
#[serde]
|
||||||
|
fn op_load(
|
||||||
|
state: &mut OpState,
|
||||||
|
#[serde] v: LoadArgs,
|
||||||
|
) -> Result<LoadResponse, AnyError> {
|
||||||
let state = state.borrow_mut::<State>();
|
let state = state.borrow_mut::<State>();
|
||||||
let v: LoadArgs = serde_json::from_value(args)
|
|
||||||
.context("Invalid request from JavaScript for \"op_load\".")?;
|
|
||||||
let specifier = normalize_specifier(&v.specifier, &state.current_dir)
|
let specifier = normalize_specifier(&v.specifier, &state.current_dir)
|
||||||
.context("Error converting a string module specifier for \"op_load\".")?;
|
.context("Error converting a string module specifier for \"op_load\".")?;
|
||||||
|
|
||||||
let mut hash: Option<String> = None;
|
let mut hash: Option<String> = None;
|
||||||
let mut media_type = MediaType::Unknown;
|
let mut media_type = MediaType::Unknown;
|
||||||
let graph = &state.graph;
|
let graph = &state.graph;
|
||||||
|
|
||||||
let data = if &v.specifier == "internal:///.tsbuildinfo" {
|
let data = if &v.specifier == "internal:///.tsbuildinfo" {
|
||||||
state.maybe_tsbuildinfo.as_deref().map(Cow::Borrowed)
|
state.maybe_tsbuildinfo.as_deref().map(Cow::Borrowed)
|
||||||
// in certain situations we return a "blank" module to tsc and we need to
|
// in certain situations we return a "blank" module to tsc and we need to
|
||||||
|
@ -521,11 +530,11 @@ fn op_load(state: &mut OpState, args: Value) -> Result<Value, AnyError> {
|
||||||
maybe_source
|
maybe_source
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(json!({
|
Ok(LoadResponse {
|
||||||
"data": data,
|
data: data.map(String::from),
|
||||||
"version": hash,
|
version: hash,
|
||||||
"scriptKind": as_ts_script_kind(media_type),
|
script_kind: as_ts_script_kind(media_type),
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
@ -879,6 +888,7 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::args::TsConfig;
|
use crate::args::TsConfig;
|
||||||
use deno_core::futures::future;
|
use deno_core::futures::future;
|
||||||
|
use deno_core::serde_json;
|
||||||
use deno_core::OpState;
|
use deno_core::OpState;
|
||||||
use deno_graph::GraphKind;
|
use deno_graph::GraphKind;
|
||||||
use deno_graph::ModuleGraph;
|
use deno_graph::ModuleGraph;
|
||||||
|
@ -1054,11 +1064,13 @@ mod tests {
|
||||||
.await;
|
.await;
|
||||||
let actual = op_load::call(
|
let actual = op_load::call(
|
||||||
&mut state,
|
&mut state,
|
||||||
json!({ "specifier": "https://deno.land/x/mod.ts"}),
|
LoadArgs {
|
||||||
|
specifier: "https://deno.land/x/mod.ts".to_string(),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
actual,
|
serde_json::to_value(actual).unwrap(),
|
||||||
json!({
|
json!({
|
||||||
"data": "console.log(\"hello deno\");\n",
|
"data": "console.log(\"hello deno\");\n",
|
||||||
"version": "7821807483407828376",
|
"version": "7821807483407828376",
|
||||||
|
@ -1067,14 +1079,6 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
struct LoadResponse {
|
|
||||||
data: String,
|
|
||||||
version: Option<String>,
|
|
||||||
script_kind: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_load_asset() {
|
async fn test_load_asset() {
|
||||||
let mut state = setup(
|
let mut state = setup(
|
||||||
|
@ -1083,15 +1087,15 @@ mod tests {
|
||||||
Some("some content".to_string()),
|
Some("some content".to_string()),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let value = op_load::call(
|
let actual = op_load::call(
|
||||||
&mut state,
|
&mut state,
|
||||||
json!({ "specifier": "asset:///lib.dom.d.ts" }),
|
LoadArgs {
|
||||||
|
specifier: "asset:///lib.dom.d.ts".to_string(),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
.expect("should have invoked op");
|
.expect("should have invoked op");
|
||||||
let actual: LoadResponse =
|
|
||||||
serde_json::from_value(value).expect("failed to deserialize");
|
|
||||||
let expected = get_lazily_loaded_asset("lib.dom.d.ts").unwrap();
|
let expected = get_lazily_loaded_asset("lib.dom.d.ts").unwrap();
|
||||||
assert_eq!(actual.data, expected);
|
assert_eq!(actual.data.unwrap(), expected);
|
||||||
assert!(actual.version.is_some());
|
assert!(actual.version.is_some());
|
||||||
assert_eq!(actual.script_kind, 3);
|
assert_eq!(actual.script_kind, 3);
|
||||||
}
|
}
|
||||||
|
@ -1106,11 +1110,13 @@ mod tests {
|
||||||
.await;
|
.await;
|
||||||
let actual = op_load::call(
|
let actual = op_load::call(
|
||||||
&mut state,
|
&mut state,
|
||||||
json!({ "specifier": "internal:///.tsbuildinfo"}),
|
LoadArgs {
|
||||||
|
specifier: "internal:///.tsbuildinfo".to_string(),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
.expect("should have invoked op");
|
.expect("should have invoked op");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
actual,
|
serde_json::to_value(actual).unwrap(),
|
||||||
json!({
|
json!({
|
||||||
"data": "some content",
|
"data": "some content",
|
||||||
"version": null,
|
"version": null,
|
||||||
|
@ -1124,11 +1130,13 @@ mod tests {
|
||||||
let mut state = setup(None, None, None).await;
|
let mut state = setup(None, None, None).await;
|
||||||
let actual = op_load::call(
|
let actual = op_load::call(
|
||||||
&mut state,
|
&mut state,
|
||||||
json!({ "specifier": "https://deno.land/x/mod.ts"}),
|
LoadArgs {
|
||||||
|
specifier: "https://deno.land/x/mod.ts".to_string(),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
.expect("should have invoked op");
|
.expect("should have invoked op");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
actual,
|
serde_json::to_value(actual).unwrap(),
|
||||||
json!({
|
json!({
|
||||||
"data": null,
|
"data": null,
|
||||||
"version": null,
|
"version": null,
|
||||||
|
|
Loading…
Reference in a new issue