Skip to content

Commit 6bbb92a

Browse files
rtsaoclaudecamc314
authored
fix(linter): remove unused oxlint-disable directives with --fix (#27698)
Fixes #22420 - Apply unused directive fixes after linting, skipping files that other fixes already changed - Make `oxlint-disable` directive removals a safe fix since only oxlint reads them (and JS plugins now run in both the CLI and LSP). Removal of `eslint-disable` comments remains a suggestion per #17522 AI disclosure: code authored with AI tools. I reviewed the changes and opened the PR myself --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Cameron <cameron.clark@hey.com>
1 parent 3b35cb6 commit 6bbb92a

10 files changed

Lines changed: 252 additions & 132 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// oxlint-disable-next-line no-debugger
2+
const a = 1;
3+
// eslint-disable-next-line no-debugger
4+
const b = 2;
5+
// oxlint-disable-next-line no-debugger, no-console
6+
debugger;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
// oxlint-disable-next-line no-debugger
3+
const a = 1;
4+
// eslint-disable-next-line no-debugger
5+
const b = 2;
6+
</script>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// oxlint-disable-next-line no-debugger
2+
var x = new String('Hello world');

‎apps/oxlint/src/lint.rs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,52 @@ mod test {
13191319
);
13201320
}
13211321

1322+
#[test]
1323+
fn test_fix_unused_disable_directives() {
1324+
let tester = Tester::new().with_cwd("fixtures/cli/fix_argument".into());
1325+
let fix = &["--report-unused-disable-directives"];
1326+
let fix_suggestions = &["--report-unused-disable-directives", "--fix-suggestions"];
1327+
1328+
// `eslint-disable` directives may still be needed by ESLint, so only `oxlint-disable`
1329+
// directives are removed without `--fix-suggestions`.
1330+
let js = "// oxlint-disable-next-line no-debugger\nconst a = 1;\n// eslint-disable-next-line no-debugger\nconst b = 2;\n// oxlint-disable-next-line no-debugger, no-console\ndebugger;\n";
1331+
tester.test_fix_with_args(
1332+
"unused_disable_directives.js",
1333+
js,
1334+
"const a = 1;\n// eslint-disable-next-line no-debugger\nconst b = 2;\n// oxlint-disable-next-line no-debugger\ndebugger;\n",
1335+
fix,
1336+
);
1337+
tester.test_fix_with_args(
1338+
"unused_disable_directives.js",
1339+
js,
1340+
"const a = 1;\nconst b = 2;\n// oxlint-disable-next-line no-debugger\ndebugger;\n",
1341+
fix_suggestions,
1342+
);
1343+
1344+
let vue = "<script>\n// oxlint-disable-next-line no-debugger\nconst a = 1;\n// eslint-disable-next-line no-debugger\nconst b = 2;\n</script>\n";
1345+
tester.test_fix_with_args(
1346+
"unused_disable_directives.vue",
1347+
vue,
1348+
"<script>\nconst a = 1;\n// eslint-disable-next-line no-debugger\nconst b = 2;\n</script>\n",
1349+
fix,
1350+
);
1351+
tester.test_fix_with_args(
1352+
"unused_disable_directives.vue",
1353+
vue,
1354+
"<script>\nconst a = 1;\nconst b = 2;\n</script>\n",
1355+
fix_suggestions,
1356+
);
1357+
1358+
// Directive spans are stale once other fixes rewrite the file, so the directive is
1359+
// only removed by the next run.
1360+
tester.test_fix_with_args(
1361+
"unused_disable_directives_with_fix.js",
1362+
"// oxlint-disable-next-line no-debugger\nvar x = new String('Hello world');\n",
1363+
"// oxlint-disable-next-line no-debugger\nvar x = 'Hello world';\n",
1364+
fix_suggestions,
1365+
);
1366+
}
1367+
13221368
#[test]
13231369
fn test_print_config_ban_all_rules() {
13241370
let args = &["-A", "all", "--print-config"];

‎apps/oxlint/src/lsp/snapshots/fixtures_lsp_unused_disabled_directives@test.js.snap‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,45 @@ TextEdit: TextEdit {
220220

221221

222222
########### Fix All Action
223-
None
223+
CodeAction:
224+
Title: fix all safe fixable oxlint issues
225+
Is Preferred: Some(true)
226+
TextEdit: TextEdit {
227+
range: Range {
228+
start: Position {
229+
line: 0,
230+
character: 0,
231+
},
232+
end: Position {
233+
line: 1,
234+
character: 0,
235+
},
236+
},
237+
new_text: "",
238+
}
239+
TextEdit: TextEdit {
240+
range: Range {
241+
start: Position {
242+
line: 5,
243+
character: 39,
244+
},
245+
end: Position {
246+
line: 5,
247+
character: 52,
248+
},
249+
},
250+
new_text: "",
251+
}
252+
TextEdit: TextEdit {
253+
range: Range {
254+
start: Position {
255+
line: 8,
256+
character: 0,
257+
},
258+
end: Position {
259+
line: 9,
260+
character: 0,
261+
},
262+
},
263+
new_text: "",
264+
}

‎crates/oxc_linter/src/context/host.rs‎

Lines changed: 11 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::{
2-
borrow::Cow,
32
cell::{Cell, OnceCell, RefCell},
43
ffi::OsStr,
54
path::Path,
@@ -8,19 +7,20 @@ use std::{
87
};
98

109
use oxc_allocator::{Allocator, ArenaBox};
11-
use oxc_diagnostics::{OxcDiagnostic, Severity};
1210
use oxc_parser::Token;
1311
use oxc_semantic::Semantic;
14-
use oxc_span::{SourceType, Span};
12+
use oxc_span::SourceType;
1513

1614
use crate::{
1715
AllowWarnDeny, FrameworkFlags,
1816
config::{
1917
LintConfig, LintPlugins, OxlintEnv, OxlintGlobals, OxlintSettings,
2018
plugins::plugin_display_name,
2119
},
22-
disable_directives::{DisableDirectives, DisableDirectivesBuilder, RuleCommentType},
23-
fixer::{FixKind, Message, PossibleFixes},
20+
disable_directives::{
21+
DisableDirectives, DisableDirectivesBuilder, create_unused_directives_messages,
22+
},
23+
fixer::{FixKind, Message},
2424
frameworks::FrameworkOptions,
2525
module_record::ModuleRecord,
2626
options::LintOptions,
@@ -393,78 +393,12 @@ impl<'a> ContextHost<'a> {
393393
}
394394

395395
/// report unused enable/disable directives, add these as Messages to diagnostics
396-
pub fn report_unused_directives(&self, rule_severity: Severity) {
397-
// report unused disable
398-
// relate to lint result, check after linter run finish
399-
let unused_disable_comments = self.disable_directives().collect_unused_disable_comments();
400-
let fix_message = "remove unused disable directive";
401-
let source_text = self.source_text();
402-
403-
for unused_disable_comment in unused_disable_comments {
404-
let span = unused_disable_comment.span;
405-
match &unused_disable_comment.r#type {
406-
RuleCommentType::All => {
407-
self.push_diagnostic(Message::new(
408-
OxcDiagnostic::error(
409-
unused_disable_comment.directive_prefix.unused_disable_message(),
410-
)
411-
.with_label(span)
412-
.with_severity(rule_severity),
413-
PossibleFixes::Single(
414-
unused_disable_comment
415-
.create_fix(source_text)
416-
.with_kind(FixKind::Suggestion)
417-
.with_message(fix_message),
418-
),
419-
));
420-
}
421-
RuleCommentType::Single(rules_vec) => {
422-
for rule in rules_vec {
423-
let rule_message = Cow::<str>::Owned(
424-
rule.directive_prefix.unused_disable_rule_message(&rule.rule_name),
425-
);
426-
427-
let fix = rule.create_fix(source_text, span).with_message(fix_message);
428-
429-
self.push_diagnostic(Message::new(
430-
OxcDiagnostic::error(rule_message)
431-
.with_label(rule.name_span)
432-
.with_severity(rule_severity),
433-
PossibleFixes::Single(fix),
434-
));
435-
}
436-
}
437-
}
438-
}
439-
440-
let unused_enable_comments = self.disable_directives().unused_enable_comments();
441-
let mut unused_directive_diagnostics: Vec<(Cow<str>, Span)> =
442-
Vec::with_capacity(unused_enable_comments.len());
443-
// report unused enable
444-
// not relate to lint result, check during comment directives' construction
445-
for (directive_prefix, rule_name, enable_comment_span) in unused_enable_comments {
446-
unused_directive_diagnostics.push((
447-
rule_name.as_ref().map_or_else(
448-
|| Cow::Owned(directive_prefix.unused_enable_message()),
449-
|name| Cow::Owned(directive_prefix.unused_enable_rule_message(name)),
450-
),
451-
*enable_comment_span,
452-
));
453-
}
454-
455-
self.append_diagnostics(
456-
unused_directive_diagnostics
457-
.into_iter()
458-
.map(|(message, span)| {
459-
Message::new(
460-
OxcDiagnostic::error(message).with_label(span).with_severity(rule_severity),
461-
// TODO: fixer
462-
// copy the structure of disable directives
463-
PossibleFixes::None,
464-
)
465-
})
466-
.collect(),
467-
);
396+
pub fn report_unused_directives(&self, severity: AllowWarnDeny) {
397+
self.append_diagnostics(create_unused_directives_messages(
398+
self.disable_directives(),
399+
severity,
400+
Some((self.fix, self.source_text())),
401+
));
468402
}
469403

470404
/// Take ownership of all diagnostics collected during linting.

0 commit comments

Comments
 (0)