From 4fc1057104cf0201c3ab14ff1f3aff9c1bce3cf3 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 22 Oct 2020 19:03:51 +0900 Subject: [PATCH] Support struct constructor for `SendNode#macro?` This PR supports struct constructor for `SendNode#macro?`. 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. https://github.com/rubocop-hq/rubocop/issues/8919 --- CHANGELOG.md | 4 ++++ lib/rubocop/ast/node/mixin/method_dispatch_node.rb | 1 + spec/rubocop/ast/send_node_spec.rb | 11 +++++++++++ 3 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b913e2745..57b9f39a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Changes + +* [#141](https://github.com/rubocop-hq/rubocop-ast/pull/141): Support struct constructor for `SendNode#macro?`. ([@koic][]) + ## 1.0.0 (2020-10-21) ### Changes diff --git a/lib/rubocop/ast/node/mixin/method_dispatch_node.rb b/lib/rubocop/ast/node/mixin/method_dispatch_node.rb index 8e9a2c881..f1fc7f458 100644 --- a/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +++ b/lib/rubocop/ast/node/mixin/method_dispatch_node.rb @@ -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 diff --git a/spec/rubocop/ast/send_node_spec.rb b/spec/rubocop/ast/send_node_spec.rb index 7c8d7504c..86e269fe3 100644 --- a/spec/rubocop/ast/send_node_spec.rb +++ b/spec/rubocop/ast/send_node_spec.rb @@ -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',