Class: RuboCop::Cop::Layout::EmptyLinesAroundAccessModifier
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:
|
|
Inherits: |
RuboCop::Cop::Base
|
Defined in: | lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb |
Overview
Access modifiers should be surrounded by blank lines.
Constant Summary
-
MSG_AFTER =
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 48'Keep a blank line after `%<modifier>s`.'
-
MSG_AFTER_FOR_ONLY_BEFORE =
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 52'Remove a blank line after `%<modifier>s`.'
-
MSG_BEFORE_AND_AFTER =
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 49'Keep a blank line before and after `%<modifier>s`.'
-
MSG_BEFORE_FOR_ONLY_BEFORE =
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 51'Keep a blank line before `%<modifier>s`.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 54%i[public protected private module_function].freeze
::RuboCop::Cop::Base
- Inherited
EMPTY_OFFENSES, RESTRICT_ON_SEND
::RuboCop::Cop::ConfigurableEnforcedStyle
- Included
::RuboCop::Cop::RangeHelp
- Included
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
::RuboCop::Cop::ConfigurableEnforcedStyle
- Included
::RuboCop::Cop::Base
- Inherited
::RuboCop::Cop::AutocorrectLogic
- Included
Instance Method Summary
- #on_block(node) (also: #on_numblock)
- #on_class(node)
- #on_module(node)
-
#on_numblock(node)
Alias for #on_block.
- #on_sclass(node)
- #on_send(node)
- #allowed_only_before_style?(node) ⇒ Boolean private
- #block_start?(line) ⇒ Boolean private
- #body_end?(line) ⇒ Boolean private
- #class_def?(line) ⇒ Boolean private
- #correct_next_line_if_denied_style(corrector, node, line) private
- #empty_lines_around?(node) ⇒ Boolean private
- #expected_empty_lines?(node) ⇒ Boolean private
- #message(node) private
- #message_for_around_style(node) private
- #message_for_only_before_style(node) private
- #next_empty_line_range(node) private
- #next_line_empty?(last_send_line) ⇒ Boolean private
- #previous_line_empty?(send_line) ⇒ Boolean private
- #previous_line_ignoring_comments(processed_source, send_line) private
::RuboCop::Cop::RangeHelp
- Included
#add_range, #column_offset_between, | |
#contents_range | A range containing only the contents of a literal with delimiters (e.g. |
#directions, | |
#effective_column | Returns the column attribute of the range, except if the range is on the first line and there’s a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value. |
#final_pos, #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, #range_with_comments, #range_with_comments_and_lines, #range_with_surrounding_comma, #range_with_surrounding_space, #source_range |
::RuboCop::Cop::ConfigurableEnforcedStyle
- 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
.new(config = nil, options = nil) ⇒ EmptyLinesAroundAccessModifier
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 56
def initialize(config = nil, = nil) super @block_line = nil end
Instance Method Details
#allowed_only_before_style?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 115
def allowed_only_before_style?(node) if node.special_modifier? return true if processed_source[node.last_line] == 'end' return false if next_line_empty?(node.last_line) end previous_line_empty?(node.first_line) end
#block_start?(line) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 164
def block_start?(line) return false unless @block_line line == @block_line + 1 end
#body_end?(line) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 170
def body_end?(line) return false unless @class_or_module_def_last_line line == @class_or_module_def_last_line - 1 end
#class_def?(line) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 158
def class_def?(line) return false unless @class_or_module_def_first_line line == @class_or_module_def_first_line + 1 end
#correct_next_line_if_denied_style(corrector, node, line) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 124
def correct_next_line_if_denied_style(corrector, node, line) case style when :around corrector.insert_after(line, "\n") unless next_line_empty?(node.last_line) when :only_before if next_line_empty?(node.last_line) range = next_empty_line_range(node) corrector.remove(range) end end end
#empty_lines_around?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 154
def empty_lines_around?(node) previous_line_empty?(node.first_line) && next_line_empty?(node.last_line) end
#expected_empty_lines?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 104
def expected_empty_lines?(node) case style when :around return true if empty_lines_around?(node) when :only_before return true if allowed_only_before_style?(node) end false end
#message(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 180
def (node) case style when :around (node) when :only_before (node) end end
#message_for_around_style(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 189
def (node) send_line = node.first_line if block_start?(send_line) || class_def?(send_line) format(MSG_AFTER, modifier: node.loc.selector.source) else format(MSG_BEFORE_AND_AFTER, modifier: node.loc.selector.source) end end
#message_for_only_before_style(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 199
def (node) modifier = node.loc.selector.source if next_line_empty?(node.last_line) format(MSG_AFTER_FOR_ONLY_BEFORE, modifier: modifier) else format(MSG_BEFORE_FOR_ONLY_BEFORE, modifier: modifier) end end
#next_empty_line_range(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 176
def next_empty_line_range(node) source_range(processed_source.buffer, node.last_line + 1, 0) end
#next_line_empty?(last_send_line) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 148
def next_line_empty?(last_send_line) next_line = processed_source[last_send_line] body_end?(last_send_line) || next_line.blank? end
#on_block(node) Also known as: #on_numblock
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 81
def on_block(node) @block_line = node.source_range.first_line end
#on_class(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 62
def on_class(node) @class_or_module_def_first_line = if node.parent_class node.parent_class.first_line else node.source_range.first_line end @class_or_module_def_last_line = node.source_range.last_line end
#on_module(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 71
def on_module(node) @class_or_module_def_first_line = node.source_range.first_line @class_or_module_def_last_line = node.source_range.last_line end
#on_numblock(node)
Alias for #on_block.
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 85
alias on_numblock on_block
#on_sclass(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 76
def on_sclass(node) @class_or_module_def_first_line = node.identifier.source_range.first_line @class_or_module_def_last_line = node.source_range.last_line end
#on_send(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 87
def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity return unless node. && !(node.parent&.block_type? || node.parent&.numblock_type?) return if expected_empty_lines?(node) = (node) add_offense(node, message: ) do |corrector| line = range_by_whole_lines(node.source_range) corrector.insert_before(line, "\n") unless previous_line_empty?(node.first_line) correct_next_line_if_denied_style(corrector, node, line) end end
#previous_line_empty?(send_line) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 141
def previous_line_empty?(send_line) previous_line = previous_line_ignoring_comments(processed_source, send_line) return true unless previous_line block_start?(send_line) || class_def?(send_line) || previous_line.blank? end
#previous_line_ignoring_comments(processed_source, send_line) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 137
def previous_line_ignoring_comments(processed_source, send_line) processed_source[0..send_line - 2].reverse.find { |line| !comment_line?(line) } end