Skip to content

Commit

Permalink
feat(linter): support eslint-plugin-vitest/valid-describe-callback (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
eryue0220 and mysteryven authored Jul 12, 2024
1 parent aab7aaa commit 126b66c
Show file tree
Hide file tree
Showing 3 changed files with 430 additions and 10 deletions.
189 changes: 179 additions & 10 deletions crates/oxc_linter/src/rules/jest/valid_describe_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ use crate::{
context::LintContext,
rule::Rule,
utils::{
collect_possible_jest_call_node, parse_general_jest_fn_call, JestFnKind, JestGeneralFnKind,
PossibleJestNode,
collect_possible_jest_call_node, get_test_plugin_name, parse_general_jest_fn_call,
JestFnKind, JestGeneralFnKind, PossibleJestNode, TestPluginName,
},
};

fn valid_describe_callback_diagnostic(x0: &str, x1: &str, span2: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("eslint-plugin-jest(valid-describe-callback): {x0:?}"))
.with_help(format!("{x1:?}"))
.with_label(span2)
fn valid_describe_callback_diagnostic(
x0: TestPluginName,
x1: &str,
x2: &str,
span3: Span,
) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("{x0}(valid-describe-callback): {x1:?}"))
.with_help(format!("{x2:?}"))
.with_label(span3)
}

#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -58,6 +63,17 @@ declare_oxc_lint!(
/// expect(myFunction()).toBeTruthy();
/// }));
/// ```
///
/// This rule is compatible with [eslint-plugin-vitest](https://github.com/veritem/eslint-plugin-vitest/blob/main/docs/rules/valid-describe-callback.md),
/// to use it, add the following configuration to your `.eslintrc.json`:
///
/// ```json
/// {
/// "rules": {
/// "vitest/valid-describe-callback": "error"
/// }
/// }
/// ```
ValidDescribeCallback,
correctness
);
Expand Down Expand Up @@ -151,7 +167,8 @@ fn find_first_return_stmt_span(function_body: &FunctionBody) -> Option<Span> {

fn diagnostic(ctx: &LintContext, span: Span, message: Message) {
let (error, help) = message.details();
ctx.diagnostic(valid_describe_callback_diagnostic(error, help, span));
let plugin_name = get_test_plugin_name(ctx);
ctx.diagnostic(valid_describe_callback_diagnostic(plugin_name, error, help, span));
}

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -192,7 +209,7 @@ impl Message {
fn test() {
use crate::tester::Tester;

let pass = vec![
let mut pass = vec![
("describe.each([1, 2, 3])('%s', (a, b) => {});", None),
("describe('foo', function() {})", None),
("describe('foo', () => {})", None),
Expand Down Expand Up @@ -235,7 +252,7 @@ fn test() {
),
];

let fail = vec![
let mut fail = vec![
("describe.each()()", None),
("describe['each']()()", None),
("describe.each(() => {})()", None),
Expand Down Expand Up @@ -331,5 +348,157 @@ fn test() {
("describe('foo', async function (done) {})", None),
];

Tester::new(ValidDescribeCallback::NAME, pass, fail).with_jest_plugin(true).test_and_snapshot();
let pass_vitest = vec![
("describe.each([1, 2, 3])(\"%s\", (a, b) => {});", None),
("describe(\"foo\", function() {})", None),
("describe(\"foo\", () => {})", None),
("describe(`foo`, () => {})", None),
("xdescribe(\"foo\", () => {})", None),
("fdescribe(\"foo\", () => {})", None),
("describe.only(\"foo\", () => {})", None),
("describe.skip(\"foo\", () => {})", None),
("describe.todo(\"runPrettierFormat\");", None),
(
"
describe('foo', () => {
it('bar', () => {
return Promise.resolve(42).then(value => {
expect(value).toBe(42)
})
})
})
",
None,
),
(
"
describe('foo', () => {
it('bar', async () => {
expect(await Promise.resolve(42)).toBe(42)
})
})
",
None,
),
(
"
if (hasOwnProperty(obj, key)) {
}
",
None,
),
(
"
describe.each`
foo | foe
${1} | ${2}
`('$something', ({ foo, foe }) => {});
",
None,
),
];

let fail_vitest = vec![
("describe.each()()", None),
("describe[\"each\"]()()", None),
("describe.each(() => {})()", None),
("describe.each(() => {})(\"foo\")", None),
("describe.each()(() => {})", None),
("describe[\"each\"]()(() => {})", None),
("describe.each(\"foo\")(() => {})", None),
("describe.only.each(\"foo\")(() => {})", None),
("describe(() => {})", None),
("describe(\"foo\")", None),
("describe(\"foo\", \"foo2\")", None),
("describe()", None),
("describe(\"foo\", async () => {})", None),
("describe(\"foo\", async function () {})", None),
("xdescribe(\"foo\", async function () {})", None),
("fdescribe(\"foo\", async function () {})", None),
("describe.only(\"foo\", async function () {})", None),
("describe.skip(\"foo\", async function () {})", None),
(
"
describe('sample case', () => {
it('works', () => {
expect(true).toEqual(true);
});
describe('async', async () => {
await new Promise(setImmediate);
it('breaks', () => {
throw new Error('Fail');
});
});
});
",
None,
),
(
"
describe('foo', function () {
return Promise.resolve().then(() => {
it('breaks', () => {
throw new Error('Fail')
})
})
})
",
None,
),
(
"
describe('foo', () => {
return Promise.resolve().then(() => {
it('breaks', () => {
throw new Error('Fail')
})
})
describe('nested', () => {
return Promise.resolve().then(() => {
it('breaks', () => {
throw new Error('Fail')
})
})
})
})
",
None,
),
(
"
describe('foo', async () => {
await something()
it('does something')
describe('nested', () => {
return Promise.resolve().then(() => {
it('breaks', () => {
throw new Error('Fail')
})
})
})
})
",
None,
),
(
"
describe('foo', () =>
test('bar', () => {})
)
",
None,
),
("describe(\"foo\", done => {})", None),
("describe(\"foo\", function (done) {})", None),
("describe(\"foo\", function (one, two, three) {})", None),
("describe(\"foo\", async function (done) {})", None),
];

pass.extend(pass_vitest);
fail.extend(fail_vitest);

Tester::new(ValidDescribeCallback::NAME, pass, fail)
.with_jest_plugin(true)
.with_vitest_plugin(true)
.test_and_snapshot();
}
Loading

0 comments on commit 126b66c

Please sign in to comment.