Class: RuboCop::Cop::Layout::EmptyLineAfterGuardClause
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
::RuboCop::Cop::Util,
::RuboCop::PathUtil,
::RuboCop::Cop::AutoCorrector,
::RuboCop::Cop::Base,
::RuboCop::ExcludeLimit,
NodePattern::Macros,
RuboCop::AST::Sexp
|
|
|
Instance Chain:
self,
::RuboCop::Cop::RangeHelp,
::RuboCop::Cop::Base,
::RuboCop::Cop::AutocorrectLogic,
::RuboCop::Cop::IgnoredNode,
::RuboCop::Util,
RuboCop::AST::Sexp
|
|
| Inherits: |
RuboCop::Cop::Base
|
| Defined in: | lib/rubocop/cop/layout/empty_line_after_guard_clause.rb |
Overview
Enforces empty line after guard clause.
This cop allows # :nocov: directive after guard clause because
SimpleCov excludes code from the coverage report by wrapping it in # :nocov::
def foo
# :nocov:
return if condition
# :nocov:
end
Refer to SimpleCov’s documentation for more details: https://github.com/simplecov-ruby/simplecov#ignoringskipping-code
Constant Summary
-
END_OF_HEREDOC_LINE =
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 601 -
MSG =
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 59'Add empty line after guard clause.' -
SIMPLE_DIRECTIVE_COMMENT_PATTERN =
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 61/\A# *:nocov:\z/.freeze
::RuboCop::Cop::Base - Inherited
EMPTY_OFFENSES, RESTRICT_ON_SEND
::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::Util - Extended
| add_parentheses | Metrics/MethodLength. |
| any_descendant? | Metrics/MethodLength. |
| args_begin, args_end, begins_its_line?, | |
| comment_line? | This is a bad API. |
| comment_lines?, compatible_external_encoding_for?, | |
| double_quotes_required? | If converting a string to Ruby string literal source code, must double quotes be used? |
| escape_string, | |
| first_part_of_call_chain | Returns, for example, a bare |
| include_or_equal?, indent, interpret_string_escapes, line, line_range, needs_escaping?, on_node, parentheses?, parse_regexp, same_line?, to_string_literal, to_supported_styles, trim_string_interpolation_escape_character | |
::RuboCop::PathUtil - Included
| absolute? | Returns true for an absolute Unix or Windows path. |
| glob? | Returns true for a glob. |
| hidden_dir?, hidden_file?, hidden_file_in_not_hidden_dir?, | |
| match_path? | Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity. |
| maybe_hidden_file? | Loose check to reduce memory allocations. |
| relative_path, remote_file?, smart_path | |
::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::Base - Inherited
::RuboCop::Cop::AutocorrectLogic - Included
Instance Method Summary
- #on_if(node)
- #autocorrect(corrector, node) private
- #contains_guard_clause?(node) ⇒ Boolean private
- #correct_style?(node) ⇒ Boolean private
- #heredoc?(node) ⇒ Boolean private
- #heredoc_line(node, heredoc_node) private
- #last_heredoc_argument(node) private
- #last_heredoc_argument_node(node) private
- #multiple_statements_on_line?(node) ⇒ Boolean private
- #next_line_allowed_directive_comment?(line) ⇒ Boolean private
- #next_line_empty?(line) ⇒ Boolean private
- #next_line_empty_or_allowed_directive_comment?(line) ⇒ Boolean private
- #next_line_rescue_or_ensure?(node) ⇒ Boolean private
- #next_sibling_empty_or_guard_clause?(node) ⇒ Boolean private
- #next_sibling_parent_empty_or_else?(node) ⇒ Boolean private
- #offense_location(node) private
-
#simplecov_directive_comment?(comment) ⇒ Boolean
private
SimpleCov excludes code from the coverage report by wrapping it in
# :nocov:: https://github.com/simplecov-ruby/simplecov#ignoringskipping-code. - #use_heredoc_in_condition?(condition) ⇒ 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, #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::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_gem_version | Returns a gems locked versions (i.e. |
| #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
::RuboCop::Cop::IgnoredNode - Included
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#autocorrect(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 84
def autocorrect(corrector, node) node_range = if heredoc?(node) range_by_whole_lines(node.loc.heredoc_body) else range_by_whole_lines(node.source_range) end next_line = node_range.last_line + 1 if next_line_allowed_directive_comment?(next_line) node_range = processed_source.comment_at_line(next_line) end corrector.insert_after(node_range, "\n") end
#contains_guard_clause?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 106
def contains_guard_clause?(node) node.if_branch&.guard_clause? end
#correct_style?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 99
def correct_style?(node) !contains_guard_clause?(node) || next_line_rescue_or_ensure?(node) || next_sibling_parent_empty_or_else?(node) || next_sibling_empty_or_guard_clause?(node) end
#heredoc?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 181
def heredoc?(node) node.respond_to?(:heredoc?) && node.heredoc? end
#heredoc_line(node, heredoc_node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 174
def heredoc_line(node, heredoc_node) heredoc_body = heredoc_node.loc.heredoc_body num_of_heredoc_lines = heredoc_body.last_line - heredoc_body.first_line node.last_line + num_of_heredoc_lines + END_OF_HEREDOC_LINE end
#last_heredoc_argument(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 148
def last_heredoc_argument(node) n = last_heredoc_argument_node(node) return n if heredoc?(n) return unless n.respond_to?(:arguments) n.arguments.each do |argument| node = last_heredoc_argument(argument) return node if node end last_heredoc_argument(n.receiver) if n.respond_to?(:receiver) end
#last_heredoc_argument_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 162
def last_heredoc_argument_node(node) return node unless node.respond_to?(:if_branch) if node.if_branch.and_type? node.if_branch.children.first elsif use_heredoc_in_condition?(node.condition) node.condition else node.if_branch.children.last end end
#multiple_statements_on_line?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 199
def multiple_statements_on_line?(node) parent = node.parent return false unless parent parent.begin_type? && same_line?(node, node.right_sibling) end
#next_line_allowed_directive_comment?(line) ⇒ Boolean (private)
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 121
def next_line_allowed_directive_comment?(line) return false unless (comment = processed_source.comment_at_line(line)) DirectiveComment.new(comment).enabled? || simplecov_directive_comment?(comment) end
#next_line_empty?(line) ⇒ Boolean (private)
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 117
def next_line_empty?(line) processed_source[line].blank? end
#next_line_empty_or_allowed_directive_comment?(line) ⇒ Boolean (private)
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 110
def next_line_empty_or_allowed_directive_comment?(line) return true if next_line_empty?(line) next_line = line + 1 next_line_allowed_directive_comment?(next_line) && next_line_empty?(next_line) end
#next_line_rescue_or_ensure?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 127
def next_line_rescue_or_ensure?(node) parent = node.parent parent.nil? || parent.rescue_type? || parent.ensure_type? end
#next_sibling_empty_or_guard_clause?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 141
def next_sibling_empty_or_guard_clause?(node) next_sibling = node.right_sibling return true if next_sibling.nil? next_sibling.if_type? && contains_guard_clause?(next_sibling) end
#next_sibling_parent_empty_or_else?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 132
def next_sibling_parent_empty_or_else?(node) next_sibling = node.right_sibling return true unless next_sibling.is_a?(AST::Node) parent = next_sibling.parent parent&.if_type? && parent.else? end
#offense_location(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 191
def offense_location(node) if node.loc?(:end) node.loc.end else node end end
#on_if(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 63
def on_if(node) return if correct_style?(node) return if multiple_statements_on_line?(node) if node.modifier_form? && (heredoc_node = last_heredoc_argument(node)) if next_line_empty_or_allowed_directive_comment?(heredoc_line(node, heredoc_node)) return end add_offense(heredoc_node.loc.heredoc_end) do |corrector| autocorrect(corrector, heredoc_node) end else return if next_line_empty_or_allowed_directive_comment?(node.last_line) add_offense(offense_location(node)) { |corrector| autocorrect(corrector, node) } end end
#simplecov_directive_comment?(comment) ⇒ Boolean (private)
SimpleCov excludes code from the coverage report by wrapping it in # :nocov::
https://github.com/simplecov-ruby/simplecov#ignoringskipping-code
# File 'lib/rubocop/cop/layout/empty_line_after_guard_clause.rb', line 208
def simplecov_directive_comment?(comment) SIMPLE_DIRECTIVE_COMMENT_PATTERN.match?(comment.text) end