Sha256: a722b69524f6a4d8a5967c78843cf0c8b17eaa9d04eb7f7cf120739aedccee23

Contents?: true

Size: 959 Bytes

Versions: 16

Compression:

Stored size: 959 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop looks for uses of Perl-style regexp match
      # backreferences like $1, $2, etc.
      #
      # @example
      #   # bad
      #   puts $1
      #
      #   # good
      #   puts Regexp.last_match(1)
      class PerlBackrefs < Cop
        MSG = 'Avoid the use of Perl-style backrefs.'

        def on_nth_ref(node)
          add_offense(node)
        end

        def autocorrect(node)
          lambda do |corrector|
            backref, = *node
            parent_type = node.parent ? node.parent.type : nil
            if %i[dstr xstr regexp].include?(parent_type)
              corrector.replace(node,
                                "{Regexp.last_match(#{backref})}")
            else
              corrector.replace(node,
                                "Regexp.last_match(#{backref})")
            end
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 3 rubygems

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