Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(linter): make some jest rules report more detailed #1666

Merged
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
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/jest/expect_expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use oxc_diagnostics::{
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_span::{GetSpan, Span};
use regex::Regex;

use crate::{
Expand Down Expand Up @@ -117,7 +117,7 @@ fn run<'a>(
let has_assert_function = check_arguments(call_expr, &rule.assert_function_names, ctx);

if !has_assert_function {
ctx.diagnostic(ExpectExpectDiagnostic(call_expr.span));
ctx.diagnostic(ExpectExpectDiagnostic(call_expr.callee.span()));
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions crates/oxc_linter/src/rules/jest/no_conditional_expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::{
context::LintContext,
rule::Rule,
utils::{
collect_possible_jest_call_node, is_type_of_jest_fn_call, JestFnKind, JestGeneralFnKind,
PossibleJestNode,
collect_possible_jest_call_node, is_type_of_jest_fn_call, parse_expect_jest_fn_call,
JestFnKind, JestGeneralFnKind, PossibleJestNode,
},
};

Expand Down Expand Up @@ -77,13 +77,14 @@ fn run<'a>(
) {
let node = possible_jest_node.node;
if let AstKind::CallExpression(call_expr) = node.kind() {
if !is_type_of_jest_fn_call(call_expr, possible_jest_node, ctx, &[JestFnKind::Expect]) {
let Some(jest_fn_call) = parse_expect_jest_fn_call(call_expr, possible_jest_node, ctx)
else {
return;
}
};

let has_condition_or_catch = check_parents(node, id_nodes_mapping, ctx, false);
if has_condition_or_catch {
ctx.diagnostic(NoConditionalExpectDiagnostic(call_expr.span));
ctx.diagnostic(NoConditionalExpectDiagnostic(jest_fn_call.head.span));
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions crates/oxc_linter/src/rules/jest/no_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ impl Rule for NoExport {
return;
}

for local_export in &ctx.semantic().module_record().local_export_entries {
ctx.diagnostic(NoExportDiagnostic(local_export.span));
for span in ctx.semantic().module_record().exported_bindings.values() {
ctx.diagnostic(NoExportDiagnostic(*span));
}

if let Some(span) = ctx.semantic().module_record().export_default {
ctx.diagnostic(NoExportDiagnostic(span));
}
}
}
Expand Down
56 changes: 25 additions & 31 deletions crates/oxc_linter/src/snapshots/expect_expect.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,124 +5,118 @@ expression: expect_expect
⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │ it("should fail", () => {});
· ───────────────────────────
· ──
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │ it("should fail", myTest); function myTest() {}
· ─────────────────────────
· ──
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │ test("should fail", () => {});
· ─────────────────────────────
· ────
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │ test.skip("should fail", () => {});
· ──────────────────────────────────
· ─────────
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │ afterEach(() => {});
· ───────────────────
· ─────────
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │ it("should fail", () => { somePromise.then(() => {}); });
· ────────────────────────────────────────────────────────
· ──
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │ test("should fail", () => { foo(true).toBe(true); })
· ────────────────────────────────────────────────────
· ────
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │ it("should also fail",() => expectSaga(mySaga).returns());
· ─────────────────────────────────────────────────────────
· ──
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │ test('should fail', () => request.get().foo().expect(456));
· ──────────────────────────────────────────────────────────
· ────
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │ test('should fail', () => request.get().foo().bar().expect(456));
· ────────────────────────────────────────────────────────────────
· ────
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │ test('should fail', () => tester.request(123));
· ──────────────────────────────────────────────
· ────
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │ test('should fail', () => request(123));
· ───────────────────────────────────────
· ────
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │ test('should fail', () => request(123));
· ───────────────────────────────────────
· ────
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:3:1]
3 │
4 │ ╭─▶ checkThat('this passes', () => {
5 │ │ // ...
6 │ ╰─▶ });
7 │
3 │
4 │ checkThat('this passes', () => {
· ─────────
5 │ // ...
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:3:1]
3 │
4 │ ╭─▶ checkThat.skip('this passes', () => {
5 │ │ // ...
6 │ ╰─▶ });
7 │
3 │
4 │ checkThat.skip('this passes', () => {
· ──────────────
5 │ // ...
╰────
help: Add assertion(s) in this Test

⚠ eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1 │
2 │ ╭─▶ it("should warn on non-assert await expression", async () => {
3 │ │ const asyncFunction = async () => {
4 │ │ throw new Error('nope')
5 │ │ };
6 │ │ await foo(asyncFunction()).rejects.toThrow();
7 │ ╰─▶ });
8 │
1 │
2 │ it("should warn on non-assert await expression", async () => {
· ──
3 │ const asyncFunction = async () => {
╰────
help: Add assertion(s) in this Test

Expand Down
Loading