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

Merge pull request #39409 from fatkodima/rubocop-performance-delete_prefix_suffix

Update rubocop-performance gem and enable Performance/DeletePrefix and Performance/DeleteSuffix cops
This commit is contained in:
Ryuta Kamizono 2020-05-25 00:33:41 +09:00 committed by GitHub
commit 0cffb053a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 27 additions and 21 deletions

View file

@ -263,3 +263,9 @@ Performance/ReverseEach:
Performance/UnfreezeString:
Enabled: true
Performance/DeletePrefix:
Enabled: true
Performance/DeleteSuffix:
Enabled: true

View file

@ -347,7 +347,7 @@ GEM
mini_portile2 (~> 2.4.0)
os (1.0.1)
parallel (1.19.1)
parser (2.7.1.1)
parser (2.7.1.2)
ast (~> 2.4.0)
path_expander (1.1.0)
pg (1.2.3)
@ -415,7 +415,7 @@ GEM
rexml
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-performance (1.5.2)
rubocop-performance (1.6.0)
rubocop (>= 0.71.0)
rubocop-rails (2.5.2)
activesupport

View file

@ -133,7 +133,7 @@ module AbstractController
# ==== Returns
# * <tt>String</tt>
def controller_path
@controller_path ||= name.sub(/Controller$/, "").underscore unless anonymous?
@controller_path ||= name.delete_suffix("Controller").underscore unless anonymous?
end
# Refresh the cached action_methods when a new action_method is added.

View file

@ -126,7 +126,7 @@ module ActionController
# ==== Returns
# * <tt>string</tt>
def self.controller_name
@controller_name ||= name.demodulize.sub(/Controller$/, "").underscore
@controller_name ||= name.demodulize.delete_suffix("Controller").underscore
end
def self.make_response!(request)

View file

@ -151,7 +151,7 @@ module ActionController
# try to find Foo::Bar::User, Foo::User and finally User.
def _default_wrap_model
return nil if klass.anonymous?
model_name = klass.name.sub(/Controller$/, "").classify
model_name = klass.name.delete_suffix("Controller").classify
begin
if model_klass = model_name.safe_constantize

View file

@ -1945,7 +1945,7 @@ module ActionDispatch
path_without_format = path.sub(/\(\.:format\)$/, "")
if using_match_shorthand?(path_without_format)
path_without_format.gsub(%r{^/}, "").sub(%r{/([^/]*)$}, '#\1').tr("-", "_")
path_without_format.delete_prefix("/").sub(%r{/([^/]*)$}, '#\1').tr("-", "_")
else
nil
end

View file

@ -176,7 +176,7 @@ end
class Rack::TestCase < ActionDispatch::IntegrationTest
def self.testing(klass = nil)
if klass
@testing = "/#{klass.name.underscore}".sub(/_controller$/, "")
@testing = "/#{klass.name.underscore}".delete_suffix("_controller")
else
@testing
end

View file

@ -115,7 +115,7 @@ module ActionView
end
feed_opts = { "xml:lang" => options[:language] || "en-US", "xmlns" => "http://www.w3.org/2005/Atom" }
feed_opts.merge!(options).reject! { |k, v| !k.to_s.match(/^xml/) }
feed_opts.merge!(options).reject! { |k, v| !k.to_s.start_with?("xml") }
xml.feed(feed_opts) do
xml.id(options[:id] || "tag:#{request.host},#{options[:schema_date]}:#{request.fullpath.split(".")[0]}")

View file

@ -1668,8 +1668,8 @@ module ActionView
convert_to_legacy_options(@options)
if @object_name.to_s.match(/\[\]$/)
if (object ||= @template.instance_variable_get("@#{Regexp.last_match.pre_match}")) && object.respond_to?(:to_param)
if @object_name.to_s.end_with?("[]")
if (object ||= @template.instance_variable_get("@#{@object_name.to_s.delete_suffix("[]")}")) && object.respond_to?(:to_param)
@auto_index = object.to_param
else
raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}"

View file

@ -22,7 +22,7 @@ module ActiveRecord
def load(yaml)
return object_class.new if object_class != Object && yaml.nil?
return yaml unless yaml.is_a?(String) && /^---/.match?(yaml)
return yaml unless yaml.is_a?(String) && yaml.start_with?("---")
obj = YAML.load(yaml)
assert_valid_value(obj, action: "load")

View file

@ -35,7 +35,7 @@ module ActiveRecord
private
def number_for_point(number)
number.to_s.gsub(/\.0$/, "")
number.to_s.delete_suffix(".0")
end
end
end

View file

@ -51,7 +51,7 @@ module ActiveRecord
private
def number_for_point(number)
number.to_s.gsub(/\.0$/, "")
number.to_s.delete_suffix(".0")
end
def build_point(x, y)

View file

@ -42,7 +42,7 @@ module ActiveRecord
# Return a Hash that can be merged into the main config that represents
# the passed in url
def build_url_hash
if url.nil? || /^jdbc:/.match?(url)
if url.nil? || url.start_with?("jdbc:")
{ url: url }
else
ConnectionUrlResolver.new(url).to_hash

View file

@ -59,7 +59,7 @@ module ActiveSupport #:nodoc:
# Forward all undefined methods to the wrapped string.
def method_missing(method, *args, &block)
result = @wrapped_string.__send__(method, *args, &block)
if /!$/.match?(method)
if method.end_with?("!")
self if result
else
result.kind_of?(String) ? chars(result) : result

View file

@ -358,8 +358,8 @@ module Rails
end
def method_missing(method, *args)
if method =~ /=$/
@configurations[$`.to_sym] = args.first
if method.to_s.end_with?("=")
@configurations[method.to_s.delete_suffix("=").to_sym] = args.first
else
@configurations.fetch(method) {
@configurations[method] = ActiveSupport::OrderedOptions.new

View file

@ -109,7 +109,7 @@ module Rails
end
def extract_environment_from_path(path)
available_environments.find { |env| path.include? env } if path.match?(/\.yml\.enc$/)
available_environments.find { |env| path.include? env } if path.end_with?(".yml.enc")
end
def encryption_key_file_generator

View file

@ -131,7 +131,7 @@ module Rails
end
def foreign_key?
/_id$/.match?(name)
name.end_with?("_id")
end
def reference?

View file

@ -88,8 +88,8 @@ module Rails
private
def method_missing(name, *args, &blk)
if name.to_s =~ /=$/
@@options[$`.to_sym] = args.first
if name.to_s.end_with?("=")
@@options[name.to_s.delete_suffix("=").to_sym] = args.first
elsif @@options.key?(name)
@@options[name]
else