Class: RuboCop::Cop::Rails::EagerEvaluationLogMessage
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::EagerEvaluationLogMessage
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/rails/eager_evaluation_log_message.rb
Overview
This cop checks that blocks are used for interpolated strings passed to `Rails.logger.debug`.
By default, Rails production environments use the `:info` log level. At the `:info` log level, `Rails.logger.debug` statements do not result in log output. However, Ruby must eagerly evaluate interpolated string arguments passed as method arguments. Passing a block to `Rails.logger.debug` prevents costly evaluation of interpolated strings when no output would be produced anyway.
Constant Summary collapse
- MSG =
'Pass a block to `Rails.logger.debug`.'- RESTRICT_ON_SEND =
%i[debug].freeze
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rubocop/cop/rails/eager_evaluation_log_message.rb', line 40 def on_send(node) return if node.parent&.block_type? interpolated_string_passed_to_debug(node) do |arguments| = format(MSG) range = replacement_range(node) replacement = replacement_source(node, arguments) add_offense(range, message: ) do |corrector| corrector.replace(range, replacement) end end end |