Sha256: d741750230468c964a36bd01e4fa190a0ca4cb2d4d18265a3d8e082f57cc3e05

Contents?: true

Size: 1.39 KB

Versions: 179

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks that a range literal is enclosed in parentheses when the end of the range is
      # at a line break.
      #
      # NOTE: The following is maybe intended for `(42..)`. But, compatible is `42..do_something`.
      # So, this cop does not provide autocorrection because it is left to user.
      #
      # [source,ruby]
      # ----
      # case condition
      # when 42..
      #   do_something
      # end
      # ----
      #
      # @example
      #
      #   # bad - Represents `(1..42)`, not endless range.
      #   1..
      #   42
      #
      #   # good - It's incompatible, but your intentions when using endless range may be:
      #   (1..)
      #   42
      #
      #   # good
      #   1..42
      #
      #   # good
      #   (1..42)
      #
      #   # good
      #   (1..
      #   42)
      #
      class RequireRangeParentheses < Base
        MSG = 'Wrap the endless range literal `%<range>s` to avoid precedence ambiguity.'

        def on_irange(node)
          return if node.parent&.begin_type?
          return unless node.begin && node.end
          return if same_line?(node.begin, node.end)

          message = format(MSG, range: "#{node.begin.source}#{node.loc.operator.source}")

          add_offense(node, message: message)
        end

        alias on_erange on_irange
      end
    end
  end
end

Version data entries

179 entries across 174 versions & 17 rubygems

Version Path
rubocop-1.68.0 lib/rubocop/cop/lint/require_range_parentheses.rb
rubocop-1.67.0 lib/rubocop/cop/lint/require_range_parentheses.rb
rubocop-1.66.1 lib/rubocop/cop/lint/require_range_parentheses.rb
rubocop-1.66.0 lib/rubocop/cop/lint/require_range_parentheses.rb
rubocop-1.65.1 lib/rubocop/cop/lint/require_range_parentheses.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/require_range_parentheses.rb
rubocop-1.65.0 lib/rubocop/cop/lint/require_range_parentheses.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/require_range_parentheses.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/require_range_parentheses.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/require_range_parentheses.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/require_range_parentheses.rb
rubocop-1.64.1 lib/rubocop/cop/lint/require_range_parentheses.rb
rubocop-1.63.4 lib/rubocop/cop/lint/require_range_parentheses.rb
rubocop-1.63.3 lib/rubocop/cop/lint/require_range_parentheses.rb
rubocop-1.63.2 lib/rubocop/cop/lint/require_range_parentheses.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/require_range_parentheses.rb
rubocop-1.63.1 lib/rubocop/cop/lint/require_range_parentheses.rb
rubocop-1.63.0 lib/rubocop/cop/lint/require_range_parentheses.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/require_range_parentheses.rb
rubocop-1.62.1 lib/rubocop/cop/lint/require_range_parentheses.rb