diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e2a0ac93..ead5aea4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/ast/node.rb b/lib/rubocop/ast/node.rb index 7e4d8e01f..483ffc13f 100644 --- a/lib/rubocop/ast/node.rb +++ b/lib/rubocop/ast/node.rb @@ -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 = {}) @@ -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 diff --git a/spec/rubocop/ast/node_spec.rb b/spec/rubocop/ast/node_spec.rb index cfdee5aaf..4eb67aac6 100644 --- a/spec/rubocop/ast/node_spec.rb +++ b/spec/rubocop/ast/node_spec.rb @@ -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