Sha256: 56411561c1ee85b53144333c5bc3b020538e8a4c3345c6012f0623f91c4667de

Contents?: true

Size: 1.96 KB

Versions: 6507

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for single-line method definitions that contain a body.
      # It will accept single-line methods with no body.
      #
      # @example
      #   # bad
      #   def some_method; body end
      #   def link_to(url); {:name => url}; end
      #   def @table.columns; super; end
      #
      #   # good
      #   def no_op; end
      #   def self.resource_class=(klass); end
      #   def @table.columns; end
      #
      class SingleLineMethods < Cop
        include Alignment

        MSG = 'Avoid single-line method definitions.'.freeze

        def on_def(node)
          return unless node.single_line?
          return if allow_empty? && !node.body

          add_offense(node)
        end
        alias on_defs on_def

        def autocorrect(node)
          lambda do |corrector|
            each_part(node.body) do |part|
              LineBreakCorrector.break_line_before(
                range: part, node: node, corrector: corrector,
                configured_width: configured_indentation_width
              )
            end

            LineBreakCorrector.break_line_before(
              range: node.loc.end, node: node, corrector: corrector,
              indent_steps: 0, configured_width: configured_indentation_width
            )

            move_comment(node, corrector)
          end
        end

        private

        def allow_empty?
          cop_config['AllowIfMethodIsEmpty']
        end

        def each_part(body)
          return unless body

          if body.begin_type?
            body.each_child_node { |part| yield part.source_range }
          else
            yield body.source_range
          end
        end

        def move_comment(node, corrector)
          LineBreakCorrector.move_comment(
            eol_comment: end_of_line_comment(node.source_range.line),
            node: node, corrector: corrector
          )
        end
      end
    end
  end
end

Version data entries

6,507 entries across 6,501 versions & 25 rubygems

Version Path
mux_ruby-3.20.0 vendor/bundle/ruby/3.2.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_bank_ruby-0.123.12 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_organization_ruby-0.123.12 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_id_ruby-0.123.12 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_bank_ruby-0.123.11 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_organization_ruby-0.123.11 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_id_ruby-0.123.11 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_id_ruby-0.123.10 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_bank_ruby-0.123.10 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_organization_ruby-0.123.10 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_organization_ruby-0.123.7 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_id_ruby-0.123.7 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_bank_ruby-0.123.7 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
ory-client-1.15.12 vendor/bundle/ruby/3.1.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_bank_ruby-0.123.4 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_id_ruby-0.123.4 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_organization_ruby-0.123.4 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_bank_ruby-0.123.3 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_organization_ruby-0.123.3 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb
cybrid_api_id_ruby-0.123.3 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/single_line_methods.rb