Skip to content

Commit

Permalink
refactor(linter): make some jest rules report more detailed (#1666)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryven committed Dec 14, 2023
1 parent 7594a9d commit d719af4
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 92 deletions.
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]
1it("should fail", () => {});
· ───────────────────────────
· ──
╰────
help: Add assertion(s) in this Test

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

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

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

eslint-plugin-jest(expect-expect): Test has no assertions
╭─[expect_expect.tsx:1:1]
1it("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]
1test("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]
1it("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]
1test('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]
1test('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]
1test('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]
1test('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]
1test('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
4checkThat('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
4checkThat.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
2it("should warn on non-assert await expression", async () => {
· ──
3const asyncFunction = async () => {
╰────
help: Add assertion(s) in this Test

Expand Down
Loading

0 comments on commit d719af4

Please sign in to comment.