Sha256: 9980bf79b582899a5787f47a4d6f5f7d5c3e4290306d95d2d55d44100ca7dd92

Contents?: true

Size: 868 Bytes

Versions: 24

Compression:

Stored size: 868 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 < Base
        extend AutoCorrector

        MSG = 'Avoid the use of Perl-style backrefs.'

        def on_nth_ref(node)
          add_offense(node) 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

24 entries across 24 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/perl_backrefs.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/perl_backrefs.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/perl_backrefs.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/perl_backrefs.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/perl_backrefs.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/perl_backrefs.rb
rubocop-1.5.2 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-1.5.1 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-1.5.0 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-1.4.2 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-1.4.1 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-1.4.0 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-1.3.1 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-1.3.0 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-1.2.0 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-1.1.0 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-1.0.0 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-0.93.1 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-0.93.0 lib/rubocop/cop/style/perl_backrefs.rb
rubocop-0.92.0 lib/rubocop/cop/style/perl_backrefs.rb