Class: RuboCop::Cop::Lint::DataDefineOverride

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/lint/data_define_override.rb

Overview

Checks unexpected overrides of the Data built-in methods via Data.define.

Examples:

# bad
Bad = Data.define(:members, :clone, :to_s)
b = Bad.new(members: [], clone: true, to_s: 'bad')
b.members #=> [] (overriding `Data#members`)
b.clone #=> true (overriding `Object#clone`)
b.to_s #=> "bad" (overriding `Data#to_s`)

# good
Good = Data.define(:id, :name)
g = Good.new(id: 1, name: "foo")
g.members #=> [:id, :name]
g.clone #=> #<data Good id=1, name="foo">

Constant Summary collapse

MSG =
'`%<member_name>s` member overrides `Data#%<method_name>s` and it may be unexpected.'
RESTRICT_ON_SEND =
%i[define].freeze
DATA_METHOD_NAMES =

This is based on Data.define.instance_methods.sort in Ruby 4.0.0.

%i[
  ! != !~ <=> == === __id__ __send__ class clone deconstruct deconstruct_keys
  define_singleton_method display dup enum_for eql? equal? extend freeze frozen? hash
  inspect instance_eval instance_exec instance_of? instance_variable_defined?
  instance_variable_get instance_variable_set instance_variables is_a? itself kind_of?
  members method methods nil? object_id private_methods protected_methods
  public_method public_methods public_send remove_instance_variable respond_to? send
  singleton_class singleton_method singleton_methods tap then to_enum to_h to_s with
  yield_self
].freeze
MEMBER_NAME_TYPES =
%i[sym str].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

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_gem_version, #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

#data_define(node) ⇒ Object



41
42
43
44
# File 'lib/rubocop/cop/lint/data_define_override.rb', line 41

def_node_matcher :data_define, <<~PATTERN
  (send
    (const {nil? cbase} :Data) :define ...)
PATTERN

#on_send(node) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/lint/data_define_override.rb', line 46

def on_send(node)
  return unless data_define(node)

  node.arguments.each do |arg|
    next unless MEMBER_NAME_TYPES.include?(arg.type)

    member_name = arg.value

    next unless DATA_METHOD_NAMES.include?(member_name.to_sym)

    message = format(MSG, member_name: member_name.inspect, method_name: member_name.to_s)
    add_offense(arg, message: message)
  end
end