0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-10 14:17:49 -04:00
deno/cli/tools/lint/plugins.rs

562 lines
16 KiB
Rust
Raw Normal View History

2025-01-07 01:05:48 +01:00
// Copyright 2018-2025 the Deno authors. MIT license.
2024-12-01 03:21:16 +01:00
2025-01-02 19:07:55 +01:00
use std::path::Path;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;
2025-01-03 23:52:51 +01:00
use ::tokio_util::sync::CancellationToken;
2024-12-02 01:38:54 +01:00
use deno_ast::ModuleSpecifier;
use deno_ast::ParsedSource;
2024-12-02 18:29:30 +01:00
use deno_ast::SourceTextInfo;
2025-01-09 15:18:03 +01:00
use deno_core::anyhow::bail;
2024-12-01 03:21:16 +01:00
use deno_core::error::AnyError;
2025-01-10 01:28:29 +01:00
use deno_core::error::CoreError;
2025-01-09 14:07:10 +01:00
use deno_core::error::JsError;
2024-12-01 03:21:16 +01:00
use deno_core::futures::FutureExt;
2025-01-02 19:07:55 +01:00
use deno_core::parking_lot::Mutex;
2024-12-04 02:37:04 +01:00
use deno_core::resolve_url_or_path;
2024-12-01 03:21:16 +01:00
use deno_core::v8;
use deno_core::PollEventLoopOptions;
2024-12-02 02:27:30 +01:00
use deno_lint::diagnostic::LintDiagnostic;
2024-12-04 02:37:04 +01:00
use deno_runtime::deno_permissions::Permissions;
use deno_runtime::deno_permissions::PermissionsContainer;
2024-12-02 01:57:49 +01:00
use deno_runtime::tokio_util;
2024-12-04 02:37:04 +01:00
use deno_runtime::worker::MainWorker;
use deno_runtime::WorkerExecutionMode;
2024-12-02 01:38:54 +01:00
use tokio::sync::mpsc::channel;
use tokio::sync::mpsc::Receiver;
use tokio::sync::mpsc::Sender;
2024-12-01 03:21:16 +01:00
2024-12-04 02:59:39 +01:00
use crate::args::DenoSubcommand;
2024-12-04 02:37:04 +01:00
use crate::args::Flags;
2024-12-04 02:59:39 +01:00
use crate::args::LintFlags;
2024-12-04 02:37:04 +01:00
use crate::factory::CliFactory;
2024-12-22 21:20:52 +01:00
use crate::ops::lint::LintPluginContainer;
2024-12-21 02:38:23 +01:00
use crate::tools::lint::serialize_ast_to_buffer;
2024-12-04 02:37:04 +01:00
2024-12-02 01:57:49 +01:00
#[derive(Debug)]
2024-12-26 13:33:47 +01:00
pub enum PluginHostRequest {
2025-01-06 23:45:34 +01:00
LoadPlugins {
specifiers: Vec<ModuleSpecifier>,
exclude_rules: Option<Vec<String>>,
},
Run {
serialized_ast: Vec<u8>,
file_path: PathBuf,
source_text_info: SourceTextInfo,
maybe_token: Option<CancellationToken>,
},
2024-12-02 01:38:54 +01:00
}
2024-12-26 13:33:47 +01:00
pub enum PluginHostResponse {
2025-01-03 23:52:51 +01:00
// TODO: write to structs
2025-01-02 19:07:55 +01:00
LoadPlugin(Result<Vec<PluginInfo>, AnyError>),
2024-12-02 02:27:30 +01:00
Run(Result<Vec<LintDiagnostic>, AnyError>),
}
2024-12-26 13:33:47 +01:00
impl std::fmt::Debug for PluginHostResponse {
2024-12-02 02:27:30 +01:00
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::LoadPlugin(_arg0) => f.debug_tuple("LoadPlugin").finish(),
Self::Run(_arg0) => f.debug_tuple("Run").finish(),
}
}
2024-12-02 01:38:54 +01:00
}
#[derive(Clone)]
pub struct PluginLogger {
print: fn(&str, bool),
debug: bool,
}
impl PluginLogger {
pub fn new(print: fn(&str, bool), debug: bool) -> Self {
Self { print, debug }
}
pub fn log(&self, msg: &str) {
(self.print)(msg, false);
}
pub fn error(&self, msg: &str) {
(self.print)(msg, true);
}
pub fn debug(&self, msg: &str) {
if self.debug {
(self.print)(msg, false);
2025-01-10 02:00:55 +01:00
(self.print)("\n", false);
}
}
}
impl std::fmt::Debug for PluginLogger {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("PluginLogger").field(&self.debug).finish()
}
}
2024-12-26 11:54:49 +01:00
macro_rules! v8_static_strings {
($($ident:ident = $str:literal),* $(,)?) => {
$(
pub static $ident: deno_core::FastStaticString = deno_core::ascii_str!($str);
)*
};
}
v8_static_strings! {
2025-01-07 00:47:43 +01:00
DEFAULT = "default",
INSTALL_PLUGINS = "installPlugins",
2024-12-26 11:54:49 +01:00
RUN_PLUGINS_FOR_FILE = "runPluginsForFile",
}
2024-12-02 01:57:49 +01:00
#[derive(Debug)]
2024-12-26 13:33:47 +01:00
pub struct PluginHostProxy {
tx: Sender<PluginHostRequest>,
rx: Arc<tokio::sync::Mutex<Receiver<PluginHostResponse>>>,
2025-01-02 19:07:55 +01:00
pub(crate) plugin_info: Arc<Mutex<Vec<PluginInfo>>>,
2024-12-03 13:56:47 +01:00
#[allow(unused)]
2024-12-02 01:38:54 +01:00
join_handle: std::thread::JoinHandle<Result<(), AnyError>>,
logger: PluginLogger,
2024-12-02 01:38:54 +01:00
}
2025-01-02 19:07:55 +01:00
impl PluginHostProxy {
pub fn get_plugin_rules(&self) -> Vec<String> {
let infos = self.plugin_info.lock();
let mut all_names = vec![];
for info in infos.iter() {
all_names.extend_from_slice(&info.get_rules());
}
all_names
}
}
2024-12-26 13:33:47 +01:00
pub struct PluginHost {
2024-12-04 02:37:04 +01:00
worker: MainWorker,
2025-01-07 00:47:43 +01:00
install_plugins_fn: Rc<v8::Global<v8::Function>>,
2024-12-26 12:26:44 +01:00
run_plugins_for_file_fn: Rc<v8::Global<v8::Function>>,
2024-12-26 13:33:47 +01:00
tx: Sender<PluginHostResponse>,
rx: Receiver<PluginHostRequest>,
logger: PluginLogger,
2024-12-02 01:38:54 +01:00
}
2024-12-26 12:26:44 +01:00
async fn create_plugin_runner_inner(
logger: PluginLogger,
2024-12-26 13:33:47 +01:00
rx_req: Receiver<PluginHostRequest>,
tx_res: Sender<PluginHostResponse>,
) -> Result<PluginHost, AnyError> {
2024-12-26 12:26:44 +01:00
let flags = Flags {
subcommand: DenoSubcommand::Lint(LintFlags::default()),
..Default::default()
};
let flags = Arc::new(flags);
2025-01-02 19:07:55 +01:00
let factory = CliFactory::from_flags(flags.clone());
2024-12-26 12:26:44 +01:00
let cli_options = factory.cli_options()?;
let main_module =
resolve_url_or_path("./$deno$lint.mts", cli_options.initial_cwd()).unwrap();
2025-01-02 19:07:55 +01:00
let perm_parser = factory.permission_desc_parser()?;
let permissions = Permissions::from_options(
perm_parser.as_ref(),
2025-01-06 23:18:07 +01:00
&cli_options.permissions_options(),
2025-01-02 19:07:55 +01:00
)?;
let permissions = PermissionsContainer::new(perm_parser.clone(), permissions);
2024-12-26 12:26:44 +01:00
// let npm_resolver = factory.npm_resolver().await?.clone();
// let resolver = factory.resolver().await?.clone();
let worker_factory = factory.create_cli_main_worker_factory().await?;
let worker = worker_factory
.create_custom_worker(
// TODO(bartlomieju): add "lint" execution mode
WorkerExecutionMode::Run,
main_module.clone(),
permissions,
vec![crate::ops::lint::deno_lint_ext::init_ops(logger.clone())],
Default::default(),
)
.await?;
let mut worker = worker.into_main_worker();
let runtime = &mut worker.js_runtime;
2025-01-10 02:00:55 +01:00
logger.debug("before loaded");
2024-12-26 12:26:44 +01:00
let obj = runtime.execute_script("lint.js", "Deno[Deno.internal]")?;
2025-01-10 02:00:55 +01:00
logger.debug("After plugin loaded, capturing exports");
2025-01-07 00:47:43 +01:00
let (install_plugins_fn, run_plugins_for_file_fn) = {
2024-12-26 12:26:44 +01:00
let scope = &mut runtime.handle_scope();
let module_exports: v8::Local<v8::Object> =
v8::Local::new(scope, obj).try_into().unwrap();
2025-01-07 00:47:43 +01:00
let install_plugins_fn_name = INSTALL_PLUGINS.v8_string(scope).unwrap();
let install_plugins_fn_val = module_exports
.get(scope, install_plugins_fn_name.into())
2024-12-26 12:26:44 +01:00
.unwrap();
2025-01-07 00:47:43 +01:00
let install_plugins_fn: v8::Local<v8::Function> =
install_plugins_fn_val.try_into().unwrap();
2024-12-26 12:26:44 +01:00
let run_plugins_for_file_fn_name =
RUN_PLUGINS_FOR_FILE.v8_string(scope).unwrap();
let run_plugins_for_file_fn_val = module_exports
.get(scope, run_plugins_for_file_fn_name.into())
.unwrap();
let run_plugins_for_file_fn: v8::Local<v8::Function> =
run_plugins_for_file_fn_val.try_into().unwrap();
(
2025-01-07 00:47:43 +01:00
Rc::new(v8::Global::new(scope, install_plugins_fn)),
2024-12-26 12:26:44 +01:00
Rc::new(v8::Global::new(scope, run_plugins_for_file_fn)),
)
};
2024-12-26 13:33:47 +01:00
Ok(PluginHost {
2024-12-26 12:26:44 +01:00
worker,
2025-01-07 00:47:43 +01:00
install_plugins_fn,
2024-12-26 12:26:44 +01:00
run_plugins_for_file_fn,
tx: tx_res,
rx: rx_req,
logger: logger.clone(),
})
}
2025-01-02 19:07:55 +01:00
#[derive(Debug, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PluginInfo {
pub name: String,
pub rule_names: Vec<String>,
}
impl PluginInfo {
pub fn get_rules(&self) -> Vec<String> {
let mut rules = Vec::with_capacity(self.rule_names.len());
for rule_name in &self.rule_names {
rules.push(format!("{}/{}", self.name, rule_name));
}
rules
}
}
2024-12-26 13:33:47 +01:00
impl PluginHost {
fn create(logger: PluginLogger) -> Result<PluginHostProxy, AnyError> {
2024-12-02 01:38:54 +01:00
let (tx_req, rx_req) = channel(10);
let (tx_res, rx_res) = channel(10);
2025-01-10 02:00:55 +01:00
logger.debug("spawning thread");
let logger_ = logger.clone();
2024-12-02 01:38:54 +01:00
let join_handle = std::thread::spawn(move || {
let logger = logger_;
2025-01-10 02:00:55 +01:00
logger.debug("PluginHost thread spawned");
2024-12-04 04:30:32 +01:00
let start = std::time::Instant::now();
2024-12-04 02:37:04 +01:00
let fut = async move {
2024-12-26 12:26:44 +01:00
let runner =
create_plugin_runner_inner(logger.clone(), rx_req, tx_res).await?;
2024-12-04 02:37:04 +01:00
// TODO(bartlomieju): send "host ready" message to the proxy
2025-01-10 02:00:55 +01:00
logger.debug("running host loop");
2024-12-04 02:37:04 +01:00
runner.run_loop().await?;
2025-01-10 02:00:55 +01:00
logger.debug(&format!(
2024-12-26 13:33:47 +01:00
"PluginHost thread finished, took {:?}",
2024-12-04 04:30:32 +01:00
std::time::Instant::now() - start
));
2024-12-04 02:37:04 +01:00
Ok(())
}
.boxed_local();
tokio_util::create_and_run_current_thread(fut)
2024-12-02 01:38:54 +01:00
});
2025-01-10 02:00:55 +01:00
logger.debug(&format!("is thread finished {}", join_handle.is_finished()));
2024-12-26 13:33:47 +01:00
let proxy = PluginHostProxy {
2024-12-02 01:38:54 +01:00
tx: tx_req,
rx: Arc::new(tokio::sync::Mutex::new(rx_res)),
2025-01-02 19:07:55 +01:00
plugin_info: Arc::new(Mutex::new(vec![])),
2024-12-02 01:38:54 +01:00
join_handle,
logger,
2024-12-02 01:38:54 +01:00
};
Ok(proxy)
}
2024-12-04 02:37:04 +01:00
async fn run_loop(mut self) -> Result<(), AnyError> {
2025-01-10 02:00:55 +01:00
self.logger.debug("waiting for message");
2024-12-04 02:37:04 +01:00
while let Some(req) = self.rx.recv().await {
2025-01-10 02:00:55 +01:00
self.logger.debug("received message");
2024-12-04 02:37:04 +01:00
match req {
2025-01-06 23:45:34 +01:00
PluginHostRequest::LoadPlugins {
specifiers,
exclude_rules,
} => {
let r = self.load_plugins(specifiers, exclude_rules).await;
2024-12-26 13:33:47 +01:00
let _ = self.tx.send(PluginHostResponse::LoadPlugin(r)).await;
2024-12-04 02:37:04 +01:00
}
2025-01-06 23:45:34 +01:00
PluginHostRequest::Run {
2025-01-03 23:52:51 +01:00
serialized_ast,
2025-01-06 23:45:34 +01:00
file_path,
2025-01-03 23:52:51 +01:00
source_text_info,
maybe_token,
2025-01-06 23:45:34 +01:00
} => {
2024-12-04 02:37:04 +01:00
let start = std::time::Instant::now();
let r = match self
2025-01-03 23:52:51 +01:00
.run_plugins(
2025-01-06 23:45:34 +01:00
&file_path,
2025-01-03 23:52:51 +01:00
serialized_ast,
source_text_info,
maybe_token,
)
2024-12-04 02:37:04 +01:00
.await
{
Ok(()) => Ok(self.take_diagnostics()),
Err(err) => Err(err),
};
2025-01-10 02:00:55 +01:00
self.logger.debug(&format!(
2024-12-04 02:37:04 +01:00
"Running rules took {:?}",
std::time::Instant::now() - start
));
2024-12-26 13:33:47 +01:00
let _ = self.tx.send(PluginHostResponse::Run(r)).await;
2024-12-02 01:57:49 +01:00
}
}
}
2025-01-10 02:00:55 +01:00
self.logger.debug("breaking loop");
2024-12-04 02:37:04 +01:00
Ok(())
2024-12-02 01:38:54 +01:00
}
2024-12-02 02:27:30 +01:00
fn take_diagnostics(&mut self) -> Vec<LintDiagnostic> {
2024-12-04 02:37:04 +01:00
let op_state = self.worker.js_runtime.op_state();
2024-12-02 02:27:30 +01:00
let mut state = op_state.borrow_mut();
2024-12-03 13:56:47 +01:00
let container = state.borrow_mut::<LintPluginContainer>();
2024-12-02 02:27:30 +01:00
std::mem::take(&mut container.diagnostics)
}
2024-12-04 04:30:32 +01:00
async fn run_plugins(
2024-12-02 01:38:54 +01:00
&mut self,
2025-01-06 23:45:34 +01:00
file_path: &Path,
2024-12-05 11:21:34 +01:00
serialized_ast: Vec<u8>,
2024-12-02 18:29:30 +01:00
source_text_info: SourceTextInfo,
2025-01-03 23:52:51 +01:00
maybe_token: Option<CancellationToken>,
2024-12-02 01:38:54 +01:00
) -> Result<(), AnyError> {
2024-12-02 18:29:30 +01:00
{
2024-12-04 02:37:04 +01:00
let state = self.worker.js_runtime.op_state();
2024-12-02 18:29:30 +01:00
let mut state = state.borrow_mut();
2025-01-03 23:52:51 +01:00
let container = state.borrow_mut::<LintPluginContainer>();
container.set_info_for_file(
2025-01-06 23:45:34 +01:00
ModuleSpecifier::from_file_path(file_path).unwrap(),
2024-12-26 13:36:50 +01:00
source_text_info,
);
2025-01-03 23:52:51 +01:00
container.set_cancellation_token(maybe_token);
2024-12-02 18:29:30 +01:00
}
2024-12-04 00:31:58 +01:00
2025-01-10 01:28:29 +01:00
let scope = &mut self.worker.js_runtime.handle_scope();
let file_name_v8: v8::Local<v8::Value> =
v8::String::new(scope, &file_path.display().to_string())
.unwrap()
.into();
let store = v8::ArrayBuffer::new_backing_store_from_vec(serialized_ast);
let ast_buf =
v8::ArrayBuffer::with_backing_store(scope, &store.make_shared());
let ast_bin_v8: v8::Local<v8::Value> =
v8::Uint8Array::new(scope, ast_buf, 0, ast_buf.byte_length())
.unwrap()
.into();
let run_plugins_for_file =
v8::Local::new(scope, &*self.run_plugins_for_file_fn);
let undefined = v8::undefined(scope);
let mut tc_scope = v8::TryCatch::new(scope);
let _run_plugins_result = run_plugins_for_file.call(
&mut tc_scope,
undefined.into(),
&[file_name_v8, ast_bin_v8],
2024-12-04 04:30:32 +01:00
);
2025-01-10 01:28:29 +01:00
if let Some(exception) = tc_scope.exception() {
let error = JsError::from_v8_exception(&mut tc_scope, exception);
2025-01-10 02:00:55 +01:00
self.logger.debug("error running plugins");
2025-01-10 01:28:29 +01:00
let core_err = CoreError::Js(error);
return Err(core_err.into());
2024-12-01 04:53:47 +01:00
}
2025-01-10 01:28:29 +01:00
drop(tc_scope);
2024-12-01 04:53:47 +01:00
2025-01-10 02:00:55 +01:00
self.logger.debug("plugins finished");
2024-12-02 01:38:54 +01:00
Ok(())
}
2024-12-01 04:53:47 +01:00
2024-12-02 01:38:54 +01:00
async fn load_plugins(
&mut self,
plugin_specifiers: Vec<ModuleSpecifier>,
exclude: Option<Vec<String>>,
2025-01-02 19:07:55 +01:00
) -> Result<Vec<PluginInfo>, AnyError> {
2024-12-02 01:38:54 +01:00
let mut load_futures = Vec::with_capacity(plugin_specifiers.len());
for specifier in plugin_specifiers {
2024-12-04 02:37:04 +01:00
let mod_id = self
.worker
.js_runtime
.load_side_es_module(&specifier)
.await?;
let mod_future =
self.worker.js_runtime.mod_evaluate(mod_id).boxed_local();
2024-12-02 17:00:49 +01:00
load_futures.push((mod_future, mod_id));
2024-12-02 01:38:54 +01:00
}
self
2024-12-04 02:37:04 +01:00
.worker
.js_runtime
2024-12-02 01:38:54 +01:00
.run_event_loop(PollEventLoopOptions::default())
.await?;
2025-01-07 00:47:43 +01:00
let mut plugin_handles = Vec::with_capacity(load_futures.len());
2025-01-02 19:07:55 +01:00
2024-12-02 17:00:49 +01:00
for (fut, mod_id) in load_futures {
2024-12-03 13:56:47 +01:00
fut.await?;
2024-12-04 02:37:04 +01:00
let module = self.worker.js_runtime.get_module_namespace(mod_id).unwrap();
let scope = &mut self.worker.js_runtime.handle_scope();
2024-12-02 17:00:49 +01:00
let module_local = v8::Local::new(scope, module);
2025-01-07 00:47:43 +01:00
let default_export_str = DEFAULT.v8_string(scope).unwrap();
2024-12-02 17:00:49 +01:00
let default_export =
module_local.get(scope, default_export_str.into()).unwrap();
2025-01-07 00:47:43 +01:00
let default_export_global = v8::Global::new(scope, default_export);
plugin_handles.push(default_export_global);
2024-12-02 01:38:54 +01:00
}
2025-01-07 00:47:43 +01:00
let scope = &mut self.worker.js_runtime.handle_scope();
let install_plugins_local =
v8::Local::new(scope, &*self.install_plugins_fn.clone());
let exclude_v8: v8::Local<v8::Value> =
exclude.map_or(v8::null(scope).into(), |v| {
let elems = v
.iter()
.map(|item| v8::String::new(scope, item).unwrap().into())
.collect::<Vec<_>>();
v8::Array::new_with_elements(scope, elems.as_slice()).into()
});
let undefined = v8::undefined(scope);
let local_handles = {
let arr = v8::Array::new(scope, plugin_handles.len().try_into().unwrap());
for (idx, plugin_handle) in plugin_handles.into_iter().enumerate() {
let handle = v8::Local::new(scope, plugin_handle);
arr
.set_index(scope, idx.try_into().unwrap(), handle)
.unwrap();
}
arr
};
let args = &[local_handles.into(), exclude_v8];
2025-01-10 02:00:55 +01:00
self.logger.debug("Installing plugins...");
2025-01-09 14:07:10 +01:00
let mut tc_scope = v8::TryCatch::new(scope);
let plugins_info_result =
install_plugins_local.call(&mut tc_scope, undefined.into(), args);
if let Some(exception) = tc_scope.exception() {
let error = JsError::from_v8_exception(&mut tc_scope, exception);
return Err(error.into());
}
drop(tc_scope);
let plugins_info = plugins_info_result.unwrap();
2025-01-07 00:47:43 +01:00
let infos: Vec<PluginInfo> =
deno_core::serde_v8::from_v8(scope, plugins_info)?;
self
.logger
2025-01-10 02:26:39 +01:00
.debug(&format!("Plugins installed: {}", infos.len()));
2025-01-07 00:47:43 +01:00
2025-01-02 19:07:55 +01:00
Ok(infos)
2024-12-02 01:38:54 +01:00
}
}
2024-12-26 13:33:47 +01:00
impl PluginHostProxy {
2024-12-02 01:38:54 +01:00
pub async fn load_plugins(
&self,
2025-01-06 23:45:34 +01:00
specifiers: Vec<ModuleSpecifier>,
exclude_rules: Option<Vec<String>>,
2024-12-02 01:38:54 +01:00
) -> Result<(), AnyError> {
self
.tx
2025-01-06 23:45:34 +01:00
.send(PluginHostRequest::LoadPlugins {
specifiers,
exclude_rules,
})
2024-12-02 01:38:54 +01:00
.await?;
let mut rx = self.rx.lock().await;
2025-01-10 02:00:55 +01:00
self.logger.debug("receiving load plugins");
2024-12-02 17:00:49 +01:00
if let Some(val) = rx.recv().await {
2024-12-26 13:33:47 +01:00
let PluginHostResponse::LoadPlugin(result) = val else {
2024-12-02 17:00:49 +01:00
unreachable!()
};
self
.logger
.error(&format!("load plugins response {:#?}", result));
2025-01-02 19:07:55 +01:00
let infos = result?;
*self.plugin_info.lock() = infos;
2024-12-02 01:38:54 +01:00
return Ok(());
2024-12-01 03:40:59 +01:00
}
2025-01-09 15:18:03 +01:00
bail!("Plugin host has closed")
2024-12-01 03:40:59 +01:00
}
2024-12-02 01:38:54 +01:00
pub async fn run_rules(
&self,
2024-12-12 10:53:12 +01:00
specifier: &Path,
2024-12-05 11:21:34 +01:00
serialized_ast: Vec<u8>,
2024-12-02 18:29:30 +01:00
source_text_info: SourceTextInfo,
2025-01-03 23:52:51 +01:00
maybe_token: Option<CancellationToken>,
2024-12-02 02:27:30 +01:00
) -> Result<Vec<LintDiagnostic>, AnyError> {
2024-12-02 01:38:54 +01:00
self
.tx
2025-01-06 23:45:34 +01:00
.send(PluginHostRequest::Run {
2024-12-05 11:21:34 +01:00
serialized_ast,
2025-01-06 23:45:34 +01:00
file_path: specifier.to_path_buf(),
source_text_info,
2025-01-03 23:52:51 +01:00
maybe_token,
2025-01-06 23:45:34 +01:00
})
2024-12-02 01:38:54 +01:00
.await?;
let mut rx = self.rx.lock().await;
2025-01-10 02:00:55 +01:00
self.logger.debug("receiving diagnostics");
2024-12-26 13:33:47 +01:00
if let Some(PluginHostResponse::Run(diagnostics_result)) = rx.recv().await {
2024-12-02 02:27:30 +01:00
return diagnostics_result;
2024-12-02 01:38:54 +01:00
}
2025-01-09 15:18:03 +01:00
bail!("Plugin host has closed")
2024-12-02 01:38:54 +01:00
}
2024-12-26 11:41:21 +01:00
pub fn serialize_ast(
&self,
parsed_source: ParsedSource,
) -> Result<Vec<u8>, AnyError> {
let start = std::time::Instant::now();
let r = serialize_ast_to_buffer(&parsed_source);
self.logger.debug(&format!(
"serialize custom ast took {:?}",
std::time::Instant::now() - start
));
Ok(r)
}
2024-12-02 01:38:54 +01:00
}
pub async fn create_runner_and_load_plugins(
plugin_specifiers: Vec<ModuleSpecifier>,
logger: PluginLogger,
exclude: Option<Vec<String>>,
2024-12-26 13:33:47 +01:00
) -> Result<PluginHostProxy, AnyError> {
let host_proxy = PluginHost::create(logger)?;
host_proxy.load_plugins(plugin_specifiers, exclude).await?;
2024-12-26 13:33:47 +01:00
Ok(host_proxy)
2024-12-02 01:38:54 +01:00
}
pub async fn run_rules_for_ast(
2024-12-26 13:33:47 +01:00
host_proxy: &mut PluginHostProxy,
2024-12-12 10:53:12 +01:00
specifier: &Path,
2024-12-05 11:21:34 +01:00
serialized_ast: Vec<u8>,
2024-12-02 18:29:30 +01:00
source_text_info: SourceTextInfo,
2025-01-03 23:52:51 +01:00
maybe_token: Option<CancellationToken>,
2024-12-02 02:27:30 +01:00
) -> Result<Vec<LintDiagnostic>, AnyError> {
2024-12-26 13:33:47 +01:00
let d = host_proxy
2025-01-03 23:52:51 +01:00
.run_rules(specifier, serialized_ast, source_text_info, maybe_token)
2024-12-02 18:29:30 +01:00
.await?;
2024-12-02 02:27:30 +01:00
Ok(d)
2024-12-01 03:21:16 +01:00
}