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 argument_type? method to make it easy to recognize argument nodes #15

Merged
merged 1 commit into from Jun 8, 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 @@ -6,6 +6,7 @@

* [#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][])

## 0.0.3 (2020-05-15)

Expand Down
5 changes: 5 additions & 0 deletions lib/rubocop/ast/node.rb
Expand Up @@ -53,6 +53,7 @@ class Node < Parser::AST::Node # rubocop:disable Metrics/ClassLength
yield].freeze
OPERATOR_KEYWORDS = %i[and or].freeze
SPECIAL_KEYWORDS = %w[__FILE__ __LINE__ __ENCODING__].freeze
ARGUMENT_TYPES = %i[arg optarg restarg kwarg kwoptarg kwrestarg blockarg].freeze

# @see https://www.rubydoc.info/gems/ast/AST/Node:initialize
def initialize(type, children = [], properties = {})
Expand Down Expand Up @@ -456,6 +457,10 @@ def argument?
parent&.send_type? && parent.arguments.include?(self)
end

def argument_type?
ARGUMENT_TYPES.include?(type)
end

def boolean_type?
true_type? || false_type?
end
Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/ast/node_spec.rb
Expand Up @@ -347,4 +347,30 @@ def used?
end
end
end

describe '#argument_type?' do
tejasbubane marked this conversation as resolved.
Show resolved Hide resolved
context 'block arguments' do
let(:src) { 'bar { |a, b = 42, *c, d: 42, **e| nil }' }

it 'returns true for all argument types' do
node.arguments.children.each do |arg|
expect(arg.argument_type?).to eq(true)
end

expect(node.arguments.argument_type?).to eq(false)
end
end

context 'method arguments' do
let(:src) { 'def method_name(a = 0, *b, c: 42, **d); end' }

it 'returns true for all argument types' do
node.arguments.children.each do |arg|
expect(arg.argument_type?).to eq(true)
end

expect(node.arguments.argument_type?).to eq(false)
end
end
end
end