Skip to content

Commit

Permalink
Add post_condition_loop? and loop? for Node
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Jun 14, 2020
1 parent 2a5908f commit 40e456f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### New features

* [#36](https://github.com/rubocop-hq/rubocop-ast/pull/36): Add `post_condition_loop?` and `loop?` for `Node`. ([@fatkodima][])
* [#4](https://github.com/rubocop-hq/rubocop-ast/issues/4): Add `interpolation?` for `RegexpNode`. ([@tejasbubane][])
* [#20](https://github.com/rubocop-hq/rubocop-ast/pull/20): Add option predicates for `RegexpNode`. ([@owst][])
* [#11](https://github.com/rubocop-hq/rubocop-ast/issues/11): Add `argument_type?` method to make it easy to recognize argument nodes. ([@tejasbubane][])
Expand Down Expand Up @@ -32,3 +33,4 @@

[@marcandre]: https://github.com/marcandre
[@owst]: https://github.com/owst
[@fatkodima]: https://github.com/fatkodima
10 changes: 10 additions & 0 deletions lib/rubocop/ast/node.rb
Expand Up @@ -44,6 +44,8 @@ class Node < Parser::AST::Node # rubocop:disable Metrics/ClassLength

BASIC_CONDITIONALS = %i[if while until].freeze
CONDITIONALS = [*BASIC_CONDITIONALS, :case].freeze
POST_CONDITION_LOOP_TYPES = %i[while_post until_post].freeze
LOOP_TYPES = (POST_CONDITION_LOOP_TYPES + %i[while until for]).freeze
VARIABLES = %i[ivar gvar cvar lvar].freeze
REFERENCES = %i[nth_ref back_ref].freeze
KEYWORDS = %i[alias and break case class def defs defined?
Expand Down Expand Up @@ -426,6 +428,14 @@ def conditional?
CONDITIONALS.include?(type)
end

def post_condition_loop?
POST_CONDITION_LOOP_TYPES.include?(type)
end

def loop?
LOOP_TYPES.include?(type)
end

def keyword?
return true if special_keyword? || send_type? && prefix_not?
return false unless KEYWORDS.include?(type)
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/ast/for_node_spec.rb
Expand Up @@ -60,4 +60,16 @@

it { expect(for_node.body.sym_type?).to be(true) }
end

describe '#post_condition_loop?' do
let(:source) { 'for foo in bar; baz; end' }

it { expect(for_node.post_condition_loop?).to be_falsey }
end

describe '#loop?' do
let(:source) { 'for foo in bar; baz; end' }

it { expect(for_node.loop?).to be_truthy }
end
end
28 changes: 28 additions & 0 deletions spec/rubocop/ast/until_node_spec.rb
Expand Up @@ -42,4 +42,32 @@
it { expect(until_node.do?).to be_falsey }
end
end

describe '#post_condition_loop?' do
context 'with a statement until' do
let(:source) { 'until foo; bar; end' }

it { expect(until_node.post_condition_loop?).to be_falsey }
end

context 'with a modifier until' do
let(:source) { 'begin foo; end until bar' }

it { expect(until_node.post_condition_loop?).to be_truthy }
end
end

describe '#loop?' do
context 'with a statement until' do
let(:source) { 'until foo; bar; end' }

it { expect(until_node.loop?).to be_truthy }
end

context 'with a modifier until' do
let(:source) { 'begin foo; end until bar' }

it { expect(until_node.loop?).to be_truthy }
end
end
end
28 changes: 28 additions & 0 deletions spec/rubocop/ast/while_node_spec.rb
Expand Up @@ -42,4 +42,32 @@
it { expect(while_node.do?).to be_falsey }
end
end

describe '#post_condition_loop?' do
context 'with a statement while' do
let(:source) { 'while foo; bar; end' }

it { expect(while_node.post_condition_loop?).to be_falsey }
end

context 'with a modifier while' do
let(:source) { 'begin foo; end while bar' }

it { expect(while_node.post_condition_loop?).to be_truthy }
end
end

describe '#loop?' do
context 'with a statement while' do
let(:source) { 'while foo; bar; end' }

it { expect(while_node.loop?).to be_truthy }
end

context 'with a modifier while' do
let(:source) { 'begin foo; end while bar' }

it { expect(while_node.loop?).to be_truthy }
end
end
end

0 comments on commit 40e456f

Please sign in to comment.