Skip to content

Commit

Permalink
Fix Lint/UriRegexp to register offense with array arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasbubane committed Apr 4, 2020
1 parent 5365e61 commit 831a99e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#7842](https://github.com/rubocop-hq/rubocop/issues/7842): Fix a false positive for `Lint/RaiseException` when raising Exception with explicit namespace. ([@koic][])
* [#7834](https://github.com/rubocop-hq/rubocop/issues/7834): Fix `Lint/UriRegexp` to register offense with array arguments. ([@tejasbubane][])

## 0.81.0 (2020-04-01)

Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/lint/uri_regexp.rb
Expand Up @@ -21,7 +21,7 @@ class UriRegexp < Cop
def_node_matcher :uri_regexp_with_argument?, <<~PATTERN
(send
(const ${nil? cbase} :URI) :regexp
(str $_))
${(str _) (array ...)})
PATTERN

def_node_matcher :uri_regexp_without_argument?, <<~PATTERN
Expand All @@ -32,7 +32,7 @@ class UriRegexp < Cop
def on_send(node)
uri_regexp_with_argument?(node) do |double_colon, arg|
register_offense(
node, top_level: double_colon ? '::' : '', arg: "('#{arg}')"
node, top_level: double_colon ? '::' : '', arg: "(#{arg.source})"
)
end

Expand All @@ -51,7 +51,7 @@ def autocorrect(node)
double_colon, arg = captured_values

top_level = double_colon ? '::' : ''
argument = arg ? "('#{arg}')" : ''
argument = arg ? "(#{arg.source})" : ''

corrector.replace(
node.loc.expression,
Expand Down
36 changes: 36 additions & 0 deletions spec/rubocop/cop/lint/uri_regexp_spec.rb
Expand Up @@ -49,4 +49,40 @@
::URI::DEFAULT_PARSER.make_regexp
RUBY
end

context 'array argument' do
it 'registers an offense and corrects using `URI.regexp` with '\
'literal arrays' do
expect_offense(<<~RUBY)
URI.regexp(['http', 'https'])
^^^^^^ `URI.regexp(['http', 'https'])` is obsolete and should not be used. Instead, use `URI::DEFAULT_PARSER.make_regexp(['http', 'https'])`.
RUBY

expect_correction(<<~RUBY)
URI::DEFAULT_PARSER.make_regexp(['http', 'https'])
RUBY
end

it 'registers an offense and corrects using `URI.regexp` with %w arrays' do
expect_offense(<<~RUBY)
URI.regexp(%w[http https])
^^^^^^ `URI.regexp(%w[http https])` is obsolete and should not be used. Instead, use `URI::DEFAULT_PARSER.make_regexp(%w[http https])`.
RUBY

expect_correction(<<~RUBY)
URI::DEFAULT_PARSER.make_regexp(%w[http https])
RUBY
end

it 'registers an offense and corrects using `URI.regexp` with %i arrays' do
expect_offense(<<~RUBY)
URI.regexp(%i[http https])
^^^^^^ `URI.regexp(%i[http https])` is obsolete and should not be used. Instead, use `URI::DEFAULT_PARSER.make_regexp(%i[http https])`.
RUBY

expect_correction(<<~RUBY)
URI::DEFAULT_PARSER.make_regexp(%i[http https])
RUBY
end
end
end

0 comments on commit 831a99e

Please sign in to comment.