summaryrefslogtreecommitdiff
path: root/cli/tsc.rs
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2021-06-22 07:18:32 +1000
committerGitHub <noreply@github.com>2021-06-22 07:18:32 +1000
commit281c4cd8fcf5fd54f558a6922736def2c7804529 (patch)
tree65ac91c5a41a64dc0b85ee9c5949d7086e8620ef /cli/tsc.rs
parentcda15f2a98b10330422d1c8352d163d703ee6a49 (diff)
feat(cli): support "types" when type checking (#10999)
Fixes #10677
Diffstat (limited to 'cli/tsc.rs')
-rw-r--r--cli/tsc.rs55
1 files changed, 50 insertions, 5 deletions
diff --git a/cli/tsc.rs b/cli/tsc.rs
index f87682fca..bfd5e8dbe 100644
--- a/cli/tsc.rs
+++ b/cli/tsc.rs
@@ -12,6 +12,7 @@ use deno_core::error::AnyError;
use deno_core::error::Context;
use deno_core::op_sync;
use deno_core::resolve_url_or_path;
+use deno_core::serde::de;
use deno_core::serde::Deserialize;
use deno_core::serde::Serialize;
use deno_core::serde_json;
@@ -179,6 +180,7 @@ pub struct Request {
pub debug: bool,
pub graph: Arc<Mutex<Graph>>,
pub hash_data: Vec<Vec<u8>>,
+ pub maybe_config_specifier: Option<ModuleSpecifier>,
pub maybe_tsbuildinfo: Option<String>,
/// A vector of strings that represent the root/entry point modules for the
/// program.
@@ -203,6 +205,7 @@ struct State {
hash_data: Vec<Vec<u8>>,
emitted_files: Vec<EmittedFile>,
graph: Arc<Mutex<Graph>>,
+ maybe_config_specifier: Option<ModuleSpecifier>,
maybe_tsbuildinfo: Option<String>,
maybe_response: Option<RespondArgs>,
root_map: HashMap<String, ModuleSpecifier>,
@@ -212,6 +215,7 @@ impl State {
pub fn new(
graph: Arc<Mutex<Graph>>,
hash_data: Vec<Vec<u8>>,
+ maybe_config_specifier: Option<ModuleSpecifier>,
maybe_tsbuildinfo: Option<String>,
root_map: HashMap<String, ModuleSpecifier>,
data_url_map: HashMap<String, ModuleSpecifier>,
@@ -221,6 +225,7 @@ impl State {
hash_data,
emitted_files: Default::default(),
graph,
+ maybe_config_specifier,
maybe_tsbuildinfo,
maybe_response: None,
root_map,
@@ -228,9 +233,16 @@ impl State {
}
}
-fn op<F>(op_fn: F) -> Box<OpFn>
+fn normalize_specifier(specifier: &str) -> Result<ModuleSpecifier, AnyError> {
+ resolve_url_or_path(&specifier.replace(".d.ts.d.ts", ".d.ts"))
+ .map_err(|err| err.into())
+}
+
+fn op<F, V, R>(op_fn: F) -> Box<OpFn>
where
- F: Fn(&mut State, Value) -> Result<Value, AnyError> + 'static,
+ F: Fn(&mut State, V) -> Result<R, AnyError> + 'static,
+ V: de::DeserializeOwned,
+ R: Serialize + 'static,
{
op_sync(move |s, args, _: ()| {
let state = s.borrow_mut::<State>();
@@ -255,6 +267,15 @@ fn op_create_hash(state: &mut State, args: Value) -> Result<Value, AnyError> {
Ok(json!({ "hash": hash }))
}
+fn op_cwd(state: &mut State, _args: Value) -> Result<String, AnyError> {
+ if let Some(config_specifier) = &state.maybe_config_specifier {
+ let cwd = config_specifier.join("./")?;
+ Ok(cwd.to_string())
+ } else {
+ Ok("cache:///".to_string())
+ }
+}
+
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct EmitArgs {
@@ -285,7 +306,7 @@ fn op_emit(state: &mut State, args: Value) -> Result<Value, AnyError> {
} else if let Some(remapped_specifier) = state.root_map.get(s) {
remapped_specifier.clone()
} else {
- resolve_url_or_path(s).unwrap()
+ normalize_specifier(s).unwrap()
}
})
.collect();
@@ -301,6 +322,25 @@ fn op_emit(state: &mut State, args: Value) -> Result<Value, AnyError> {
}
#[derive(Debug, Deserialize)]
+struct ExistsArgs {
+ /// The fully qualified specifier that should be loaded.
+ specifier: String,
+}
+
+fn op_exists(state: &mut State, args: ExistsArgs) -> Result<bool, AnyError> {
+ if let Ok(specifier) = normalize_specifier(&args.specifier) {
+ if specifier.scheme() == "asset" || specifier.scheme() == "data" {
+ Ok(true)
+ } else {
+ let graph = state.graph.lock().unwrap();
+ Ok(graph.contains(&specifier))
+ }
+ } else {
+ Ok(false)
+ }
+}
+
+#[derive(Debug, Deserialize)]
struct LoadArgs {
/// The fully qualified specifier that should be loaded.
specifier: String,
@@ -309,7 +349,7 @@ struct LoadArgs {
fn op_load(state: &mut State, args: Value) -> Result<Value, AnyError> {
let v: LoadArgs = serde_json::from_value(args)
.context("Invalid request from JavaScript for \"op_load\".")?;
- let specifier = resolve_url_or_path(&v.specifier)
+ let specifier = normalize_specifier(&v.specifier)
.context("Error converting a string module specifier for \"op_load\".")?;
let mut hash: Option<String> = None;
let mut media_type = MediaType::Unknown;
@@ -372,7 +412,7 @@ fn op_resolve(state: &mut State, args: Value) -> Result<Value, AnyError> {
} else if let Some(remapped_base) = state.root_map.get(&v.base) {
remapped_base.clone()
} else {
- resolve_url_or_path(&v.base).context(
+ normalize_specifier(&v.base).context(
"Error converting a string module specifier for \"op_resolve\".",
)?
};
@@ -490,14 +530,17 @@ pub fn exec(request: Request) -> Result<Response, AnyError> {
op_state.put(State::new(
request.graph.clone(),
request.hash_data.clone(),
+ request.maybe_config_specifier.clone(),
request.maybe_tsbuildinfo.clone(),
root_map,
data_url_map,
));
}
+ runtime.register_op("op_cwd", op(op_cwd));
runtime.register_op("op_create_hash", op(op_create_hash));
runtime.register_op("op_emit", op(op_emit));
+ runtime.register_op("op_exists", op(op_exists));
runtime.register_op("op_load", op(op_load));
runtime.register_op("op_resolve", op(op_resolve));
runtime.register_op("op_respond", op(op_respond));
@@ -573,6 +616,7 @@ mod tests {
State::new(
graph,
hash_data,
+ None,
maybe_tsbuildinfo,
HashMap::new(),
HashMap::new(),
@@ -614,6 +658,7 @@ mod tests {
debug: false,
graph,
hash_data,
+ maybe_config_specifier: None,
maybe_tsbuildinfo: None,
root_names: vec![(specifier.clone(), MediaType::TypeScript)],
};