-
-
Notifications
You must be signed in to change notification settings - Fork 394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(linter): eslint-plugin-vitest/require-local-test-context-for-concurrent-snapshots
#4796
Closed
eryue0220
wants to merge
2
commits into
oxc-project:main
from
eryue0220:feat/lint-require-local-test-context-for-concurrent-snapshots
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
257 changes: 257 additions & 0 deletions
257
crates/oxc_linter/src/rules/vitest/require_local_test_context_for_concurrent_snapshots.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
use oxc_ast::{ | ||
ast::{BindingPatternKind, FormalParameters, PropertyKey}, | ||
AstKind, | ||
}; | ||
use oxc_diagnostics::OxcDiagnostic; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_semantic::AstNode; | ||
use oxc_span::Span; | ||
|
||
use crate::{ | ||
context::LintContext, | ||
rule::Rule, | ||
utils::{ | ||
is_type_of_jest_fn_call, parse_expect_jest_fn_call, JestFnKind, JestGeneralFnKind, | ||
PossibleJestNode, | ||
}, | ||
}; | ||
|
||
fn require_local_test_context(span0: Span) -> OxcDiagnostic { | ||
OxcDiagnostic::warn("require local Test Context for concurrent snapshot tests".to_string()) | ||
.with_help("Use local Test Context instead") | ||
.with_label(span0) | ||
} | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct RequireLocalTestContextForConcurrentSnapshots; | ||
|
||
declare_oxc_lint!( | ||
/// ### Examples | ||
/// | ||
/// ```javascript | ||
/// // invalid | ||
/// test.concurrent('myLogic', () => { | ||
/// expect(true).toMatchSnapshot(); | ||
/// }) | ||
/// | ||
/// describe.concurrent('something', () => { | ||
/// test('myLogic', () => { | ||
/// expect(true).toMatchInlineSnapshot(); | ||
/// }) | ||
/// }) | ||
/// ``` | ||
/// | ||
/// ```javascript | ||
/// // valid | ||
/// test.concurrent('myLogic', ({ expect }) => { | ||
/// expect(true).toMatchSnapshot(); | ||
/// }) | ||
/// | ||
/// test.concurrent('myLogic', (context) => { | ||
/// context.expect(true).toMatchSnapshot(); | ||
/// }) | ||
/// ``` | ||
RequireLocalTestContextForConcurrentSnapshots, | ||
style, | ||
); | ||
|
||
impl Rule for RequireLocalTestContextForConcurrentSnapshots { | ||
fn run_once(&self, ctx: &LintContext) { | ||
let mut function_args: Vec<String> = vec![]; | ||
let nodes = ctx.nodes(); | ||
|
||
for node in nodes.iter() { | ||
if let AstKind::Function(func) = node.kind() { | ||
Self::collect_functions_params(&func.params, &mut function_args); | ||
} else if let AstKind::ArrowFunctionExpression(arrow_func) = node.kind() { | ||
Self::collect_functions_params(&arrow_func.params, &mut function_args); | ||
} | ||
} | ||
|
||
for node in nodes.iter() { | ||
Self::check(node, &mut function_args, ctx); | ||
} | ||
} | ||
} | ||
|
||
impl RequireLocalTestContextForConcurrentSnapshots { | ||
fn collect_functions_params(params: &FormalParameters, function_args: &mut Vec<String>) { | ||
if !params.is_empty() { | ||
for params in ¶ms.items { | ||
match ¶ms.pattern.kind { | ||
BindingPatternKind::BindingIdentifier(ident) => { | ||
function_args.push(ident.name.to_string()); | ||
} | ||
BindingPatternKind::ObjectPattern(obj_pat) => { | ||
for prop in &obj_pat.properties { | ||
if let PropertyKey::StaticIdentifier(ident) = &prop.key { | ||
function_args.push(ident.name.to_string()); | ||
} | ||
} | ||
} | ||
_ => (), | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn check<'a>(node: &AstNode<'a>, function_args: &mut [String], ctx: &LintContext<'a>) { | ||
let AstKind::CallExpression(call_expr) = node.kind() else { | ||
return; | ||
}; | ||
let Some(callee_name) = call_expr.callee_name() else { | ||
return; | ||
}; | ||
let Some(expect_fn_call) = | ||
parse_expect_jest_fn_call(call_expr, &PossibleJestNode { node, original: None }, ctx) | ||
else { | ||
return; | ||
}; | ||
|
||
if function_args.contains(&expect_fn_call.name.to_string()) { | ||
return; | ||
} | ||
|
||
if ![ | ||
"toMatchSnapshot", | ||
"toMatchInlineSnapshot", | ||
"toMatchFileSnapshot", | ||
"toThrowErrorMatchingSnapshot", | ||
"toThrowErrorMatchingInlineSnapshot", | ||
] | ||
.contains(&callee_name) | ||
{ | ||
return; | ||
} | ||
|
||
let mut is_inside_sequential_describe_or_test = false; | ||
|
||
for parent_node_id in ctx.nodes().ancestors(node.id()) { | ||
let parent_node = ctx.nodes().get_node(parent_node_id); | ||
if let AstKind::CallExpression(parent_call_expr) = parent_node.kind() { | ||
if let Some(callee_name) = parent_call_expr.callee_name() { | ||
let is_describe_or_test = is_type_of_jest_fn_call( | ||
parent_call_expr, | ||
&PossibleJestNode { node: parent_node, original: None }, | ||
ctx, | ||
&[ | ||
JestFnKind::General(JestGeneralFnKind::Describe), | ||
JestFnKind::General(JestGeneralFnKind::Test), | ||
], | ||
); | ||
|
||
if is_describe_or_test | ||
&& parent_call_expr.callee.is_member_expression() | ||
&& callee_name.eq("concurrent") | ||
{ | ||
is_inside_sequential_describe_or_test = true; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if !is_inside_sequential_describe_or_test { | ||
return; | ||
} | ||
|
||
ctx.diagnostic(require_local_test_context(call_expr.span)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn tests() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec![ | ||
(r#"it("something", () => { expect(true).toBe(true) })"#, None), | ||
(r#"it.concurrent("something", () => { expect(true).toBe(true) })"#, None), | ||
(r#"it("something", () => { expect(1).toMatchSnapshot() })"#, None), | ||
(r#"it.concurrent("something", ({ expect }) => { expect(1).toMatchSnapshot() })"#, None), | ||
( | ||
r#"it.concurrent("something", ({ expect }) => { expect(1).toMatchInlineSnapshot("1") })"#, | ||
None, | ||
), | ||
( | ||
r#"describe.concurrent("something", () => { it("something", ({ expect }) => { expect(1).toMatchSnapshot() }) })"#, | ||
None, | ||
), | ||
( | ||
r#"describe.concurrent("something", () => { it("something", ({ expect }) => { expect(1).toMatchInlineSnapshot() }) })"#, | ||
None, | ||
), | ||
( | ||
r#"describe.concurrent("something", () => { it("something", () => { expect(true).toBe(true) }) })"#, | ||
None, | ||
), | ||
( | ||
r#"describe("something", () => { it("something", (context) => { context.expect(1).toMatchInlineSnapshot() }) })"#, | ||
None, | ||
), | ||
( | ||
r#"describe("something", () => { it("something", (context) => { expect(1).toMatchInlineSnapshot() }) })"#, | ||
None, | ||
), | ||
( | ||
r#"it.concurrent("something", (context) => { context.expect(1).toMatchSnapshot() })"#, | ||
None, | ||
), | ||
]; | ||
|
||
let fail = vec![ | ||
(r#"it.concurrent("should fail", () => { expect(true).toMatchSnapshot() })"#, None), | ||
( | ||
r#"it.concurrent("should fail", () => { expect(true).toMatchInlineSnapshot("true") })"#, | ||
None, | ||
), | ||
( | ||
r#" | ||
import { describe, expect, it } from 'vitest'; | ||
describe.concurrent("failing", () => { it("should fail", () => { expect(true).toMatchSnapshot() }) }) | ||
"#, | ||
None, | ||
), | ||
( | ||
r#" | ||
import { describe, expect, it } from 'vitest'; | ||
describe.concurrent("failing", () => { it("should fail", () => { expect(true).toMatchInlineSnapshot("true") }) }) | ||
"#, | ||
None, | ||
), | ||
(r#"it.concurrent("something", (context) => { expect(true).toMatchSnapshot() })"#, None), | ||
( | ||
r#" | ||
it.concurrent("something", () => { | ||
expect(true).toMatchSnapshot(); | ||
expect(true).toMatchSnapshot(); | ||
}) | ||
"#, | ||
None, | ||
), | ||
( | ||
r#" | ||
it.concurrent("something", () => { | ||
expect(true).toBe(true); | ||
expect(true).toMatchSnapshot(); | ||
}) | ||
"#, | ||
None, | ||
), | ||
( | ||
r#"it.concurrent("should fail", () => { expect(true).toMatchFileSnapshot("./test/basic.output.html") })"#, | ||
None, | ||
), | ||
( | ||
r#"it.concurrent("should fail", () => { expect(foo()).toThrowErrorMatchingSnapshot() })"#, | ||
None, | ||
), | ||
( | ||
r#"it.concurrent("should fail", () => { expect(foo()).toThrowErrorMatchingInlineSnapshot("bar") })"#, | ||
None, | ||
), | ||
]; | ||
|
||
Tester::new(RequireLocalTestContextForConcurrentSnapshots::NAME, pass, fail) | ||
.with_vitest_plugin(true) | ||
.test_and_snapshot(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we don't need to collect function parameters? We can iterate all
expect
call expression and combine youcheck
methodhttps://github.com/veritem/eslint-plugin-vitest/blob/8b11357b8690c276b869a04140f5238679bab5de/src/rules/require-local-test-context-for-concurrent-snapshots.ts#L33