Sha256: 1f75869ea6df7b5d80fe76817fc09ba3550280022cbab97789e1c9d1f6b694a5

Contents?: true

Size: 1.79 KB

Versions: 24

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop 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

        RESTRICT_ON_SEND = %i[call].freeze

        def on_send(node)
          return unless node.receiver

          if offense?(node) && opposite_style_detected
            add_offense(node) do |corrector|
              autocorrect(corrector, node)
            end
          else
            correct_style_detected
          end
        end

        def autocorrect(corrector, node)
          if explicit_style?
            receiver = node.receiver.source
            replacement = node.source.sub("#{receiver}.", "#{receiver}.call")

            corrector.replace(node, replacement)
          else
            add_parentheses(node, corrector) unless node.parenthesized?
            corrector.remove(node.loc.selector)
          end
        end

        private

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

        def message(_node)
          if explicit_style?
            'Prefer the use of `lambda.call(...)` over `lambda.(...)`.'
          else
            'Prefer the use of `lambda.(...)` over `lambda.call(...)`.'
          end
        end

        def implicit_style?
          style == :braces
        end

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

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/lambda_call.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/lambda_call.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/lambda_call.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/lambda_call.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/lambda_call.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/lambda_call.rb
rubocop-1.6.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.6.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.5.2 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.5.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.5.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.4.2 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.4.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.4.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.3.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.3.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.2.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.1.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.0.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.93.1 lib/rubocop/cop/style/lambda_call.rb