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 rubocop#11
  • Loading branch information
tejasbubane committed May 31, 2020
1 parent 0facf3e commit 09aa21a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Changes

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

### Changes
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
12 changes: 12 additions & 0 deletions spec/rubocop/ast/node_spec.rb
Expand Up @@ -347,4 +347,16 @@ def used?
end
end
end

describe '#argument_type?' do
let(:src) { ' bar { |a, b = 42, *c, d: 42, **e| nil }' }

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

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

0 comments on commit 09aa21a

Please sign in to comment.