Class: RuboCop::Cop::Style::TrivialAccessors
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::RuboCop::Cop::AutoCorrector ,
::RuboCop::Cop::Base ,
::RuboCop::ExcludeLimit ,
NodePattern::Macros,
RuboCop::AST::Sexp
|
|
Instance Chain:
self,
::RuboCop::Cop::AllowedMethods ,
::RuboCop::Cop::Base ,
::RuboCop::Cop::AutocorrectLogic ,
::RuboCop::Cop::IgnoredNode ,
::RuboCop::Util ,
RuboCop::AST::Sexp
|
|
Inherits: |
RuboCop::Cop::Base
|
Defined in: | lib/rubocop/cop/style/trivial_accessors.rb |
Overview
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 ::RuboCop::Cop::AllowedMethods
option.
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 102'Use `attr_%<kind>s` to define trivial %<kind>s methods.'
::RuboCop::Cop::Base
- Inherited
Class Attribute Summary
::RuboCop::Cop::AutoCorrector
- Extended
::RuboCop::Cop::Base
- Inherited
.gem_requirements, .lint?, | |
.support_autocorrect? | Returns if class supports autocorrect. |
.support_multiple_source? | Override if your cop should be called repeatedly for multiple investigations Between calls to |
Class Method Summary
::RuboCop::Cop::Base
- Inherited
.autocorrect_incompatible_with | List of cops that should not try to autocorrect at the same time as this cop. |
.badge | Naming. |
.callbacks_needed, .cop_name, .department, | |
.documentation_url | Returns a url to view this cops documentation online. |
.exclude_from_registry | Call for abstract Cop classes. |
.inherited, | |
.joining_forces | Override and return the Force class(es) you need to join. |
.match? | Returns true if the cop name or the cop namespace matches any of the given names. |
.new, | |
.requires_gem | Register a version requirement for the given gem name. |
.restrict_on_send |
::RuboCop::ExcludeLimit
- Extended
exclude_limit | Sets up a configuration option to have an exclude limit tracked. |
transform |
Instance Attribute Summary
- #allow_dsl_writers? ⇒ Boolean readonly private
- #allow_predicates? ⇒ Boolean readonly private
- #exact_name_match? ⇒ Boolean readonly private
- #ignore_class_methods? ⇒ Boolean readonly private
::RuboCop::Cop::AllowedMethods
- Included
::RuboCop::Cop::Base
- Inherited
::RuboCop::Cop::AutocorrectLogic
- Included
Instance Method Summary
- #on_def(node) (also: #on_defs)
-
#on_defs(node)
Alias for #on_def.
- #accessor(kind, method_name) private
- #allowed_method_name?(node) ⇒ Boolean private
- #allowed_method_names private
- #allowed_reader?(node) ⇒ Boolean private
- #allowed_writer?(node) ⇒ Boolean private
- #autocorrect(corrector, node) private
- #autocorrect_class(corrector, node) private
- #autocorrect_instance(corrector, node) private
- #dsl_writer?(node) ⇒ Boolean private
- #in_module_or_instance_eval?(node) ⇒ Boolean private
- #looks_like_trivial_reader?(node) ⇒ Boolean private
- #looks_like_trivial_writer?(node) private
- #names_match?(node) ⇒ Boolean private
- #on_method_def(node) private
- #top_level_node?(node) ⇒ Boolean private
- #trivial_accessor_kind(node) private
- #trivial_reader?(node) ⇒ Boolean private
- #trivial_writer?(node) ⇒ Boolean private
::RuboCop::Cop::AllowedMethods
- Included
::RuboCop::Cop::Base
- Inherited
#add_global_offense | Adds an offense that has no particular location. |
#add_offense | Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense. |
#begin_investigation | Called before any investigation. |
#callbacks_needed, | |
#cop_config | Configuration Helpers. |
#cop_name, #excluded_file?, | |
#external_dependency_checksum | This method should be overridden when a cop’s behavior depends on state that lives outside of these locations: |
#inspect, | |
#message | Gets called if no message is specified when calling |
#name | Alias for Base#cop_name. |
#offenses, | |
#on_investigation_end | Called after all on_… |
#on_new_investigation | Called before all on_… |
#on_other_file | Called instead of all on_… |
#parse | There should be very limited reasons for a Cop to do it’s own parsing. |
#parser_engine, | |
#ready | Called between investigations. |
#relevant_file?, #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction, | |
#callback_argument | Reserved for Cop::Cop. |
#complete_investigation | Called to complete an investigation. |
#correct, #current_corrector, | |
#current_offense_locations | Reserved for Commissioner: |
#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range, | |
#reset_investigation | Actually private methods. |
#use_corrector |
::RuboCop::Cop::AutocorrectLogic
- Included
#disable_offense, #disable_offense_at_end_of_line, #disable_offense_before_and_after, #disable_offense_with_eol_or_surround_comment, #max_line_length, | |
#range_by_lines | Expand the given range to include all of any lines it covers. |
#range_of_first_line, #range_overlaps_offense?, #string_continuation, #string_continuation?, #surrounding_heredoc, #surrounding_percent_array |
::RuboCop::Cop::IgnoredNode
- Included
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Attribute Details
#allow_dsl_writers? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 161
def allow_dsl_writers? cop_config['AllowDSLWriters'] end
#allow_predicates? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 157
def allow_predicates? cop_config['AllowPredicates'] end
#exact_name_match? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 153
def exact_name_match? cop_config['ExactNameMatch'] end
#ignore_class_methods? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 165
def ignore_class_methods? cop_config['IgnoreClassMethods'] end
Instance Method Details
#accessor(kind, method_name) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 222
def accessor(kind, method_name) "attr_#{kind} :#{method_name.to_s.chomp('=')}" end
#allowed_method_name?(node) ⇒ Boolean
(private)
# 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 (private)
[ GitHub ]# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 169
def allowed_method_names allowed_methods.map(&:to_sym) + [:initialize] end
#allowed_reader?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 204
def allowed_reader?(node) allow_predicates? && node.predicate_method? end
#allowed_writer?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 200
def allowed_writer?(node) allow_dsl_writers? && dsl_writer?(node) end
#autocorrect(corrector, node) (private)
[ GitHub ]# 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) (private)
[ GitHub ]# 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) (private)
[ GitHub ]# 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) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 173
def dsl_writer?(node) !node.assignment_method? end
#in_module_or_instance_eval?(node) ⇒ Boolean
(private)
# 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) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 181
def looks_like_trivial_reader?(node) !node.arguments? && node.body&.ivar_type? end
#looks_like_trivial_writer?(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 190
def_node_matcher :looks_like_trivial_writer?, <<~PATTERN {(def _ (args (arg ...)) (ivasgn _ (lvar _))) (defs _ _ (args (arg ...)) (ivasgn _ (lvar _)))} PATTERN
#names_match?(node) ⇒ Boolean
(private)
# 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_def(node) Also known as: #on_defs
[ GitHub ]# 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
#on_defs(node)
Alias for #on_def.
# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 111
alias on_defs on_def
#on_method_def(node) (private)
[ GitHub ]# 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) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/trivial_accessors.rb', line 248
def top_level_node?(node) node.parent.nil? end
#trivial_accessor_kind(node) (private)
[ GitHub ]# 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) ⇒ Boolean
(private)
# 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) ⇒ Boolean
(private)
# 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