Skip to content

Commit 12baf5e

Browse files
committed
fix(linter/exhaustive-deps): respect primary span when identifying disable directive location (#13781)
disable directive location fixes #13311
1 parent 2b81f9b commit 12baf5e

File tree

5 files changed

+53
-14
lines changed

5 files changed

+53
-14
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"plugins": ["react"],
3+
"categories": {
4+
"correctness": "off"
5+
},
6+
"rules": {
7+
"react/exhaustive-deps": "warn"
8+
}
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React, { useEffect } from 'react';
2+
3+
function Component() {
4+
const emit = (event) => console.log(event);
5+
const EVENTS = { CLEAR: 'clear' };
6+
7+
useEffect(() => {
8+
emit(EVENTS.CLEAR);
9+
// oxlint-disable-next-line exhaustive-deps
10+
}, []);
11+
12+
return null;
13+
}

apps/oxlint/src/lint.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,17 @@ mod test {
12511251
Tester::new().with_cwd("fixtures/dot_folder".into()).test_and_snapshot(&[]);
12521252
}
12531253

1254+
#[test]
1255+
fn test_exhaustive_deps_disable_directive_issue_13311() {
1256+
// Test that exhaustive-deps diagnostics are reported at the dependency array
1257+
// so that disable directives work correctly
1258+
// Issue: https://github.com/oxc-project/oxc/issues/13311
1259+
let args = &["test.jsx"];
1260+
Tester::new()
1261+
.with_cwd("fixtures/exhaustive_deps_disable_directive_issue_13311".into())
1262+
.test_and_snapshot(args);
1263+
}
1264+
12541265
// ToDo: `tsgolint` does not support `big-endian`?
12551266
#[test]
12561267
#[cfg(not(target_endian = "big"))]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: apps/oxlint/src/tester.rs
3+
---
4+
##########
5+
arguments: test.jsx
6+
working directory: fixtures/exhaustive_deps_disable_directive_issue_13311
7+
----------
8+
Found 0 warnings and 0 errors.
9+
Finished in <variable>ms on 1 file using 1 threads.
10+
----------
11+
CLI result: LintSucceeded
12+
----------

crates/oxc_linter/src/fixer/mod.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -242,20 +242,14 @@ impl<'new> CloneIn<'new> for Message<'_> {
242242
impl<'a> Message<'a> {
243243
#[expect(clippy::cast_possible_truncation)] // for `as u32`
244244
pub fn new(error: OxcDiagnostic, fixes: PossibleFixes<'a>) -> Self {
245-
let (start, end) = if let Some(labels) = &error.labels {
246-
let start = labels
247-
.iter()
248-
.min_by_key(|span| span.offset())
249-
.map_or(0, |span| span.offset() as u32);
250-
let end = labels
251-
.iter()
252-
.max_by_key(|span| span.offset() + span.len())
253-
.map_or(0, |span| (span.offset() + span.len()) as u32);
254-
(start, end)
255-
} else {
256-
(0, 0)
257-
};
258-
Self { error, span: Span::new(start, end), fixes, fixed: false }
245+
let span = error
246+
.labels
247+
.as_ref()
248+
.and_then(|labels| labels.iter().find(|span| span.primary()).or_else(|| labels.first()))
249+
.map(|span| Span::new(span.offset() as u32, (span.offset() + span.len()) as u32))
250+
.unwrap_or_default();
251+
252+
Self { error, span, fixes, fixed: false }
259253
}
260254

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

0 commit comments

Comments
 (0)