brew vendor-gems: commit updates.

This commit is contained in:
Mike McQuaid 2020-05-22 08:30:19 +01:00
parent bab32515fa
commit d02b15d31a
No known key found for this signature in database
GPG Key ID: 48A898132FD8EE70
36 changed files with 391 additions and 93 deletions

View File

@ -12,6 +12,8 @@ $:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/zeitwerk-2.3.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/activesupport-6.0.3.1/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/ast-2.4.0/lib"
$:.unshift "#{path}/"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/extensions/universal-darwin-19/2.6.0/byebug-11.1.3"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/byebug-11.1.3/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/connection_pool-2.2.2/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/extensions/universal-darwin-19/2.6.0/json-2.3.0"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/json-2.3.0/lib"
@ -59,10 +61,10 @@ $:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rspec-3.9.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rspec-its-1.3.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rspec-retry-0.6.2/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rspec-wait-0.0.9/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-ast-0.0.3/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/ruby-progressbar-1.10.1/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/unicode-display_width-1.7.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-0.83.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-performance-1.5.2/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-0.84.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-performance-1.6.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-rspec-1.39.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/ruby-macho-2.2.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/byebug-11.1.3/lib"

View File

@ -1,5 +1,10 @@
# This is the default configuration file.
Performance/BindCall:
Description: 'Use `bind_call(obj, args, ...)` instead of `bind(obj).call(args, ...)`.'
Enabled: true
VersionAdded: '1.6'
Performance/Caller:
Description: >-
Use `caller(n..n)` instead of `caller`.
@ -21,6 +26,7 @@ Performance/Casecmp:
Use `casecmp` rather than `downcase ==`, `upcase ==`, `== downcase`, or `== upcase`..
Reference: 'https://github.com/JuanitoFatas/fast-ruby#stringcasecmp-vs-stringdowncase---code'
Enabled: true
Safe: false
VersionAdded: '0.36'
Performance/ChainArrayAllocation:
@ -49,6 +55,16 @@ Performance/Count:
VersionAdded: '0.31'
VersionChanged: '1.5'
Performance/DeletePrefix:
Description: 'Use `delete_prefix` instead of `gsub`.'
Enabled: true
VersionAdded: '1.6'
Performance/DeleteSuffix:
Description: 'Use `delete_suffix` instead of `gsub`.'
Enabled: true
VersionAdded: '1.6'
Performance/Detect:
Description: >-
Use `detect` instead of `select.first`, `find_all.first`,
@ -87,7 +103,7 @@ Performance/EndWith:
VersionChanged: '0.44'
Performance/FixedSize:
Description: 'Do not compute the size of statically sized objects except in constants'
Description: 'Do not compute the size of statically sized objects except in constants.'
Enabled: true
VersionAdded: '0.35'
@ -95,7 +111,7 @@ Performance/FlatMap:
Description: >-
Use `Enumerable#flat_map`
instead of `Enumerable#map...Array#flatten(1)`
or `Enumberable#collect..Array#flatten(1)`
or `Enumberable#collect..Array#flatten(1)`.
Reference: 'https://github.com/JuanitoFatas/fast-ruby#enumerablemaparrayflatten-vs-enumerableflat_map-code'
Enabled: true
VersionAdded: '0.30'
@ -106,7 +122,7 @@ Performance/FlatMap:
# `flatten` without any parameters can flatten multiple levels.
Performance/InefficientHashSearch:
Description: 'Use `key?` or `value?` instead of `keys.include?` or `values.include?`'
Description: 'Use `key?` or `value?` instead of `keys.include?` or `values.include?`.'
Reference: 'https://github.com/JuanitoFatas/fast-ruby#hashkey-instead-of-hashkeysinclude-code'
Enabled: true
VersionAdded: '0.56'

View File

@ -0,0 +1,41 @@
# frozen_string_literal: true
module RuboCop
module Cop
# Common functionality for handling regexp metacharacters.
module RegexpMetacharacter
def literal_at_start?(regex_str)
# is this regexp 'literal' in the sense of only matching literal
# chars, rather than using metachars like `.` and `*` and so on?
# also, is it anchored at the start of the string?
# (tricky: \s, \d, and so on are metacharacters, but other characters
# escaped with a slash are just literals. LITERAL_REGEX takes all
# that into account.)
regex_str =~ /\A(\\A|\^)(?:#{Util::LITERAL_REGEX})+\z/
end
def literal_at_end?(regex_str)
# is this regexp 'literal' in the sense of only matching literal
# chars, rather than using metachars like . and * and so on?
# also, is it anchored at the end of the string?
regex_str =~ /\A(?:#{Util::LITERAL_REGEX})+(\\z|\$)\z/
end
def drop_start_metacharacter(regexp_string)
if regexp_string.start_with?('\\A')
regexp_string[2..-1] # drop `\A` anchor
else
regexp_string[1..-1] # drop `^` anchor
end
end
def drop_end_metacharacter(regexp_string)
if regexp_string.end_with?('\\z')
regexp_string.chomp('\z') # drop `\z` anchor
else
regexp_string.chop # drop `$` anchor
end
end
end
end
end

View File

@ -0,0 +1,87 @@
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
# In Ruby 2.7, `UnboundMethod#bind_call` has been added.
#
# This cop identifies places where `bind(obj).call(args, ...)`
# can be replaced by `bind_call(obj, args, ...)`.
#
# The `bind_call(obj, args, ...)` method is faster than
# `bind(obj).call(args, ...)`.
#
# @example
# # bad
# umethod.bind(obj).call(foo, bar)
# umethod.bind(obj).(foo, bar)
#
# # good
# umethod.bind_call(obj, foo, bar)
#
class BindCall < Cop
include RangeHelp
extend TargetRubyVersion
minimum_target_ruby_version 2.7
MSG = 'Use `bind_call(%<bind_arg>s%<comma>s%<call_args>s)` ' \
'instead of `bind(%<bind_arg>s).call(%<call_args>s)`.'
def_node_matcher :bind_with_call_method?, <<~PATTERN
(send
$(send
(send nil? _) :bind
$(...)) :call
$...)
PATTERN
def on_send(node)
bind_with_call_method?(node) do |receiver, bind_arg, call_args_node|
range = correction_range(receiver, node)
call_args = build_call_args(call_args_node)
message = message(bind_arg.source, call_args)
add_offense(node, location: range, message: message)
end
end
def autocorrect(node)
receiver, bind_arg, call_args_node = bind_with_call_method?(node)
range = correction_range(receiver, node)
call_args = build_call_args(call_args_node)
call_args = ", #{call_args}" unless call_args.empty?
replacement_method = "bind_call(#{bind_arg.source}#{call_args})"
lambda do |corrector|
corrector.replace(range, replacement_method)
end
end
private
def message(bind_arg, call_args)
comma = call_args.empty? ? '' : ', '
format(MSG, bind_arg: bind_arg, comma: comma, call_args: call_args)
end
def correction_range(receiver, node)
location_of_bind = receiver.loc.selector.begin_pos
location_of_call = node.loc.end.end_pos
range_between(location_of_bind, location_of_call)
end
def build_call_args(call_args_node)
call_args_node.map(&:source).join(', ')
end
end
end
end
end

View File

@ -24,14 +24,14 @@ module RuboCop
MSG_FIRST = 'Use `%<method>s(%<n>d..%<n>d).first`' \
' instead of `%<method>s.first`.'
def_node_matcher :slow_caller?, <<-PATTERN
def_node_matcher :slow_caller?, <<~PATTERN
{
(send nil? {:caller :caller_locations})
(send nil? {:caller :caller_locations} int)
}
PATTERN
def_node_matcher :caller_with_scope_method?, <<-PATTERN
def_node_matcher :caller_with_scope_method?, <<~PATTERN
{
(send #slow_caller? :first)
(send #slow_caller? :[] int)

View File

@ -5,6 +5,8 @@ module RuboCop
module Performance
# This cop identifies places where a case-insensitive string comparison
# can better be implemented using `casecmp`.
# This cop is unsafe because `String#casecmp` and `String#casecmp?` behave
# differently when using Non-ASCII characters.
#
# @example
# # bad
@ -21,21 +23,21 @@ module RuboCop
MSG = 'Use `%<good>s` instead of `%<bad>s`.'
CASE_METHODS = %i[downcase upcase].freeze
def_node_matcher :downcase_eq, <<-PATTERN
def_node_matcher :downcase_eq, <<~PATTERN
(send
$(send _ ${:downcase :upcase})
${:== :eql? :!=}
${str (send _ {:downcase :upcase} ...) (begin str)})
PATTERN
def_node_matcher :eq_downcase, <<-PATTERN
def_node_matcher :eq_downcase, <<~PATTERN
(send
{str (send _ {:downcase :upcase} ...) (begin str)}
${:== :eql? :!=}
$(send _ ${:downcase :upcase}))
PATTERN
def_node_matcher :downcase_downcase, <<-PATTERN
def_node_matcher :downcase_downcase, <<~PATTERN
(send
$(send _ ${:downcase :upcase})
${:== :eql? :!=}

View File

@ -51,7 +51,7 @@ module RuboCop
'(followed by `return array` if required) instead of chaining '\
'`%<method>s...%<second_method>s`.'
def_node_matcher :flat_map_candidate?, <<-PATTERN
def_node_matcher :flat_map_candidate?, <<~PATTERN
{
(send (send _ ${#{RETURN_NEW_ARRAY_WHEN_ARGS}} {int lvar ivar cvar gvar}) ${#{HAS_MUTATION_ALTERNATIVE}} $...)
(send (block (send _ ${#{ALWAYS_RETURNS_NEW_ARRAY} }) ...) ${#{HAS_MUTATION_ALTERNATIVE}} $...)

View File

@ -30,14 +30,14 @@ module RuboCop
'`%<compare_method>s { |%<var_a>s, %<var_b>s| %<str_a>s ' \
'<=> %<str_b>s }`.'
def_node_matcher :compare?, <<-PATTERN
def_node_matcher :compare?, <<~PATTERN
(block
$(send _ {:sort :min :max})
(args (arg $_a) (arg $_b))
$send)
PATTERN
def_node_matcher :replaceable_body?, <<-PATTERN
def_node_matcher :replaceable_body?, <<~PATTERN
(send
(send (lvar %1) $_method $...)
:<=>

View File

@ -42,7 +42,7 @@ module RuboCop
MSG = 'Use `count` instead of `%<selector>s...%<counter>s`.'
def_node_matcher :count_candidate?, <<-PATTERN
def_node_matcher :count_candidate?, <<~PATTERN
{
(send (block $(send _ ${:select :reject}) ...) ${:count :length :size})
(send $(send _ ${:select :reject} (:block_pass _)) ${:count :length :size})

View File

@ -0,0 +1,72 @@
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
# In Ruby 2.5, `String#delete_prefix` has been added.
#
# This cop identifies places where `gsub(/\Aprefix/, '')`
# can be replaced by `delete_prefix('prefix')`.
#
# The `delete_prefix('prefix')` method is faster than
# `gsub(/\Aprefix/, '')`.
#
# @example
#
# # bad
# str.gsub(/\Aprefix/, '')
# str.gsub!(/\Aprefix/, '')
# str.gsub(/^prefix/, '')
# str.gsub!(/^prefix/, '')
#
# # good
# str.delete_prefix('prefix')
# str.delete_prefix!('prefix')
#
class DeletePrefix < Cop
extend TargetRubyVersion
include RegexpMetacharacter
minimum_target_ruby_version 2.5
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
PREFERRED_METHODS = {
gsub: :delete_prefix,
gsub!: :delete_prefix!
}.freeze
def_node_matcher :gsub_method?, <<~PATTERN
(send $!nil? ${:gsub :gsub!} (regexp (str $#literal_at_start?) (regopt)) (str $_))
PATTERN
def on_send(node)
gsub_method?(node) do |_, bad_method, _, replace_string|
return unless replace_string.blank?
good_method = PREFERRED_METHODS[bad_method]
message = format(MSG, current: bad_method, prefer: good_method)
add_offense(node, location: :selector, message: message)
end
end
def autocorrect(node)
gsub_method?(node) do |receiver, bad_method, regexp_str, _|
lambda do |corrector|
good_method = PREFERRED_METHODS[bad_method]
regexp_str = drop_start_metacharacter(regexp_str)
regexp_str = interpret_string_escapes(regexp_str)
string_literal = to_string_literal(regexp_str)
new_code = "#{receiver.source}.#{good_method}(#{string_literal})"
corrector.replace(node, new_code)
end
end
end
end
end
end
end

View File

@ -0,0 +1,72 @@
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
# In Ruby 2.5, `String#delete_suffix` has been added.
#
# This cop identifies places where `gsub(/suffix\z/, '')`
# can be replaced by `delete_suffix('suffix')`.
#
# The `delete_suffix('suffix')` method is faster than
# `gsub(/suffix\z/, '')`.
#
# @example
#
# # bad
# str.gsub(/suffix\z/, '')
# str.gsub!(/suffix\z/, '')
# str.gsub(/suffix$/, '')
# str.gsub!(/suffix$/, '')
#
# # good
# str.delete_suffix('suffix')
# str.delete_suffix!('suffix')
#
class DeleteSuffix < Cop
extend TargetRubyVersion
include RegexpMetacharacter
minimum_target_ruby_version 2.5
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
PREFERRED_METHODS = {
gsub: :delete_suffix,
gsub!: :delete_suffix!
}.freeze
def_node_matcher :gsub_method?, <<~PATTERN
(send $!nil? ${:gsub :gsub!} (regexp (str $#literal_at_end?) (regopt)) (str $_))
PATTERN
def on_send(node)
gsub_method?(node) do |_, bad_method, _, replace_string|
return unless replace_string.blank?
good_method = PREFERRED_METHODS[bad_method]
message = format(MSG, current: bad_method, prefer: good_method)
add_offense(node, location: :selector, message: message)
end
end
def autocorrect(node)
gsub_method?(node) do |receiver, bad_method, regexp_str, _|
lambda do |corrector|
good_method = PREFERRED_METHODS[bad_method]
regexp_str = drop_end_metacharacter(regexp_str)
regexp_str = interpret_string_escapes(regexp_str)
string_literal = to_string_literal(regexp_str)
new_code = "#{receiver.source}.#{good_method}(#{string_literal})"
corrector.replace(node, new_code)
end
end
end
end
end
end
end

View File

@ -28,7 +28,7 @@ module RuboCop
REVERSE_MSG = 'Use `reverse.%<prefer>s` instead of ' \
'`%<first_method>s.%<second_method>s`.'
def_node_matcher :detect_candidate?, <<-PATTERN
def_node_matcher :detect_candidate?, <<~PATTERN
{
(send $(block (send _ {:select :find_all}) ...) ${:first :last} $...)
(send $(send _ {:select :find_all} ...) ${:first :last} $...)

View File

@ -75,13 +75,13 @@ module RuboCop
cop_config['IncludeActiveSupportAliases']
end
def_node_matcher :two_start_end_with_calls, <<-PATTERN
def_node_matcher :two_start_end_with_calls, <<~PATTERN
(or
(send $_recv [{:start_with? :end_with?} $_method] $...)
(send _recv _method $...))
PATTERN
def_node_matcher :check_with_active_support_aliases, <<-PATTERN
def_node_matcher :check_with_active_support_aliases, <<~PATTERN
(or
(send $_recv
[{:start_with? :starts_with? :end_with? :ends_with?} $_method]

View File

@ -15,26 +15,27 @@ module RuboCop
# 'abc'.match(/bc\Z/)
# /bc\Z/.match('abc')
#
# 'abc'.match?(/bc$/)
# /bc$/.match?('abc')
# 'abc' =~ /bc$/
# /bc$/ =~ 'abc'
# 'abc'.match(/bc$/)
# /bc$/.match('abc')
#
# # good
# 'abc'.end_with?('bc')
class EndWith < Cop
include RegexpMetacharacter
MSG = 'Use `String#end_with?` instead of a regex match anchored to ' \
'the end of the string.'
SINGLE_QUOTE = "'"
def_node_matcher :redundant_regex?, <<-PATTERN
def_node_matcher :redundant_regex?, <<~PATTERN
{(send $!nil? {:match :=~ :match?} (regexp (str $#literal_at_end?) (regopt)))
(send (regexp (str $#literal_at_end?) (regopt)) {:match :match?} $_)
(match-with-lvasgn (regexp (str $#literal_at_end?) (regopt)) $_)}
PATTERN
def literal_at_end?(regex_str)
# is this regexp 'literal' in the sense of only matching literal
# chars, rather than using metachars like . and * and so on?
# also, is it anchored at the end of the string?
regex_str =~ /\A(?:#{LITERAL_REGEX})+\\z\z/
end
def on_send(node)
return unless redundant_regex?(node)
@ -45,7 +46,7 @@ module RuboCop
def autocorrect(node)
redundant_regex?(node) do |receiver, regex_str|
receiver, regex_str = regex_str, receiver if receiver.is_a?(String)
regex_str = regex_str[0..-3] # drop \Z anchor
regex_str = drop_end_metacharacter(regex_str)
regex_str = interpret_string_escapes(regex_str)
lambda do |corrector|

View File

@ -48,7 +48,7 @@ module RuboCop
class FixedSize < Cop
MSG = 'Do not compute the size of statically sized objects.'
def_node_matcher :counter, <<-MATCHER
def_node_matcher :counter, <<~MATCHER
(send ${array hash str sym} {:count :length :size} $...)
MATCHER

View File

@ -22,7 +22,7 @@ module RuboCop
'and `flatten` can be used to flatten ' \
'multiple levels.'
def_node_matcher :flat_map_candidate?, <<-PATTERN
def_node_matcher :flat_map_candidate?, <<~PATTERN
(send
{
(block $(send _ ${:collect :map}) ...)

View File

@ -37,7 +37,7 @@ module RuboCop
# h = { a: 1, b: 2 }; h.value?(nil)
#
class InefficientHashSearch < Cop
def_node_matcher :inefficient_include?, <<-PATTERN
def_node_matcher :inefficient_include?, <<~PATTERN
(send (send $_ {:keys :values}) :include? _)
PATTERN

View File

@ -31,7 +31,7 @@ module RuboCop
MSG = 'Consider using `Struct` over `OpenStruct` ' \
'to optimize the performance.'
def_node_matcher :open_struct, <<-PATTERN
def_node_matcher :open_struct, <<~PATTERN
(send (const {nil? cbase} :OpenStruct) :new ...)
PATTERN

View File

@ -31,7 +31,7 @@ module RuboCop
# Right now, we only detect direct calls on a Range literal
# (We don't even catch it if the Range is in double parens)
def_node_matcher :range_include, <<-PATTERN
def_node_matcher :range_include, <<~PATTERN
(send {irange erange (begin {irange erange})} :include? ...)
PATTERN

View File

@ -29,16 +29,16 @@ module RuboCop
CLOSE_PAREN = ')'
SPACE = ' '
def_node_matcher :blockarg_def, <<-PATTERN
def_node_matcher :blockarg_def, <<~PATTERN
{(def _ (args ... (blockarg $_)) $_)
(defs _ _ (args ... (blockarg $_)) $_)}
PATTERN
def_node_search :blockarg_calls, <<-PATTERN
def_node_search :blockarg_calls, <<~PATTERN
(send (lvar %1) :call ...)
PATTERN
def_node_search :blockarg_assigned?, <<-PATTERN
def_node_search :blockarg_assigned?, <<~PATTERN
(lvasgn %1 ...)
PATTERN

View File

@ -23,12 +23,12 @@ module RuboCop
# 'match' is a fairly generic name, so we don't flag it unless we see
# a string or regexp literal on one side or the other
def_node_matcher :match_call?, <<-PATTERN
def_node_matcher :match_call?, <<~PATTERN
{(send {str regexp} :match _)
(send !nil? :match {str regexp})}
PATTERN
def_node_matcher :only_truthiness_matters?, <<-PATTERN
def_node_matcher :only_truthiness_matters?, <<~PATTERN
^({if while until case while_post until_post} equal?(%0) ...)
PATTERN

View File

@ -5,11 +5,25 @@ module RuboCop
module Performance
# This cop identifies places where `Hash#merge!` can be replaced by
# `Hash#[]=`.
# You can set the maximum number of key-value pairs to consider
# an offense with `MaxKeyValuePairs`.
#
# @example
# # bad
# hash.merge!(a: 1)
# hash.merge!({'key' => 'value'})
#
# # good
# hash[:a] = 1
# hash['key'] = 'value'
#
# @example MaxKeyValuePairs: 2 (default)
# # bad
# hash.merge!(a: 1, b: 2)
#
# # good
# hash[:a] = 1
# hash[:b] = 2
class RedundantMerge < Cop
AREF_ASGN = '%<receiver>s[%<key>s] = %<value>s'
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
@ -20,11 +34,11 @@ module RuboCop
%<leading_space>send
RUBY
def_node_matcher :redundant_merge_candidate, <<-PATTERN
def_node_matcher :redundant_merge_candidate, <<~PATTERN
(send $!nil? :merge! [(hash $...) !kwsplat_type?])
PATTERN
def_node_matcher :modifier_flow_control?, <<-PATTERN
def_node_matcher :modifier_flow_control?, <<~PATTERN
[{if while until} modifier_form?]
PATTERN
@ -168,13 +182,11 @@ module RuboCop
end
def unwind(receiver)
while receiver.respond_to?(:send_type?) && receiver.send_type?
receiver, = *receiver
end
receiver, = *receiver while receiver.respond_to?(:send_type?) && receiver.send_type?
receiver
end
def_node_matcher :each_with_object_node, <<-PATTERN
def_node_matcher :each_with_object_node, <<~PATTERN
(block (send _ :each_with_object _) (args _ $_) ...)
PATTERN
end

View File

@ -73,32 +73,28 @@ module RuboCop
# end
# end
class RegexpMatch < Cop
extend TargetRubyVersion
minimum_target_ruby_version 2.4
# Constants are included in this list because it is unlikely that
# someone will store `nil` as a constant and then use it for comparison
TYPES_IMPLEMENTING_MATCH = %i[const regexp str sym].freeze
MSG = 'Use `match?` instead of `%<current>s` when `MatchData` ' \
'is not used.'
def_node_matcher :match_method?, <<-PATTERN
def_node_matcher :match_method?, <<~PATTERN
{
(send _recv :match {regexp str sym})
(send {regexp str sym} :match _)
}
PATTERN
def_node_matcher :match_with_int_arg_method?, <<-PATTERN
def_node_matcher :match_with_int_arg_method?, <<~PATTERN
(send _recv :match _ (int ...))
PATTERN
def_node_matcher :match_operator?, <<-PATTERN
def_node_matcher :match_operator?, <<~PATTERN
(send !nil? {:=~ :!~} !nil?)
PATTERN
def_node_matcher :match_threequals?, <<-PATTERN
def_node_matcher :match_threequals?, <<~PATTERN
(send (regexp (str _) {(regopt) (regopt _)}) :=== !nil?)
PATTERN
@ -109,7 +105,7 @@ module RuboCop
regexp.to_regexp.named_captures.empty?
end
MATCH_NODE_PATTERN = <<-PATTERN
MATCH_NODE_PATTERN = <<~PATTERN
{
#match_method?
#match_with_int_arg_method?
@ -122,7 +118,7 @@ module RuboCop
def_node_matcher :match_node?, MATCH_NODE_PATTERN
def_node_search :search_match_nodes, MATCH_NODE_PATTERN
def_node_search :last_matches, <<-PATTERN
def_node_search :last_matches, <<~PATTERN
{
(send (const nil? :Regexp) :last_match)
(send (const nil? :Regexp) :last_match _)
@ -256,6 +252,13 @@ module RuboCop
def correct_operator(corrector, recv, arg, oper = nil)
op_range = correction_range(recv, arg)
replace_with_match_predicate_method(corrector, recv, arg, op_range)
corrector.insert_after(arg.loc.expression, ')') unless op_range.source.end_with?('(')
corrector.insert_before(recv.loc.expression, '!') if oper == :!~
end
def replace_with_match_predicate_method(corrector, recv, arg, op_range)
if TYPES_IMPLEMENTING_MATCH.include?(recv.type)
corrector.replace(op_range, '.match?(')
elsif TYPES_IMPLEMENTING_MATCH.include?(arg.type)
@ -264,9 +267,6 @@ module RuboCop
else
corrector.replace(op_range, '&.match?(')
end
corrector.insert_after(arg.loc.expression, ')')
corrector.insert_before(recv.loc.expression, '!') if oper == :!~
end
def swap_receiver_and_arg(corrector, recv, arg)

View File

@ -18,7 +18,7 @@ module RuboCop
MSG = 'Use `reverse_each` instead of `reverse.each`.'
UNDERSCORE = '_'
def_node_matcher :reverse_each?, <<-MATCHER
def_node_matcher :reverse_each?, <<~MATCHER
(send $(send _ :reverse) :each)
MATCHER
@ -34,7 +34,8 @@ module RuboCop
end
def autocorrect(node)
->(corrector) { corrector.replace(node.loc.dot, UNDERSCORE) }
range = range_between(node.loc.dot.begin_pos, node.loc.selector.begin_pos)
->(corrector) { corrector.replace(range, UNDERSCORE) }
end
end
end

View File

@ -15,29 +15,27 @@ module RuboCop
# 'abc'.match(/\Aab/)
# /\Aab/.match('abc')
#
# 'abc'.match?(/^ab/)
# /^ab/.match?('abc')
# 'abc' =~ /^ab/
# /^ab/ =~ 'abc'
# 'abc'.match(/^ab/)
# /^ab/.match('abc')
#
# # good
# 'abc'.start_with?('ab')
class StartWith < Cop
include RegexpMetacharacter
MSG = 'Use `String#start_with?` instead of a regex match anchored to ' \
'the beginning of the string.'
SINGLE_QUOTE = "'"
def_node_matcher :redundant_regex?, <<-PATTERN
def_node_matcher :redundant_regex?, <<~PATTERN
{(send $!nil? {:match :=~ :match?} (regexp (str $#literal_at_start?) (regopt)))
(send (regexp (str $#literal_at_start?) (regopt)) {:match :match?} $_)
(match-with-lvasgn (regexp (str $#literal_at_start?) (regopt)) $_)}
PATTERN
def literal_at_start?(regex_str)
# is this regexp 'literal' in the sense of only matching literal
# chars, rather than using metachars like `.` and `*` and so on?
# also, is it anchored at the start of the string?
# (tricky: \s, \d, and so on are metacharacters, but other characters
# escaped with a slash are just literals. LITERAL_REGEX takes all
# that into account.)
regex_str =~ /\A\\A(?:#{LITERAL_REGEX})+\z/
end
def on_send(node)
return unless redundant_regex?(node)
@ -48,7 +46,7 @@ module RuboCop
def autocorrect(node)
redundant_regex?(node) do |receiver, regex_str|
receiver, regex_str = regex_str, receiver if receiver.is_a?(String)
regex_str = regex_str[2..-1] # drop \A anchor
regex_str = drop_start_metacharacter(regex_str)
regex_str = interpret_string_escapes(regex_str)
lambda do |corrector|

View File

@ -26,9 +26,8 @@ module RuboCop
DELETE = 'delete'
TR = 'tr'
BANG = '!'
SINGLE_QUOTE = "'"
def_node_matcher :string_replacement?, <<-PATTERN
def_node_matcher :string_replacement?, <<~PATTERN
(send _ {:gsub :gsub!}
${regexp str (send (const nil? :Regexp) {:new :compile} _)}
$str)
@ -48,9 +47,7 @@ module RuboCop
first_source, = first_source(first_param)
second_source, = *second_param
unless first_param.str_type?
first_source = interpret_string_escapes(first_source)
end
first_source = interpret_string_escapes(first_source) unless first_param.str_type?
replacement_method =
replacement_method(node, first_source, second_source)
@ -67,9 +64,7 @@ module RuboCop
to_string_literal(first))
end
if second.empty? && first.length == 1
remove_second_param(corrector, node, first_param)
end
remove_second_param(corrector, node, first_param) if second.empty? && first.length == 1
end
end
@ -99,9 +94,7 @@ module RuboCop
def offense(node, first_param, second_param)
first_source, = first_source(first_param)
unless first_param.str_type?
first_source = interpret_string_escapes(first_source)
end
first_source = interpret_string_escapes(first_source) unless first_param.str_type?
second_source, = *second_param
message = message(node, first_source, second_source)

View File

@ -61,7 +61,7 @@ module RuboCop
map_or_collect: map_or_collect.method_name)
end
def_node_matcher :times_map_call, <<-PATTERN
def_node_matcher :times_map_call, <<~PATTERN
{(block $(send (send $!nil? :times) {:map :collect}) ...)
$(send (send $!nil? :times) {:map :collect} (block_pass ...))}
PATTERN

View File

@ -24,17 +24,13 @@ module RuboCop
# +'something'
# +''
class UnfreezeString < Cop
extend TargetRubyVersion
minimum_target_ruby_version 2.3
MSG = 'Use unary plus to get an unfrozen string literal.'
def_node_matcher :dup_string?, <<-PATTERN
def_node_matcher :dup_string?, <<~PATTERN
(send {str dstr} :dup)
PATTERN
def_node_matcher :string_new?, <<-PATTERN
def_node_matcher :string_new?, <<~PATTERN
{
(send (const nil? :String) :new {str dstr})
(send (const nil? :String) :new)

View File

@ -17,7 +17,7 @@ module RuboCop
MSG = 'Use `%<double_colon>sURI::DEFAULT_PARSER` instead of ' \
'`%<double_colon>sURI::Parser.new`.'
def_node_matcher :uri_parser_new?, <<-PATTERN
def_node_matcher :uri_parser_new?, <<~PATTERN
(send
(const
(const ${nil? cbase} :URI) :Parser) :new)

View File

@ -1,10 +1,15 @@
# frozen_string_literal: true
require_relative 'mixin/regexp_metacharacter'
require_relative 'performance/bind_call'
require_relative 'performance/caller'
require_relative 'performance/case_when_splat'
require_relative 'performance/casecmp'
require_relative 'performance/compare_with_block'
require_relative 'performance/count'
require_relative 'performance/delete_prefix'
require_relative 'performance/delete_suffix'
require_relative 'performance/detect'
require_relative 'performance/double_start_end_with'
require_relative 'performance/end_with'

View File

@ -8,7 +8,7 @@ module RuboCop
def self.defaults!
path = CONFIG_DEFAULT.to_s
hash = ConfigLoader.send(:load_yaml_configuration, path)
config = Config.new(hash, path)
config = Config.new(hash, path).tap(&:make_excludes_absolute)
puts "configuration from #{path}" if ConfigLoader.debug?
config = ConfigLoader.merge_with_default(config, path)
ConfigLoader.instance_variable_set(:@default_configuration, config)