Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 46 additions & 58 deletions crates/oxc_language_server/src/linter/isolated_lint_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tower_lsp_server::{
use oxc_allocator::Allocator;
use oxc_linter::{
ConfigStore, LINTABLE_EXTENSIONS, LintOptions, LintService, LintServiceOptions, Linter,
MessageWithPosition, loader::Loader, read_to_arena_str,
MessageWithPosition, read_to_arena_str,
};
use oxc_linter::{RuntimeFileSystem, read_to_string};

Expand Down Expand Up @@ -93,79 +93,67 @@ impl IsolatedLintHandler {
}

let mut allocator = Allocator::default();

Some(self.lint_path(&mut allocator, &path, content).map_or(vec![], |errors| {
let mut diagnostics: Vec<DiagnosticReport> = errors
.iter()
.map(|e| message_with_position_to_lsp_diagnostic_report(e, uri))
.collect();

// a diagnostics connected from related_info to original diagnostic
let mut inverted_diagnostics = vec![];
for d in &diagnostics {
let Some(related_info) = &d.diagnostic.related_information else {
let source_text = content.or_else(|| read_to_string(&path).ok())?;
let errors = self.lint_path(&mut allocator, &path, source_text);

let mut diagnostics: Vec<DiagnosticReport> =
errors.iter().map(|e| message_with_position_to_lsp_diagnostic_report(e, uri)).collect();

// a diagnostics connected from related_info to original diagnostic
let mut inverted_diagnostics = vec![];
for d in &diagnostics {
let Some(related_info) = &d.diagnostic.related_information else {
continue;
};
let related_information = Some(vec![DiagnosticRelatedInformation {
location: lsp_types::Location { uri: uri.clone(), 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: uri.clone(), range: d.diagnostic.range },
message: "original diagnostic".to_string(),
}]);
for r in related_info {
if r.location.range == d.diagnostic.range {
continue;
}
// If there is no message content for this span, then don't produce an additional diagnostic
// which also has no content. This prevents issues where editors expect diagnostics to have messages.
if r.message.is_empty() {
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: PossibleFixContent::None,
rule_name: None,
});
}
// If there is no message content for this span, then don't produce an additional diagnostic
// which also has no content. This prevents issues where editors expect diagnostics to have messages.
if r.message.is_empty() {
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: PossibleFixContent::None,
rule_name: None,
});
}
diagnostics.append(&mut inverted_diagnostics);
diagnostics
}))
}
diagnostics.append(&mut inverted_diagnostics);
Some(diagnostics)
}

fn lint_path<'a>(
&mut self,
allocator: &'a mut Allocator,
path: &Path,
source_text: Option<String>,
) -> Option<Vec<MessageWithPosition<'a>>> {
if !Loader::can_load(path) {
debug!("extension not supported yet.");
return None;
}

let source_text = source_text.or_else(|| read_to_string(path).ok())?;

source_text: String,
) -> Vec<MessageWithPosition<'a>> {
debug!("lint {}", path.display());

let result = self
.service
self.service
.with_file_system(Box::new(IsolatedLintHandlerFileSystem::new(
path.to_path_buf(),
source_text,
)))
.with_paths(vec![Arc::from(path.as_os_str())])
.run_source(allocator);

Some(result)
.run_source(allocator)
}

fn should_lint_path(path: &Path) -> bool {
Expand Down
35 changes: 17 additions & 18 deletions editors/vscode/tests/e2e_server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ import {
getDiagnostics,
loadFixture,
sleep,
testMultiFolderMode,
testSingleFolderMode,
waitForDiagnosticChange,
WORKSPACE_DIR,
WORKSPACE_SECOND_DIR
WORKSPACE_DIR
} from './test-helpers';
import assert = require('assert');

Expand Down Expand Up @@ -170,21 +168,22 @@ suite('E2E Diagnostics', () => {
strictEqual(nestedDiagnostics[0].severity, DiagnosticSeverity.Error);
});

testMultiFolderMode('different diagnostic severity', async () => {
await loadFixture('debugger', WORKSPACE_DIR);
await loadFixture('debugger_error', WORKSPACE_SECOND_DIR);

const firstDiagnostics = await getDiagnostics('debugger.js', WORKSPACE_DIR);
const secondDiagnostics = await getDiagnostics('debugger.js', WORKSPACE_SECOND_DIR);

assert(typeof firstDiagnostics[0].code == 'object');
strictEqual(firstDiagnostics[0].code.target.authority, 'oxc.rs');
strictEqual(firstDiagnostics[0].severity, DiagnosticSeverity.Warning);

assert(typeof secondDiagnostics[0].code == 'object');
strictEqual(secondDiagnostics[0].code.target.authority, 'oxc.rs');
strictEqual(secondDiagnostics[0].severity, DiagnosticSeverity.Error);
});
// somehow this test is flaky in CI
// testMultiFolderMode('different diagnostic severity', async () => {
// await loadFixture('debugger', WORKSPACE_DIR);
// await loadFixture('debugger_error', WORKSPACE_SECOND_DIR);
//
// const firstDiagnostics = await getDiagnostics('debugger.js', WORKSPACE_DIR);
// const secondDiagnostics = await getDiagnostics('debugger.js', WORKSPACE_SECOND_DIR);
//
// assert(typeof firstDiagnostics[0].code == 'object');
// strictEqual(firstDiagnostics[0].code.target.authority, 'oxc.rs');
// strictEqual(firstDiagnostics[0].severity, DiagnosticSeverity.Warning);
//
// assert(typeof secondDiagnostics[0].code == 'object');
// strictEqual(secondDiagnostics[0].code.target.authority, 'oxc.rs');
// strictEqual(secondDiagnostics[0].severity, DiagnosticSeverity.Error);
// });

// somehow this test is flaky in CI
test.skip('changing config from `extends` will revalidate the diagnostics', async () => {
Expand Down
Loading