Class: RuboCop::Cop::Rails::SafeNavigation
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::SafeNavigation
- Extended by:
- AutoCorrector, TargetRubyVersion
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/rails/safe_navigation.rb
Overview
Converts usages of try! to &.. It can also be configured
to convert try. It will convert code to use safe navigation
if the target Ruby version is set to 2.3+
Constant Summary collapse
- MSG =
'Use safe navigation (`&.`) instead of `%<try>s`.'- RESTRICT_ON_SEND =
%i[try try!].freeze
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.autocorrect_incompatible_with ⇒ Object
69 70 71 |
# File 'lib/rubocop/cop/rails/safe_navigation.rb', line 69 def self.autocorrect_incompatible_with [Style::RedundantSelf] end |
Instance Method Details
#on_send(node) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rubocop/cop/rails/safe_navigation.rb', line 73 def on_send(node) try_call(node) do |try_method, dispatch| return if try_method == :try && !cop_config['ConvertTry'] return unless dispatch.sym_type? || symbol_proc_method(dispatch) # When a `try` is nested in another `try`'s argument, correcting both at once # produces overlapping replacements. Correct the outer one first and defer the # inner one to a subsequent pass. return if part_of_ignored_node?(node) add_offense(node, message: format(MSG, try: try_method)) do |corrector| autocorrect(corrector, node) end ignore_node(node) end end |