Skip to content

Commit

Permalink
Merge pull request #7697 from Homebrew/dependabot/bundler/Library/Hom…
Browse files Browse the repository at this point in the history
…ebrew/rubocop-performance-1.6.1

build(deps): bump rubocop-performance from 1.6.0 to 1.6.1 in /Library/Homebrew
  • Loading branch information
MikeMcQuaid authored Jun 5, 2020
2 parents 9c9ef77 + e090631 commit 6af8b76
Show file tree
Hide file tree
Showing 38 changed files with 202 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Library/Homebrew/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ GEM
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (0.0.3)
parser (>= 2.7.0.1)
rubocop-performance (1.6.0)
rubocop-performance (1.6.1)
rubocop (>= 0.71.0)
rubocop-rspec (1.39.0)
rubocop (>= 0.68.1)
Expand Down
9 changes: 5 additions & 4 deletions Library/Homebrew/vendor/bundle/bundler/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$:.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.3/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"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/docile-1.3.2/lib"
Expand Down Expand Up @@ -46,11 +46,12 @@
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/mustache-1.1.1/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/parallel-1.19.1/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/parallel_tests-2.32.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/parser-2.7.1.2/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/parser-2.7.1.3/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/plist-3.5.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rainbow-3.0.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/extensions/universal-darwin-19/2.6.0/rdiscount-2.2.0.1"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rdiscount-2.2.0.1/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/regexp_parser-1.7.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rexml-3.2.4/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/ronn-0.7.3/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rspec-support-3.9.3/lib"
Expand All @@ -64,7 +65,7 @@
$:.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.84.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-performance-1.6.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-0.85.0/lib"
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/rubocop-performance-1.6.1/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"

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ Performance/Count:
Performance/DeletePrefix:
Description: 'Use `delete_prefix` instead of `gsub`.'
Enabled: true
SafeMultiline: true
VersionAdded: '1.6'

Performance/DeleteSuffix:
Description: 'Use `delete_suffix` instead of `gsub`.'
Enabled: true
SafeMultiline: true
VersionAdded: '1.6'

Performance/Detect:
Expand Down Expand Up @@ -99,8 +101,9 @@ Performance/EndWith:
SafeAutoCorrect: false
AutoCorrect: false
Enabled: true
SafeMultiline: true
VersionAdded: '0.36'
VersionChanged: '0.44'
VersionChanged: '1.6'

Performance/FixedSize:
Description: 'Do not compute the size of statically sized objects except in constants.'
Expand Down Expand Up @@ -193,8 +196,9 @@ Performance/StartWith:
SafeAutoCorrect: false
AutoCorrect: false
Enabled: true
SafeMultiline: true
VersionAdded: '0.36'
VersionChanged: '0.44'
VersionChanged: '1.6'

Performance/StringReplacement:
Description: >-
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# frozen_string_literal: true

module RuboCop
module Cop
# Common functionality for handling regexp metacharacters.
module RegexpMetacharacter
private

def literal_at_start?(regexp)
return true if literal_at_start_with_backslash_a?(regexp)

!safe_multiline? && literal_at_start_with_caret?(regexp)
end

def literal_at_end?(regexp)
return true if literal_at_end_with_backslash_z?(regexp)

!safe_multiline? && literal_at_end_with_dollar?(regexp)
end

def literal_at_start_with_backslash_a?(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.)
/\A\\A(?:#{Util::LITERAL_REGEX})+\z/.match?(regex_str)
end

def literal_at_start_with_caret?(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.)
/\A\^(?:#{Util::LITERAL_REGEX})+\z/.match?(regex_str)
end

def literal_at_end_with_backslash_z?(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?
/\A(?:#{Util::LITERAL_REGEX})+\\z\z/.match?(regex_str)
end

def literal_at_end_with_dollar?(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?
/\A(?:#{Util::LITERAL_REGEX})+\$\z/.match?(regex_str)
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

def safe_multiline?
cop_config.fetch('SafeMultiline', true)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,44 @@ module Cop
module Performance
# In Ruby 2.5, `String#delete_prefix` has been added.
#
# This cop identifies places where `gsub(/\Aprefix/, '')`
# This cop identifies places where `gsub(/\Aprefix/, '')` and `sub(/\Aprefix/, '')`
# can be replaced by `delete_prefix('prefix')`.
#
# The `delete_prefix('prefix')` method is faster than
# `gsub(/\Aprefix/, '')`.
# This cop has `SafeMultiline` configuration option that `true` by default because
# `^prefix` is unsafe as it will behave incompatible with `delete_prefix`
# for receiver is multiline string.
#
# The `delete_prefix('prefix')` method is faster than `gsub(/\Aprefix/, '')`.
#
# @example
#
# # bad
# str.gsub(/\Aprefix/, '')
# str.gsub!(/\Aprefix/, '')
# str.gsub(/^prefix/, '')
# str.gsub!(/^prefix/, '')
#
# str.sub(/\Aprefix/, '')
# str.sub!(/\Aprefix/, '')
#
# # good
# str.delete_prefix('prefix')
# str.delete_prefix!('prefix')
#
# @example SafeMultiline: true (default)
#
# # good
# str.gsub(/^prefix/, '')
# str.gsub!(/^prefix/, '')
# str.sub(/^prefix/, '')
# str.sub!(/^prefix/, '')
#
# @example SafeMultiline: false
#
# # bad
# str.gsub(/^prefix/, '')
# str.gsub!(/^prefix/, '')
# str.sub(/^prefix/, '')
# str.sub!(/^prefix/, '')
#
class DeletePrefix < Cop
extend TargetRubyVersion
include RegexpMetacharacter
Expand All @@ -33,15 +53,17 @@ class DeletePrefix < Cop

PREFERRED_METHODS = {
gsub: :delete_prefix,
gsub!: :delete_prefix!
gsub!: :delete_prefix!,
sub: :delete_prefix,
sub!: :delete_prefix!
}.freeze

def_node_matcher :gsub_method?, <<~PATTERN
(send $!nil? ${:gsub :gsub!} (regexp (str $#literal_at_start?) (regopt)) (str $_))
def_node_matcher :delete_prefix_candidate?, <<~PATTERN
(send $!nil? ${:gsub :gsub! :sub :sub!} (regexp (str $#literal_at_start?) (regopt)) (str $_))
PATTERN

def on_send(node)
gsub_method?(node) do |_, bad_method, _, replace_string|
delete_prefix_candidate?(node) do |_, bad_method, _, replace_string|
return unless replace_string.blank?

good_method = PREFERRED_METHODS[bad_method]
Expand All @@ -53,7 +75,7 @@ def on_send(node)
end

def autocorrect(node)
gsub_method?(node) do |receiver, bad_method, regexp_str, _|
delete_prefix_candidate?(node) do |receiver, bad_method, regexp_str, _|
lambda do |corrector|
good_method = PREFERRED_METHODS[bad_method]
regexp_str = drop_start_metacharacter(regexp_str)
Expand All @@ -62,7 +84,9 @@ def autocorrect(node)

new_code = "#{receiver.source}.#{good_method}(#{string_literal})"

corrector.replace(node, new_code)
# TODO: `source_range` is no longer required when RuboCop 0.81 or lower support will be dropped.
# https://github.com/rubocop-hq/rubocop/commit/82eb350d2cba16
corrector.replace(node.source_range, new_code)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,44 @@ module Cop
module Performance
# In Ruby 2.5, `String#delete_suffix` has been added.
#
# This cop identifies places where `gsub(/suffix\z/, '')`
# This cop identifies places where `gsub(/suffix\z/, '')` and `sub(/suffix\z/, '')`
# can be replaced by `delete_suffix('suffix')`.
#
# The `delete_suffix('suffix')` method is faster than
# `gsub(/suffix\z/, '')`.
# This cop has `SafeMultiline` configuration option that `true` by default because
# `suffix$` is unsafe as it will behave incompatible with `delete_suffix?`
# for receiver is multiline string.
#
# 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$/, '')
#
# str.sub(/suffix\z/, '')
# str.sub!(/suffix\z/, '')
#
# # good
# str.delete_suffix('suffix')
# str.delete_suffix!('suffix')
#
# @example SafeMultiline: true (default)
#
# # good
# str.gsub(/suffix$/, '')
# str.gsub!(/suffix$/, '')
# str.sub(/suffix$/, '')
# str.sub!(/suffix$/, '')
#
# @example SafeMultiline: false
#
# # bad
# str.gsub(/suffix$/, '')
# str.gsub!(/suffix$/, '')
# str.sub(/suffix$/, '')
# str.sub!(/suffix$/, '')
#
class DeleteSuffix < Cop
extend TargetRubyVersion
include RegexpMetacharacter
Expand All @@ -33,15 +53,17 @@ class DeleteSuffix < Cop

PREFERRED_METHODS = {
gsub: :delete_suffix,
gsub!: :delete_suffix!
gsub!: :delete_suffix!,
sub: :delete_suffix,
sub!: :delete_suffix!
}.freeze

def_node_matcher :gsub_method?, <<~PATTERN
(send $!nil? ${:gsub :gsub!} (regexp (str $#literal_at_end?) (regopt)) (str $_))
def_node_matcher :delete_suffix_candidate?, <<~PATTERN
(send $!nil? ${:gsub :gsub! :sub :sub!} (regexp (str $#literal_at_end?) (regopt)) (str $_))
PATTERN

def on_send(node)
gsub_method?(node) do |_, bad_method, _, replace_string|
delete_suffix_candidate?(node) do |_, bad_method, _, replace_string|
return unless replace_string.blank?

good_method = PREFERRED_METHODS[bad_method]
Expand All @@ -53,7 +75,7 @@ def on_send(node)
end

def autocorrect(node)
gsub_method?(node) do |receiver, bad_method, regexp_str, _|
delete_suffix_candidate?(node) do |receiver, bad_method, regexp_str, _|
lambda do |corrector|
good_method = PREFERRED_METHODS[bad_method]
regexp_str = drop_end_metacharacter(regexp_str)
Expand All @@ -62,7 +84,9 @@ def autocorrect(node)

new_code = "#{receiver.source}.#{good_method}(#{string_literal})"

corrector.replace(node, new_code)
# TODO: `source_range` is no longer required when RuboCop 0.81 or lower support will be dropped.
# https://github.com/rubocop-hq/rubocop/commit/82eb350d2cba16
corrector.replace(node.source_range, new_code)
end
end
end
Expand Down
Loading

0 comments on commit 6af8b76

Please sign in to comment.