123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Layout::SpaceAroundKeyword

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/space_around_keyword.rb

Overview

Checks the spacing around the keywords.

Examples:

# bad
something 'test'do|x|
end

while(something)
end

something = 123if test

# good
something 'test' do |x|
end

while (something)
end

something = 123 if test

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

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 on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

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

Instance Method Summary

::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 add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#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

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#accept_left_parenthesis?(range) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 235

def accept_left_parenthesis?(range)
  ACCEPT_LEFT_PAREN.include?(range.source)
end

#accept_left_square_bracket?(range) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 239

def accept_left_square_bracket?(range)
  ACCEPT_LEFT_SQUARE_BRACKET.include?(range.source)
end

#accept_namespace_operator?(range) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 243

def accept_namespace_operator?(range)
  range.source == ACCEPT_NAMESPACE_OPERATOR
end

#accepted_opening_delimiter?(range, char) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 228

def accepted_opening_delimiter?(range, char)
  return true unless char

  (accept_left_square_bracket?(range) && char == '[') ||
    (accept_left_parenthesis?(range) && char == '(')
end

#check(node, locations, begin_keyword = DO) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 162

def check(node, locations, begin_keyword = DO)
  locations.each do |loc|
    next unless node.loc.respond_to?(loc)

    range = node.loc.public_send(loc)
    next unless range

    case loc
    when :begin then check_begin(node, range, begin_keyword)
    when :end then check_end(node, range, begin_keyword)
    else check_keyword(node, range)
    end
  end
end

#check_begin(node, range, begin_keyword) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 177

def check_begin(node, range, begin_keyword)
  return if begin_keyword && !range.is?(begin_keyword)

  check_keyword(node, range)
end

#check_end(node, range, begin_keyword) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 183

def check_end(node, range, begin_keyword)
  return if begin_keyword == DO && !do?(node)
  return unless space_before_missing?(range)

  add_offense(range, message: format(MSG_BEFORE, range: range.source)) do |corrector|
    corrector.insert_before(range, ' ')
  end
end

#check_keyword(node, range) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 196

def check_keyword(node, range)
  if space_before_missing?(range) && !preceded_by_operator?(node, range)
    add_offense(range, message: format(MSG_BEFORE, range: range.source)) do |corrector|
      corrector.insert_before(range, ' ')
    end
  end

  return unless space_after_missing?(range)

  add_offense(range, message: format(MSG_AFTER, range: range.source)) do |corrector|
    corrector.insert_after(range, ' ')
  end
end

#do?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 192

def do?(node)
  node.loc.begin&.is?(DO)
end

#namespace_operator?(range, pos) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 251

def namespace_operator?(range, pos)
  range.source_buffer.source[pos, 2].start_with?(NAMESPACE_OPERATOR)
end

#on_and(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 40

def on_and(node)
  check(node, [:operator].freeze) if node.keyword?
end

#on_block(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 44

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  check(node, %i[begin end].freeze)
end

#on_break(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 48

def on_break(node)
  check(node, [:keyword].freeze)
end

#on_case(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 52

def on_case(node)
  check(node, %i[keyword else].freeze)
end

#on_case_match(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 56

def on_case_match(node)
  check(node, %i[keyword else].freeze)
end

#on_defined?(node) ⇒ Boolean

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 156

def on_defined?(node)
  check(node, [:keyword].freeze)
end

#on_ensure(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 60

def on_ensure(node)
  check(node, [:keyword].freeze)
end

#on_for(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 64

def on_for(node)
  check(node, %i[begin end].freeze)
end

#on_if(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 68

def on_if(node)
  check(node, %i[keyword else begin end].freeze, 'then')
end

#on_if_guard(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 72

def on_if_guard(node)
  check(node, [:keyword].freeze)
end

#on_in_pattern(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 76

def on_in_pattern(node)
  check(node, [:keyword].freeze)
end

#on_kwbegin(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 80

def on_kwbegin(node)
  check(node, %i[begin end].freeze, nil)
end

#on_match_pattern(node)

Handle one-line pattern matching syntax (in) with Parser::Ruby27.

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 85

def on_match_pattern(node)
  return if target_ruby_version >= 3.0

  check(node, [:operator].freeze)
end

#on_match_pattern_p(node)

Handle one-line pattern matching syntax (in) with Parser::Ruby30.

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 92

def on_match_pattern_p(node)
  check(node, [:operator].freeze)
end

#on_next(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 96

def on_next(node)
  check(node, [:keyword].freeze)
end

#on_or(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 100

def on_or(node)
  check(node, [:operator].freeze) if node.keyword?
end

#on_postexe(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 104

def on_postexe(node)
  check(node, [:keyword].freeze)
end

#on_preexe(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 108

def on_preexe(node)
  check(node, [:keyword].freeze)
end

#on_resbody(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 112

def on_resbody(node)
  check(node, [:keyword].freeze)
end

#on_rescue(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 116

def on_rescue(node)
  check(node, [:else].freeze)
end

#on_return(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 120

def on_return(node)
  check(node, [:keyword].freeze)
end

#on_send(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 124

def on_send(node)
  check(node, [:selector].freeze) if node.prefix_not?
end

#on_super(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 128

def on_super(node)
  check(node, [:keyword].freeze)
end

#on_unless_guard(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 136

def on_unless_guard(node)
  check(node, [:keyword].freeze)
end

#on_until(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 140

def on_until(node)
  check(node, %i[begin end keyword].freeze)
end

#on_when(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 144

def on_when(node)
  check(node, [:keyword].freeze)
end

#on_while(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 148

def on_while(node)
  check(node, %i[begin end keyword].freeze)
end

#on_yield(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 152

def on_yield(node)
  check(node, [:keyword].freeze)
end

#on_zsuper(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 132

def on_zsuper(node)
  check(node, [:keyword].freeze)
end

#preceded_by_operator?(node, _range) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 255

def preceded_by_operator?(node, _range)
  # regular dotted method calls bind more tightly than operators
  # so we need to climb up the AST past them
  node.each_ancestor do |ancestor|
    return true if ancestor.operator_keyword? || ancestor.range_type?
    return false unless ancestor.send_type?
    return true if ancestor.operator_method?
  end
  false
end

#safe_navigation_call?(range, pos) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 247

def safe_navigation_call?(range, pos)
  range.source_buffer.source[pos, 2].start_with?(SAFE_NAVIGATION)
end

#space_after_missing?(range) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 217

def space_after_missing?(range)
  pos = range.end_pos
  char = range.source_buffer.source[pos]

  return false if accepted_opening_delimiter?(range, char)
  return false if safe_navigation_call?(range, pos)
  return false if accept_namespace_operator?(range) && namespace_operator?(range, pos)

  !/[\s;,#\\)}\].]/.match?(char)
end

#space_before_missing?(range) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 210

def space_before_missing?(range)
  pos = range.begin_pos - 1
  return false if pos.negative?

  !/[\s(|{\[;,*=]/.match?(range.source_buffer.source[pos])
end