Skip to content

Commit

Permalink
Add argument_type? method to make it easy to recognize argument nodes
Browse files Browse the repository at this point in the history
Closes #11
  • Loading branch information
tejasbubane authored and marcandre committed Jun 8, 2020
1 parent f86c861 commit f24216a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
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
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

0 comments on commit f24216a

Please sign in to comment.