From 4450fa75f0e9f613b5eee01687f3642a962a8525 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 2 Mar 2019 14:01:01 +0200 Subject: [PATCH] rubocop: fix offences of the Style/RegexpLiteral cop --- .rubocop_todo.yml | 19 ------------------- lib/pry/cli.rb | 2 +- lib/pry/command.rb | 2 +- lib/pry/commands/cat/file_formatter.rb | 2 +- lib/pry/commands/easter_eggs.rb | 2 +- lib/pry/commands/edit.rb | 2 +- lib/pry/helpers/documentation_helpers.rb | 10 +++++----- lib/pry/indent.rb | 2 +- lib/pry/input_completer.rb | 6 +++--- lib/pry/object_path.rb | 2 +- lib/pry/rubygem.rb | 2 +- spec/pry_spec.rb | 20 ++++++++++---------- spec/sticky_locals_spec.rb | 4 ++-- 13 files changed, 28 insertions(+), 47 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 300aec39..dbf642dd 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -314,25 +314,6 @@ Style/PerlBackrefs: - 'lib/pry/method.rb' - 'lib/pry/rubygem.rb' -# Offense count: 28 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, AllowInnerSlashes. -# SupportedStyles: slashes, percent_r, mixed -Style/RegexpLiteral: - Exclude: - - 'lib/pry/cli.rb' - - 'lib/pry/command.rb' - - 'lib/pry/commands/cat/file_formatter.rb' - - 'lib/pry/commands/easter_eggs.rb' - - 'lib/pry/commands/edit.rb' - - 'lib/pry/helpers/documentation_helpers.rb' - - 'lib/pry/indent.rb' - - 'lib/pry/input_completer.rb' - - 'lib/pry/object_path.rb' - - 'lib/pry/rubygem.rb' - - 'spec/pry_spec.rb' - - 'spec/sticky_locals_spec.rb' - # Offense count: 12 # Cop supports --auto-correct. Style/RescueModifier: diff --git a/lib/pry/cli.rb b/lib/pry/cli.rb index 9af1d7dd..869a5626 100644 --- a/lib/pry/cli.rb +++ b/lib/pry/cli.rb @@ -193,7 +193,7 @@ Pry::CLI.add_options do on(:I=, "Add a path to the $LOAD_PATH", as: Array, delimiter: ":") do |load_path| load_path.map! do |path| - /\A\.\// =~ path ? path : File.expand_path(path) + %r{\A\./} =~ path ? path : File.expand_path(path) end $LOAD_PATH.unshift(*load_path) diff --git a/lib/pry/command.rb b/lib/pry/command.rb index 414c0b3b..3c5e26d0 100644 --- a/lib/pry/command.rb +++ b/lib/pry/command.rb @@ -201,7 +201,7 @@ class Pry case Pry::Method(block).source_file when %r{/pry/.*_commands/(.*).rb} Regexp.last_match(1).capitalize.tr('_', " ") - when %r{(pry-\w+)-([\d\.]+([\w\.]+)?)} + when /(pry-\w+)-([\d\.]+([\w\.]+)?)/ name = Regexp.last_match(1) version = Regexp.last_match(2) "#{name} (v#{version})" diff --git a/lib/pry/commands/cat/file_formatter.rb b/lib/pry/commands/cat/file_formatter.rb index 4da1d03b..38095ac2 100644 --- a/lib/pry/commands/cat/file_formatter.rb +++ b/lib/pry/commands/cat/file_formatter.rb @@ -21,7 +21,7 @@ class Pry end def file_and_line - file_name, line_num = file_with_embedded_line.split(/:(?!\/|\\)/) + file_name, line_num = file_with_embedded_line.split(%r{:(?!/|\\)}) [file_name, line_num ? line_num.to_i : nil] end diff --git a/lib/pry/commands/easter_eggs.rb b/lib/pry/commands/easter_eggs.rb index 016a74b5..e08b19c7 100644 --- a/lib/pry/commands/easter_eggs.rb +++ b/lib/pry/commands/easter_eggs.rb @@ -4,7 +4,7 @@ class Pry run ".nyancat" end - command(/!s\/(.*?)\/(.*?)/, "") do |source, dest| + command(%r{!s/(.*?)/(.*?)}, "") do |source, dest| eval_string.gsub!(/#{source}/) { dest } run "show-input" end diff --git a/lib/pry/commands/edit.rb b/lib/pry/commands/edit.rb index 270d03b6..bd0a8347 100644 --- a/lib/pry/commands/edit.rb +++ b/lib/pry/commands/edit.rb @@ -194,7 +194,7 @@ class Pry def probably_a_file?(str) [".rb", ".c", ".py", ".yml", ".gemspec"].include?(File.extname(str)) || - str =~ /\/|\\/ + str =~ %r{/|\\} end end diff --git a/lib/pry/helpers/documentation_helpers.rb b/lib/pry/helpers/documentation_helpers.rb index d289a4fe..7f7f0065 100644 --- a/lib/pry/helpers/documentation_helpers.rb +++ b/lib/pry/helpers/documentation_helpers.rb @@ -12,10 +12,10 @@ class Pry def process_rdoc(comment) comment = comment.dup - comment.gsub(/(?:\s*\n)?(.*?)\s*<\/code>/m) { CodeRay.scan(Regexp.last_match(1), :ruby).term } - .gsub(/(?:\s*\n)?(.*?)\s*<\/em>/m) { "\e[1m#{Regexp.last_match(1)}\e[0m" } - .gsub(/(?:\s*\n)?(.*?)\s*<\/i>/m) { "\e[1m#{Regexp.last_match(1)}\e[0m" } - .gsub(/(?:\s*\n)?(.*?)\s*<\/tt>/m) { CodeRay.scan(Regexp.last_match(1), :ruby).term } + comment.gsub(%r{(?:\s*\n)?(.*?)\s*}m) { CodeRay.scan(Regexp.last_match(1), :ruby).term } + .gsub(%r{(?:\s*\n)?(.*?)\s*}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" } + .gsub(%r{(?:\s*\n)?(.*?)\s*}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" } + .gsub(%r{(?:\s*\n)?(.*?)\s*}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}`" } @@ -49,7 +49,7 @@ class Pry # @param [String] code # @return [String] def strip_comments_from_c_code(code) - code.sub(/\A\s*\/\*.*?\*\/\s*/m, '') + code.sub(%r{\A\s*/\*.*?\*/\s*}m, '') end # Given a string that makes up a comment in a source-code file parse out the content diff --git a/lib/pry/indent.rb b/lib/pry/indent.rb index 115dbf0f..86b770f4 100644 --- a/lib/pry/indent.rb +++ b/lib/pry/indent.rb @@ -257,7 +257,7 @@ class Pry # If the code just before an "if" or "while" token on a line looks like the end of a statement, # then we want to treat that "if" as a singleline, not multiline statement. def end_of_statement?(last_token, last_kind) - (last_token =~ /^[)\]}\/]$/ || STATEMENT_END_TOKENS.include?(last_kind)) + (last_token =~ %r{^[)\]\}/]$} || STATEMENT_END_TOKENS.include?(last_kind)) end # Are we currently in the middle of a string literal. diff --git a/lib/pry/input_completer.rb b/lib/pry/input_completer.rb index 7fa28694..39a2e087 100644 --- a/lib/pry/input_completer.rb +++ b/lib/pry/input_completer.rb @@ -6,7 +6,7 @@ class Pry ARRAY_REGEXP = /^([^\]]*\])\.([^.]*)$/.freeze SYMBOL_REGEXP = /^(:[^:.]*)$/.freeze SYMBOL_METHOD_CALL_REGEXP = /^(:[^:.]+)\.([^.]*)$/.freeze - REGEX_REGEXP = /^(\/[^\/]*\/)\.([^.]*)$/.freeze + REGEX_REGEXP = %r{^(/[^/]*/)\.([^.]*)$}.freeze PROC_OR_HASH_REGEXP = /^([^\}]*\})\.([^.]*)$/.freeze TOPLEVEL_LOOKUP_REGEXP = /^::([A-Z][^:\.\(]*)$/.freeze CONSTANT_REGEXP = /^([A-Z][A-Za-z0-9]*)$/.freeze @@ -220,10 +220,10 @@ class Pry # path is a proc that takes an input and builds a full path. def build_path(input) # check to see if the input is a regex - return proc { |i| i.to_s }, input if input[/\/\./] + return proc { |i| i.to_s }, input if input[%r{/\.}] trailing_slash = input.end_with?('/') - contexts = input.chomp('/').split(/\//) + contexts = input.chomp('/').split(%r{/}) input = contexts[-1] path = proc do |i| p = contexts[0..-2].push(i).join('/') diff --git a/lib/pry/object_path.rb b/lib/pry/object_path.rb index 0598cd61..5ea4d74b 100644 --- a/lib/pry/object_path.rb +++ b/lib/pry/object_path.rb @@ -32,7 +32,7 @@ class Pry loop do # Scan for as long as we don't see a slash - next_segment << scanner.scan(/[^\/]*/) + next_segment << scanner.scan(%r{[^/]*}) if complete?(next_segment) || scanner.eos? scanner.getch # consume the slash diff --git a/lib/pry/rubygem.rb b/lib/pry/rubygem.rb index 8cba0090..f089b738 100644 --- a/lib/pry/rubygem.rb +++ b/lib/pry/rubygem.rb @@ -45,7 +45,7 @@ class Pry # @return [Array] completions def complete(so_far) if so_far =~ / ([^ ]*)\z/ - list(%r{\A#{$2}}).map(&:name) + list(/\A#{$2}/).map(&:name) else list.map(&:name) end diff --git a/spec/pry_spec.rb b/spec/pry_spec.rb index ec911f08..baac6dd6 100644 --- a/spec/pry_spec.rb +++ b/spec/pry_spec.rb @@ -195,19 +195,19 @@ describe Pry do describe "newline stripping from an empty string" do it "with double quotes" do - expect(mock_pry('"', '"')).to match(%r{"\\n"}) - expect(mock_pry('"', "\n", "\n", "\n", '"')).to match(%r{"\\n\\n\\n\\n"}) + expect(mock_pry('"', '"')).to match(/"\\n"/) + expect(mock_pry('"', "\n", "\n", "\n", '"')).to match(/"\\n\\n\\n\\n"/) end it "with single quotes" do - expect(mock_pry("'", "'")).to match(%r{"\\n"}) - expect(mock_pry("'", "\n", "\n", "\n", "'")).to match(%r{"\\n\\n\\n\\n"}) + expect(mock_pry("'", "'")).to match(/"\\n"/) + expect(mock_pry("'", "\n", "\n", "\n", "'")).to match(/"\\n\\n\\n\\n"/) end it "with fancy delimiters" do - expect(mock_pry('%(', ')')).to match(%r{"\\n"}) - expect(mock_pry('%|', "\n", "\n", '|')).to match(%r{"\\n\\n\\n"}) - expect(mock_pry('%q[', "\n", "\n", ']')).to match(%r{"\\n\\n\\n"}) + expect(mock_pry('%(', ')')).to match(/"\\n"/) + expect(mock_pry('%|', "\n", "\n", '|')).to match(/"\\n\\n\\n"/) + expect(mock_pry('%q[', "\n", "\n", ']')).to match(/"\\n\\n\\n"/) end end @@ -224,9 +224,9 @@ describe Pry do describe "newline from an empty heredoc" do it "works" do - expect(mock_pry('<