From 11ef5c05453eb1b2c1b70893a7a5769c55a22916 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Sat, 1 Aug 2020 15:13:42 -0400 Subject: [PATCH] Refactor WrappedArgumentsNode => ParameterizedNode::WrappedArguments --- lib/rubocop/ast.rb | 1 - lib/rubocop/ast/node/break_node.rb | 3 +-- .../ast/node/mixin/parameterized_node.rb | 16 ++++++++++++++++ .../ast/node/mixin/wrapped_arguments_node.rb | 19 ------------------- lib/rubocop/ast/node/next_node.rb | 3 +-- lib/rubocop/ast/node/return_node.rb | 3 +-- 6 files changed, 19 insertions(+), 26 deletions(-) delete mode 100644 lib/rubocop/ast/node/mixin/wrapped_arguments_node.rb diff --git a/lib/rubocop/ast.rb b/lib/rubocop/ast.rb index dd9f6bcff..db59188bd 100644 --- a/lib/rubocop/ast.rb +++ b/lib/rubocop/ast.rb @@ -18,7 +18,6 @@ require_relative 'ast/node/mixin/parameterized_node' require_relative 'ast/node/mixin/predicate_operator_node' require_relative 'ast/node/mixin/basic_literal_node' -require_relative 'ast/node/mixin/wrapped_arguments_node' require_relative 'ast/node/alias_node' require_relative 'ast/node/and_node' require_relative 'ast/node/args_node' diff --git a/lib/rubocop/ast/node/break_node.rb b/lib/rubocop/ast/node/break_node.rb index 5e1e14396..447b4cb63 100644 --- a/lib/rubocop/ast/node/break_node.rb +++ b/lib/rubocop/ast/node/break_node.rb @@ -6,8 +6,7 @@ module AST # plain node when the builder constructs the AST, making its methods # available to all `break` nodes within RuboCop. class BreakNode < Node - include ParameterizedNode - include WrappedArgumentsNode + include ParameterizedNode::WrappedArguments end end end diff --git a/lib/rubocop/ast/node/mixin/parameterized_node.rb b/lib/rubocop/ast/node/mixin/parameterized_node.rb index 4e6e04917..0a08cfa34 100644 --- a/lib/rubocop/ast/node/mixin/parameterized_node.rb +++ b/lib/rubocop/ast/node/mixin/parameterized_node.rb @@ -60,6 +60,22 @@ def block_argument? (last_argument.block_pass_type? || last_argument.blockarg_type?) end + # A specialized `ParameterizedNode` for node that have a single child + # containing either `nil`, an argument, or a `begin` node with all the + # arguments + module WrappedArguments + include ParameterizedNode + # @return [Array] The arguments of the node. + def arguments + first = children.first + if first&.begin_type? + first.children + else + children + end + end + end + # A specialized `ParameterizedNode`. # Requires implementing `first_argument_index` # Implements `arguments` as `children[first_argument_index..-1]` diff --git a/lib/rubocop/ast/node/mixin/wrapped_arguments_node.rb b/lib/rubocop/ast/node/mixin/wrapped_arguments_node.rb deleted file mode 100644 index 56e3b10b1..000000000 --- a/lib/rubocop/ast/node/mixin/wrapped_arguments_node.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module RuboCop - module AST - # Common functionality for nodes that may have their arguments - # wrapped in a `begin` node - module WrappedArgumentsNode - # @return [Array] The arguments of the node. - def arguments - first = children.first - if first&.begin_type? - first.children - else - children - end - end - end - end -end diff --git a/lib/rubocop/ast/node/next_node.rb b/lib/rubocop/ast/node/next_node.rb index 1f7b9dcc1..871561311 100644 --- a/lib/rubocop/ast/node/next_node.rb +++ b/lib/rubocop/ast/node/next_node.rb @@ -6,8 +6,7 @@ module AST # plain node when the builder constructs the AST, making its methods # available to all `next` nodes within RuboCop. class NextNode < Node - include ParameterizedNode - include WrappedArgumentsNode + include ParameterizedNode::WrappedArguments end end end diff --git a/lib/rubocop/ast/node/return_node.rb b/lib/rubocop/ast/node/return_node.rb index a8d617f88..a3495e646 100644 --- a/lib/rubocop/ast/node/return_node.rb +++ b/lib/rubocop/ast/node/return_node.rb @@ -6,8 +6,7 @@ module AST # plain node when the builder constructs the AST, making its methods # available to all `return` nodes within RuboCop. class ReturnNode < Node - include ParameterizedNode - include WrappedArgumentsNode + include ParameterizedNode::WrappedArguments end end end