diff --git a/lib/rubocop/ast.rb b/lib/rubocop/ast.rb index 098f91cf..c473150f 100644 --- a/lib/rubocop/ast.rb +++ b/lib/rubocop/ast.rb @@ -5,6 +5,7 @@ require 'set' require_relative 'ast/ext/range' +require_relative 'ast/utilities/simple_forwardable' require_relative 'ast/node_pattern/method_definer' require_relative 'ast/node_pattern' require_relative 'ast/node/mixin/descendence' diff --git a/lib/rubocop/ast/node/mixin/collection_node.rb b/lib/rubocop/ast/node/mixin/collection_node.rb index 0c2d55e5..43083f13 100644 --- a/lib/rubocop/ast/node/mixin/collection_node.rb +++ b/lib/rubocop/ast/node/mixin/collection_node.rb @@ -4,7 +4,7 @@ module RuboCop module AST # A mixin that helps give collection nodes array polymorphism. module CollectionNode - extend Forwardable + extend SimpleForwardable ARRAY_METHODS = (Array.instance_methods - Object.instance_methods - [:to_a]).freeze diff --git a/lib/rubocop/ast/node_pattern.rb b/lib/rubocop/ast/node_pattern.rb index ef3879be..d5ced7f9 100644 --- a/lib/rubocop/ast/node_pattern.rb +++ b/lib/rubocop/ast/node_pattern.rb @@ -48,7 +48,7 @@ def def_node_search(method_name, pattern_str, **keyword_defaults) end end - extend Forwardable + extend SimpleForwardable include MethodDefiner Invalid = Class.new(StandardError) diff --git a/lib/rubocop/ast/node_pattern/compiler.rb b/lib/rubocop/ast/node_pattern/compiler.rb index b7163346..7b6ba47e 100644 --- a/lib/rubocop/ast/node_pattern/compiler.rb +++ b/lib/rubocop/ast/node_pattern/compiler.rb @@ -9,7 +9,7 @@ class NodePattern # Doc on how this fits in the compiling process: # /docs/modules/ROOT/pages/node_pattern.adoc class Compiler - extend Forwardable + extend SimpleForwardable attr_reader :captures, :named_parameters, :positional_parameters, :binding def initialize diff --git a/lib/rubocop/ast/node_pattern/node.rb b/lib/rubocop/ast/node_pattern/node.rb index d98709fc..8645f9f0 100644 --- a/lib/rubocop/ast/node_pattern/node.rb +++ b/lib/rubocop/ast/node_pattern/node.rb @@ -5,7 +5,7 @@ module AST class NodePattern # Base class for AST Nodes of a `NodePattern` class Node < ::Parser::AST::Node - extend Forwardable + extend SimpleForwardable include ::RuboCop::AST::Descendence MATCHES_WITHIN_SET = %i[symbol number string].to_set.freeze diff --git a/lib/rubocop/ast/node_pattern/parser.rb b/lib/rubocop/ast/node_pattern/parser.rb index c1df451b..758eee79 100644 --- a/lib/rubocop/ast/node_pattern/parser.rb +++ b/lib/rubocop/ast/node_pattern/parser.rb @@ -11,7 +11,7 @@ class NodePattern # Doc on how this fits in the compiling process: # /docs/modules/ROOT/pages/node_pattern.adoc class Parser < Racc::Parser - extend Forwardable + extend SimpleForwardable Builder = NodePattern::Builder Lexer = NodePattern::Lexer diff --git a/lib/rubocop/ast/utilities/simple_forwardable.rb b/lib/rubocop/ast/utilities/simple_forwardable.rb new file mode 100644 index 00000000..168ede69 --- /dev/null +++ b/lib/rubocop/ast/utilities/simple_forwardable.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module RuboCop + # Similar to `Forwardable#def_delegators`, but simpler & faster + module SimpleForwardable + def def_delegators(accessor, *methods) + methods.each do |method| + class_eval(<<~RUBY, __FILE__, __LINE__ + 1) + def #{method}(...) # def example(...) + #{accessor}.#{method}(...) # foo.example(...) + end # end + RUBY + end + end + end +end