Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style/StructInheritance の修正で直前行がコメントの場合まずいことになる #61

Closed
nabetani opened this issue Aug 3, 2020 · 2 comments
Labels

Comments

@nabetani
Copy link

nabetani commented Aug 3, 2020

環境

$ ruby -v
ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-darwin19]
$ rubocop -v
0.88.0

入力

# hoge
class Hoge < Struct.new(:foo, :bar)
  def fuga
    :piyo
  end
end

実行

$ rubocop -a hoge.rb
Inspecting 1 file
E

Offenses:

hoge.rb:2:14: C: [Corrected] Style/StructInheritance: Don't extend an instance initialized by Struct.new. Use a block to customize the struct.
class Hoge < Struct.new(:foo, :bar)
             ^^^^^^^^^^^^^^^^^^^^^^
hoge.rb:5:1: E: Lint/Syntax: unexpected token kEND
(Using Ruby 2.4 parser; configure using TargetRubyVersion parameter, under AllCops)
end
^^^

1 file inspected, 2 offenses detected, 1 offense corrected

結果

# hogeHoge = Struct.new(:foo, :bar) do
  def fuga
    :piyo
  end
end

補足

Hoge = Struct.new(:foo, :bar) do の前に改行が必要ですが、改行コードがないので Hoge = Struct.new(:foo, :bar) do がコメントの一部になってしまいます。

@koic
Copy link
Member

koic commented Aug 3, 2020

鍋谷さん、フィードバックありがとうございます!rubocop/rubocop#8443 として PR を開きました。

koic added a commit to koic/rubocop that referenced this issue Aug 3, 2020
This PR fixes the following incorrect auto-correct for `Style/StructInheritance`
incorrect auto-correct for `Style/StructInheritance` when there is a comment
before class declaration.

```console
% cat example.rb
# comment
class Foo < Struct.new(:foo)
end

% bundle exec rubocop --only Style/StructInheritance -a
(snip)

Offenses:

example.rb:2:13: C: [Corrected] Style/StructInheritance: Don't extend an
instance initialized by Struct.new. Use a block to customize the struct.
class Foo < Struct.new(:foo)
            ^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

% cat example.rb
# commentFoo = Struct.new(:foo) do
end
```

This issue has been reported on rubocop-jp. (Japanese)
rubocop/rubocop-jp#61
@koic
Copy link
Member

koic commented Aug 3, 2020

次のリリースとなる RuboCop 0.89 で修正が取り込まれる予定です。イシューとしてクローズします。

@koic koic closed this as completed Aug 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants