Skip to content

Commit f05f4ab

Browse files
committed
refactor(language_server): Backend chekcs the correct LintOptions::Run
1 parent 716cfc9 commit f05f4ab

File tree

14 files changed

+32
-357
lines changed

14 files changed

+32
-357
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.to_string());
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.to_string());
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 & 92 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
}
@@ -171,7 +152,6 @@ impl ServerLinter {
171152
),
172153
gitignore_glob: Self::create_ignore_glob(&root_path),
173154
extended_paths,
174-
lint_on_run: options.run,
175155
diagnostics: ServerLinterDiagnostics::default(),
176156
}
177157
}
@@ -279,9 +259,7 @@ impl ServerLinter {
279259
pub async fn revalidate_diagnostics(&self, uris: Vec<Uri>) -> Vec<(String, Vec<Diagnostic>)> {
280260
let mut diagnostics = Vec::with_capacity(uris.len());
281261
for uri in uris {
282-
if let Some(file_diagnostic) =
283-
self.run_single(&uri, None, ServerLinterRun::Always).await
284-
{
262+
if let Some(file_diagnostic) = self.run_single(&uri, None).await {
285263
diagnostics.push((
286264
uri.to_string(),
287265
file_diagnostic.into_iter().map(|d| d.diagnostic).collect(),
@@ -317,15 +295,7 @@ impl ServerLinter {
317295
&self,
318296
uri: &Uri,
319297
content: Option<String>,
320-
run_type: ServerLinterRun,
321298
) -> Option<Vec<DiagnosticReport>> {
322-
let run = matches!(run_type, ServerLinterRun::Always) || run_type.matches(self.lint_on_run);
323-
324-
// return `None` when both tools do not want to be used
325-
if !run {
326-
return None;
327-
}
328-
329299
if self.is_ignored(uri) {
330300
return None;
331301
}
@@ -460,66 +430,6 @@ mod test {
460430
assert!(result_empty.is_none());
461431
}
462432

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

crates/oxc_language_server/src/snapshots/[email protected]

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

crates/oxc_language_server/src/snapshots/[email protected]

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

0 commit comments

Comments
 (0)