Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Lint/UriRegexp cop #4694

Merged
merged 8 commits into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* [#4629](https://github.com/bbatsov/rubocop/issues/4629): Add Metrics/MethodLength cop for `define_method`. ([@jekuta][])
* [#4702](https://github.com/bbatsov/rubocop/pull/4702): Add new `Lint/UriEscapeUnescape` cop. ([@koic][])
* [#4696](https://github.com/bbatsov/rubocop/pull/4696): Add new `Performance/UriDefaultParser` cop. ([@koic][])
* [#4694](https://github.com/bbatsov/rubocop/pull/4694): Add new `Lint/UriRegexp` cop. ([@koic][])

### Bug fixes

Expand Down
4 changes: 4 additions & 0 deletions config/enabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,10 @@ Lint/UriEscapeUnescape:
depending on your specific use case.
Enabled: true

Lint/UriRegexp:
Description: 'Use `URI::DEFAULT_PARSER.make_regexp` instead of `URI.regexp`.'
Enabled: true

Lint/UselessAccessModifier:
Description: 'Checks for useless access modifiers.'
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
require 'rubocop/cop/lint/unused_block_argument'
require 'rubocop/cop/lint/unused_method_argument'
require 'rubocop/cop/lint/uri_escape_unescape'
require 'rubocop/cop/lint/uri_regexp'
require 'rubocop/cop/lint/useless_access_modifier'
require 'rubocop/cop/lint/useless_assignment'
require 'rubocop/cop/lint/useless_comparison'
Expand Down
73 changes: 73 additions & 0 deletions lib/rubocop/cop/lint/uri_regexp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Lint
# This cop identifies places where `URI.regexp` is obsolete and should
# not be used. Instead, use `URI::DEFAULT_PARSER.make_regexp`.
#
# @example
# # bad
# URI.regexp('http://example.com')
#
# # good
# URI::DEFAULT_PARSER.make_regexp('http://example.com')
#
class UriRegexp < Cop
MSG = '`%<top_level>sURI.regexp%<arg>s` is obsolete and should not ' \
'be used. Instead, use `%<top_level>sURI::DEFAULT_PARSER.' \
'make_regexp%<arg>s`.'.freeze

def_node_matcher :uri_regexp_with_argument?, <<-PATTERN
(send
(const ${nil cbase} :URI) :regexp
(str $_))
PATTERN

def_node_matcher :uri_regexp_without_argument?, <<-PATTERN
(send
(const ${nil cbase} :URI) :regexp)
PATTERN

def on_send(node)
uri_regexp_with_argument?(node) do |double_colon, arg|
register_offense(
node, top_level: double_colon ? '::' : '', arg: "('#{arg}')"
)
end

uri_regexp_without_argument?(node) do |double_colon|
register_offense(node, top_level: double_colon ? '::' : '')
end
end

def autocorrect(node)
lambda do |corrector|
if (captured_values = uri_regexp_with_argument?(node))
else
captured_values = uri_regexp_without_argument?(node)
end

double_colon, arg = captured_values

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

corrector.replace(
node.loc.expression,
"#{top_level}URI::DEFAULT_PARSER.make_regexp#{argument}"
)
end
end

private

def register_offense(node, top_level: '', arg: '')
format = format(MSG, top_level: top_level, arg: arg)

add_offense(node, :selector, format)
end
end
end
end
end
1 change: 1 addition & 0 deletions manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ In the following section you find all available cops:
* [Lint/UnusedBlockArgument](cops_lint.md#lintunusedblockargument)
* [Lint/UnusedMethodArgument](cops_lint.md#lintunusedmethodargument)
* [Lint/UriEscapeUnescape](cops_lint.md#linturiescapeunescape)
* [Lint/UriRegexp](cops_lint.md#linturiregexp)
* [Lint/UselessAccessModifier](cops_lint.md#lintuselessaccessmodifier)
* [Lint/UselessAssignment](cops_lint.md#lintuselessassignment)
* [Lint/UselessComparison](cops_lint.md#lintuselesscomparison)
Expand Down
19 changes: 19 additions & 0 deletions manual/cops_lint.md
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,25 @@ URI.decode_www_form(enc_uri)
URI.decode_www_form_component(enc_uri)
```

## Lint/UriRegexp

Enabled by default | Supports autocorrection
--- | ---
Enabled | Yes

This cop identifies places where `URI.regexp` is obsolete and should
not be used. Instead, use `URI::DEFAULT_PARSER.make_regexp`.

### Example

```ruby
# bad
URI.regexp('http://example.com')

# good
URI::DEFAULT_PARSER.make_regexp('http://example.com')
```

## Lint/UselessAccessModifier

Enabled by default | Supports autocorrection
Expand Down
62 changes: 62 additions & 0 deletions spec/rubocop/cop/lint/uri_regexp_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

describe RuboCop::Cop::Lint::UriRegexp do
let(:config) { RuboCop::Config.new }
subject(:cop) { described_class.new(config) }

it 'registers an offense when using `URI.regexp` with argument' do
expect_offense(<<-RUBY.strip_indent)
URI.regexp('http://example.com')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about having the offense highlight more of the expression? It seems like this should highlight URI.regexp or URI.regexp('http://example.com')

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know if that is correct, but since URI module does not change, this cop focused on only regexp.

^^^^^^ `URI.regexp('http://example.com')` is obsolete and should not be used. Instead, use `URI::DEFAULT_PARSER.make_regexp('http://example.com')`.
RUBY
end

it 'registers an offense when using `::URI.regexp` with argument' do
expect_offense(<<-RUBY.strip_indent)
::URI.regexp('http://example.com')
^^^^^^ `::URI.regexp('http://example.com')` is obsolete and should not be used. Instead, use `::URI::DEFAULT_PARSER.make_regexp('http://example.com')`.
RUBY
end

it 'registers an offense when using `URI.regexp` without argument' do
expect_offense(<<-RUBY.strip_indent)
URI.regexp
^^^^^^ `URI.regexp` is obsolete and should not be used. Instead, use `URI::DEFAULT_PARSER.make_regexp`.
RUBY
end

it 'registers an offense when using `::URI.regexp` without argument' do
expect_offense(<<-RUBY.strip_indent)
::URI.regexp
^^^^^^ `::URI.regexp` is obsolete and should not be used. Instead, use `::URI::DEFAULT_PARSER.make_regexp`.
RUBY
end

it "autocorrects URI::DEFAULT_PARSER.make_regexp('http://example.com')" do
new_source = autocorrect_source("URI.regexp('http://example.com')")

expect(
new_source
).to eq "URI::DEFAULT_PARSER.make_regexp('http://example.com')"
end

it "autocorrects ::URI::DEFAULT_PARSER.make_regexp('http://example.com')" do
new_source = autocorrect_source("::URI.regexp('http://example.com')")

expect(
new_source
).to eq "::URI::DEFAULT_PARSER.make_regexp('http://example.com')"
end

it 'autocorrects URI::DEFAULT_PARSER.make_regexp' do
new_source = autocorrect_source('URI.regexp')

expect(new_source).to eq 'URI::DEFAULT_PARSER.make_regexp'
end

it 'autocorrects ::URI::DEFAULT_PARSER.make_regexp' do
new_source = autocorrect_source('::URI.regexp')

expect(new_source).to eq '::URI::DEFAULT_PARSER.make_regexp'
end
end