From 8c6e0a6fa39fc0c1abbca51af0f9bdedcb5a44eb Mon Sep 17 00:00:00 2001 From: "Alexander S." Date: Sat, 18 Jan 2025 04:49:21 +0100 Subject: [PATCH] refactor(language_server): simplify IsolatedLintHandler (#8183) the `PathBuf` is only leeded in line 47. I do not know why the other methods should handle it and return the result. And a simple fast return. --- .../src/linter/isolated_lint_handler.rs | 101 +++++++++--------- 1 file changed, 51 insertions(+), 50 deletions(-) diff --git a/crates/oxc_language_server/src/linter/isolated_lint_handler.rs b/crates/oxc_language_server/src/linter/isolated_lint_handler.rs index 3c2bb5ad1823d..b6324844d4801 100644 --- a/crates/oxc_language_server/src/linter/isolated_lint_handler.rs +++ b/crates/oxc_language_server/src/linter/isolated_lint_handler.rs @@ -1,6 +1,6 @@ use std::{ fs, - path::{Path, PathBuf}, + path::Path, rc::Rc, sync::{Arc, OnceLock}, }; @@ -38,56 +38,59 @@ impl IsolatedLintHandler { path: &Path, content: Option, ) -> Option> { - if Self::should_lint_path(path) { - Some(self.lint_path(path, content).map_or(vec![], |(p, errors)| { - let mut diagnostics: Vec = - errors.into_iter().map(|e| e.into_diagnostic_report(&p)).collect(); - // a diagnostics connected from related_info to original diagnostic - let mut inverted_diagnostics = vec![]; - for d in &diagnostics { - let Some(ref related_info) = d.diagnostic.related_information else { + if !Self::should_lint_path(path) { + return None; + } + + Some(self.lint_path(path, content).map_or(vec![], |errors| { + let path_buf = &path.to_path_buf(); + + let mut diagnostics: Vec = + errors.into_iter().map(|e| e.into_diagnostic_report(path_buf)).collect(); + + // a diagnostics connected from related_info to original diagnostic + let mut inverted_diagnostics = vec![]; + for d in &diagnostics { + let Some(ref related_info) = d.diagnostic.related_information else { + continue; + }; + let related_information = Some(vec![DiagnosticRelatedInformation { + location: lsp_types::Location { + uri: lsp_types::Url::from_file_path(path).unwrap(), + range: d.diagnostic.range, + }, + message: "original diagnostic".to_string(), + }]); + for r in related_info { + if r.location.range == d.diagnostic.range { continue; - }; - let related_information = Some(vec![DiagnosticRelatedInformation { - location: lsp_types::Location { - uri: lsp_types::Url::from_file_path(path).unwrap(), - range: d.diagnostic.range, - }, - message: "original diagnostic".to_string(), - }]); - for r in related_info { - if r.location.range == d.diagnostic.range { - continue; - } - inverted_diagnostics.push(DiagnosticReport { - diagnostic: lsp_types::Diagnostic { - range: r.location.range, - severity: Some(DiagnosticSeverity::HINT), - code: None, - message: r.message.clone(), - source: d.diagnostic.source.clone(), - code_description: None, - related_information: related_information.clone(), - tags: None, - data: None, - }, - fixed_content: None, - }); } + inverted_diagnostics.push(DiagnosticReport { + diagnostic: lsp_types::Diagnostic { + range: r.location.range, + severity: Some(DiagnosticSeverity::HINT), + code: None, + message: r.message.clone(), + source: d.diagnostic.source.clone(), + code_description: None, + related_information: related_information.clone(), + tags: None, + data: None, + }, + fixed_content: None, + }); } - diagnostics.append(&mut inverted_diagnostics); - diagnostics - })) - } else { - None - } + } + diagnostics.append(&mut inverted_diagnostics); + diagnostics + })) } fn lint_path( &self, path: &Path, source_text: Option, - ) -> Option<(PathBuf, Vec)> { + ) -> Option> { if !Loader::can_load(path) { debug!("extension not supported yet."); return None; @@ -170,12 +173,10 @@ impl IsolatedLintHandler { ErrorReport { error: Error::from(msg.error), fixed_content } }) .collect::>(); - let (_, errors_with_position) = - Self::wrap_diagnostics(path, &source_text, reports, start); - diagnostics.extend(errors_with_position); + diagnostics.extend(Self::wrap_diagnostics(path, &source_text, reports, start)); } - Some((path.to_path_buf(), diagnostics)) + Some(diagnostics) } fn should_lint_path(path: &Path) -> bool { @@ -194,9 +195,10 @@ impl IsolatedLintHandler { source_text: &str, reports: Vec, start: u32, - ) -> (PathBuf, Vec) { + ) -> Vec { let source = Arc::new(NamedSource::new(path.to_string_lossy(), source_text.to_owned())); - let diagnostics = reports + + reports .into_iter() .map(|report| { ErrorWithPosition::new( @@ -206,7 +208,6 @@ impl IsolatedLintHandler { start as usize, ) }) - .collect(); - (path.to_path_buf(), diagnostics) + .collect() } }