Skip to content

Commit f7becef

Browse files
committed
feat(linter/eslint-plugin-vitest): Now forcing the vitest detection and update snpashots
1 parent b90dc68 commit f7becef

File tree

4 files changed

+98
-53
lines changed

4 files changed

+98
-53
lines changed

apps/oxlint/src/snapshots/_--vitest-plugin -c fixtures__eslintrc_vitest_replace__eslintrc.json [email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ working directory:
2323
help: Remove the appending `.skip`
2424
2525
Found 1 warning and 1 error.
26-
Finished in <variable>ms on 1 file with 102 rules using 1 threads.
26+
Finished in <variable>ms on 1 file with 103 rules using 1 threads.
2727
----------
2828
CLI result: LintFoundErrors
2929
----------

crates/oxc_linter/src/rules/vitest/warn_todo.rs

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ declare_oxc_lint!(
4848
/// ```
4949
WarnTodo,
5050
vitest,
51-
correctness
51+
correctness,
5252
);
5353

5454
impl Rule for WarnTodo {
@@ -98,28 +98,53 @@ impl WarnTodo {
9898
fn test() {
9999
use crate::tester::Tester;
100100

101+
/*
102+
* Currently the responsible to set what frameworks are active or not is not `with_vitest_plugin` or oxlint config.
103+
* The code that set what test framewors are active is ContextHost::sniff_for_frameworks, and the current detection lead to a
104+
* a false negative. To detect if the current source code belongs to vitest is based if a `vitest` import exist, if not, assumes
105+
* we are on a possible jest test. On top of that, the method `frameworks::is_jestlike_file` most of the times is going to be true, at least in
106+
* our current situation. So this lead that the ContextHost can have jest and vitest active **at same time**.
107+
*
108+
* This detection isn't compatible on how `parse_general_jest_fn_call` handle if a node is valid or not. To make it simple:
109+
*
110+
* - Jest file: ctx.frameworks().is_jest() is true && ctx.frameworks().is_vitest() is false
111+
* - Vitest file: ctx.frameworks().is_jest() is true && ctx.frameworks().is_vitest is true
112+
*
113+
* And if you are dealing with non compatible modifiers or methods, that only exists in vitest, it will fail as in jest doesn't exist.
114+
*
115+
* In case of dealing with syntax that only exists in vitest, add an import of `vitest` to force the ContextHost to detect we are dealing with vitest.
116+
* This probably will allow reuse allow of the methods that rely on this false negative detection.
117+
*/
118+
macro_rules! vitest_context {
119+
($test: literal) => {
120+
concat!("import * as vi from 'vitest'\n\n", $test)
121+
};
122+
}
123+
101124
let pass = vec![
102-
("describe('foo', function () {})", None),
103-
("it('foo', function () {})", None),
104-
("it.concurrent('foo', function () {})", None),
105-
("test('foo', function () {})", None),
106-
("test.concurrent('foo', function () {})", None),
107-
("describe.only('foo', function () {})", None),
108-
("it.only('foo', function () {})", None),
109-
("it.each()('foo', function () {})", None),
125+
(vitest_context!("describe('foo', function () {})"), None),
126+
(vitest_context!("it('foo', function () {})"), None),
127+
(vitest_context!("it.concurrent('foo', function () {})"), None),
128+
(vitest_context!("test('foo', function () {})"), None),
129+
(vitest_context!("test.concurrent('foo', function () {})"), None),
130+
(vitest_context!("describe.only('foo', function () {})"), None),
131+
(vitest_context!("it.only('foo', function () {})"), None),
132+
(vitest_context!("it.each()('foo', function () {})"), None),
110133
];
111134

112135
let fail = vec![
113-
("describe.todo('foo', function () {})", None),
114-
("it.todo('foo', function () {})", None),
115-
("test.todo('foo', function () {})", None),
116-
("describe.todo.each([])('foo', function () {})", None),
117-
("it.todo.each([])('foo', function () {})", None),
118-
("test.todo.each([])('foo', function () {})", None),
119-
("describe.only.todo('foo', function () {})", None),
120-
("it.only.todo('foo', function () {})", None),
121-
("test.only.todo('foo', function () {})", None),
136+
(vitest_context!("describe.todo('foo', function () {})"), None),
137+
(vitest_context!("it.todo('foo', function () {})"), None),
138+
(vitest_context!("test.todo('foo', function () {})"), None),
139+
(vitest_context!("describe.todo.each([])('foo', function () {})"), None),
140+
(vitest_context!("it.todo.each([])('foo', function () {})"), None),
141+
(vitest_context!("test.todo.each([])('foo', function () {})"), None),
142+
(vitest_context!("describe.only.todo('foo', function () {})"), None),
143+
(vitest_context!("it.only.todo('foo', function () {})"), None),
144+
(vitest_context!("test.only.todo('foo', function () {})"), None),
122145
];
123146

124-
Tester::new(WarnTodo::NAME, WarnTodo::PLUGIN, pass, fail).test_and_snapshot();
147+
Tester::new(WarnTodo::NAME, WarnTodo::PLUGIN, pass, fail)
148+
.with_vitest_plugin(true)
149+
.test_and_snapshot();
125150
}

crates/oxc_linter/src/snapshots/vitest_warn_todo.snap

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,73 @@
22
source: crates/oxc_linter/src/tester.rs
33
---
44
eslint-plugin-vitest(warn-todo): The use of `.todo` is not recommended.
5-
╭─[warn_todo.tsx:1:10]
6-
1describe.todo('foo', function () {})
5+
╭─[warn_todo.tsx:3:10]
6+
2
7+
3describe.todo('foo', function () {})
78
· ────
89
╰────
9-
help: Remove the `.todo` modifier before push your changes.
10+
help: Write an actual test and remove the `.todo` modifier before pushing/merging your changes.
1011

1112
eslint-plugin-vitest(warn-todo): The use of `.todo` is not recommended.
12-
╭─[warn_todo.tsx:1:4]
13-
1it.todo('foo', function () {})
13+
╭─[warn_todo.tsx:3:4]
14+
2
15+
3it.todo('foo', function () {})
1416
· ────
1517
╰────
16-
help: Remove the `.todo` modifier before push your changes.
18+
help: Write an actual test and remove the `.todo` modifier before pushing/merging your changes.
1719

1820
eslint-plugin-vitest(warn-todo): The use of `.todo` is not recommended.
19-
╭─[warn_todo.tsx:1:6]
20-
1test.todo('foo', function () {})
21+
╭─[warn_todo.tsx:3:6]
22+
2
23+
3test.todo('foo', function () {})
2124
· ────
2225
╰────
23-
help: Remove the `.todo` modifier before push your changes.
26+
help: Write an actual test and remove the `.todo` modifier before pushing/merging your changes.
2427

2528
eslint-plugin-vitest(warn-todo): The use of `.todo` is not recommended.
26-
╭─[warn_todo.tsx:1:10]
27-
1describe.todo.each([])('foo', function () {})
29+
╭─[warn_todo.tsx:3:10]
30+
2
31+
3describe.todo.each([])('foo', function () {})
2832
· ────
2933
╰────
30-
help: Remove the `.todo` modifier before push your changes.
34+
help: Write an actual test and remove the `.todo` modifier before pushing/merging your changes.
3135

3236
eslint-plugin-vitest(warn-todo): The use of `.todo` is not recommended.
33-
╭─[warn_todo.tsx:1:4]
34-
1it.todo.each([])('foo', function () {})
37+
╭─[warn_todo.tsx:3:4]
38+
2
39+
3it.todo.each([])('foo', function () {})
3540
· ────
3641
╰────
37-
help: Remove the `.todo` modifier before push your changes.
42+
help: Write an actual test and remove the `.todo` modifier before pushing/merging your changes.
3843

3944
eslint-plugin-vitest(warn-todo): The use of `.todo` is not recommended.
40-
╭─[warn_todo.tsx:1:6]
41-
1test.todo.each([])('foo', function () {})
45+
╭─[warn_todo.tsx:3:6]
46+
2
47+
3test.todo.each([])('foo', function () {})
4248
· ────
4349
╰────
44-
help: Remove the `.todo` modifier before push your changes.
50+
help: Write an actual test and remove the `.todo` modifier before pushing/merging your changes.
4551

4652
eslint-plugin-vitest(warn-todo): The use of `.todo` is not recommended.
47-
╭─[warn_todo.tsx:1:15]
48-
1describe.only.todo('foo', function () {})
53+
╭─[warn_todo.tsx:3:15]
54+
2
55+
3describe.only.todo('foo', function () {})
4956
· ────
5057
╰────
51-
help: Remove the `.todo` modifier before push your changes.
58+
help: Write an actual test and remove the `.todo` modifier before pushing/merging your changes.
5259

5360
eslint-plugin-vitest(warn-todo): The use of `.todo` is not recommended.
54-
╭─[warn_todo.tsx:1:9]
55-
1it.only.todo('foo', function () {})
61+
╭─[warn_todo.tsx:3:9]
62+
2
63+
3it.only.todo('foo', function () {})
5664
· ────
5765
╰────
58-
help: Remove the `.todo` modifier before push your changes.
66+
help: Write an actual test and remove the `.todo` modifier before pushing/merging your changes.
5967

6068
eslint-plugin-vitest(warn-todo): The use of `.todo` is not recommended.
61-
╭─[warn_todo.tsx:1:11]
62-
1test.only.todo('foo', function () {})
69+
╭─[warn_todo.tsx:3:11]
70+
2
71+
3test.only.todo('foo', function () {})
6372
· ────
6473
╰────
65-
help: Remove the `.todo` modifier before push your changes.
74+
help: Write an actual test and remove the `.todo` modifier before pushing/merging your changes.

crates/oxc_linter/src/utils/jest/parse_jest_fn.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,23 @@ pub fn parse_jest_fn_call<'a>(
106106
let mut call_chains = Vec::from([Cow::Borrowed(name)]);
107107
call_chains.extend(members.iter().filter_map(KnownMemberExpressionProperty::name));
108108

109-
if ctx.frameworks().is_jest() && !is_valid_jest_call(&call_chains) {
110-
return None;
111-
}
112-
113-
if ctx.frameworks().is_vitest() && !is_valid_vitest_call(&call_chains) {
114-
return None;
109+
match (ctx.frameworks().is_jest(), ctx.frameworks().is_vitest()) {
110+
(true, true) => {
111+
if !is_valid_jest_call(&call_chains) && !is_valid_vitest_call(&call_chains) {
112+
return None;
113+
}
114+
}
115+
(true, false) => {
116+
if !is_valid_jest_call(&call_chains) {
117+
return None;
118+
}
119+
}
120+
(false, true) => {
121+
if !is_valid_vitest_call(&call_chains) {
122+
return None;
123+
}
124+
}
125+
(false, false) => {}
115126
}
116127

117128
return Some(ParsedJestFnCall::GeneralJest(ParsedGeneralJestFnCall {

0 commit comments

Comments
 (0)