Sha256: e103e7928589c14bb1c201fc99342d7fcb8fd45ee246b3485a2a302da2188352

Contents?: true

Size: 1.71 KB

Versions: 20

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for ambiguous operators in the first argument of a
      # method invocation without parentheses.
      #
      # @example
      #
      #   # bad
      #
      #   # The `*` is interpreted as a splat operator but it could possibly be
      #   # a `*` method invocation (i.e. `do_something.*(some_array)`).
      #   do_something *some_array
      #
      # @example
      #
      #   # good
      #
      #   # With parentheses, there's no ambiguity.
      #   do_something(*some_array)
      class AmbiguousOperator < Cop
        include ParserDiagnostic

        AMBIGUITIES = {
          '+'  => { actual: 'positive number', possible: 'addition' },
          '-'  => { actual: 'negative number', possible: 'subtraction' },
          '*'  => { actual: 'splat',           possible: 'multiplication' },
          '&'  => { actual: 'block',           possible: 'binary AND' },
          '**' => { actual: 'keyword splat',   possible: 'exponent' }
        }.each do |key, hash|
          hash[:operator] = key
        end

        MSG_FORMAT = 'Ambiguous %<actual>s operator. Parenthesize the method ' \
                     "arguments if it's surely a %<actual>s operator, or add " \
                     'a whitespace to the right of the `%<operator>s` if it ' \
                     'should be a %<possible>s.'.freeze

        private

        def relevant_diagnostic?(diagnostic)
          diagnostic.reason == :ambiguous_prefix
        end

        def alternative_message(diagnostic)
          operator = diagnostic.location.source
          hash = AMBIGUITIES[operator]
          format(MSG_FORMAT, hash)
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
rubocop-0.59.2 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.59.1 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.59.0 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.58.2 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.58.1 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.58.0 lib/rubocop/cop/lint/ambiguous_operator.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.57.2 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.57.1 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.57.0 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.56.0 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.55.0 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.54.0 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.53.0 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.52.1 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.52.0 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.51.0 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.50.0 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.49.1 lib/rubocop/cop/lint/ambiguous_operator.rb
rubocop-0.49.0 lib/rubocop/cop/lint/ambiguous_operator.rb