Sha256: 5f483417956968a06b635ee24a9ac3d5faf58320f84ba8a8046ce62d78e0ffde

Contents?: true

Size: 1.44 KB

Versions: 34

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for trailing code after the method definition.
      #
      # @example
      #   # bad
      #   def some_method
      #   do_stuff; end
      #
      #   def do_this(x)
      #     baz.map { |b| b.this(x) } end
      #
      #   def foo
      #     block do
      #       bar
      #     end end
      #
      #   # good
      #   def some_method
      #     do_stuff
      #   end
      #
      #   def do_this(x)
      #     baz.map { |b| b.this(x) }
      #   end
      #
      #   def foo
      #     block do
      #       bar
      #     end
      #   end
      #
      class TrailingMethodEndStatement < Base
        extend AutoCorrector

        MSG = 'Place the end statement of a multi-line method on ' \
              'its own line.'

        def on_def(node)
          return unless trailing_end?(node)

          add_offense(node.loc.end) do |corrector|
            corrector.insert_before(
              node.loc.end,
              "\n#{' ' * node.loc.keyword.column}"
            )
          end
        end

        private

        def trailing_end?(node)
          node.body &&
            node.multiline? &&
            body_and_end_on_same_line?(node)
        end

        def body_and_end_on_same_line?(node)
          last_child = node.children.last
          last_child.loc.last_line == node.loc.end.last_line
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/trailing_method_end_statement.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/trailing_method_end_statement.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/trailing_method_end_statement.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/trailing_method_end_statement.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/trailing_method_end_statement.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.10.0 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.9.1 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.9.0 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.8.1 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.8.0 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.7.0 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.6.1 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.6.0 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.5.2 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.5.1 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.5.0 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.4.2 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.4.1 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.4.0 lib/rubocop/cop/style/trailing_method_end_statement.rb