Sha256: c4d61a64ab62ff902dfaf12a3854370d584d439c20785c2d561281b1b3bc7132

Contents?: true

Size: 1.72 KB

Versions: 14

Compression:

Stored size: 1.72 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 < Cop
        include ConfigurableEnforcedStyle

        def on_send(node)
          return unless node.receiver && node.method?(:call)

          if offense?(node)
            add_offense(node) { opposite_style_detected }
          else
            correct_style_detected
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            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
        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

14 entries across 14 versions & 3 rubygems

Version Path
rubocop-0.89.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.88.0 lib/rubocop/cop/style/lambda_call.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.87.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.87.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.86.0 lib/rubocop/cop/style/lambda_call.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/lambda_call.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/lambda_call.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.85.1 lib/rubocop/cop/style/lambda_call.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.85.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.84.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.83.0 lib/rubocop/cop/style/lambda_call.rb