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

refactor: migrate last tsc op to op2 macro #20816

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 40 additions & 32 deletions cli/tsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ use deno_core::anyhow::Context;
use deno_core::ascii_str;
use deno_core::error::AnyError;
use deno_core::located_script_name;
use deno_core::op;
use deno_core::op2;
use deno_core::resolve_url_or_path;
use deno_core::serde::Deserialize;
use deno_core::serde::Deserializer;
use deno_core::serde::Serialize;
use deno_core::serde::Serializer;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::serde_json::Value;
use deno_core::serde_v8;
use deno_core::JsRuntime;
use deno_core::ModuleSpecifier;
Expand Down Expand Up @@ -444,17 +441,29 @@ pub fn as_ts_script_kind(media_type: MediaType) -> i32 {
}
}

// TODO(bartlomieju): `op2` doesn't support `serde_json::Value`
#[op]
fn op_load(state: &mut OpState, args: Value) -> Result<Value, AnyError> {
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct LoadResponse {
data: Option<String>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is correct - op2 doesn't handle Cow<'_, str>, but I believe op macro did turn it into String internally anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is an issue because it's used only in a serde struct.

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 v: LoadArgs = serde_json::from_value(args)
.context("Invalid request from JavaScript for \"op_load\".")?;

let specifier = normalize_specifier(&v.specifier, &state.current_dir)
.context("Error converting a string module specifier for \"op_load\".")?;

let mut hash: Option<String> = None;
let mut media_type = MediaType::Unknown;
let graph = &state.graph;

let data = if &v.specifier == "internal:https:///.tsbuildinfo" {
state.maybe_tsbuildinfo.as_deref().map(Cow::Borrowed)
// in certain situations we return a "blank" module to tsc and we need to
Expand Down Expand Up @@ -521,11 +530,11 @@ fn op_load(state: &mut OpState, args: Value) -> Result<Value, AnyError> {
maybe_source
};

Ok(json!({
"data": data,
"version": hash,
"scriptKind": as_ts_script_kind(media_type),
}))
Ok(LoadResponse {
data: data.map(String::from),
version: hash,
script_kind: as_ts_script_kind(media_type),
})
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -879,6 +888,7 @@ mod tests {
use super::*;
use crate::args::TsConfig;
use deno_core::futures::future;
use deno_core::serde_json;
use deno_core::OpState;
use deno_graph::GraphKind;
use deno_graph::ModuleGraph;
Expand Down Expand Up @@ -1054,11 +1064,13 @@ mod tests {
.await;
let actual = op_load::call(
&mut state,
json!({ "specifier": "https://deno.land/x/mod.ts"}),
LoadArgs {
specifier: "https://deno.land/x/mod.ts".to_string(),
},
)
.unwrap();
assert_eq!(
actual,
serde_json::to_value(actual).unwrap(),
json!({
"data": "console.log(\"hello deno\");\n",
"version": "7821807483407828376",
Expand All @@ -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]
async fn test_load_asset() {
let mut state = setup(
Expand All @@ -1083,15 +1087,15 @@ mod tests {
Some("some content".to_string()),
)
.await;
let value = op_load::call(
let actual = op_load::call(
&mut state,
json!({ "specifier": "asset:https:///lib.dom.d.ts" }),
LoadArgs {
specifier: "asset:https:///lib.dom.d.ts".to_string(),
},
)
.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();
assert_eq!(actual.data, expected);
assert_eq!(actual.data.unwrap(), expected);
assert!(actual.version.is_some());
assert_eq!(actual.script_kind, 3);
}
Expand All @@ -1106,11 +1110,13 @@ mod tests {
.await;
let actual = op_load::call(
&mut state,
json!({ "specifier": "internal:https:///.tsbuildinfo"}),
LoadArgs {
specifier: "internal:https:///.tsbuildinfo".to_string(),
},
)
.expect("should have invoked op");
assert_eq!(
actual,
serde_json::to_value(actual).unwrap(),
json!({
"data": "some content",
"version": null,
Expand All @@ -1124,11 +1130,13 @@ mod tests {
let mut state = setup(None, None, None).await;
let actual = op_load::call(
&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");
assert_eq!(
actual,
serde_json::to_value(actual).unwrap(),
json!({
"data": null,
"version": null,
Expand Down