class RuboCop::Cop::Style::TrivialAccessors

Looks for trivial reader/writer methods, that could have been created with the attr_* family of functions automatically. ‘to_ary`, `to_a`, `to_c`, `to_enum`, `to_h`, `to_hash`, `to_i`, `to_int`, `to_io`, `to_open`, `to_path`, `to_proc`, `to_r`, `to_regexp`, `to_str`, `to_s`, and `to_sym` methods are allowed by default. These are customizable with `AllowedMethods` option.

@example

# bad
def foo
  @foo
end

def bar=(val)
  @bar = val
end

def self.baz
  @baz
end

# good
attr_reader :foo
attr_writer :bar

class << self
  attr_reader :baz
end

@example ExactNameMatch: true (default)

# good
def name
  @other_name
end

@example ExactNameMatch: false

# bad
def name
  @other_name
end

@example AllowPredicates: true (default)

# good
def foo?
  @foo
end

@example AllowPredicates: false

# bad
def foo?
  @foo
end

# good
attr_reader :foo

@example AllowDSLWriters: true (default)

# good
def on_exception(action)
  @on_exception=action
end

@example AllowDSLWriters: false

# bad
def on_exception(action)
  @on_exception=action
end

# good
attr_writer :on_exception

@example IgnoreClassMethods: false (default)

# bad
def self.foo
  @foo
end

# good
class << self
  attr_reader :foo
end

@example IgnoreClassMethods: true

# good
def self.foo
  @foo
end

@example AllowedMethods: [‘allowed_method’]

# good
def allowed_method
  @foo
end

Constants

MSG

Public Instance Methods

on_def(node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 104
def on_def(node)
  return if top_level_node?(node)
  return if in_module_or_instance_eval?(node)
  return if ignore_class_methods? && node.defs_type?

  on_method_def(node)
end
Also aliased as: on_defs
on_defs(node)
Alias for: on_def

Private Instance Methods

accessor(kind, method_name) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 222
def accessor(kind, method_name)
  "attr_#{kind} :#{method_name.to_s.chomp('=')}"
end
allow_dsl_writers?() click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 161
def allow_dsl_writers?
  cop_config['AllowDSLWriters']
end
allow_predicates?() click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 157
def allow_predicates?
  cop_config['AllowPredicates']
end
allowed_method_name?(node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 195
def allowed_method_name?(node)
  allowed_method_names.include?(node.method_name) ||
    (exact_name_match? && !names_match?(node))
end
allowed_method_names() click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 169
def allowed_method_names
  allowed_methods.map(&:to_sym) + [:initialize]
end
allowed_reader?(node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 204
def allowed_reader?(node)
  allow_predicates? && node.predicate_method?
end
allowed_writer?(node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 200
def allowed_writer?(node)
  allow_dsl_writers? && dsl_writer?(node)
end
autocorrect(corrector, node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 142
def autocorrect(corrector, node)
  parent = node.parent
  return if parent&.send_type?

  if node.def_type?
    autocorrect_instance(corrector, node)
  elsif node.defs_type? && node.children.first.self_type?
    autocorrect_class(corrector, node)
  end
end
autocorrect_class(corrector, node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 234
def autocorrect_class(corrector, node)
  kind = trivial_accessor_kind(node)

  return unless names_match?(node) && kind

  indent = ' ' * node.loc.column
  corrector.replace(
    node,
    ['class << self',
     "#{indent}  #{accessor(kind, node.method_name)}",
     "#{indent}end"].join("\n")
  )
end
autocorrect_instance(corrector, node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 226
def autocorrect_instance(corrector, node)
  kind = trivial_accessor_kind(node)

  return unless names_match?(node) && !node.predicate_method? && kind

  corrector.replace(node, accessor(kind, node.method_name))
end
dsl_writer?(node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 173
def dsl_writer?(node)
  !node.assignment_method?
end
exact_name_match?() click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 153
def exact_name_match?
  cop_config['ExactNameMatch']
end
ignore_class_methods?() click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 165
def ignore_class_methods?
  cop_config['IgnoreClassMethods']
end
in_module_or_instance_eval?(node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 115
def in_module_or_instance_eval?(node)
  node.each_ancestor(:block, :class, :sclass, :module).each do |pnode|
    case pnode.type
    when :class, :sclass
      return false
    when :module
      return true
    else
      return true if pnode.method?(:instance_eval)
    end
  end
  false
end
looks_like_trivial_reader?(node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 181
def looks_like_trivial_reader?(node)
  !node.arguments? && node.body&.ivar_type?
end
names_match?(node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 208
def names_match?(node)
  ivar_name, = *node.body

  node.method_name.to_s.sub(/[=?]$/, '') == ivar_name[1..]
end
on_method_def(node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 129
def on_method_def(node)
  kind = if trivial_reader?(node)
           'reader'
         elsif trivial_writer?(node)
           'writer'
         end
  return unless kind

  add_offense(node.loc.keyword, message: format(MSG, kind: kind)) do |corrector|
    autocorrect(corrector, node)
  end
end
top_level_node?(node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 248
def top_level_node?(node)
  node.parent.nil?
end
trivial_accessor_kind(node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 214
def trivial_accessor_kind(node)
  if trivial_writer?(node) && !dsl_writer?(node)
    'writer'
  elsif trivial_reader?(node)
    'reader'
  end
end
trivial_reader?(node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 177
def trivial_reader?(node)
  looks_like_trivial_reader?(node) && !allowed_method_name?(node) && !allowed_reader?(node)
end
trivial_writer?(node) click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 185
def trivial_writer?(node)
  looks_like_trivial_writer?(node) && !allowed_method_name?(node) && !allowed_writer?(node)
end