Skip to content

Commit d4e5d2a

Browse files
committed
fix(linter): report errors with the correct severity for custom plugins
1 parent 658abfe commit d4e5d2a

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

crates/oxc_linter/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,18 @@ impl Linter {
278278
// TODO: `error` isn't right, we need to get the severity from `external_rules`
279279
OxcDiagnostic::error(diagnostic.message)
280280
.with_label(Span::new(diagnostic.loc.start, diagnostic.loc.end))
281-
.with_error_code(
282-
plugin_name.to_string(),
283-
rule_name.to_string(),
281+
.with_error_code(plugin_name.to_string(), rule_name.to_string())
282+
.with_severity(
283+
(*external_rules
284+
.iter()
285+
.find(|(rule_id, _)| {
286+
rule_id.raw() == diagnostic.external_rule_id
287+
})
288+
.map(|(_, severity)| severity)
289+
.expect(
290+
"external rule must exist when resolving severity",
291+
))
292+
.into(),
284293
),
285294
PossibleFixes::None,
286295
));

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[`oxlint2 CLI > should report an error if a rule is not found within a cu
336336
x Rule 'unknown-rule' not found in plugin 'basic-custom-plugin'
337337
"
338338
`;
339+
340+
exports[`oxlint2 CLI > 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('oxlint2 CLI', () => {
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+
return {
9+
DebuggerStatement(debuggerStatement) {
10+
context.report({
11+
message: "Unexpected Debugger Statement",
12+
node: debuggerStatement,
13+
});
14+
},
15+
};
16+
},
17+
},
18+
},
19+
};

0 commit comments

Comments
 (0)