Skip to content

Commit ed759d1

Browse files
committed
fix(linter/plugins): fix error messages for invalid suggestions (#19059)
Fix the error message when a rule provides invalid suggestions. ```diff - Plugin `whatever` returned invalid fixes. + Plugin `whatever` returned invalid suggestions. ```
1 parent 34851a7 commit ed759d1

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

apps/oxlint/test/fixtures/suggestions/fix-suggestions.snap.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77
| File path: <fixture>/files/range_end_negative.js
88
| Failed to deserialize JSON returned by `lintFile`: invalid value: integer `-10`, expected u32 at line 1 column 158
99
10-
x Plugin `suggestions-plugin/suggestions` returned invalid fixes.
10+
x Plugin `suggestions-plugin/suggestions` returned invalid suggestions.
1111
| File path: <fixture>/files/range_end_out_of_bounds.js
1212
| Invalid range: 7..7
1313
14-
x Plugin `suggestions-plugin/suggestions` returned invalid fixes.
14+
x Plugin `suggestions-plugin/suggestions` returned invalid suggestions.
1515
| File path: <fixture>/files/range_end_out_of_bounds.js
1616
| Invalid range: 7..7
1717
1818
x Error running JS plugin.
1919
| File path: <fixture>/files/range_end_too_large.js
2020
| Failed to deserialize JSON returned by `lintFile`: invalid value: integer `4294967296`, expected u32 at line 1 column 166
2121
22-
x Plugin `suggestions-plugin/suggestions` returned invalid fixes.
22+
x Plugin `suggestions-plugin/suggestions` returned invalid suggestions.
2323
| File path: <fixture>/files/range_start_after_end.js
2424
| Negative range is invalid: Span { start: 3, end: 2 }
2525
26-
x Plugin `suggestions-plugin/suggestions` returned invalid fixes.
26+
x Plugin `suggestions-plugin/suggestions` returned invalid suggestions.
2727
| File path: <fixture>/files/range_start_after_end.js
2828
| Negative range is invalid: Span { start: 3, end: 2 }
2929

crates/oxc_linter/src/external_linter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use oxc_span::Span;
99
use crate::{
1010
config::{OxlintEnv, OxlintGlobals},
1111
context::ContextHost,
12-
fixer::{CompositeFix, Fix, FixKind, MergeFixesError},
12+
fixer::{CompositeFix, Fix, MergeFixesError},
1313
};
1414

1515
pub type ExternalLinterCreateWorkspaceCb =
@@ -111,7 +111,7 @@ pub fn convert_and_merge_js_fixes(
111111
let mut fixes = fixes.into_iter().map(|fix| {
112112
let mut span = Span::new(fix.range[0], fix.range[1]);
113113
span_converter.convert_span_back(&mut span);
114-
Fix::new(fix.text, span).with_kind(FixKind::Fix)
114+
Fix::new(fix.text, span)
115115
});
116116

117117
if is_single {

crates/oxc_linter/src/lib.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -642,15 +642,20 @@ impl Linter {
642642
}
643643

644644
// Convert a `Vec<JsFix>` to a `Fix`, including converting spans back to UTF-8
645-
let create_fix = |fixes| match convert_and_merge_js_fixes(
645+
let create_fix = |fixes, fix_kind| match convert_and_merge_js_fixes(
646646
fixes,
647647
original_source_text,
648648
&span_converter,
649649
) {
650-
Ok(fix) => Some(fix),
650+
Ok(fix) => Some(fix.with_kind(fix_kind)),
651651
Err(err) => {
652+
let fixes_type = if fix_kind.contains(FixKind::Suggestion) {
653+
"suggestions"
654+
} else {
655+
"fixes"
656+
};
652657
let message = format!(
653-
"Plugin `{plugin_name}/{rule_name}` returned invalid fixes.\nFile path: {path}\n{err}"
658+
"Plugin `{plugin_name}/{rule_name}` returned invalid {fixes_type}.\nFile path: {path}\n{err}"
654659
);
655660
ctx_host.push_diagnostic(Message::new(
656661
OxcDiagnostic::error(message),
@@ -661,7 +666,7 @@ impl Linter {
661666
};
662667

663668
// Convert fix
664-
let fix = diagnostic.fixes.and_then(create_fix);
669+
let fix = diagnostic.fixes.and_then(|fixes| create_fix(fixes, FixKind::Fix));
665670

666671
// Convert suggestions (only if fix kind allows suggestions), and combine with fix
667672
let possible_fixes = if let Some(suggestions) = diagnostic.suggestions
@@ -674,11 +679,8 @@ impl Linter {
674679
}
675680

676681
for suggestion in suggestions {
677-
if let Some(fix) = create_fix(suggestion.fixes) {
678-
fixes.push(
679-
fix.with_message(suggestion.message)
680-
.with_kind(FixKind::Suggestion),
681-
);
682+
if let Some(fix) = create_fix(suggestion.fixes, FixKind::Suggestion) {
683+
fixes.push(fix.with_message(suggestion.message));
682684
}
683685
}
684686

0 commit comments

Comments
 (0)