Skip to content

Commit db0b099

Browse files
committed
refactor(language_server): convert only once uri to path when creating ServerLinter (#11503)
1 parent 0946dac commit db0b099

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

crates/oxc_language_server/src/linter/server_linter.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub struct ServerLinter {
2626

2727
impl ServerLinter {
2828
pub fn new(root_uri: &Uri, options: &Options) -> Self {
29-
let (nested_configs, mut extended_paths) = Self::create_nested_configs(root_uri, options);
3029
let root_path = root_uri.to_file_path().unwrap();
30+
let (nested_configs, mut extended_paths) = Self::create_nested_configs(&root_path, options);
3131
let relative_config_path = options.config_path.clone();
3232
let oxlintrc = if let Some(relative_config_path) = relative_config_path {
3333
let config = normalize_path(root_path.join(relative_config_path));
@@ -89,15 +89,15 @@ impl ServerLinter {
8989

9090
Self {
9191
isolated_linter: Arc::new(isolated_linter),
92-
gitignore_glob: Self::create_ignore_glob(root_uri, &oxlintrc),
92+
gitignore_glob: Self::create_ignore_glob(&root_path, &oxlintrc),
9393
extended_paths,
9494
}
9595
}
9696

9797
/// Searches inside root_uri recursively for the default oxlint config files
9898
/// and insert them inside the nested configuration
9999
fn create_nested_configs(
100-
root_uri: &Uri,
100+
root_path: &Path,
101101
options: &Options,
102102
) -> (ConcurrentHashMap<PathBuf, Config>, Vec<PathBuf>) {
103103
let mut extended_paths = Vec::new();
@@ -106,9 +106,7 @@ impl ServerLinter {
106106
return (ConcurrentHashMap::default(), extended_paths);
107107
}
108108

109-
let root_path = root_uri.to_file_path().expect("Failed to convert URI to file path");
110-
111-
let paths = ConfigWalker::new(&root_path).paths();
109+
let paths = ConfigWalker::new(root_path).paths();
112110
let nested_configs =
113111
ConcurrentHashMap::with_capacity_and_hasher(paths.capacity(), FxBuildHasher);
114112

@@ -134,15 +132,15 @@ impl ServerLinter {
134132
(nested_configs, extended_paths)
135133
}
136134

137-
fn create_ignore_glob(root_uri: &Uri, oxlintrc: &Oxlintrc) -> Vec<Gitignore> {
135+
fn create_ignore_glob(root_path: &Path, oxlintrc: &Oxlintrc) -> Vec<Gitignore> {
138136
let mut builder = globset::GlobSetBuilder::new();
139137
// Collecting all ignore files
140138
builder.add(Glob::new("**/.eslintignore").unwrap());
141139
builder.add(Glob::new("**/.gitignore").unwrap());
142140

143141
let ignore_file_glob_set = builder.build().unwrap();
144142

145-
let walk = ignore::WalkBuilder::new(root_uri.to_file_path().unwrap())
143+
let walk = ignore::WalkBuilder::new(root_path)
146144
.ignore(true)
147145
.hidden(false)
148146
.git_global(false)
@@ -233,19 +231,14 @@ pub fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
233231

234232
#[cfg(test)]
235233
mod test {
236-
use std::{
237-
path::{Path, PathBuf},
238-
str::FromStr,
239-
};
240-
241-
use rustc_hash::FxHashMap;
242-
use tower_lsp_server::lsp_types::Uri;
234+
use std::path::{Path, PathBuf};
243235

244236
use crate::{
245237
Options,
246238
linter::server_linter::{ServerLinter, normalize_path},
247-
tester::{Tester, get_file_uri},
239+
tester::{Tester, get_file_path},
248240
};
241+
use rustc_hash::FxHashMap;
249242

250243
#[test]
251244
fn test_normalize_path() {
@@ -261,7 +254,7 @@ mod test {
261254
flags.insert("disable_nested_configs".to_string(), "true".to_string());
262255

263256
let (configs, _) = ServerLinter::create_nested_configs(
264-
&Uri::from_str("file:///root/").unwrap(),
257+
Path::new("/root/"),
265258
&Options { flags, ..Options::default() },
266259
);
267260

@@ -271,7 +264,7 @@ mod test {
271264
#[test]
272265
fn test_create_nested_configs() {
273266
let (configs, _) = ServerLinter::create_nested_configs(
274-
&get_file_uri("fixtures/linter/init_nested_configs"),
267+
&get_file_path("fixtures/linter/init_nested_configs"),
275268
&Options::default(),
276269
);
277270
let configs = configs.pin();

crates/oxc_language_server/src/tester.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt::Write;
1+
use std::{fmt::Write, path::PathBuf};
22

33
use tower_lsp_server::{
44
UriExt,
@@ -9,11 +9,15 @@ use crate::{Options, worker::WorkspaceWorker};
99

1010
use super::linter::error_with_position::DiagnosticReport;
1111

12+
/// Given a file path relative to the crate root directory, return the absolute path of the file.
13+
pub fn get_file_path(relative_file_path: &str) -> PathBuf {
14+
std::env::current_dir().expect("could not get current dir").join(relative_file_path)
15+
}
16+
1217
/// Given a file path relative to the crate root directory, return the URI of the file.
1318
pub fn get_file_uri(relative_file_path: &str) -> Uri {
14-
let absolute_file_path =
15-
std::env::current_dir().expect("could not get current dir").join(relative_file_path);
16-
Uri::from_file_path(absolute_file_path).expect("failed to convert file path to URL")
19+
Uri::from_file_path(get_file_path(relative_file_path))
20+
.expect("failed to convert file path to URL")
1721
}
1822

1923
fn get_snapshot_from_report(report: &DiagnosticReport) -> String {

0 commit comments

Comments
 (0)