Skip to content

Commit

Permalink
Make SendNode#macro? aware of struct constructor
Browse files Browse the repository at this point in the history
This PR makes `SendNode#macro?` aware of struct constructor.

Like class constructor, struct constructor will be recognized as a
macro and will be awakened in the following `private` modifier:

```ruby
Foo = Struct.new(:foo) do
  private
    def private_foo
      foo
    end
end
```

With this change, this PR aims to resolve the following issue.
rubocop/rubocop#8919
  • Loading branch information
koic committed Oct 22, 2020
1 parent b4af7eb commit 008a5c9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Changes

* [#141](https://github.com/rubocop-hq/rubocop-ast/pull/141): Make `SendNode#macro?` aware of struct constructor. ([@koic][])

## 1.0.0 (2020-10-21)

### Changes
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/ast/node/mixin/method_dispatch_node.rb
Expand Up @@ -229,6 +229,7 @@ def binary_operation?
root? # Either a root node,
^{ # or the parent is...
sclass class module class_constructor? # a class-like node
struct_constructor?
[ { # or some "wrapper"
kwbegin begin block
(if _condition <%0 _>) # note: we're excluding the condition of `if` nodes
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/ast/send_node_spec.rb
Expand Up @@ -242,6 +242,17 @@ module Foo
it { expect(send_node).to be_macro }
end

context 'when parent is a struct constructor' do
let(:source) do
['Foo = Struct.new do',
'>>bar :baz<<',
' bar :qux',
'end'].join("\n")
end

it { expect(send_node).to be_macro }
end

context 'when parent is a singleton class' do
let(:source) do
['class << self',
Expand Down

0 comments on commit 008a5c9

Please sign in to comment.