rubocop 1.18.4 → 1.19.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 (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +22 -5
  4. data/lib/rubocop.rb +4 -0
  5. data/lib/rubocop/cli.rb +18 -0
  6. data/lib/rubocop/config_loader_resolver.rb +21 -6
  7. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
  8. data/lib/rubocop/cop/correctors/require_library_corrector.rb +23 -0
  9. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  10. data/lib/rubocop/cop/internal_affairs.rb +2 -0
  11. data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +34 -0
  12. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +71 -0
  13. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -0
  14. data/lib/rubocop/cop/layout/end_alignment.rb +2 -1
  15. data/lib/rubocop/cop/layout/hash_alignment.rb +7 -3
  16. data/lib/rubocop/cop/layout/heredoc_indentation.rb +0 -7
  17. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  18. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +33 -14
  19. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +21 -9
  20. data/lib/rubocop/cop/layout/space_around_operators.rb +8 -1
  21. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
  22. data/lib/rubocop/cop/layout/space_inside_parens.rb +5 -5
  23. data/lib/rubocop/cop/layout/trailing_whitespace.rb +24 -1
  24. data/lib/rubocop/cop/lint/ambiguous_range.rb +105 -0
  25. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -2
  26. data/lib/rubocop/cop/lint/duplicate_methods.rb +8 -5
  27. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
  28. data/lib/rubocop/cop/mixin/heredoc.rb +7 -0
  29. data/lib/rubocop/cop/mixin/percent_array.rb +10 -7
  30. data/lib/rubocop/cop/mixin/require_library.rb +59 -0
  31. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  32. data/lib/rubocop/cop/naming/inclusive_language.rb +18 -1
  33. data/lib/rubocop/cop/style/block_delimiters.rb +16 -0
  34. data/lib/rubocop/cop/style/commented_keyword.rb +2 -1
  35. data/lib/rubocop/cop/style/conditional_assignment.rb +19 -5
  36. data/lib/rubocop/cop/style/explicit_block_argument.rb +32 -7
  37. data/lib/rubocop/cop/style/hash_transform_keys.rb +0 -3
  38. data/lib/rubocop/cop/style/identical_conditional_branches.rb +30 -5
  39. data/lib/rubocop/cop/style/method_def_parentheses.rb +10 -1
  40. data/lib/rubocop/cop/style/missing_else.rb +7 -0
  41. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +88 -0
  42. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  43. data/lib/rubocop/cop/style/semicolon.rb +32 -24
  44. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  45. data/lib/rubocop/cop/style/special_global_vars.rb +21 -0
  46. data/lib/rubocop/cop/style/word_array.rb +20 -2
  47. data/lib/rubocop/cop/util.rb +7 -2
  48. data/lib/rubocop/options.rb +1 -1
  49. data/lib/rubocop/version.rb +1 -1
  50. metadata +11 -5
@@ -143,13 +143,6 @@ module RuboCop
143
143
  indent_level(base_line)
144
144
  end
145
145
 
146
- def indent_level(str)
147
- indentations = str.lines
148
- .map { |line| line[/^\s*/] }
149
- .reject { |line| line.end_with?("\n") }
150
- indentations.empty? ? 0 : indentations.min_by(&:size).size
151
- end
152
-
153
146
  # Returns '~', '-' or nil
154
147
  def heredoc_indent_type(node)
155
148
  node.source[/^<<([~-])/, 1]
@@ -57,7 +57,7 @@ module RuboCop
57
57
 
58
58
  def on_new_investigation
59
59
  processed_source.comments.each do |comment|
60
- next unless /\A#+[^#\s=:+-]/.match?(comment.text)
60
+ next unless /\A#+[^#\s=+-]/.match?(comment.text)
61
61
  next if comment.loc.line == 1 && allowed_on_first_line?(comment)
62
62
  next if doxygen_comment_style?(comment)
63
63
  next if gemfile_ruby_comment?(comment)
@@ -11,8 +11,7 @@ module RuboCop
11
11
  # concatenated string parts shall be indented regardless of `EnforcedStyle` configuration.
12
12
  #
13
13
  # If `EnforcedStyle: indented` is set, it's the second line that shall be indented one step
14
- # more than the first line. Lines 3 and forward shall be aligned with line 2. Here too there
15
- # are exceptions. Values in a hash literal are always aligned.
14
+ # more than the first line. Lines 3 and forward shall be aligned with line 2.
16
15
  #
17
16
  # @example
18
17
  # # bad
@@ -34,29 +33,44 @@ module RuboCop
34
33
  # 'z'
35
34
  # end
36
35
  #
37
- # my_hash = {
38
- # first: 'a message' \
39
- # 'in two parts'
40
- # }
41
- #
42
36
  # @example EnforcedStyle: aligned (default)
43
37
  # # bad
44
38
  # puts 'x' \
45
39
  # 'y'
46
40
  #
41
+ # my_hash = {
42
+ # first: 'a message' \
43
+ # 'in two parts'
44
+ # }
45
+ #
47
46
  # # good
48
47
  # puts 'x' \
49
48
  # 'y'
50
49
  #
50
+ # my_hash = {
51
+ # first: 'a message' \
52
+ # 'in two parts'
53
+ # }
54
+ #
51
55
  # @example EnforcedStyle: indented
52
56
  # # bad
53
57
  # result = 'x' \
54
58
  # 'y'
55
59
  #
60
+ # my_hash = {
61
+ # first: 'a message' \
62
+ # 'in two parts'
63
+ # }
64
+ #
56
65
  # # good
57
66
  # result = 'x' \
58
67
  # 'y'
59
68
  #
69
+ # my_hash = {
70
+ # first: 'a message' \
71
+ # 'in two parts'
72
+ # }
73
+ #
60
74
  class LineEndStringConcatenationIndentation < Base
61
75
  include ConfigurableEnforcedStyle
62
76
  include Alignment
@@ -70,7 +84,7 @@ module RuboCop
70
84
  return unless strings_concatenated_with_backslash?(node)
71
85
 
72
86
  children = node.children
73
- if style == :aligned && !always_indented?(node) || always_aligned?(node)
87
+ if style == :aligned && !always_indented?(node)
74
88
  check_aligned(children, 1)
75
89
  else
76
90
  check_indented(children)
@@ -94,10 +108,6 @@ module RuboCop
94
108
  PARENT_TYPES_FOR_INDENTED.include?(dstr_node.parent&.type)
95
109
  end
96
110
 
97
- def always_aligned?(dstr_node)
98
- dstr_node.parent&.pair_type?
99
- end
100
-
101
111
  def check_aligned(children, start_index)
102
112
  base_column = children[start_index - 1].loc.column
103
113
  children[start_index..-1].each do |child|
@@ -108,11 +118,20 @@ module RuboCop
108
118
  end
109
119
 
110
120
  def check_indented(children)
111
- base_column = children[0].source_range.source_line =~ /\S/
112
- @column_delta = base_column + configured_indentation_width - children[1].loc.column
121
+ @column_delta = base_column(children[0]) + configured_indentation_width -
122
+ children[1].loc.column
113
123
  add_offense_and_correction(children[1], MSG_INDENT) if @column_delta != 0
114
124
  end
115
125
 
126
+ def base_column(child)
127
+ grandparent = child.parent.parent
128
+ if grandparent&.type == :pair
129
+ grandparent.loc.column
130
+ else
131
+ child.source_range.source_line =~ /\S/
132
+ end
133
+ end
134
+
116
135
  def add_offense_and_correction(node, message)
117
136
  add_offense(node, message: message) { |corrector| autocorrect(corrector, node) }
118
137
  end
@@ -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,25 @@ 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.selector
141
+
142
+ if send_node_loc.respond_to?(:dot) && (dot = send_node_loc.dot) &&
143
+ aligned_with_leading_dot?(do_keyword_line, dot, rescue_keyword_column)
144
+ return true
145
+ end
146
+
147
+ do_keyword_line == selector.line && rescue_keyword_column == selector.column
148
+ end
138
149
 
139
- node.each_ancestor(*ancestor_types).first
150
+ def aligned_with_leading_dot?(do_keyword_line, dot, rescue_keyword_column)
151
+ do_keyword_line == dot.line && rescue_keyword_column == dot.column
140
152
  end
141
153
 
142
154
  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)
@@ -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