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

fix(linter): false positive with require-yield where yield is anywhere but function scope #2324

Closed
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
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/eslint/require_yield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
17 changes: 15 additions & 2 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AstNodeId> {
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.
Expand Down