diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index 0f2f06f0cd053..20dfa88e729a2 100644 --- a/crates/oxc_linter/src/lib.rs +++ b/crates/oxc_linter/src/lib.rs @@ -330,6 +330,13 @@ impl Linter { let (plugin_name, rule_name) = self.config.resolve_plugin_rule_names(external_rule_id); + if ctx_host + .disable_directives() + .contains(&format!("{plugin_name}/{rule_name}"), span) + { + continue; + } + ctx_host.push_diagnostic(Message::new( OxcDiagnostic::error(diagnostic.message) .with_label(span) diff --git a/napi/oxlint2/test/__snapshots__/e2e.test.ts.snap b/napi/oxlint2/test/__snapshots__/e2e.test.ts.snap index a7febe0714e87..81dd3724372f0 100644 --- a/napi/oxlint2/test/__snapshots__/e2e.test.ts.snap +++ b/napi/oxlint2/test/__snapshots__/e2e.test.ts.snap @@ -460,6 +460,51 @@ Found 2 warnings and 0 errors. Finished in Xms on 1 file using X threads." `; +exports[`oxlint2 CLI > should respect disable directives for custom plugin rules 1`] = ` +" + x test-plugin(no-var): Use let or const instead of var + ,-[index.js:1:1] + 1 | var shouldError = 1; + : ^^^^^^^^^^^^^^^^^^^^ + 2 | + \`---- + + x ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-debugger.html\\eslint(no-debugger)]8;;\\: \`debugger\` statement is not allowed + ,-[index.js:10:1] + 9 | // should trigger an error + 10 | debugger; + : ^^^^^^^^^ + 11 | + \`---- + help: Remove the debugger statement + + x test-plugin(no-var): Use let or const instead of var + ,-[index.js:16:1] + 15 | /* oxlint-disable-next-line test-plugin */ // \`test-plugin\` should be \`test-plugin/no-var\` + 16 | var incorrectlyDisabled = 4; + : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 17 | + \`---- + + x test-plugin(no-var): Use let or const instead of var + ,-[index.js:19:1] + 18 | /* oxlint-disable-next-line no-var */ // \`no-var\` should be \`test-plugin/no-var\` + 19 | var anotherIncorrectlyDisabled = 4; + : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 20 | + \`---- + + x test-plugin(no-var): Use let or const instead of var + ,-[index.js:22:1] + 21 | // This var should trigger an error again + 22 | var shouldErrorAgain = 3; + : ^^^^^^^^^^^^^^^^^^^^^^^^^ + \`---- + +Found 0 warnings and 5 errors. +Finished in Xms on 1 file using X threads." +`; + exports[`oxlint2 CLI > should work with multiple rules 1`] = ` " x basic-custom-plugin(no-debugger): Unexpected Debugger Statement diff --git a/napi/oxlint2/test/e2e.test.ts b/napi/oxlint2/test/e2e.test.ts index b8f40f02ce899..413bc34e9d2d3 100644 --- a/napi/oxlint2/test/e2e.test.ts +++ b/napi/oxlint2/test/e2e.test.ts @@ -92,4 +92,10 @@ describe('oxlint2 CLI', () => { expect(exitCode).toBe(1); expect(normalizeOutput(stdout)).toMatchSnapshot(); }); + + it('should respect disable directives for custom plugin rules', async () => { + const { stdout, exitCode } = await runOxlint('test/fixtures/custom_plugin_disable_directives'); + expect(exitCode).toBe(1); + expect(normalizeOutput(stdout)).toMatchSnapshot(); + }); }); diff --git a/napi/oxlint2/test/fixtures/custom_plugin_disable_directives/.oxlintrc.json b/napi/oxlint2/test/fixtures/custom_plugin_disable_directives/.oxlintrc.json new file mode 100644 index 0000000000000..68c80117577c4 --- /dev/null +++ b/napi/oxlint2/test/fixtures/custom_plugin_disable_directives/.oxlintrc.json @@ -0,0 +1,9 @@ +{ + "plugins": ["./test_plugin"], + "categories": { "correctness": "off" }, + "rules": { + "test-plugin/no-var": "error", + "no-debugger": "error" + }, + "ignorePatterns": ["test_plugin/**"] +} diff --git a/napi/oxlint2/test/fixtures/custom_plugin_disable_directives/index.js b/napi/oxlint2/test/fixtures/custom_plugin_disable_directives/index.js new file mode 100644 index 0000000000000..e52042cc5700e --- /dev/null +++ b/napi/oxlint2/test/fixtures/custom_plugin_disable_directives/index.js @@ -0,0 +1,22 @@ +var shouldError = 1; + +// oxlint-disable-next-line test-plugin/no-var +var shouldBeDisabled = 2; + +// eslint-disable-next-line no-debugger +debugger; + +// should trigger an error +debugger; + +/* oxlint-disable-next-line test-plugin/no-var */ +var anotherDisabled = 4; + +/* oxlint-disable-next-line test-plugin */ // `test-plugin` should be `test-plugin/no-var` +var incorrectlyDisabled = 4; + +/* oxlint-disable-next-line no-var */ // `no-var` should be `test-plugin/no-var` +var anotherIncorrectlyDisabled = 4; + +// This var should trigger an error again +var shouldErrorAgain = 3; diff --git a/napi/oxlint2/test/fixtures/custom_plugin_disable_directives/test_plugin/index.js b/napi/oxlint2/test/fixtures/custom_plugin_disable_directives/test_plugin/index.js new file mode 100644 index 0000000000000..9dc55824476fd --- /dev/null +++ b/napi/oxlint2/test/fixtures/custom_plugin_disable_directives/test_plugin/index.js @@ -0,0 +1,21 @@ +export default { + meta: { + name: "test-plugin", + }, + rules: { + "no-var": { + create(context) { + return { + VariableDeclaration(node) { + if (node.kind === "var") { + context.report({ + message: "Use let or const instead of var", + node: node, + }); + } + }, + }; + }, + }, + }, +}; \ No newline at end of file