From 0d048acaa9ec8a750f98ce267a1d51d0be7d4937 Mon Sep 17 00:00:00 2001 From: Ted Johansson Date: Fri, 5 Aug 2016 13:46:50 +0800 Subject: [PATCH] Use node type predicates and `#each_child_node(type)` where possible (#3364) This change replaces constructs like: ``` node.type == :dstr ``` with: ``` node.dstr_type? ``` and constructs like: ``` node.children.select { |child| child.type == :dstr }.each do |n| ``` with: ``` node.each_child_node(:dstr) do |n| ``` --- .rubocop_todo.yml | 12 ++++++------ .../cop/lint/assignment_in_condition.rb | 10 ++++------ lib/rubocop/cop/lint/else_layout.rb | 2 +- lib/rubocop/cop/lint/empty_interpolation.rb | 2 +- lib/rubocop/cop/lint/eval.rb | 2 +- .../cop/lint/format_parameter_mismatch.rb | 2 +- lib/rubocop/cop/lint/literal_in_condition.rb | 10 +++++----- .../cop/lint/literal_in_interpolation.rb | 4 ++-- .../cop/lint/next_without_accumulator.rb | 2 +- .../string_conversion_in_interpolation.rb | 4 ++-- .../lint/underscore_prefixed_variable_name.rb | 3 +-- lib/rubocop/cop/lint/useless_assignment.rb | 2 +- lib/rubocop/cop/lint/useless_setter_call.rb | 10 +++++----- lib/rubocop/cop/lint/void.rb | 6 ++---- lib/rubocop/cop/mixin/access_modifier_node.rb | 10 +++++----- lib/rubocop/cop/mixin/configurable_naming.rb | 9 ++++----- .../mixin/multiline_expression_indentation.rb | 4 ++-- lib/rubocop/cop/mixin/negative_conditional.rb | 12 ++++++------ lib/rubocop/cop/mixin/trailing_comma.rb | 2 +- lib/rubocop/cop/performance/fixed_size.rb | 8 ++------ lib/rubocop/cop/rails/delegate.rb | 5 ++--- lib/rubocop/cop/rails/scope_args.rb | 4 +++- lib/rubocop/cop/rails/validation.rb | 2 +- lib/rubocop/cop/style/alias.rb | 2 +- lib/rubocop/cop/style/align_hash.rb | 2 +- lib/rubocop/cop/style/and_or.rb | 2 +- lib/rubocop/cop/style/array_join.rb | 6 +++--- .../style/braces_around_hash_parameters.rb | 4 ++-- lib/rubocop/cop/style/class_methods.rb | 7 +++---- lib/rubocop/cop/style/collection_methods.rb | 2 +- .../cop/style/conditional_assignment.rb | 4 ++-- lib/rubocop/cop/style/def_with_parentheses.rb | 8 +++----- lib/rubocop/cop/style/each_with_object.rb | 4 ++-- lib/rubocop/cop/style/else_alignment.rb | 19 ++++++++++--------- lib/rubocop/cop/style/even_odd.rb | 8 +++----- lib/rubocop/cop/style/for.rb | 2 +- lib/rubocop/cop/style/format_string.rb | 2 +- lib/rubocop/cop/style/if_unless_modifier.rb | 2 +- lib/rubocop/cop/style/indentation_width.rb | 6 +++--- .../style/method_called_on_do_end_block.rb | 2 +- .../cop/style/method_def_parentheses.rb | 2 +- lib/rubocop/cop/style/method_missing.rb | 2 +- lib/rubocop/cop/style/module_function.rb | 2 +- .../cop/style/multiline_block_chain.rb | 2 +- .../cop/style/multiline_block_layout.rb | 2 +- lib/rubocop/cop/style/mutable_constant.rb | 8 +++++--- lib/rubocop/cop/style/negated_if.rb | 2 +- lib/rubocop/cop/style/negated_while.rb | 6 +++--- lib/rubocop/cop/style/non_nil_check.rb | 11 +++++------ lib/rubocop/cop/style/op_method.rb | 2 +- .../cop/style/parentheses_around_condition.rb | 4 ++-- .../cop/style/percent_literal_delimiters.rb | 2 +- lib/rubocop/cop/style/raise_args.rb | 4 ++-- lib/rubocop/cop/style/redundant_begin.rb | 2 +- lib/rubocop/cop/style/redundant_return.rb | 10 +++++----- lib/rubocop/cop/style/redundant_self.rb | 7 +++++-- lib/rubocop/cop/style/regexp_literal.rb | 3 +-- lib/rubocop/cop/style/self_assignment.rb | 4 ++-- .../cop/style/single_line_block_params.rb | 2 +- lib/rubocop/cop/style/single_line_methods.rb | 2 +- .../space_inside_string_interpolation.rb | 2 +- .../cop/style/stabby_lambda_parentheses.rb | 6 ++---- lib/rubocop/cop/style/symbol_proc.rb | 6 +++--- lib/rubocop/cop/style/trivial_accessors.rb | 12 ++++++------ lib/rubocop/cop/style/unneeded_capital_w.rb | 12 ++++++++---- .../cop/style/unneeded_interpolation.rb | 9 ++++----- lib/rubocop/cop/style/word_array.rb | 2 +- lib/rubocop/cop/util.rb | 5 +---- lib/rubocop/cop/variable_force.rb | 6 +++--- lib/rubocop/cop/variable_force/locatable.rb | 4 ++-- lib/rubocop/cop/variable_force/variable.rb | 4 ++-- .../cop/variable_force/variable_table.rb | 6 +++--- 72 files changed, 175 insertions(+), 187 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 901c531efd36..b1819cd8eb90 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,25 +1,25 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2016-08-03 09:52:07 +0300 using RuboCop version 0.42.0. +# on 2016-08-04 02:09:05 +0800 using RuboCop version 0.42.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 141 +# Offense count: 131 Metrics/AbcSize: Max: 21 -# Offense count: 30 +# Offense count: 31 # Configuration parameters: CountComments. Metrics/ClassLength: Max: 158 -# Offense count: 58 +# Offense count: 57 Metrics/CyclomaticComplexity: Max: 8 -# Offense count: 192 +# Offense count: 191 # Configuration parameters: CountComments. Metrics/MethodLength: Max: 16 @@ -29,6 +29,6 @@ Metrics/MethodLength: Metrics/ModuleLength: Max: 156 -# Offense count: 30 +# Offense count: 29 Metrics/PerceivedComplexity: Max: 8 diff --git a/lib/rubocop/cop/lint/assignment_in_condition.rb b/lib/rubocop/cop/lint/assignment_in_condition.rb index a251818b8721..634233420bac 100644 --- a/lib/rubocop/cop/lint/assignment_in_condition.rb +++ b/lib/rubocop/cop/lint/assignment_in_condition.rb @@ -30,9 +30,9 @@ def check(node) condition, = *node # assignments inside blocks are not what we're looking for - return if condition.type == :block + return if condition.block_type? traverse_node(condition, ASGN_TYPES) do |asgn_node| - if asgn_node.type == :send + if asgn_node.send_type? _receiver, method_name, *_args = *asgn_node next :skip_children if method_name !~ /=\z/ end @@ -43,7 +43,7 @@ def check(node) end # assignment nodes from shorthand ops like ||= don't have operator - if asgn_node.type != :begin && asgn_node.loc.operator + if !asgn_node.begin_type? && asgn_node.loc.operator add_offense(asgn_node, :operator) end end @@ -54,9 +54,7 @@ def traverse_node(node, types, &block) result = yield node if types.include?(node.type) # return to skip all descendant nodes return if result == :skip_children - node.children.each do |child| - traverse_node(child, types, &block) if child.is_a?(Node) - end + node.each_child_node { |child| traverse_node(child, types, &block) } end end end diff --git a/lib/rubocop/cop/lint/else_layout.rb b/lib/rubocop/cop/lint/else_layout.rb index 6023da5a387c..0612a61ed7c1 100644 --- a/lib/rubocop/cop/lint/else_layout.rb +++ b/lib/rubocop/cop/lint/else_layout.rb @@ -37,7 +37,7 @@ def check(node) def check_else(node) _cond, _if_branch, else_branch = *node - return unless else_branch && else_branch.type == :begin + return unless else_branch && else_branch.begin_type? first_else_expr = else_branch.children.first return unless first_else_expr.source_range.line == node.loc.else.line diff --git a/lib/rubocop/cop/lint/empty_interpolation.rb b/lib/rubocop/cop/lint/empty_interpolation.rb index 03f6e81cdb75..4c76fa73977f 100644 --- a/lib/rubocop/cop/lint/empty_interpolation.rb +++ b/lib/rubocop/cop/lint/empty_interpolation.rb @@ -13,7 +13,7 @@ class EmptyInterpolation < Cop MSG = 'Empty interpolation detected.'.freeze def on_dstr(node) - node.children.select { |n| n.type == :begin }.each do |begin_node| + node.each_child_node(:begin) do |begin_node| add_offense(begin_node, :expression) if begin_node.children.empty? end end diff --git a/lib/rubocop/cop/lint/eval.rb b/lib/rubocop/cop/lint/eval.rb index e5e95a63811a..a7212ffe2434 100644 --- a/lib/rubocop/cop/lint/eval.rb +++ b/lib/rubocop/cop/lint/eval.rb @@ -14,7 +14,7 @@ def on_send(node) return unless receiver.nil? && method_name == :eval && !args.empty? && - args.first.type != :str + !args.first.str_type? add_offense(node, :selector) end end diff --git a/lib/rubocop/cop/lint/format_parameter_mismatch.rb b/lib/rubocop/cop/lint/format_parameter_mismatch.rb index aab02aa4c444..2a337a78de84 100644 --- a/lib/rubocop/cop/lint/format_parameter_mismatch.rb +++ b/lib/rubocop/cop/lint/format_parameter_mismatch.rb @@ -73,7 +73,7 @@ def node_with_splat_args?(node) _receiver_node, _method_name, *args = *node - args.butfirst.any? { |arg| arg.type == :splat } + args.butfirst.any?(&:splat_type?) end def heredoc?(node) diff --git a/lib/rubocop/cop/lint/literal_in_condition.rb b/lib/rubocop/cop/lint/literal_in_condition.rb index c4aab75b4e9f..9bc5412e662d 100644 --- a/lib/rubocop/cop/lint/literal_in_condition.rb +++ b/lib/rubocop/cop/lint/literal_in_condition.rb @@ -73,7 +73,7 @@ def check_for_literal(node) end def not?(node) - return false unless node && node.type == :send + return false unless node && node.send_type? _receiver, method_name, *_args = *node @@ -81,7 +81,7 @@ def not?(node) end def basic_literal?(node) - if node && node.type == :array + if node && node.array_type? primitive_array?(node) else node.basic_literal? @@ -104,7 +104,7 @@ def check_node(node) operands.each do |op| handle_node(op) end - elsif node.type == :begin && node.children.size == 1 + elsif node.begin_type? && node.children.one? child_node = node.children.first handle_node(child_node) end @@ -119,8 +119,8 @@ def handle_node(node) end def check_case_cond(node) - return if node.type == :array && !primitive_array?(node) - return if node.type == :dstr + return if node.array_type? && !primitive_array?(node) + return if node.dstr_type? handle_node(node) end diff --git a/lib/rubocop/cop/lint/literal_in_interpolation.rb b/lib/rubocop/cop/lint/literal_in_interpolation.rb index 3751113398e8..1cceafaa8a3a 100644 --- a/lib/rubocop/cop/lint/literal_in_interpolation.rb +++ b/lib/rubocop/cop/lint/literal_in_interpolation.rb @@ -14,7 +14,7 @@ class LiteralInInterpolation < Cop COMPOSITE = [:array, :hash, :pair, :irange, :erange].freeze def on_dstr(node) - node.children.select { |n| n.type == :begin }.each do |begin_node| + node.each_child_node(:begin) do |begin_node| final_node = begin_node.children.last next unless final_node next if special_keyword?(final_node) @@ -35,7 +35,7 @@ def autocorrect(node) def special_keyword?(node) # handle strings like __FILE__ - (node.type == :str && !node.loc.respond_to?(:begin)) || + (node.str_type? && !node.loc.respond_to?(:begin)) || node.source_range.is?('__LINE__') end diff --git a/lib/rubocop/cop/lint/next_without_accumulator.rb b/lib/rubocop/cop/lint/next_without_accumulator.rb index 43e6e07060ee..685a27ce1844 100644 --- a/lib/rubocop/cop/lint/next_without_accumulator.rb +++ b/lib/rubocop/cop/lint/next_without_accumulator.rb @@ -38,7 +38,7 @@ def on_block(node) private def parent_block_node(node) - node.each_ancestor.find { |n| n.type == :block } + node.each_ancestor(:block).first end end end diff --git a/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb b/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb index c9a90e5a5b13..b34710fcb95a 100644 --- a/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +++ b/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb @@ -16,9 +16,9 @@ class StringConversionInInterpolation < Cop 'interpolation.'.freeze def on_dstr(node) - node.children.select { |n| n.type == :begin }.each do |begin_node| + node.each_child_node(:begin) do |begin_node| final_node = begin_node.children.last - next unless final_node && final_node.type == :send + next unless final_node && final_node.send_type? receiver, method_name, *args = *final_node next unless method_name == :to_s && args.empty? diff --git a/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb b/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb index 3ff675cd2c4d..21719fa63764 100644 --- a/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +++ b/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb @@ -21,12 +21,11 @@ def after_leaving_scope(scope, _variable_table) def check_variable(variable) return unless variable.should_be_unused? - return if variable.references.empty? return if variable.references.none?(&:explicit?) node = variable.declaration_node - location = if node.type == :match_with_lvasgn + location = if node.match_with_lvasgn_type? node.children.first.source_range else node.loc.name diff --git a/lib/rubocop/cop/lint/useless_assignment.rb b/lib/rubocop/cop/lint/useless_assignment.rb index 63af172844a8..3544ebb07887 100644 --- a/lib/rubocop/cop/lint/useless_assignment.rb +++ b/lib/rubocop/cop/lint/useless_assignment.rb @@ -71,7 +71,7 @@ def message_for_useless_assignment(assignment) def return_value_node_of_scope(scope) body_node = scope.body_node - if body_node.type == :begin + if body_node.begin_type? body_node.children.last else body_node diff --git a/lib/rubocop/cop/lint/useless_setter_call.rb b/lib/rubocop/cop/lint/useless_setter_call.rb index 44f515d53723..b44b13f48c76 100644 --- a/lib/rubocop/cop/lint/useless_setter_call.rb +++ b/lib/rubocop/cop/lint/useless_setter_call.rb @@ -24,7 +24,7 @@ class UselessSetterCall < Cop def on_method_def(_node, _method_name, _args, body) return unless body - expression = if body.type == :begin + expression = if body.begin_type? body.children else body @@ -45,9 +45,9 @@ def on_method_def(_node, _method_name, _args, body) end def setter_call_to_local_variable?(node) - return unless node && node.type == :send + return unless node && node.send_type? receiver, method, _args = *node - return unless receiver && receiver.type == :lvar + return unless receiver && receiver.lvar_type? method =~ /(?:\w|\[\])=$/ end @@ -100,7 +100,7 @@ def process_multiple_assignment(masgn_node) lhs_variable_name, = *lhs_node rhs_node = mrhs_node.children[index] - if mrhs_node.type == :array && rhs_node + if mrhs_node.array_type? && rhs_node process_assignment(lhs_variable_name, rhs_node) else @local[lhs_variable_name] = true @@ -140,7 +140,7 @@ def process_assignment(asgn_node, rhs_node) def constructor?(node) return true if node.literal? - return false unless node.type == :send + return false unless node.send_type? _receiver, method = *node method == :new end diff --git a/lib/rubocop/cop/lint/void.rb b/lib/rubocop/cop/lint/void.rb index c59dfa536047..076566121aeb 100644 --- a/lib/rubocop/cop/lint/void.rb +++ b/lib/rubocop/cop/lint/void.rb @@ -34,8 +34,7 @@ def check_begin(node) end def check_for_void_op(node) - return unless node.type == :send - return unless node.loc.selector + return unless node.send_type? && node.loc.selector op = node.loc.selector.source @@ -48,8 +47,7 @@ def check_for_var(node) end def check_for_literal(node) - return unless node.literal? - return if node.xstr_type? + return if !node.literal? || node.xstr_type? add_offense(node, :expression, format(LIT_MSG, node.source)) end diff --git a/lib/rubocop/cop/mixin/access_modifier_node.rb b/lib/rubocop/cop/mixin/access_modifier_node.rb index 7edec2e10c06..387b22e35745 100644 --- a/lib/rubocop/cop/mixin/access_modifier_node.rb +++ b/lib/rubocop/cop/mixin/access_modifier_node.rb @@ -29,11 +29,11 @@ def modifier_structure?(node) # is a Class or Module. Filters out simple method calls to similarly # named private, protected or public. def class_or_module_parent?(node) - node.each_ancestor do |a| - if a.type == :block - return true if a.class_constructor? - elsif a.type != :begin - return [:casgn, :sclass, :class, :module].include?(a.type) + node.each_ancestor do |ancestor| + if ancestor.block_type? + return true if ancestor.class_constructor? + elsif !ancestor.begin_type? + return [:casgn, :sclass, :class, :module].include?(ancestor.type) end end end diff --git a/lib/rubocop/cop/mixin/configurable_naming.rb b/lib/rubocop/cop/mixin/configurable_naming.rb index 515c15486b8c..d446931dda4a 100644 --- a/lib/rubocop/cop/mixin/configurable_naming.rb +++ b/lib/rubocop/cop/mixin/configurable_naming.rb @@ -31,14 +31,13 @@ def valid_name?(node, name) # A class emitter method is a singleton method in a class/module, where # the method has the same name as a class defined in the class/module. def class_emitter_method?(node, name) - return false unless node.defs_type? + return false unless node.parent && node.defs_type? # a class emitter method may be defined inside `def self.included`, # `def self.extended`, etc. - node = node.parent while node.parent && node.parent.defs_type? - return false unless node.parent + node = node.parent while node.parent.defs_type? - node.parent.children.compact.any? do |c| - c.class_type? && c.loc.name.is?(name.to_s) + node.parent.each_child_node(:class).any? do |c| + c.loc.name.is?(name.to_s) end end end diff --git a/lib/rubocop/cop/mixin/multiline_expression_indentation.rb b/lib/rubocop/cop/mixin/multiline_expression_indentation.rb index b15c8679e1d1..7223687218d0 100644 --- a/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +++ b/lib/rubocop/cop/mixin/multiline_expression_indentation.rb @@ -177,12 +177,12 @@ def not_for_this_cop?(node) end def grouped_expression?(node) - node.type == :begin && node.loc.respond_to?(:begin) && node.loc.begin + node.begin_type? && node.loc.respond_to?(:begin) && node.loc.begin end def inside_arg_list_parentheses?(node, ancestor) a = ancestor.loc - return false unless ancestor.type == :send && a.begin && + return false unless ancestor.send_type? && a.begin && a.begin.is?('(') n = node.source_range n.begin_pos > a.begin.begin_pos && n.end_pos < a.end.end_pos diff --git a/lib/rubocop/cop/mixin/negative_conditional.rb b/lib/rubocop/cop/mixin/negative_conditional.rb index 3bd19adaaae7..94add6544d6b 100644 --- a/lib/rubocop/cop/mixin/negative_conditional.rb +++ b/lib/rubocop/cop/mixin/negative_conditional.rb @@ -6,19 +6,19 @@ module Cop # Some common code shared between FavorUnlessOverNegatedIf and # FavorUntilOverNegatedWhile. module NegativeConditional - def self.included(mod) - mod.def_node_matcher :single_negative?, '(send !(send _ :!) :!)' - end + extend NodePattern::Macros + include IfNode + + def_node_matcher :single_negative?, '(send !(send _ :!) :!)' def check_negative_conditional(node) condition, _body, _rest = *node # Look at last expression of contents if there are parentheses # around condition. - condition = condition.children.last while condition.type == :begin + condition = condition.children.last while condition.begin_type? - return unless single_negative?(condition) && - !(node.loc.respond_to?(:else) && node.loc.else) + return unless single_negative?(condition) && !if_else?(node) add_offense(node, :expression) end diff --git a/lib/rubocop/cop/mixin/trailing_comma.rb b/lib/rubocop/cop/mixin/trailing_comma.rb index f866893418fa..6623f3b7c96b 100644 --- a/lib/rubocop/cop/mixin/trailing_comma.rb +++ b/lib/rubocop/cop/mixin/trailing_comma.rb @@ -128,7 +128,7 @@ def avoid_comma(kind, comma_begin_pos, sb, extra_info) def put_comma(node, items, kind, sb) last_item = items.last - return if last_item.type == :block_pass + return if last_item.block_pass_type? last_expr = last_item.source_range ix = last_expr.source.rindex("\n") || 0 diff --git a/lib/rubocop/cop/performance/fixed_size.rb b/lib/rubocop/cop/performance/fixed_size.rb index e3b941a30ddc..1b72db00ef2f 100644 --- a/lib/rubocop/cop/performance/fixed_size.rb +++ b/lib/rubocop/cop/performance/fixed_size.rb @@ -30,17 +30,13 @@ def on_send(node) def contains_splat?(node) return unless node.array_type? - node.children.any? do |child| - child.respond_to?(:splat_type?) && child.splat_type? - end + node.each_child_node(:splat).any? end def contains_double_splat?(node) return unless node.hash_type? - node.children.any? do |child| - child.respond_to?(:kwsplat_type?) && child.kwsplat_type? - end + node.each_child_node(:kwsplat).any? end def string_argument?(node) diff --git a/lib/rubocop/cop/rails/delegate.rb b/lib/rubocop/cop/rails/delegate.rb index 774406806386..391743b913b1 100644 --- a/lib/rubocop/cop/rails/delegate.rb +++ b/lib/rubocop/cop/rails/delegate.rb @@ -61,9 +61,8 @@ def trivial_delegate?(method_name, args, body) end def delegate?(body) - receiver, * = *body - return false unless receiver.is_a? Parser::AST::Node - return false unless receiver.type == :send + receiver, = *body + return false unless receiver.is_a?(Node) && receiver.send_type? receiver.child_nodes.empty? end diff --git a/lib/rubocop/cop/rails/scope_args.rb b/lib/rubocop/cop/rails/scope_args.rb index f8ea3b4adec1..ce0b807dfd35 100644 --- a/lib/rubocop/cop/rails/scope_args.rb +++ b/lib/rubocop/cop/rails/scope_args.rb @@ -26,7 +26,9 @@ def on_send(node) second_arg = args[1] - add_offense(second_arg, :expression) if second_arg.type == :send + return unless second_arg.send_type? + + add_offense(second_arg, :expression) end end end diff --git a/lib/rubocop/cop/rails/validation.rb b/lib/rubocop/cop/rails/validation.rb index db9bb545de54..ad0210018527 100644 --- a/lib/rubocop/cop/rails/validation.rb +++ b/lib/rubocop/cop/rails/validation.rb @@ -51,7 +51,7 @@ def autocorrect(node) def correct_validate_type(corrector, node) _receiver, method_name, *args = *node - options = args.find { |arg| arg.type != :sym } + options = args.find { |arg| !arg.sym_type? } validate_type = method_name.to_s.split('_')[1] if options diff --git a/lib/rubocop/cop/style/alias.rb b/lib/rubocop/cop/style/alias.rb index 374e00df253e..6bbe5d51f522 100644 --- a/lib/rubocop/cop/style/alias.rb +++ b/lib/rubocop/cop/style/alias.rb @@ -26,7 +26,7 @@ def on_send(node) def on_alias(node) # alias_method can't be used with global variables - return if node.children.any?(&:gvar_type?) + return if node.each_child_node(:gvar).any? # alias_method can't be used in instance_eval blocks scope_type = scope_type(node) return if scope_type == :instance_eval diff --git a/lib/rubocop/cop/style/align_hash.rb b/lib/rubocop/cop/style/align_hash.rb index 18767a8ed02b..6ca89f7dc15f 100644 --- a/lib/rubocop/cop/style/align_hash.rb +++ b/lib/rubocop/cop/style/align_hash.rb @@ -189,7 +189,7 @@ def ignore_last_argument_hash?(node) end def hash?(node) - node.respond_to?(:type) && node.type == :hash + node.respond_to?(:type) && node.hash_type? end def explicit_hash?(node) diff --git a/lib/rubocop/cop/style/and_or.rb b/lib/rubocop/cop/style/and_or.rb index cd9d9a00b0c0..a2ba69d2a5e6 100644 --- a/lib/rubocop/cop/style/and_or.rb +++ b/lib/rubocop/cop/style/and_or.rb @@ -60,7 +60,7 @@ def process_logical_op(node) def autocorrect(node) expr1, expr2 = *node - replacement = (node.type == :and ? '&&' : '||') + replacement = (node.and_type? ? '&&' : '||') lambda do |corrector| [expr1, expr2].each do |expr| if expr.send_type? diff --git a/lib/rubocop/cop/style/array_join.rb b/lib/rubocop/cop/style/array_join.rb index 95b879a79d09..2b31e46b4ee6 100644 --- a/lib/rubocop/cop/style/array_join.rb +++ b/lib/rubocop/cop/style/array_join.rb @@ -14,8 +14,8 @@ class ArrayJoin < Cop def on_send(node) receiver_node, method_name, *arg_nodes = *node - return unless receiver_node && receiver_node.type == :array && - method_name == :* && arg_nodes[0].type == :str + return unless receiver_node && receiver_node.array_type? && + method_name == :* && arg_nodes.first.str_type? add_offense(node, :selector) end @@ -23,7 +23,7 @@ def on_send(node) def autocorrect(node) receiver_node, _method_name, *arg_nodes = *node array = receiver_node.source - join_arg = arg_nodes[0].source + join_arg = arg_nodes.first.source lambda do |corrector| corrector.replace(node.source_range, "#{array}.join(#{join_arg})") diff --git a/lib/rubocop/cop/style/braces_around_hash_parameters.rb b/lib/rubocop/cop/style/braces_around_hash_parameters.rb index 4bdeaa0dc73d..2e781871382e 100644 --- a/lib/rubocop/cop/style/braces_around_hash_parameters.rb +++ b/lib/rubocop/cop/style/braces_around_hash_parameters.rb @@ -39,7 +39,7 @@ def check(arg, args) end def check_context_dependent(arg, args) - braces_around_2nd_from_end = args.length > 1 && args[-2].type == :hash + braces_around_2nd_from_end = args.length > 1 && args[-2].hash_type? if braces?(arg) unless braces_around_2nd_from_end add_offense(arg.parent, arg.source_range, @@ -102,7 +102,7 @@ def add_braces(corrector, node) end def non_empty_hash?(arg) - arg && arg.type == :hash && arg.children.any? + arg && arg.hash_type? && !arg.children.empty? end def braces?(arg) diff --git a/lib/rubocop/cop/style/class_methods.rb b/lib/rubocop/cop/style/class_methods.rb index 1eb2f585a5e9..2ded9743c6ae 100644 --- a/lib/rubocop/cop/style/class_methods.rb +++ b/lib/rubocop/cop/style/class_methods.rb @@ -39,11 +39,10 @@ def on_module(node) def check(name, node) return unless node - if node.type == :defs + if node.defs_type? check_defs(name, node) - elsif node.type == :begin - defs_nodes = node.children.compact.select { |n| n.type == :defs } - defs_nodes.each { |n| check_defs(name, n) } + elsif node.begin_type? + node.each_child_node(:defs) { |n| check_defs(name, n) } end end diff --git a/lib/rubocop/cop/style/collection_methods.rb b/lib/rubocop/cop/style/collection_methods.rb index d6618c4cfcf6..cc8dead783fe 100644 --- a/lib/rubocop/cop/style/collection_methods.rb +++ b/lib/rubocop/cop/style/collection_methods.rb @@ -23,7 +23,7 @@ def on_block(node) def on_send(node) _receiver, _method_name, *args = *node - return unless args.size == 1 && args.first.type == :block_pass + return unless args.one? && args.first.block_pass_type? check_method_node(node) end diff --git a/lib/rubocop/cop/style/conditional_assignment.rb b/lib/rubocop/cop/style/conditional_assignment.rb index 02ce9e5270b2..a815d9658679 100644 --- a/lib/rubocop/cop/style/conditional_assignment.rb +++ b/lib/rubocop/cop/style/conditional_assignment.rb @@ -277,7 +277,7 @@ def autocorrect(node) def assignment_node(node) *_variable, assignment = *node - if assignment.begin_type? && assignment.children.size == 1 + if assignment.begin_type? && assignment.children.one? assignment, = *assignment end @@ -486,7 +486,7 @@ def move_assignment_inside_condition(node) def extract_branches(node) *_var, rhs = *node - condition, = *rhs if rhs.begin_type? && rhs.children.size == 1 + condition, = *rhs if rhs.begin_type? && rhs.children.one? _condition, if_branch, else_branch = *(condition || rhs) [if_branch, else_branch] diff --git a/lib/rubocop/cop/style/def_with_parentheses.rb b/lib/rubocop/cop/style/def_with_parentheses.rb index c3021d86b9b7..3eeab4a92e7a 100644 --- a/lib/rubocop/cop/style/def_with_parentheses.rb +++ b/lib/rubocop/cop/style/def_with_parentheses.rb @@ -14,12 +14,10 @@ class DefWithParentheses < Cop 'any arguments.'.freeze def on_method_def(node, _method_name, args, _body) - start_line = node.loc.keyword.line - end_line = node.loc.end.line + return if node.single_line? + return unless args.children.empty? && args.loc.begin - return if start_line == end_line - - add_offense(args, :begin) if args.children == [] && args.loc.begin + add_offense(args, :begin) end def autocorrect(node) diff --git a/lib/rubocop/cop/style/each_with_object.rb b/lib/rubocop/cop/style/each_with_object.rb index a249bc2ef545..d4a6c0978a27 100644 --- a/lib/rubocop/cop/style/each_with_object.rb +++ b/lib/rubocop/cop/style/each_with_object.rb @@ -76,8 +76,8 @@ def accumulator_param_assigned_to?(body, args) def return_value(body) return unless body - return_value = body.type == :begin ? body.children.last : body - return_value if return_value && return_value.type == :lvar + return_value = body.begin_type? ? body.children.last : body + return_value if return_value && return_value.lvar_type? end def first_argument_returned?(args, return_value) diff --git a/lib/rubocop/cop/style/else_alignment.rb b/lib/rubocop/cop/style/else_alignment.rb index 251997d73a75..6e5cbfde445b 100644 --- a/lib/rubocop/cop/style/else_alignment.rb +++ b/lib/rubocop/cop/style/else_alignment.rb @@ -12,25 +12,26 @@ class ElseAlignment < Cop include EndKeywordAlignment include AutocorrectAlignment include CheckAssignment + include IfNode MSG = 'Align `%s` with `%s`.'.freeze def on_if(node, base = nil) return if ignored_node?(node) - return unless node.loc.respond_to?(:else) - return if node.loc.else.nil? + return unless if_else?(node) else_range = node.loc.else return unless begins_its_line?(else_range) check_alignment(base_range(node, base), else_range) - return if else_range.source != 'elsif' + _, _, else_body = *node + + return unless else_body && elsif?(else_body) # If the `else` part is actually an `elsif`, we check the `elsif` # node in case it contains an `else` within, because that `else` # should have the same alignment (base). - _condition, _if_body, else_body = *node on_if(else_body, base) # The `elsif` node will get an `on_if` call from the framework later, # but we're done here, so we set it to ignored. @@ -38,10 +39,10 @@ def on_if(node, base = nil) end def on_rescue(node) - return unless node.loc.else + return unless if_else?(node) parent = node.parent - parent = parent.parent if parent.type == :ensure + parent = parent.parent if parent.ensure_type? base = case parent.type when :def, :defs then base_for_method_definition(parent) when :kwbegin then parent.loc.begin @@ -52,7 +53,7 @@ def on_rescue(node) def on_case(node) _cond, *whens, _else = *node - return unless node.loc.else + return unless if_else?(node) check_alignment(whens.last.loc.keyword, node.loc.else) end @@ -72,7 +73,7 @@ def base_range(node, base) def base_for_method_definition(node) parent = node.parent - if parent && parent.type == :send + if parent && parent.send_type? parent.loc.selector # For example "private def ..." else node.loc.keyword @@ -90,7 +91,7 @@ def check_assignment(node, rhs) style = end_config['AlignWith'] || 'keyword' base = variable_alignment?(node.loc, rhs, style.to_sym) ? node : rhs - return if rhs.type != :if + return unless rhs.if_type? on_if(rhs, base) ignore_node(rhs) diff --git a/lib/rubocop/cop/style/even_odd.rb b/lib/rubocop/cop/style/even_odd.rb index 2275fa7ae3a4..286c42c0b917 100644 --- a/lib/rubocop/cop/style/even_odd.rb +++ b/lib/rubocop/cop/style/even_odd.rb @@ -36,7 +36,7 @@ def autocorrect(node) def base_number(node) receiver, = *node node = expression(receiver) - node.children[0].source + node.children.first.source end def offense_type(node) @@ -64,11 +64,9 @@ def expression(node) return unless node # check for scenarios like (x % 2) == 0 - if node.type == :begin && node.children.size == 1 - node = node.children.first - end + node = node.children.first if node.begin_type? && node.children.one? - return unless node.type == :send + return unless node.send_type? node end end diff --git a/lib/rubocop/cop/style/for.rb b/lib/rubocop/cop/style/for.rb index c7cec3332c39..bab1dbe77484 100644 --- a/lib/rubocop/cop/style/for.rb +++ b/lib/rubocop/cop/style/for.rb @@ -26,7 +26,7 @@ def on_block(node) return if block_length(node).zero? method, _args, _body = *node - return unless method.type == :send + return unless method.send_type? _receiver, method_name, *args = *method return unless method_name == :each && args.empty? diff --git a/lib/rubocop/cop/style/format_string.rb b/lib/rubocop/cop/style/format_string.rb index a0be527412ef..a9b926a5304f 100644 --- a/lib/rubocop/cop/style/format_string.rb +++ b/lib/rubocop/cop/style/format_string.rb @@ -54,7 +54,7 @@ def percent?(node) method_name == :% && ([:str, :dstr].include?(receiver_node.type) || - arg_nodes[0].type == :array) + arg_nodes.first.array_type?) end def message(node) diff --git a/lib/rubocop/cop/style/if_unless_modifier.rb b/lib/rubocop/cop/style/if_unless_modifier.rb index 77b33d3303dc..fcad0906530d 100644 --- a/lib/rubocop/cop/style/if_unless_modifier.rb +++ b/lib/rubocop/cop/style/if_unless_modifier.rb @@ -59,7 +59,7 @@ def autocorrect(node) # returns false if the then or else children are conditionals def nested_conditional?(node) - node.children[1, 2].any? { |child| child && child.type == :if } + node.children[1, 2].compact.any?(&:if_type?) end def oneline(node) diff --git a/lib/rubocop/cop/style/indentation_width.rb b/lib/rubocop/cop/style/indentation_width.rb index 2a08e96a3139..5a90598162fa 100644 --- a/lib/rubocop/cop/style/indentation_width.rb +++ b/lib/rubocop/cop/style/indentation_width.rb @@ -121,7 +121,7 @@ def on_case(node) _condition, *branches = *node latest_when = nil branches.compact.each do |b| - if b.type == :when + if b.when_type? # TODO: Revert to the original expression once the fix in Rubinius # is released. # @@ -208,7 +208,7 @@ def check_indentation(base_loc, body_node, style = 'normal') def offense(body_node, indentation, style) # This cop only auto-corrects the first statement in a def body, for # example. - if body_node.type == :begin && !parentheses?(body_node) + if body_node.begin_type? && !parentheses?(body_node) body_node = body_node.children.first end @@ -272,7 +272,7 @@ def offending_range(body_node, indentation) end def starts_with_access_modifier?(body_node) - body_node.type == :begin && modifier_node?(body_node.children.first) + body_node.begin_type? && modifier_node?(body_node.children.first) end def configured_indentation_width diff --git a/lib/rubocop/cop/style/method_called_on_do_end_block.rb b/lib/rubocop/cop/style/method_called_on_do_end_block.rb index def9ee6c07b3..76c9e6c23745 100644 --- a/lib/rubocop/cop/style/method_called_on_do_end_block.rb +++ b/lib/rubocop/cop/style/method_called_on_do_end_block.rb @@ -29,7 +29,7 @@ def on_block(node) def on_send(node) return if ignored_node?(node) receiver, _method_name, *_args = *node - return unless receiver && receiver.type == :block && + return unless receiver && receiver.block_type? && receiver.loc.end.is?('end') range = Parser::Source::Range.new(receiver.loc.end.source_buffer, diff --git a/lib/rubocop/cop/style/method_def_parentheses.rb b/lib/rubocop/cop/style/method_def_parentheses.rb index 4e6201ff0127..c4b213088121 100644 --- a/lib/rubocop/cop/style/method_def_parentheses.rb +++ b/lib/rubocop/cop/style/method_def_parentheses.rb @@ -68,7 +68,7 @@ def unwanted_parentheses(args) end def args_node(def_node) - if def_node.type == :def + if def_node.def_type? _method_name, args, _body = *def_node else _scope, _method_name, args, _body = *def_node diff --git a/lib/rubocop/cop/style/method_missing.rb b/lib/rubocop/cop/style/method_missing.rb index 02013df5d13e..a29bf31aafa4 100644 --- a/lib/rubocop/cop/style/method_missing.rb +++ b/lib/rubocop/cop/style/method_missing.rb @@ -60,7 +60,7 @@ def calls_super?(node) end def implements_respond_to_missing?(node) - node.parent.children.any? do |sibling| + node.parent.each_child_node(:def).any? do |sibling| respond_to_missing_def?(sibling) end end diff --git a/lib/rubocop/cop/style/module_function.rb b/lib/rubocop/cop/style/module_function.rb index d99f4e7b11d8..81f4ed0f5074 100644 --- a/lib/rubocop/cop/style/module_function.rb +++ b/lib/rubocop/cop/style/module_function.rb @@ -38,7 +38,7 @@ class ModuleFunction < Cop def on_module(node) _name, body = *node - return unless body && body.type == :begin + return unless body && body.begin_type? body.children.each do |body_node| if style == :module_function && body_node == EXTEND_SELF_NODE diff --git a/lib/rubocop/cop/style/multiline_block_chain.rb b/lib/rubocop/cop/style/multiline_block_chain.rb index f6716ccaf80e..a59f2a5d3cf4 100644 --- a/lib/rubocop/cop/style/multiline_block_chain.rb +++ b/lib/rubocop/cop/style/multiline_block_chain.rb @@ -21,7 +21,7 @@ def on_block(node) method, _args, _body = *node method.each_node(:send) do |send_node| receiver, _method_name, *_args = *send_node - next unless receiver && receiver.type == :block + next unless receiver && receiver.block_type? # The begin and end could also be braces, but we call the # variables do... and end... diff --git a/lib/rubocop/cop/style/multiline_block_layout.rb b/lib/rubocop/cop/style/multiline_block_layout.rb index 294c9a149917..83c0742bf63a 100644 --- a/lib/rubocop/cop/style/multiline_block_layout.rb +++ b/lib/rubocop/cop/style/multiline_block_layout.rb @@ -108,7 +108,7 @@ def autocorrect_arguments(corrector, node, args) end def autocorrect_body(corrector, node, block_body) - first_node = if block_body.type == :begin + first_node = if block_body.begin_type? block_body.children.first else block_body diff --git a/lib/rubocop/cop/style/mutable_constant.rb b/lib/rubocop/cop/style/mutable_constant.rb index b8b1390db532..81aa356d9903 100644 --- a/lib/rubocop/cop/style/mutable_constant.rb +++ b/lib/rubocop/cop/style/mutable_constant.rb @@ -25,7 +25,10 @@ def on_casgn(node) def on_or_asgn(node) lhs, value = *node - on_assignment(value) if lhs && lhs.type == :casgn + + return unless lhs && lhs.casgn_type? + + on_assignment(value) end def autocorrect(node) @@ -43,8 +46,7 @@ def autocorrect(node) private def on_assignment(value) - return unless value - return unless value.mutable_literal? + return unless value && value.mutable_literal? return if FROZEN_STRING_LITERAL_TYPES.include?(value.type) && frozen_string_literals_enabled?(processed_source) diff --git a/lib/rubocop/cop/style/negated_if.rb b/lib/rubocop/cop/style/negated_if.rb index 9e5e48a4ad91..bb214228b513 100644 --- a/lib/rubocop/cop/style/negated_if.rb +++ b/lib/rubocop/cop/style/negated_if.rb @@ -32,7 +32,7 @@ def autocorrect(node) lambda do |corrector| condition, _body, _rest = *node # look inside parentheses around the condition - condition = condition.children.first while condition.type == :begin + condition = condition.children.first while condition.begin_type? # unwrap the negated portion of the condition (a send node) pos_condition, _method, = *condition corrector.replace( diff --git a/lib/rubocop/cop/style/negated_while.rb b/lib/rubocop/cop/style/negated_while.rb index 23c4f16ca104..5fd0ef7f4592 100644 --- a/lib/rubocop/cop/style/negated_while.rb +++ b/lib/rubocop/cop/style/negated_while.rb @@ -19,7 +19,7 @@ def on_until(node) end def message(node) - if node.type == :while + if node.while_type? format(MSG, 'until', 'while') else format(MSG, 'while', 'until') @@ -32,12 +32,12 @@ def autocorrect(node) lambda do |corrector| condition, _body, _rest = *node # Look inside parentheses around the condition, if any. - condition, = *condition while condition.type == :begin + condition, = *condition while condition.begin_type? # Unwrap the negated portion of the condition (a send node). pos_condition, _method, = *condition corrector.replace( node.loc.keyword, - node.type == :while ? 'until' : 'while' + node.while_type? ? 'until' : 'while' ) corrector.replace(condition.source_range, pos_condition.source) end diff --git a/lib/rubocop/cop/style/non_nil_check.rb b/lib/rubocop/cop/style/non_nil_check.rb index 4776e929cb7b..53fe5c0bf891 100644 --- a/lib/rubocop/cop/style/non_nil_check.rb +++ b/lib/rubocop/cop/style/non_nil_check.rb @@ -57,18 +57,17 @@ def include_semantic_changes? def on_method_def(_node, name, _args, body) # only predicate methods are handled differently - return unless name.to_s.end_with?('?') - return unless body + return unless name.to_s.end_with?('?') && body - if body.type != :begin - ignore_node(body) - elsif body.type == :begin + if body.begin_type? ignore_node(body.children.last) + else + ignore_node(body) end end def nil_check?(node) - return false unless node && node.type == :send + return false unless node && node.send_type? _receiver, method, *_args = *node method == :nil? diff --git a/lib/rubocop/cop/style/op_method.rb b/lib/rubocop/cop/style/op_method.rb index 037216a3b30d..48247aa46e2d 100644 --- a/lib/rubocop/cop/style/op_method.rb +++ b/lib/rubocop/cop/style/op_method.rb @@ -31,7 +31,7 @@ def on_def(node) args.children.one? && !TARGET_ARGS.include?(args) - add_offense(args.children[0], :expression, format(MSG, name)) + add_offense(args.children.first, :expression, format(MSG, name)) end def op_method?(name) diff --git a/lib/rubocop/cop/style/parentheses_around_condition.rb b/lib/rubocop/cop/style/parentheses_around_condition.rb index 69e3e7a6069b..eb55643dd290 100644 --- a/lib/rubocop/cop/style/parentheses_around_condition.rb +++ b/lib/rubocop/cop/style/parentheses_around_condition.rb @@ -29,7 +29,7 @@ def on_until(node) def process_control_op(node) cond, _body = *node - return unless cond.type == :begin + return unless cond.begin_type? # handle `if (something rescue something_else) ...` return if modifier_op?(cond.children.first) # check if there's any whitespace between the keyword and the cond @@ -42,7 +42,7 @@ def process_control_op(node) def modifier_op?(node) return false if ternary?(node) - return true if node.type == :rescue + return true if node.rescue_type? [:if, :while, :until].include?(node.type) && node.loc.end.nil? diff --git a/lib/rubocop/cop/style/percent_literal_delimiters.rb b/lib/rubocop/cop/style/percent_literal_delimiters.rb index 7169ea940ff7..74d86e004b9a 100644 --- a/lib/rubocop/cop/style/percent_literal_delimiters.rb +++ b/lib/rubocop/cop/style/percent_literal_delimiters.rb @@ -76,7 +76,7 @@ def contains_preferred_delimiter?(node, type) def string_source(node) if node.is_a?(String) node - elsif node.respond_to?(:type) && node.type == :str + elsif node.respond_to?(:type) && node.str_type? node.source end end diff --git a/lib/rubocop/cop/style/raise_args.rb b/lib/rubocop/cop/style/raise_args.rb index 6cf666256a79..acf3ce6c4664 100644 --- a/lib/rubocop/cop/style/raise_args.rb +++ b/lib/rubocop/cop/style/raise_args.rb @@ -106,10 +106,10 @@ def check_compact(node) def check_exploded(node) _receiver, selector, *args = *node - if args.size == 1 + if args.one? arg, = *args - if arg.type == :send && arg.loc.selector.is?('new') + if arg.send_type? && arg.loc.selector.is?('new') _receiver, _selector, *constructor_args = *arg # Allow code like `raise Ex.new(arg1, arg2)`. diff --git a/lib/rubocop/cop/style/redundant_begin.rb b/lib/rubocop/cop/style/redundant_begin.rb index 24c81f776c30..76a1b06c7494 100644 --- a/lib/rubocop/cop/style/redundant_begin.rb +++ b/lib/rubocop/cop/style/redundant_begin.rb @@ -31,7 +31,7 @@ class RedundantBegin < Cop MSG = 'Redundant `begin` block detected.'.freeze def on_method_def(_node, _method_name, _args, body) - return unless body && body.type == :kwbegin + return unless body && body.kwbegin_type? add_offense(body, :begin) end diff --git a/lib/rubocop/cop/style/redundant_return.rb b/lib/rubocop/cop/style/redundant_return.rb index f006652ee711..9784b4716882 100644 --- a/lib/rubocop/cop/style/redundant_return.rb +++ b/lib/rubocop/cop/style/redundant_return.rb @@ -72,15 +72,15 @@ def arguments?(args) def on_method_def(_node, _method_name, _args, body) return unless body - if body.type == :return + if body.return_type? check_return_node(body) - elsif body.type == :begin + elsif body.begin_type? expressions = *body last_expr = expressions.last - if last_expr && last_expr.type == :return - check_return_node(last_expr) - end + return unless last_expr && last_expr.return_type? + + check_return_node(last_expr) end end diff --git a/lib/rubocop/cop/style/redundant_self.rb b/lib/rubocop/cop/style/redundant_self.rb index d996b64bfb49..f8a17c785397 100644 --- a/lib/rubocop/cop/style/redundant_self.rb +++ b/lib/rubocop/cop/style/redundant_self.rb @@ -139,10 +139,13 @@ def constant_name?(method_name) end def allow_self(node) - return unless node.type == :send + return unless node.send_type? receiver, _method_name, *_args = *node - @allowed_send_nodes << node if receiver && receiver.type == :self + + return unless receiver && receiver.self_type? + + @allowed_send_nodes << node end end end diff --git a/lib/rubocop/cop/style/regexp_literal.rb b/lib/rubocop/cop/style/regexp_literal.rb index 558671a0b126..78a30f8d077d 100644 --- a/lib/rubocop/cop/style/regexp_literal.rb +++ b/lib/rubocop/cop/style/regexp_literal.rb @@ -76,8 +76,7 @@ def allow_inner_slashes? end def node_body(node) - string_parts = node.children.select { |child| child.type == :str } - string_parts.map(&:source).join + node.each_child_node(:str).map(&:source).join end def slash_literal?(node) diff --git a/lib/rubocop/cop/style/self_assignment.rb b/lib/rubocop/cop/style/self_assignment.rb index 39b3a3a30247..3ef71a13b729 100644 --- a/lib/rubocop/cop/style/self_assignment.rb +++ b/lib/rubocop/cop/style/self_assignment.rb @@ -35,7 +35,7 @@ def check(node, var_type) var_name, rhs = *node return unless rhs - if rhs.type == :send + if rhs.send_type? check_send_node(node, rhs, var_name, var_type) elsif [:and, :or].include?(rhs.type) check_boolean_node(node, rhs, var_name, var_type) @@ -65,7 +65,7 @@ def check_boolean_node(node, rhs, var_name, var_type) def autocorrect(node) _var_name, rhs = *node - if rhs.type == :send + if rhs.send_type? autocorrect_send_node(node, rhs) elsif [:and, :or].include?(rhs.type) autocorrect_boolean_node(node, rhs) diff --git a/lib/rubocop/cop/style/single_line_block_params.rb b/lib/rubocop/cop/style/single_line_block_params.rb index f1d0fbf96325..8e7e3af14573 100644 --- a/lib/rubocop/cop/style/single_line_block_params.rb +++ b/lib/rubocop/cop/style/single_line_block_params.rb @@ -25,7 +25,7 @@ def on_block(node) return if args.empty? # discard cases with argument destructuring - return true unless args.all? { |n| n.type == :arg } + return true unless args.all?(&:arg_type?) return if args_match?(method_name, args) add_offense(args_node, :expression, message(method_name)) diff --git a/lib/rubocop/cop/style/single_line_methods.rb b/lib/rubocop/cop/style/single_line_methods.rb index 6dc094c37475..7027ee311923 100644 --- a/lib/rubocop/cop/style/single_line_methods.rb +++ b/lib/rubocop/cop/style/single_line_methods.rb @@ -52,7 +52,7 @@ def each_part(body) return unless body if body.begin_type? - body.children.each { |part| yield part.source_range } + body.each_child_node { |part| yield part.source_range } else yield body.source_range end diff --git a/lib/rubocop/cop/style/space_inside_string_interpolation.rb b/lib/rubocop/cop/style/space_inside_string_interpolation.rb index 4d95c78383e2..b4d6f17a419b 100644 --- a/lib/rubocop/cop/style/space_inside_string_interpolation.rb +++ b/lib/rubocop/cop/style/space_inside_string_interpolation.rb @@ -19,7 +19,7 @@ class SpaceInsideStringInterpolation < Cop SPACE_MSG = 'Missing space around string interpolation detected.'.freeze def on_dstr(node) - node.children.select { |n| n.type == :begin }.each do |begin_node| + node.each_child_node(:begin) do |begin_node| final_node = begin_node.children.last next unless final_node diff --git a/lib/rubocop/cop/style/stabby_lambda_parentheses.rb b/lib/rubocop/cop/style/stabby_lambda_parentheses.rb index a8391e717cec..44e7d72b13bd 100644 --- a/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +++ b/lib/rubocop/cop/style/stabby_lambda_parentheses.rb @@ -101,13 +101,11 @@ def node_args(node) end def args?(node) - args = node_args(node) - args.children.count > 0 + !node_args(node).children.empty? end def parentheses?(node) - args = node_args(node) - args.loc.begin + node_args(node).loc.begin end end end diff --git a/lib/rubocop/cop/style/symbol_proc.rb b/lib/rubocop/cop/style/symbol_proc.rb index 1283df27ec91..7449680e5f83 100644 --- a/lib/rubocop/cop/style/symbol_proc.rb +++ b/lib/rubocop/cop/style/symbol_proc.rb @@ -119,15 +119,15 @@ def ignored_method?(name) def can_shorten?(block_args, block_body) # something { |x, y| ... } - return false unless block_args.children.size == 1 + return false unless block_args.children.one? return false if non_shortenable_args?(block_args) - return false unless block_body && block_body.type == :send + return false unless block_body && block_body.send_type? receiver, _method_name, args = *block_body # method in block must be invoked on a lvar without args return false if args - return false unless receiver && receiver.type == :lvar + return false unless receiver && receiver.lvar_type? block_arg_name, = *block_args.children.first receiver_name, = *receiver diff --git a/lib/rubocop/cop/style/trivial_accessors.rb b/lib/rubocop/cop/style/trivial_accessors.rb index 86fed3ef75b4..8d477e05ec50 100644 --- a/lib/rubocop/cop/style/trivial_accessors.rb +++ b/lib/rubocop/cop/style/trivial_accessors.rb @@ -85,7 +85,7 @@ def trivial_reader?(method_name, args, body) end def looks_like_trivial_reader?(args, body) - args.children.empty? && body && body.type == :ivar + args.children.empty? && body && body.ivar_type? end def trivial_writer?(method_name, args, body) @@ -95,10 +95,10 @@ def trivial_writer?(method_name, args, body) end def looks_like_trivial_writer?(args, body) - args.children.size == 1 && + args.children.one? && ![:restarg, :blockarg].include?(args.children[0].type) && - body && body.type == :ivasgn && - body.children[1] && body.children[1].type == :lvar + body && body.ivasgn_type? && + body.children[1] && body.children[1].lvar_type? end def allowed_method?(method_name, body) @@ -134,9 +134,9 @@ def accessor(kind, method_name) end def autocorrect(node) - if node.type == :def + if node.def_type? autocorrect_instance(node) - elsif node.type == :defs && node.children.first.type == :self + elsif node.defs_type? && node.children.first.self_type? autocorrect_class(node) end end diff --git a/lib/rubocop/cop/style/unneeded_capital_w.rb b/lib/rubocop/cop/style/unneeded_capital_w.rb index 8fad0bb0656e..ebb08391063e 100644 --- a/lib/rubocop/cop/style/unneeded_capital_w.rb +++ b/lib/rubocop/cop/style/unneeded_capital_w.rb @@ -18,11 +18,15 @@ def on_array(node) private def on_percent_literal(node) - requires_interpolation = node.children.any? do |string| - string.type == :dstr || - double_quotes_acceptable?(string.str_content) + return if requires_interpolation?(node) + + add_offense(node, :expression) + end + + def requires_interpolation?(node) + node.child_nodes.any? do |string| + string.dstr_type? || double_quotes_acceptable?(string.str_content) end - add_offense(node, :expression) unless requires_interpolation end def autocorrect(node) diff --git a/lib/rubocop/cop/style/unneeded_interpolation.rb b/lib/rubocop/cop/style/unneeded_interpolation.rb index 9f24b9ac80b9..be9c0a5ef0fd 100644 --- a/lib/rubocop/cop/style/unneeded_interpolation.rb +++ b/lib/rubocop/cop/style/unneeded_interpolation.rb @@ -39,11 +39,11 @@ def single_variable_interpolation?(node) end def single_child?(node) - node.children.size == 1 + node.children.one? end def interpolation?(node) - variable_interpolation?(node) || node.type == :begin + variable_interpolation?(node) || node.begin_type? end def variable_interpolation?(node) @@ -51,12 +51,11 @@ def variable_interpolation?(node) end def implicit_concatenation?(node) - node.parent && node.parent.type == :dstr + node.parent && node.parent.dstr_type? end def embedded_in_percent_array?(node) - node.parent && - node.parent.type == :array && + node.parent && node.parent.array_type? && percent_literal?(node.parent) end diff --git a/lib/rubocop/cop/style/word_array.rb b/lib/rubocop/cop/style/word_array.rb index c30c3d448505..99fe20081b09 100644 --- a/lib/rubocop/cop/style/word_array.rb +++ b/lib/rubocop/cop/style/word_array.rb @@ -110,7 +110,7 @@ def autocorrect_words(word_nodes, escape, base_line_number) number_of_line_breaks = node.loc.line - previous_node_line_number line_breaks = "\n" * number_of_line_breaks previous_node_line_number = node.loc.line - content = node.children[0] + content = node.children.first content = escape ? escape_string(content) : content content.gsub!(/\)/, '\\)') line_breaks + content diff --git a/lib/rubocop/cop/util.rb b/lib/rubocop/cop/util.rb index 58ed4bda4806..38304e906006 100644 --- a/lib/rubocop/cop/util.rb +++ b/lib/rubocop/cop/util.rb @@ -81,10 +81,7 @@ def on_node(syms, sexp, excludes = [], &block) yield sexp if Array(syms).include?(sexp.type) return if Array(excludes).include?(sexp.type) - sexp.children.each do |elem| - next unless elem.is_a?(Parser::AST::Node) - on_node(syms, elem, excludes, &block) - end + sexp.each_child_node { |elem| on_node(syms, elem, excludes, &block) } end def source_range(source_buffer, line_number, column, length = 1) diff --git a/lib/rubocop/cop/variable_force.rb b/lib/rubocop/cop/variable_force.rb index 86fb027d1356..d1fa257bafa8 100644 --- a/lib/rubocop/cop/variable_force.rb +++ b/lib/rubocop/cop/variable_force.rb @@ -185,7 +185,7 @@ def process_variable_operator_assignment(node) asgn_node, _operator, rhs_node = *node end - return unless asgn_node.type == :lvasgn + return unless asgn_node.lvasgn_type? name = asgn_node.children.first @@ -264,7 +264,7 @@ def process_scope(node) if TWISTED_SCOPE_TYPES.include?(node.type) # See the comment at the end of file for this behavior. twisted_nodes = [node.children[0]] - twisted_nodes << node.children[1] if node.type == :class + twisted_nodes << node.children[1] if node.class_type? twisted_nodes.compact! twisted_nodes.each do |twisted_node| @@ -322,7 +322,7 @@ def find_variables_in_loop(loop_node) assignment_nodes_in_loop << node when *OPERATOR_ASSIGNMENT_TYPES asgn_node = node.children.first - if asgn_node.type == :lvasgn + if asgn_node.lvasgn_type? referenced_variable_names_in_loop << asgn_node.children.first end end diff --git a/lib/rubocop/cop/variable_force/locatable.rb b/lib/rubocop/cop/variable_force/locatable.rb index b7a4a7092318..4549762de36e 100644 --- a/lib/rubocop/cop/variable_force/locatable.rb +++ b/lib/rubocop/cop/variable_force/locatable.rb @@ -45,7 +45,7 @@ def run_exclusively_with?(other) # rescue # # resbody # end - if branch_point_node.type == :rescue && + if branch_point_node.rescue_type? && (branch_body_name == 'main' || other.branch_body_name == 'main') return false end @@ -109,7 +109,7 @@ def if_body_name end def case_body_name - if branch_body_node.type == :when + if branch_body_node.when_type? "when#{body_index - 1}" else 'else' diff --git a/lib/rubocop/cop/variable_force/variable.rb b/lib/rubocop/cop/variable_force/variable.rb index ab7d12209244..0c1c16837b93 100644 --- a/lib/rubocop/cop/variable_force/variable.rb +++ b/lib/rubocop/cop/variable_force/variable.rb @@ -87,7 +87,7 @@ def method_argument? end def block_argument? - argument? && @scope.node.type == :block + argument? && @scope.node.block_type? end def keyword_argument? @@ -95,7 +95,7 @@ def keyword_argument? end def explicit_block_local_variable? - @declaration_node.type == :shadowarg + @declaration_node.shadowarg_type? end end end diff --git a/lib/rubocop/cop/variable_force/variable_table.rb b/lib/rubocop/cop/variable_force/variable_table.rb index eafd4e562559..de5b23a704c3 100644 --- a/lib/rubocop/cop/variable_force/variable_table.rb +++ b/lib/rubocop/cop/variable_force/variable_table.rb @@ -99,7 +99,7 @@ def find_variable(name) variable = scope.variables[name] return variable if variable # Only block scope allows referencing outer scope variables. - return nil unless scope.node.type == :block + return nil unless scope.node.block_type? end nil @@ -112,14 +112,14 @@ def variable_exist?(name) def accessible_variables scope_stack.reverse_each.each_with_object([]) do |scope, variables| variables.concat(scope.variables.values) - break variables unless scope.node.type == :block + break variables unless scope.node.block_type? end end private def mark_variable_as_captured_by_block_if_so(variable) - return unless current_scope.node.type == :block + return unless current_scope.node.block_type? return if variable.scope == current_scope variable.capture_with_block! end