11use crate :: {
22 ps:: {
3- comparison:: infer_comparison,
43 LoopStatus :: { Dead , Inifite , OneTurn } ,
54 Powershell :: { self , Loop , Raw } ,
65 Value :: { self , Bool } ,
@@ -33,15 +32,19 @@ impl IteratorVariable {
3332/// use minusone::ps::build_powershell_tree;
3433/// use minusone::ps::linter::Linter;
3534/// use minusone::ps::forward::Forward;
35+ /// use minusone::ps::bool::Comparison;
3636/// use minusone::ps::integer::ParseInt;
3737/// use minusone::ps::integer::AddInt;
38+ /// use minusone::ps::var::Var;
3839/// use minusone::ps::loops::ForStatementCondition;
3940///
4041/// let mut tree = build_powershell_tree("for ($i = 132 + 324 - 3; $i -lt 200 - 190; $i++) {echo $i}").unwrap();
4142/// tree.apply_mut(&mut (
4243/// ParseInt::default(),
4344/// AddInt::default(),
45+ /// Comparison::default(),
4446/// Forward::default(),
47+ /// Var::default(),
4548/// ForStatementCondition::default(),
4649/// )).unwrap();
4750///
@@ -71,48 +74,7 @@ impl<'a> RuleMut<'a> for ForStatementCondition {
7174 ) -> crate :: error:: MinusOneResult < ( ) > {
7275 let view = node. view ( ) ;
7376 match view. kind ( ) {
74- "for_statement" => {
75- if let ( Some ( initialisation) , Some ( comparison) ) = (
76- view. named_child ( "for_initializer" )
77- . map ( |n| n. smallest_child ( ) ) ,
78- view. named_child ( "for_condition" )
79- . map ( |n| n. smallest_child ( ) ) ,
80- ) {
81- if comparison. kind ( ) == "comparison_expression"
82- && initialisation. kind ( ) == "assignment_expression"
83- {
84- if let (
85- Some ( var_name) ,
86- Some ( value) ,
87- Some ( comp_left) ,
88- Some ( operator) ,
89- Some ( comp_right) ,
90- ) = (
91- initialisation
92- . child ( 0 )
93- . and_then ( |n| Some ( n. text ( ) . ok ( ) ?. to_lowercase ( ) ) ) ,
94- initialisation. child ( 2 ) ,
95- comparison. child ( 0 ) ,
96- comparison. child ( 1 ) ,
97- comparison. child ( 2 ) ,
98- ) {
99- if let Some ( false ) =
100- infer_comparison ( & comp_left, & operator, & comp_right)
101- . or ( ( comp_left. text ( ) . unwrap_or_default ( ) . to_lowercase ( )
102- == var_name)
103- . then_some ( 1 )
104- . and ( infer_comparison ( & value, & operator, & comp_right) ) )
105- . or ( ( comp_right. text ( ) . unwrap_or_default ( ) == var_name)
106- . then_some ( 1 )
107- . and ( infer_comparison ( & comp_left, & operator, & value) ) )
108- {
109- node. set ( Loop ( Dead ) ) ;
110- }
111- }
112- }
113- }
114- }
115- "while_condition" => {
77+ "while_condition" | "for_condition" => {
11678 if let Some ( & Raw ( Bool ( false ) ) ) = view. data ( ) {
11779 node. set_by_node_id ( view. parent ( ) . unwrap ( ) . id ( ) , Loop ( Dead ) ) ;
11880 node. apply_transaction ( ) ;
@@ -255,10 +217,12 @@ impl<'a> RuleMut<'a> for ForStatementFlowControl {
255217
256218#[ cfg( test) ]
257219mod test {
220+ use crate :: ps:: bool:: Comparison ;
258221 use crate :: ps:: build_powershell_tree;
259222 use crate :: ps:: forward:: Forward ;
260223 use crate :: ps:: integer:: ParseInt ;
261224 use crate :: ps:: loops:: { ForStatementCondition , ForStatementFlowControl } ;
225+ use crate :: ps:: var:: Var ;
262226 use crate :: ps:: LoopStatus :: { Dead , OneTurn } ;
263227 use crate :: ps:: Powershell :: { Loop , Raw } ;
264228 use crate :: ps:: Value :: Num ;
@@ -267,8 +231,10 @@ mod test {
267231 fn test_dead_for_statement ( ) {
268232 let mut tree = build_powershell_tree ( "for ($i = 0; $i -gt 1; $i++) {}" ) . unwrap ( ) ;
269233 tree. apply_mut ( & mut (
270- ParseInt :: default ( ) ,
271234 Forward :: default ( ) ,
235+ ParseInt :: default ( ) ,
236+ Comparison :: default ( ) ,
237+ Var :: default ( ) ,
272238 ForStatementCondition :: default ( ) ,
273239 ) )
274240 . unwrap ( ) ;
0 commit comments