Skip to content

Commit

Permalink
Optimize forwarded calls
Browse files Browse the repository at this point in the history
Fixes [#312]
  • Loading branch information
marcandre committed Sep 1, 2024
1 parent d053335 commit a6d4701
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/rubocop/ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/mixin/collection_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node_pattern/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node_pattern/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node_pattern/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions lib/rubocop/ast/utilities/simple_forwardable.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a6d4701

Please sign in to comment.