-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from mfbmina/regexp-in-split
Cop for deterministic regexp on String#split
- Loading branch information
Showing
7 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# This cop identifies places where `split` argument can be replaced from | ||
# a deterministic regexp to a string. | ||
# | ||
# @example | ||
# # bad | ||
# 'a,b,c'.split(/,/) | ||
# | ||
# # good | ||
# 'a,b,c'.split(',') | ||
class RedundantSplitRegexpArgument < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use string as argument instead of regexp.' | ||
RESTRICT_ON_SEND = %i[split].freeze | ||
DETERMINISTIC_REGEX = /\A(?:#{LITERAL_REGEX})+\Z/.freeze | ||
STR_SPECIAL_CHARS = %w[\n \" \' \\ \t \b \f \r].freeze | ||
|
||
def_node_matcher :split_call_with_regexp?, <<~PATTERN | ||
{(send !nil? :split {regexp})} | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless split_call_with_regexp?(node) | ||
return unless determinist_regexp?(node.first_argument) | ||
|
||
add_offense(node.first_argument) do |corrector| | ||
autocorrect(corrector, node) | ||
end | ||
end | ||
|
||
private | ||
|
||
def determinist_regexp?(first_argument) | ||
DETERMINISTIC_REGEX.match?(first_argument.source) | ||
end | ||
|
||
def autocorrect(corrector, node) | ||
new_argument = node.first_argument.source[1..-2] | ||
new_argument.delete!('\\') unless STR_SPECIAL_CHARS.include?(new_argument) | ||
|
||
corrector.replace(node.first_argument, "\"#{new_argument}\"") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
spec/rubocop/cop/performance/redundant_split_regexp_argument_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Performance::RedundantSplitRegexpArgument do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'accepts methods other than split' do | ||
expect_no_offenses("'a,b,c'.insert(2, 'a')") | ||
end | ||
|
||
it 'accepts when split does not receive a regexp' do | ||
expect_no_offenses("'a,b,c'.split(',')") | ||
end | ||
|
||
it 'accepts when split does not receive a deterministic regexp' do | ||
expect_no_offenses("'a,b,c'.split(/,+/)") | ||
end | ||
|
||
it 'registers an offense when the method is split and correctly removes escaping characters' do | ||
expect_offense(<<~RUBY) | ||
'a,b,c'.split(/\\./) | ||
^^^^ Use string as argument instead of regexp. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
'a,b,c'.split(".") | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when the method is split and corrects correctly special string chars' do | ||
expect_offense(<<~RUBY) | ||
"foo\\nbar\\nbaz\\n".split(/\\n/) | ||
^^^^ Use string as argument instead of regexp. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
"foo\\nbar\\nbaz\\n".split("\\n") | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when the method is split' do | ||
expect_offense(<<~RUBY) | ||
'a,b,c'.split(/,/) | ||
^^^ Use string as argument instead of regexp. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
'a,b,c'.split(",") | ||
RUBY | ||
end | ||
end |