Skip to content

Commit 842eccc

Browse files
committed
feat: add more in-depth check to see performance difference.
1 parent 1818303 commit 842eccc

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

crates/oxc_linter/src/rules/eslint/no_unreachable.rs

+30-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use oxc_diagnostics::OxcDiagnostic;
33
use oxc_macros::declare_oxc_lint;
44
use oxc_semantic::{
55
petgraph::{self, graph::NodeIndex},
6-
AstNode,
6+
pg::neighbors_filtered_by_edge_weight,
7+
AstNode, EdgeType,
78
};
89
use oxc_span::{GetSpan, Span};
910

@@ -51,13 +52,34 @@ impl Rule for NoUnreachable {
5152
}
5253
}
5354

54-
fn is_unreachable(ctx: &LintContext, node_cfg_ix: NodeIndex, parent_cfg_ix: NodeIndex) -> bool {
55-
!petgraph::algo::has_path_connecting(
56-
&ctx.semantic().cfg().graph,
57-
parent_cfg_ix,
58-
node_cfg_ix,
59-
None,
55+
fn is_unreachable(ctx: &LintContext, target_ix: NodeIndex, from_ix: NodeIndex) -> bool {
56+
let msg = format!("is_unreachable {target_ix:?} from: {from_ix:?}");
57+
dbg!(msg);
58+
let cfg = ctx.semantic().cfg();
59+
if !petgraph::algo::has_path_connecting(&cfg.graph, from_ix, target_ix, None) {
60+
return true;
61+
}
62+
63+
neighbors_filtered_by_edge_weight(
64+
&cfg.graph,
65+
from_ix,
66+
&|e| match e {
67+
EdgeType::Normal => None,
68+
EdgeType::Backedge | EdgeType::NewFunction => Some(None),
69+
},
70+
&mut |ix: &NodeIndex, _| {
71+
if target_ix == *ix {
72+
return (Some(true), false);
73+
}
74+
75+
let connected = !petgraph::algo::has_path_connecting(&cfg.graph, *ix, target_ix, None);
76+
dbg!(ix, target_ix, connected);
77+
78+
(None, connected)
79+
},
6080
)
81+
.iter()
82+
.any(|it| it.is_some_and(|val| !val))
6183
}
6284

6385
#[test]
@@ -103,6 +125,7 @@ fn test() {
103125
"function foo() { return x; var x = 1; }",
104126
//[{ messageId: "unreachableCode", type: "VariableDeclaration" }]
105127
"function foo() { return x; var x, y = 1; }",
128+
"while (true) { break; var x = 1; }",
106129
//[{ messageId: "unreachableCode", type: "VariableDeclaration" }]
107130
"while (true) { continue; var x = 1; }",
108131
//[{ messageId: "unreachableCode", type: "ExpressionStatement" }]

0 commit comments

Comments
 (0)