Skip to content

Commit 68fc54d

Browse files
committed
fix(linter): report errors with the correct severity for custom plugins
1 parent 54d143a commit 68fc54d

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed

crates/oxc_linter/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ impl Linter {
225225
.with_error_code(
226226
plugin_name.to_string(),
227227
rule_name.to_string(),
228+
)
229+
.with_severity(
230+
// panicking is impossible here, since we know that the rule exists as we found it in `self.config.resolve_plugin_rule_names`
231+
#[expect(clippy::missing_panics_doc)]
232+
(*external_rules
233+
.iter()
234+
.find(|(rule_id, _)| {
235+
rule_id.raw() == diagnostic.external_rule_id
236+
})
237+
.map(|(_, severity)| severity)
238+
.expect("external rule must exist when resolving severity"))
239+
.into(),
228240
),
229241
PossibleFixes::None,
230242
));

napi/oxlint2/test/__snapshots__/e2e.test.ts.snap

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,22 @@ exports[`cli options for bundling > should report an error if a rule is not foun
336336
x Rule 'unknown-rule' not found in plugin 'basic-custom-plugin'
337337
"
338338
`;
339+
340+
exports[`cli options for bundling > should report the correct severity when using a custom plugin 1`] = `
341+
"
342+
! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-debugger.html\\eslint(no-debugger)]8;;\\: \`debugger\` statement is not allowed
343+
,-[index.js:1:1]
344+
1 | debugger;
345+
: ^^^^^^^^^
346+
\`----
347+
help: Remove the debugger statement
348+
349+
! basic-custom-plugin(no-debugger): Unexpected Debugger Statement
350+
,-[index.js:1:1]
351+
1 | debugger;
352+
: ^
353+
\`----
354+
355+
Found 2 warnings and 0 errors.
356+
Finished in Xms on 1 file using X threads."
357+
`;

napi/oxlint2/test/e2e.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,13 @@ describe('cli options for bundling', () => {
8383
expect(exitCode).toBe(1);
8484
expect(normalizeOutput(stdout)).toMatchSnapshot();
8585
});
86+
87+
it('should report the correct severity when using a custom plugin', async () => {
88+
const { stdout, exitCode } = await runOxlint(
89+
'test/fixtures/basic_custom_plugin_warn_severity',
90+
);
91+
92+
expect(exitCode).toBe(0);
93+
expect(normalizeOutput(stdout)).toMatchSnapshot();
94+
});
8695
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"plugins": ["./test_plugin"],
3+
"rules": {
4+
"basic-custom-plugin/no-debugger": "warn"
5+
},
6+
"ignorePatterns": ["test_plugin"]
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
debugger;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default {
2+
meta: {
3+
name: "basic-custom-plugin",
4+
},
5+
rules: {
6+
"no-debugger": {
7+
create(context) {
8+
// TODO: move this call into `DebuggerStatement`, once we are walking the ast.
9+
context.report({
10+
message: "Unexpected Debugger Statement",
11+
node: { start: 0, end: 0 },
12+
});
13+
return {
14+
DebuggerStatement(_debuggerStatement) {},
15+
};
16+
},
17+
},
18+
},
19+
};

0 commit comments

Comments
 (0)