mirror of https://github.com/rails/rails
Address to false negative for Performance/DeletePrefix,DeleteSuffix
Follow up to c07dff7227
.
Actually it is not the cop's fault, but we mistakenly use `^`, `$`, and
`\Z` in much places, the cop doesn't correct those conservatively.
I've checked all those usage and replaced all safe ones.
This commit is contained in:
parent
d55120e429
commit
528b62e386
|
@ -14,7 +14,7 @@ module ActionCable
|
|||
# Chats::AppearancesChannel.channel_name # => 'chats:appearances'
|
||||
# FooChats::BarAppearancesChannel.channel_name # => 'foo_chats:bar_appearances'
|
||||
def channel_name
|
||||
@channel_name ||= name.sub(/Channel$/, "").gsub("::", ":").underscore
|
||||
@channel_name ||= name.delete_suffix("Channel").gsub("::", ":").underscore
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -114,7 +114,7 @@ module ActionMailer
|
|||
|
||||
# Returns the underscored name of the mailer preview without the suffix.
|
||||
def preview_name
|
||||
name.sub(/Preview$/, "").underscore
|
||||
name.delete_suffix("Preview").underscore
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -50,9 +50,9 @@ module ActionDispatch
|
|||
ENV_METHODS.each do |env|
|
||||
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
||||
# frozen_string_literal: true
|
||||
def #{env.sub(/^HTTP_/n, '').downcase} # def accept_charset
|
||||
get_header "#{env}" # get_header "HTTP_ACCEPT_CHARSET"
|
||||
end # end
|
||||
def #{env.delete_prefix("HTTP_").downcase} # def accept_charset
|
||||
get_header "#{env}" # get_header "HTTP_ACCEPT_CHARSET"
|
||||
end # end
|
||||
METHOD
|
||||
end
|
||||
|
||||
|
|
|
@ -171,7 +171,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
u = URI("http://example.org/foo/bar.html")
|
||||
assert_equal u.path.sub(/^\//, ""), get(u)
|
||||
assert_equal u.path.delete_prefix("/"), get(u)
|
||||
end
|
||||
|
||||
def test_star_paths_are_greedy_but_not_too_much
|
||||
|
@ -196,7 +196,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
u = URI("http://example.org/ne_27.065938,-80.6092/sw_25.489856,-82.542794")
|
||||
assert_equal u.path.sub(/^\//, ""), get(u)
|
||||
assert_equal u.path.delete_prefix("/"), get(u)
|
||||
end
|
||||
|
||||
def test_optional_star_paths_are_greedy_but_not_too_much
|
||||
|
|
|
@ -2175,7 +2175,7 @@ module ActionView
|
|||
index = if options.has_key?(:index)
|
||||
options[:index]
|
||||
elsif defined?(@auto_index)
|
||||
object_name = object_name.to_s.sub(/\[\]$/, "")
|
||||
object_name = object_name.to_s.delete_suffix("[]")
|
||||
@auto_index
|
||||
end
|
||||
|
||||
|
|
|
@ -129,11 +129,11 @@ module ActionView
|
|||
end
|
||||
|
||||
def sanitized_object_name
|
||||
@sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
|
||||
@sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").delete_suffix("_")
|
||||
end
|
||||
|
||||
def sanitized_method_name
|
||||
@sanitized_method_name ||= @method_name.sub(/\?$/, "")
|
||||
@sanitized_method_name ||= @method_name.delete_suffix("?")
|
||||
end
|
||||
|
||||
def sanitized_value(value)
|
||||
|
|
|
@ -190,7 +190,7 @@ module ActionView
|
|||
end
|
||||
|
||||
def short_identifier
|
||||
@short_identifier ||= defined?(Rails.root) ? identifier.sub("#{Rails.root}/", "") : identifier
|
||||
@short_identifier ||= defined?(Rails.root) ? identifier.delete_prefix("#{Rails.root}/") : identifier
|
||||
end
|
||||
|
||||
def inspect
|
||||
|
|
|
@ -11,7 +11,7 @@ ActionController::Base.view_paths = [ File.expand_path("../../fixtures/actionpac
|
|||
|
||||
class LayoutTest < ActionController::Base
|
||||
def self.controller_path; "views" end
|
||||
def self._implied_layout_name; to_s.underscore.gsub(/_controller$/, "") ; end
|
||||
def self._implied_layout_name; to_s.underscore.delete_suffix("_controller") ; end
|
||||
self.view_paths = ActionController::Base.view_paths.dup
|
||||
end
|
||||
|
||||
|
|
|
@ -85,7 +85,7 @@ class PolymorphicRoutesTest < ActionController::TestCase
|
|||
def assert_url(url, args)
|
||||
host = self.class.default_url_options[:host]
|
||||
|
||||
assert_equal url.sub(/http:\/\/#{host}/, ""), polymorphic_path(args)
|
||||
assert_equal url.delete_prefix("http://#{host}"), polymorphic_path(args)
|
||||
assert_equal url, polymorphic_url(args)
|
||||
assert_equal url, url_for(args)
|
||||
end
|
||||
|
@ -733,7 +733,7 @@ class PolymorphicPathRoutesTest < PolymorphicRoutesTest
|
|||
def assert_url(url, args)
|
||||
host = self.class.default_url_options[:host]
|
||||
|
||||
assert_equal url.sub(/http:\/\/#{host}/, ""), url_for(args)
|
||||
assert_equal url.delete_prefix("http://#{host}"), url_for(args)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -166,7 +166,7 @@ module ActiveModel
|
|||
|
||||
raise ArgumentError, "Class name cannot be blank. You need to supply a name argument when anonymous class given" if @name.blank?
|
||||
|
||||
@unnamespaced = @name.sub(/^#{namespace.name}::/, "") if namespace
|
||||
@unnamespaced = @name.delete_prefix("#{namespace.name}::") if namespace
|
||||
@klass = klass
|
||||
@singular = _singularize(@name)
|
||||
@plural = ActiveSupport::Inflector.pluralize(@singular)
|
||||
|
|
|
@ -150,7 +150,7 @@ module ActiveRecord
|
|||
# Person.attribute_method?(:age=) # => true
|
||||
# Person.attribute_method?(:nothing) # => false
|
||||
def attribute_method?(attribute)
|
||||
super || (table_exists? && column_names.include?(attribute.to_s.sub(/=$/, "")))
|
||||
super || (table_exists? && column_names.include?(attribute.to_s.delete_suffix("=")))
|
||||
end
|
||||
|
||||
# Returns an array of column names as strings if it's not an abstract class and
|
||||
|
|
|
@ -10,8 +10,8 @@ module ActiveRecord
|
|||
when "infinity" then ::Float::INFINITY
|
||||
when "-infinity" then -::Float::INFINITY
|
||||
when / BC$/
|
||||
astronomical_year = format("%04d", -value[/^\d+/].to_i + 1)
|
||||
super(value.sub(/ BC$/, "").sub(/^\d+/, astronomical_year))
|
||||
value = value.sub(/^\d+/) { |year| format("%04d", -year.to_i + 1) }
|
||||
super(value.delete_suffix!(" BC"))
|
||||
else
|
||||
super
|
||||
end
|
||||
|
|
|
@ -10,8 +10,8 @@ module ActiveRecord
|
|||
when "infinity" then ::Float::INFINITY
|
||||
when "-infinity" then -::Float::INFINITY
|
||||
when / BC$/
|
||||
astronomical_year = format("%04d", -value[/^\d+/].to_i + 1)
|
||||
super(value.sub(/ BC$/, "").sub(/^\d+/, astronomical_year))
|
||||
value = value.sub(/^\d+/) { |year| format("%04d", -year.to_i + 1) }
|
||||
super(value.delete_suffix!(" BC"))
|
||||
else
|
||||
super
|
||||
end
|
||||
|
|
|
@ -90,7 +90,7 @@ module ActiveRecord
|
|||
# Only SQLite uses a filename as the "database" name; for
|
||||
# anything else, a leading slash would be silly.
|
||||
|
||||
uri.path.sub(%r{^/}, "")
|
||||
uri.path.delete_prefix("/")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,6 +4,6 @@ class LoadError
|
|||
# Returns true if the given path name (except perhaps for the ".rb"
|
||||
# extension) is the missing file which caused the exception to be raised.
|
||||
def is_missing?(location)
|
||||
location.sub(/\.rb$/, "") == path.to_s.sub(/\.rb$/, "")
|
||||
location.delete_suffix(".rb") == path.to_s.delete_suffix(".rb")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -28,7 +28,7 @@ class Module
|
|||
end
|
||||
|
||||
def attr_internal_define(attr_name, type)
|
||||
internal_name = attr_internal_ivar_name(attr_name).sub(/\A@/, "")
|
||||
internal_name = attr_internal_ivar_name(attr_name).delete_prefix("@")
|
||||
# use native attr_* methods as they are faster on some Ruby implementations
|
||||
send("attr_#{type}", internal_name)
|
||||
attr_name, internal_name = "#{attr_name}=", "#{internal_name}=" if type == :writer
|
||||
|
|
|
@ -722,7 +722,7 @@ module ActiveSupport #:nodoc:
|
|||
# A module, class, symbol, or string may be provided.
|
||||
def to_constant_name(desc) #:nodoc:
|
||||
case desc
|
||||
when String then desc.sub(/^::/, "")
|
||||
when String then desc.delete_prefix("::")
|
||||
when Symbol then desc.to_s
|
||||
when Module
|
||||
real_mod_name(desc) ||
|
||||
|
|
|
@ -1650,7 +1650,7 @@ Mailer test cases obtain the mailer being tested from the name of the test class
|
|||
```ruby
|
||||
# action_mailer/test_case.rb
|
||||
def determine_default_mailer(name)
|
||||
name.sub(/Test$/, '').constantize
|
||||
name.delete_suffix("Test").constantize
|
||||
rescue NameError => e
|
||||
raise NonInferrableMailerError.new(name)
|
||||
end
|
||||
|
@ -3662,7 +3662,7 @@ For example, when an action of `ArticlesController` is called Rails tries optimi
|
|||
|
||||
```ruby
|
||||
def default_helper_module!
|
||||
module_name = name.sub(/Controller$/, '')
|
||||
module_name = name.delete_suffix("Controller")
|
||||
module_path = module_name.underscore
|
||||
helper module_path
|
||||
rescue LoadError => e
|
||||
|
@ -3685,7 +3685,7 @@ For example, when an action of `ArticlesController` is called Rails tries to loa
|
|||
|
||||
```ruby
|
||||
def default_helper_module!
|
||||
module_name = name.sub(/Controller$/, '')
|
||||
module_name = name.delete_suffix("Controller")
|
||||
module_path = module_name.underscore
|
||||
helper module_path
|
||||
rescue LoadError => e
|
||||
|
|
|
@ -56,7 +56,7 @@ module Rails
|
|||
def lookup!
|
||||
$LOAD_PATH.each do |base|
|
||||
Dir[File.join(base, *file_lookup_paths)].each do |path|
|
||||
path = path.sub("#{base}/", "")
|
||||
path = path.delete_prefix("#{base}/")
|
||||
require path
|
||||
rescue Exception
|
||||
# No problem
|
||||
|
|
|
@ -137,7 +137,7 @@ module Rails
|
|||
end
|
||||
|
||||
def method_missing(method, *args)
|
||||
method = method.to_s.sub(/=$/, "").to_sym
|
||||
method = method.to_s.delete_suffix("=").to_sym
|
||||
|
||||
if args.empty?
|
||||
if method == :rails
|
||||
|
|
|
@ -213,7 +213,7 @@ module Rails
|
|||
end
|
||||
|
||||
rails = groups.delete("rails")
|
||||
rails.map! { |n| n.sub(/^rails:/, "") }
|
||||
rails.map! { |n| n.delete_prefix("rails:") }
|
||||
rails.delete("app")
|
||||
rails.delete("plugin")
|
||||
rails.delete("encrypted_secrets")
|
||||
|
|
|
@ -53,7 +53,7 @@ module Rails
|
|||
# is removed.
|
||||
def self.namespace(name = nil)
|
||||
return super if name
|
||||
@namespace ||= super.sub(/_generator$/, "").sub(/:generators:/, ":")
|
||||
@namespace ||= super.delete_suffix("_generator").sub(/:generators:/, ":")
|
||||
end
|
||||
|
||||
# Convenience method to hide this generator from the available ones when
|
||||
|
@ -319,7 +319,7 @@ module Rails
|
|||
|
||||
# Use Rails default banner.
|
||||
def self.banner # :doc:
|
||||
"rails generate #{namespace.sub(/^rails:/, '')} #{arguments.map(&:usage).join(' ')} [options]".gsub(/\s+/, " ")
|
||||
"rails generate #{namespace.delete_prefix("rails:")} #{arguments.map(&:usage).join(' ')} [options]".gsub(/\s+/, " ")
|
||||
end
|
||||
|
||||
# Sets the base_name taking into account the current class namespace.
|
||||
|
@ -336,7 +336,7 @@ module Rails
|
|||
def self.generator_name # :doc:
|
||||
@generator_name ||= begin
|
||||
if generator = name.to_s.split("::").last
|
||||
generator.sub!(/Generator$/, "")
|
||||
generator.delete_suffix!("Generator")
|
||||
generator.underscore
|
||||
end
|
||||
end
|
||||
|
|
|
@ -107,11 +107,11 @@ module Rails
|
|||
end
|
||||
|
||||
def plural_name
|
||||
name.sub(/_id$/, "").pluralize
|
||||
name.delete_suffix("_id").pluralize
|
||||
end
|
||||
|
||||
def singular_name
|
||||
name.sub(/_id$/, "").singularize
|
||||
name.delete_suffix("_id").singularize
|
||||
end
|
||||
|
||||
def human_name
|
||||
|
|
|
@ -430,7 +430,7 @@ end
|
|||
|
||||
def relative_path
|
||||
return unless inside_application?
|
||||
app_path.sub(/^#{rails_app_path}\//, "")
|
||||
app_path.delete_prefix("#{rails_app_path}/")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
<%- if attribute.password_digest? -%>
|
||||
password_digest: <%%= BCrypt::Password.create('secret') %>
|
||||
<%- elsif attribute.reference? -%>
|
||||
<%= yaml_key_value(attribute.column_name.sub(/_id$/, ''), attribute.default || name) %>
|
||||
<%= yaml_key_value(attribute.column_name.delete_suffix("_id"), attribute.default || name) %>
|
||||
<%- elsif !attribute.virtual? -%>
|
||||
<%= yaml_key_value(attribute.column_name, attribute.default) %>
|
||||
<%- end -%>
|
||||
|
|
|
@ -104,7 +104,7 @@ module Rails
|
|||
|
||||
def migration_file_name(relative)
|
||||
absolute = File.expand_path(relative, destination_root)
|
||||
dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, "")
|
||||
dirname, file_name = File.dirname(absolute), File.basename(absolute).delete_suffix(".rb")
|
||||
Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first
|
||||
end
|
||||
end
|
||||
|
|
|
@ -42,7 +42,7 @@ module GeneratorsTestHelper
|
|||
setup :prepare_destination
|
||||
|
||||
begin
|
||||
base.tests Rails::Generators.const_get(base.name.sub(/Test$/, ""))
|
||||
base.tests Rails::Generators.const_get(base.name.delete_suffix("Test"))
|
||||
rescue
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue