提交 eb5fea40 编写于 作者: B Bart de Water

Enable Start/EndWith and RegexpMatch cops

In cases where the MatchData object is not used, this provides a speed-up:
https://github.com/JuanitoFatas/fast-ruby/#stringmatch-vs-stringmatch-vs-stringstart_withstringend_with-code-start-code-end
上级 8741052b
......@@ -193,3 +193,12 @@ Performance/FlatMap:
Performance/RedundantMerge:
Enabled: true
Performance/StartWith:
Enabled: true
Performance/EndWith:
Enabled: true
Performance/RegexpMatch:
Enabled: true
......@@ -57,7 +57,7 @@ namespace :assets do
end
print "[verify] #{file} is a UMD module "
if pathname.read =~ /module\.exports.*define\.amd/m
if /module\.exports.*define\.amd/m.match?(pathname.read)
puts "[OK]"
else
$stderr.puts "[FAIL]"
......
......@@ -17,7 +17,7 @@ def initialize(error, path)
@path = "helpers/#{path}.rb"
set_backtrace error.backtrace
if error.path =~ /^#{path}(\.rb)?$/
if /^#{path}(\.rb)?$/.match?(error.path)
super("Missing helper file helpers/%s.rb" % path)
else
raise error
......
......@@ -121,7 +121,7 @@ def env; @req.env.dup; end
# not contained within the headers hash.
def env_name(key)
key = key.to_s
if key =~ HTTP_HEADER
if HTTP_HEADER.match?(key)
key = key.upcase.tr("-", "_")
key = "HTTP_" + key unless CGI_VARIABLES.include?(key)
end
......
......@@ -113,7 +113,7 @@ def flag_cookies_as_secure!(headers)
cookies = cookies.split("\n".freeze)
headers["Set-Cookie".freeze] = cookies.map { |cookie|
if cookie !~ /;\s*secure\s*(;|$)/i
if !/;\s*secure\s*(;|$)/i.match?(cookie)
"#{cookie}; secure"
else
cookie
......
......@@ -279,7 +279,7 @@ def normalize_format(formatted)
def verify_regexp_requirements(requirements)
requirements.each do |requirement|
if requirement.source =~ ANCHOR_CHARACTERS_REGEX
if ANCHOR_CHARACTERS_REGEX.match?(requirement.source)
raise ArgumentError, "Regexp anchor characters are not allowed in routing requirements: #{requirement.inspect}"
end
......@@ -333,7 +333,7 @@ def check_part(name, part, path_params, hash)
end
def split_to(to)
if to =~ /#/
if /#/.match?(to)
to.split("#")
else
[]
......@@ -342,7 +342,7 @@ def split_to(to)
def add_controller_module(controller, modyoule)
if modyoule && !controller.is_a?(Regexp)
if controller =~ %r{\A/}
if %r{\A/}.match?(controller)
controller[1..-1]
else
[modyoule, controller].compact.join("/")
......@@ -1588,7 +1588,7 @@ def match(path, *rest, &block)
when Symbol
options[:action] = to
when String
if to =~ /#/
if /#/.match?(to)
options[:to] = to
else
options[:controller] = to
......@@ -1914,7 +1914,7 @@ def add_route(action, controller, options, _path, to, via, formatted, anchor, op
default_action = options.delete(:action) || @scope[:action]
if action =~ /^[\w\-\/]+$/
if /^[\w\-\/]+$/.match?(action)
default_action ||= action.tr("-", "_") unless action.include?("/")
else
action = nil
......
......@@ -78,7 +78,7 @@ def assert_recognizes(expected_options, path, extras = {}, msg = nil)
# # Asserts that the generated route gives us our custom route
# assert_generates "changesets/12", { controller: 'scm', action: 'show_diff', revision: "12" }
def assert_generates(expected_path, options, defaults = {}, extras = {}, message = nil)
if expected_path =~ %r{://}
if %r{://}.match?(expected_path)
fail_on(URI::InvalidURIError, message) do
uri = URI.parse(expected_path)
expected_path = uri.path.to_s.empty? ? "/" : uri.path
......@@ -189,7 +189,7 @@ def recognized_request_for(path, extras = {}, msg)
request = ActionController::TestRequest.create @controller.class
if path =~ %r{://}
if %r{://}.match?(path)
fail_on(URI::InvalidURIError, msg) do
uri = URI.parse(path)
request.env["rack.url_scheme"] = uri.scheme || "http"
......
......@@ -217,7 +217,7 @@ def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: n
method = :post
end
if path =~ %r{://}
if %r{://}.match?(path)
path = build_expanded_path(path) do |location|
https! URI::HTTPS === location if location.scheme
......
......@@ -107,7 +107,7 @@ namespace :assets do
end
print "[verify] #{file} is a UMD module "
if pathname.read =~ /module\.exports.*define\.amd/m
if /module\.exports.*define\.amd/m.match?(pathname.read)
puts "[OK]"
else
$stderr.puts "[FAIL]"
......
......@@ -15,7 +15,7 @@ def render
def validate_color_string(string)
regex = /#[0-9a-fA-F]{6}/
if regex.match(string)
if regex.match?(string)
string.downcase
else
"#000000"
......
......@@ -188,7 +188,7 @@ def excerpt(text, phrase, options = {})
unless separator.empty?
text.split(separator).each do |value|
if value.match(regex)
if value.match?(regex)
phrase = value
break
end
......
......@@ -81,7 +81,7 @@ class AbstractAdapter
alias :in_use? :owner
def self.type_cast_config_to_integer(config)
if config =~ SIMPLE_INT
if SIMPLE_INT.match?(config)
config.to_i
else
config
......
......@@ -245,7 +245,7 @@ def perform_calculation(operation, column_name)
if distinct && (group_values.any? || select_values.empty? && order_values.empty?)
column_name = primary_key
end
elsif column_name =~ /\s*DISTINCT[\s(]+/i
elsif /\s*DISTINCT[\s(]+/i.match?(column_name)
distinct = nil
end
end
......
......@@ -30,7 +30,7 @@ namespace :guides do
unless Kindlerb.kindlegen_available?
abort "Please run `setupkindlerb` to install kindlegen"
end
unless `convert` =~ /convert/
unless /convert/.match?(`convert`)
abort "Please install ImageMagick"
end
ENV["KINDLE"] = "1"
......
......@@ -35,7 +35,7 @@ def generate(output_dir, mobi_outfile, logfile)
def generate_front_matter(html_pages)
frontmatter = []
html_pages.delete_if { |x|
if x =~ /(toc|welcome|copyright).html/
if /(toc|welcome|copyright).html/.match?(x)
frontmatter << x unless x =~ /toc/
true
end
......
......@@ -69,7 +69,7 @@ def engine
end
def extract_raw_header_and_body
if @raw_body =~ /^\-{40,}$/
if /^\-{40,}$/.match?(@raw_body)
@raw_header, _, @raw_body = @raw_body.partition(/^\-{40,}$/).map(&:strip)
end
end
......@@ -89,7 +89,7 @@ def generate_structure
hierarchy = []
doc.children.each do |node|
if node.name =~ /^h[3-6]$/
if /^h[3-6]$/.match?(node.name)
case node.name
when "h3"
hierarchy = [node]
......
......@@ -35,7 +35,7 @@ def header(text, header_level)
def paragraph(text)
if text =~ %r{^NOTE:\s+Defined\s+in\s+<code>(.*?)</code>\.?$}
%(<div class="note"><p>Defined in <code><a href="#{github_file_url($1)}">#{$1}</a></code>.</p></div>)
elsif text =~ /^(TIP|IMPORTANT|CAUTION|WARNING|NOTE|INFO|TODO)[.:]/
elsif /^(TIP|IMPORTANT|CAUTION|WARNING|NOTE|INFO|TODO)[.:]/.match?(text)
convert_notes(text)
elsif text.include?("DO NOT READ THIS FILE ON GITHUB")
elsif text =~ /^\[<sup>(\d+)\]:<\/sup> (.+)$/
......@@ -110,7 +110,7 @@ def github_file_url(file_path)
end
def api_link(url)
if url =~ %r{http://api\.rubyonrails\.org/v\d+\.}
if %r{http://api\.rubyonrails\.org/v\d+\.}.match?(url)
url
elsif edge
url.sub("api", "edgeapi")
......
......@@ -49,7 +49,7 @@ def exec_app
if exe = find_executable
contents = File.read(exe)
if contents =~ /(APP|ENGINE)_PATH/
if /(APP|ENGINE)_PATH/.match?(contents)
exec RUBY, exe, *ARGV
break # non reachable, hack to be able to stub exec in the test suite
elsif exe.end_with?("bin/rails") && contents.include?("This file was generated by Bundler")
......
......@@ -46,7 +46,7 @@ def calculate_directory_statistics(directory, pattern = /^(?!\.).*?\.(rb|js|coff
if File.directory?(path) && (/^\./ !~ file_name)
stats.add(calculate_directory_statistics(path, pattern))
elsif file_name =~ pattern
elsif file_name&.match?(pattern)
stats.add_by_file_path(path)
end
end
......
......@@ -42,7 +42,7 @@ def edit
rescue Rails::Secrets::MissingKeyError => error
say error.message
rescue Errno::ENOENT => error
if error.message =~ /secrets\.yml\.enc/
if /secrets\.yml\.enc/.match?(error.message)
deprecate_in_favor_of_credentials_and_exit
else
raise
......
......@@ -376,7 +376,7 @@ def javascript_runtime_gemfile_entry
comment = "See https://github.com/rails/execjs#readme for more supported runtimes"
if defined?(JRUBY_VERSION)
GemfileEntry.version "therubyrhino", nil, comment
elsif RUBY_PLATFORM =~ /mingw|mswin/
elsif RUBY_PLATFORM.match?(/mingw|mswin/)
GemfileEntry.version "duktape", nil, comment
else
GemfileEntry.new "mini_racer", nil, comment, { platforms: :ruby }, true
......
......@@ -515,7 +515,7 @@ def app_const
end
def valid_const?
if app_const =~ /^\d/
if /^\d/.match?(app_const)
raise Error, "Invalid application name #{original_app_name}. Please give a name which does not start with numbers."
elsif RESERVED_NAMES.include?(original_app_name)
raise Error, "Invalid application name #{original_app_name}. Please give a " \
......
......@@ -385,11 +385,11 @@ def email
end
def valid_const?
if original_name =~ /-\d/
if /-\d/.match?(original_name)
raise Error, "Invalid plugin name #{original_name}. Please give a name which does not contain a namespace starting with numeric characters."
elsif original_name =~ /[^\w-]+/
elsif /[^\w-]+/.match?(original_name)
raise Error, "Invalid plugin name #{original_name}. Please give a name which uses only alphabetic, numeric, \"_\" or \"-\" characters."
elsif camelized =~ /^\d/
elsif /^\d/.match?(camelized)
raise Error, "Invalid plugin name #{original_name}. Please give a name which does not start with numbers."
elsif RESERVED_NAMES.include?(name)
raise Error, "Invalid plugin name #{original_name}. Please give a " \
......
......@@ -63,7 +63,7 @@ def extract_filters(argv)
# Extract absolute and relative paths but skip -n /.*/ regexp filters.
argv.select { |arg| arg =~ %r%^/?\w+/% && !arg.end_with?("/") }.map do |path|
case
when path =~ /(:\d+)+$/
when /(:\d+)+$/.match?(path)
file, *lines = path.split(":")
filters << [ file, lines ]
file
......
......@@ -89,7 +89,7 @@
if File.exist?("#{framework}/package.json")
Dir.chdir("#{framework}") do
npm_tag = version =~ /[a-z]/ ? "pre" : "latest"
npm_tag = /[a-z]/.match?(version) ? "pre" : "latest"
sh "npm publish --tag #{npm_tag}"
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册