File tree Expand file tree Collapse file tree 5 files changed +79
-0
lines changed Expand file tree Collapse file tree 5 files changed +79
-0
lines changed Original file line number Diff line number Diff line change 4
4
5
5
### New features
6
6
7
+ * [ #36 ] ( https://github.com/rubocop-hq/rubocop-ast/pull/36 ) : Add ` post_condition_loop? ` and ` loop? ` for ` Node ` . ([ @fatkodima ] [ ] )
7
8
* [ #37 ] ( https://github.com/rubocop-hq/rubocop-ast/pull/37 ) : Add ` enumerable_method? ` for ` MethodIdentifierPredicates ` . ([ @fatkodima ] [ ] )
8
9
* [ #4 ] ( https://github.com/rubocop-hq/rubocop-ast/issues/4 ) : Add ` interpolation? ` for ` RegexpNode ` . ([ @tejasbubane ] [ ] )
9
10
* [ #20 ] ( https://github.com/rubocop-hq/rubocop-ast/pull/20 ) : Add option predicates for ` RegexpNode ` . ([ @owst ] [ ] )
Original file line number Diff line number Diff line change @@ -44,6 +44,8 @@ class Node < Parser::AST::Node # rubocop:disable Metrics/ClassLength
44
44
45
45
BASIC_CONDITIONALS = %i[ if while until ] . freeze
46
46
CONDITIONALS = [ *BASIC_CONDITIONALS , :case ] . freeze
47
+ POST_CONDITION_LOOP_TYPES = %i[ while_post until_post ] . freeze
48
+ LOOP_TYPES = ( POST_CONDITION_LOOP_TYPES + %i[ while until for ] ) . freeze
47
49
VARIABLES = %i[ ivar gvar cvar lvar ] . freeze
48
50
REFERENCES = %i[ nth_ref back_ref ] . freeze
49
51
KEYWORDS = %i[ alias and break case class def defs defined?
@@ -426,6 +428,14 @@ def conditional?
426
428
CONDITIONALS . include? ( type )
427
429
end
428
430
431
+ def post_condition_loop?
432
+ POST_CONDITION_LOOP_TYPES . include? ( type )
433
+ end
434
+
435
+ def loop?
436
+ LOOP_TYPES . include? ( type )
437
+ end
438
+
429
439
def keyword?
430
440
return true if special_keyword? || send_type? && prefix_not?
431
441
return false unless KEYWORDS . include? ( type )
Original file line number Diff line number Diff line change 60
60
61
61
it { expect ( for_node . body . sym_type? ) . to be ( true ) }
62
62
end
63
+
64
+ describe '#post_condition_loop?' do
65
+ let ( :source ) { 'for foo in bar; baz; end' }
66
+
67
+ it { expect ( for_node . post_condition_loop? ) . to be_falsey }
68
+ end
69
+
70
+ describe '#loop?' do
71
+ let ( :source ) { 'for foo in bar; baz; end' }
72
+
73
+ it { expect ( for_node . loop? ) . to be_truthy }
74
+ end
63
75
end
Original file line number Diff line number Diff line change 42
42
it { expect ( until_node . do? ) . to be_falsey }
43
43
end
44
44
end
45
+
46
+ describe '#post_condition_loop?' do
47
+ context 'with a statement until' do
48
+ let ( :source ) { 'until foo; bar; end' }
49
+
50
+ it { expect ( until_node . post_condition_loop? ) . to be_falsey }
51
+ end
52
+
53
+ context 'with a modifier until' do
54
+ let ( :source ) { 'begin foo; end until bar' }
55
+
56
+ it { expect ( until_node . post_condition_loop? ) . to be_truthy }
57
+ end
58
+ end
59
+
60
+ describe '#loop?' do
61
+ context 'with a statement until' do
62
+ let ( :source ) { 'until foo; bar; end' }
63
+
64
+ it { expect ( until_node . loop? ) . to be_truthy }
65
+ end
66
+
67
+ context 'with a modifier until' do
68
+ let ( :source ) { 'begin foo; end until bar' }
69
+
70
+ it { expect ( until_node . loop? ) . to be_truthy }
71
+ end
72
+ end
45
73
end
Original file line number Diff line number Diff line change 42
42
it { expect ( while_node . do? ) . to be_falsey }
43
43
end
44
44
end
45
+
46
+ describe '#post_condition_loop?' do
47
+ context 'with a statement while' do
48
+ let ( :source ) { 'while foo; bar; end' }
49
+
50
+ it { expect ( while_node . post_condition_loop? ) . to be_falsey }
51
+ end
52
+
53
+ context 'with a modifier while' do
54
+ let ( :source ) { 'begin foo; end while bar' }
55
+
56
+ it { expect ( while_node . post_condition_loop? ) . to be_truthy }
57
+ end
58
+ end
59
+
60
+ describe '#loop?' do
61
+ context 'with a statement while' do
62
+ let ( :source ) { 'while foo; bar; end' }
63
+
64
+ it { expect ( while_node . loop? ) . to be_truthy }
65
+ end
66
+
67
+ context 'with a modifier while' do
68
+ let ( :source ) { 'begin foo; end while bar' }
69
+
70
+ it { expect ( while_node . loop? ) . to be_truthy }
71
+ end
72
+ end
45
73
end
You can’t perform that action at this time.
0 commit comments