Sha256: 8c2e0444626de6320a7b577c41c36cb1768397f8be2940ad17ef5044a41ca06c

Contents?: true

Size: 1.33 KB

Versions: 38

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for trailing code after the method definition.
      #
      # NOTE: It always accepts endless method definitions that are basically on the same line.
      #
      # @example
      #   # bad
      #   def some_method; do_stuff
      #   end
      #
      #   def f(x); b = foo
      #     b[c: x]
      #   end
      #
      #   # good
      #   def some_method
      #     do_stuff
      #   end
      #
      #   def f(x)
      #     b = foo
      #     b[c: x]
      #   end
      #
      #   def endless_method = do_stuff
      #
      class TrailingBodyOnMethodDefinition < Base
        include Alignment
        include TrailingBody
        extend AutoCorrector

        MSG = "Place the first line of a multi-line method definition's body on its own line."

        def on_def(node)
          return unless trailing_body?(node)
          return if node.endless?

          add_offense(first_part_of(node.body)) do |corrector|
            LineBreakCorrector.correct_trailing_body(
              configured_width: configured_indentation_width,
              corrector: corrector,
              node: node,
              processed_source: processed_source
            )
          end
        end
        alias on_defs on_def
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/trailing_body_on_method_definition.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.29.1 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.29.0 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.28.2 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.28.1 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.28.0 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.27.0 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.26.1 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.26.0 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.25.1 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.25.0 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.24.1 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.24.0 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.23.0 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.22.3 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.22.2 lib/rubocop/cop/style/trailing_body_on_method_definition.rb
rubocop-1.22.1 lib/rubocop/cop/style/trailing_body_on_method_definition.rb