0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-09 13:49:37 -04:00
deno/cli/ops/lint.rs

184 lines
4.5 KiB
Rust
Raw Normal View History

2025-01-01 04:12:39 +09:00
// Copyright 2018-2025 the Deno authors. MIT license.
use deno_ast::MediaType;
use deno_ast::ModuleSpecifier;
2024-12-22 21:20:52 +01:00
use deno_ast::SourceRange;
use deno_ast::SourceTextInfo;
use deno_ast::SourceTextProvider;
use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::op2;
2024-12-22 21:20:52 +01:00
use deno_core::OpState;
use deno_lint::diagnostic::LintDiagnostic;
use deno_lint::diagnostic::LintDiagnosticDetails;
use deno_lint::diagnostic::LintDiagnosticRange;
2025-01-02 16:28:13 +01:00
use deno_lint::diagnostic::LintFix;
use deno_lint::diagnostic::LintFixChange;
use tokio_util::sync::CancellationToken;
use crate::tools::lint;
use crate::tools::lint::PluginLogger;
2024-12-22 21:20:52 +01:00
deno_core::extension!(
deno_lint_ext,
ops = [
op_lint_create_serialized_ast,
op_lint_report,
op_lint_get_source,
op_is_cancelled
2024-12-22 21:20:52 +01:00
],
options = {
logger: PluginLogger,
},
2024-12-26 11:26:04 +01:00
// TODO(bartlomieju): this should only be done,
// if not in the "test worker".
middleware = |op| match op.name {
"op_print" => op_print(),
_ => op,
},
state = |state, options| {
state.put(options.logger);
2024-12-22 21:20:52 +01:00
state.put(LintPluginContainer::default());
},
2024-12-22 21:20:52 +01:00
);
#[derive(Default)]
pub struct LintPluginContainer {
pub diagnostics: Vec<LintDiagnostic>,
pub source_text_info: Option<SourceTextInfo>,
2024-12-26 11:26:04 +01:00
pub specifier: Option<ModuleSpecifier>,
pub token: CancellationToken,
2024-12-22 21:20:52 +01:00
}
impl LintPluginContainer {
2024-12-26 13:36:50 +01:00
pub fn set_info_for_file(
&mut self,
specifier: ModuleSpecifier,
source_text_info: SourceTextInfo,
) {
self.specifier = Some(specifier);
self.source_text_info = Some(source_text_info);
}
fn report(
&mut self,
id: String,
message: String,
hint: Option<String>,
start: usize,
end: usize,
2025-01-02 16:28:13 +01:00
fix: Option<LintReportFix>,
2024-12-26 13:36:50 +01:00
) {
2024-12-22 21:20:52 +01:00
let source_text_info = self.source_text_info.as_ref().unwrap();
2024-12-26 11:26:04 +01:00
let specifier = self.specifier.clone().unwrap();
2024-12-22 21:20:52 +01:00
let start_pos = source_text_info.start_pos();
let source_range = SourceRange::new(start_pos + start, start_pos + end);
let range = LintDiagnosticRange {
range: source_range,
description: None,
text_info: source_text_info.clone(),
};
2025-01-02 16:28:13 +01:00
let mut fixes: Vec<LintFix> = vec![];
if let Some(fix) = fix {
fixes.push(LintFix {
changes: vec![LintFixChange {
new_text: fix.text.into(),
range: SourceRange::new(
start_pos + fix.range.0,
start_pos + fix.range.1,
),
}],
description: format!("Fix this {} problem", id).into(),
});
}
2024-12-22 21:20:52 +01:00
let lint_diagnostic = LintDiagnostic {
2024-12-26 11:26:04 +01:00
specifier,
2024-12-22 21:20:52 +01:00
range: Some(range),
details: LintDiagnosticDetails {
message,
code: id,
2024-12-26 13:36:50 +01:00
hint,
2025-01-02 16:28:13 +01:00
fixes,
2024-12-22 21:20:52 +01:00
custom_docs_url: None,
info: vec![],
},
};
self.diagnostics.push(lint_diagnostic);
}
}
#[op2(fast)]
pub fn op_print(state: &mut OpState, #[string] msg: &str, is_err: bool) {
let logger = state.borrow::<PluginLogger>();
if is_err {
logger.error(msg);
} else {
logger.log(msg);
}
}
#[op2(fast)]
fn op_is_cancelled(state: &mut OpState) -> bool {
let container = state.borrow_mut::<LintPluginContainer>();
container.token.is_cancelled()
}
#[op2]
#[buffer]
fn op_lint_create_serialized_ast(
#[string] file_name: &str,
#[string] source: String,
) -> Result<Vec<u8>, AnyError> {
let file_text = deno_ast::strip_bom(source);
let path = std::env::current_dir()?.join(file_name);
let specifier = ModuleSpecifier::from_file_path(&path).map_err(|_| {
generic_error(format!("Failed to parse path as URL: {}", path.display()))
})?;
let media_type = MediaType::from_specifier(&specifier);
let parsed_source = deno_ast::parse_program(deno_ast::ParseParams {
specifier,
text: file_text.into(),
media_type,
capture_tokens: false,
scope_analysis: false,
maybe_syntax: None,
})?;
Ok(lint::serialize_ast_to_buffer(&parsed_source))
}
2024-12-22 21:20:52 +01:00
2025-01-02 16:28:13 +01:00
#[derive(serde::Deserialize)]
struct LintReportFix {
text: String,
range: (usize, usize),
}
2024-12-26 13:36:50 +01:00
#[op2]
2024-12-22 21:20:52 +01:00
fn op_lint_report(
state: &mut OpState,
#[string] id: String,
#[string] message: String,
2024-12-26 13:36:50 +01:00
#[string] hint: Option<String>,
2024-12-22 21:20:52 +01:00
#[smi] start: usize,
#[smi] end: usize,
2025-01-02 16:28:13 +01:00
#[serde] fix: Option<LintReportFix>,
2024-12-22 21:20:52 +01:00
) {
let container = state.borrow_mut::<LintPluginContainer>();
2025-01-02 16:28:13 +01:00
container.report(id, message, hint, start, end, fix);
2024-12-22 21:20:52 +01:00
}
#[op2]
#[string]
fn op_lint_get_source(state: &mut OpState) -> String {
let container = state.borrow_mut::<LintPluginContainer>();
container
.source_text_info
.as_ref()
.unwrap()
.text_str()
.to_string()
}