Skip to content

Commit c232751

Browse files
committed
fix(linter): memorize visited block id in neighbors_filtered_by_edge_weight
1 parent de75fb2 commit c232751

11 files changed

+55
-52
lines changed

crates/oxc_linter/src/rules/react/require_render_return.rs

+35-7
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,18 @@ fn contains_return_statement<'a>(node: &AstNode<'a>, ctx: &LintContext<'a>) -> b
110110
}
111111

112112
for entry in cfg.basic_block(*basic_block_id) {
113-
if let BasicBlockElement::Assignment(to_reg, val) = entry {
114-
if matches!(to_reg, Register::Return)
115-
&& matches!(val, AssignmentValue::NotImplicitUndefined)
116-
{
117-
return (FoundReturn::Yes, STOP_WALKING_ON_THIS_PATH);
113+
match entry {
114+
BasicBlockElement::Assignment(to_reg, val) => {
115+
if matches!(to_reg, Register::Return)
116+
&& matches!(val, AssignmentValue::NotImplicitUndefined)
117+
{
118+
return (FoundReturn::Yes, STOP_WALKING_ON_THIS_PATH);
119+
}
118120
}
119-
} else {
120-
// We don't care about other types of instructions.
121+
BasicBlockElement::Unreachable | BasicBlockElement::Throw(_) => {
122+
return (FoundReturn::No, STOP_WALKING_ON_THIS_PATH);
123+
}
124+
BasicBlockElement::Break(_) => {}
121125
}
122126
}
123127

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

193+
// let too_many_if_else = (1..10)
194+
// .map(|i| {
195+
// "
196+
// if (a > i) {
197+
// foo1()
198+
// } else {
199+
// foo2()
200+
// }
201+
// "
202+
// })
203+
// .collect::<String>();
204+
205+
// let too_many_if_else_case = format!(
206+
// "
207+
// class Hello extends React.Component {{
208+
// render() {{
209+
// {too_many_if_else}
210+
// return 'div'
211+
// }}
212+
// }}
213+
// ",
214+
// );
215+
189216
let pass = vec![
217+
// &too_many_if_else_case,
190218
r"
191219
class Hello extends React.Component {
192220
render() {

crates/oxc_semantic/src/builder.rs

-6
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,6 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
801801
let after_conditional_graph_ix = self.cfg.new_basic_block();
802802
/* cfg */
803803

804-
self.cfg.put_unreachable();
805804
self.cfg.add_edge(
806805
after_consequent_expr_graph_ix,
807806
after_conditional_graph_ix,
@@ -1078,12 +1077,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
10781077
/* cfg - bb after if statement joins consequent and alternate */
10791078
let after_if_graph_ix = self.cfg.new_basic_block();
10801079

1081-
if stmt.alternate.is_some() {
1082-
self.cfg.put_unreachable();
1083-
}
1084-
// else {
10851080
self.cfg.add_edge(after_consequent_stmt_graph_ix, after_if_graph_ix, EdgeType::Normal);
1086-
// }
10871081

10881082
self.cfg.add_edge(
10891083
before_if_stmt_graph_ix,

crates/oxc_semantic/src/pg.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use petgraph::{visit::EdgeRef, Direction, Graph};
2+
use rustc_hash::FxHashSet;
23

34
use crate::BasicBlockId;
45

@@ -15,6 +16,7 @@ where
1516
{
1617
let mut q = vec![];
1718
let mut final_states = vec![];
19+
let mut visited = FxHashSet::default();
1820

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

2830
while let Some((graph_ix, state)) = q.pop() {
2931
let mut edges = 0;
32+
if visited.contains(&graph_ix) {
33+
continue;
34+
}
35+
visited.insert(graph_ix);
3036
for edge in graph.edges_directed(graph_ix, Direction::Outgoing) {
3137
if let Some(result_of_edge_filtering) = edge_filter(edge.weight()) {
3238
final_states.push(result_of_edge_filtering);

crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@cond_expr_in_arrow_fn.js-2.snap

-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ digraph {
99
2 [ label = ""]
1010
3 [ label = ""]
1111
4 [ label = ""]
12-
5 [ label = "Unreachable()"]
1312
0 -> 1 [ ]
14-
4 -> 5 [ ]
1513
2 -> 4 [ ]
1614
1 -> 2 [ ]
1715
1 -> 3 [ ]

crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@cond_expr_in_arrow_fn.js.snap

-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,3 @@ bb3: {
2222
bb4: {
2323

2424
}
25-
26-
bb5: {
27-
Unreachable()
28-
}

crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@conditional_expression.js-2.snap

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ digraph {
88
1 [ label = ""]
99
2 [ label = ""]
1010
3 [ label = ""]
11-
4 [ label = "Unreachable()"]
12-
3 -> 4 [ ]
1311
1 -> 3 [ ]
1412
0 -> 1 [ ]
1513
0 -> 2 [ ]

crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@conditional_expression.js.snap

-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,3 @@ bb2: {
1818
bb3: {
1919

2020
}
21-
22-
bb4: {
23-
Unreachable()
24-
}

crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@if_else.js-2.snap

+6-8
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@ digraph {
1414
7 [ label = "$return = <value>"]
1515
8 [ label = ""]
1616
9 [ label = "Unreachable()"]
17-
10 [ label = ""]
18-
11 [ label = "Unreachable()\n$return = <value>"]
19-
12 [ label = ""]
20-
13 [ label = "Unreachable()"]
21-
14 [ label = ""]
17+
10 [ label = "$return = <value>"]
18+
11 [ label = ""]
19+
12 [ label = "Unreachable()"]
20+
13 [ label = ""]
2221
0 -> 1 [ ]
2322
1 -> 2 [ ]
2423
1 -> 3 [ ]
2524
2 -> 3 [ ]
2625
5 -> 6 [ ]
2726
8 -> 9 [ ]
28-
10 -> 11 [ ]
2927
6 -> 10 [ ]
3028
3 -> 4 [ ]
3129
3 -> 7 [ ]
3230
9 -> 10 [ ]
33-
12 -> 13 [ ]
34-
0 -> 14 [ ]
31+
11 -> 12 [ ]
32+
0 -> 13 [ ]
3533
}

crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@if_else.js.snap

+3-8
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,17 @@ bb9: {
4444
}
4545

4646
bb10: {
47-
48-
}
49-
50-
bb11: {
51-
Unreachable()
5247
$return = <value>
5348
}
5449

55-
bb12: {
50+
bb11: {
5651

5752
}
5853

59-
bb13: {
54+
bb12: {
6055
Unreachable()
6156
}
6257

63-
bb14: {
58+
bb13: {
6459

6560
}

crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@if_stmt_in_for_in.js-2.snap

+4-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ digraph {
1717
10 [ label = "Unreachable()"]
1818
11 [ label = ""]
1919
12 [ label = ""]
20-
13 [ label = "Unreachable()"]
20+
13 [ label = ""]
2121
14 [ label = ""]
22-
15 [ label = ""]
2322
0 -> 1 [ ]
2423
5 -> 6 [ ]
2524
5 -> 6 [ ]
@@ -29,16 +28,15 @@ digraph {
2928
10 -> 11 [ ]
3029
7 -> 8 [ ]
3130
7 -> 11 [ ]
32-
12 -> 13 [ ]
3331
6 -> 12 [ ]
3432
4 -> 5 [ ]
3533
4 -> 7 [ ]
3634
11 -> 12 [ ]
3735
1 -> 2 [ ]
3836
2 -> 3 [ ]
3937
3 -> 4 [ ]
40-
13 -> 3 [ ]
41-
3 -> 14 [ ]
38+
12 -> 3 [ ]
39+
3 -> 13 [ ]
4240
9 -> 3 [ ]
43-
0 -> 15 [ ]
41+
0 -> 14 [ ]
4442
}

crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@if_stmt_in_for_in.js.snap

+1-5
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,9 @@ bb12: {
5757
}
5858

5959
bb13: {
60-
Unreachable()
61-
}
62-
63-
bb14: {
6460

6561
}
6662

67-
bb15: {
63+
bb14: {
6864

6965
}

0 commit comments

Comments
 (0)