Sha256: 87b6dac94f60dc7c379c8d0ec10197f3cff0d2dd7c3c9a413e994848d9d29dbf

Contents?: true

Size: 1.5 KB

Versions: 13

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for ambiguous regexp literals in the first argument of
      # a method invocation without parentheses.
      #
      # @example
      #
      #   # bad
      #
      #   # This is interpreted as a method invocation with a regexp literal,
      #   # but it could possibly be `/` method invocations.
      #   # (i.e. `do_something./(pattern)./(i)`)
      #   do_something /pattern/i
      #
      # @example
      #
      #   # good
      #
      #   # With parentheses, there's no ambiguity.
      #   do_something(/pattern/i)
      class AmbiguousRegexpLiteral < Cop
        include ParserDiagnostic

        MSG = 'Ambiguous regexp literal. Parenthesize the method arguments ' \
              "if it's surely a regexp literal, or add a whitespace to the " \
              'right of the `/` if it should be a division.'

        def autocorrect(node)
          lambda do |corrector|
            add_parentheses(node, corrector)
          end
        end

        private

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

        def find_offense_node_by(diagnostic)
          node = processed_source.ast.each_node(:regexp).find do |regexp_node|
            regexp_node.source_range.begin_pos == diagnostic.location.begin_pos
          end

          node.parent
        end

        def alternative_message(_diagnostic)
          MSG
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

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