Skip to content

Commit 10732e8

Browse files
committed
refactor(language_server): Backend checks the correct LintOptions::Run (#15166)
Because `tsgolint` works now on type, the `Backend` can now be responsible to skip the linting part. This simplifies a lot :)
1 parent e70a37f commit 10732e8

14 files changed

+32
-362
lines changed

crates/oxc_language_server/fixtures/linter/lint_on_run/.oxlintrc.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/oxc_language_server/fixtures/linter/lint_on_run/on_save/on-save.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

crates/oxc_language_server/fixtures/linter/lint_on_run/on_save/on-type.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

crates/oxc_language_server/fixtures/linter/lint_on_run/on_type/on-save-no-type-aware.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

crates/oxc_language_server/fixtures/linter/lint_on_run/on_type/on-save.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

crates/oxc_language_server/fixtures/linter/lint_on_run/on_type/on-type.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

crates/oxc_language_server/src/backend.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323
code_actions::CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC,
2424
commands::{FIX_ALL_COMMAND_ID, FixAllCommandArgs},
2525
file_system::LSPFileSystem,
26-
linter::server_linter::ServerLinterRun,
26+
linter::options::Run,
2727
options::{Options, WorkspaceOption},
2828
worker::WorkspaceWorker,
2929
};
@@ -488,7 +488,11 @@ impl LanguageServer for Backend {
488488
self.file_system.write().await.remove(uri);
489489
}
490490

491-
if let Some(diagnostics) = worker.lint_file(uri, None, ServerLinterRun::OnSave).await {
491+
if !worker.should_lint_on_run_type(Run::OnSave).await {
492+
return;
493+
}
494+
495+
if let Some(diagnostics) = worker.lint_file(uri, None).await {
492496
self.client
493497
.publish_diagnostics(
494498
uri.clone(),
@@ -516,7 +520,11 @@ impl LanguageServer for Backend {
516520
self.file_system.write().await.set(uri, content.clone());
517521
}
518522

519-
if let Some(diagnostics) = worker.lint_file(uri, content, ServerLinterRun::OnType).await {
523+
if !worker.should_lint_on_run_type(Run::OnType).await {
524+
return;
525+
}
526+
527+
if let Some(diagnostics) = worker.lint_file(uri, content).await {
520528
self.client
521529
.publish_diagnostics(
522530
uri.clone(),
@@ -544,9 +552,7 @@ impl LanguageServer for Backend {
544552
self.file_system.write().await.set(uri, content.clone());
545553
}
546554

547-
if let Some(diagnostics) =
548-
worker.lint_file(uri, Some(content), ServerLinterRun::Always).await
549-
{
555+
if let Some(diagnostics) = worker.lint_file(uri, Some(content)).await {
550556
self.client
551557
.publish_diagnostics(
552558
uri.clone(),

crates/oxc_language_server/src/linter/server_linter.rs

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,17 @@ use crate::linter::options::UnusedDisableDirectives;
1818
use crate::linter::{
1919
error_with_position::DiagnosticReport,
2020
isolated_lint_handler::{IsolatedLintHandler, IsolatedLintHandlerOptions},
21-
options::{LintOptions as LSPLintOptions, Run},
21+
options::LintOptions as LSPLintOptions,
2222
};
2323
use crate::utils::normalize_path;
2424
use crate::{ConcurrentHashMap, LINT_CONFIG_FILE};
2525

2626
use super::config_walker::ConfigWalker;
2727

28-
#[derive(Debug, PartialEq, Eq)]
29-
pub enum ServerLinterRun {
30-
OnType,
31-
OnSave,
32-
Always,
33-
}
34-
35-
impl ServerLinterRun {
36-
fn matches(&self, run: Run) -> bool {
37-
matches!(
38-
(self, run),
39-
(ServerLinterRun::OnType, Run::OnType)
40-
| (ServerLinterRun::OnSave, Run::OnSave)
41-
| (ServerLinterRun::Always, _)
42-
)
43-
}
44-
}
45-
4628
pub struct ServerLinter {
4729
isolated_linter: Arc<Mutex<IsolatedLintHandler>>,
4830
ignore_matcher: LintIgnoreMatcher,
4931
gitignore_glob: Vec<Gitignore>,
50-
lint_on_run: Run,
5132
diagnostics: ServerLinterDiagnostics,
5233
extended_paths: FxHashSet<PathBuf>,
5334
}
@@ -172,7 +153,6 @@ impl ServerLinter {
172153
),
173154
gitignore_glob: Self::create_ignore_glob(&root_path),
174155
extended_paths,
175-
lint_on_run: options.run,
176156
diagnostics: ServerLinterDiagnostics::default(),
177157
}
178158
}
@@ -280,9 +260,7 @@ impl ServerLinter {
280260
pub async fn revalidate_diagnostics(&self, uris: Vec<Uri>) -> Vec<(String, Vec<Diagnostic>)> {
281261
let mut diagnostics = Vec::with_capacity(uris.len());
282262
for uri in uris {
283-
if let Some(file_diagnostic) =
284-
self.run_single(&uri, None, ServerLinterRun::Always).await
285-
{
263+
if let Some(file_diagnostic) = self.run_single(&uri, None).await {
286264
diagnostics.push((
287265
uri.to_string(),
288266
file_diagnostic.into_iter().map(|d| d.diagnostic).collect(),
@@ -318,15 +296,7 @@ impl ServerLinter {
318296
&self,
319297
uri: &Uri,
320298
content: Option<String>,
321-
run_type: ServerLinterRun,
322299
) -> Option<Vec<DiagnosticReport>> {
323-
let run = matches!(run_type, ServerLinterRun::Always) || run_type.matches(self.lint_on_run);
324-
325-
// return `None` when both tools do not want to be used
326-
if !run {
327-
return None;
328-
}
329-
330300
if self.is_ignored(uri) {
331301
return None;
332302
}
@@ -461,71 +431,6 @@ mod test {
461431
assert!(result_empty.is_none());
462432
}
463433

464-
#[test]
465-
#[cfg(not(target_endian = "big"))]
466-
fn test_lint_on_run_on_type_on_type() {
467-
Tester::new(
468-
"fixtures/linter/lint_on_run/on_type",
469-
Some(LintOptions {
470-
type_aware: true,
471-
run: Run::OnType,
472-
fix_kind: LintFixKindFlag::All,
473-
..Default::default()
474-
}),
475-
)
476-
.test_and_snapshot_single_file_with_run_type("on-type.ts", Run::OnType);
477-
}
478-
479-
#[test]
480-
#[cfg(not(target_endian = "big"))]
481-
fn test_lint_on_run_on_save_on_save() {
482-
Tester::new(
483-
"fixtures/linter/lint_on_run/on_save",
484-
Some(LintOptions {
485-
type_aware: true,
486-
run: Run::OnType,
487-
fix_kind: LintFixKindFlag::All,
488-
..Default::default()
489-
}),
490-
)
491-
.test_and_snapshot_single_file_with_run_type("on-save.ts", Run::OnSave);
492-
}
493-
494-
#[test]
495-
#[cfg(not(target_endian = "big"))]
496-
fn test_lint_on_run_on_save_on_type() {
497-
Tester::new(
498-
"fixtures/linter/lint_on_run/on_save",
499-
Some(LintOptions { type_aware: true, run: Run::OnSave, ..Default::default() }),
500-
)
501-
.test_and_snapshot_single_file_with_run_type("on-type.ts", Run::OnType);
502-
}
503-
504-
#[test]
505-
#[cfg(not(target_endian = "big"))]
506-
fn test_lint_on_run_on_type_on_save() {
507-
Tester::new(
508-
"fixtures/linter/lint_on_run/on_save",
509-
Some(LintOptions {
510-
type_aware: true,
511-
run: Run::OnType,
512-
fix_kind: LintFixKindFlag::All,
513-
..Default::default()
514-
}),
515-
)
516-
.test_and_snapshot_single_file_with_run_type("on-save.ts", Run::OnSave);
517-
}
518-
519-
#[test]
520-
#[cfg(not(target_endian = "big"))]
521-
fn test_lint_on_run_on_type_on_save_without_type_aware() {
522-
Tester::new(
523-
"fixtures/linter/lint_on_run/on_type",
524-
Some(LintOptions { type_aware: false, run: Run::OnType, ..Default::default() }),
525-
)
526-
.test_and_snapshot_single_file_with_run_type("on-save-no-type-aware.ts", Run::OnSave);
527-
}
528-
529434
#[test]
530435
fn test_no_errors() {
531436
Tester::new("fixtures/linter/no_errors", None)

crates/oxc_language_server/src/snapshots/fixtures_linter_lint_on_run_on_save@on-save.ts.snap

Lines changed: 0 additions & 7 deletions
This file was deleted.

crates/oxc_language_server/src/snapshots/fixtures_linter_lint_on_run_on_save@on-type.ts.snap

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)