diff --git a/crates/oxc_linter/src/rules/eslint/require_yield.rs b/crates/oxc_linter/src/rules/eslint/require_yield.rs index de67d957df801..7e6c33f94cfe7 100644 --- a/crates/oxc_linter/src/rules/eslint/require_yield.rs +++ b/crates/oxc_linter/src/rules/eslint/require_yield.rs @@ -60,6 +60,7 @@ fn test() { let pass = vec![ ("function foo() { return 0; }", None), ("function* foo() { yield 0; }", None), + ("function* foo() { while (true) { yield 0; } }", None), ("function* foo() { }", None), ("(function* foo() { yield 0; })();", None), ("(function* foo() { })();", None), diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index a3d59af880ef7..032cd261b1a21 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -226,10 +226,23 @@ impl<'a> SemanticBuilder<'a> { } pub fn set_function_node_flag(&mut self, flag: NodeFlags) { + if let Some(node_id) = self.get_function_node_id() { + *self.nodes.get_node_mut(node_id).flags_mut() |= flag; + } + } + + fn get_function_node_id(&self) -> Option { if self.current_scope_flags().is_function() { - *self.nodes.get_node_mut(self.scope.get_node_id(self.current_scope_id)).flags_mut() |= - flag; + return Some(self.scope.get_node_id(self.current_scope_id)); } + + for scope_id in self.scope.ancestors(self.current_scope_id) { + if self.scope.get_flags(scope_id).is_function() { + return Some(self.scope.get_node_id(scope_id)); + } + } + + None } /// Declares a `Symbol` for the node, adds it to symbol table, and binds it to the scope.