add rubocop cop to prevent the safe navigation operator

Close #596
This commit is contained in:
SengMing Tan 2018-11-24 17:41:09 -05:00 committed by Peter Bhat Harkins
parent 04753278b3
commit 0b6476e4ce
3 changed files with 36 additions and 2 deletions

View File

@ -1,4 +1,5 @@
# Project setup:
require: ./extras/rubocop/cop/style/disallow_safe_navigation
Rails:
Enabled: true
AllCops:

View File

@ -9,7 +9,7 @@ module UsersHelper
capture do
concat link_to(stories_displayed, "/newest/#{showing_user.username}")
concat(" (+#{stories_deleted} deleted)") if @user&.is_moderator? && stories_deleted > 0
concat(" (+#{stories_deleted} deleted)") if user_is_moderator? && stories_deleted > 0
if tag
concat ", most commonly tagged "
@ -24,9 +24,15 @@ module UsersHelper
capture do
concat link_to(showing_user.comments_posted_count, "/threads/#{showing_user.username}")
if @user&.is_moderator? && comments_deleted > 0
if user_is_moderator? && comments_deleted > 0
concat " (+#{comments_deleted} deleted)"
end
end
end
private
def user_is_moderator?
@user && @user.is_moderator?
end
end

View File

@ -0,0 +1,27 @@
module RuboCop
module Cop
module Style
# The "safe navigation" operator &. makes it easier to work with and
# propagate nil values. This will disallow the use of the safe navigation
# operator
#
# @example
#
# # bad
# foo&.bar
# a.foo&.bar
#
class DisallowSafeNavigation < Cop
extend TargetRubyVersion
MSG = 'Do not use &.'.freeze
minimum_target_ruby_version 2.3
def on_csend(node)
add_offense(node)
end
end
end
end
end