Sha256: 8f2d8e07ab810c3a486648ccc40a37bdd7117ba91cf5a4c3e2c1e164257852c9

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for use of the lambda.(args) syntax.
      #
      # @example EnforcedStyle: call (default)
      #  # bad
      #  lambda.(x, y)
      #
      #  # good
      #  lambda.call(x, y)
      #
      # @example EnforcedStyle: braces
      #  # bad
      #  lambda.call(x, y)
      #
      #  # good
      #  lambda.(x, y)
      class LambdaCall < Base
        include ConfigurableEnforcedStyle
        extend AutoCorrector

        MSG = 'Prefer the use of `%<prefer>s` over `%<current>s`.'
        RESTRICT_ON_SEND = %i[call].freeze

        def on_send(node)
          return unless node.receiver

          if offense?(node)
            prefer = prefer(node)
            current = node.source

            add_offense(node, message: format(MSG, prefer: prefer, current: current)) do |corrector|
              next if part_of_ignored_node?(node)

              opposite_style_detected
              corrector.replace(node, prefer)

              ignore_node(node)
            end
          else
            correct_style_detected
          end
        end
        alias on_csend on_send

        private

        def offense?(node)
          (explicit_style? && node.implicit_call?) || (implicit_style? && !node.implicit_call?)
        end

        def prefer(node)
          receiver = node.receiver.source
          arguments = node.arguments.map(&:source).join(', ')
          dot = node.loc.dot.source
          method = explicit_style? ? "call(#{arguments})" : "(#{arguments})"

          "#{receiver}#{dot}#{method}"
        end

        def implicit_style?
          style == :braces
        end

        def explicit_style?
          style == :call
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.69.2 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.69.1 lib/rubocop/cop/style/lambda_call.rb