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

chore(linter); add regression case for require-yield #2326

Merged
Merged
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
49 changes: 23 additions & 26 deletions crates/oxc_linter/src/rules/eslint/require_yield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use oxc_diagnostics::{
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_span::Span;

use crate::{context::LintContext, rule::Rule, AstNode};

Expand Down Expand Up @@ -37,16 +37,12 @@ declare_oxc_lint!(

impl Rule for RequireYield {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let kind = node.kind();

if node.flags().has_yield() {
return;
}

if let AstKind::Function(func) = kind {
if func.generator && func.body.as_ref().is_some_and(|body| !body.statements.is_empty())
if let AstKind::Function(func) = node.kind() {
if !node.flags().has_yield()
&& func.generator
&& func.body.as_ref().is_some_and(|body| !body.statements.is_empty())
{
let span = func.id.as_ref().map_or_else(|| kind.span(), |ident| ident.span);
let span = func.id.as_ref().map_or_else(|| func.span, |ident| ident.span);
ctx.diagnostic(RequireYieldDiagnostic(span));
}
}
Expand All @@ -58,25 +54,26 @@ fn test() {
use crate::tester::Tester;

let pass = vec![
("function foo() { return 0; }", None),
("function* foo() { yield 0; }", None),
("function* foo() { }", None),
("(function* foo() { yield 0; })();", None),
("(function* foo() { })();", None),
("var obj = { *foo() { yield 0; } };", None),
("var obj = { *foo() { } };", None),
("class A { *foo() { yield 0; } };", None),
("class A { *foo() { } };", None),
("() => {}", None),
"function foo() { return 0; }",
"function* foo() { yield 0; }",
"function* foo() { }",
"(function* foo() { yield 0; })();",
"(function* foo() { })();",
"function* foo() { while (true) { yield 0; } }",
"var obj = { *foo() { yield 0; } };",
"var obj = { *foo() { } };",
"class A { *foo() { yield 0; } };",
"class A { *foo() { } };",
"() => {}",
];

let fail = vec![
("function* foo() { return 0; }", None),
("(function* foo() { return 0; })();", None),
("var obj = { *foo() { return 0; } }", None),
("class A { *foo() { return 0; } }", None),
("function* foo() { function* bar() { yield 0; } }", None),
("function* foo() { function* bar() { return 0; } yield 0; }", None),
"function* foo() { return 0; }",
"(function* foo() { return 0; })();",
"var obj = { *foo() { return 0; } }",
"class A { *foo() { return 0; } }",
"function* foo() { function* bar() { yield 0; } }",
"function* foo() { function* bar() { return 0; } yield 0; }",
];

Tester::new(RequireYield::NAME, pass, fail).test_and_snapshot();
Expand Down