rubocop 0.77.0 → 0.78.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +37 -25
  4. data/lib/rubocop.rb +5 -2
  5. data/lib/rubocop/cli/command/auto_genenerate_config.rb +7 -7
  6. data/lib/rubocop/config.rb +1 -1
  7. data/lib/rubocop/config_obsoletion.rb +3 -3
  8. data/lib/rubocop/config_validator.rb +6 -4
  9. data/lib/rubocop/cop/autocorrect_logic.rb +1 -1
  10. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
  11. data/lib/rubocop/cop/cop.rb +3 -1
  12. data/lib/rubocop/cop/layout/first_argument_indentation.rb +2 -2
  13. data/lib/rubocop/cop/layout/heredoc_indentation.rb +4 -4
  14. data/lib/rubocop/cop/{metrics → layout}/line_length.rb +5 -78
  15. data/lib/rubocop/cop/layout/space_around_operators.rb +31 -6
  16. data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
  17. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  18. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
  19. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +2 -2
  20. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +4 -4
  21. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  22. data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
  23. data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
  24. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
  25. data/lib/rubocop/cop/mixin/trailing_comma.rb +6 -3
  26. data/lib/rubocop/cop/offense.rb +11 -0
  27. data/lib/rubocop/cop/style/attr.rb +8 -0
  28. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  29. data/lib/rubocop/cop/style/guard_clause.rb +3 -2
  30. data/lib/rubocop/cop/style/if_unless_modifier.rb +38 -3
  31. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  32. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  33. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
  34. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  35. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  36. data/lib/rubocop/formatter/json_formatter.rb +6 -5
  37. data/lib/rubocop/node_pattern.rb +1 -1
  38. data/lib/rubocop/options.rb +8 -8
  39. data/lib/rubocop/result_cache.rb +2 -0
  40. data/lib/rubocop/runner.rb +5 -1
  41. data/lib/rubocop/version.rb +1 -1
  42. metadata +6 -3
@@ -3,24 +3,37 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Layout
6
- # Checks that operators have space around them, except for **
7
- # which should not have surrounding space.
6
+ # Checks that operators have space around them, except for ** which
7
+ # should or shouldn't have surrounding space depending on configuration.
8
8
  #
9
9
  # @example
10
10
  # # bad
11
11
  # total = 3*4
12
12
  # "apple"+"juice"
13
13
  # my_number = 38/4
14
- # a ** b
15
14
  #
16
15
  # # good
17
16
  # total = 3 * 4
18
17
  # "apple" + "juice"
19
18
  # my_number = 38 / 4
19
+ #
20
+ # @example EnforcedStyleForExponentOperator: no_space (default)
21
+ # # bad
22
+ # a ** b
23
+ #
24
+ # # good
25
+ # a**b
26
+ #
27
+ # @example EnforcedStyleForExponentOperator: space
28
+ # # bad
20
29
  # a**b
30
+ #
31
+ # # good
32
+ # a ** b
21
33
  class SpaceAroundOperators < Cop
22
34
  include PrecedingFollowingAlignment
23
35
  include RangeHelp
36
+ include RationalLiteral
24
37
 
25
38
  IRREGULAR_METHODS = %i[[] ! []=].freeze
26
39
  EXCESSIVE_SPACE = ' '
@@ -53,6 +66,8 @@ module RuboCop
53
66
  end
54
67
 
55
68
  def on_send(node)
69
+ return if rational_literal?(node)
70
+
56
71
  if node.setter_method?
57
72
  on_special_asgn(node)
58
73
  elsif regular_operator?(node)
@@ -101,7 +116,7 @@ module RuboCop
101
116
 
102
117
  def autocorrect(range)
103
118
  lambda do |corrector|
104
- if range.source =~ /\*\*/
119
+ if range.source =~ /\*\*/ && !space_around_exponent_operator?
105
120
  corrector.replace(range, '**')
106
121
  elsif range.source.end_with?("\n")
107
122
  corrector.replace(range, " #{range.source.strip}\n")
@@ -138,8 +153,10 @@ module RuboCop
138
153
  end
139
154
 
140
155
  def offense_message(type, operator, with_space, right_operand)
141
- if operator.is?('**')
142
- 'Space around operator `**` detected.' unless with_space.is?('**')
156
+ if should_not_have_surrounding_space?(operator)
157
+ return if with_space.is?(operator.source)
158
+
159
+ "Space around operator `#{operator.source}` detected."
143
160
  elsif with_space.source !~ /^\s.*\s$/
144
161
  "Surrounding space missing for operator `#{operator.source}`."
145
162
  elsif excess_leading_space?(type, operator, with_space) ||
@@ -176,6 +193,14 @@ module RuboCop
176
193
  align_hash_cop_config &&
177
194
  align_hash_cop_config['EnforcedHashRocketStyle'] == 'table'
178
195
  end
196
+
197
+ def space_around_exponent_operator?
198
+ cop_config['EnforcedStyleForExponentOperator'] == 'space'
199
+ end
200
+
201
+ def should_not_have_surrounding_space?(operator)
202
+ operator.is?('**') ? !space_around_exponent_operator? : false
203
+ end
179
204
  end
180
205
  end
181
206
  end
@@ -41,6 +41,14 @@ module RuboCop
41
41
  def on_block(node)
42
42
  return if node.keywords?
43
43
 
44
+ # Do not register an offense for multi-line braces when specifying
45
+ # `EnforcedStyle: no_space`. It will conflict with auto-correction
46
+ # by `EnforcedStyle: line_count_based` of `Style/BlockDelimiters` cop.
47
+ # That means preventing auto-correction to incorrect auto-corrected
48
+ # code.
49
+ # See: https://github.com/rubocop-hq/rubocop/issues/7534
50
+ return if conflict_with_block_delimiters?
51
+
44
52
  left_brace = node.loc.begin
45
53
  space_plus_brace = range_with_surrounding_space(range: left_brace)
46
54
  used_style =
@@ -110,6 +118,15 @@ module RuboCop
110
118
  end
111
119
  end
112
120
 
121
+ def conflict_with_block_delimiters?
122
+ block_delimiters_style == 'line_count_based' &&
123
+ style == :no_space && node.multiline?
124
+ end
125
+
126
+ def block_delimiters_style
127
+ config.for_cop('Style/BlockDelimiters')['EnforcedStyle']
128
+ end
129
+
113
130
  def empty_braces?(loc)
114
131
  loc.begin.end_pos == loc.end.begin_pos
115
132
  end
@@ -22,7 +22,7 @@ module RuboCop
22
22
  # num = 0
23
23
  # sum = numbers.each_with_object(num) { |e, a| a += e }
24
24
  class EachWithObjectArgument < Cop
25
- MSG = 'The argument to each_with_object can not be immutable.'
25
+ MSG = 'The argument to each_with_object cannot be immutable.'
26
26
 
27
27
  def_node_matcher :each_with_object?, <<~PATTERN
28
28
  ({send csend} _ :each_with_object $_)
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # `Dir[...]` and `Dir.glob(...)` do not make any guarantees about
7
+ # the order in which files are returned. The final order is
8
+ # determined by the operating system and file system.
9
+ # This means that using them in cases where the order matters,
10
+ # such as requiring files, can lead to intermittent failures
11
+ # that are hard to debug. To ensure this doesn't happen,
12
+ # always sort the list.
13
+ #
14
+ # @example
15
+ #
16
+ # # bad
17
+ # Dir["./lib/**/*.rb"].each do |file|
18
+ # require file
19
+ # end
20
+ #
21
+ # # good
22
+ # Dir["./lib/**/*.rb"].sort.each do |file|
23
+ # require file
24
+ # end
25
+ #
26
+ # @example
27
+ #
28
+ # # bad
29
+ # Dir.glob(Rails.root.join(__dir__, 'test', '*.rb')) do |file|
30
+ # require file
31
+ # end
32
+ #
33
+ # # good
34
+ # Dir.glob(Rails.root.join(__dir__, 'test', '*.rb')).sort.each do |file|
35
+ # require file
36
+ # end
37
+ #
38
+ class NonDeterministicRequireOrder < Cop
39
+ MSG = 'Sort files before requiring them.'
40
+
41
+ def on_block(node)
42
+ return unless node.body
43
+ return unless unsorted_dir_loop?(node.send_node)
44
+
45
+ loop_variable(node.arguments) do |var_name|
46
+ return unless var_is_required?(node.body, var_name)
47
+
48
+ add_offense(node.send_node)
49
+ end
50
+ end
51
+
52
+ def autocorrect(node)
53
+ if unsorted_dir_block?(node)
54
+ lambda do |corrector|
55
+ corrector.replace(node.loc.expression, "#{node.source}.sort.each")
56
+ end
57
+ else
58
+ lambda do |corrector|
59
+ source = node.receiver.source
60
+ corrector.replace(node.loc.expression, "#{source}.sort.each")
61
+ end
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+ def unsorted_dir_loop?(node)
68
+ unsorted_dir_block?(node) || unsorted_dir_each?(node)
69
+ end
70
+
71
+ def_node_matcher :unsorted_dir_block?, <<~PATTERN
72
+ (send (const nil? :Dir) :glob ...)
73
+ PATTERN
74
+
75
+ def_node_matcher :unsorted_dir_each?, <<~PATTERN
76
+ (send (send (const nil? :Dir) {:[] :glob} ...) :each)
77
+ PATTERN
78
+
79
+ def_node_matcher :loop_variable, <<~PATTERN
80
+ (args (arg $_))
81
+ PATTERN
82
+
83
+ def_node_search :var_is_required?, <<~PATTERN
84
+ (send nil? :require (lvar %1))
85
+ PATTERN
86
+ end
87
+ end
88
+ end
89
+ end
@@ -19,9 +19,9 @@ module RuboCop
19
19
  #
20
20
  # @example
21
21
  # # bad
22
- # # rubocop:disable Metrics/LineLength
22
+ # # rubocop:disable Layout/LineLength
23
23
  # x += 1
24
- # # rubocop:enable Metrics/LineLength
24
+ # # rubocop:enable Layout/LineLength
25
25
  #
26
26
  # # good
27
27
  # x += 1
@@ -15,20 +15,20 @@ module RuboCop
15
15
  # @example
16
16
  # # bad
17
17
  # foo = 1
18
- # # rubocop:enable Metrics/LineLength
18
+ # # rubocop:enable Layout/LineLength
19
19
  #
20
20
  # # good
21
21
  # foo = 1
22
22
  # @example
23
23
  # # bad
24
- # # rubocop:disable Metrics/LineLength
24
+ # # rubocop:disable Layout/LineLength
25
25
  # baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaarrrrrrrrrrrrr
26
- # # rubocop:enable Metrics/LineLength
26
+ # # rubocop:enable Layout/LineLength
27
27
  # baz
28
28
  # # rubocop:enable all
29
29
  #
30
30
  # # good
31
- # # rubocop:disable Metrics/LineLength
31
+ # # rubocop:disable Layout/LineLength
32
32
  # baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaarrrrrrrrrrrrr
33
33
  # # rubocop:enable all
34
34
  # baz
@@ -35,7 +35,7 @@ module RuboCop
35
35
  # If this offense is within a line range that is already being
36
36
  # realigned by autocorrect, we report the offense without
37
37
  # autocorrecting it. Two rewrites in the same area by the same
38
- # cop can not be handled. The next iteration will find the
38
+ # cop cannot be handled. The next iteration will find the
39
39
  # offense again and correct it.
40
40
  add_offense(nil, location: expr)
41
41
  else
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Help methods for determining if a line is too long.
6
+ module LineLengthHelp
7
+ private
8
+
9
+ def ignore_cop_directives?
10
+ config.for_cop('Layout/LineLength')['IgnoreCopDirectives']
11
+ end
12
+
13
+ def directive_on_source_line?(line_index)
14
+ source_line_number = line_index + processed_source.buffer.first_line
15
+ comment =
16
+ processed_source.comments
17
+ .detect { |e| e.location.line == source_line_number }
18
+
19
+ return false unless comment
20
+
21
+ comment.text.match(CommentConfig::COMMENT_DIRECTIVE_REGEXP)
22
+ end
23
+
24
+ def allow_uri?
25
+ config.for_cop('Layout/LineLength')['AllowURI']
26
+ end
27
+
28
+ def allowed_uri_position?(line, uri_range)
29
+ uri_range.begin < max_line_length &&
30
+ (uri_range.end == line_length(line) ||
31
+ uri_range.end == line_length(line) - 1)
32
+ end
33
+
34
+ def line_length(line)
35
+ line.length + indentation_difference(line)
36
+ end
37
+
38
+ def find_excessive_uri_range(line)
39
+ last_uri_match = match_uris(line).last
40
+ return nil unless last_uri_match
41
+
42
+ begin_position, end_position = last_uri_match.offset(0).map do |pos|
43
+ pos + indentation_difference(line)
44
+ end
45
+ return nil if begin_position < max_line_length &&
46
+ end_position < max_line_length
47
+
48
+ begin_position...end_position
49
+ end
50
+
51
+ def match_uris(string)
52
+ matches = []
53
+ string.scan(uri_regexp) do
54
+ matches << $LAST_MATCH_INFO if valid_uri?($LAST_MATCH_INFO[0])
55
+ end
56
+ matches
57
+ end
58
+
59
+ def indentation_difference(line)
60
+ return 0 unless tab_indentation_width
61
+
62
+ line.match(/^\t*/)[0].size * (tab_indentation_width - 1)
63
+ end
64
+
65
+ def tab_indentation_width
66
+ config.for_cop('Layout/Tab')['IndentationWidth']
67
+ end
68
+
69
+ def uri_regexp
70
+ @uri_regexp ||=
71
+ URI::DEFAULT_PARSER
72
+ .make_regexp(config.for_cop('Layout/LineLength')['URISchemes'])
73
+ end
74
+
75
+ def valid_uri?(uri_ish_string)
76
+ URI.parse(uri_ish_string)
77
+ true
78
+ rescue URI::InvalidURIError, NoMethodError
79
+ false
80
+ end
81
+
82
+ def line_length_without_directive(line)
83
+ before_comment, = line.split(CommentConfig::COMMENT_DIRECTIVE_REGEXP)
84
+ before_comment.rstrip.length
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Common functionality for handling Rational literals.
6
+ module RationalLiteral
7
+ extend NodePattern::Macros
8
+
9
+ private
10
+
11
+ def_node_matcher :rational_literal?, <<~PATTERN
12
+ (send
13
+ (int _) :/
14
+ (rational _))
15
+ PATTERN
16
+ end
17
+ end
18
+ end
@@ -52,9 +52,9 @@ module RuboCop
52
52
  end
53
53
 
54
54
  def max_line_length
55
- return unless config.for_cop('Metrics/LineLength')['Enabled']
55
+ return unless config.for_cop('Layout/LineLength')['Enabled']
56
56
 
57
- config.for_cop('Metrics/LineLength')['Max']
57
+ config.for_cop('Layout/LineLength')['Max']
58
58
  end
59
59
 
60
60
  def indentation_multiplier
@@ -93,9 +93,12 @@ module RuboCop
93
93
  end
94
94
 
95
95
  def method_name_and_arguments_on_same_line?(node)
96
- %i[send csend].include?(node.type) &&
97
- node.loc.selector.line == node.arguments.last.last_line &&
98
- node.last_line == node.arguments.last.last_line
96
+ return false unless node.call_type?
97
+
98
+ line = node.loc.selector.nil? ? node.loc.line : node.loc.selector.line
99
+
100
+ line == node.last_argument.last_line &&
101
+ node.last_line == node.last_argument.last_line
99
102
  end
100
103
 
101
104
  # A single argument with the closing bracket on the same line as the end
@@ -65,6 +65,17 @@ module RuboCop
65
65
  freeze
66
66
  end
67
67
 
68
+ # @api public
69
+ #
70
+ # @!attribute [r] correctable?
71
+ #
72
+ # @return [Boolean]
73
+ # whether this offense can be automatically corrected via
74
+ # autocorrect or a todo.
75
+ def correctable?
76
+ @status != :unsupported
77
+ end
78
+
68
79
  # @api public
69
80
  #
70
81
  # @!attribute [r] corrected?
@@ -21,6 +21,10 @@ module RuboCop
21
21
 
22
22
  def on_send(node)
23
23
  return unless node.command?(:attr) && node.arguments?
24
+ # check only for method definitions in class/module body
25
+ return if node.parent &&
26
+ !node.parent.class_type? &&
27
+ !class_eval?(node.parent)
24
28
 
25
29
  add_offense(node, location: :selector)
26
30
  end
@@ -56,6 +60,10 @@ module RuboCop
56
60
  'attr_reader'
57
61
  end
58
62
  end
63
+
64
+ def_node_matcher :class_eval?, <<~PATTERN
65
+ (block (send _ {:class_eval :module_eval}) ...)
66
+ PATTERN
59
67
  end
60
68
  end
61
69
  end
@@ -212,7 +212,7 @@ module RuboCop
212
212
  %i[casgn cvasgn gvasgn ivasgn lvasgn].freeze
213
213
  ASSIGNMENT_TYPES = VARIABLE_ASSIGNMENT_TYPES +
214
214
  %i[and_asgn or_asgn op_asgn masgn].freeze
215
- LINE_LENGTH = 'Metrics/LineLength'
215
+ LINE_LENGTH = 'Layout/LineLength'
216
216
  INDENTATION_WIDTH = 'Layout/IndentationWidth'
217
217
  ENABLED = 'Enabled'
218
218
  MAX = 'Max'
@@ -376,7 +376,7 @@ module RuboCop
376
376
  assignment_types_match?(*statements)
377
377
  end
378
378
 
379
- # If `Metrics/LineLength` is enabled, we do not want to introduce an
379
+ # If `Layout/LineLength` is enabled, we do not want to introduce an
380
380
  # offense by auto-correcting this cop. Find the max configured line
381
381
  # length. Find the longest line of condition. Remove the assignment
382
382
  # from lines that contain the offending assignment because after