Skip to content

Commit 83855de

Browse files
committed
Add post_condition_loop? and loop? for Node
1 parent 8b95869 commit 83855de

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### New features
66

7+
* [#36](https://github.com/rubocop-hq/rubocop-ast/pull/36): Add `post_condition_loop?` and `loop?` for `Node`. ([@fatkodima][])
78
* [#37](https://github.com/rubocop-hq/rubocop-ast/pull/37): Add `enumerable_method?` for `MethodIdentifierPredicates`. ([@fatkodima][])
89
* [#4](https://github.com/rubocop-hq/rubocop-ast/issues/4): Add `interpolation?` for `RegexpNode`. ([@tejasbubane][])
910
* [#20](https://github.com/rubocop-hq/rubocop-ast/pull/20): Add option predicates for `RegexpNode`. ([@owst][])

lib/rubocop/ast/node.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Node < Parser::AST::Node # rubocop:disable Metrics/ClassLength
4444

4545
BASIC_CONDITIONALS = %i[if while until].freeze
4646
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
4749
VARIABLES = %i[ivar gvar cvar lvar].freeze
4850
REFERENCES = %i[nth_ref back_ref].freeze
4951
KEYWORDS = %i[alias and break case class def defs defined?
@@ -426,6 +428,14 @@ def conditional?
426428
CONDITIONALS.include?(type)
427429
end
428430

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+
429439
def keyword?
430440
return true if special_keyword? || send_type? && prefix_not?
431441
return false unless KEYWORDS.include?(type)

spec/rubocop/ast/for_node_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,16 @@
6060

6161
it { expect(for_node.body.sym_type?).to be(true) }
6262
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
6375
end

spec/rubocop/ast/until_node_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,32 @@
4242
it { expect(until_node.do?).to be_falsey }
4343
end
4444
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
4573
end

spec/rubocop/ast/while_node_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,32 @@
4242
it { expect(while_node.do?).to be_falsey }
4343
end
4444
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
4573
end

0 commit comments

Comments
 (0)