From 09aa21a4dec8e4089203452c5d5b68277dd96aeb Mon Sep 17 00:00:00 2001 From: Tejas Bubane Date: Sun, 31 May 2020 16:26:57 +0530 Subject: [PATCH] Add `argument_type?` method to make it easy to recognize argument nodes Closes #11 --- CHANGELOG.md | 4 ++++ lib/rubocop/ast/node.rb | 5 +++++ spec/rubocop/ast/node_spec.rb | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7762bc3b7..d136c81f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 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..6a511503b 100644 --- a/spec/rubocop/ast/node_spec.rb +++ b/spec/rubocop/ast/node_spec.rb @@ -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