This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 653
feat(rome_js_analyze): useYield
rule
#4037
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9d48bb3
feat(rome_js_analyze): `useYield` rule
kaioduarte 5dc9fa2
fix: allow empty generator
kaioduarte b2137c6
fix: codegen
kaioduarte cca7cb4
chore: simplify code to check only yield expressions
kaioduarte e558195
chore: PR feedback
kaioduarte 6f65354
fix: codegen
kaioduarte 307d57b
fix: clippy
kaioduarte 4e1f07d
refactor: use custom visitor
kaioduarte 48a9bb4
chore: PR feedback
kaioduarte 10b2a34
chore: flip conditions
kaioduarte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
180 changes: 180 additions & 0 deletions
180
crates/rome_js_analyze/src/analyzers/nursery/use_yield.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
use rome_analyze::context::RuleContext; | ||
use rome_analyze::{ | ||
declare_rule, AddVisitor, Phases, QueryMatch, Queryable, Rule, RuleDiagnostic, ServiceBag, | ||
Visitor, VisitorContext, | ||
}; | ||
use rome_console::markup; | ||
use rome_js_syntax::{ | ||
AnyJsFunction, JsLanguage, JsMethodClassMember, JsMethodObjectMember, JsStatementList, | ||
JsYieldExpression, TextRange, WalkEvent, | ||
}; | ||
use rome_rowan::{declare_node_union, AstNode, AstNodeList, Language, SyntaxNode}; | ||
|
||
declare_rule! { | ||
/// Require generator functions to contain `yield`. | ||
/// | ||
/// This rule generates warnings for generator functions that do not have the `yield` keyword. | ||
/// | ||
/// Source: [require-await](https://eslint.org/docs/latest/rules/require-yield). | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// function* foo() { | ||
/// return 10; | ||
/// } | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// ```js | ||
/// function* foo() { | ||
/// yield 5; | ||
/// return 10; | ||
/// } | ||
/// | ||
/// function foo() { | ||
/// return 10; | ||
/// } | ||
/// | ||
/// // This rule does not warn on empty generator functions. | ||
/// function* foo() { } | ||
/// ``` | ||
pub(crate) UseYield { | ||
version: "next", | ||
name: "useYield", | ||
recommended: true, | ||
} | ||
} | ||
|
||
declare_node_union! { | ||
pub(crate) AnyFunctionLike = AnyJsFunction | JsMethodObjectMember | JsMethodClassMember | ||
} | ||
|
||
impl AnyFunctionLike { | ||
fn is_generator(&self) -> bool { | ||
match self { | ||
AnyFunctionLike::AnyJsFunction(any_js_function) => any_js_function.is_generator(), | ||
AnyFunctionLike::JsMethodClassMember(method_class_member) => { | ||
method_class_member.star_token().is_some() | ||
} | ||
AnyFunctionLike::JsMethodObjectMember(method_obj_member) => { | ||
method_obj_member.star_token().is_some() | ||
} | ||
} | ||
} | ||
|
||
fn statements(&self) -> Option<JsStatementList> { | ||
Some(match self { | ||
AnyFunctionLike::AnyJsFunction(any_js_function) => any_js_function | ||
.body() | ||
.ok()? | ||
.as_js_function_body()? | ||
.statements(), | ||
AnyFunctionLike::JsMethodClassMember(method_class_member) => { | ||
method_class_member.body().ok()?.statements() | ||
} | ||
AnyFunctionLike::JsMethodObjectMember(method_obj_member) => { | ||
method_obj_member.body().ok()?.statements() | ||
} | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
struct MissingYieldVisitor { | ||
/// Vector to hold a function node and a boolean indicating whether the function | ||
/// contains an `yield` expression or not. | ||
stack: Vec<(AnyFunctionLike, bool)>, | ||
} | ||
|
||
impl Visitor for MissingYieldVisitor { | ||
type Language = JsLanguage; | ||
|
||
fn visit( | ||
&mut self, | ||
event: &WalkEvent<SyntaxNode<Self::Language>>, | ||
mut ctx: VisitorContext<Self::Language>, | ||
) { | ||
match event { | ||
WalkEvent::Enter(node) => { | ||
// When the visitor enters a function node, push a new entry on the stack | ||
if let Some(node) = AnyFunctionLike::cast_ref(node) { | ||
self.stack.push((node, false)); | ||
} | ||
|
||
if JsYieldExpression::can_cast(node.kind()) { | ||
// When the visitor enters a `yield` expression, set the | ||
// `has_yield` flag for the top entry on the stack to `true` | ||
if let Some((_, has_yield)) = self.stack.last_mut() { | ||
*has_yield = true; | ||
} | ||
} | ||
} | ||
WalkEvent::Leave(node) => { | ||
// When the visitor exits a function, if it matches the node of the top-most | ||
// entry of the stack and the `has_yield` flag is `false`, emit a query match | ||
if let Some(exit_node) = AnyFunctionLike::cast_ref(node) { | ||
if let Some((enter_node, has_yield)) = self.stack.pop() { | ||
if enter_node == exit_node && !has_yield { | ||
ctx.match_query(MissingYield(enter_node)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub(crate) struct MissingYield(AnyFunctionLike); | ||
|
||
impl QueryMatch for MissingYield { | ||
fn text_range(&self) -> TextRange { | ||
self.0.range() | ||
} | ||
} | ||
|
||
impl Queryable for MissingYield { | ||
type Input = Self; | ||
type Language = JsLanguage; | ||
type Output = AnyFunctionLike; | ||
type Services = (); | ||
|
||
fn build_visitor( | ||
analyzer: &mut impl AddVisitor<Self::Language>, | ||
_: &<Self::Language as Language>::Root, | ||
) { | ||
analyzer.add_visitor(Phases::Syntax, MissingYieldVisitor::default); | ||
} | ||
|
||
fn unwrap_match(_: &ServiceBag, query: &Self::Input) -> Self::Output { | ||
query.0.clone() | ||
} | ||
} | ||
|
||
impl Rule for UseYield { | ||
type Query = MissingYield; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let query = ctx.query(); | ||
|
||
// Don't emit diagnostic for non-generators or generators with an empty body | ||
if !query.is_generator() || query.statements()?.is_empty() { | ||
return None; | ||
} | ||
|
||
Some(()) | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> { | ||
Some(RuleDiagnostic::new( | ||
rule_category!(), | ||
ctx.query().range(), | ||
markup! {"This generator function doesn't contain "<Emphasis>"yield"</Emphasis>"."}, | ||
)) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Valid | ||
function foo() { | ||
return 0; | ||
} | ||
|
||
function* foo() { | ||
yield 0; | ||
} | ||
|
||
function* foo() {} | ||
|
||
(function* foo() { | ||
yield 0; | ||
})(); | ||
|
||
(function* foo() {})(); | ||
|
||
const obj = { | ||
*foo() { | ||
yield 0; | ||
}, | ||
}; | ||
|
||
const obj = { *foo() {} }; | ||
|
||
class A { | ||
*foo() { | ||
yield 0; | ||
} | ||
} | ||
|
||
class A { | ||
*foo() {} | ||
} | ||
|
||
// Invalid | ||
function* foo() { | ||
return 0; | ||
} | ||
|
||
(function* foo() { | ||
return 0; | ||
})(); | ||
|
||
const obj = { | ||
*foo() { | ||
return 0; | ||
}, | ||
}; | ||
|
||
class A { | ||
*foo() { | ||
return 0; | ||
} | ||
} | ||
|
||
function* foo() { | ||
function* bar() { | ||
yield 0; | ||
} | ||
} | ||
|
||
function* foo() { | ||
function* bar() { | ||
return 0; | ||
} | ||
yield 0; | ||
} | ||
|
||
function* foo() { | ||
function* bar() { | ||
yield 0; | ||
} | ||
return 0; | ||
} | ||
|
||
function* foo() { | ||
const obj = { | ||
*bar() { | ||
return 0; | ||
}, | ||
}; | ||
|
||
class A { | ||
*baz() { | ||
return 0; | ||
} | ||
} | ||
|
||
if (a === 'a') { | ||
yield 0; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.