Skip to content

Commit

Permalink
[Fix #360] Fix an error when the Array core class contains a writer…
Browse files Browse the repository at this point in the history
… method

This is a syntax error:
```
def foo=(...)
end
```

So it must use `public_send` instead in that case. It's caused by https://github.com/rubocop/rubocop-ast/blob/47e0eead4b84c3d28d7251a548e152f914219773/lib/rubocop/ast/node/mixin/collection_node.rb#L9-L10

By default the `Array` class doesn't contain such methods but when something like spring
is in use, code can be evaluated before rubocop.

I thought about simply rejecting such methods but supporting it is easy enough
  • Loading branch information
Earlopain authored and marcandre committed Feb 25, 2025
1 parent 47e0eea commit 5459734
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/fix_array_writer_method.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#360](https://github.com/rubocop/rubocop-ast/pull/360): Fix an error when the `Array` core class contains a writer method before `rubocop-ast` loaded. ([@earlopain][])
21 changes: 16 additions & 5 deletions lib/rubocop/ast/utilities/simple_forwardable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ module RuboCop
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
if method.end_with?('=') && method.to_s != '[]='
# Defining a delegator for `foo=` can't use `foo=(...)` because it is a
# syntax error. Fall back to doing a slower `public_send` instead.
# TODO: Use foo(method, ...) when Ruby 3.1 is required.
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{method}(*args, **kwargs, &blk) # def example=(*args, **kwargs, &blk)
#{accessor}.public_send(:#{method}, *args, **kwargs, &blk) # foo.public_send(:example=, *args, **kwargs, &blk)
end # end
RUBY
else
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{method}(...) # def example(...)
#{accessor}.#{method}(...) # foo.example(...)
end # end
RUBY
end
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/rubocop/simple_forwardable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

RSpec.describe RuboCop::SimpleForwardable do
let(:delegator) do
Class.new do
attr_reader :target

def initialize
@target = Struct.new(:foo).new
end

extend RuboCop::SimpleForwardable

def_delegators :target, :foo=, :foo
end
end

it 'correctly delegates to writer methods' do
d = delegator.new
d.foo = 123
expect(d.foo).to eq(123)
end
end

4 comments on commit 5459734

@ahoward
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is still going to fail for both 'foo?' and 'foo!', at minimum. #fyi

@Earlopain
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested these and it seemed to work in all rubies that support ... delegation. Do you have a concrete example I can try out?

@ahoward
Copy link

@ahoward ahoward commented on 5459734 Feb 25, 2025 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Earlopain
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, thanks for double-checking! The rules about writer methods are pretty weird.

Please sign in to comment.