Class: RuboCop::Cop::Style::EvalWithLocation

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/style/eval_with_location.rb

Overview

This cop ensures that eval methods (`eval`, `instance_eval`, `class_eval` and `module_eval`) are given filename and line number values (`__FILE__` and `__LINE__`). This data is used to ensure that any errors raised within the evaluated code will be given the correct identification in a backtrace.

The cop also checks that the line number given relative to `__LINE__` is correct.

This cop will autocorrect incorrect or missing filename and line number values. However, if `eval` is called without a binding argument, the cop will not attempt to automatically add a binding, or add filename and line values.

This cop works only when a string literal is given as a code string. No offense is reported if a string variable is given as below:

Examples:

# bad
eval <<-RUBY
  def do_something
  end
RUBY

# bad
C.class_eval <<-RUBY
  def do_something
  end
RUBY

# good
eval <<-RUBY, binding, __FILE__, __LINE__ + 1
  def do_something
  end
RUBY

# good
C.class_eval <<-RUBY, __FILE__, __LINE__ + 1
  def do_something
  end
RUBY
# not checked
code = <<-RUBY
  def do_something
  end
RUBY
eval code

Constant Summary collapse

MSG =
'Pass `__FILE__` and `__LINE__` to `%<method_name>s`.'
MSG_EVAL =
'Pass a binding, `__FILE__` and `__LINE__` to `eval`.'
MSG_INCORRECT_FILE =
'Incorrect file for `%<method_name>s`; ' \
'use `%<expected>s` instead of `%<actual>s`.'
MSG_INCORRECT_LINE =
'Incorrect line number for `%<method_name>s`; ' \
'use `%<expected>s` instead of `%<actual>s`.'
RESTRICT_ON_SEND =
%i[eval class_eval module_eval instance_eval].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#line_with_offset?(node, sign, num) ⇒ Object


74
75
76
77
78
79
# File 'lib/rubocop/cop/style/eval_with_location.rb', line 74

def_node_matcher :line_with_offset?, <<~PATTERN
  {
    (send #special_line_keyword? %1 (int %2))
    (send (int %2) %1 #special_line_keyword?)
  }
PATTERN

#on_send(node) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubocop/cop/style/eval_with_location.rb', line 81

def on_send(node)
  # Classes should not redefine eval, but in case one does, it shouldn't
  # register an offense. Only `eval` without a receiver and `Kernel.eval`
  # are considered.
  return if node.method?(:eval) && !valid_eval_receiver?(node.receiver)

  code = node.arguments.first
  return unless code && (code.str_type? || code.dstr_type?)

  check_location(node, code)
end

#valid_eval_receiver?(node) ⇒ Object


69
70
71
# File 'lib/rubocop/cop/style/eval_with_location.rb', line 69

def_node_matcher :valid_eval_receiver?, <<~PATTERN
  { nil? (const {nil? cbase} :Kernel) }
PATTERN