Class: RuboCop::Cop::Rails::SafeNavigation
- Inherits:
-
RuboCop::Cop
- Object
- RuboCop::Cop
- RuboCop::Cop::Rails::SafeNavigation
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/rails/safe_navigation.rb
Overview
This cop converts usages of ‘try!` to `&.`. It can also be configured to convert `try`. It will convert code to use safe navigation.
Constant Summary collapse
- MSG =
'Use safe navigation (`&.`) instead of `%<try>s`.'
Instance Method Summary collapse
Instance Method Details
#autocorrect(node) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rubocop/cop/rails/safe_navigation.rb', line 57 def autocorrect(node) method_node, *params = *node.arguments method = method_node.source[1..-1] range = range_between(node.loc.dot.begin_pos, node.loc.expression.end_pos) lambda do |corrector| corrector.replace(range, replacement(method, params)) end end |
#on_send(node) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/rubocop/cop/rails/safe_navigation.rb', line 48 def on_send(node) try_call(node) do |try_method, dispatch| return if try_method == :try && !cop_config['ConvertTry'] return unless dispatch.sym_type? && dispatch.value.match?(/\w+[=!?]?/) add_offense(node, message: format(MSG, try: try_method)) end end |