From 27f5c4bbffea1f7813891adb9357714e2e647490 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Tue, 23 Jul 2024 17:38:19 +0800 Subject: [PATCH] feat: support vitest-no-commented-out-tests --- .../src/rules/jest/no_commented_out_tests.rs | 77 ++++++++++++++++--- .../src/snapshots/no_commented_out_tests.snap | 52 +++++++++++++ crates/oxc_linter/src/utils/mod.rs | 1 + 3 files changed, 119 insertions(+), 11 deletions(-) diff --git a/crates/oxc_linter/src/rules/jest/no_commented_out_tests.rs b/crates/oxc_linter/src/rules/jest/no_commented_out_tests.rs index 6be16fafeff45..159ec5c40f4fc 100644 --- a/crates/oxc_linter/src/rules/jest/no_commented_out_tests.rs +++ b/crates/oxc_linter/src/rules/jest/no_commented_out_tests.rs @@ -4,14 +4,16 @@ use oxc_macros::declare_oxc_lint; use oxc_span::Span; use regex::Regex; -use crate::{context::LintContext, rule::Rule}; +use crate::{ + context::LintContext, + rule::Rule, + utils::{get_test_plugin_name, TestPluginName}, +}; -fn no_commented_out_tests_diagnostic(span0: Span) -> OxcDiagnostic { - OxcDiagnostic::warn( - "eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented", - ) - .with_help("Remove or uncomment this comment") - .with_label(span0) +fn no_commented_out_tests_diagnostic(x0: TestPluginName, span1: Span) -> OxcDiagnostic { + OxcDiagnostic::warn(format!("{x0}(no-commented-out-tests): Some tests seem to be commented")) + .with_help("Remove or uncomment this comment") + .with_label(span1) } #[derive(Debug, Default, Clone)] @@ -39,6 +41,17 @@ declare_oxc_lint!( /// // it.skip('foo', () => {}); /// // test.skip('foo', () => {}); /// ``` + /// + /// This rule is compatible with [eslint-plugin-vitest](https://github.com/veritem/eslint-plugin-vitest/blob/main/docs/rules/no-commented-out-tests.md), + /// to use it, add the following configuration to your `.eslintrc.json`: + /// + /// ```json + /// { + /// "rules": { + /// "vitest/no-commented-out-tests": "error" + /// } + /// } + /// ``` NoCommentedOutTests, suspicious ); @@ -60,9 +73,10 @@ impl Rule for NoCommentedOutTests { None } }); + let plugin_name = get_test_plugin_name(ctx); for span in commented_tests { - ctx.diagnostic(no_commented_out_tests_diagnostic(span)); + ctx.diagnostic(no_commented_out_tests_diagnostic(plugin_name, span)); } } } @@ -71,7 +85,7 @@ impl Rule for NoCommentedOutTests { fn test() { use crate::tester::Tester; - let pass = vec![ + let mut pass = vec![ ("// foo('bar', function () {})", None), ("describe('foo', function () {})", None), ("it('foo', function () {})", None), @@ -122,7 +136,7 @@ fn test() { ), ]; - let fail = vec![ + let mut fail = vec![ ("// fdescribe('foo', function () {})", None), ("// describe['skip']('foo', function () {})", None), ("// describe['skip']('foo', function () {})", None), @@ -172,5 +186,46 @@ fn test() { ), ]; - Tester::new(NoCommentedOutTests::NAME, pass, fail).with_jest_plugin(true).test_and_snapshot(); + let pass_vitest = vec![ + r#"// foo("bar", function () {})"#, + r#"describe("foo", function () {})"#, + r#"it("foo", function () {})"#, + r#"describe.only("foo", function () {})"#, + r#"it.only("foo", function () {})"#, + r#"it.concurrent("foo", function () {})"#, + r#"test("foo", function () {})"#, + r#"test.only("foo", function () {})"#, + r#"test.concurrent("foo", function () {})"#, + r"var appliedSkip = describe.skip; appliedSkip.apply(describe)", + r"var calledSkip = it.skip; calledSkip.call(it)", + r"({ f: function () {} }).f()", + r"(a || b).f()", + r"itHappensToStartWithIt()", + r"testSomething()", + r"// latest(dates)", + r"// TODO: unify with Git implementation from Shipit (?)", + "#!/usr/bin/env node#", + ]; + + let fail_vitest = vec![ + r"// describe(\'foo\', function () {})\'", + r#"// test.concurrent("foo", function () {})"#, + r#"// test["skip"]("foo", function () {})"#, + r#"// xdescribe("foo", function () {})"#, + r#"// xit("foo", function () {})"#, + r#"// fit("foo", function () {})"#, + r#" + // test( + // "foo", function () {} + // ) + "#, + ]; + + pass.extend(pass_vitest.into_iter().map(|x| (x, None))); + fail.extend(fail_vitest.into_iter().map(|x| (x, None))); + + Tester::new(NoCommentedOutTests::NAME, pass, fail) + .with_jest_plugin(true) + .with_vitest_plugin(true) + .test_and_snapshot(); } diff --git a/crates/oxc_linter/src/snapshots/no_commented_out_tests.snap b/crates/oxc_linter/src/snapshots/no_commented_out_tests.snap index dd899762988cd..e7ba77647c4be 100644 --- a/crates/oxc_linter/src/snapshots/no_commented_out_tests.snap +++ b/crates/oxc_linter/src/snapshots/no_commented_out_tests.snap @@ -1,5 +1,6 @@ --- source: crates/oxc_linter/src/tester.rs +assertion_line: 216 --- ⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented ╭─[no_commented_out_tests.tsx:1:3] @@ -164,3 +165,54 @@ source: crates/oxc_linter/src/tester.rs 6 │ bar() ╰──── help: Remove or uncomment this comment + + ⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented + ╭─[no_commented_out_tests.tsx:1:3] + 1 │ // describe(\'foo\', function () {})\' + · ──────────────────────────────────── + ╰──── + help: Remove or uncomment this comment + + ⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented + ╭─[no_commented_out_tests.tsx:1:3] + 1 │ // test.concurrent("foo", function () {}) + · ─────────────────────────────────────── + ╰──── + help: Remove or uncomment this comment + + ⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented + ╭─[no_commented_out_tests.tsx:1:3] + 1 │ // test["skip"]("foo", function () {}) + · ──────────────────────────────────── + ╰──── + help: Remove or uncomment this comment + + ⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented + ╭─[no_commented_out_tests.tsx:1:3] + 1 │ // xdescribe("foo", function () {}) + · ───────────────────────────────── + ╰──── + help: Remove or uncomment this comment + + ⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented + ╭─[no_commented_out_tests.tsx:1:3] + 1 │ // xit("foo", function () {}) + · ─────────────────────────── + ╰──── + help: Remove or uncomment this comment + + ⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented + ╭─[no_commented_out_tests.tsx:1:3] + 1 │ // fit("foo", function () {}) + · ─────────────────────────── + ╰──── + help: Remove or uncomment this comment + + ⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented + ╭─[no_commented_out_tests.tsx:2:15] + 1 │ + 2 │ // test( + · ────── + 3 │ // "foo", function () {} + ╰──── + help: Remove or uncomment this comment diff --git a/crates/oxc_linter/src/utils/mod.rs b/crates/oxc_linter/src/utils/mod.rs index c62c3de126fc1..0abf9a8996437 100644 --- a/crates/oxc_linter/src/utils/mod.rs +++ b/crates/oxc_linter/src/utils/mod.rs @@ -20,6 +20,7 @@ pub fn is_jest_rule_adapted_to_vitest(rule_name: &str) -> bool { "consistent-test-it", "expect-expect", "no-alias-methods", + "no-commented-out-tests", "no-disabled-tests", "no-focused-tests", "no-test-prefixes",