Sha256: 6753f1e79397706f5282c8a994cfaa07f98261f264166a8640a42b91ebb55e3e

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop checks for redundant `begin` blocks.
      #
      # Currently it checks for code like this:
      #
      # @example
      #
      #   def test
      #     begin
      #       ala
      #       bala
      #     rescue StandardError => e
      #       something
      #     end
      #   end
      class RedundantBegin < Cop
        include OnMethod

        MSG = 'Redundant `begin` block detected.'

        def on_method(_node, _method_name, _args, body)
          return unless body && body.type == :kwbegin

          add_offense(body, :begin)
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            child = node.children.first

            begin_indent = node.loc.column
            child_indent = child.loc.column

            indent_diff = child_indent - begin_indent

            corrector.replace(
              range_with_surrounding_space(node.loc.expression),
              range_with_surrounding_space(
                child.loc.expression
              ).source.gsub(/^[ \t]{#{indent_diff}}/, '')
            )
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/redundant_begin.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/redundant_begin.rb
rubocop-0.26.0 lib/rubocop/cop/style/redundant_begin.rb