class RuboCop::Cop::Style::TrivialAccessors
This cop looks for trivial reader/writer methods, that could have been created with the attr_* family of functions automatically.
Constants
- MSG
Public Instance Methods
on_def(node)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 11 def on_def(node) return if in_module_or_instance_eval?(node) method_name, args, body = *node on_method_def(node, method_name, args, body) end
on_defs(node)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 17 def on_defs(node) return if in_module_or_instance_eval?(node) return if ignore_class_methods? _scope, method_name, args, body = *node on_method_def(node, method_name, args, body) end
Private Instance Methods
accessor(kind, method_name)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 131 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 59 def allow_dsl_writers? cop_config['AllowDSLWriters'] end
allow_predicates?()
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 55 def allow_predicates? cop_config['AllowPredicates'] end
allowed_method?(method_name, body)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 103 def allowed_method?(method_name, body) whitelist.include?(method_name) || exact_name_match? && !names_match?(method_name, body) end
allowed_reader?(method_name)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 112 def allowed_reader?(method_name) allow_predicates? && predicate?(method_name) end
allowed_writer?(method_name)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 108 def allowed_writer?(method_name) allow_dsl_writers? && dsl_writer?(method_name) end
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 135 def autocorrect(node) if node.type == :def autocorrect_instance(node) elsif node.type == :defs && node.children.first.type == :self autocorrect_class(node) end end
autocorrect_class(node)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 156 def autocorrect_class(node) _, method_name, args, body = *node unless names_match?(method_name, body) && (kind = trivial_accessor_kind(method_name, args, body)) return end lambda do |corrector| indent = ' ' * node.loc.column corrector.replace( node.source_range, ['class << self', "#{indent} #{accessor(kind, method_name)}", "#{indent}end"].join("\n") ) end end
autocorrect_instance(node)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 143 def autocorrect_instance(node) method_name, args, body = *node unless names_match?(method_name, body) && !predicate?(method_name) && (kind = trivial_accessor_kind(method_name, args, body)) return end lambda do |corrector| corrector.replace(node.source_range, accessor(kind, method_name)) end end
dsl_writer?(method_name)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 76 def dsl_writer?(method_name) !method_name.to_s.end_with?('=') end
exact_name_match?()
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 51 def exact_name_match? cop_config['ExactNameMatch'] end
ignore_class_methods?()
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 63 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 26 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_name == :instance_eval end end false end
looks_like_trivial_reader?(args, body)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 86 def looks_like_trivial_reader?(args, body) args.children.empty? && body && body.type == :ivar end
looks_like_trivial_writer?(args, body)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 96 def looks_like_trivial_writer?(args, body) args.children.size == 1 && ![:restarg, :blockarg].include?(args.children[0].type) && body && body.type == :ivasgn && body.children[1] && body.children[1].type == :lvar end
names_match?(method_name, body)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 116 def names_match?(method_name, body) ivar_name, = *body method_name.to_s.sub(/[=?]$/, '') == ivar_name[1..-1] end
on_method_def(node, method_name, args, body)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 40 def on_method_def(node, method_name, args, body) kind = if trivial_reader?(method_name, args, body) 'reader' elsif trivial_writer?(method_name, args, body) 'writer' end return unless kind add_offense(node, :keyword, format(MSG, kind, kind)) end
predicate?(method_name)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 72 def predicate?(method_name) method_name[-1] == '?' end
trivial_accessor_kind(method_name, args, body)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 122 def trivial_accessor_kind(method_name, args, body) if trivial_writer?(method_name, args, body) && !dsl_writer?(method_name) 'writer' elsif trivial_reader?(method_name, args, body) 'reader' end end
trivial_reader?(method_name, args, body)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 80 def trivial_reader?(method_name, args, body) looks_like_trivial_reader?(args, body) && !allowed_method?(method_name, body) && !allowed_reader?(method_name) end
trivial_writer?(method_name, args, body)
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 90 def trivial_writer?(method_name, args, body) looks_like_trivial_writer?(args, body) && !allowed_method?(method_name, body) && !allowed_writer?(method_name) end
whitelist()
click to toggle source
# File lib/rubocop/cop/style/trivial_accessors.rb, line 67 def whitelist whitelist = cop_config['Whitelist'] Array(whitelist).map(&:to_sym) + [:initialize] end