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

Merge pull request #39152 from kamipo/deprecate_starts_ends_with

Deprecate `starts_with?` and `ends_with?` for String core extensions
This commit is contained in:
Ryuta Kamizono 2020-05-05 22:33:30 +09:00 committed by GitHub
commit 55e038cf17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 40 additions and 47 deletions

View file

@ -29,7 +29,7 @@ class AssetHostTest < ActionMailer::TestCase
def test_asset_host_as_one_argument_proc def test_asset_host_as_one_argument_proc
AssetHostMailer.config.asset_host = Proc.new { |source| AssetHostMailer.config.asset_host = Proc.new { |source|
if source.starts_with?("/images") if source.start_with?("/images")
"http://images.example.com" "http://images.example.com"
end end
} }

View file

@ -114,7 +114,7 @@ module ActionDispatch
# True if an ETag is set and it's a weak validator (preceded with W/) # True if an ETag is set and it's a weak validator (preceded with W/)
def weak_etag? def weak_etag?
etag? && etag.starts_with?('W/"') etag? && etag.start_with?('W/"')
end end
# True if an ETag is set and it isn't a weak validator (not preceded with W/) # True if an ETag is set and it isn't a weak validator (not preceded with W/)

View file

@ -1,7 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
require "singleton" require "singleton"
require "active_support/core_ext/string/starts_ends_with"
module Mime module Mime
class Mimes class Mimes
@ -117,7 +116,7 @@ module Mime
type = list[idx] type = list[idx]
break if type.q < app_xml.q break if type.q < app_xml.q
if type.name.ends_with? "+xml" if type.name.end_with? "+xml"
list[app_xml_idx], list[idx] = list[idx], app_xml list[app_xml_idx], list[idx] = list[idx], app_xml
app_xml_idx = idx app_xml_idx = idx
end end
@ -306,7 +305,7 @@ module Mime
def to_a; end def to_a; end
def method_missing(method, *args) def method_missing(method, *args)
if method.to_s.ends_with? "?" if method.to_s.end_with? "?"
method[0..-2].downcase.to_sym == to_sym method[0..-2].downcase.to_sym == to_sym
else else
super super
@ -314,7 +313,7 @@ module Mime
end end
def respond_to_missing?(method, include_private = false) def respond_to_missing?(method, include_private = false)
(method.to_s.ends_with? "?") || super (method.to_s.end_with? "?") || super
end end
end end
@ -349,11 +348,11 @@ module Mime
private private
def respond_to_missing?(method, _) def respond_to_missing?(method, _)
method.to_s.ends_with? "?" method.to_s.end_with? "?"
end end
def method_missing(method, *args) def method_missing(method, *args)
false if method.to_s.ends_with? "?" false if method.to_s.end_with? "?"
end end
end end
end end

View file

@ -80,7 +80,7 @@ module ActionView
# absolute path of the asset, for example "/assets/rails.png". # absolute path of the asset, for example "/assets/rails.png".
# #
# ActionController::Base.asset_host = Proc.new { |source| # ActionController::Base.asset_host = Proc.new { |source|
# if source.ends_with?('.css') # if source.end_with?('.css')
# "http://stylesheets.example.com" # "http://stylesheets.example.com"
# else # else
# "http://assets.example.com" # "http://assets.example.com"
@ -206,7 +206,7 @@ module ActionView
relative_url_root = defined?(config.relative_url_root) && config.relative_url_root relative_url_root = defined?(config.relative_url_root) && config.relative_url_root
if relative_url_root if relative_url_root
source = File.join(relative_url_root, source) unless source.starts_with?("#{relative_url_root}/") source = File.join(relative_url_root, source) unless source.start_with?("#{relative_url_root}/")
end end
if host = compute_asset_host(source, options) if host = compute_asset_host(source, options)

View file

@ -134,7 +134,7 @@ module ActionView
# # <option selected="selected">MasterCard</option></select> # # <option selected="selected">MasterCard</option></select>
def select_tag(name, option_tags = nil, options = {}) def select_tag(name, option_tags = nil, options = {})
option_tags ||= "" option_tags ||= ""
html_name = (options[:multiple] == true && !name.to_s.ends_with?("[]")) ? "#{name}[]" : name html_name = (options[:multiple] == true && !name.to_s.end_with?("[]")) ? "#{name}[]" : name
if options.include?(:include_blank) if options.include?(:include_blank)
include_blank = options[:include_blank] include_blank = options[:include_blank]

View file

@ -9,7 +9,7 @@ module ActiveRecord
exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", "SCHEMA").map do |row| exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", "SCHEMA").map do |row|
# Indexes SQLite creates implicitly for internal use start with "sqlite_". # Indexes SQLite creates implicitly for internal use start with "sqlite_".
# See https://www.sqlite.org/fileformat2.html#intschema # See https://www.sqlite.org/fileformat2.html#intschema
next if row["name"].starts_with?("sqlite_") next if row["name"].start_with?("sqlite_")
index_sql = query_value(<<~SQL, "SCHEMA") index_sql = query_value(<<~SQL, "SCHEMA")
SELECT sql SELECT sql

View file

@ -26,7 +26,7 @@ module ActiveRecord
# Allow database path relative to Rails.root, but only if the database # Allow database path relative to Rails.root, but only if the database
# path is not the special path that tells sqlite to build a database only # path is not the special path that tells sqlite to build a database only
# in memory. # in memory.
if ":memory:" != config[:database] && !config[:database].to_s.starts_with?("file:") if ":memory:" != config[:database] && !config[:database].to_s.start_with?("file:")
config[:database] = File.expand_path(config[:database], Rails.root) if defined?(Rails.root) config[:database] = File.expand_path(config[:database], Rails.root) if defined?(Rails.root)
dirname = File.dirname(config[:database]) dirname = File.dirname(config[:database])
Dir.mkdir(dirname) unless File.directory?(dirname) Dir.mkdir(dirname) unless File.directory?(dirname)

View file

@ -44,7 +44,7 @@ module ActiveRecord
if fixture_set_names.first == :all if fixture_set_names.first == :all
raise StandardError, "No fixture path found. Please set `#{self}.fixture_path`." if fixture_path.blank? raise StandardError, "No fixture path found. Please set `#{self}.fixture_path`." if fixture_path.blank?
fixture_set_names = Dir[::File.join(fixture_path, "{**,*}/*.{yml}")].uniq fixture_set_names = Dir[::File.join(fixture_path, "{**,*}/*.{yml}")].uniq
fixture_set_names.reject! { |f| f.starts_with?(file_fixture_path.to_s) } if defined?(file_fixture_path) && file_fixture_path fixture_set_names.reject! { |f| f.start_with?(file_fixture_path.to_s) } if defined?(file_fixture_path) && file_fixture_path
fixture_set_names.map! { |f| f[fixture_path.to_s.size..-5].delete_prefix("/") } fixture_set_names.map! { |f| f[fixture_path.to_s.size..-5].delete_prefix("/") }
else else
fixture_set_names = fixture_set_names.flatten.map(&:to_s) fixture_set_names = fixture_set_names.flatten.map(&:to_s)

View file

@ -139,8 +139,8 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
blob.open do |file| blob.open do |file|
assert file.binmode? assert file.binmode?
assert_equal 0, file.pos assert_equal 0, file.pos
assert File.basename(file.path).starts_with?("ActiveStorage-#{blob.id}-") assert File.basename(file.path).start_with?("ActiveStorage-#{blob.id}-")
assert file.path.ends_with?(".jpg") assert file.path.end_with?(".jpg")
assert_equal file_fixture("racecar.jpg").binread, file.read, "Expected downloaded file to match fixture file" assert_equal file_fixture("racecar.jpg").binread, file.read, "Expected downloaded file to match fixture file"
end end
end end
@ -161,7 +161,7 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
assert file.binmode? assert file.binmode?
assert_equal 0, file.pos assert_equal 0, file.pos
assert_match(/\.jpg\z/, file.path) assert_match(/\.jpg\z/, file.path)
assert file.path.starts_with?(tmpdir) assert file.path.start_with?(tmpdir)
assert_equal file_fixture("racecar.jpg").binread, file.read, "Expected downloaded file to match fixture file" assert_equal file_fixture("racecar.jpg").binread, file.read, "Expected downloaded file to match fixture file"
end end
end end

View file

@ -1,3 +1,8 @@
* Deprecate `starts_with?` and `ends_with?` for String core extensions.
Use the native `start_with?` and `end_with?` instead.
*Ryuta Kamizono*
* Add override of unary plus for `ActiveSupport::Duration`. * Add override of unary plus for `ActiveSupport::Duration`.
`+ 1.second` is now identical to `+1.second` to prevent errors `+ 1.second` is now identical to `+1.second` to prevent errors

View file

@ -3,4 +3,6 @@
class String class String
alias_method :starts_with?, :start_with? alias_method :starts_with?, :start_with?
alias_method :ends_with?, :end_with? alias_method :ends_with?, :end_with?
deprecate starts_with?: :start_with?
deprecate ends_with?: :end_with?
end end

View file

@ -12,7 +12,6 @@ require "active_support/core_ext/object/blank"
require "active_support/core_ext/kernel/reporting" require "active_support/core_ext/kernel/reporting"
require "active_support/core_ext/load_error" require "active_support/core_ext/load_error"
require "active_support/core_ext/name_error" require "active_support/core_ext/name_error"
require "active_support/core_ext/string/starts_ends_with"
require "active_support/dependencies/interlock" require "active_support/dependencies/interlock"
require "active_support/inflector" require "active_support/inflector"
@ -453,7 +452,7 @@ module ActiveSupport #:nodoc:
# Search for a file in autoload_paths matching the provided suffix. # Search for a file in autoload_paths matching the provided suffix.
def search_for_file(path_suffix) def search_for_file(path_suffix)
path_suffix += ".rb" unless path_suffix.ends_with?(".rb") path_suffix += ".rb" unless path_suffix.end_with?(".rb")
autoload_paths.each do |root| autoload_paths.each do |root|
path = File.join(root, path_suffix) path = File.join(root, path_suffix)
@ -473,9 +472,9 @@ module ActiveSupport #:nodoc:
end end
def load_once_path?(path) def load_once_path?(path)
# to_s works around a ruby issue where String#starts_with?(Pathname) # to_s works around a ruby issue where String#start_with?(Pathname)
# will raise a TypeError: no implicit conversion of Pathname into String # will raise a TypeError: no implicit conversion of Pathname into String
autoload_once_paths.any? { |base| path.starts_with? base.to_s } autoload_once_paths.any? { |base| path.start_with?(base.to_s) }
end end
# Attempt to autoload the provided module name by searching for a directory # Attempt to autoload the provided module name by searching for a directory
@ -585,7 +584,7 @@ module ActiveSupport #:nodoc:
end end
name_error = NameError.new("uninitialized constant #{qualified_name}", const_name) name_error = NameError.new("uninitialized constant #{qualified_name}", const_name)
name_error.set_backtrace(caller.reject { |l| l.starts_with? __FILE__ }) name_error.set_backtrace(caller.reject { |l| l.start_with?(__FILE__) })
raise name_error raise name_error
end end

View file

@ -239,15 +239,15 @@ class StringInflectionsTest < ActiveSupport::TestCase
assert_equal 97, "abc".ord assert_equal 97, "abc".ord
end end
def test_starts_ends_with_alias def test_deprecated_starts_ends_with_alias
s = "hello" s = "hello"
assert s.starts_with?("h") assert_deprecated { assert s.starts_with?("h") }
assert s.starts_with?("hel") assert_deprecated { assert s.starts_with?("hel") }
assert_not s.starts_with?("el") assert_deprecated { assert_not s.starts_with?("el") }
assert s.ends_with?("o") assert_deprecated { assert s.ends_with?("o") }
assert s.ends_with?("lo") assert_deprecated { assert s.ends_with?("lo") }
assert_not s.ends_with?("el") assert_deprecated { assert_not s.ends_with?("el") }
end end
def test_string_squish def test_string_squish

View file

@ -985,7 +985,7 @@ and performs the validation on it. The custom validator is called using the
```ruby ```ruby
class MyValidator < ActiveModel::Validator class MyValidator < ActiveModel::Validator
def validate(record) def validate(record)
unless record.name.starts_with? 'X' unless record.name.start_with? 'X'
record.errors.add :name, "Need a name starting with X please!" record.errors.add :name, "Need a name starting with X please!"
end end
end end

View file

@ -1212,17 +1212,6 @@ The `inquiry` method converts a string into a `StringInquirer` object making equ
NOTE: Defined in `active_support/core_ext/string/inquiry.rb`. NOTE: Defined in `active_support/core_ext/string/inquiry.rb`.
### `starts_with?` and `ends_with?`
Active Support defines 3rd person aliases of `String#start_with?` and `String#end_with?`:
```ruby
"foo".starts_with?("f") # => true
"foo".ends_with?("o") # => true
```
NOTE: Defined in `active_support/core_ext/string/starts_ends_with.rb`.
### `strip_heredoc` ### `strip_heredoc`
The method `strip_heredoc` strips indentation in heredocs. The method `strip_heredoc` strips indentation in heredocs.

View file

@ -4,7 +4,6 @@ require "isolation/abstract_unit"
require "rack/test" require "rack/test"
require "env_helpers" require "env_helpers"
require "set" require "set"
require "active_support/core_ext/string/starts_ends_with"
class ::MyMailInterceptor class ::MyMailInterceptor
def self.delivering_email(email); email; end def self.delivering_email(email); email; end
@ -1710,8 +1709,8 @@ module ApplicationTests
test "autoload paths do not include asset paths" do test "autoload paths do not include asset paths" do
app "development" app "development"
ActiveSupport::Dependencies.autoload_paths.each do |path| ActiveSupport::Dependencies.autoload_paths.each do |path|
assert_not_operator path, :ends_with?, "app/assets" assert_not_operator path, :end_with?, "app/assets"
assert_not_operator path, :ends_with?, "app/javascript" assert_not_operator path, :end_with?, "app/javascript"
end end
end end
@ -1722,8 +1721,8 @@ module ApplicationTests
app "development" app "development"
ActiveSupport::Dependencies.autoload_paths.each do |path| ActiveSupport::Dependencies.autoload_paths.each do |path|
assert_not_operator path, :ends_with?, "app/assets" assert_not_operator path, :end_with?, "app/assets"
assert_not_operator path, :ends_with?, "app/webpack" assert_not_operator path, :end_with?, "app/webpack"
end end
end end
@ -1733,7 +1732,7 @@ module ApplicationTests
# Action Mailer modifies AS::Dependencies.autoload_paths in-place. # Action Mailer modifies AS::Dependencies.autoload_paths in-place.
autoload_paths = ActiveSupport::Dependencies.autoload_paths autoload_paths = ActiveSupport::Dependencies.autoload_paths
autoload_paths_from_app_and_engines = autoload_paths.reject do |path| autoload_paths_from_app_and_engines = autoload_paths.reject do |path|
path.ends_with?("mailers/previews") path.end_with?("mailers/previews")
end end
assert_equal true, Rails.configuration.add_autoload_paths_to_load_path assert_equal true, Rails.configuration.add_autoload_paths_to_load_path
assert_empty autoload_paths_from_app_and_engines - $LOAD_PATH assert_empty autoload_paths_from_app_and_engines - $LOAD_PATH