Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add post_condition_loop? and loop_keyword? for Node #36

Merged
merged 1 commit into from Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 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_keyword?` for `Node`. ([@fatkodima][])
* [#38](https://github.com/rubocop-hq/rubocop-ast/pull/38): Add helpers allowing to check whether the method is a nonmutating operator method or a nonmutating method of several core classes. ([@fatkodima][])
* [#37](https://github.com/rubocop-hq/rubocop-ast/pull/37): Add `enumerable_method?` for `MethodIdentifierPredicates`. ([@fatkodima][])
* [#4](https://github.com/rubocop-hq/rubocop-ast/issues/4): Add `interpolation?` for `RegexpNode`. ([@tejasbubane][])
Expand Down
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_keyword?
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_keyword?' do
let(:source) { 'for foo in bar; baz; end' }

it { expect(for_node.loop_keyword?).to be_truthy }
end
end
12 changes: 12 additions & 0 deletions spec/rubocop/ast/send_node_spec.rb
Expand Up @@ -1414,4 +1414,16 @@ module Foo
it { expect(send_node.binary_operation?).to be(false) }
end
end

describe '#post_condition_loop?' do
let(:source) { 'foo(bar)' }

it { expect(send_node.post_condition_loop?).to be(false) }
end

describe '#loop_keyword?' do
let(:source) { 'foo(bar)' }

it { expect(send_node.loop_keyword?).to be(false) }
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_keyword?' do
context 'with a statement until' do
let(:source) { 'until foo; bar; end' }

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

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

it { expect(until_node.loop_keyword?).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_keyword?' do
context 'with a statement while' do
let(:source) { 'while foo; bar; end' }

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

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

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