Sha256: 9534b8172f213e70318e26d6d1ab442cddb439bcd9f6650b18c057ca4c03f571

Contents?: true

Size: 888 Bytes

Versions: 13

Compression:

Stored size: 888 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.
      class PerlBackrefs < Cop
        MSG = 'Avoid the use of Perl-style backrefs.'.freeze

        def on_nth_ref(node)
          add_offense(node, :expression)
        end

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

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/perl_backrefs.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/perl_backrefs.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/perl_backrefs.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/perl_backrefs.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/perl_backrefs.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/perl_backrefs.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/perl_backrefs.rb
rubocop-0.47.1 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-0.47.0 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-0.46.0 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-0.45.0 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-0.44.1 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-0.44.0 lib/rubocop/cop/style/perl_backrefs.rb