Skip to content

Commit 7708d56

Browse files
autofix-ci[bot]Sysix
authored andcommitted
[autofix.ci] apply automated fixes
1 parent 07e4a02 commit 7708d56

File tree

8 files changed

+87
-39
lines changed

8 files changed

+87
-39
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/oxc_language_server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ oxc_diagnostics = { workspace = true }
2727
oxc_linter = { workspace = true, features = ["language_server"] }
2828

2929
#
30+
cow-utils = { workspace = true }
3031
env_logger = { workspace = true, features = ["humantime"] }
3132
futures = { workspace = true }
3233
globset = { workspace = true }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
debugger;

crates/oxc_language_server/src/linter/server_linter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ mod test {
316316
Tester::new("fixtures/linter/vue", None).test_and_snapshot_single_file("debugger.vue");
317317
Tester::new("fixtures/linter/svelte", None)
318318
.test_and_snapshot_single_file("debugger.svelte");
319-
// ToDo: fix Tester to work only with Uris and do not access the file system
320-
// Tester::new("fixtures/linter/nextjs").test_and_snapshot_single_file("%5B%5B..rest%5D%5D/debugger.ts");
319+
Tester::new("fixtures/linter/nextjs", None)
320+
.test_and_snapshot_single_file("[[...rest]]/debugger.ts");
321321
}
322322

323323
#[test]

crates/oxc_language_server/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod linter;
3030
mod options;
3131
#[cfg(test)]
3232
mod tester;
33+
#[cfg(test)]
3334
mod uri_ext;
3435
mod worker;
3536

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: crates/oxc_language_server/src/tester.rs
3+
input_file: "crates/oxc_language_server/fixtures/linter/nextjs/[[...rest]]/debugger.ts"
4+
---
5+
code: "eslint(no-debugger)"
6+
code_description.href: "https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-debugger.html"
7+
message: "`debugger` statement is not allowed\nhelp: Remove the debugger statement"
8+
range: Range { start: Position { line: 0, character: 0 }, end: Position { line: 0, character: 9 } }
9+
related_information[0].message: ""
10+
related_information[0].location.uri: "file://<variable>/fixtures/linter/nextjs/%5B%5B...rest%5D%5D/debugger.ts"
11+
related_information[0].location.range: Range { start: Position { line: 0, character: 0 }, end: Position { line: 0, character: 9 } }
12+
severity: Some(Warning)
13+
source: Some("oxc")
14+
tags: None
15+
fixed: Single(FixedContent { message: Some("Remove the debugger statement"), code: "", range: Range { start: Position { line: 0, character: 0 }, end: Position { line: 0, character: 9 } } })

crates/oxc_language_server/src/tester.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use tower_lsp_server::{
55
lsp_types::{CodeDescription, NumberOrString, Uri},
66
};
77

8-
use crate::{Options, worker::WorkspaceWorker};
8+
use crate::{Options, uri_ext::path_to_uri, worker::WorkspaceWorker};
99

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

1212
/// Given a file path relative to the crate root directory, return the URI of the file.
1313
pub fn get_file_uri(relative_file_path: &str) -> Uri {
1414
let absolute_file_path =
1515
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")
16+
path_to_uri(&absolute_file_path)
1717
}
1818

1919
fn get_snapshot_from_report(report: &DiagnosticReport) -> String {
Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,96 @@
1-
use std::{path::{Path, PathBuf}, str::FromStr};
1+
use std::{path::Path, str::FromStr};
22

3+
use cow_utils::CowUtils;
34
use percent_encoding::AsciiSet;
45
use tower_lsp_server::lsp_types::Uri;
56

6-
fn path_to_uri(path: &PathBuf) -> Uri {
7-
let path_str = normalize_path_with_utf8_percent_encode(path);
8-
Uri::from_str(&format!("file://{}", path_str.to_string_lossy())).expect("Failed to create URI from path")
7+
pub fn path_to_uri(path: &Path) -> Uri {
8+
let path = if cfg!(target_os = "windows") {
9+
// On Windows, we need to replace backslashes with forward slashes.
10+
// Tripleslash is a shorthand for `file://localhost/C:/Windows` with the `localhost` omitted.
11+
// We encode the driver Letter `C:` as well. LSP Specification allows it.
12+
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#uri
13+
format!(
14+
"file:///{}",
15+
percent_encoding::utf8_percent_encode(
16+
&path.to_string_lossy().cow_replace('\\', "/"),
17+
&ASCII_SET
18+
)
19+
)
20+
} else {
21+
// For Unix-like systems, just convert to a file URI directly
22+
format!(
23+
"file://{}",
24+
percent_encoding::utf8_percent_encode(&path.to_string_lossy(), &ASCII_SET)
25+
)
26+
};
27+
Uri::from_str(&path).expect("Failed to create URI from path")
928
}
1029

11-
const ASCII_SET: AsciiSet = percent_encoding::NON_ALPHANUMERIC.remove(b'.');
30+
const ASCII_SET: AsciiSet =
31+
// RFC3986 allows only alphanumeric characters, `-`, `.`, `_`, and `~` in the path.
32+
percent_encoding::NON_ALPHANUMERIC
33+
.remove(b'-')
34+
.remove(b'.')
35+
.remove(b'_')
36+
.remove(b'~')
37+
// we do not want path separators to be percent-encoded
38+
.remove(b'/');
1239

13-
/// Normalize a path by removing `.` and resolving `..` components,
14-
/// without touching the filesystem.
15-
pub fn normalize_path_with_utf8_percent_encode<P: AsRef<Path>>(path: P) -> PathBuf {
16-
let mut result = PathBuf::new();
17-
let components = path.as_ref().components();
18-
19-
for component in components {
20-
match component {
21-
std::path::Component::Prefix(_) => {
22-
// Keep the prefix (e.g., drive letter on Windows)
23-
result.push(component.as_os_str());
24-
}
25-
std::path::Component::RootDir => {
26-
// Keep the root directory
27-
result.push(component.as_os_str());
28-
}
29-
std::path::Component::Normal(part) => {
30-
// Normal components are added to the path
31-
result.push(percent_encoding::utf8_percent_encode(&part.to_str().unwrap(), &ASCII_SET).to_string());
32-
}
33-
_ => {}
34-
}
35-
}
36-
37-
result
38-
}
3940
#[cfg(test)]
4041
mod test {
4142
use std::path::PathBuf;
4243

4344
use crate::uri_ext::path_to_uri;
4445

46+
const EXPECTED_SCHEMA: &str = if cfg!(target_os = "windows") { "file:///" } else { "file://" };
47+
48+
fn with_schema(path: &str) -> String {
49+
format!("{EXPECTED_SCHEMA}{path}")
50+
}
4551

4652
#[test]
4753
fn test_path_to_uri() {
4854
let path = PathBuf::from("/some/path/to/file.txt");
4955
let uri = path_to_uri(&path);
50-
assert_eq!(uri.to_string(), "file:///some/path/to/file.txt");
56+
assert_eq!(uri.to_string(), with_schema("/some/path/to/file.txt"));
5157
}
5258

5359
#[test]
5460
fn test_path_to_uri_with_spaces() {
5561
let path = PathBuf::from("/some/path/to/file with spaces.txt");
5662
let uri = path_to_uri(&path);
57-
assert_eq!(uri.to_string(), "file:///some/path/to/file%20with%20spaces.txt");
63+
assert_eq!(uri.to_string(), with_schema("/some/path/to/file%20with%20spaces.txt"));
5864
}
5965

6066
#[test]
61-
6267
fn test_path_to_uri_with_special_characters() {
6368
let path = PathBuf::from("/some/path/[[...rest]]/file.txt");
6469
let uri = path_to_uri(&path);
65-
assert_eq!(uri.to_string(), "file:///some/path/%5B%5B...rest%5D%5D/file.txt");
70+
assert_eq!(uri.to_string(), with_schema("/some/path/%5B%5B...rest%5D%5D/file.txt"));
71+
}
72+
73+
#[test]
74+
fn test_path_to_uri_non_ascii() {
75+
let path = PathBuf::from("/some/path/to/файл.txt");
76+
let uri = path_to_uri(&path);
77+
assert_eq!(uri.to_string(), with_schema("/some/path/to/%D1%84%D0%B0%D0%B9%D0%BB.txt"));
78+
}
79+
80+
#[test]
81+
fn test_path_to_uri_with_unicode() {
82+
let path = PathBuf::from("/some/path/to/文件.txt");
83+
let uri = path_to_uri(&path);
84+
assert_eq!(uri.to_string(), with_schema("/some/path/to/%E6%96%87%E4%BB%B6.txt"));
85+
}
86+
87+
#[cfg(all(test, target_os = "windows"))]
88+
#[test]
89+
fn test_path_to_uri_windows() {
90+
let path = PathBuf::from("C:\\some\\path\\to\\file.txt");
91+
let uri = path_to_uri(&path);
92+
// yes we encode `:` too, LSP allows it
93+
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#uri
94+
assert_eq!(uri.to_string(), with_schema("C%3A/some/path/to/file.txt"));
6695
}
6796
}

0 commit comments

Comments
 (0)