Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions napi/oxlint2/test/__snapshots__/e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions napi/oxlint2/test/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,42 +54,48 @@
it('should report an error if a custom plugin cannot be loaded', async () => {
const { stdout, exitCode } = await runOxlint('test/fixtures/missing_custom_plugin');
expect(exitCode).toBe(1);
expect(normalizeOutput(stdout)).toMatchSnapshot();

Check failure on line 57 in napi/oxlint2/test/e2e.test.ts

View workflow job for this annotation

GitHub Actions / copilot

test/e2e.test.ts > oxlint2 CLI > should report an error if a custom plugin cannot be loaded

Error: Snapshot `oxlint2 CLI > should report an error if a custom plugin cannot be loaded 1` mismatched - Expected + Received "Failed to parse configuration file. - x Failed to load external plugin: ./test_plugin + × Failed to load external plugin: ./test_plugin - | Cannot find module './test_plugin' + │ Cannot find module './test_plugin' " ❯ test/e2e.test.ts:57:37
});

it('should report an error if a rule is not found within a custom plugin', async () => {
const { stdout, exitCode } = await runOxlint('test/fixtures/custom_plugin_missing_rule');
expect(exitCode).toBe(1);
expect(normalizeOutput(stdout)).toMatchSnapshot();

Check failure on line 63 in napi/oxlint2/test/e2e.test.ts

View workflow job for this annotation

GitHub Actions / copilot

test/e2e.test.ts > oxlint2 CLI > should report an error if a rule is not found within a custom plugin

Error: Snapshot `oxlint2 CLI > should report an error if a rule is not found within a custom plugin 1` mismatched - Expected + Received "Failed to parse configuration file. - x Rule 'unknown-rule' not found in plugin 'basic-custom-plugin' + × Rule 'unknown-rule' not found in plugin 'basic-custom-plugin' " ❯ test/e2e.test.ts:63:37
});

it('should report an error if a a rule is not found within a custom plugin (via overrides)', async () => {
const { stdout, exitCode } = await runOxlint('test/fixtures/custom_plugin_via_overrides_missing_rule');
expect(exitCode).toBe(1);
expect(normalizeOutput(stdout)).toMatchSnapshot();

Check failure on line 69 in napi/oxlint2/test/e2e.test.ts

View workflow job for this annotation

GitHub Actions / copilot

test/e2e.test.ts > oxlint2 CLI > should report an error if a a rule is not found within a custom plugin (via overrides)

Error: Snapshot `oxlint2 CLI > should report an error if a a rule is not found within a custom plugin (via overrides) 1` mismatched - Expected + Received "Failed to build configuration. - x Rule 'missing' not found in plugin 'basic-custom-plugin' + × Rule 'missing' not found in plugin 'basic-custom-plugin' " ❯ test/e2e.test.ts:69:37
});

it('should report the correct severity when using a custom plugin', async () => {
const { stdout, exitCode } = await runOxlint('test/fixtures/basic_custom_plugin_warn_severity');
expect(exitCode).toBe(0);
expect(normalizeOutput(stdout)).toMatchSnapshot();

Check failure on line 75 in napi/oxlint2/test/e2e.test.ts

View workflow job for this annotation

GitHub Actions / copilot

test/e2e.test.ts > oxlint2 CLI > should report the correct severity when using a custom plugin

Error: Snapshot `oxlint2 CLI > should report the correct severity when using a custom plugin 1` mismatched - Expected + Received " - ! basic-custom-plugin(no-debugger): Unexpected Debugger Statement - ,-[index.js:1:1] + ⚠ eslint(no-debugger): `debugger` statement is not allowed + ╭─[index.js:1:1] - 1 | debugger; + 1 │ debugger; - : ^^^^^^^^^ - `---- - - ! eslint(no-debugger): `debugger` statement is not allowed - ,-[index.js:1:1] + · ───────── + ╰──── + help: Remove the debugger statement + + ⚠ basic-custom-plugin(no-debugger): Unexpected Debugger Statement + ╭─[index.js:1:1] - 1 | debugger; - : ^^^^^^^^^ - `---- - help: Remove the debugger statement + 1 │ debugger; + · ───────── + ╰──── Found 2 warnings and 0 errors. Finished in Xms on 1 file using X threads." ❯ test/e2e.test.ts:75:37
});

it('should work with multiple rules', async () => {
const { stdout, exitCode } = await runOxlint('test/fixtures/basic_custom_plugin_multiple_rules');
expect(exitCode).toBe(1);
expect(normalizeOutput(stdout)).toMatchSnapshot();

Check failure on line 81 in napi/oxlint2/test/e2e.test.ts

View workflow job for this annotation

GitHub Actions / copilot

test/e2e.test.ts > oxlint2 CLI > should work with multiple rules

Error: Snapshot `oxlint2 CLI > should work with multiple rules 1` mismatched - Expected + Received " - x basic-custom-plugin(no-debugger): Unexpected Debugger Statement - ,-[index.js:1:1] + ⚠ eslint(no-debugger): `debugger` statement is not allowed + ╭─[index.js:1:1] - 1 | debugger; + 1 │ debugger; - : ^^^^^^^^^ - 2 | - `---- - - x basic-custom-plugin(no-debugger-2): Unexpected Debugger Statement - ,-[index.js:1:1] + · ───────── + 2 │ + ╰──── + help: Remove the debugger statement + + × basic-custom-plugin(no-debugger-2): Unexpected Debugger Statement + ╭─[index.js:1:1] - 1 | debugger; + 1 │ debugger; - : ^^^^^^^^^ - 2 | - `---- - - ! eslint(no-debugger): `debugger` statement is not allowed - ,-[index.js:1:1] + · ───────── + 2 │ + ╰──── + + × basic-custom-plugin(no-debugger): Unexpected Debugger Statement + ╭─[index.js:1:1] - 1 | debugger; + 1 │ debugger; - : ^^^^^^^^^ - 2 | - `---- - help: Remove the debugger statement - - x basic-custom-plugin(no-ident-references-named-foo): Unexpected Identifier Reference named foo - ,-[index.js:3:1] + · ───────── + 2 │ + ╰──── + + × basic-custom-plugin(no-ident-references-named-foo): Unexpected Identifier Reference named foo + ╭─[index.js:3:1] - 2 | - 3 | foo; - : ^^^ - `---- + 2 │ + 3 │ foo; + · ─── + ╰──── Found 1 warning and 3 errors. Finished in Xms on 1 file using X threads." ❯ test/e2e.test.ts:81:37
});

it('should receive data via `context`', async () => {
const { stdout, exitCode } = await runOxlint('test/fixtures/context_properties');
expect(exitCode).toBe(1);
expect(normalizeOutput(stdout)).toMatchSnapshot();

Check failure on line 87 in napi/oxlint2/test/e2e.test.ts

View workflow job for this annotation

GitHub Actions / copilot

test/e2e.test.ts > oxlint2 CLI > should receive data via `context`

Error: Snapshot `oxlint2 CLI > should receive data via `context` 1` mismatched - Expected + Received " - x context-plugin(log-context): id: context-plugin/log-context - ,-[files/1.js:1:1] + × context-plugin(log-context): id: context-plugin/log-context + ╭─[files/2.js:1:1] - 1 | let x; + 1 │ let y; - : ^ - `---- - - x context-plugin(log-context): filename: files/1.js - ,-[files/1.js:1:1] + · ▲ + ╰──── + + × context-plugin(log-context): filename: files/2.js + ╭─[files/2.js:1:1] - 1 | let x; + 1 │ let y; - : ^ - `---- - - x context-plugin(log-context): physicalFilename: files/1.js - ,-[files/1.js:1:1] + · ▲ + ╰──── + + × context-plugin(log-context): physicalFilename: files/2.js + ╭─[files/2.js:1:1] - 1 | let x; + 1 │ let y; - : ^ - `---- - - x context-plugin(log-context): id: context-plugin/log-context - ,-[files/2.js:1:1] + · ▲ + ╰──── + + × context-plugin(log-context): id: context-plugin/log-context + ╭─[files/1.js:1:1] - 1 | let y; + 1 │ let x; - : ^ - `---- - - x context-plugin(log-context): filename: files/2.js - ,-[files/2.js:1:1] + · ▲ + ╰──── + + × context-plugin(log-context): filename: files/1.js + ╭─[files/1.js:1:1] - 1 | let y; + 1 │ let x; - : ^ - `---- - - x context-plugin(log-context): physicalFilename: files/2.js - ,-[files/2.js:1:1] + · ▲ + ╰──── + + × context-plugin(log-context): physicalFilename: files/1.js + ╭─[files/1.js:1:1] - 1 | let y; + 1 │ let x; - : ^ - `---- + · ▲ + ╰──── Found 0 warnings and 6 errors. Finished in Xms on 2 files using X threads." ❯ test/e2e.test.ts:87:37
});

it('should have UTF-16 spans in AST', async () => {
const { stdout, exitCode } = await runOxlint('test/fixtures/utf16_offsets');
expect(exitCode).toBe(1);
expect(normalizeOutput(stdout)).toMatchSnapshot();

Check failure on line 93 in napi/oxlint2/test/e2e.test.ts

View workflow job for this annotation

GitHub Actions / copilot

test/e2e.test.ts > oxlint2 CLI > should have UTF-16 spans in AST

Error: Snapshot `oxlint2 CLI > should have UTF-16 spans in AST 1` mismatched - Expected + Received " - ! eslint(no-debugger): `debugger` statement is not allowed - ,-[index.js:1:1] + ⚠ eslint(no-debugger): `debugger` statement is not allowed + ╭─[index.js:1:1] - 1 | debugger; + 1 │ debugger; - : ^^^^^^^^^ - 2 | // £ - `---- - help: Remove the debugger statement + · ───────── + 2 │ // £ + ╰──── + help: Remove the debugger statement - x utf16-plugin(no-debugger): Debugger at 0-9 - ,-[index.js:1:1] - 1 | debugger; - : ^^^^^^^^^ - 2 | // £ + ⚠ eslint(no-debugger): `debugger` statement is not allowed + ╭─[index.js:3:1] + 2 │ // £ + 3 │ debugger; + · ───────── + 4 │ // 🤨 - `---- - - ! eslint(no-debugger): `debugger` statement is not allowed - ,-[index.js:3:1] - 2 | // £ - 3 | debugger; + ╰──── + help: Remove the debugger statement + + ⚠ eslint(no-debugger): `debugger` statement is not allowed + ╭─[index.js:6:3] + 5 │ { + 6 │ debugger; - : ^^^^^^^^^ - 4 | // 🤨 - `---- - help: Remove the debugger statement + · ───────── + 7 │ } + ╰──── + help: Remove the debugger statement - x utf16-plugin(no-debugger): Debugger at 15-24 - ,-[index.js:3:1] - 2 | // £ - 3 | debugger; - : ^^^^^^^^^ - 4 | // 🤨 + × utf16-plugin(no-debugger): Debugger at 0-9 + ╭─[index.js:1:1] + 1 │ debugger; + · ───────── + 2 │ // £ - `---- - - ! eslint(no-debugger): `debugger` statement is not allowed - ,-[index.js:6:3] - 5 | { - 6 | debugger; - : ^^^^^^^^^ - 7 | } - `---- - help: Remove the debugger statement - - x utf16-plugin(no-debugger): Debugger at 35-44 - ,-[index.js:6:3] + ╰──── + + × utf16-plugin(no-debugger): Debugger at 15-24 + ╭─[index.js:3:1] + 2 │ // £ + 3 │ debugger; + · ───────── + 4 │ // 🤨 + ╰──── + + × utf16-plugin(no-debugger): Debugger at 35-44 + ╭─[index.js:6:3] - 5 | { - 6 | debugger; - : ^^^^^^^^^ - 7 | } + 5 │ { + 6 │ debugger; + · ───────── + 7 │ } - `---- + ╰──── Found 3 warnings and 3 errors. Finished in Xms on 1 file using X threads." ❯ test/e2e.test.ts:93:37
});

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();

Check failure on line 99 in napi/oxlint2/test/e2e.test.ts

View workflow job for this annotation

GitHub Actions / copilot

test/e2e.test.ts > oxlint2 CLI > should respect disable directives for custom plugin rules

Error: Snapshot `oxlint2 CLI > should respect disable directives for custom plugin rules 1` mismatched - Expected + Received - " - x test-plugin(no-var): Use let or const instead of var - ,-[index.js:1:1] - 1 | var shouldError = 1; - : ^^^^^^^^^^^^^^^^^^^^ - 2 | - `---- - - x eslint(no-debugger): `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." + "" ❯ test/e2e.test.ts:99:37
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": ["./test_plugin"],
"categories": { "correctness": "off" },
"rules": {
"test-plugin/no-var": "error",
"no-debugger": "error"
},
"ignorePatterns": ["test_plugin/**"]
}
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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,
});
}
},
};
},
},
},
};
Loading