Skip to content

Commit

Permalink
Apply reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
mfbmina committed Nov 8, 2020
1 parent 34c5dea commit 9db4848
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 47 deletions.
13 changes: 6 additions & 7 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,6 @@ Performance/Detect:
VersionAdded: '0.30'
VersionChanged: '1.8'

Performance/DeterministicRegexp:
Description: 'This cop identifies places where `split` argument can be replaced from a deterministic regexp to a string.'
SafeAutoCorrect: true
Enabled: true
VersionAdded: '1.8'
VersionChanged: '1.8'

Performance/DoubleStartEndWith:
Description: >-
Use `str.{start,end}_with?(x, ..., y, ...)`
Expand Down Expand Up @@ -222,6 +215,12 @@ Performance/RedundantSortBlock:
Enabled: 'pending'
VersionAdded: '1.7'

Performance/RedundantSplitRegexpArgument:
Description: 'This cop identifies places where `split` argument can be replaced from a deterministic regexp to a string.'
SafeAutoCorrect: true
Enabled: true
VersionAdded: '1.9'

Performance/RedundantStringChars:
Description: 'Checks for redundant `String#chars`.'
Enabled: 'pending'
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Performance cops optimization analysis for your projects.
* xref:cops_performance.adoc#performancedeleteprefix[Performance/DeletePrefix]
* xref:cops_performance.adoc#performancedeletesuffix[Performance/DeleteSuffix]
* xref:cops_performance.adoc#performancedetect[Performance/Detect]
* xref:cops_performance.adoc#performancedeterministicregexp[Performance/DeterministicRegexp]
* xref:cops_performance.adoc#performancedoublestartendwith[Performance/DoubleStartEndWith]
* xref:cops_performance.adoc#performanceendwith[Performance/EndWith]
* xref:cops_performance.adoc#performancefixedsize[Performance/FixedSize]
Expand All @@ -42,6 +41,7 @@ Performance cops optimization analysis for your projects.
* xref:cops_performance.adoc#performanceredundantmatch[Performance/RedundantMatch]
* xref:cops_performance.adoc#performanceredundantmerge[Performance/RedundantMerge]
* xref:cops_performance.adoc#performanceredundantsortblock[Performance/RedundantSortBlock]
* xref:cops_performance.adoc#performanceredundantsplitregexpargument[Performance/RedundantSplitRegexpArgument]
* xref:cops_performance.adoc#performanceredundantstringchars[Performance/RedundantStringChars]
* xref:cops_performance.adoc#performanceregexpmatch[Performance/RegexpMatch]
* xref:cops_performance.adoc#performancereverseeach[Performance/ReverseEach]
Expand Down
52 changes: 26 additions & 26 deletions docs/modules/ROOT/pages/cops_performance.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -722,32 +722,6 @@ considered unsafe.

* https://github.com/JuanitoFatas/fast-ruby#enumerabledetect-vs-enumerableselectfirst-code

== Performance/DeterministicRegexp

|===
| Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged

| Enabled
| Yes
| Yes
| 1.8
| 1.8
|===

This cop identifies places where `split` argument can be replaced from
a deterministic regexp to a string.

=== Examples

[source,ruby]
----
# bad
'a,b,c'.split(/,/)
# good
'a,b,c'.split(',')
----

== Performance/DoubleStartEndWith

|===
Expand Down Expand Up @@ -1328,6 +1302,32 @@ array.sort { |a, b| a <=> b }
array.sort
----

== Performance/RedundantSplitRegexpArgument

|===
| Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged

| Enabled
| Yes
| Yes
| 1.9
| -
|===

This cop identifies places where `split` argument can be replaced from
a deterministic regexp to a string.

=== Examples

[source,ruby]
----
# bad
'a,b,c'.split(/,/)
# good
'a,b,c'.split(',')
----

== Performance/RedundantStringChars

|===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Performance
#
# # good
# 'a,b,c'.split(',')
class DeterministicRegexp < Base
class RedundantSplitRegexpArgument < Base
extend AutoCorrector

MSG = 'Use string as argument instead of regexp.'
Expand All @@ -35,7 +35,7 @@ def on_send(node)
private

def determinist_regexp?(first_argument)
first_argument.source =~ DETERMINISTIC_REGEX
DETERMINISTIC_REGEX.match?(first_argument.source)
end

def autocorrect(corrector, node)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/performance_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
require_relative 'performance/delete_prefix'
require_relative 'performance/delete_suffix'
require_relative 'performance/detect'
require_relative 'performance/deterministic_regexp'
require_relative 'performance/double_start_end_with'
require_relative 'performance/end_with'
require_relative 'performance/fixed_size'
Expand All @@ -32,6 +31,7 @@
require_relative 'performance/redundant_match'
require_relative 'performance/redundant_merge'
require_relative 'performance/redundant_sort_block'
require_relative 'performance/redundant_split_regexp_argument'
require_relative 'performance/redundant_string_chars'
require_relative 'performance/regexp_match'
require_relative 'performance/reverse_each'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Performance::DeterministicRegexp do
RSpec.describe RuboCop::Cop::Performance::RedundantSplitRegexpArgument do
subject(:cop) { described_class.new }

it 'accepts methods other than split' do
Expand All @@ -20,15 +20,9 @@
'a,b,c'.split(/,/)
^^^^^^^^^^^^^^^^^^ Use string as argument instead of regexp.
RUBY
end

context 'when auto-correcting' do
describe 'converts regexp to string' do
it 'corrects when the length of the pattern and replacement are one' do
new_source = autocorrect_source("'a,b,c'.split(/,/)")

expect(new_source).to eq("'a,b,c'.split(',')")
end
end
expect_correction(<<~RUBY)
'a,b,c'.split(',')
RUBY
end
end

0 comments on commit 9db4848

Please sign in to comment.