Sha256: d33cc021fb6aa3daf98b742fc0423f4b5304add9c12b915471629d26b777b857

Contents?: true

Size: 1.41 KB

Versions: 32

Compression:

Stored size: 1.41 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 if node.endless? || !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

32 entries across 32 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/trailing_method_end_statement.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.26.1 lib/rubocop/cop/style/trailing_method_end_statement.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.26.0 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.25.1 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.25.0 lib/rubocop/cop/style/trailing_method_end_statement.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.24.1 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.24.0 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.23.0 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.22.3 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.22.2 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.22.1 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.22.0 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.21.0 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.20.0 lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.19.1 lib/rubocop/cop/style/trailing_method_end_statement.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/style/trailing_method_end_statement.rb
rubocop-1.19.0 lib/rubocop/cop/style/trailing_method_end_statement.rb