Sha256: bf9cea7c41933225e0261309f333c18031c09454ef13d468c892cb4b1587e1e9

Contents?: true

Size: 889 Bytes

Versions: 1

Compression:

Stored size: 889 Bytes

Contents

# encoding: utf-8

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.'

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.28.0 lib/rubocop/cop/style/perl_backrefs.rb