Sha256: 0c4023a163bdc92a08061f8b2b6734a963e99dfd188705c967df982211d91181

Contents?: true

Size: 1.57 KB

Versions: 15

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks that block braces have or don't have a space before the opening
      # brace depending on configuration.
      class SpaceBeforeBlockBraces < Cop
        include ConfigurableEnforcedStyle

        def on_block(node)
          return if node.loc.begin.is?('do') # No braces.

          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

          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

        private

        def space_missing(left_brace)
          add_offense(left_brace, left_brace,
                      'Space missing to the left of {.') do
            opposite_style_detected
          end
        end

        def space_detected(left_brace, space_plus_brace)
          space = range_between(space_plus_brace.begin_pos,
                                left_brace.begin_pos)
          add_offense(space, space, 'Space detected to the left of {.') do
            opposite_style_detected
          end
        end

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

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_block_braces.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_block_braces.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_block_braces.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_block_braces.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_block_braces.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_block_braces.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_block_braces.rb
rubocop-0.48.1 lib/rubocop/cop/style/space_before_block_braces.rb
rubocop-0.48.0 lib/rubocop/cop/style/space_before_block_braces.rb
rubocop-0.47.1 lib/rubocop/cop/style/space_before_block_braces.rb
rubocop-0.47.0 lib/rubocop/cop/style/space_before_block_braces.rb
rubocop-0.46.0 lib/rubocop/cop/style/space_before_block_braces.rb
rubocop-0.45.0 lib/rubocop/cop/style/space_before_block_braces.rb
rubocop-0.44.1 lib/rubocop/cop/style/space_before_block_braces.rb
rubocop-0.44.0 lib/rubocop/cop/style/space_before_block_braces.rb