Skip to content

Commit a55dd55

Browse files
committed
fix(langage_server): correct position for "ignore this rule for this file"
1 parent 7079e73 commit a55dd55

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

crates/oxc_language_server/src/snapshots/fixtures_linter_astro@debugger.astro.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ fixed: Multiple(
120120
code: "// oxlint-disable no-debugger\n",
121121
range: Range {
122122
start: Position {
123-
line: 0,
123+
line: 9,
124124
character: 0,
125125
},
126126
end: Position {
127-
line: 0,
127+
line: 9,
128128
character: 0,
129129
},
130130
},
@@ -184,11 +184,11 @@ fixed: Multiple(
184184
code: "// oxlint-disable no-debugger\n",
185185
range: Range {
186186
start: Position {
187-
line: 0,
187+
line: 13,
188188
character: 0,
189189
},
190190
end: Position {
191-
line: 0,
191+
line: 13,
192192
character: 0,
193193
},
194194
},
@@ -248,11 +248,11 @@ fixed: Multiple(
248248
code: "// oxlint-disable no-debugger\n",
249249
range: Range {
250250
start: Position {
251-
line: 0,
251+
line: 17,
252252
character: 0,
253253
},
254254
end: Position {
255-
line: 0,
255+
line: 17,
256256
character: 0,
257257
},
258258
},

crates/oxc_language_server/src/snapshots/fixtures_linter_vue@debugger.vue.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ fixed: Multiple(
5656
code: "// oxlint-disable no-debugger\n",
5757
range: Range {
5858
start: Position {
59-
line: 0,
59+
line: 4,
6060
character: 0,
6161
},
6262
end: Position {
63-
line: 0,
63+
line: 4,
6464
character: 0,
6565
},
6666
},
@@ -120,11 +120,11 @@ fixed: Multiple(
120120
code: "// oxlint-disable no-debugger\n",
121121
range: Range {
122122
start: Position {
123-
line: 0,
123+
line: 8,
124124
character: 0,
125125
},
126126
end: Position {
127-
line: 0,
127+
line: 8,
128128
character: 0,
129129
},
130130
},

crates/oxc_linter/src/context/host.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'a> ContextHost<'a> {
174174
}
175175

176176
/// The current [`ContextSubHost`]
177-
fn current_sub_host(&self) -> &ContextSubHost<'a> {
177+
pub fn current_sub_host(&self) -> &ContextSubHost<'a> {
178178
&self.sub_hosts[self.current_sub_host_index.get()]
179179
}
180180

crates/oxc_linter/src/context/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,14 @@ impl<'a> LintContext<'a> {
247247
/// Use [`LintContext::diagnostic_with_fix`] to provide an automatic fix.
248248
#[inline]
249249
pub fn diagnostic(&self, diagnostic: OxcDiagnostic) {
250+
#[cfg(not(feature = "language_server"))]
250251
self.add_diagnostic(Message::new(diagnostic, PossibleFixes::None));
252+
253+
#[cfg(feature = "language_server")]
254+
self.add_diagnostic(
255+
Message::new(diagnostic, PossibleFixes::None)
256+
.with_section_offset(self.parent.current_sub_host().source_text_offset),
257+
);
251258
}
252259

253260
/// Report a lint rule violation and provide an automatic fix.
@@ -358,7 +365,14 @@ impl<'a> LintContext<'a> {
358365
{
359366
let (diagnostic, fix) = self.create_fix(fix_kind, fix, diagnostic);
360367
if let Some(fix) = fix {
368+
#[cfg(not(feature = "language_server"))]
361369
self.add_diagnostic(Message::new(diagnostic, PossibleFixes::Single(fix)));
370+
371+
#[cfg(feature = "language_server")]
372+
self.add_diagnostic(
373+
Message::new(diagnostic, PossibleFixes::Single(fix))
374+
.with_section_offset(self.parent.current_sub_host().source_text_offset),
375+
);
362376
} else {
363377
self.diagnostic(diagnostic);
364378
}
@@ -387,7 +401,14 @@ impl<'a> LintContext<'a> {
387401
if fixes_result.is_empty() {
388402
self.diagnostic(diagnostic);
389403
} else {
404+
#[cfg(not(feature = "language_server"))]
390405
self.add_diagnostic(Message::new(diagnostic, PossibleFixes::Multiple(fixes_result)));
406+
407+
#[cfg(feature = "language_server")]
408+
self.add_diagnostic(
409+
Message::new(diagnostic, PossibleFixes::Multiple(fixes_result))
410+
.with_section_offset(self.parent.current_sub_host().source_text_offset),
411+
);
391412
}
392413
}
393414

crates/oxc_linter/src/fixer/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ pub struct Message<'a> {
224224
pub fixes: PossibleFixes<'a>,
225225
span: Span,
226226
fixed: bool,
227+
#[cfg(feature = "language_server")]
228+
pub section_offset: u32,
227229
}
228230

229231
impl<'new> CloneIn<'new> for Message<'_> {
@@ -235,6 +237,8 @@ impl<'new> CloneIn<'new> for Message<'_> {
235237
fixes: self.fixes.clone_in(allocator),
236238
span: self.span,
237239
fixed: self.fixed,
240+
#[cfg(feature = "language_server")]
241+
section_offset: self.section_offset,
238242
}
239243
}
240244
}
@@ -249,7 +253,20 @@ impl<'a> Message<'a> {
249253
.map(|span| Span::new(span.offset() as u32, (span.offset() + span.len()) as u32))
250254
.unwrap_or_default();
251255

252-
Self { error, span, fixes, fixed: false }
256+
Self {
257+
error,
258+
span,
259+
fixes,
260+
fixed: false,
261+
#[cfg(feature = "language_server")]
262+
section_offset: 0,
263+
}
264+
}
265+
266+
#[cfg(feature = "language_server")]
267+
pub fn with_section_offset(mut self, section_offset: u32) -> Self {
268+
self.section_offset = section_offset;
269+
self
253270
}
254271

255272
/// move the offset of all spans to the right

crates/oxc_linter/src/lsp.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ pub fn message_to_message_with_position<'a>(
109109
) -> MessageWithPosition<'a> {
110110
let code = message.error.code.clone();
111111
let error_offset = message.span().start;
112-
// TODO: feature flag for knowing the source text section start offset of this message
113-
let section_offset = 0;
112+
let section_offset = message.section_offset;
114113

115114
let mut result = oxc_diagnostic_to_message_with_position(message.error, source_text, rope);
116115
let fixes = match &message.fixes {

0 commit comments

Comments
 (0)