class RuboCop::Cop::Style::TrivialAccessors
end
@foo
def allowed_method
# good
@example AllowedMethods: [‘allowed_method’]
end
@foo
def self.foo
# good
@example IgnoreClassMethods: true
end
attr_reader :foo
class << self
# good
end
@foo
def self.foo
# bad
@example IgnoreClassMethods: false (default)
attr_writer :on_exception
# good
end
@on_exception=action
def on_exception(action)
# bad
@example AllowDSLWriters: false
end
@on_exception=action
def on_exception(action)
# good
@example AllowDSLWriters: true (default)
attr_reader :foo
# good
end
@foo
def foo?
# bad
@example AllowPredicates: false
end
@foo
def foo?
# good
@example AllowPredicates: true (default)
end
@other_name
def name
# bad
@example ExactNameMatch: false
end
@other_name
def name
# good
@example ExactNameMatch: true (default)
end
attr_reader :baz
class << self
attr_writer :bar
attr_reader :foo
# good
end
@baz
def self.baz
end
@bar = val
def bar=(val)
end
@foo
def foo
# bad
@example
are allowed by default. These are customizable with ‘AllowedMethods` option.
`to_open`, `to_path`, `to_proc`, `to_r`, `to_regexp`, `to_str`, `to_s`, and `to_sym` methods
`to_ary`, `to_a`, `to_c`, `to_enum`, `to_h`, `to_hash`, `to_i`, `to_int`, `to_io`,
have been created with the attr_* family of functions automatically.
Looks for trivial reader/writer methods, that could
def accessor(kind, method_name)
def accessor(kind, method_name) "attr_#{kind} :#{method_name.to_s.chomp('=')}" end
def allow_dsl_writers?
def allow_dsl_writers? cop_config['AllowDSLWriters'] end
def allow_predicates?
def allow_predicates? cop_config['AllowPredicates'] end
def allowed_method_name?(node)
def allowed_method_name?(node) allowed_method_names.include?(node.method_name) || (exact_name_match? && !names_match?(node)) end
def allowed_method_names
def allowed_method_names allowed_methods.map(&:to_sym) + [:initialize] end
def allowed_reader?(node)
def allowed_reader?(node) allow_predicates? && node.predicate_method? end
def allowed_writer?(node)
def allowed_writer?(node) allow_dsl_writers? && dsl_writer?(node) end
def autocorrect(corrector, node)
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
def autocorrect_class(corrector, node)
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
def autocorrect_instance(corrector, node)
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
def dsl_writer?(node)
def dsl_writer?(node) !node.assignment_method? end
def exact_name_match?
def exact_name_match? cop_config['ExactNameMatch'] end
def ignore_class_methods?
def ignore_class_methods? cop_config['IgnoreClassMethods'] end
def in_module_or_instance_eval?(node)
def in_module_or_instance_eval?(node) node.each_ancestor(:any_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
def looks_like_trivial_reader?(node)
def looks_like_trivial_reader?(node) !node.arguments? && node.body&.ivar_type? end
def names_match?(node)
def names_match?(node) ivar_name, = *node.body node.method_name.to_s.sub(/[=?]$/, '') == ivar_name[1..] end
def on_def(node)
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
def on_method_def(node)
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
def top_level_node?(node)
def top_level_node?(node) node.parent.nil? end
def trivial_accessor_kind(node)
def trivial_accessor_kind(node) if trivial_writer?(node) && !dsl_writer?(node) 'writer' elsif trivial_reader?(node) 'reader' end end
def trivial_reader?(node)
def trivial_reader?(node) looks_like_trivial_reader?(node) && !allowed_method_name?(node) && !allowed_reader?(node) end
def trivial_writer?(node)
def trivial_writer?(node) looks_like_trivial_writer?(node) && !allowed_method_name?(node) && !allowed_writer?(node) end