123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Layout::SpaceBeforeBlockBraces

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_before_block_braces.rb

Overview

Checks that block braces have or don’t have a space before the opening brace depending on configuration.

Examples:

EnforcedStyle: space (default)

# bad
foo.map{ |a|
  a.bar.to_s
}

# good
foo.map { |a|
  a.bar.to_s
}

EnforcedStyle: no_space

# bad
foo.map { |a|
  a.bar.to_s
}

# good
foo.map{ |a|
  a.bar.to_s
}

EnforcedStyleForEmptyBraces: space (default)

# bad
7.times{}

# good
7.times {}

EnforcedStyleForEmptyBraces: no_space

# bad
7.times {}

# good
7.times{}

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

::RuboCop::Cop::ConfigurableEnforcedStyle - Included

SYMBOL_TO_STRING_CACHE

::RuboCop::Cop::RangeHelp - Included

BYTE_ORDER_MARK, NOT_GIVEN

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::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 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_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

Class Method Details

.autocorrect_incompatible_with

[ GitHub ]

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

def self.autocorrect_incompatible_with
  [Style::SymbolProc]
end

Instance Method Details

#autocorrect(corrector, range) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_before_block_braces.rb', line 133

def autocorrect(corrector, range)
  case range.source
  when /\s/ then corrector.remove(range)
  else           corrector.insert_before(range, ' ')
  end
end

#block_delimiters_style (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_before_block_braces.rb', line 153

def block_delimiters_style
  config.for_cop('Style/BlockDelimiters')['EnforcedStyle']
end

#check_empty(left_brace, space_plus_brace, used_style) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_before_block_braces.rb', line 83

def check_empty(left_brace, space_plus_brace, used_style)
  if style_for_empty_braces == used_style
    handle_different_styles_for_empty_braces(used_style)
    return
  elsif !config_to_allow_offenses.key?('Enabled')
    config_to_allow_offenses['EnforcedStyleForEmptyBraces'] = used_style.to_s
  end

  if style_for_empty_braces == :space
    range = left_brace
    msg = MISSING_MSG
  else
    range = range_between(space_plus_brace.begin_pos, left_brace.begin_pos)
    msg = DETECTED_MSG
  end
  add_offense(range, message: msg) { |corrector| autocorrect(corrector, range) }
end

#check_non_empty(left_brace, space_plus_brace, used_style) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_before_block_braces.rb', line 109

def check_non_empty(left_brace, space_plus_brace, used_style)
  case used_style
  when style  then correct_style_detected
  when :space then space_detected(left_brace, space_plus_brace)
  else             space_missing(left_brace)
  end
end

#conflict_with_block_delimiters?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_before_block_braces.rb', line 149

def conflict_with_block_delimiters?(node)
  block_delimiters_style == 'line_count_based' && style == :no_space && node.multiline?
end

#empty_braces?(loc) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_before_block_braces.rb', line 157

def empty_braces?(loc)
  loc.begin.end_pos == loc.end.begin_pos
end

#handle_different_styles_for_empty_braces(used_style) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_before_block_braces.rb', line 101

def handle_different_styles_for_empty_braces(used_style)
  if config_to_allow_offenses['EnforcedStyleForEmptyBraces'] &&
     config_to_allow_offenses['EnforcedStyleForEmptyBraces'].to_sym != used_style
    config_to_allow_offenses.clear
    config_to_allow_offenses['Enabled'] = false
  end
end

#on_block(node) Also known as: #on_numblock

[ GitHub ]

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

def on_block(node)
  return if node.keywords?

  # Do not register an offense for multi-line braces when specifying
  # `EnforcedStyle: no_space`. It will conflict with autocorrection
  # by `EnforcedStyle: line_count_based` of `Style/BlockDelimiters` cop.
  # That means preventing autocorrection to incorrect autocorrected
  # code.
  # See: https://github.com/rubocop/rubocop/issues/7534
  return if conflict_with_block_delimiters?(node)

  left_brace = node.loc.begin
  space_plus_brace = range_with_surrounding_space(left_brace)
  used_style =
    space_plus_brace.source.start_with?('{') ? :no_space : :space

  if empty_braces?(node.loc)
    check_empty(left_brace, space_plus_brace, used_style)
  else
    check_non_empty(left_brace, space_plus_brace, used_style)
  end
end

#on_numblock(node)

Alias for #on_block.

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_before_block_braces.rb', line 79

alias on_numblock on_block

#space_detected(left_brace, space_plus_brace) (private)

[ GitHub ]

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

def space_detected(left_brace, space_plus_brace)
  space = range_between(space_plus_brace.begin_pos, left_brace.begin_pos)

  add_offense(space, message: DETECTED_MSG) do |corrector|
    autocorrect(corrector, space)
    opposite_style_detected
  end
end

#space_missing(left_brace) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_before_block_braces.rb', line 117

def space_missing(left_brace)
  add_offense(left_brace, message: MISSING_MSG) do |corrector|
    autocorrect(corrector, left_brace)
    opposite_style_detected
  end
end

#style_for_empty_braces (private)

[ GitHub ]

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

def style_for_empty_braces
  case cop_config['EnforcedStyleForEmptyBraces']
  when 'space'    then :space
  when 'no_space' then :no_space
  when nil then style
  else raise 'Unknown EnforcedStyleForEmptyBraces selected!'
  end
end