Skip to content

Commit abdbaa9

Browse files
refactor(language_server): Use rule name directly from OxcCode instead of parsing out of the stringified version of OxcCode (#11714)
1 parent cfdc518 commit abdbaa9

File tree

3 files changed

+6
-16
lines changed

3 files changed

+6
-16
lines changed

crates/oxc_language_server/src/code_actions.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
use tower_lsp_server::lsp_types::{
2-
CodeAction, CodeActionKind, Diagnostic, NumberOrString, Position, Range, TextEdit, Uri,
3-
WorkspaceEdit,
2+
CodeAction, CodeActionKind, Position, Range, TextEdit, Uri, WorkspaceEdit,
43
};
54

65
use crate::linter::error_with_position::{DiagnosticReport, FixedContent, PossibleFixContent};
76

87
pub const CODE_ACTION_KIND_SOURCE_FIX_ALL_OXC: CodeActionKind =
98
CodeActionKind::new("source.fixAll.oxc");
109

11-
// TODO: Would be better if we had exact rule name from the diagnostic instead of having to parse it.
12-
fn get_rule_name(diagnostic: &Diagnostic) -> Option<String> {
13-
if let Some(NumberOrString::String(code)) = &diagnostic.code {
14-
let open_paren = code.chars().position(|c| c == '(')?;
15-
let close_paren = code.chars().position(|c| c == ')')?;
16-
17-
return Some(code[(open_paren + 1)..close_paren].to_string());
18-
}
19-
20-
None
21-
}
22-
2310
fn fix_content_to_code_action(
2411
fixed_content: &FixedContent,
2512
uri: &Uri,
@@ -123,7 +110,7 @@ pub fn apply_all_fix_code_action<'a>(
123110
}
124111

125112
pub fn ignore_this_line_code_action(report: &DiagnosticReport, uri: &Uri) -> CodeAction {
126-
let rule_name = get_rule_name(&report.diagnostic);
113+
let rule_name = report.rule_name.as_ref();
127114

128115
// TODO: This CodeAction doesn't support disabling multiple rules by name for a given line.
129116
// To do that, we need to read `report.diagnostic.range.start.line` and check if a disable comment already exists.
@@ -168,7 +155,7 @@ pub fn ignore_this_line_code_action(report: &DiagnosticReport, uri: &Uri) -> Cod
168155
}
169156

170157
pub fn ignore_this_rule_code_action(report: &DiagnosticReport, uri: &Uri) -> CodeAction {
171-
let rule_name = get_rule_name(&report.diagnostic);
158+
let rule_name = report.rule_name.as_ref();
172159

173160
CodeAction {
174161
title: rule_name.as_ref().map_or_else(

crates/oxc_language_server/src/linter/error_with_position.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const LSP_MAX_INT: u32 = 2u32.pow(31) - 1;
1515
pub struct DiagnosticReport {
1616
pub diagnostic: lsp_types::Diagnostic,
1717
pub fixed_content: PossibleFixContent,
18+
pub rule_name: Option<String>,
1819
}
1920

2021
#[derive(Debug, Clone)]
@@ -145,5 +146,6 @@ pub fn message_with_position_to_lsp_diagnostic_report(
145146
fixes.iter().map(fix_with_position_to_fix_content).collect(),
146147
),
147148
},
149+
rule_name: message.code.number.as_ref().map(std::string::ToString::to_string),
148150
}
149151
}

crates/oxc_language_server/src/linter/isolated_lint_handler.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl IsolatedLintHandler {
110110
data: None,
111111
},
112112
fixed_content: PossibleFixContent::None,
113+
rule_name: None,
113114
});
114115
}
115116
}

0 commit comments

Comments
 (0)