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

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

Overview

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.

NOTE: 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:

source,ruby

code = <<-RUBY

def do_something
end

RUBY eval code # not checked.


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

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

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, 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?, #autocorrect_with_disable_uncorrectable?, #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



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

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



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

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.first_argument
  return unless code && (code.str_type? || code.dstr_type?)

  check_location(node, code)
end

#valid_eval_receiver?(node) ⇒ Object



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

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