Module: RuboCop::Cop::Layout::EmptyLinesAroundBody
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
Macros
|
|
|
Instance Chain:
|
|
| Defined in: | lib/rubocop/cop/mixin/empty_lines_around_body.rb |
Overview
Common functionality for checking if presence/absence of empty lines around some kind of body matches the configuration.
Constant Summary
-
LEADING_DOT_TYPES =
# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 17%i[tDOT tANDDOT].to_set.freeze
-
MSG_DEFERRED =
# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 15'Empty line missing before first %<type>s definition' -
MSG_EXTRA =
# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 13'Extra empty line detected at %<kind>s body %<location>s.' -
MSG_MISSING =
# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 14'Empty line missing at %<kind>s body %<location>s.'
::RuboCop::Cop::ConfigurableEnforcedStyle - Included
::RuboCop::Cop::RangeHelp - Included
Instance Attribute Summary
::RuboCop::Cop::ConfigurableEnforcedStyle - Included
Instance Method Summary
- #on_new_investigation
- #access_modifier_send?(node) private
- #check(node, body, adjusted_first_line: nil) private
- #check_beginning(style, body, first_line) private
- #check_body_lines(body, first_line, last_line) private
- #check_both(style, body, first_line, last_line) private
- #check_deferred_empty_line(body) private
- #check_empty_lines_except_namespace(body, first_line, last_line) private
- #check_empty_lines_special(body, first_line, last_line) private
- #check_ending(style, body, last_line) private
- #check_line(style, line, msg) private
- #check_no_empty_line_at_beginning(body, first_line) private
- #check_source(style, line_no, desc) private
- #constant_definition?(node) private
- #deferred_empty_line_at_beginning?(body, first_line) ⇒ Boolean private
- #deferred_message(node) private
- #empty_line_required?(node) private
- #first_child_requires_empty_line?(body) ⇒ Boolean private
- #first_empty_line_required_child(body) private
- #heredoc_end_line(line) private
- #heredocs_by_line private
- #leading_dot_line?(line) ⇒ Boolean private
- #message(type, desc) private
- #namespace?(body, with_one_child: false) ⇒ Boolean private
- #previous_line_ignoring_comments(send_line) private
- #same_line_as_previous_statement?(node) ⇒ Boolean private
- #valid_body_style?(body) ⇒ Boolean private
::RuboCop::Cop::RangeHelp - Included
| #add_range, | |
| #arguments_range | A range containing the first to the last argument of a method call or method definition. |
| #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 | rubocop:disable-next Metrics/ParameterLists. |
| #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, | |
| #range_for_statement_removal | Returns the range to remove a statement: its whole lines, including a trailing comment and the final newline, when no other code shares them, and otherwise the range itself with the spaces after it. |
| #range_with_comments, #range_with_comments_and_lines, #range_with_surrounding_comma, #range_with_surrounding_space, #source_range | |
::RuboCop::Cop::ConfigurableEnforcedStyle - Included
Instance Method Details
#access_modifier_send?(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 30
def_node_matcher :access_modifier_send?, '(send nil? {:private :protected :public})'
#check(node, body, adjusted_first_line: nil) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 40
def check(node, body, adjusted_first_line: nil) return if valid_body_style?(body) return if node.single_line? first_line = adjusted_first_line || node.first_line if (end_line = heredoc_end_line(first_line)) first_line = end_line body = node.children.first if node.kwbegin_type? end last_line = node.source_range.last_line return if first_line >= last_line check_body_lines(body, first_line, last_line) end
#check_beginning(style, body, first_line) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 117
def check_beginning(style, body, first_line) if body return if body.first_line < first_line return if style == :no_empty_lines && body.last_line <= first_line return if style == :empty_lines && body.first_line == first_line end check_source(style, first_line, 'beginning') end
#check_body_lines(body, first_line, last_line) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 56
def check_body_lines(body, first_line, last_line) case style when :empty_lines_except_namespace check_empty_lines_except_namespace(body, first_line, last_line) when :empty_lines_special check_empty_lines_special(body, first_line, last_line) else check_both(style, body, first_line, last_line) end end
#check_both(style, body, first_line, last_line) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 103
def check_both(style, body, first_line, last_line) case style when :beginning_only check_beginning(:empty_lines, body, first_line) check_ending(:no_empty_lines, body, last_line) when :ending_only check_beginning(:no_empty_lines, body, first_line) check_ending(:empty_lines, body, last_line) else check_beginning(style, body, first_line) check_ending(style, body, last_line) end end
#check_deferred_empty_line(body) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 174
def check_deferred_empty_line(body) node = first_empty_line_required_child(body) return unless node return if same_line_as_previous_statement?(node) line = previous_line_ignoring_comments(node.first_line) return if processed_source[line].empty? range = source_range(processed_source.buffer, line + 2, 0) add_offense(range, message: (node)) do |corrector| EmptyLineCorrector.correct(corrector, [:empty_lines, range]) end end
#check_empty_lines_except_namespace(body, first_line, last_line) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 79
def check_empty_lines_except_namespace(body, first_line, last_line) if namespace?(body, with_one_child: true) check_both(:no_empty_lines, body, first_line, last_line) else check_both(:empty_lines, body, first_line, last_line) end end
#check_empty_lines_special(body, first_line, last_line) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 87
def check_empty_lines_special(body, first_line, last_line) return unless body if namespace?(body, with_one_child: true) check_both(:no_empty_lines, body, first_line, last_line) else if first_child_requires_empty_line?(body) check_beginning(:empty_lines, body, first_line) else check_no_empty_line_at_beginning(body, first_line) check_deferred_empty_line(body) end check_ending(:empty_lines, body, last_line) end end
#check_ending(style, body, last_line) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 127
def check_ending(style, body, last_line) return if style == :empty_lines && body&.last_line == last_line check_source(style, last_line - 2, 'end') end
#check_line(style, line, msg) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 142
def check_line(style, line, msg) return unless yield(processed_source.lines[line]) offset = style == :empty_lines && msg.include?('end.') ? 2 : 1 return if style == :empty_lines && leading_dot_line?(line + offset) range = source_range(processed_source.buffer, line + offset, 0) add_offense(range, message: msg) do |corrector| EmptyLineCorrector.correct(corrector, [style, range]) end end
#check_no_empty_line_at_beginning(body, first_line) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 160
def check_no_empty_line_at_beginning(body, first_line) return if deferred_empty_line_at_beginning?(body, first_line) check_beginning(:no_empty_lines, body, first_line) end
#check_source(style, line_no, desc) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 133
def check_source(style, line_no, desc) case style when :no_empty_lines check_line(style, line_no, (MSG_EXTRA, desc), &:empty?) when :empty_lines check_line(style, line_no, (MSG_MISSING, desc)) { |line| !line.empty? } end end
#constant_definition?(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 27
def_node_matcher :constant_definition?, '{class module}'
#deferred_empty_line_at_beginning?(body, first_line) ⇒ Boolean (private)
# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 166
def deferred_empty_line_at_beginning?(body, first_line) node = first_empty_line_required_child(body) return false unless node return false if node.first_line <= first_line previous_line_ignoring_comments(node.first_line) <= first_line end
#deferred_message(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 230
def (node) format(MSG_DEFERRED, type: node.type) end
#empty_line_required?(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 33
def_node_matcher :empty_line_required?, <<~PATTERN { any_def class module #access_modifier_send? [{(if _ #access_modifier_send? nil?) (if _ nil? #access_modifier_send?)} modifier_form?] } PATTERN
#first_child_requires_empty_line?(body) ⇒ Boolean (private)
# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 199
def first_child_requires_empty_line?(body) if body.begin_type? empty_line_required?(body.children.first) else empty_line_required?(body) end end
#first_empty_line_required_child(body) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 207
def first_empty_line_required_child(body) if body.begin_type? body.children.find { |child| empty_line_required?(child) } elsif empty_line_required?(body) body end end
#heredoc_end_line(line) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 67
def heredoc_end_line(line) return unless processed_source[line - 1].include?('<<') heredocs = heredocs_by_line[line] heredocs&.map { |str| str.loc.heredoc_end.line }&.max end
#heredocs_by_line (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 74
def heredocs_by_line @heredocs_by_line ||= processed_source.ast.each_node(:any_str) .select(&:heredoc?).group_by(&:first_line) end
#leading_dot_line?(line) ⇒ Boolean (private)
# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 154
def leading_dot_line?(line) first_token = processed_source.sorted_tokens.find { |token| token.line == line } LEADING_DOT_TYPES.include?(first_token&.type) end
#message(type, desc) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 226
def (type, desc) format(type, kind: self.class::KIND, location: desc) end
#namespace?(body, with_one_child: false) ⇒ Boolean (private)
# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 189
def namespace?(body, with_one_child: false) if body.begin_type? return false if with_one_child body.children.all? { |child| constant_definition?(child) } else constant_definition?(body) end end
#on_new_investigation
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 19
def on_new_investigation super @heredocs_by_line = nil end
#previous_line_ignoring_comments(send_line) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 219
def previous_line_ignoring_comments(send_line) (send_line - 2).downto(0) do |line| return line unless comment_line?(processed_source[line]) end 0 end
#same_line_as_previous_statement?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 215
def same_line_as_previous_statement?(node) node.left_sibling.last_line == node.first_line end
#valid_body_style?(body) ⇒ Boolean (private)
# File 'lib/rubocop/cop/mixin/empty_lines_around_body.rb', line 234
def valid_body_style?(body) # When style is `empty_lines`, if the body is empty, we don't enforce # the presence OR absence of an empty line # But if style is `no_empty_lines`, there must not be an empty line body.nil? && style != :no_empty_lines end