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

params[:foo].something を怒るCop #4

Open
pocke opened this issue Nov 12, 2017 · 0 comments
Open

params[:foo].something を怒るCop #4

pocke opened this issue Nov 12, 2017 · 0 comments

Comments

@pocke
Copy link
Contributor

pocke commented Nov 12, 2017

# In controller
def index
  params[:foo].something
end

これは、paramsに値が入ってこなかったらエラーになって死ぬ。
なので、params.require(:foo).somethingparams[:foo]&.something になるべき。
(ただしnilに生えているメソッドは呼べる)

実装は多分簡単。

nil に生えているメソッドを呼び出しているケース(例えば、params[:foo].nil?)の対応は、 Lint/SafeNavigationChain とかを見ると良いと思う。 https://github.com/bbatsov/rubocop/blob/245155158db2d4e0d3d0da918d3e7f3bf6291e78/lib/rubocop/cop/lint/safe_navigation_chain.rb#L40
この nil に生えているメソッドのリストをいい感じに共通化したいけど、現状のRuboCopにはCop感をまたがるような設定はあまりいい感じに出来ないので、ちょっとむずかしい。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant