rubocop 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -3
  3. data/config/default.yml +31 -5
  4. data/exe/rubocop +1 -1
  5. data/lib/rubocop.rb +4 -0
  6. data/lib/rubocop/cop/layout/else_alignment.rb +15 -2
  7. data/lib/rubocop/cop/layout/end_alignment.rb +3 -3
  8. data/lib/rubocop/cop/layout/hash_alignment.rb +4 -4
  9. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +24 -18
  10. data/lib/rubocop/cop/layout/space_inside_parens.rb +35 -13
  11. data/lib/rubocop/cop/lint/else_layout.rb +29 -3
  12. data/lib/rubocop/cop/lint/empty_block.rb +15 -2
  13. data/lib/rubocop/cop/lint/loop.rb +0 -4
  14. data/lib/rubocop/cop/lint/nested_percent_literal.rb +14 -0
  15. data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +58 -0
  16. data/lib/rubocop/cop/lint/useless_setter_call.rb +6 -1
  17. data/lib/rubocop/cop/mixin/configurable_numbering.rb +3 -3
  18. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +11 -1
  19. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +11 -5
  20. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +67 -18
  21. data/lib/rubocop/cop/naming/variable_number.rb +82 -8
  22. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +0 -4
  23. data/lib/rubocop/cop/style/case_like_if.rb +0 -4
  24. data/lib/rubocop/cop/style/collection_compact.rb +85 -0
  25. data/lib/rubocop/cop/style/double_negation.rb +6 -1
  26. data/lib/rubocop/cop/style/hash_syntax.rb +3 -3
  27. data/lib/rubocop/cop/style/keyword_parameters_order.rb +12 -0
  28. data/lib/rubocop/cop/style/mixin_grouping.rb +0 -4
  29. data/lib/rubocop/cop/style/negated_if_else_condition.rb +99 -0
  30. data/lib/rubocop/cop/style/raise_args.rb +21 -6
  31. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  32. data/lib/rubocop/cop/util.rb +4 -0
  33. data/lib/rubocop/ext/regexp_node.rb +10 -5
  34. data/lib/rubocop/ext/regexp_parser.rb +9 -2
  35. data/lib/rubocop/formatter/formatter_set.rb +1 -0
  36. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +47 -0
  37. data/lib/rubocop/options.rb +2 -0
  38. data/lib/rubocop/version.rb +1 -1
  39. metadata +7 -3
@@ -3,10 +3,14 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Lint
6
- # This cop checks for odd else block layout - like
7
- # having an expression on the same line as the else keyword,
6
+ # This cop checks for odd `else` block layout - like
7
+ # having an expression on the same line as the `else` keyword,
8
8
  # which is usually a mistake.
9
9
  #
10
+ # Its auto-correction tweaks layout to keep the syntax. So, this auto-correction
11
+ # is compatible correction for bad case syntax, but if your code makes a mistake
12
+ # with `elsif` and `else`, you will have to correct it manually.
13
+ #
10
14
  # @example
11
15
  #
12
16
  # # bad
@@ -21,13 +25,25 @@ module RuboCop
21
25
  #
22
26
  # # good
23
27
  #
28
+ # # This code is compatible with the bad case. It will be auto-corrected like this.
24
29
  # if something
25
30
  # # ...
26
31
  # else
27
32
  # do_this
28
33
  # do_that
29
34
  # end
35
+ #
36
+ # # This code is incompatible with the bad case.
37
+ # # If `do_this` is a condition, `elsif` should be used instead of `else`.
38
+ # if something
39
+ # # ...
40
+ # elsif do_this
41
+ # do_that
42
+ # end
30
43
  class ElseLayout < Base
44
+ include RangeHelp
45
+ extend AutoCorrector
46
+
31
47
  MSG = 'Odd `else` layout detected. Did you mean to use `elsif`?'
32
48
 
33
49
  def on_if(node)
@@ -58,7 +74,17 @@ module RuboCop
58
74
  return unless first_else
59
75
  return unless first_else.source_range.line == node.loc.else.line
60
76
 
61
- add_offense(first_else)
77
+ add_offense(first_else) do |corrector|
78
+ autocorrect(corrector, node, first_else)
79
+ end
80
+ end
81
+
82
+ def autocorrect(corrector, node, first_else)
83
+ corrector.insert_after(node.loc.else, "\n")
84
+
85
+ blank_range = range_between(node.loc.else.end_pos, first_else.loc.expression.begin_pos)
86
+ indentation = indent(node.else_branch.children[1])
87
+ corrector.replace(blank_range, indentation)
62
88
  end
63
89
  end
64
90
  end
@@ -35,11 +35,24 @@ module RuboCop
35
35
 
36
36
  def on_block(node)
37
37
  return if node.body
38
- return if cop_config['AllowComments'] &&
39
- processed_source.contains_comment?(node.source_range)
38
+ return if cop_config['AllowComments'] && allow_comment?(node)
40
39
 
41
40
  add_offense(node)
42
41
  end
42
+
43
+ private
44
+
45
+ def allow_comment?(node)
46
+ return false unless processed_source.contains_comment?(node.source_range)
47
+
48
+ line_comment = processed_source.comment_at_line(node.source_range.line)
49
+ !line_comment || !comment_disables_cop?(line_comment.loc.expression.source)
50
+ end
51
+
52
+ def comment_disables_cop?(comment)
53
+ regexp_pattern = "# rubocop : (disable|todo) ([^,],)* (all|#{cop_name})"
54
+ Regexp.new(regexp_pattern.gsub(' ', '\s*')).match?(comment)
55
+ end
43
56
  end
44
57
  end
45
58
  end
@@ -76,10 +76,6 @@ module RuboCop
76
76
  conditional_keyword = node.while_post_type? ? 'unless' : 'if'
77
77
  "break #{conditional_keyword} #{node.condition.source}\n#{indent(node)}"
78
78
  end
79
-
80
- def indent(node)
81
- ' ' * node.loc.column
82
- end
83
79
  end
84
80
  end
85
81
  end
@@ -15,6 +15,20 @@ module RuboCop
15
15
  # valid_attributes: %i[name content],
16
16
  # nested_attributes: %i[name content %i[incorrectly nested]]
17
17
  # }
18
+ #
19
+ # # good
20
+ #
21
+ # # Neither is incompatible with the bad case, but probably the intended code.
22
+ # attributes = {
23
+ # valid_attributes: %i[name content],
24
+ # nested_attributes: [:name, :content, %i[incorrectly nested]]
25
+ # }
26
+ #
27
+ # attributes = {
28
+ # valid_attributes: %i[name content],
29
+ # nested_attributes: [:name, :content, [:incorrectly, :nested]]
30
+ # }
31
+ #
18
32
  class NestedPercentLiteral < Base
19
33
  include PercentLiteral
20
34
 
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # Checks for the presence of a `return` inside a `begin..end` block
7
+ # in assignment contexts.
8
+ # In this situation, the `return` will result in an exit from the current
9
+ # method, possibly leading to unexpected behavior.
10
+ #
11
+ # @example
12
+ #
13
+ # # bad
14
+ #
15
+ # @some_variable ||= begin
16
+ # return some_value if some_condition_is_met
17
+ #
18
+ # do_something
19
+ # end
20
+ #
21
+ # @example
22
+ #
23
+ # # good
24
+ #
25
+ # @some_variable ||= begin
26
+ # if some_condition_is_met
27
+ # some_value
28
+ # else
29
+ # do_something
30
+ # end
31
+ # end
32
+ #
33
+ # # good
34
+ #
35
+ # some_variable = if some_condition_is_met
36
+ # return if another_condition_is_met
37
+ #
38
+ # some_value
39
+ # else
40
+ # do_something
41
+ # end
42
+ #
43
+ class NoReturnInBeginEndBlocks < Cop
44
+ MSG = 'Do not `return` in `begin..end` blocks in assignment contexts.'
45
+
46
+ def on_lvasgn(node)
47
+ node.each_node(:kwbegin) do |kwbegin_node|
48
+ kwbegin_node.each_node(:return) do |return_node|
49
+ add_offense(return_node)
50
+ end
51
+ end
52
+ end
53
+ alias on_or_asgn on_lvasgn
54
+ alias on_op_asgn on_lvasgn
55
+ end
56
+ end
57
+ end
58
+ end
@@ -5,6 +5,7 @@ module RuboCop
5
5
  module Lint
6
6
  # This cop checks for setter call to local variable as the final
7
7
  # expression of a function definition.
8
+ # Its auto-correction is marked as unsafe because return value will be changed.
8
9
  #
9
10
  # NOTE: There are edge cases in which the local variable references a
10
11
  # value that is also accessible outside the local scope. This is not
@@ -29,6 +30,8 @@ module RuboCop
29
30
  # x
30
31
  # end
31
32
  class UselessSetterCall < Base
33
+ extend AutoCorrector
34
+
32
35
  MSG = 'Useless setter call to local variable `%<variable>s`.'
33
36
  ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn].freeze
34
37
 
@@ -45,7 +48,9 @@ module RuboCop
45
48
 
46
49
  loc_name = receiver.loc.name
47
50
 
48
- add_offense(loc_name, message: format(MSG, variable: loc_name.source))
51
+ add_offense(loc_name, message: format(MSG, variable: loc_name.source)) do |corrector|
52
+ corrector.insert_after(last_expr, "\n#{indent(last_expr)}#{loc_name.source}")
53
+ end
49
54
  end
50
55
  alias on_defs on_def
51
56
 
@@ -8,9 +8,9 @@ module RuboCop
8
8
  include ConfigurableFormatting
9
9
 
10
10
  FORMATS = {
11
- snake_case: /(?:[[[:lower:]]_]|_\d+)$/,
12
- normalcase: /(?:_\D*|[[[:upper:]][[:lower:]]]\d*)$/,
13
- non_integer: /[[[:upper:]][[:lower:]]_]$/
11
+ snake_case: /(?:\D|_\d+)$/,
12
+ normalcase: /(?:\D|[^_\d]\d+)$/,
13
+ non_integer: /\D$/
14
14
  }.freeze
15
15
  end
16
16
  end
@@ -14,6 +14,8 @@ module RuboCop
14
14
  # # good
15
15
  # def +(other); end
16
16
  class BinaryOperatorParameterName < Base
17
+ extend AutoCorrector
18
+
17
19
  MSG = 'When defining the `%<opr>s` operator, ' \
18
20
  'name its argument `other`.'
19
21
 
@@ -26,7 +28,15 @@ module RuboCop
26
28
 
27
29
  def on_def(node)
28
30
  op_method_candidate?(node) do |name, arg|
29
- add_offense(arg, message: format(MSG, opr: name))
31
+ add_offense(arg, message: format(MSG, opr: name)) do |corrector|
32
+ corrector.replace(arg, 'other')
33
+ node.each_descendant(:lvar, :lvasgn) do |lvar|
34
+ lvar_location = lvar.loc.name
35
+ next unless lvar_location.source == arg.source
36
+
37
+ corrector.replace(lvar_location, 'other')
38
+ end
39
+ end
30
40
  end
31
41
  end
32
42
 
@@ -30,13 +30,19 @@ module RuboCop
30
30
  class HeredocDelimiterCase < Base
31
31
  include Heredoc
32
32
  include ConfigurableEnforcedStyle
33
+ extend AutoCorrector
33
34
 
34
35
  MSG = 'Use %<style>s heredoc delimiters.'
35
36
 
36
37
  def on_heredoc(node)
37
38
  return if correct_case_delimiters?(node)
38
39
 
39
- add_offense(node.loc.heredoc_end)
40
+ add_offense(node.loc.heredoc_end) do |corrector|
41
+ expr = node.loc.expression
42
+
43
+ corrector.replace(expr, correct_delimiters(expr.source))
44
+ corrector.replace(node.loc.heredoc_end, correct_delimiters(delimiter_string(expr)))
45
+ end
40
46
  end
41
47
 
42
48
  private
@@ -46,14 +52,14 @@ module RuboCop
46
52
  end
47
53
 
48
54
  def correct_case_delimiters?(node)
49
- delimiter_string(node) == correct_delimiters(node)
55
+ delimiter_string(node) == correct_delimiters(delimiter_string(node))
50
56
  end
51
57
 
52
- def correct_delimiters(node)
58
+ def correct_delimiters(source)
53
59
  if style == :uppercase
54
- delimiter_string(node).upcase
60
+ source.upcase
55
61
  else
56
- delimiter_string(node).downcase
62
+ source.downcase
57
63
  end
58
64
  end
59
65
  end
@@ -20,6 +20,11 @@ module RuboCop
20
20
  # @something ||= calculate_expensive_thing
21
21
  # end
22
22
  #
23
+ # def foo
24
+ # return @something if defined?(@something)
25
+ # @something = calculate_expensive_thing
26
+ # end
27
+ #
23
28
  # # good
24
29
  # def _foo
25
30
  # @foo ||= calculate_expensive_thing
@@ -54,6 +59,11 @@ module RuboCop
54
59
  # @foo ||= calculate_expensive_thing
55
60
  # end
56
61
  #
62
+ # def foo
63
+ # return @foo if defined?(@foo)
64
+ # @foo = calculate_expensive_thing
65
+ # end
66
+ #
57
67
  # # good
58
68
  # def foo
59
69
  # @_foo ||= calculate_expensive_thing
@@ -64,6 +74,11 @@ module RuboCop
64
74
  # @_foo ||= calculate_expensive_thing
65
75
  # end
66
76
  #
77
+ # def foo
78
+ # return @_foo if defined?(@_foo)
79
+ # @_foo = calculate_expensive_thing
80
+ # end
81
+ #
67
82
  # @example EnforcedStyleForLeadingUnderscores :optional
68
83
  # # bad
69
84
  # def foo
@@ -84,6 +99,12 @@ module RuboCop
84
99
  # def _foo
85
100
  # @_foo ||= calculate_expensive_thing
86
101
  # end
102
+ #
103
+ # # good
104
+ # def foo
105
+ # return @_foo if defined?(@_foo)
106
+ # @_foo = calculate_expensive_thing
107
+ # end
87
108
  class MemoizedInstanceVariableName < Base
88
109
  include ConfigurableEnforcedStyle
89
110
 
@@ -92,32 +113,60 @@ module RuboCop
92
113
  UNDERSCORE_REQUIRED = 'Memoized variable `%<var>s` does not start ' \
93
114
  'with `_`. Use `@%<suggested_var>s` instead.'
94
115
 
95
- def self.node_pattern
96
- memo_assign = '(or_asgn $(ivasgn _) _)'
97
- memoized_at_end_of_method = "(begin ... #{memo_assign})"
98
- instance_method =
99
- "(def $_ _ {#{memo_assign} #{memoized_at_end_of_method}})"
100
- class_method =
101
- "(defs self $_ _ {#{memo_assign} #{memoized_at_end_of_method}})"
102
- "{#{instance_method} #{class_method}}"
103
- end
116
+ # rubocop:disable Metrics/AbcSize
117
+ def on_or_asgn(node)
118
+ lhs, _value = *node
119
+ return unless lhs.ivasgn_type?
120
+ return unless (method_node = node.each_ancestor(:def, :defs).first)
104
121
 
105
- private_class_method :node_pattern
106
- def_node_matcher :memoized?, node_pattern
122
+ body = method_node.body
123
+ return unless body == node || body.children.last == node
107
124
 
108
- def on_def(node)
109
- (method_name, ivar_assign) = memoized?(node)
110
- return if matches?(method_name, ivar_assign)
125
+ method_name = method_node.method_name
126
+ return if matches?(method_name, lhs)
111
127
 
112
128
  msg = format(
113
- message(ivar_assign.children.first.to_s),
114
- var: ivar_assign.children.first.to_s,
129
+ message(lhs.children.first.to_s),
130
+ var: lhs.children.first.to_s,
115
131
  suggested_var: suggested_var(method_name),
116
132
  method: method_name
117
133
  )
118
- add_offense(ivar_assign.source_range, message: msg)
134
+ add_offense(lhs, message: msg)
135
+ end
136
+ # rubocop:enable Metrics/AbcSize
137
+
138
+ def_node_matcher :defined_memoized?, <<~PATTERN
139
+ (begin
140
+ (if (defined $(ivar %1)) (return $(ivar %1)) nil?)
141
+ ...
142
+ $(ivasgn %1 _))
143
+ PATTERN
144
+
145
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
146
+ def on_defined?(node)
147
+ arg = node.arguments.first
148
+ return unless arg.ivar_type?
149
+
150
+ method_node = node.each_ancestor(:def, :defs).first
151
+ return unless method_node
152
+
153
+ var_name = arg.children.first
154
+ method_name = method_node.method_name
155
+ defined_memoized?(method_node.body, var_name) do |defined_ivar, return_ivar, ivar_assign|
156
+ return if matches?(method_name, ivar_assign)
157
+
158
+ msg = format(
159
+ message(var_name.to_s),
160
+ var: var_name.to_s,
161
+ suggested_var: suggested_var(method_name),
162
+ method: method_name
163
+ )
164
+ add_offense(defined_ivar, message: msg)
165
+ add_offense(return_ivar, message: msg)
166
+ add_offense(ivar_assign.loc.name, message: msg)
167
+ end
119
168
  end
120
- alias on_defs on_def
169
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
121
170
 
122
171
  private
123
172
 
@@ -7,42 +7,98 @@ module RuboCop
7
7
  # configured style, snake_case, normalcase, or non_integer,
8
8
  # for their numbering.
9
9
  #
10
+ # Additionally, `CheckMethodNames` and `CheckSymbols` configuration options
11
+ # can be used to specify whether method names and symbols should be checked.
12
+ # Both are enabled by default.
13
+ #
10
14
  # @example EnforcedStyle: snake_case
11
15
  # # bad
12
- #
16
+ # :some_sym1
13
17
  # variable1 = 1
14
18
  #
15
- # # good
19
+ # def some_method1; end
16
20
  #
21
+ # def some_method_1(arg1); end
22
+ #
23
+ # # good
24
+ # :some_sym_1
17
25
  # variable_1 = 1
18
26
  #
27
+ # def some_method_1; end
28
+ #
29
+ # def some_method_1(arg_1); end
30
+ #
19
31
  # @example EnforcedStyle: normalcase (default)
20
32
  # # bad
21
- #
33
+ # :some_sym_1
22
34
  # variable_1 = 1
23
35
  #
24
- # # good
36
+ # def some_method_1; end
25
37
  #
38
+ # def some_method1(arg_1); end
39
+ #
40
+ # # good
41
+ # :some_sym1
26
42
  # variable1 = 1
27
43
  #
44
+ # def some_method1; end
45
+ #
46
+ # def some_method1(arg1); end
47
+ #
28
48
  # @example EnforcedStyle: non_integer
29
49
  # # bad
50
+ # :some_sym1
51
+ # :some_sym_1
30
52
  #
31
53
  # variable1 = 1
32
- #
33
54
  # variable_1 = 1
34
55
  #
56
+ # def some_method1; end
57
+ #
58
+ # def some_method_1; end
59
+ #
60
+ # def some_methodone(arg1); end
61
+ # def some_methodone(arg_1); end
62
+ #
35
63
  # # good
64
+ # :some_symone
65
+ # :some_sym_one
36
66
  #
37
67
  # variableone = 1
38
- #
39
68
  # variable_one = 1
69
+ #
70
+ # def some_methodone; end
71
+ #
72
+ # def some_method_one; end
73
+ #
74
+ # def some_methodone(argone); end
75
+ # def some_methodone(arg_one); end
76
+ #
77
+ # # In the following examples, we assume `EnforcedStyle: normalcase` (default).
78
+ #
79
+ # @example CheckMethodNames: true (default)
80
+ # # bad
81
+ # def some_method_1; end
82
+ #
83
+ # @example CheckMethodNames: false
84
+ # # good
85
+ # def some_method_1; end
86
+ #
87
+ # @example CheckSymbols: true (default)
88
+ # # bad
89
+ # :some_sym_1
90
+ #
91
+ # @example CheckSymbols: false
92
+ # # good
93
+ # :some_sym_1
94
+ #
40
95
  class VariableNumber < Base
41
96
  include ConfigurableNumbering
42
97
 
43
- MSG = 'Use %<style>s for variable numbers.'
98
+ MSG = 'Use %<style>s for %<identifier_type>s numbers.'
44
99
 
45
100
  def on_arg(node)
101
+ @node = node
46
102
  name, = *node
47
103
  check_name(node, name, node.loc.name)
48
104
  end
@@ -50,10 +106,28 @@ module RuboCop
50
106
  alias on_ivasgn on_arg
51
107
  alias on_cvasgn on_arg
52
108
 
109
+ def on_def(node)
110
+ @node = node
111
+ check_name(node, node.method_name, node.loc.name) if cop_config['CheckMethodNames']
112
+ end
113
+ alias on_defs on_def
114
+
115
+ def on_sym(node)
116
+ @node = node
117
+ check_name(node, node.value, node) if cop_config['CheckSymbols']
118
+ end
119
+
53
120
  private
54
121
 
55
122
  def message(style)
56
- format(MSG, style: style)
123
+ identifier_type =
124
+ case @node.type
125
+ when :def, :defs then 'method name'
126
+ when :sym then 'symbol'
127
+ else 'variable'
128
+ end
129
+
130
+ format(MSG, style: style, identifier_type: identifier_type)
57
131
  end
58
132
  end
59
133
  end