rubocop 1.18.3 → 1.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +46 -7
  4. data/lib/rubocop/cli.rb +18 -0
  5. data/lib/rubocop/config_loader.rb +2 -2
  6. data/lib/rubocop/config_loader_resolver.rb +21 -6
  7. data/lib/rubocop/config_validator.rb +18 -5
  8. data/lib/rubocop/cop/bundler/gem_filename.rb +103 -0
  9. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
  10. data/lib/rubocop/cop/correctors/require_library_corrector.rb +23 -0
  11. data/lib/rubocop/cop/documentation.rb +1 -1
  12. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  13. data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +34 -0
  14. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +71 -0
  15. data/lib/rubocop/cop/internal_affairs.rb +2 -0
  16. data/lib/rubocop/cop/layout/class_structure.rb +5 -1
  17. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -0
  18. data/lib/rubocop/cop/layout/end_alignment.rb +10 -2
  19. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  20. data/lib/rubocop/cop/layout/hash_alignment.rb +22 -18
  21. data/lib/rubocop/cop/layout/heredoc_indentation.rb +0 -7
  22. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  23. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  24. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +33 -14
  25. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +3 -0
  26. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +22 -9
  27. data/lib/rubocop/cop/layout/space_around_operators.rb +8 -1
  28. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
  29. data/lib/rubocop/cop/layout/space_inside_parens.rb +5 -5
  30. data/lib/rubocop/cop/layout/trailing_whitespace.rb +24 -1
  31. data/lib/rubocop/cop/lint/ambiguous_range.rb +105 -0
  32. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -2
  33. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  34. data/lib/rubocop/cop/lint/duplicate_branch.rb +2 -1
  35. data/lib/rubocop/cop/lint/duplicate_methods.rb +8 -5
  36. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
  37. data/lib/rubocop/cop/mixin/annotation_comment.rb +57 -34
  38. data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
  39. data/lib/rubocop/cop/mixin/documentation_comment.rb +5 -2
  40. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +14 -1
  41. data/lib/rubocop/cop/mixin/hash_transform_method.rb +6 -1
  42. data/lib/rubocop/cop/mixin/heredoc.rb +7 -0
  43. data/lib/rubocop/cop/mixin/percent_array.rb +13 -7
  44. data/lib/rubocop/cop/mixin/require_library.rb +59 -0
  45. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  46. data/lib/rubocop/cop/naming/inclusive_language.rb +18 -1
  47. data/lib/rubocop/cop/style/block_delimiters.rb +39 -6
  48. data/lib/rubocop/cop/style/comment_annotation.rb +25 -39
  49. data/lib/rubocop/cop/style/commented_keyword.rb +2 -1
  50. data/lib/rubocop/cop/style/conditional_assignment.rb +19 -5
  51. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -7
  52. data/lib/rubocop/cop/style/double_negation.rb +12 -1
  53. data/lib/rubocop/cop/style/encoding.rb +26 -15
  54. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  55. data/lib/rubocop/cop/style/explicit_block_argument.rb +32 -7
  56. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  57. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +11 -0
  58. data/lib/rubocop/cop/style/hash_except.rb +4 -3
  59. data/lib/rubocop/cop/style/hash_transform_keys.rb +0 -3
  60. data/lib/rubocop/cop/style/identical_conditional_branches.rb +30 -5
  61. data/lib/rubocop/cop/style/method_def_parentheses.rb +10 -1
  62. data/lib/rubocop/cop/style/missing_else.rb +7 -0
  63. data/lib/rubocop/cop/style/mutable_constant.rb +73 -13
  64. data/lib/rubocop/cop/style/redundant_begin.rb +25 -0
  65. data/lib/rubocop/cop/style/redundant_freeze.rb +4 -3
  66. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +83 -0
  67. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  68. data/lib/rubocop/cop/style/semicolon.rb +32 -24
  69. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  70. data/lib/rubocop/cop/style/single_line_methods.rb +14 -9
  71. data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -0
  72. data/lib/rubocop/cop/style/special_global_vars.rb +21 -0
  73. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -0
  74. data/lib/rubocop/cop/style/symbol_array.rb +3 -3
  75. data/lib/rubocop/cop/style/word_array.rb +23 -5
  76. data/lib/rubocop/cop/util.rb +7 -2
  77. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -1
  78. data/lib/rubocop/magic_comment.rb +44 -15
  79. data/lib/rubocop/options.rb +1 -1
  80. data/lib/rubocop/version.rb +1 -1
  81. data/lib/rubocop.rb +6 -1
  82. metadata +12 -5
@@ -6,6 +6,9 @@ module RuboCop
6
6
  # This cop ensures that each argument in a multi-line method call
7
7
  # starts on a separate line.
8
8
  #
9
+ # NOTE: this cop does not move the first argument, if you want that to
10
+ # be on a separate line, see `Layout/FirstMethodArgumentLineBreak`.
11
+ #
9
12
  # @example
10
13
  #
11
14
  # # bad
@@ -29,8 +29,7 @@ module RuboCop
29
29
  MSG = '`%<kw_loc>s` at %<kw_loc_line>d, %<kw_loc_column>d is not ' \
30
30
  'aligned with `%<beginning>s` at ' \
31
31
  '%<begin_loc_line>d, %<begin_loc_column>d.'
32
- ANCESTOR_TYPES = %i[kwbegin def defs class module].freeze
33
- RUBY_2_5_ANCESTOR_TYPES = (ANCESTOR_TYPES + %i[block]).freeze
32
+ ANCESTOR_TYPES = %i[kwbegin def defs class module block].freeze
34
33
  ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS = %i[def defs].freeze
35
34
  ALTERNATIVE_ACCESS_MODIFIERS = %i[public_class_method private_class_method].freeze
36
35
 
@@ -118,6 +117,8 @@ module RuboCop
118
117
  ancestor_node = ancestor_node(node)
119
118
 
120
119
  return ancestor_node if ancestor_node.nil? || ancestor_node.kwbegin_type?
120
+ return if ancestor_node.respond_to?(:send_node) &&
121
+ aligned_with_line_break_method?(ancestor_node, node)
121
122
 
122
123
  assignment_node = assignment_node(ancestor_node)
123
124
  return assignment_node if same_line?(ancestor_node, assignment_node)
@@ -129,14 +130,26 @@ module RuboCop
129
130
  end
130
131
 
131
132
  def ancestor_node(node)
132
- ancestor_types =
133
- if target_ruby_version >= 2.5
134
- RUBY_2_5_ANCESTOR_TYPES
135
- else
136
- ANCESTOR_TYPES
137
- end
133
+ node.each_ancestor(*ANCESTOR_TYPES).first
134
+ end
135
+
136
+ def aligned_with_line_break_method?(ancestor_node, node)
137
+ send_node_loc = ancestor_node.send_node.loc
138
+ do_keyword_line = ancestor_node.loc.begin.line
139
+ rescue_keyword_column = node.loc.keyword.column
140
+ selector = send_node_loc.respond_to?(:selector) ? send_node_loc.selector : send_node_loc
141
+
142
+ if aligned_with_leading_dot?(do_keyword_line, send_node_loc, rescue_keyword_column)
143
+ return true
144
+ end
145
+
146
+ do_keyword_line == selector.line && rescue_keyword_column == selector.column
147
+ end
148
+
149
+ def aligned_with_leading_dot?(do_keyword_line, send_node_loc, rescue_keyword_column)
150
+ return false unless send_node_loc.respond_to?(:dot) && (dot = send_node_loc.dot)
138
151
 
139
- node.each_ancestor(*ancestor_types).first
152
+ do_keyword_line == dot.line && rescue_keyword_column == dot.column
140
153
  end
141
154
 
142
155
  def assignment_node(node)
@@ -108,6 +108,14 @@ module RuboCop
108
108
  check_operator(:assignment, node.loc.operator, rhs.source_range)
109
109
  end
110
110
 
111
+ def on_casgn(node)
112
+ _, _, right, = *node
113
+
114
+ return unless right
115
+
116
+ check_operator(:assignment, node.loc.operator, right.source_range)
117
+ end
118
+
111
119
  def on_binary(node)
112
120
  _, rhs, = *node
113
121
 
@@ -134,7 +142,6 @@ module RuboCop
134
142
  alias on_and on_binary
135
143
  alias on_lvasgn on_assignment
136
144
  alias on_masgn on_assignment
137
- alias on_casgn on_special_asgn
138
145
  alias on_ivasgn on_assignment
139
146
  alias on_cvasgn on_assignment
140
147
  alias on_gvasgn on_assignment
@@ -18,7 +18,7 @@ module RuboCop
18
18
  MSG = 'Put a space before an end-of-line comment.'
19
19
 
20
20
  def on_new_investigation
21
- processed_source.tokens.each_cons(2) do |token1, token2|
21
+ processed_source.sorted_tokens.each_cons(2) do |token1, token2|
22
22
  next unless token2.comment?
23
23
  next unless token1.line == token2.line
24
24
  next unless token1.pos.end == token2.pos.begin
@@ -43,12 +43,12 @@ module RuboCop
43
43
  MSG_SPACE = 'No space inside parentheses detected.'
44
44
 
45
45
  def on_new_investigation
46
- @processed_source = processed_source
46
+ tokens = processed_source.sorted_tokens
47
47
 
48
48
  if style == :space
49
- process_with_space_style(processed_source)
49
+ process_with_space_style(tokens)
50
50
  else
51
- each_extraneous_space(processed_source.tokens) do |range|
51
+ each_extraneous_space(tokens) do |range|
52
52
  add_offense(range) do |corrector|
53
53
  corrector.remove(range)
54
54
  end
@@ -58,8 +58,8 @@ module RuboCop
58
58
 
59
59
  private
60
60
 
61
- def process_with_space_style(processed_source)
62
- processed_source.tokens.each_cons(2) do |token1, token2|
61
+ def process_with_space_style(tokens)
62
+ tokens.each_cons(2) do |token1, token2|
63
63
  each_extraneous_space_in_empty_parens(token1, token2) do |range|
64
64
  add_offense(range) do |corrector|
65
65
  corrector.remove(range)
@@ -41,6 +41,7 @@ module RuboCop
41
41
  #
42
42
  class TrailingWhitespace < Base
43
43
  include RangeHelp
44
+ include Heredoc
44
45
  extend AutoCorrector
45
46
 
46
47
  MSG = 'Trailing whitespace detected.'
@@ -54,6 +55,8 @@ module RuboCop
54
55
  end
55
56
  end
56
57
 
58
+ def on_heredoc(_node); end
59
+
57
60
  private
58
61
 
59
62
  def process_line(line, lineno)
@@ -63,13 +66,33 @@ module RuboCop
63
66
  range = offense_range(lineno, line)
64
67
  add_offense(range) do |corrector|
65
68
  if heredoc
66
- corrector.wrap(range, "\#{'", "'}") unless static?(heredoc)
69
+ process_line_in_heredoc(corrector, range, heredoc)
67
70
  else
68
71
  corrector.remove(range)
69
72
  end
70
73
  end
71
74
  end
72
75
 
76
+ def process_line_in_heredoc(corrector, range, heredoc)
77
+ indent_level = indent_level(find_heredoc(range.line).loc.heredoc_body.source)
78
+ whitespace_only = whitespace_only?(range)
79
+ if whitespace_only && whitespace_is_indentation?(range, indent_level)
80
+ corrector.remove(range)
81
+ elsif !static?(heredoc)
82
+ range = range_between(range.begin_pos + indent_level, range.end_pos) if whitespace_only
83
+ corrector.wrap(range, "\#{'", "'}")
84
+ end
85
+ end
86
+
87
+ def whitespace_is_indentation?(range, level)
88
+ range.source[/ +/].length <= level
89
+ end
90
+
91
+ def whitespace_only?(range)
92
+ source = range_with_surrounding_space(range: range).source
93
+ source.start_with?("\n") && source.end_with?("\n")
94
+ end
95
+
73
96
  def static?(heredoc)
74
97
  heredoc.loc.expression.source.end_with? "'"
75
98
  end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # This cop checks for ambiguous ranges.
7
+ #
8
+ # Ranges have quite low precedence, which leads to unexpected behaviour when
9
+ # using a range with other operators. This cop avoids that by making ranges
10
+ # explicit by requiring parenthesis around complex range boundaries (anything
11
+ # that is not a basic literal: numerics, strings, symbols, etc.).
12
+ #
13
+ # NOTE: The cop auto-corrects by wrapping the entire boundary in parentheses, which
14
+ # makes the outcome more explicit but is possible to not be the intention of the
15
+ # programmer. For this reason, this cop's auto-correct is marked as unsafe (it
16
+ # will not change the behaviour of the code, but will not necessarily match the
17
+ # intent of the program).
18
+ #
19
+ # This cop can be configured with `RequireParenthesesForMethodChains` in order to
20
+ # specify whether method chains (including `self.foo`) should be wrapped in parens
21
+ # by this cop.
22
+ #
23
+ # NOTE: Regardless of this configuration, if a method receiver is a basic literal
24
+ # value, it will be wrapped in order to prevent the ambiguity of `1..2.to_a`.
25
+ #
26
+ # @example
27
+ # # bad
28
+ # x || 1..2
29
+ # (x || 1..2)
30
+ # 1..2.to_a
31
+ #
32
+ # # good, unambiguous
33
+ # 1..2
34
+ # 'a'..'z'
35
+ # :bar..:baz
36
+ # MyClass::MIN..MyClass::MAX
37
+ # @min..@max
38
+ # a..b
39
+ # -a..b
40
+ #
41
+ # # good, ambiguity removed
42
+ # x || (1..2)
43
+ # (x || 1)..2
44
+ # (x || 1)..(y || 2)
45
+ # (1..2).to_a
46
+ #
47
+ # @example RequireParenthesesForMethodChains: false (default)
48
+ # # good
49
+ # a.foo..b.bar
50
+ # (a.foo)..(b.bar)
51
+ #
52
+ # @example RequireParenthesesForMethodChains: true
53
+ # # bad
54
+ # a.foo..b.bar
55
+ #
56
+ # # good
57
+ # (a.foo)..(b.bar)
58
+ #
59
+ class AmbiguousRange < Base
60
+ extend AutoCorrector
61
+
62
+ MSG = 'Wrap complex range boundaries with parentheses to avoid ambiguity.'
63
+
64
+ def on_irange(node)
65
+ each_boundary(node) do |boundary|
66
+ next if acceptable?(boundary)
67
+
68
+ add_offense(boundary) do |corrector|
69
+ corrector.wrap(boundary, '(', ')')
70
+ end
71
+ end
72
+ end
73
+ alias on_erange on_irange
74
+
75
+ private
76
+
77
+ def each_boundary(range)
78
+ yield range.begin if range.begin
79
+ yield range.end if range.end
80
+ end
81
+
82
+ def acceptable?(node)
83
+ node.begin_type? ||
84
+ node.basic_literal? ||
85
+ node.variable? || node.const_type? ||
86
+ node.call_type? && acceptable_call?(node)
87
+ end
88
+
89
+ def acceptable_call?(node)
90
+ return true if node.unary_operation?
91
+
92
+ # Require parentheses when making a method call on a literal
93
+ # to avoid the ambiguity of `1..2.to_a`.
94
+ return false if node.receiver&.basic_literal?
95
+
96
+ require_parentheses_for_method_chain? || node.receiver.nil?
97
+ end
98
+
99
+ def require_parentheses_for_method_chain?
100
+ !cop_config['RequireParenthesesForMethodChains']
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -46,12 +46,11 @@ module RuboCop
46
46
  node = processed_source.ast.each_node(:regexp).find do |regexp_node|
47
47
  regexp_node.source_range.begin_pos == diagnostic.location.begin_pos
48
48
  end
49
-
50
49
  find_offense_node(node.parent, node)
51
50
  end
52
51
 
53
52
  def find_offense_node(node, regexp_receiver)
54
- return node unless node.parent
53
+ return node if first_argument_is_regexp?(node) || !node.parent
55
54
 
56
55
  if (node.parent.send_type? && node.receiver) ||
57
56
  method_chain_to_regexp_receiver?(node, regexp_receiver)
@@ -61,6 +60,10 @@ module RuboCop
61
60
  node
62
61
  end
63
62
 
63
+ def first_argument_is_regexp?(node)
64
+ node.send_type? && node.first_argument&.regexp_type?
65
+ end
66
+
64
67
  def method_chain_to_regexp_receiver?(node, regexp_receiver)
65
68
  return false unless (parent = node.parent)
66
69
  return false unless (parent_receiver = parent.receiver)
@@ -7,8 +7,8 @@ module RuboCop
7
7
  # not be kept for production code.
8
8
  #
9
9
  # The cop can be configured using `DebuggerMethods`. By default, a number of gems
10
- # debug entrypoints are configured (`Kernel`, `Byebug`, `Capybara`, `Pry`, `Rails`,
11
- # and `WebConsole`). Additional methods can be added.
10
+ # debug entrypoints are configured (`Kernel`, `Byebug`, `Capybara`, `debug.rb`,
11
+ # `Pry`, `Rails`, `RubyJard`, and `WebConsole`). Additional methods can be added.
12
12
  #
13
13
  # Specific default groups can be disabled if necessary:
14
14
  #
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module Lint
6
6
  # This cop checks that there are no repeated bodies
7
- # within `if/unless`, `case-when` and `rescue` constructs.
7
+ # within `if/unless`, `case-when`, `case-in` and `rescue` constructs.
8
8
  #
9
9
  # With `IgnoreLiteralBranches: true`, branches are not registered
10
10
  # as offenses if they return a basic literal value (string, symbol,
@@ -97,6 +97,7 @@ module RuboCop
97
97
  end
98
98
  alias on_if on_branching_statement
99
99
  alias on_case on_branching_statement
100
+ alias on_case_match on_branching_statement
100
101
  alias on_rescue on_branching_statement
101
102
 
102
103
  private
@@ -135,11 +135,14 @@ module RuboCop
135
135
  def found_instance_method(node, name)
136
136
  return unless (scope = node.parent_module_name)
137
137
 
138
- if scope =~ /\A#<Class:(.*)>\Z/
139
- found_method(node, "#{Regexp.last_match(1)}.#{name}")
140
- else
141
- found_method(node, "#{scope}##{name}")
142
- end
138
+ # Humanize the scope
139
+ scope = scope.sub(
140
+ /(?:(?<name>.*)::)#<Class:\k<name>>|#<Class:(?<name>.*)>(?:::)?/,
141
+ '\k<name>.'
142
+ )
143
+ scope << '#' unless scope.end_with?('.')
144
+
145
+ found_method(node, "#{scope}#{name}")
143
146
  end
144
147
 
145
148
  def found_method(node, method_name)
@@ -141,7 +141,7 @@ module RuboCop
141
141
  end
142
142
 
143
143
  def reference_pos(node)
144
- node = node.parent.masgn_type? ? node.parent : node
144
+ node = node.parent if node.parent.masgn_type?
145
145
 
146
146
  node.source_range.begin_pos
147
147
  end
@@ -2,40 +2,63 @@
2
2
 
3
3
  module RuboCop
4
4
  module Cop
5
- module Style
6
- # Common functionality related to annotation comments.
7
- module AnnotationComment
8
- private
9
-
10
- # @api public
11
- def annotation?(comment)
12
- _margin, first_word, colon, space, note = split_comment(comment)
13
- keyword_appearance?(first_word, colon, space) &&
14
- !just_first_word_of_sentence?(first_word, colon, space, note)
15
- end
16
-
17
- # @api public
18
- def split_comment(comment)
19
- match = comment.text.match(/^(# ?)([A-Za-z]+)(\s*:)?(\s+)?(\S+)?/)
20
- return false unless match
21
-
22
- match.captures
23
- end
24
-
25
- # @api public
26
- def keyword_appearance?(first_word, colon, space)
27
- first_word && keyword?(first_word.upcase) && (colon || space)
28
- end
29
-
30
- # @api private
31
- def just_first_word_of_sentence?(first_word, colon, space, note)
32
- first_word == first_word.capitalize && !colon && space && note
33
- end
34
-
35
- # @api public
36
- def keyword?(word)
37
- config.for_cop('Style/CommentAnnotation')['Keywords'].include?(word)
38
- end
5
+ # Representation of an annotation comment in source code (eg. `# TODO: blah blah blah`).
6
+ class AnnotationComment
7
+ extend Forwardable
8
+
9
+ attr_reader :comment, :margin, :keyword, :colon, :space, :note
10
+
11
+ # @param [Parser::Source::Comment] comment
12
+ # @param [Array<String>] keywords
13
+ def initialize(comment, keywords)
14
+ @comment = comment
15
+ @keywords = keywords
16
+ @margin, @keyword, @colon, @space, @note = split_comment(comment)
17
+ end
18
+
19
+ def annotation?
20
+ keyword_appearance? && !just_keyword_of_sentence?
21
+ end
22
+
23
+ def correct?(colon:)
24
+ return false unless keyword && space && note
25
+ return false unless keyword == keyword.upcase
26
+
27
+ self.colon.nil? == !colon
28
+ end
29
+
30
+ # Returns the range bounds for just the annotation
31
+ def bounds
32
+ start = comment.loc.expression.begin_pos + margin.length
33
+ length = [keyword, colon, space].reduce(0) { |len, elem| len + elem.to_s.length }
34
+ [start, start + length]
35
+ end
36
+
37
+ private
38
+
39
+ attr_reader :keywords
40
+
41
+ def split_comment(comment)
42
+ # Sort keywords by reverse length so that if a keyword is in a phrase
43
+ # but also on its own, both will match properly.
44
+ keywords_regex = Regexp.new(
45
+ Regexp.union(keywords.sort_by { |w| -w.length }).source,
46
+ Regexp::IGNORECASE
47
+ )
48
+ regex = /^(# ?)(\b#{keywords_regex}\b)(\s*:)?(\s+)?(\S+)?/i
49
+
50
+ match = comment.text.match(regex)
51
+ return false unless match
52
+
53
+ match.captures
54
+ end
55
+
56
+ def keyword_appearance?
57
+ keyword && (colon || space)
58
+ end
59
+
60
+ def just_keyword_of_sentence?
61
+ keyword == keyword.capitalize && !colon && space && note
39
62
  end
40
63
  end
41
64
  end