Merge pull request #7622 from Homebrew/dependabot/bundler/Library/Homebrew/rubocop-performance-1.6.0
build(deps): bump rubocop-performance from 1.5.2 to 1.6.0 in /Library/Homebrew
This commit is contained in:
commit
e4719cb6f3
@ -91,7 +91,7 @@ GEM
|
|||||||
unicode-display_width (>= 1.4.0, < 2.0)
|
unicode-display_width (>= 1.4.0, < 2.0)
|
||||||
rubocop-ast (0.0.3)
|
rubocop-ast (0.0.3)
|
||||||
parser (>= 2.7.0.1)
|
parser (>= 2.7.0.1)
|
||||||
rubocop-performance (1.5.2)
|
rubocop-performance (1.6.0)
|
||||||
rubocop (>= 0.71.0)
|
rubocop (>= 0.71.0)
|
||||||
rubocop-rspec (1.39.0)
|
rubocop-rspec (1.39.0)
|
||||||
rubocop (>= 0.68.1)
|
rubocop (>= 0.68.1)
|
||||||
|
|||||||
@ -89,7 +89,7 @@ module Cask
|
|||||||
args: ["list", service],
|
args: ["list", service],
|
||||||
sudo: with_sudo, print_stderr: false
|
sudo: with_sudo, print_stderr: false
|
||||||
).stdout
|
).stdout
|
||||||
if plist_status.match?(/^\{/)
|
if plist_status.start_with?("{")
|
||||||
command.run!("/bin/launchctl", args: ["remove", service], sudo: with_sudo)
|
command.run!("/bin/launchctl", args: ["remove", service], sudo: with_sudo)
|
||||||
sleep 1
|
sleep 1
|
||||||
end
|
end
|
||||||
|
|||||||
@ -984,7 +984,7 @@ module Homebrew
|
|||||||
except_audits = @except
|
except_audits = @except
|
||||||
|
|
||||||
methods.map(&:to_s).grep(/^audit_/).each do |audit_method_name|
|
methods.map(&:to_s).grep(/^audit_/).each do |audit_method_name|
|
||||||
name = audit_method_name.gsub(/^audit_/, "")
|
name = audit_method_name.delete_prefix("audit_")
|
||||||
if only_audits
|
if only_audits
|
||||||
next unless only_audits.include?(name)
|
next unless only_audits.include?(name)
|
||||||
elsif except_audits
|
elsif except_audits
|
||||||
|
|||||||
@ -103,7 +103,7 @@ module Homebrew
|
|||||||
help_lines = command_help_lines(path)
|
help_lines = command_help_lines(path)
|
||||||
return if help_lines.blank?
|
return if help_lines.blank?
|
||||||
|
|
||||||
Formatter.wrap(help_lines.join.gsub(/^ /, ""), COMMAND_DESC_WIDTH)
|
Formatter.wrap(help_lines.join.delete_prefix(" "), COMMAND_DESC_WIDTH)
|
||||||
.sub("@hide_from_man_page ", "")
|
.sub("@hide_from_man_page ", "")
|
||||||
.sub(/^\* /, "#{Tty.bold}Usage: brew#{Tty.reset} ")
|
.sub(/^\* /, "#{Tty.bold}Usage: brew#{Tty.reset} ")
|
||||||
.gsub(/`(.*?)`/m, "#{Tty.bold}\\1#{Tty.reset}")
|
.gsub(/`(.*?)`/m, "#{Tty.bold}\\1#{Tty.reset}")
|
||||||
|
|||||||
@ -230,7 +230,7 @@ module RuboCop
|
|||||||
# Use new-style archive downloads
|
# Use new-style archive downloads
|
||||||
archive_gh_pattern = %r{https://.*github.*/(?:tar|zip)ball/}
|
archive_gh_pattern = %r{https://.*github.*/(?:tar|zip)ball/}
|
||||||
audit_urls(urls, archive_gh_pattern) do |_, url|
|
audit_urls(urls, archive_gh_pattern) do |_, url|
|
||||||
next if url.match?(/\.git$/)
|
next if url.end_with?(".git")
|
||||||
|
|
||||||
problem "Use /archive/ URLs for GitHub tarballs (url is #{url})."
|
problem "Use /archive/ URLs for GitHub tarballs (url is #{url})."
|
||||||
end
|
end
|
||||||
|
|||||||
@ -3,11 +3,12 @@
|
|||||||
shared_examples "parseable arguments" do
|
shared_examples "parseable arguments" do
|
||||||
subject(:method_name) do |example|
|
subject(:method_name) do |example|
|
||||||
example.metadata[:example_group][:parent_example_group][:description]
|
example.metadata[:example_group][:parent_example_group][:description]
|
||||||
.gsub(/^Homebrew\./, "")
|
.delete_prefix("Homebrew.")
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:command_name) do
|
let(:command_name) do
|
||||||
method_name.gsub(/_args$/, "").tr("_", "-")
|
method_name.delete_suffix("_args")
|
||||||
|
.tr("_", "-")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "can parse arguments" do
|
it "can parse arguments" do
|
||||||
|
|||||||
@ -68,7 +68,7 @@ describe RuboCop::Cop::FormulaAudit::Patches do
|
|||||||
line: 5,
|
line: 5,
|
||||||
column: 33,
|
column: 33,
|
||||||
source: source }]
|
source: source }]
|
||||||
elsif patch_url.match?(%r{^http://trac\.macports\.org})
|
elsif patch_url.start_with?("http://trac.macports.org")
|
||||||
[{ message:
|
[{ message:
|
||||||
<<~EOS.chomp,
|
<<~EOS.chomp,
|
||||||
Patches from MacPorts Trac should be https://, not http:
|
Patches from MacPorts Trac should be https://, not http:
|
||||||
@ -78,7 +78,7 @@ describe RuboCop::Cop::FormulaAudit::Patches do
|
|||||||
line: 5,
|
line: 5,
|
||||||
column: 5,
|
column: 5,
|
||||||
source: source }]
|
source: source }]
|
||||||
elsif patch_url.match?(%r{^http://bugs\.debian\.org})
|
elsif patch_url.start_with?("http://bugs.debian.org")
|
||||||
[{ message:
|
[{ message:
|
||||||
<<~EOS.chomp,
|
<<~EOS.chomp,
|
||||||
Patches from Debian should be https://, not http:
|
Patches from Debian should be https://, not http:
|
||||||
@ -205,7 +205,7 @@ describe RuboCop::Cop::FormulaAudit::Patches do
|
|||||||
line: 5,
|
line: 5,
|
||||||
column: 37,
|
column: 37,
|
||||||
source: source }]
|
source: source }]
|
||||||
elsif patch_url.match?(%r{^http://trac\.macports\.org})
|
elsif patch_url.start_with?("http://trac.macports.org")
|
||||||
[{ message:
|
[{ message:
|
||||||
<<~EOS.chomp,
|
<<~EOS.chomp,
|
||||||
Patches from MacPorts Trac should be https://, not http:
|
Patches from MacPorts Trac should be https://, not http:
|
||||||
@ -215,7 +215,7 @@ describe RuboCop::Cop::FormulaAudit::Patches do
|
|||||||
line: 5,
|
line: 5,
|
||||||
column: 9,
|
column: 9,
|
||||||
source: source }]
|
source: source }]
|
||||||
elsif patch_url.match?(%r{^http://bugs\.debian\.org})
|
elsif patch_url.start_with?("http://bugs.debian.org")
|
||||||
[{ message:
|
[{ message:
|
||||||
<<~EOS.chomp,
|
<<~EOS.chomp,
|
||||||
Patches from Debian should be https://, not http:
|
Patches from Debian should be https://, not http:
|
||||||
|
|||||||
@ -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/activesupport-6.0.3.1/lib"
|
||||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/ast-2.4.0/lib"
|
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/ast-2.4.0/lib"
|
||||||
$:.unshift "#{path}/"
|
$:.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}/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}/extensions/universal-darwin-19/2.6.0/json-2.3.0"
|
||||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/json-2.3.0/lib"
|
$:.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-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-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/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/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/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-0.84.0/lib"
|
||||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-performance-1.5.2/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/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/ruby-macho-2.2.0/lib"
|
||||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/byebug-11.1.3/lib"
|
|
||||||
|
|||||||
@ -1,5 +1,10 @@
|
|||||||
# This is the default configuration file.
|
# 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:
|
Performance/Caller:
|
||||||
Description: >-
|
Description: >-
|
||||||
Use `caller(n..n)` instead of `caller`.
|
Use `caller(n..n)` instead of `caller`.
|
||||||
@ -21,6 +26,7 @@ Performance/Casecmp:
|
|||||||
Use `casecmp` rather than `downcase ==`, `upcase ==`, `== downcase`, or `== upcase`..
|
Use `casecmp` rather than `downcase ==`, `upcase ==`, `== downcase`, or `== upcase`..
|
||||||
Reference: 'https://github.com/JuanitoFatas/fast-ruby#stringcasecmp-vs-stringdowncase---code'
|
Reference: 'https://github.com/JuanitoFatas/fast-ruby#stringcasecmp-vs-stringdowncase---code'
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
Safe: false
|
||||||
VersionAdded: '0.36'
|
VersionAdded: '0.36'
|
||||||
|
|
||||||
Performance/ChainArrayAllocation:
|
Performance/ChainArrayAllocation:
|
||||||
@ -49,6 +55,16 @@ Performance/Count:
|
|||||||
VersionAdded: '0.31'
|
VersionAdded: '0.31'
|
||||||
VersionChanged: '1.5'
|
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:
|
Performance/Detect:
|
||||||
Description: >-
|
Description: >-
|
||||||
Use `detect` instead of `select.first`, `find_all.first`,
|
Use `detect` instead of `select.first`, `find_all.first`,
|
||||||
@ -87,7 +103,7 @@ Performance/EndWith:
|
|||||||
VersionChanged: '0.44'
|
VersionChanged: '0.44'
|
||||||
|
|
||||||
Performance/FixedSize:
|
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
|
Enabled: true
|
||||||
VersionAdded: '0.35'
|
VersionAdded: '0.35'
|
||||||
|
|
||||||
@ -95,7 +111,7 @@ Performance/FlatMap:
|
|||||||
Description: >-
|
Description: >-
|
||||||
Use `Enumerable#flat_map`
|
Use `Enumerable#flat_map`
|
||||||
instead of `Enumerable#map...Array#flatten(1)`
|
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'
|
Reference: 'https://github.com/JuanitoFatas/fast-ruby#enumerablemaparrayflatten-vs-enumerableflat_map-code'
|
||||||
Enabled: true
|
Enabled: true
|
||||||
VersionAdded: '0.30'
|
VersionAdded: '0.30'
|
||||||
@ -106,7 +122,7 @@ Performance/FlatMap:
|
|||||||
# `flatten` without any parameters can flatten multiple levels.
|
# `flatten` without any parameters can flatten multiple levels.
|
||||||
|
|
||||||
Performance/InefficientHashSearch:
|
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'
|
Reference: 'https://github.com/JuanitoFatas/fast-ruby#hashkey-instead-of-hashkeysinclude-code'
|
||||||
Enabled: true
|
Enabled: true
|
||||||
VersionAdded: '0.56'
|
VersionAdded: '0.56'
|
||||||
@ -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
|
||||||
@ -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
|
||||||
@ -24,14 +24,14 @@ module RuboCop
|
|||||||
MSG_FIRST = 'Use `%<method>s(%<n>d..%<n>d).first`' \
|
MSG_FIRST = 'Use `%<method>s(%<n>d..%<n>d).first`' \
|
||||||
' instead of `%<method>s.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})
|
||||||
(send nil? {:caller :caller_locations} int)
|
(send nil? {:caller :caller_locations} int)
|
||||||
}
|
}
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
def_node_matcher :caller_with_scope_method?, <<-PATTERN
|
def_node_matcher :caller_with_scope_method?, <<~PATTERN
|
||||||
{
|
{
|
||||||
(send #slow_caller? :first)
|
(send #slow_caller? :first)
|
||||||
(send #slow_caller? :[] int)
|
(send #slow_caller? :[] int)
|
||||||
@ -5,6 +5,8 @@ module RuboCop
|
|||||||
module Performance
|
module Performance
|
||||||
# This cop identifies places where a case-insensitive string comparison
|
# This cop identifies places where a case-insensitive string comparison
|
||||||
# can better be implemented using `casecmp`.
|
# can better be implemented using `casecmp`.
|
||||||
|
# This cop is unsafe because `String#casecmp` and `String#casecmp?` behave
|
||||||
|
# differently when using Non-ASCII characters.
|
||||||
#
|
#
|
||||||
# @example
|
# @example
|
||||||
# # bad
|
# # bad
|
||||||
@ -21,21 +23,21 @@ module RuboCop
|
|||||||
MSG = 'Use `%<good>s` instead of `%<bad>s`.'
|
MSG = 'Use `%<good>s` instead of `%<bad>s`.'
|
||||||
CASE_METHODS = %i[downcase upcase].freeze
|
CASE_METHODS = %i[downcase upcase].freeze
|
||||||
|
|
||||||
def_node_matcher :downcase_eq, <<-PATTERN
|
def_node_matcher :downcase_eq, <<~PATTERN
|
||||||
(send
|
(send
|
||||||
$(send _ ${:downcase :upcase})
|
$(send _ ${:downcase :upcase})
|
||||||
${:== :eql? :!=}
|
${:== :eql? :!=}
|
||||||
${str (send _ {:downcase :upcase} ...) (begin str)})
|
${str (send _ {:downcase :upcase} ...) (begin str)})
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
def_node_matcher :eq_downcase, <<-PATTERN
|
def_node_matcher :eq_downcase, <<~PATTERN
|
||||||
(send
|
(send
|
||||||
{str (send _ {:downcase :upcase} ...) (begin str)}
|
{str (send _ {:downcase :upcase} ...) (begin str)}
|
||||||
${:== :eql? :!=}
|
${:== :eql? :!=}
|
||||||
$(send _ ${:downcase :upcase}))
|
$(send _ ${:downcase :upcase}))
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
def_node_matcher :downcase_downcase, <<-PATTERN
|
def_node_matcher :downcase_downcase, <<~PATTERN
|
||||||
(send
|
(send
|
||||||
$(send _ ${:downcase :upcase})
|
$(send _ ${:downcase :upcase})
|
||||||
${:== :eql? :!=}
|
${:== :eql? :!=}
|
||||||
@ -51,7 +51,7 @@ module RuboCop
|
|||||||
'(followed by `return array` if required) instead of chaining '\
|
'(followed by `return array` if required) instead of chaining '\
|
||||||
'`%<method>s...%<second_method>s`.'
|
'`%<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 (send _ ${#{RETURN_NEW_ARRAY_WHEN_ARGS}} {int lvar ivar cvar gvar}) ${#{HAS_MUTATION_ALTERNATIVE}} $...)
|
||||||
(send (block (send _ ${#{ALWAYS_RETURNS_NEW_ARRAY} }) ...) ${#{HAS_MUTATION_ALTERNATIVE}} $...)
|
(send (block (send _ ${#{ALWAYS_RETURNS_NEW_ARRAY} }) ...) ${#{HAS_MUTATION_ALTERNATIVE}} $...)
|
||||||
@ -30,14 +30,14 @@ module RuboCop
|
|||||||
'`%<compare_method>s { |%<var_a>s, %<var_b>s| %<str_a>s ' \
|
'`%<compare_method>s { |%<var_a>s, %<var_b>s| %<str_a>s ' \
|
||||||
'<=> %<str_b>s }`.'
|
'<=> %<str_b>s }`.'
|
||||||
|
|
||||||
def_node_matcher :compare?, <<-PATTERN
|
def_node_matcher :compare?, <<~PATTERN
|
||||||
(block
|
(block
|
||||||
$(send _ {:sort :min :max})
|
$(send _ {:sort :min :max})
|
||||||
(args (arg $_a) (arg $_b))
|
(args (arg $_a) (arg $_b))
|
||||||
$send)
|
$send)
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
def_node_matcher :replaceable_body?, <<-PATTERN
|
def_node_matcher :replaceable_body?, <<~PATTERN
|
||||||
(send
|
(send
|
||||||
(send (lvar %1) $_method $...)
|
(send (lvar %1) $_method $...)
|
||||||
:<=>
|
:<=>
|
||||||
@ -42,7 +42,7 @@ module RuboCop
|
|||||||
|
|
||||||
MSG = 'Use `count` instead of `%<selector>s...%<counter>s`.'
|
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 (block $(send _ ${:select :reject}) ...) ${:count :length :size})
|
||||||
(send $(send _ ${:select :reject} (:block_pass _)) ${:count :length :size})
|
(send $(send _ ${:select :reject} (:block_pass _)) ${:count :length :size})
|
||||||
@ -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
|
||||||
@ -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
|
||||||
@ -28,7 +28,7 @@ module RuboCop
|
|||||||
REVERSE_MSG = 'Use `reverse.%<prefer>s` instead of ' \
|
REVERSE_MSG = 'Use `reverse.%<prefer>s` instead of ' \
|
||||||
'`%<first_method>s.%<second_method>s`.'
|
'`%<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 $(block (send _ {:select :find_all}) ...) ${:first :last} $...)
|
||||||
(send $(send _ {:select :find_all} ...) ${:first :last} $...)
|
(send $(send _ {:select :find_all} ...) ${:first :last} $...)
|
||||||
@ -75,13 +75,13 @@ module RuboCop
|
|||||||
cop_config['IncludeActiveSupportAliases']
|
cop_config['IncludeActiveSupportAliases']
|
||||||
end
|
end
|
||||||
|
|
||||||
def_node_matcher :two_start_end_with_calls, <<-PATTERN
|
def_node_matcher :two_start_end_with_calls, <<~PATTERN
|
||||||
(or
|
(or
|
||||||
(send $_recv [{:start_with? :end_with?} $_method] $...)
|
(send $_recv [{:start_with? :end_with?} $_method] $...)
|
||||||
(send _recv _method $...))
|
(send _recv _method $...))
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
def_node_matcher :check_with_active_support_aliases, <<-PATTERN
|
def_node_matcher :check_with_active_support_aliases, <<~PATTERN
|
||||||
(or
|
(or
|
||||||
(send $_recv
|
(send $_recv
|
||||||
[{:start_with? :starts_with? :end_with? :ends_with?} $_method]
|
[{:start_with? :starts_with? :end_with? :ends_with?} $_method]
|
||||||
@ -15,26 +15,27 @@ module RuboCop
|
|||||||
# 'abc'.match(/bc\Z/)
|
# 'abc'.match(/bc\Z/)
|
||||||
# /bc\Z/.match('abc')
|
# /bc\Z/.match('abc')
|
||||||
#
|
#
|
||||||
|
# 'abc'.match?(/bc$/)
|
||||||
|
# /bc$/.match?('abc')
|
||||||
|
# 'abc' =~ /bc$/
|
||||||
|
# /bc$/ =~ 'abc'
|
||||||
|
# 'abc'.match(/bc$/)
|
||||||
|
# /bc$/.match('abc')
|
||||||
|
#
|
||||||
# # good
|
# # good
|
||||||
# 'abc'.end_with?('bc')
|
# 'abc'.end_with?('bc')
|
||||||
class EndWith < Cop
|
class EndWith < Cop
|
||||||
|
include RegexpMetacharacter
|
||||||
|
|
||||||
MSG = 'Use `String#end_with?` instead of a regex match anchored to ' \
|
MSG = 'Use `String#end_with?` instead of a regex match anchored to ' \
|
||||||
'the end of the string.'
|
'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 $!nil? {:match :=~ :match?} (regexp (str $#literal_at_end?) (regopt)))
|
||||||
(send (regexp (str $#literal_at_end?) (regopt)) {:match :match?} $_)
|
(send (regexp (str $#literal_at_end?) (regopt)) {:match :match?} $_)
|
||||||
(match-with-lvasgn (regexp (str $#literal_at_end?) (regopt)) $_)}
|
(match-with-lvasgn (regexp (str $#literal_at_end?) (regopt)) $_)}
|
||||||
PATTERN
|
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)
|
def on_send(node)
|
||||||
return unless redundant_regex?(node)
|
return unless redundant_regex?(node)
|
||||||
|
|
||||||
@ -45,7 +46,7 @@ module RuboCop
|
|||||||
def autocorrect(node)
|
def autocorrect(node)
|
||||||
redundant_regex?(node) do |receiver, regex_str|
|
redundant_regex?(node) do |receiver, regex_str|
|
||||||
receiver, regex_str = regex_str, receiver if receiver.is_a?(String)
|
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)
|
regex_str = interpret_string_escapes(regex_str)
|
||||||
|
|
||||||
lambda do |corrector|
|
lambda do |corrector|
|
||||||
@ -48,7 +48,7 @@ module RuboCop
|
|||||||
class FixedSize < Cop
|
class FixedSize < Cop
|
||||||
MSG = 'Do not compute the size of statically sized objects.'
|
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} $...)
|
(send ${array hash str sym} {:count :length :size} $...)
|
||||||
MATCHER
|
MATCHER
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ module RuboCop
|
|||||||
'and `flatten` can be used to flatten ' \
|
'and `flatten` can be used to flatten ' \
|
||||||
'multiple levels.'
|
'multiple levels.'
|
||||||
|
|
||||||
def_node_matcher :flat_map_candidate?, <<-PATTERN
|
def_node_matcher :flat_map_candidate?, <<~PATTERN
|
||||||
(send
|
(send
|
||||||
{
|
{
|
||||||
(block $(send _ ${:collect :map}) ...)
|
(block $(send _ ${:collect :map}) ...)
|
||||||
@ -37,7 +37,7 @@ module RuboCop
|
|||||||
# h = { a: 1, b: 2 }; h.value?(nil)
|
# h = { a: 1, b: 2 }; h.value?(nil)
|
||||||
#
|
#
|
||||||
class InefficientHashSearch < Cop
|
class InefficientHashSearch < Cop
|
||||||
def_node_matcher :inefficient_include?, <<-PATTERN
|
def_node_matcher :inefficient_include?, <<~PATTERN
|
||||||
(send (send $_ {:keys :values}) :include? _)
|
(send (send $_ {:keys :values}) :include? _)
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ module RuboCop
|
|||||||
MSG = 'Consider using `Struct` over `OpenStruct` ' \
|
MSG = 'Consider using `Struct` over `OpenStruct` ' \
|
||||||
'to optimize the performance.'
|
'to optimize the performance.'
|
||||||
|
|
||||||
def_node_matcher :open_struct, <<-PATTERN
|
def_node_matcher :open_struct, <<~PATTERN
|
||||||
(send (const {nil? cbase} :OpenStruct) :new ...)
|
(send (const {nil? cbase} :OpenStruct) :new ...)
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ module RuboCop
|
|||||||
# Right now, we only detect direct calls on a Range literal
|
# Right now, we only detect direct calls on a Range literal
|
||||||
# (We don't even catch it if the Range is in double parens)
|
# (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? ...)
|
(send {irange erange (begin {irange erange})} :include? ...)
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
@ -29,16 +29,16 @@ module RuboCop
|
|||||||
CLOSE_PAREN = ')'
|
CLOSE_PAREN = ')'
|
||||||
SPACE = ' '
|
SPACE = ' '
|
||||||
|
|
||||||
def_node_matcher :blockarg_def, <<-PATTERN
|
def_node_matcher :blockarg_def, <<~PATTERN
|
||||||
{(def _ (args ... (blockarg $_)) $_)
|
{(def _ (args ... (blockarg $_)) $_)
|
||||||
(defs _ _ (args ... (blockarg $_)) $_)}
|
(defs _ _ (args ... (blockarg $_)) $_)}
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
def_node_search :blockarg_calls, <<-PATTERN
|
def_node_search :blockarg_calls, <<~PATTERN
|
||||||
(send (lvar %1) :call ...)
|
(send (lvar %1) :call ...)
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
def_node_search :blockarg_assigned?, <<-PATTERN
|
def_node_search :blockarg_assigned?, <<~PATTERN
|
||||||
(lvasgn %1 ...)
|
(lvasgn %1 ...)
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
@ -23,12 +23,12 @@ module RuboCop
|
|||||||
|
|
||||||
# 'match' is a fairly generic name, so we don't flag it unless we see
|
# '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
|
# 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 {str regexp} :match _)
|
||||||
(send !nil? :match {str regexp})}
|
(send !nil? :match {str regexp})}
|
||||||
PATTERN
|
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) ...)
|
^({if while until case while_post until_post} equal?(%0) ...)
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
@ -5,11 +5,25 @@ module RuboCop
|
|||||||
module Performance
|
module Performance
|
||||||
# This cop identifies places where `Hash#merge!` can be replaced by
|
# This cop identifies places where `Hash#merge!` can be replaced by
|
||||||
# `Hash#[]=`.
|
# `Hash#[]=`.
|
||||||
|
# You can set the maximum number of key-value pairs to consider
|
||||||
|
# an offense with `MaxKeyValuePairs`.
|
||||||
#
|
#
|
||||||
# @example
|
# @example
|
||||||
|
# # bad
|
||||||
# hash.merge!(a: 1)
|
# hash.merge!(a: 1)
|
||||||
# hash.merge!({'key' => 'value'})
|
# hash.merge!({'key' => 'value'})
|
||||||
|
#
|
||||||
|
# # good
|
||||||
|
# hash[:a] = 1
|
||||||
|
# hash['key'] = 'value'
|
||||||
|
#
|
||||||
|
# @example MaxKeyValuePairs: 2 (default)
|
||||||
|
# # bad
|
||||||
# hash.merge!(a: 1, b: 2)
|
# hash.merge!(a: 1, b: 2)
|
||||||
|
#
|
||||||
|
# # good
|
||||||
|
# hash[:a] = 1
|
||||||
|
# hash[:b] = 2
|
||||||
class RedundantMerge < Cop
|
class RedundantMerge < Cop
|
||||||
AREF_ASGN = '%<receiver>s[%<key>s] = %<value>s'
|
AREF_ASGN = '%<receiver>s[%<key>s] = %<value>s'
|
||||||
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
|
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
|
||||||
@ -20,11 +34,11 @@ module RuboCop
|
|||||||
%<leading_space>send
|
%<leading_space>send
|
||||||
RUBY
|
RUBY
|
||||||
|
|
||||||
def_node_matcher :redundant_merge_candidate, <<-PATTERN
|
def_node_matcher :redundant_merge_candidate, <<~PATTERN
|
||||||
(send $!nil? :merge! [(hash $...) !kwsplat_type?])
|
(send $!nil? :merge! [(hash $...) !kwsplat_type?])
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
def_node_matcher :modifier_flow_control?, <<-PATTERN
|
def_node_matcher :modifier_flow_control?, <<~PATTERN
|
||||||
[{if while until} modifier_form?]
|
[{if while until} modifier_form?]
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
@ -168,13 +182,11 @@ module RuboCop
|
|||||||
end
|
end
|
||||||
|
|
||||||
def unwind(receiver)
|
def unwind(receiver)
|
||||||
while receiver.respond_to?(:send_type?) && receiver.send_type?
|
receiver, = *receiver while receiver.respond_to?(:send_type?) && receiver.send_type?
|
||||||
receiver, = *receiver
|
|
||||||
end
|
|
||||||
receiver
|
receiver
|
||||||
end
|
end
|
||||||
|
|
||||||
def_node_matcher :each_with_object_node, <<-PATTERN
|
def_node_matcher :each_with_object_node, <<~PATTERN
|
||||||
(block (send _ :each_with_object _) (args _ $_) ...)
|
(block (send _ :each_with_object _) (args _ $_) ...)
|
||||||
PATTERN
|
PATTERN
|
||||||
end
|
end
|
||||||
@ -73,32 +73,28 @@ module RuboCop
|
|||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
class RegexpMatch < Cop
|
class RegexpMatch < Cop
|
||||||
extend TargetRubyVersion
|
|
||||||
|
|
||||||
minimum_target_ruby_version 2.4
|
|
||||||
|
|
||||||
# Constants are included in this list because it is unlikely that
|
# Constants are included in this list because it is unlikely that
|
||||||
# someone will store `nil` as a constant and then use it for comparison
|
# someone will store `nil` as a constant and then use it for comparison
|
||||||
TYPES_IMPLEMENTING_MATCH = %i[const regexp str sym].freeze
|
TYPES_IMPLEMENTING_MATCH = %i[const regexp str sym].freeze
|
||||||
MSG = 'Use `match?` instead of `%<current>s` when `MatchData` ' \
|
MSG = 'Use `match?` instead of `%<current>s` when `MatchData` ' \
|
||||||
'is not used.'
|
'is not used.'
|
||||||
|
|
||||||
def_node_matcher :match_method?, <<-PATTERN
|
def_node_matcher :match_method?, <<~PATTERN
|
||||||
{
|
{
|
||||||
(send _recv :match {regexp str sym})
|
(send _recv :match {regexp str sym})
|
||||||
(send {regexp str sym} :match _)
|
(send {regexp str sym} :match _)
|
||||||
}
|
}
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
def_node_matcher :match_with_int_arg_method?, <<-PATTERN
|
def_node_matcher :match_with_int_arg_method?, <<~PATTERN
|
||||||
(send _recv :match _ (int ...))
|
(send _recv :match _ (int ...))
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
def_node_matcher :match_operator?, <<-PATTERN
|
def_node_matcher :match_operator?, <<~PATTERN
|
||||||
(send !nil? {:=~ :!~} !nil?)
|
(send !nil? {:=~ :!~} !nil?)
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
def_node_matcher :match_threequals?, <<-PATTERN
|
def_node_matcher :match_threequals?, <<~PATTERN
|
||||||
(send (regexp (str _) {(regopt) (regopt _)}) :=== !nil?)
|
(send (regexp (str _) {(regopt) (regopt _)}) :=== !nil?)
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
@ -109,7 +105,7 @@ module RuboCop
|
|||||||
regexp.to_regexp.named_captures.empty?
|
regexp.to_regexp.named_captures.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
MATCH_NODE_PATTERN = <<-PATTERN
|
MATCH_NODE_PATTERN = <<~PATTERN
|
||||||
{
|
{
|
||||||
#match_method?
|
#match_method?
|
||||||
#match_with_int_arg_method?
|
#match_with_int_arg_method?
|
||||||
@ -122,7 +118,7 @@ module RuboCop
|
|||||||
def_node_matcher :match_node?, MATCH_NODE_PATTERN
|
def_node_matcher :match_node?, MATCH_NODE_PATTERN
|
||||||
def_node_search :search_match_nodes, 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)
|
||||||
(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)
|
def correct_operator(corrector, recv, arg, oper = nil)
|
||||||
op_range = correction_range(recv, arg)
|
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)
|
if TYPES_IMPLEMENTING_MATCH.include?(recv.type)
|
||||||
corrector.replace(op_range, '.match?(')
|
corrector.replace(op_range, '.match?(')
|
||||||
elsif TYPES_IMPLEMENTING_MATCH.include?(arg.type)
|
elsif TYPES_IMPLEMENTING_MATCH.include?(arg.type)
|
||||||
@ -264,9 +267,6 @@ module RuboCop
|
|||||||
else
|
else
|
||||||
corrector.replace(op_range, '&.match?(')
|
corrector.replace(op_range, '&.match?(')
|
||||||
end
|
end
|
||||||
|
|
||||||
corrector.insert_after(arg.loc.expression, ')')
|
|
||||||
corrector.insert_before(recv.loc.expression, '!') if oper == :!~
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def swap_receiver_and_arg(corrector, recv, arg)
|
def swap_receiver_and_arg(corrector, recv, arg)
|
||||||
@ -18,7 +18,7 @@ module RuboCop
|
|||||||
MSG = 'Use `reverse_each` instead of `reverse.each`.'
|
MSG = 'Use `reverse_each` instead of `reverse.each`.'
|
||||||
UNDERSCORE = '_'
|
UNDERSCORE = '_'
|
||||||
|
|
||||||
def_node_matcher :reverse_each?, <<-MATCHER
|
def_node_matcher :reverse_each?, <<~MATCHER
|
||||||
(send $(send _ :reverse) :each)
|
(send $(send _ :reverse) :each)
|
||||||
MATCHER
|
MATCHER
|
||||||
|
|
||||||
@ -34,7 +34,8 @@ module RuboCop
|
|||||||
end
|
end
|
||||||
|
|
||||||
def autocorrect(node)
|
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
|
end
|
||||||
end
|
end
|
||||||
@ -15,29 +15,27 @@ module RuboCop
|
|||||||
# 'abc'.match(/\Aab/)
|
# 'abc'.match(/\Aab/)
|
||||||
# /\Aab/.match('abc')
|
# /\Aab/.match('abc')
|
||||||
#
|
#
|
||||||
|
# 'abc'.match?(/^ab/)
|
||||||
|
# /^ab/.match?('abc')
|
||||||
|
# 'abc' =~ /^ab/
|
||||||
|
# /^ab/ =~ 'abc'
|
||||||
|
# 'abc'.match(/^ab/)
|
||||||
|
# /^ab/.match('abc')
|
||||||
|
#
|
||||||
# # good
|
# # good
|
||||||
# 'abc'.start_with?('ab')
|
# 'abc'.start_with?('ab')
|
||||||
class StartWith < Cop
|
class StartWith < Cop
|
||||||
|
include RegexpMetacharacter
|
||||||
|
|
||||||
MSG = 'Use `String#start_with?` instead of a regex match anchored to ' \
|
MSG = 'Use `String#start_with?` instead of a regex match anchored to ' \
|
||||||
'the beginning of the string.'
|
'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 $!nil? {:match :=~ :match?} (regexp (str $#literal_at_start?) (regopt)))
|
||||||
(send (regexp (str $#literal_at_start?) (regopt)) {:match :match?} $_)
|
(send (regexp (str $#literal_at_start?) (regopt)) {:match :match?} $_)
|
||||||
(match-with-lvasgn (regexp (str $#literal_at_start?) (regopt)) $_)}
|
(match-with-lvasgn (regexp (str $#literal_at_start?) (regopt)) $_)}
|
||||||
PATTERN
|
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)
|
def on_send(node)
|
||||||
return unless redundant_regex?(node)
|
return unless redundant_regex?(node)
|
||||||
|
|
||||||
@ -48,7 +46,7 @@ module RuboCop
|
|||||||
def autocorrect(node)
|
def autocorrect(node)
|
||||||
redundant_regex?(node) do |receiver, regex_str|
|
redundant_regex?(node) do |receiver, regex_str|
|
||||||
receiver, regex_str = regex_str, receiver if receiver.is_a?(String)
|
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)
|
regex_str = interpret_string_escapes(regex_str)
|
||||||
|
|
||||||
lambda do |corrector|
|
lambda do |corrector|
|
||||||
@ -26,9 +26,8 @@ module RuboCop
|
|||||||
DELETE = 'delete'
|
DELETE = 'delete'
|
||||||
TR = 'tr'
|
TR = 'tr'
|
||||||
BANG = '!'
|
BANG = '!'
|
||||||
SINGLE_QUOTE = "'"
|
|
||||||
|
|
||||||
def_node_matcher :string_replacement?, <<-PATTERN
|
def_node_matcher :string_replacement?, <<~PATTERN
|
||||||
(send _ {:gsub :gsub!}
|
(send _ {:gsub :gsub!}
|
||||||
${regexp str (send (const nil? :Regexp) {:new :compile} _)}
|
${regexp str (send (const nil? :Regexp) {:new :compile} _)}
|
||||||
$str)
|
$str)
|
||||||
@ -48,9 +47,7 @@ module RuboCop
|
|||||||
first_source, = first_source(first_param)
|
first_source, = first_source(first_param)
|
||||||
second_source, = *second_param
|
second_source, = *second_param
|
||||||
|
|
||||||
unless first_param.str_type?
|
first_source = interpret_string_escapes(first_source) unless first_param.str_type?
|
||||||
first_source = interpret_string_escapes(first_source)
|
|
||||||
end
|
|
||||||
|
|
||||||
replacement_method =
|
replacement_method =
|
||||||
replacement_method(node, first_source, second_source)
|
replacement_method(node, first_source, second_source)
|
||||||
@ -67,9 +64,7 @@ module RuboCop
|
|||||||
to_string_literal(first))
|
to_string_literal(first))
|
||||||
end
|
end
|
||||||
|
|
||||||
if second.empty? && first.length == 1
|
remove_second_param(corrector, node, first_param) if second.empty? && first.length == 1
|
||||||
remove_second_param(corrector, node, first_param)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -99,9 +94,7 @@ module RuboCop
|
|||||||
|
|
||||||
def offense(node, first_param, second_param)
|
def offense(node, first_param, second_param)
|
||||||
first_source, = first_source(first_param)
|
first_source, = first_source(first_param)
|
||||||
unless first_param.str_type?
|
first_source = interpret_string_escapes(first_source) unless first_param.str_type?
|
||||||
first_source = interpret_string_escapes(first_source)
|
|
||||||
end
|
|
||||||
second_source, = *second_param
|
second_source, = *second_param
|
||||||
message = message(node, first_source, second_source)
|
message = message(node, first_source, second_source)
|
||||||
|
|
||||||
@ -61,7 +61,7 @@ module RuboCop
|
|||||||
map_or_collect: map_or_collect.method_name)
|
map_or_collect: map_or_collect.method_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def_node_matcher :times_map_call, <<-PATTERN
|
def_node_matcher :times_map_call, <<~PATTERN
|
||||||
{(block $(send (send $!nil? :times) {:map :collect}) ...)
|
{(block $(send (send $!nil? :times) {:map :collect}) ...)
|
||||||
$(send (send $!nil? :times) {:map :collect} (block_pass ...))}
|
$(send (send $!nil? :times) {:map :collect} (block_pass ...))}
|
||||||
PATTERN
|
PATTERN
|
||||||
@ -24,17 +24,13 @@ module RuboCop
|
|||||||
# +'something'
|
# +'something'
|
||||||
# +''
|
# +''
|
||||||
class UnfreezeString < Cop
|
class UnfreezeString < Cop
|
||||||
extend TargetRubyVersion
|
|
||||||
|
|
||||||
minimum_target_ruby_version 2.3
|
|
||||||
|
|
||||||
MSG = 'Use unary plus to get an unfrozen string literal.'
|
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)
|
(send {str dstr} :dup)
|
||||||
PATTERN
|
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 {str dstr})
|
||||||
(send (const nil? :String) :new)
|
(send (const nil? :String) :new)
|
||||||
@ -17,7 +17,7 @@ module RuboCop
|
|||||||
MSG = 'Use `%<double_colon>sURI::DEFAULT_PARSER` instead of ' \
|
MSG = 'Use `%<double_colon>sURI::DEFAULT_PARSER` instead of ' \
|
||||||
'`%<double_colon>sURI::Parser.new`.'
|
'`%<double_colon>sURI::Parser.new`.'
|
||||||
|
|
||||||
def_node_matcher :uri_parser_new?, <<-PATTERN
|
def_node_matcher :uri_parser_new?, <<~PATTERN
|
||||||
(send
|
(send
|
||||||
(const
|
(const
|
||||||
(const ${nil? cbase} :URI) :Parser) :new)
|
(const ${nil? cbase} :URI) :Parser) :new)
|
||||||
@ -1,10 +1,15 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative 'mixin/regexp_metacharacter'
|
||||||
|
|
||||||
|
require_relative 'performance/bind_call'
|
||||||
require_relative 'performance/caller'
|
require_relative 'performance/caller'
|
||||||
require_relative 'performance/case_when_splat'
|
require_relative 'performance/case_when_splat'
|
||||||
require_relative 'performance/casecmp'
|
require_relative 'performance/casecmp'
|
||||||
require_relative 'performance/compare_with_block'
|
require_relative 'performance/compare_with_block'
|
||||||
require_relative 'performance/count'
|
require_relative 'performance/count'
|
||||||
|
require_relative 'performance/delete_prefix'
|
||||||
|
require_relative 'performance/delete_suffix'
|
||||||
require_relative 'performance/detect'
|
require_relative 'performance/detect'
|
||||||
require_relative 'performance/double_start_end_with'
|
require_relative 'performance/double_start_end_with'
|
||||||
require_relative 'performance/end_with'
|
require_relative 'performance/end_with'
|
||||||
@ -8,7 +8,7 @@ module RuboCop
|
|||||||
def self.defaults!
|
def self.defaults!
|
||||||
path = CONFIG_DEFAULT.to_s
|
path = CONFIG_DEFAULT.to_s
|
||||||
hash = ConfigLoader.send(:load_yaml_configuration, path)
|
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?
|
puts "configuration from #{path}" if ConfigLoader.debug?
|
||||||
config = ConfigLoader.merge_with_default(config, path)
|
config = ConfigLoader.merge_with_default(config, path)
|
||||||
ConfigLoader.instance_variable_set(:@default_configuration, config)
|
ConfigLoader.instance_variable_set(:@default_configuration, config)
|
||||||
@ -3,7 +3,7 @@
|
|||||||
module RuboCop
|
module RuboCop
|
||||||
module Performance
|
module Performance
|
||||||
module Version
|
module Version
|
||||||
STRING = '1.5.2'
|
STRING = '1.6.0'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Loading…
x
Reference in New Issue
Block a user