Skip to content
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): add new property for parse_jest_fn #2715

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jest/no_disabled_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>)
let node = possible_jest_node.node;
if let AstKind::CallExpression(call_expr) = node.kind() {
if let Some(jest_fn_call) = parse_general_jest_fn_call(call_expr, possible_jest_node, ctx) {
let ParsedGeneralJestFnCall { kind, members, name } = jest_fn_call;
let ParsedGeneralJestFnCall { kind, members, name, .. } = jest_fn_call;
// `test('foo')`
let kind = match kind {
JestFnKind::Expect | JestFnKind::Unknown => return,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jest/no_focused_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>)
let Some(jest_fn_call) = parse_general_jest_fn_call(call_expr, possible_jest_node, ctx) else {
return;
};
let ParsedGeneralJestFnCall { kind, members, name } = jest_fn_call;
let ParsedGeneralJestFnCall { kind, members, name, .. } = jest_fn_call;
if !matches!(kind, JestFnKind::General(JestGeneralFnKind::Describe | JestGeneralFnKind::Test)) {
return;
}
Expand Down
21 changes: 18 additions & 3 deletions crates/oxc_linter/src/utils/jest/parse_jest_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ pub fn parse_jest_fn_call<'a>(
}

if matches!(kind, JestFnKind::Expect) {
let options = ExpectFnCallOptions { call_expr, members, name, head, node, ctx };
let options = ExpectFnCallOptions {
call_expr,
members,
name,
local: resolved.local,
head,
node,
ctx,
};
return parse_jest_expect_fn_call(options);
}

Expand All @@ -80,7 +88,7 @@ pub fn parse_jest_fn_call<'a>(
}

if matches!(kind, JestFnKind::General(JestGeneralFnKind::Jest)) {
return parse_jest_jest_fn_call(members, name);
return parse_jest_jest_fn_call(members, name, resolved.local);
}

// Check every link in the chain except the last is a member expression
Expand All @@ -98,6 +106,7 @@ pub fn parse_jest_fn_call<'a>(
kind,
members,
name: Cow::Borrowed(name),
local: Cow::Borrowed(resolved.local),
}));
}

Expand All @@ -107,7 +116,7 @@ pub fn parse_jest_fn_call<'a>(
fn parse_jest_expect_fn_call<'a>(
options: ExpectFnCallOptions<'a, '_>,
) -> Option<ParsedJestFnCall<'a>> {
let ExpectFnCallOptions { call_expr, members, name, head, node, ctx } = options;
let ExpectFnCallOptions { call_expr, members, name, local, head, node, ctx } = options;
let (modifiers, matcher, mut expect_error) = match find_modifiers_and_matcher(&members) {
Ok((modifier, matcher)) => (modifier, matcher, None),
Err(e) => (vec![], None, Some(e)),
Expand All @@ -131,6 +140,7 @@ fn parse_jest_expect_fn_call<'a>(
head,
members,
name: Cow::Borrowed(name),
local: Cow::Borrowed(local),
args: &call_expr.arguments,
matcher_index: matcher,
modifier_indices: modifiers,
Expand Down Expand Up @@ -222,6 +232,7 @@ fn is_top_most_call_expr<'a, 'b>(node: &'b AstNode<'a>, ctx: &'b LintContext<'a>
fn parse_jest_jest_fn_call<'a>(
members: Vec<KnownMemberExpressionProperty<'a>>,
name: &'a str,
local: &'a str,
) -> Option<ParsedJestFnCall<'a>> {
if !name.to_ascii_lowercase().eq_ignore_ascii_case("jest") {
return None;
Expand All @@ -231,6 +242,7 @@ fn parse_jest_jest_fn_call<'a>(
kind: JestFnKind::General(JestGeneralFnKind::Jest),
members,
name: Cow::Borrowed(name),
local: Cow::Borrowed(local),
}));
}

Expand All @@ -245,6 +257,7 @@ pub struct ExpectFnCallOptions<'a, 'b> {
pub call_expr: &'a CallExpression<'a>,
pub members: Vec<KnownMemberExpressionProperty<'a>>,
pub name: &'a str,
pub local: &'a Atom<'a>,
pub head: KnownMemberExpressionProperty<'a>,
pub node: &'b AstNode<'a>,
pub ctx: &'b LintContext<'a>,
Expand Down Expand Up @@ -305,12 +318,14 @@ pub struct ParsedGeneralJestFnCall<'a> {
pub kind: JestFnKind,
pub members: Vec<KnownMemberExpressionProperty<'a>>,
pub name: Cow<'a, str>,
pub local: Cow<'a, str>,
}

pub struct ParsedExpectFnCall<'a> {
pub kind: JestFnKind,
pub members: Vec<KnownMemberExpressionProperty<'a>>,
pub name: Cow<'a, str>,
pub local: Cow<'a, str>,
pub head: KnownMemberExpressionProperty<'a>,
pub args: &'a oxc_allocator::Vec<'a, Argument<'a>>,
// In `expect(1).not.resolved.toBe()`, "not", "resolved" will be modifier
Expand Down
Loading