11use oxc_ast:: AstKind ;
2- use oxc_diagnostics:: OxcDiagnostic ;
32use oxc_macros:: declare_oxc_lint;
4- use oxc_span:: Span ;
53
64use crate :: {
75 context:: LintContext ,
86 rule:: Rule ,
9- utils :: { JestFnKind , PossibleJestNode , is_type_of_jest_fn_call } ,
7+ rules :: shared :: no_conditional_in_test :: { DOCUMENTATION , run } ,
108} ;
119
12- fn no_conditional_in_test ( span : Span ) -> OxcDiagnostic {
13- OxcDiagnostic :: warn ( "Avoid having conditionals in tests." )
14- . with_help ( "Replace conditionals with separate test cases for each branch to keep tests deterministic and easy to understand." )
15- . with_label ( span)
16- }
17-
1810#[ derive( Debug , Default , Clone ) ]
1911pub struct NoConditionalInTest ;
2012
21- declare_oxc_lint ! (
22- /// ### What it does
23- ///
24- /// Disallow conditional statements in tests.
25- ///
26- /// ### Why is this bad?
27- ///
28- /// Conditional statements in tests can make the test harder to read and understand. It is better to have a single test case per test function.
29- ///
30- /// ### Examples
31- ///
32- /// Examples of **incorrect** code for this rule:
33- /// ```js
34- /// it('foo', () => {
35- /// if (true) {
36- /// doTheThing();
37- /// }
38- /// });
39- ///
40- /// it('bar', () => {
41- /// switch (mode) {
42- /// case 'none':
43- /// generateNone();
44- /// case 'single':
45- /// generateOne();
46- /// case 'multiple':
47- /// generateMany();
48- /// }
49- ///
50- /// expect(fixtures.length).toBeGreaterThan(-1);
51- /// });
52- ///
53- /// it('baz', async () => {
54- /// const promiseValue = () => {
55- /// return something instanceof Promise
56- /// ? something
57- /// : Promise.resolve(something);
58- /// };
59- ///
60- /// await expect(promiseValue()).resolves.toBe(1);
61- /// });
62- /// ```
63- ///
64- /// Examples of **correct** code for this rule:
65- /// ```js
66- /// describe('my tests', () => {
67- /// if (true) {
68- /// it('foo', () => {
69- /// doTheThing();
70- /// });
71- /// }
72- /// });
73- ///
74- /// beforeEach(() => {
75- /// switch (mode) {
76- /// case 'none':
77- /// generateNone();
78- /// case 'single':
79- /// generateOne();
80- /// case 'multiple':
81- /// generateMany();
82- /// }
83- /// });
84- ///
85- /// it('bar', () => {
86- /// expect(fixtures.length).toBeGreaterThan(-1);
87- /// });
88- ///
89- /// const promiseValue = something => {
90- /// return something instanceof Promise ? something : Promise.resolve(something);
91- /// };
92- ///
93- /// it('baz', async () => {
94- /// await expect(promiseValue()).resolves.toBe(1);
95- /// });
96- /// ```
97- ///
98- /// This rule is compatible with [eslint-plugin-vitest](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/no-conditional-in-test.md),
99- /// to use it, add the following configuration to your `.oxlintrc.json`:
100- ///
101- /// ```json
102- /// {
103- /// "rules": {
104- /// "vitest/no-conditional-in-test": "error"
105- /// }
106- /// }
107- /// ```
108- NoConditionalInTest ,
109- jest,
110- pedantic,
111- version = "0.8.0" ,
112- ) ;
13+ declare_oxc_lint ! ( NoConditionalInTest , jest, pedantic, docs = DOCUMENTATION , version = "0.8.0" , ) ;
11314
11415impl Rule for NoConditionalInTest {
11516 fn run < ' a > ( & self , node : & oxc_semantic:: AstNode < ' a > , ctx : & LintContext < ' a > ) {
@@ -121,29 +22,7 @@ impl Rule for NoConditionalInTest {
12122 _ => return ,
12223 }
12324
124- let is_if_statement_in_test = ctx. nodes ( ) . ancestors ( node. id ( ) ) . any ( |node| {
125- let AstKind :: CallExpression ( call_expr) = node. kind ( ) else { return false } ;
126- let vitest_node = PossibleJestNode { node, original : None } ;
127-
128- is_type_of_jest_fn_call (
129- call_expr,
130- & vitest_node,
131- ctx,
132- & [ JestFnKind :: General ( crate :: utils:: JestGeneralFnKind :: Test ) ] ,
133- )
134- } ) ;
135-
136- if is_if_statement_in_test {
137- let span = match node. kind ( ) {
138- AstKind :: IfStatement ( stmt) => stmt. span ,
139- AstKind :: SwitchStatement ( stmt) => stmt. span ,
140- AstKind :: ConditionalExpression ( expr) => expr. span ,
141- AstKind :: LogicalExpression ( expr) => expr. span ,
142- _ => unreachable ! ( ) ,
143- } ;
144-
145- ctx. diagnostic ( no_conditional_in_test ( span) ) ;
146- }
25+ run ( node, ctx) ;
14726 }
14827}
14928
@@ -661,6 +540,5 @@ fn test() {
661540
662541 Tester :: new ( NoConditionalInTest :: NAME , NoConditionalInTest :: PLUGIN , pass, fail)
663542 . with_jest_plugin ( true )
664- . with_vitest_plugin ( true )
665543 . test_and_snapshot ( ) ;
666544}
0 commit comments