Skip to content

Commit

Permalink
fix(linter): memorize visited block id in `neighbors_filtered_by_edge…
Browse files Browse the repository at this point in the history
…_weight`
  • Loading branch information
mysteryven committed May 25, 2024
1 parent ecdffcf commit f068f7a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
42 changes: 35 additions & 7 deletions crates/oxc_linter/src/rules/react/require_render_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,18 @@ fn contains_return_statement<'a>(node: &AstNode<'a>, ctx: &LintContext<'a>) -> b
}

for entry in cfg.basic_block(*basic_block_id) {
if let BasicBlockElement::Assignment(to_reg, val) = entry {
if matches!(to_reg, Register::Return)
&& matches!(val, AssignmentValue::NotImplicitUndefined)
{
return (FoundReturn::Yes, STOP_WALKING_ON_THIS_PATH);
match entry {
BasicBlockElement::Assignment(to_reg, val) => {
if matches!(to_reg, Register::Return)
&& matches!(val, AssignmentValue::NotImplicitUndefined)
{
return (FoundReturn::Yes, STOP_WALKING_ON_THIS_PATH);
}
}
} else {
// We don't care about other types of instructions.
BasicBlockElement::Unreachable | BasicBlockElement::Throw(_) => {
return (FoundReturn::No, STOP_WALKING_ON_THIS_PATH);
}
BasicBlockElement::Break(_) => {}
}
}

Expand Down Expand Up @@ -186,7 +190,31 @@ fn is_in_es6_component<'a, 'b>(node: &'b AstNode<'a>, ctx: &'b LintContext<'a>)
fn test() {
use crate::tester::Tester;

// let too_many_if_else = (1..10)
// .map(|i| {
// "
// if (a > i) {
// foo1()
// } else {
// foo2()
// }
// "
// })
// .collect::<String>();

// let too_many_if_else_case = format!(
// "
// class Hello extends React.Component {{
// render() {{
// {too_many_if_else}
// return 'div'
// }}
// }}
// ",
// );

let pass = vec![
// &too_many_if_else_case,
r"
class Hello extends React.Component {
render() {
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,9 +1078,9 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
/* cfg - bb after if statement joins consequent and alternate */
let after_if_graph_ix = self.cfg.new_basic_block();

if stmt.alternate.is_some() {
self.cfg.put_unreachable();
}
// if stmt.alternate.is_some() {
// self.cfg.put_unreachable();
// }
// else {
self.cfg.add_edge(after_consequent_stmt_graph_ix, after_if_graph_ix, EdgeType::Normal);
// }
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_semantic/src/pg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use petgraph::{visit::EdgeRef, Direction, Graph};
use rustc_hash::FxHashSet;

use crate::BasicBlockId;

Expand All @@ -15,6 +16,7 @@ where
{
let mut q = vec![];
let mut final_states = vec![];
let mut visited = FxHashSet::default();

// for initial node
let (new_state, keep_walking_this_path) = visitor(&node, Default::default());
Expand All @@ -27,6 +29,10 @@ where

while let Some((graph_ix, state)) = q.pop() {
let mut edges = 0;
if visited.contains(&graph_ix) {
continue;
}
visited.insert(graph_ix);
for edge in graph.edges_directed(graph_ix, Direction::Outgoing) {
if let Some(result_of_edge_filtering) = edge_filter(edge.weight()) {
final_states.push(result_of_edge_filtering);
Expand Down

0 comments on commit f068f7a

Please sign in to comment.