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

linter: panic in jest/expect-expect #3323

Closed
Boshen opened this issue May 17, 2024 · 4 comments · Fixed by #3324
Closed

linter: panic in jest/expect-expect #3323

Boshen opened this issue May 17, 2024 · 4 comments · Fixed by #3324
Assignees
Labels
A-linter Area - Linter C-bug Category - Bug

Comments

@Boshen
Copy link
Member

Boshen commented May 17, 2024

Found in https://github.com/oxc-project/oxlint-ecosystem-ci/actions/runs/9123766396/job/25086817261?pr=6

Reduced case:

test("event emitters bound to CLS context", function(t) {
    t.test("emitter with newListener that removes handler", function(t) {
        ee.on("newListener", function handler(event: any) {
            this.removeListener("newListener", handler);
        });
    });
});

Infinite recursion in

let Some(node) = get_declaration_of_variable(ident, ctx) else {
return false;
};
let AstKind::Function(function) = node.kind() else {
return false;
};
let Some(body) = &function.body else {
return false;
};
return check_statements(&body.statements, assert_function_names, ctx);

because it went back inside handler

@Boshen Boshen added C-bug Category - Bug A-linter Area - Linter labels May 17, 2024
@Boshen
Copy link
Member Author

Boshen commented May 17, 2024

Why does it need to check for the arguments when matches_assert_function_name is false?

if matches_assert_function_name(&name, assert_function_names) {
return true;
}
let has_assert_function = check_arguments(call_expr, assert_function_names, ctx);

Edit: it failed on this case

it('should pass', () => somePromise().then(() => expect(true).toBeDefined()))

@mysteryven
Copy link
Member

mysteryven commented May 17, 2024

I found the current implementation was not correct. It finds assert function from test funtion to bottom, There will be so many cases I would forget(e.g. if (condition){ expect()}), I plan to follow the jest implementation, it needs to loop all nodes... any better ideas?

fn run_once(&self, ctx: &LintContext) {
        let test_functions = HashSet::new();

        for node in ctx.nodes() {
            if is_test_function(node) {
                test_functions.insert(node.span());
            } else if is_expect_call(node) {
                // try get nearest test function, if got, remove it from test_functions
            }
        }
    }

@Boshen
Copy link
Member Author

Boshen commented May 17, 2024

Can we do a temporary fix first, and then figure out how to best write this?

mysteryven added a commit that referenced this issue May 17, 2024
mysteryven added a commit that referenced this issue May 17, 2024
Boshen pushed a commit that referenced this issue May 17, 2024
fix: #3324

I just replace `name` with a `set`, feel like this can catch more cases. And this rule needs to be [rewritten](#3323 (comment)).
@mysteryven
Copy link
Member

Panic is resolved, and the below belongs to another issue.

I found the current implementation was not correct. It finds assert function from test funtion to bottom, There will be so many cases I would forget(e.g. if (condition){ expect()}), I plan to follow the jest implementation, it needs to loop all nodes... any better ideas?

fn run_once(&self, ctx: &LintContext) {
        let test_functions = HashSet::new();

        for node in ctx.nodes() {
            if is_test_function(node) {
                test_functions.insert(node.span());
            } else if is_expect_call(node) {
                // try get nearest test function, if got, remove it from test_functions
            }
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linter Area - Linter C-bug Category - Bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants