@@ -3,7 +3,8 @@ use oxc_diagnostics::OxcDiagnostic;
3
3
use oxc_macros:: declare_oxc_lint;
4
4
use oxc_semantic:: {
5
5
petgraph:: { self , graph:: NodeIndex } ,
6
- AstNode ,
6
+ pg:: neighbors_filtered_by_edge_weight,
7
+ AstNode , EdgeType ,
7
8
} ;
8
9
use oxc_span:: { GetSpan , Span } ;
9
10
@@ -51,13 +52,34 @@ impl Rule for NoUnreachable {
51
52
}
52
53
}
53
54
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
+ } ,
60
80
)
81
+ . iter ( )
82
+ . any ( |it| it. is_some_and ( |val| !val) )
61
83
}
62
84
63
85
#[ test]
@@ -103,6 +125,7 @@ fn test() {
103
125
"function foo() { return x; var x = 1; }" ,
104
126
//[{ messageId: "unreachableCode", type: "VariableDeclaration" }]
105
127
"function foo() { return x; var x, y = 1; }" ,
128
+ "while (true) { break; var x = 1; }" ,
106
129
//[{ messageId: "unreachableCode", type: "VariableDeclaration" }]
107
130
"while (true) { continue; var x = 1; }" ,
108
131
//[{ messageId: "unreachableCode", type: "ExpressionStatement" }]
0 commit comments