diff --git a/cli/lsp/analysis.rs b/cli/lsp/analysis.rs index 968875e48a..982260ee09 100644 --- a/cli/lsp/analysis.rs +++ b/cli/lsp/analysis.rs @@ -36,6 +36,7 @@ use node_resolver::ResolutionMode; use once_cell::sync::Lazy; use regex::Regex; use text_lines::LineAndColumnIndex; +use tokio_util::sync::CancellationToken; use tower_lsp::lsp_types as lsp; use tower_lsp::lsp_types::Position; use tower_lsp::lsp_types::Range; @@ -184,8 +185,9 @@ fn as_lsp_range( pub fn get_lint_references( parsed_source: &deno_ast::ParsedSource, linter: &CliLinter, + token: CancellationToken, ) -> Result, AnyError> { - let lint_diagnostics = linter.lint_with_ast(parsed_source); + let lint_diagnostics = linter.lint_with_ast(parsed_source, token); Ok( lint_diagnostics diff --git a/cli/lsp/diagnostics.rs b/cli/lsp/diagnostics.rs index 2293880806..5ee2acc929 100644 --- a/cli/lsp/diagnostics.rs +++ b/cli/lsp/diagnostics.rs @@ -851,6 +851,7 @@ fn generate_lint_diagnostics( &document, &lint_config, &linter, + token.clone(), ), }, }); @@ -862,6 +863,7 @@ fn generate_document_lint_diagnostics( document: &Document, lint_config: &LintConfig, linter: &CliLinter, + token: CancellationToken, ) -> Vec { if !lint_config.files.matches_specifier(document.specifier()) { return Vec::new(); @@ -869,7 +871,7 @@ fn generate_document_lint_diagnostics( match document.maybe_parsed_source() { Some(Ok(parsed_source)) => { if let Ok(references) = - analysis::get_lint_references(parsed_source, linter) + analysis::get_lint_references(parsed_source, linter, token) { references .into_iter() diff --git a/cli/ops/lint.rs b/cli/ops/lint.rs index 7579b0de18..fb8e2f87a4 100644 --- a/cli/ops/lint.rs +++ b/cli/ops/lint.rs @@ -51,6 +51,14 @@ pub struct LintPluginContainer { } impl LintPluginContainer { + pub fn set_cancellation_token( + &mut self, + maybe_token: Option, + ) { + let token = maybe_token.unwrap_or_default(); + self.token = token; + } + pub fn set_info_for_file( &mut self, specifier: ModuleSpecifier, diff --git a/cli/tools/lint/linter.rs b/cli/tools/lint/linter.rs index bfdd3001fe..dd3ca48d52 100644 --- a/cli/tools/lint/linter.rs +++ b/cli/tools/lint/linter.rs @@ -5,6 +5,7 @@ use std::path::Path; use std::path::PathBuf; use std::sync::Arc; +use ::tokio_util::sync::CancellationToken; use deno_ast::MediaType; use deno_ast::ModuleSpecifier; use deno_ast::ParsedSource; @@ -96,6 +97,7 @@ impl CliLinter { pub fn lint_with_ast( &self, parsed_source: &ParsedSource, + token: CancellationToken, ) -> Vec { // TODO(bartlomieju): surface error is running plugin fails let external_linter: Option = @@ -103,7 +105,12 @@ impl CliLinter { Some(Arc::new(move |parsed_source: ParsedSource| { // TODO: clean this up let file_path = parsed_source.specifier().to_file_path().unwrap(); - run_plugins(plugin_runner.clone(), parsed_source, file_path) + run_plugins( + plugin_runner.clone(), + parsed_source, + file_path, + Some(token.clone()), + ) })) } else { None @@ -137,7 +144,7 @@ impl CliLinter { Some(Arc::new(move |parsed_source: ParsedSource| { // TODO: clean this up let file_path = parsed_source.specifier().to_file_path().unwrap(); - run_plugins(plugin_runner.clone(), parsed_source, file_path) + run_plugins(plugin_runner.clone(), parsed_source, file_path, None) })) } else { None @@ -315,6 +322,7 @@ fn run_plugins( plugin_runner: Arc>, parsed_source: ParsedSource, file_path: PathBuf, + maybe_token: Option, ) -> Result { let source_text_info = parsed_source.text_info_lazy().clone(); let plugin_info = plugin_runner.lock().get_plugin_rules(); @@ -329,6 +337,7 @@ fn run_plugins( &file_path, serialized_ast, source_text_info, + maybe_token, ) .await } diff --git a/cli/tools/lint/plugins.rs b/cli/tools/lint/plugins.rs index a41f02940b..8ed02e52cd 100644 --- a/cli/tools/lint/plugins.rs +++ b/cli/tools/lint/plugins.rs @@ -5,6 +5,7 @@ use std::path::PathBuf; use std::rc::Rc; use std::sync::Arc; +use ::tokio_util::sync::CancellationToken; use deno_ast::ModuleSpecifier; use deno_ast::ParsedSource; use deno_ast::SourceTextInfo; @@ -34,11 +35,13 @@ use crate::tools::lint::serialize_ast_to_buffer; #[derive(Debug)] pub enum PluginHostRequest { + // TODO: write to structs LoadPlugins(Vec, Option>), - Run(Vec, PathBuf, SourceTextInfo), + Run(Vec, PathBuf, SourceTextInfo, Option), } pub enum PluginHostResponse { + // TODO: write to structs LoadPlugin(Result, AnyError>), Run(Result, AnyError>), } @@ -276,10 +279,20 @@ impl PluginHost { let r = self.load_plugins(specifiers, exclude).await; let _ = self.tx.send(PluginHostResponse::LoadPlugin(r)).await; } - PluginHostRequest::Run(serialized_ast, specifier, source_text_info) => { + PluginHostRequest::Run( + serialized_ast, + specifier, + source_text_info, + maybe_token, + ) => { let start = std::time::Instant::now(); let r = match self - .run_plugins(&specifier, serialized_ast, source_text_info) + .run_plugins( + &specifier, + serialized_ast, + source_text_info, + maybe_token, + ) .await { Ok(()) => Ok(self.take_diagnostics()), @@ -309,14 +322,17 @@ impl PluginHost { specifier: &Path, serialized_ast: Vec, source_text_info: SourceTextInfo, + maybe_token: Option, ) -> Result<(), AnyError> { { let state = self.worker.js_runtime.op_state(); let mut state = state.borrow_mut(); - state.borrow_mut::().set_info_for_file( + let container = state.borrow_mut::(); + container.set_info_for_file( ModuleSpecifier::from_file_path(specifier).unwrap(), source_text_info, ); + container.set_cancellation_token(maybe_token); } let (file_name_v8, ast_uint8arr_v8) = { @@ -452,6 +468,7 @@ impl PluginHostProxy { specifier: &Path, serialized_ast: Vec, source_text_info: SourceTextInfo, + maybe_token: Option, ) -> Result, AnyError> { self .tx @@ -459,6 +476,7 @@ impl PluginHostProxy { serialized_ast, specifier.to_path_buf(), source_text_info, + maybe_token, )) .await?; let mut rx = self.rx.lock().await; @@ -498,9 +516,10 @@ pub async fn run_rules_for_ast( specifier: &Path, serialized_ast: Vec, source_text_info: SourceTextInfo, + maybe_token: Option, ) -> Result, AnyError> { let d = host_proxy - .run_rules(specifier, serialized_ast, source_text_info) + .run_rules(specifier, serialized_ast, source_text_info, maybe_token) .await?; Ok(d) }