class RuboCop::Cop::Style::PerlBackrefs

This cop looks for uses of Perl-style regexp match backreferences like $1, $2, etc.

Constants

MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/perl_backrefs.rb, line 14
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.source_range,
                        "{Regexp.last_match(#{backref})}")
    else
      corrector.replace(node.source_range,
                        "Regexp.last_match(#{backref})")
    end
  end
end
on_nth_ref(node) click to toggle source
# File lib/rubocop/cop/style/perl_backrefs.rb, line 10
def on_nth_ref(node)
  add_offense(node)
end