From 831a99e00e6ea20f441a464dbe3b2c1ee3cb3a61 Mon Sep 17 00:00:00 2001 From: Tejas Bubane Date: Wed, 1 Apr 2020 21:25:27 +0530 Subject: [PATCH] Fix `Lint/UriRegexp` to register offense with array arguments Closes #7834 --- CHANGELOG.md | 1 + lib/rubocop/cop/lint/uri_regexp.rb | 6 ++-- spec/rubocop/cop/lint/uri_regexp_spec.rb | 36 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dce4c7eca0..f97844e9535 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/lint/uri_regexp.rb b/lib/rubocop/cop/lint/uri_regexp.rb index dbefc8d8579..2c17b7f869b 100644 --- a/lib/rubocop/cop/lint/uri_regexp.rb +++ b/lib/rubocop/cop/lint/uri_regexp.rb @@ -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 @@ -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 @@ -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, diff --git a/spec/rubocop/cop/lint/uri_regexp_spec.rb b/spec/rubocop/cop/lint/uri_regexp_spec.rb index 259d5cee5fd..990e4c76c71 100644 --- a/spec/rubocop/cop/lint/uri_regexp_spec.rb +++ b/spec/rubocop/cop/lint/uri_regexp_spec.rb @@ -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