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
34 changes: 31 additions & 3 deletions crates/oxc_language_server/src/linter/isolated_lint_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tower_lsp_server::{
};

use oxc_allocator::Allocator;
use oxc_linter::RuntimeFileSystem;
use oxc_linter::{
LINTABLE_EXTENSIONS, LintService, LintServiceOptions, Linter, MessageWithPosition,
loader::Loader, read_to_string,
Expand All @@ -32,6 +33,31 @@ pub struct IsolatedLintHandler {
options: IsolatedLintHandlerOptions,
}

pub struct IsolatedLintHandlerFileSystem {
path_to_lint: PathBuf,
source_text: String,
}

impl IsolatedLintHandlerFileSystem {
pub fn new(path_to_lint: PathBuf, source_text: String) -> Self {
Self { path_to_lint, source_text }
}
}

impl RuntimeFileSystem for IsolatedLintHandlerFileSystem {
fn read_to_string(&self, path: &Path) -> Result<String, std::io::Error> {
if path == self.path_to_lint {
return Ok(self.source_text.clone());
}

read_to_string(path)
}

fn write_file(&self, _path: &Path, _content: String) -> Result<(), std::io::Error> {
panic!("writing file should not be allowed in Language Server");
}
}

impl IsolatedLintHandler {
pub fn new(linter: Linter, options: IsolatedLintHandlerOptions) -> Self {
Self { linter, options }
Expand Down Expand Up @@ -112,9 +138,11 @@ impl IsolatedLintHandler {
)
.with_cross_module(self.options.use_cross_module);
// ToDo: do not clone the linter
let path_arc = Arc::from(path.as_os_str());
let mut lint_service = LintService::new(self.linter.clone(), lint_service_options);
let result = lint_service.run_source(allocator, &path_arc, &source_text);
let mut lint_service =
LintService::new(self.linter.clone(), lint_service_options).with_file_system(Box::new(
IsolatedLintHandlerFileSystem::new(path.to_path_buf(), source_text),
));
let result = lint_service.run_source(allocator);

Some(result)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub use crate::{
options::LintOptions,
options::{AllowWarnDeny, InvalidFilterKind, LintFilter, LintFilterKind},
rule::{RuleCategory, RuleFixMeta, RuleMeta, RuleWithSeverity},
service::{LintService, LintServiceOptions},
service::{LintService, LintServiceOptions, RuntimeFileSystem},
utils::read_to_string,
};
use crate::{
Expand Down
4 changes: 1 addition & 3 deletions crates/oxc_linter/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ impl LintService {
pub fn run_source<'a>(
&mut self,
allocator: &'a oxc_allocator::Allocator,
path: &Arc<OsStr>,
source_text: &str,
) -> Vec<crate::MessageWithPosition<'a>> {
self.runtime.run_source(allocator, path, source_text)
self.runtime.run_source(allocator)
}

/// For tests
Expand Down
32 changes: 5 additions & 27 deletions crates/oxc_linter/src/service/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ pub struct Runtime {
resolver: Option<Resolver>,

pub(super) file_system: Box<dyn RuntimeFileSystem + Sync + Send>,

// The language server uses more up to date source_text provided by `workspace/didChange` request.
// This is required to support `run: "onType"` configuration
#[cfg(feature = "language_server")]
source_text_cache: FxHashMap<Arc<OsStr>, String>,
}

/// Output of `Runtime::process_path`
Expand Down Expand Up @@ -179,8 +174,6 @@ impl Runtime {
linter,
resolver,
file_system: Box::new(OsFileSystem),
#[cfg(feature = "language_server")]
source_text_cache: FxHashMap::default(),
}
}

Expand Down Expand Up @@ -230,13 +223,6 @@ impl Runtime {
}
let source_type = source_type.unwrap_or_default();

// The language server uses more up to date source_text provided by `workspace/didChange` request.
// This is required to support `run: "onType"` configuration
#[cfg(feature = "language_server")]
if let Some(source_text) = self.source_text_cache.get(path.as_os_str()) {
return Some(Ok((source_type, source_text.clone())));
}

let file_result = self.file_system.read_to_string(path).map_err(|e| {
Error::new(OxcDiagnostic::error(format!(
"Failed to open file {path:?} with error \"{e}\""
Expand Down Expand Up @@ -563,22 +549,14 @@ impl Runtime {
pub(super) fn run_source<'a>(
&mut self,
allocator: &'a oxc_allocator::Allocator,
path: &Arc<OsStr>,
source_text: &str,
) -> Vec<MessageWithPosition<'a>> {
use std::sync::Mutex;

// the language server can have more up to date source_text then the filesystem
#[cfg(feature = "language_server")]
{
self.source_text_cache.insert(Arc::clone(path), source_text.to_owned());
}

let messages = Mutex::new(Vec::<MessageWithPosition<'a>>::new());
let (sender, _receiver) = mpsc::channel();
rayon::scope(|scope| {
self.resolve_modules(scope, true, &sender, |me, mut module| {
module.content.with_dependent_mut(|_owner, dependent| {
module.content.with_dependent_mut(|owner, dependent| {
assert_eq!(module.section_module_records.len(), dependent.len());

for (record_result, section) in
Expand Down Expand Up @@ -609,13 +587,13 @@ impl Runtime {
let offset = labeled_span.offset() as u32;
let start_position = offset_to_position(
offset + section.source.start,
source_text,
&owner.source_text,
);
let end_position = offset_to_position(
offset
+ section.source.start
+ labeled_span.len() as u32,
source_text,
&owner.source_text,
);
let message = labeled_span
.label()
Expand All @@ -642,11 +620,11 @@ impl Runtime {
span: SpanPositionMessage::new(
offset_to_position(
section.source.start + fix.span.start,
source_text,
&owner.source_text,
),
offset_to_position(
section.source.start + fix.span.end,
source_text,
&owner.source_text,
),
)
.with_message(
Expand Down
Loading