1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

rubocop: fix offences of the Style/PerlBackrefs cop

This commit is contained in:
Kyrylo Silin 2019-02-24 19:31:39 +02:00
parent 11eaa002e5
commit 8a64bcae7b
10 changed files with 47 additions and 40 deletions

View file

@ -534,7 +534,7 @@ Metrics/BlockNesting:
# Offense count: 20
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 332
Max: 333
# Offense count: 30
Metrics/CyclomaticComplexity:

View file

@ -200,9 +200,10 @@ class Pry
else
case Pry::Method(block).source_file
when %r{/pry/.*_commands/(.*).rb}
$1.capitalize.gsub(/_/, " ")
Regexp.last_match(1).capitalize.gsub(/_/, " ")
when %r{(pry-\w+)-([\d\.]+([\w\.]+)?)}
name, version = $1, $2
name = Regexp.last_match(1)
version = Regexp.last_match(2)
"#{name} (v#{version})"
when /pryrc/
"pryrc"

View file

@ -31,7 +31,7 @@ class Pry
# when file and line are passed as a single arg, e.g my_file.rb:30
def from_filename_argument(filename_argument)
f = File.expand_path(filename_argument)
l = f.sub!(/:(\d+)$/, "") ? $1.to_i : 1
l = f.sub!(/:(\d+)$/, "") ? Regexp.last_match(1).to_i : 1
[f, l]
end
end

View file

@ -17,7 +17,7 @@ class Pry
def process(cmd)
if cmd =~ /^cd\s*(.*)/i
process_cd parse_destination($1)
process_cd parse_destination(Regexp.last_match(1))
else
pass_block(cmd)
if command_block

View file

@ -199,7 +199,8 @@ class Pry
def complete(input)
if input =~ /([^ ]*)#([a-z0-9_]*)\z/
prefix, search = [$1, $2]
prefix = Regexp.last_match(1)
search = Regexp.last_match(2)
methods = begin
Pry::Method.all_from_class(binding.eval(prefix))
rescue RescuableException

View file

@ -9,13 +9,13 @@ class Pry
def process_rdoc(comment)
comment = comment.dup
comment.gsub(/<code>(?:\s*\n)?(.*?)\s*<\/code>/m) { CodeRay.scan($1, :ruby).term }.
gsub(/<em>(?:\s*\n)?(.*?)\s*<\/em>/m) { "\e[1m#{$1}\e[0m" }.
gsub(/<i>(?:\s*\n)?(.*?)\s*<\/i>/m) { "\e[1m#{$1}\e[0m" }.
gsub(/<tt>(?:\s*\n)?(.*?)\s*<\/tt>/m) { CodeRay.scan($1, :ruby).term }.
gsub(/\B\+(\w+?)\+\B/) { "\e[32m#{$1}\e[0m" }.
gsub(/((?:^[ \t]+.+(?:\n+|\Z))+)/) { CodeRay.scan($1, :ruby).term }.
gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{CodeRay.scan($1, :ruby).term}`" }
comment.gsub(/<code>(?:\s*\n)?(.*?)\s*<\/code>/m) { CodeRay.scan(Regexp.last_match(1), :ruby).term }.
gsub(/<em>(?:\s*\n)?(.*?)\s*<\/em>/m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }.
gsub(/<i>(?:\s*\n)?(.*?)\s*<\/i>/m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }.
gsub(/<tt>(?:\s*\n)?(.*?)\s*<\/tt>/m) { CodeRay.scan(Regexp.last_match(1), :ruby).term }.
gsub(/\B\+(\w+?)\+\B/) { "\e[32m#{Regexp.last_match(1)}\e[0m" }.
gsub(/((?:^[ \t]+.+(?:\n+|\Z))+)/) { CodeRay.scan(Regexp.last_match(1), :ruby).term }.
gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{CodeRay.scan(Regexp.last_match(1), :ruby).term}`" }
end
def process_yardoc_tag(comment, tag)
@ -37,7 +37,7 @@ class Pry
yard_tags = ["param", "return", "option", "yield", "attr", "attr_reader", "attr_writer",
"deprecate", "example", "raise"]
(yard_tags - ["example"]).inject(comment) { |a, v| process_yardoc_tag(a, v) }.
gsub(/^@(#{yard_tags.join("|")})/) { "\e[33m#{$1}\e[0m" }
gsub(/^@(#{yard_tags.join("|")})/) { "\e[33m#{Regexp.last_match(1)}\e[0m" }
end
def process_comment_markup(comment)

View file

@ -48,9 +48,8 @@ class Pry::InputCompleter
@input.completion_append_character = nil if @input.respond_to?(:completion_append_character=)
end
#
# Return a new completion proc for use by Readline.
#
# rubocop:disable Metrics/AbcSize
def call(str, options = {})
custom_completions = options[:custom_completions] || []
# if there are multiple contexts e.g. cd 1/2/3
@ -74,35 +73,35 @@ class Pry::InputCompleter
# Complete stdlib symbols
case input
when REGEX_REGEXP # Regexp
receiver = $1
message = Regexp.quote($2)
receiver = Regexp.last_match(1)
message = Regexp.quote(Regexp.last_match(2))
candidates = Regexp.instance_methods.collect(&:to_s)
select_message(path, receiver, message, candidates)
when ARRAY_REGEXP # Array
receiver = $1
message = Regexp.quote($2)
receiver = Regexp.last_match(1)
message = Regexp.quote(Regexp.last_match(2))
candidates = Array.instance_methods.collect(&:to_s)
select_message(path, receiver, message, candidates)
when PROC_OR_HASH_REGEXP # Proc or Hash
receiver = $1
message = Regexp.quote($2)
receiver = Regexp.last_match(1)
message = Regexp.quote(Regexp.last_match(2))
candidates = Proc.instance_methods.collect(&:to_s)
candidates |= Hash.instance_methods.collect(&:to_s)
select_message(path, receiver, message, candidates)
when SYMBOL_REGEXP # Symbol
if Symbol.respond_to?(:all_symbols)
sym = Regexp.quote($1)
sym = Regexp.quote(Regexp.last_match(1))
candidates = Symbol.all_symbols.collect { |s| ":" << s.id2name }
candidates.grep(/^#{sym}/)
else
[]
end
when TOPLEVEL_LOOKUP_REGEXP # Absolute Constant or class methods
receiver = $1
receiver = Regexp.last_match(1)
candidates = Object.constants.collect(&:to_s)
candidates.grep(/^#{receiver}/).collect { |e| "::" << e }
when CONSTANT_REGEXP # Constant
message = $1
message = Regexp.last_match(1)
begin
context = target.eval("self")
context = context.class unless context.respond_to? :constants
@ -112,8 +111,8 @@ class Pry::InputCompleter
end
candidates = candidates.grep(/^#{message}/).collect(&path)
when CONSTANT_OR_METHOD_REGEXP # Constant or class methods
receiver = $1
message = Regexp.quote($2)
receiver = Regexp.last_match(1)
message = Regexp.quote(Regexp.last_match(2))
begin
candidates = eval("#{receiver}.constants.collect(&:to_s)", bind)
candidates |= eval("#{receiver}.methods.collect(&:to_s)", bind)
@ -122,14 +121,14 @@ class Pry::InputCompleter
end
candidates.grep(/^#{message}/).collect { |e| receiver + "::" + e }
when SYMBOL_METHOD_CALL_REGEXP # method call on a Symbol
receiver = $1
message = Regexp.quote($2)
receiver = Regexp.last_match(1)
message = Regexp.quote(Regexp.last_match(2))
candidates = Symbol.instance_methods.collect(&:to_s)
select_message(path, receiver, message, candidates)
when NUMERIC_REGEXP
# Numeric
receiver = $1
message = Regexp.quote($5)
receiver = Regexp.last_match(1)
message = Regexp.quote(Regexp.last_match(5))
begin
candidates = eval(receiver, bind).methods.collect(&:to_s)
rescue Pry::RescuableException
@ -138,8 +137,8 @@ class Pry::InputCompleter
select_message(path, receiver, message, candidates)
when HEX_REGEXP
# Numeric(0xFFFF)
receiver = $1
message = Regexp.quote($2)
receiver = Regexp.last_match(1)
message = Regexp.quote(Regexp.last_match(2))
begin
candidates = eval(receiver, bind).methods.collect(&:to_s)
rescue Pry::RescuableException
@ -150,8 +149,8 @@ class Pry::InputCompleter
regmessage = Regexp.new(Regexp.quote($1))
candidates = global_variables.collect(&:to_s).grep(regmessage)
when VARIABLE_REGEXP # variable
receiver = $1
message = Regexp.quote($2)
receiver = Regexp.last_match(1)
message = Regexp.quote(Regexp.last_match(2))
gv = eval("global_variables", bind).collect(&:to_s)
lv = eval("local_variables", bind).collect(&:to_s)
@ -184,7 +183,7 @@ class Pry::InputCompleter
when /^\.([^.]*)$/
# Unknown(maybe String)
receiver = ""
message = Regexp.quote($1)
message = Regexp.quote(Regexp.last_match(1))
candidates = String.instance_methods(true).collect(&:to_s)
select_message(path, receiver, message, candidates)
else
@ -204,6 +203,7 @@ class Pry::InputCompleter
[]
end
end
# rubocop:enable Metrics/AbcSize
def select_message(path, receiver, message, candidates)
candidates.grep(/^#{message}/).collect do |e|

View file

@ -564,7 +564,8 @@ class Pry::Slop
unless option
case flag
when /\A--?([^=]+)=(.+)\z/, /\A-([a-zA-Z])(.+)\z/, /\A--no-(.+)\z/
option, argument = fetch_option($1), ($2 || false)
option = fetch_option(Regexp.last_match(1))
argument = Regexp.last_match(2) || false
option.argument_in_value = true if option
end
end

View file

@ -191,9 +191,13 @@ class Pry::Slop
def value_to_range(value)
case value.to_s
when /\A(\-?\d+)\z/
Range.new($1.to_i, $1.to_i)
Range.new(Regexp.last_match(1).to_i, Regexp.last_match(1).to_i)
when /\A(-?\d+?)(\.\.\.?|-|,)(-?\d+)\z/
Range.new($1.to_i, $3.to_i, $2 == '...')
Range.new(
Regexp.last_match(1).to_i,
Regexp.last_match(3).to_i,
Regexp.last_match(2) == '...'
)
else
if @slop.strict?
raise InvalidArgumentError, "#{value} could not be coerced into Range"

View file

@ -78,7 +78,7 @@ class Pry::Terminal
def screen_size_according_to_ansicon_env
return unless ENV['ANSICON'] =~ /\((.*)x(.*)\)/
size = [$2, $1]
size = [Regexp.last_match(2), Regexp.last_match(1)]
size if nonzero_column?(size)
end