rubocop 0.48.0 → 0.48.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +5 -0
- data/config/enabled.yml +2 -1
- data/lib/rubocop/ast/builder.rb +2 -2
- data/lib/rubocop/ast/node.rb +14 -14
- data/lib/rubocop/ast/node/send_node.rb +8 -3
- data/lib/rubocop/ast/traversal.rb +8 -8
- data/lib/rubocop/config.rb +3 -3
- data/lib/rubocop/config_loader.rb +2 -2
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +26 -13
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
- data/lib/rubocop/cop/lint/inherit_exception.rb +2 -2
- data/lib/rubocop/cop/lint/literal_in_condition.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +26 -3
- data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_code.rb +2 -2
- data/lib/rubocop/cop/lint/useless_comparison.rb +1 -1
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +1 -1
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +2 -2
- data/lib/rubocop/cop/metrics/parameter_lists.rb +1 -1
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +2 -2
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +1 -1
- data/lib/rubocop/cop/mixin/def_node.rb +1 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
- data/lib/rubocop/cop/mixin/on_method_def.rb +1 -1
- data/lib/rubocop/cop/mixin/percent_literal.rb +45 -4
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/unused_argument.rb +1 -1
- data/lib/rubocop/cop/offense.rb +2 -2
- data/lib/rubocop/cop/performance/casecmp.rb +1 -1
- data/lib/rubocop/cop/performance/end_with.rb +1 -2
- data/lib/rubocop/cop/performance/redundant_match.rb +1 -1
- data/lib/rubocop/cop/performance/regexp_match.rb +2 -2
- data/lib/rubocop/cop/performance/start_with.rb +1 -2
- data/lib/rubocop/cop/rails/action_filter.rb +4 -4
- data/lib/rubocop/cop/rails/blank.rb +1 -0
- data/lib/rubocop/cop/rails/date.rb +3 -3
- data/lib/rubocop/cop/rails/delegate.rb +1 -1
- data/lib/rubocop/cop/rails/exit.rb +2 -2
- data/lib/rubocop/cop/rails/file_path.rb +5 -1
- data/lib/rubocop/cop/rails/find_by.rb +1 -1
- data/lib/rubocop/cop/rails/find_each.rb +2 -2
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +3 -3
- data/lib/rubocop/cop/rails/relative_date_constant.rb +7 -3
- data/lib/rubocop/cop/rails/request_referer.rb +17 -2
- data/lib/rubocop/cop/rails/save_bang.rb +4 -4
- data/lib/rubocop/cop/rails/skips_model_validations.rb +2 -2
- data/lib/rubocop/cop/rails/time_zone.rb +6 -6
- data/lib/rubocop/cop/rails/validation.rb +2 -2
- data/lib/rubocop/cop/severity.rb +1 -1
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +34 -0
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +3 -3
- data/lib/rubocop/cop/style/constant_name.rb +1 -1
- data/lib/rubocop/cop/style/each_with_object.rb +1 -1
- data/lib/rubocop/cop/style/empty_line_after_magic_comment.rb +1 -1
- data/lib/rubocop/cop/style/file_name.rb +2 -2
- data/lib/rubocop/cop/style/global_vars.rb +2 -2
- data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
- data/lib/rubocop/cop/style/indentation_width.rb +2 -2
- data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +5 -5
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +15 -7
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/mixin_grouping.rb +2 -2
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -2
- data/lib/rubocop/cop/style/next.rb +3 -3
- data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
- data/lib/rubocop/cop/style/one_line_conditional.rb +2 -2
- data/lib/rubocop/cop/style/op_method.rb +2 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +1 -1
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +5 -36
- data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +2 -2
- data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self.rb +4 -5
- data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
- data/lib/rubocop/cop/style/self_assignment.rb +3 -3
- data/lib/rubocop/cop/style/space_around_keyword.rb +9 -9
- data/lib/rubocop/cop/style/space_around_operators.rb +1 -1
- data/lib/rubocop/cop/style/space_inside_brackets.rb +1 -1
- data/lib/rubocop/cop/style/space_inside_parens.rb +1 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +10 -10
- data/lib/rubocop/cop/style/symbol_proc.rb +2 -2
- data/lib/rubocop/cop/style/ternary_parentheses.rb +3 -2
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/util.rb +7 -7
- data/lib/rubocop/cop/variable_force.rb +7 -7
- data/lib/rubocop/cop/variable_force/variable.rb +2 -2
- data/lib/rubocop/formatter/colorizable.rb +2 -2
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- data/lib/rubocop/options.rb +20 -11
- data/lib/rubocop/processed_source.rb +1 -1
- data/lib/rubocop/result_cache.rb +2 -2
- data/lib/rubocop/rspec/shared_examples.rb +1 -1
- data/lib/rubocop/runner.rb +1 -1
- data/lib/rubocop/target_finder.rb +6 -6
- data/lib/rubocop/version.rb +1 -1
- metadata +2 -2
@@ -14,10 +14,10 @@ module RuboCop
|
|
14
14
|
class BlockNesting < Cop
|
15
15
|
include ConfigurableMax
|
16
16
|
|
17
|
-
NESTING_BLOCKS = %i
|
17
|
+
NESTING_BLOCKS = %i[
|
18
18
|
case if while while_post
|
19
19
|
until until_post for resbody
|
20
|
-
|
20
|
+
].freeze
|
21
21
|
|
22
22
|
def investigate(processed_source)
|
23
23
|
return unless processed_source.ast
|
@@ -17,8 +17,8 @@ module RuboCop
|
|
17
17
|
include MethodComplexity
|
18
18
|
|
19
19
|
MSG = 'Cyclomatic complexity for %s is too high. [%d/%d]'.freeze
|
20
|
-
COUNTED_NODES = %i
|
21
|
-
rescue when and or
|
20
|
+
COUNTED_NODES = %i[if while until for
|
21
|
+
rescue when and or].freeze
|
22
22
|
|
23
23
|
private
|
24
24
|
|
@@ -30,8 +30,8 @@ module RuboCop
|
|
30
30
|
include MethodComplexity
|
31
31
|
|
32
32
|
MSG = 'Perceived complexity for %s is too high. [%d/%d]'.freeze
|
33
|
-
COUNTED_NODES = %i
|
34
|
-
for rescue and or
|
33
|
+
COUNTED_NODES = %i[if case while until
|
34
|
+
for rescue and or].freeze
|
35
35
|
|
36
36
|
private
|
37
37
|
|
@@ -32,7 +32,7 @@ module RuboCop
|
|
32
32
|
if ancestor.block_type?
|
33
33
|
return true if ancestor.class_constructor?
|
34
34
|
elsif !ancestor.begin_type?
|
35
|
-
return %i
|
35
|
+
return %i[casgn sclass class module].include?(ancestor.type)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
@@ -8,7 +8,7 @@ module RuboCop
|
|
8
8
|
|
9
9
|
FROZEN_STRING_LITERAL = '# frozen_string_literal:'.freeze
|
10
10
|
FROZEN_STRING_LITERAL_ENABLED = '# frozen_string_literal: true'.freeze
|
11
|
-
FROZEN_STRING_LITERAL_TYPES = %i
|
11
|
+
FROZEN_STRING_LITERAL_TYPES = %i[str dstr].freeze
|
12
12
|
|
13
13
|
def frozen_string_literal_comment_exists?
|
14
14
|
leading_comment_lines.any? do |line|
|
@@ -4,6 +4,8 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
# Common functionality for handling percent literals.
|
6
6
|
module PercentLiteral
|
7
|
+
PERCENT_LITERAL_TYPES = %w[% %i %I %q %Q %r %s %w %W %x].freeze
|
8
|
+
|
7
9
|
private
|
8
10
|
|
9
11
|
def percent_literal?(node)
|
@@ -35,14 +37,18 @@ module RuboCop
|
|
35
37
|
words = node.children
|
36
38
|
escape = words.any? { |w| needs_escaping?(w.children[0]) }
|
37
39
|
char = char.upcase if escape
|
38
|
-
|
40
|
+
delimiters = preferred_delimiters_for("%#{char}")
|
41
|
+
contents = autocorrect_words(words, escape, node.loc.line, delimiters)
|
39
42
|
|
40
43
|
lambda do |corrector|
|
41
|
-
corrector.replace(
|
44
|
+
corrector.replace(
|
45
|
+
node.source_range,
|
46
|
+
"%#{char}#{delimiters[0]}#{contents}#{delimiters[1]}"
|
47
|
+
)
|
42
48
|
end
|
43
49
|
end
|
44
50
|
|
45
|
-
def autocorrect_words(word_nodes, escape, base_line_number)
|
51
|
+
def autocorrect_words(word_nodes, escape, base_line_number, delimiters)
|
46
52
|
previous_node_line_number = base_line_number
|
47
53
|
word_nodes.map do |node|
|
48
54
|
number_of_line_breaks = node.loc.line - previous_node_line_number
|
@@ -50,10 +56,45 @@ module RuboCop
|
|
50
56
|
previous_node_line_number = node.loc.line
|
51
57
|
content = node.children.first.to_s
|
52
58
|
content = escape ? escape_string(content) : content
|
53
|
-
|
59
|
+
delimiters.each do |delimiter|
|
60
|
+
content.gsub!(delimiter, "\\#{delimiter}")
|
61
|
+
end
|
54
62
|
line_breaks + content
|
55
63
|
end.join(' ')
|
56
64
|
end
|
65
|
+
|
66
|
+
def ensure_valid_preferred_delimiters
|
67
|
+
invalid = preferred_delimiters_config.keys -
|
68
|
+
(PERCENT_LITERAL_TYPES + %w[default])
|
69
|
+
return if invalid.empty?
|
70
|
+
|
71
|
+
raise ArgumentError,
|
72
|
+
"Invalid preferred delimiter config key: #{invalid.join(', ')}"
|
73
|
+
end
|
74
|
+
|
75
|
+
def preferred_delimiters
|
76
|
+
@preferred_delimiters ||=
|
77
|
+
begin
|
78
|
+
ensure_valid_preferred_delimiters
|
79
|
+
|
80
|
+
if preferred_delimiters_config.key?('default')
|
81
|
+
Hash[PERCENT_LITERAL_TYPES.map do |type|
|
82
|
+
[type, preferred_delimiters_config[type] ||
|
83
|
+
preferred_delimiters_config['default']]
|
84
|
+
end]
|
85
|
+
else
|
86
|
+
preferred_delimiters_config
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def preferred_delimiters_config
|
92
|
+
@config.for_cop('Style/PercentLiteralDelimiters')['PreferredDelimiters']
|
93
|
+
end
|
94
|
+
|
95
|
+
def preferred_delimiters_for(type)
|
96
|
+
preferred_delimiters[type].split(//)
|
97
|
+
end
|
57
98
|
end
|
58
99
|
end
|
59
100
|
end
|
data/lib/rubocop/cop/offense.rb
CHANGED
@@ -19,7 +19,7 @@ module RuboCop
|
|
19
19
|
# 'abc'.casecmp(str).zero?
|
20
20
|
class Casecmp < Cop
|
21
21
|
MSG = 'Use `casecmp` instead of `%s %s`.'.freeze
|
22
|
-
CASE_METHODS = %i
|
22
|
+
CASE_METHODS = %i[downcase upcase].freeze
|
23
23
|
|
24
24
|
def_node_matcher :downcase_eq, <<-END
|
25
25
|
(send
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Performance
|
6
|
-
# This cop identifies use of `Regexp#match` or `String#match in a context
|
6
|
+
# This cop identifies use of `Regexp#match` or `String#match` in a context
|
7
7
|
# where the integral return value of `=~` would do just as well.
|
8
8
|
#
|
9
9
|
# @example
|
@@ -191,14 +191,14 @@ module RuboCop
|
|
191
191
|
end
|
192
192
|
|
193
193
|
def match_gvar?(sym)
|
194
|
-
%i
|
194
|
+
%i[
|
195
195
|
$~
|
196
196
|
$MATCH
|
197
197
|
$PREMATCH
|
198
198
|
$POSTMATCH
|
199
199
|
$LAST_PAREN_MATCH
|
200
200
|
$LAST_MATCH_INFO
|
201
|
-
|
201
|
+
].include?(sym)
|
202
202
|
end
|
203
203
|
|
204
204
|
def correct_operator(corrector, recv, arg)
|
@@ -12,8 +12,7 @@ module RuboCop
|
|
12
12
|
# 'abc'.match(/\Aab/)
|
13
13
|
#
|
14
14
|
# @good
|
15
|
-
# 'abc'
|
16
|
-
# 'abc' =~ /\A\w*/
|
15
|
+
# 'abc'.start_with?('ab')
|
17
16
|
class StartWith < Cop
|
18
17
|
MSG = 'Use `String#start_with?` instead of a regex match anchored to ' \
|
19
18
|
'the beginning of the string.'.freeze
|
@@ -16,7 +16,7 @@ module RuboCop
|
|
16
16
|
|
17
17
|
MSG = 'Prefer `%s` over `%s`.'.freeze
|
18
18
|
|
19
|
-
FILTER_METHODS = %i
|
19
|
+
FILTER_METHODS = %i[
|
20
20
|
after_filter
|
21
21
|
append_after_filter
|
22
22
|
append_around_filter
|
@@ -30,9 +30,9 @@ module RuboCop
|
|
30
30
|
skip_around_filter
|
31
31
|
skip_before_filter
|
32
32
|
skip_filter
|
33
|
-
|
33
|
+
].freeze
|
34
34
|
|
35
|
-
ACTION_METHODS = %i
|
35
|
+
ACTION_METHODS = %i[
|
36
36
|
after_action
|
37
37
|
append_after_action
|
38
38
|
append_around_action
|
@@ -46,7 +46,7 @@ module RuboCop
|
|
46
46
|
skip_around_action
|
47
47
|
skip_before_action
|
48
48
|
skip_action_callback
|
49
|
-
|
49
|
+
].freeze
|
50
50
|
|
51
51
|
minimum_target_rails_version 4.0
|
52
52
|
|
@@ -43,7 +43,7 @@ module RuboCop
|
|
43
43
|
MSG_SEND = 'Do not use `%s` on Date objects, because they ' \
|
44
44
|
'know nothing about the time zone in use.'.freeze
|
45
45
|
|
46
|
-
BAD_DAYS = %i
|
46
|
+
BAD_DAYS = %i[today current yesterday tomorrow].freeze
|
47
47
|
|
48
48
|
def on_const(node)
|
49
49
|
mod, klass = *node.children
|
@@ -107,7 +107,7 @@ module RuboCop
|
|
107
107
|
end
|
108
108
|
|
109
109
|
def good_days
|
110
|
-
style == :strict ? [] : %i
|
110
|
+
style == :strict ? [] : %i[current yesterday tomorrow]
|
111
111
|
end
|
112
112
|
|
113
113
|
def bad_days
|
@@ -115,7 +115,7 @@ module RuboCop
|
|
115
115
|
end
|
116
116
|
|
117
117
|
def bad_methods
|
118
|
-
style == :strict ? %i
|
118
|
+
style == :strict ? %i[to_time to_time_in_current_zone] : [:to_time]
|
119
119
|
end
|
120
120
|
|
121
121
|
def good_methods
|
@@ -94,7 +94,7 @@ module RuboCop
|
|
94
94
|
end
|
95
95
|
|
96
96
|
def private_or_protected_before(line)
|
97
|
-
(processed_source[0..line].map(&:strip) & %w
|
97
|
+
(processed_source[0..line].map(&:strip) & %w[private protected]).any?
|
98
98
|
end
|
99
99
|
|
100
100
|
def private_or_protected_inline(line)
|
@@ -20,8 +20,8 @@ module RuboCop
|
|
20
20
|
include ConfigurableEnforcedStyle
|
21
21
|
|
22
22
|
MSG = 'Do not use `exit` in Rails applications.'.freeze
|
23
|
-
TARGET_METHODS = %i
|
24
|
-
EXPLICIT_RECEIVERS = %i
|
23
|
+
TARGET_METHODS = %i[exit exit!].freeze
|
24
|
+
EXPLICIT_RECEIVERS = %i[Kernel Process].freeze
|
25
25
|
|
26
26
|
def on_send(node)
|
27
27
|
add_offense(node, :selector) if offending_node?(node)
|
@@ -51,11 +51,15 @@ module RuboCop
|
|
51
51
|
def check_for_rails_root_join_with_slash_separated_path(node)
|
52
52
|
return unless rails_root_nodes?(node)
|
53
53
|
return unless rails_root_join_nodes?(node)
|
54
|
-
return unless node.method_args.any? { |arg| arg
|
54
|
+
return unless node.method_args.any? { |arg| string_with_slash?(arg) }
|
55
55
|
|
56
56
|
register_offense(node)
|
57
57
|
end
|
58
58
|
|
59
|
+
def string_with_slash?(node)
|
60
|
+
node.type == :str && node.source =~ %r{/}
|
61
|
+
end
|
62
|
+
|
59
63
|
def register_offense(node)
|
60
64
|
line_range = node.loc.column...node.loc.last_column
|
61
65
|
|
@@ -15,7 +15,7 @@ module RuboCop
|
|
15
15
|
# User.find_by(name: 'Bruce')
|
16
16
|
class FindBy < Cop
|
17
17
|
MSG = 'Use `find_by` instead of `where.%s`.'.freeze
|
18
|
-
TARGET_SELECTORS = %i
|
18
|
+
TARGET_SELECTORS = %i[first take].freeze
|
19
19
|
|
20
20
|
def_node_matcher :where_first?, <<-PATTERN
|
21
21
|
(send (send _ :where ...) {:first :take})
|
@@ -15,8 +15,8 @@ module RuboCop
|
|
15
15
|
class FindEach < Cop
|
16
16
|
MSG = 'Use `find_each` instead of `each`.'.freeze
|
17
17
|
|
18
|
-
SCOPE_METHODS = %i
|
19
|
-
IGNORED_METHODS = %i
|
18
|
+
SCOPE_METHODS = %i[all where not].freeze
|
19
|
+
IGNORED_METHODS = %i[order limit select].freeze
|
20
20
|
|
21
21
|
def on_send(node)
|
22
22
|
return unless node.receiver && node.method?(:each)
|
@@ -21,10 +21,10 @@ module RuboCop
|
|
21
21
|
|
22
22
|
MSG = 'Use keyword arguments instead of ' \
|
23
23
|
'positional arguments for http call: `%s`.'.freeze
|
24
|
-
KEYWORD_ARGS = %i
|
24
|
+
KEYWORD_ARGS = %i[
|
25
25
|
headers env params body flash as xhr session method
|
26
|
-
|
27
|
-
HTTP_METHODS = %i
|
26
|
+
].freeze
|
27
|
+
HTTP_METHODS = %i[get post put patch delete head].freeze
|
28
28
|
|
29
29
|
minimum_target_rails_version 5.0
|
30
30
|
|
@@ -22,10 +22,14 @@ module RuboCop
|
|
22
22
|
MSG = 'Do not assign %s to constants as it will be evaluated only ' \
|
23
23
|
'once.'.freeze
|
24
24
|
|
25
|
-
RELATIVE_DATE_METHODS = %i
|
25
|
+
RELATIVE_DATE_METHODS = %i[ago from_now since until].freeze
|
26
26
|
|
27
27
|
def on_casgn(node)
|
28
|
-
|
28
|
+
_scope, _constant, rhs = *node
|
29
|
+
|
30
|
+
return if rhs.lambda_or_proc?
|
31
|
+
|
32
|
+
bad_node = node.descendants.find { |n| relative_date_method?(n) }
|
29
33
|
return unless bad_node
|
30
34
|
|
31
35
|
add_offense(node, :expression, format(MSG, bad_node.method_name))
|
@@ -33,7 +37,7 @@ module RuboCop
|
|
33
37
|
|
34
38
|
private
|
35
39
|
|
36
|
-
def
|
40
|
+
def relative_date_method?(node)
|
37
41
|
node.send_type? &&
|
38
42
|
RELATIVE_DATE_METHODS.include?(node.method_name) &&
|
39
43
|
node.method_args.empty?
|
@@ -3,8 +3,23 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Rails
|
6
|
-
# This cop checks for consistent uses of request.
|
7
|
-
# request.referrer
|
6
|
+
# This cop checks for consistent uses of `request.referer` or
|
7
|
+
# `request.referrer`, depending on the cop's configuration.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# # EnforcedStyle: referer
|
11
|
+
# # bad
|
12
|
+
# request.referrer
|
13
|
+
#
|
14
|
+
# # good
|
15
|
+
# request.referer
|
16
|
+
#
|
17
|
+
# # EnforcedStyle: referrer
|
18
|
+
# # bad
|
19
|
+
# request.referer
|
20
|
+
#
|
21
|
+
# # good
|
22
|
+
# request.referrer
|
8
23
|
class RequestReferer < Cop
|
9
24
|
include ConfigurableEnforcedStyle
|
10
25
|
|