rubocop 1.15.0 → 1.18.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +104 -31
  4. data/lib/rubocop.rb +7 -0
  5. data/lib/rubocop/cli/command/suggest_extensions.rb +3 -3
  6. data/lib/rubocop/config_loader.rb +1 -1
  7. data/lib/rubocop/config_validator.rb +5 -5
  8. data/lib/rubocop/cop/base.rb +2 -2
  9. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
  10. data/lib/rubocop/cop/bundler/gem_version.rb +38 -4
  11. data/lib/rubocop/cop/corrector.rb +4 -4
  12. data/lib/rubocop/cop/generator.rb +1 -1
  13. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
  14. data/lib/rubocop/cop/layout/argument_alignment.rb +4 -3
  15. data/lib/rubocop/cop/layout/array_alignment.rb +2 -2
  16. data/lib/rubocop/cop/layout/block_alignment.rb +1 -1
  17. data/lib/rubocop/cop/layout/case_indentation.rb +57 -9
  18. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +7 -1
  19. data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
  20. data/lib/rubocop/cop/layout/dot_position.rb +7 -1
  21. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +13 -15
  22. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -2
  23. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -2
  24. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
  25. data/lib/rubocop/cop/layout/hash_alignment.rb +26 -8
  26. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  27. data/lib/rubocop/cop/layout/indentation_width.rb +8 -0
  28. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +122 -0
  29. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +6 -6
  30. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +2 -2
  31. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +6 -6
  32. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +6 -6
  33. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +6 -6
  34. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +3 -3
  35. data/lib/rubocop/cop/layout/parameter_alignment.rb +2 -2
  36. data/lib/rubocop/cop/layout/redundant_line_break.rb +11 -9
  37. data/lib/rubocop/cop/layout/space_around_keyword.rb +28 -0
  38. data/lib/rubocop/cop/layout/space_around_operators.rb +7 -1
  39. data/lib/rubocop/cop/lint/empty_in_pattern.rb +62 -0
  40. data/lib/rubocop/cop/lint/literal_as_condition.rb +13 -1
  41. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +32 -17
  42. data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
  43. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  44. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  45. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +105 -74
  46. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +5 -0
  47. data/lib/rubocop/cop/lint/symbol_conversion.rb +3 -13
  48. data/lib/rubocop/cop/lint/unused_block_argument.rb +1 -1
  49. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
  50. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  51. data/lib/rubocop/cop/migration/department_name.rb +3 -1
  52. data/lib/rubocop/cop/mixin/check_line_breakable.rb +10 -1
  53. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +14 -3
  54. data/lib/rubocop/cop/mixin/string_literals_help.rb +2 -4
  55. data/lib/rubocop/cop/mixin/symbol_help.rb +13 -0
  56. data/lib/rubocop/cop/naming/inclusive_language.rb +249 -0
  57. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +2 -2
  58. data/lib/rubocop/cop/style/class_and_module_children.rb +14 -0
  59. data/lib/rubocop/cop/style/hash_each_methods.rb +18 -1
  60. data/lib/rubocop/cop/style/identical_conditional_branches.rb +58 -8
  61. data/lib/rubocop/cop/style/in_pattern_then.rb +56 -0
  62. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -1
  63. data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +62 -0
  64. data/lib/rubocop/cop/style/multiline_when_then.rb +2 -11
  65. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
  66. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  67. data/lib/rubocop/cop/style/quoted_symbols.rb +110 -0
  68. data/lib/rubocop/cop/style/raise_args.rb +2 -0
  69. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +1 -1
  70. data/lib/rubocop/cop/style/redundant_self.rb +24 -2
  71. data/lib/rubocop/cop/style/regexp_literal.rb +10 -1
  72. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  73. data/lib/rubocop/cop/style/string_concatenation.rb +32 -5
  74. data/lib/rubocop/cop/style/string_literals.rb +3 -2
  75. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +1 -0
  76. data/lib/rubocop/cop/style/swap_values.rb +1 -1
  77. data/lib/rubocop/cop/style/top_level_method_definition.rb +10 -2
  78. data/lib/rubocop/cop/style/unpack_first.rb +1 -1
  79. data/lib/rubocop/cop/style/when_then.rb +6 -2
  80. data/lib/rubocop/cop/variable_force/variable_table.rb +1 -1
  81. data/lib/rubocop/directive_comment.rb +58 -6
  82. data/lib/rubocop/options.rb +4 -4
  83. data/lib/rubocop/rake_task.rb +1 -1
  84. data/lib/rubocop/remote_config.rb +10 -2
  85. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  86. data/lib/rubocop/rspec/expect_offense.rb +1 -1
  87. data/lib/rubocop/version.rb +1 -1
  88. metadata +12 -5
@@ -147,9 +147,9 @@ module RuboCop
147
147
  include ConfigurableEnforcedStyle
148
148
 
149
149
  MSG = 'Memoized variable `%<var>s` does not match ' \
150
- 'method name `%<method>s`. Use `@%<suggested_var>s` instead.'
150
+ 'method name `%<method>s`. Use `@%<suggested_var>s` instead.'
151
151
  UNDERSCORE_REQUIRED = 'Memoized variable `%<var>s` does not start ' \
152
- 'with `_`. Use `@%<suggested_var>s` instead.'
152
+ 'with `_`. Use `@%<suggested_var>s` instead.'
153
153
  DYNAMIC_DEFINE_METHODS = %i[define_method define_singleton_method].to_set.freeze
154
154
 
155
155
  # @!method method_definition?(node)
@@ -84,6 +84,7 @@ module RuboCop
84
84
  def compact_definition(corrector, node)
85
85
  compact_node(corrector, node)
86
86
  remove_end(corrector, node.body)
87
+ unindent(corrector, node)
87
88
  end
88
89
 
89
90
  def compact_node(corrector, node)
@@ -114,6 +115,19 @@ module RuboCop
114
115
  corrector.remove(range)
115
116
  end
116
117
 
118
+ def configured_indentation_width
119
+ config.for_badge(Layout::IndentationWidth.badge).fetch('Width', 2)
120
+ end
121
+
122
+ def unindent(corrector, node)
123
+ return if node.body.children.last.nil?
124
+
125
+ column_delta = configured_indentation_width - leading_spaces(node.body.children.last).size
126
+ return if column_delta.zero?
127
+
128
+ AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
129
+ end
130
+
117
131
  def leading_spaces(node)
118
132
  node.source_range.source_line[/\A\s*/]
119
133
  end
@@ -17,6 +17,11 @@ module RuboCop
17
17
  # # good
18
18
  # hash.each_key { |k| p k }
19
19
  # hash.each_value { |v| p v }
20
+ #
21
+ # @example AllowedReceivers: ['execute']
22
+ # # good
23
+ # execute(sql).keys.each { |v| p v }
24
+ # execute(sql).values.each { |v| p v }
20
25
  class HashEachMethods < Base
21
26
  include Lint::UnusedArgument
22
27
  extend AutoCorrector
@@ -36,7 +41,9 @@ module RuboCop
36
41
 
37
42
  def register_kv_offense(node)
38
43
  kv_each(node) do |target, method|
39
- return unless target.receiver.receiver
44
+ parent_receiver = target.receiver.receiver
45
+ return unless parent_receiver
46
+ return if allowed_receiver?(parent_receiver)
40
47
 
41
48
  msg = format(message, prefer: "each_#{method[0..-2]}", current: "#{method}.each")
42
49
 
@@ -80,6 +87,16 @@ module RuboCop
80
87
  def kv_range(outer_node)
81
88
  outer_node.receiver.loc.selector.join(outer_node.loc.selector)
82
89
  end
90
+
91
+ def allowed_receiver?(receiver)
92
+ receiver_name = receiver.send_type? ? receiver.method_name.to_s : receiver.source
93
+
94
+ allowed_receivers.include?(receiver_name)
95
+ end
96
+
97
+ def allowed_receivers
98
+ cop_config.fetch('AllowedReceivers', [])
99
+ end
83
100
  end
84
101
  end
85
102
  end
@@ -67,40 +67,90 @@ module RuboCop
67
67
  # do_x
68
68
  # do_z
69
69
  # end
70
+ #
71
+ # # bad
72
+ # case foo
73
+ # in 1
74
+ # do_x
75
+ # in 2
76
+ # do_x
77
+ # else
78
+ # do_x
79
+ # end
80
+ #
81
+ # # good
82
+ # case foo
83
+ # in 1
84
+ # do_x
85
+ # do_y
86
+ # in 2
87
+ # # nothing
88
+ # else
89
+ # do_x
90
+ # do_z
91
+ # end
70
92
  class IdenticalConditionalBranches < Base
93
+ include RangeHelp
94
+ extend AutoCorrector
95
+
71
96
  MSG = 'Move `%<source>s` out of the conditional.'
72
97
 
73
98
  def on_if(node)
74
99
  return if node.elsif?
75
100
 
76
101
  branches = expand_elses(node.else_branch).unshift(node.if_branch)
77
- check_branches(branches)
102
+ check_branches(node, branches)
78
103
  end
79
104
 
80
105
  def on_case(node)
81
106
  return unless node.else? && node.else_branch
82
107
 
83
108
  branches = node.when_branches.map(&:body).push(node.else_branch)
84
- check_branches(branches)
109
+ check_branches(node, branches)
110
+ end
111
+
112
+ def on_case_match(node)
113
+ return unless node.else? && node.else_branch
114
+
115
+ branches = node.in_pattern_branches.map(&:body).push(node.else_branch)
116
+ check_branches(node, branches)
85
117
  end
86
118
 
87
119
  private
88
120
 
89
- def check_branches(branches) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
121
+ def check_branches(node, branches)
90
122
  # return if any branch is empty. An empty branch can be an `if`
91
123
  # without an `else` or a branch that contains only comments.
92
124
  return if branches.any?(&:nil?)
93
125
 
94
126
  tails = branches.map { |branch| tail(branch) }
95
- check_expressions(tails) if tails.none?(&:nil?)
127
+ check_expressions(node, tails, :after_condition) if duplicated_expressions?(tails)
128
+
96
129
  heads = branches.map { |branch| head(branch) }
97
- check_expressions(heads) if tails.none?(&:nil?)
130
+ check_expressions(node, heads, :before_condition) if duplicated_expressions?(heads)
98
131
  end
99
132
 
100
- def check_expressions(expressions)
101
- return unless expressions.size > 1 && expressions.uniq.one?
133
+ def duplicated_expressions?(expressions)
134
+ expressions.size > 1 && expressions.uniq.one?
135
+ end
102
136
 
103
- expressions.each { |expression| add_offense(expression) }
137
+ def check_expressions(node, expressions, insert_position)
138
+ inserted_expression = false
139
+
140
+ expressions.each do |expression|
141
+ add_offense(expression) do |corrector|
142
+ range = range_by_whole_lines(expression.source_range, include_final_newline: true)
143
+ corrector.remove(range)
144
+ next if inserted_expression
145
+
146
+ if insert_position == :after_condition
147
+ corrector.insert_after(node, "\n#{expression.source}")
148
+ else
149
+ corrector.insert_before(node, "#{expression.source}\n")
150
+ end
151
+ inserted_expression = true
152
+ end
153
+ end
104
154
  end
105
155
 
106
156
  def message(node)
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # This cop checks for `in;` uses in `case` expressions.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # case expression
11
+ # in pattern_a; foo
12
+ # in pattern_b; bar
13
+ # end
14
+ #
15
+ # # good
16
+ # case expression
17
+ # in pattern_a then foo
18
+ # in pattern_b then bar
19
+ # end
20
+ #
21
+ class InPatternThen < Base
22
+ extend AutoCorrector
23
+ extend TargetRubyVersion
24
+
25
+ minimum_target_ruby_version 2.7
26
+
27
+ MSG = 'Do not use `in %<pattern>s;`. Use `in %<pattern>s then` instead.'
28
+
29
+ def on_in_pattern(node)
30
+ return if node.multiline? || node.then? || !node.body
31
+
32
+ pattern = node.pattern
33
+ pattern_source = if pattern.match_alt_type?
34
+ alternative_pattern_source(pattern)
35
+ else
36
+ pattern.source
37
+ end
38
+
39
+ add_offense(node.loc.begin, message: format(MSG, pattern: pattern_source)) do |corrector|
40
+ corrector.replace(node.loc.begin, ' then')
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def alternative_pattern_source(pattern)
47
+ return pattern.children.map(&:source) unless pattern.children.first.match_alt_type?
48
+
49
+ pattern_sources = alternative_pattern_source(pattern.children.first)
50
+
51
+ (pattern_sources << pattern.children[1].source).join(' | ')
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -116,7 +116,8 @@ module RuboCop
116
116
  end
117
117
 
118
118
  def call_with_braced_block?(node)
119
- (node.send_type? || node.super_type?) && node.block_node && node.block_node.braces?
119
+ (node.send_type? || node.super_type?) &&
120
+ ((node.parent&.block_type? || node.parent&.numblock_type?) && node.parent&.braces?)
120
121
  end
121
122
 
122
123
  def call_as_argument_or_chain?(node)
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # This cop checks uses of the `then` keyword in multi-line `in` statement.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # case expression
11
+ # in pattern then
12
+ # end
13
+ #
14
+ # # good
15
+ # case expression
16
+ # in pattern
17
+ # end
18
+ #
19
+ # # good
20
+ # case expression
21
+ # in pattern then do_something
22
+ # end
23
+ #
24
+ # # good
25
+ # case expression
26
+ # in pattern then do_something(arg1,
27
+ # arg2)
28
+ # end
29
+ #
30
+ class MultilineInPatternThen < Base
31
+ include RangeHelp
32
+ extend AutoCorrector
33
+ extend TargetRubyVersion
34
+
35
+ minimum_target_ruby_version 2.7
36
+
37
+ MSG = 'Do not use `then` for multiline `in` statement.'
38
+
39
+ def on_in_pattern(node)
40
+ return if !node.then? || require_then?(node)
41
+
42
+ range = node.loc.begin
43
+ add_offense(range) do |corrector|
44
+ corrector.remove(
45
+ range_with_surrounding_space(range: range, side: :left, newlines: false)
46
+ )
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ # Requires `then` for write `in` and its body on the same line.
53
+ def require_then?(in_pattern_node)
54
+ return true if in_pattern_node.pattern.first_line != in_pattern_node.pattern.last_line
55
+ return false unless in_pattern_node.body
56
+
57
+ in_pattern_node.loc.line == in_pattern_node.body.loc.line
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -35,17 +35,7 @@ module RuboCop
35
35
  MSG = 'Do not use `then` for multiline `when` statement.'
36
36
 
37
37
  def on_when(node)
38
- # Without `then`, there's no offense
39
- return unless node.then?
40
-
41
- # Single line usage of `then` is not an offense
42
- return if !node.children.last.nil? && !node.multiline?
43
-
44
- # Requires `then` for write `when` and its body on the same line.
45
- return if require_then?(node)
46
-
47
- # For arrays and hashes there's no offense
48
- return if accept_node_type?(node.body)
38
+ return if !node.then? || require_then?(node)
49
39
 
50
40
  range = node.loc.begin
51
41
  add_offense(range) do |corrector|
@@ -57,6 +47,7 @@ module RuboCop
57
47
 
58
48
  private
59
49
 
50
+ # Requires `then` for write `when` and its body on the same line.
60
51
  def require_then?(when_node)
61
52
  unless when_node.conditions.first.first_line == when_node.conditions.last.last_line
62
53
  return true
@@ -44,7 +44,7 @@ module RuboCop
44
44
  extend AutoCorrector
45
45
 
46
46
  MSG = 'Avoid comparing a variable with multiple items ' \
47
- 'in a conditional, use `Array#include?` instead.'
47
+ 'in a conditional, use `Array#include?` instead.'
48
48
 
49
49
  def on_new_investigation
50
50
  @last_comparison = nil
@@ -68,7 +68,7 @@ module RuboCop
68
68
  delimiters = preferred_delimiters_for(type)
69
69
 
70
70
  "`#{type}`-literals should be delimited by " \
71
- "`#{delimiters[0]}` and `#{delimiters[1]}`."
71
+ "`#{delimiters[0]}` and `#{delimiters[1]}`."
72
72
  end
73
73
 
74
74
  def preferred_delimiters_for(type)
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Checks if the quotes used for quoted symbols match the configured defaults.
7
+ # By default uses the same configuration as `Style/StringLiterals`.
8
+ #
9
+ # String interpolation is always kept in double quotes.
10
+ #
11
+ # Note: `Lint/SymbolConversion` can be used in parallel to ensure that symbols
12
+ # are not quoted that don't need to be. This cop is for configuring the quoting
13
+ # style to use for symbols that require quotes.
14
+ #
15
+ # @example EnforcedStyle: same_as_string_literals (default) / single_quotes
16
+ # # bad
17
+ # :"abc-def"
18
+ #
19
+ # # good
20
+ # :'abc-def'
21
+ # :"#{str}"
22
+ # :"a\'b"
23
+ #
24
+ # @example EnforcedStyle: double_quotes
25
+ # # bad
26
+ # :'abc-def'
27
+ #
28
+ # # good
29
+ # :"abc-def"
30
+ # :"#{str}"
31
+ # :"a\'b"
32
+ class QuotedSymbols < Base
33
+ include ConfigurableEnforcedStyle
34
+ include SymbolHelp
35
+ include StringLiteralsHelp
36
+ extend AutoCorrector
37
+
38
+ MSG_SINGLE = "Prefer single-quoted symbols when you don't need string interpolation " \
39
+ 'or special symbols.'
40
+ MSG_DOUBLE = 'Prefer double-quoted symbols unless you need single quotes to ' \
41
+ 'avoid extra backslashes for escaping.'
42
+
43
+ def on_sym(node)
44
+ return unless quoted?(node)
45
+
46
+ message = style == :single_quotes ? MSG_SINGLE : MSG_DOUBLE
47
+
48
+ if wrong_quotes?(node)
49
+ add_offense(node, message: message) do |corrector|
50
+ opposite_style_detected
51
+ autocorrect(corrector, node)
52
+ end
53
+ else
54
+ correct_style_detected
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def autocorrect(corrector, node)
61
+ str = if hash_colon_key?(node)
62
+ # strip quotes
63
+ correct_quotes(node.source[1..-2])
64
+ else
65
+ # strip leading `:` and quotes
66
+ ":#{correct_quotes(node.source[2..-2])}"
67
+ end
68
+
69
+ corrector.replace(node, str)
70
+ end
71
+
72
+ def hash_colon_key?(node)
73
+ # Is the node a hash key with the colon style?
74
+ hash_key?(node) && node.parent.colon?
75
+ end
76
+
77
+ def correct_quotes(str)
78
+ if style == :single_quotes
79
+ to_string_literal(str)
80
+ else
81
+ str.inspect
82
+ end
83
+ end
84
+
85
+ def style
86
+ return super unless super == :same_as_string_literals
87
+
88
+ string_literals_config = config.for_cop('Style/StringLiterals')
89
+ return :single_quotes unless string_literals_config['Enabled']
90
+
91
+ string_literals_config['EnforcedStyle'].to_sym
92
+ end
93
+
94
+ def alternative_style
95
+ (supported_styles - [style, :same_as_string_literals]).first
96
+ end
97
+
98
+ def quoted?(sym_node)
99
+ sym_node.source.match?(/\A:?(['"]).*?\1\z/m)
100
+ end
101
+
102
+ def wrong_quotes?(node)
103
+ return super if hash_key?(node)
104
+
105
+ super(node.source[1..-1])
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end