rubocop 1.3.0 → 1.5.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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +75 -16
  4. data/lib/rubocop.rb +5 -0
  5. data/lib/rubocop/cli.rb +5 -1
  6. data/lib/rubocop/cli/command/execute_runner.rb +26 -11
  7. data/lib/rubocop/cli/command/suggest_extensions.rb +67 -0
  8. data/lib/rubocop/config_loader.rb +12 -4
  9. data/lib/rubocop/config_loader_resolver.rb +5 -1
  10. data/lib/rubocop/config_obsoletion.rb +21 -3
  11. data/lib/rubocop/config_regeneration.rb +1 -1
  12. data/lib/rubocop/config_validator.rb +8 -1
  13. data/lib/rubocop/cop/autocorrect_logic.rb +21 -6
  14. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  15. data/lib/rubocop/cop/generator.rb +2 -9
  16. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  17. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +1 -1
  18. data/lib/rubocop/cop/layout/class_structure.rb +15 -3
  19. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +80 -10
  20. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +6 -1
  21. data/lib/rubocop/cop/layout/end_of_line.rb +5 -5
  22. data/lib/rubocop/cop/layout/first_argument_indentation.rb +7 -2
  23. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
  24. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -1
  25. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +4 -1
  26. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  27. data/lib/rubocop/cop/lint/missing_super.rb +7 -4
  28. data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +1 -1
  29. data/lib/rubocop/cop/lint/to_enum_arguments.rb +6 -15
  30. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +85 -0
  31. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +20 -6
  32. data/lib/rubocop/cop/metrics/abc_size.rb +25 -1
  33. data/lib/rubocop/cop/metrics/block_length.rb +13 -7
  34. data/lib/rubocop/cop/metrics/method_length.rb +7 -2
  35. data/lib/rubocop/cop/metrics/parameter_lists.rb +64 -1
  36. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +20 -10
  37. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +146 -0
  38. data/lib/rubocop/cop/metrics/utils/repeated_csend_discount.rb +6 -1
  39. data/lib/rubocop/cop/mixin/configurable_numbering.rb +4 -3
  40. data/lib/rubocop/cop/mixin/enforce_superclass.rb +9 -1
  41. data/lib/rubocop/cop/mixin/ignored_methods.rb +36 -3
  42. data/lib/rubocop/cop/mixin/method_complexity.rb +6 -0
  43. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  44. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -3
  45. data/lib/rubocop/cop/style/and_or.rb +10 -0
  46. data/lib/rubocop/cop/style/class_and_module_children.rb +8 -3
  47. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +8 -1
  48. data/lib/rubocop/cop/style/documentation.rb +12 -1
  49. data/lib/rubocop/cop/style/format_string.rb +8 -3
  50. data/lib/rubocop/cop/style/if_with_semicolon.rb +39 -4
  51. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -2
  52. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +11 -2
  53. data/lib/rubocop/cop/style/negated_if_else_condition.rb +3 -1
  54. data/lib/rubocop/cop/style/numeric_literals.rb +14 -11
  55. data/lib/rubocop/cop/style/redundant_argument.rb +75 -0
  56. data/lib/rubocop/cop/style/redundant_condition.rb +2 -1
  57. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  58. data/lib/rubocop/cop/style/sole_nested_conditional.rb +49 -3
  59. data/lib/rubocop/cop/style/symbol_proc.rb +5 -3
  60. data/lib/rubocop/cop/util.rb +1 -1
  61. data/lib/rubocop/cop/variable_force/branch.rb +1 -1
  62. data/lib/rubocop/cop/variable_force/scope.rb +1 -1
  63. data/lib/rubocop/core_ext/hash.rb +20 -0
  64. data/lib/rubocop/ext/regexp_node.rb +5 -10
  65. data/lib/rubocop/ext/regexp_parser.rb +2 -9
  66. data/lib/rubocop/formatter/disabled_config_formatter.rb +21 -6
  67. data/lib/rubocop/options.rb +5 -0
  68. data/lib/rubocop/rake_task.rb +2 -2
  69. data/lib/rubocop/runner.rb +1 -1
  70. data/lib/rubocop/target_finder.rb +1 -1
  71. data/lib/rubocop/target_ruby.rb +12 -4
  72. data/lib/rubocop/version.rb +1 -1
  73. metadata +12 -10
  74. data/bin/console +0 -10
  75. data/bin/rubocop-profile +0 -32
  76. data/bin/setup +0 -7
@@ -17,26 +17,61 @@ module RuboCop
17
17
  include OnNormalIfUnless
18
18
  extend AutoCorrector
19
19
 
20
- MSG = 'Do not use if x; Use the ternary operator instead.'
20
+ MSG_IF_ELSE = 'Do not use `if %<expr>s;` - use `if/else` instead.'
21
+ MSG_TERNARY = 'Do not use `if %<expr>s;` - use a ternary operator instead.'
21
22
 
22
23
  def on_normal_if_unless(node)
23
24
  return unless node.else_branch
25
+ return if node.parent&.if_type?
24
26
 
25
27
  beginning = node.loc.begin
26
28
  return unless beginning&.is?(';')
27
29
 
28
- add_offense(node) do |corrector|
29
- corrector.replace(node, correct_to_ternary(node))
30
+ message = node.else_branch.if_type? ? MSG_IF_ELSE : MSG_TERNARY
31
+
32
+ add_offense(node, message: format(message, expr: node.condition.source)) do |corrector|
33
+ corrector.replace(node, autocorrect(node))
30
34
  end
31
35
  end
32
36
 
33
37
  private
34
38
 
35
- def correct_to_ternary(node)
39
+ def autocorrect(node)
40
+ return correct_elsif(node) if node.else_branch.if_type?
41
+
36
42
  else_code = node.else_branch ? node.else_branch.source : 'nil'
37
43
 
38
44
  "#{node.condition.source} ? #{node.if_branch.source} : #{else_code}"
39
45
  end
46
+
47
+ def correct_elsif(node)
48
+ <<~RUBY.chop
49
+ if #{node.condition.source}
50
+ #{node.if_branch.source}
51
+ #{build_else_branch(node.else_branch).chop}
52
+ end
53
+ RUBY
54
+ end
55
+
56
+ def build_else_branch(second_condition)
57
+ result = <<~RUBY
58
+ elsif #{second_condition.condition.source}
59
+ #{second_condition.if_branch.source}
60
+ RUBY
61
+
62
+ if second_condition.else_branch
63
+ result += if second_condition.else_branch.if_type?
64
+ build_else_branch(second_condition.else_branch)
65
+ else
66
+ <<~RUBY
67
+ else
68
+ #{second_condition.else_branch.source}
69
+ RUBY
70
+ end
71
+ end
72
+
73
+ result
74
+ end
40
75
  end
41
76
  end
42
77
  end
@@ -74,7 +74,7 @@ module RuboCop
74
74
  parent &&
75
75
  (logical_operator?(parent) ||
76
76
  parent.send_type? &&
77
- parent.arguments.any?(&method(:logical_operator?)))
77
+ parent.arguments.any? { |argument| logical_operator?(argument) })
78
78
  end
79
79
 
80
80
  def call_in_optional_arguments?(node)
@@ -110,7 +110,7 @@ module RuboCop
110
110
  def hash_literal_in_arguments?(node)
111
111
  node.arguments.any? do |n|
112
112
  hash_literal?(n) ||
113
- n.send_type? && node.descendants.any?(&method(:hash_literal?))
113
+ n.send_type? && node.descendants.any? { |descendant| hash_literal?(descendant) }
114
114
  end
115
115
  end
116
116
 
@@ -21,21 +21,30 @@ module RuboCop
21
21
  def on_send(node)
22
22
  return unless !node.arguments? && node.parenthesized?
23
23
  return if ineligible_node?(node)
24
+ return if default_argument?(node)
24
25
  return if ignored_method?(node.method_name)
25
26
  return if same_name_assignment?(node)
26
27
 
28
+ register_offense(node)
29
+ end
30
+
31
+ private
32
+
33
+ def register_offense(node)
27
34
  add_offense(offense_range(node)) do |corrector|
28
35
  corrector.remove(node.loc.begin)
29
36
  corrector.remove(node.loc.end)
30
37
  end
31
38
  end
32
39
 
33
- private
34
-
35
40
  def ineligible_node?(node)
36
41
  node.camel_case_method? || node.implicit_call? || node.prefix_not?
37
42
  end
38
43
 
44
+ def default_argument?(node)
45
+ node.parent&.optarg_type?
46
+ end
47
+
39
48
  def same_name_assignment?(node)
40
49
  any_assignment?(node) do |asgn_node|
41
50
  next variable_in_mass_assignment?(node.method_name, asgn_node) if asgn_node.masgn_type?
@@ -35,6 +35,8 @@ module RuboCop
35
35
 
36
36
  NEGATED_EQUALITY_METHODS = %i[!= !~].freeze
37
37
 
38
+ def_node_matcher :double_negation?, '(send (send _ :!) :!)'
39
+
38
40
  def self.autocorrect_incompatible_with
39
41
  [Style::InverseMethods, Style::Not]
40
42
  end
@@ -47,7 +49,7 @@ module RuboCop
47
49
  return unless if_else?(node)
48
50
 
49
51
  condition = node.condition
50
- return unless negated_condition?(condition)
52
+ return if double_negation?(condition) || !negated_condition?(condition)
51
53
 
52
54
  type = node.ternary? ? 'ternary' : 'if-else'
53
55
  add_offense(node, message: format(MSG, type: type)) do |corrector|
@@ -27,12 +27,13 @@ module RuboCop
27
27
  # # bad
28
28
  # 10_000_00 # typical representation of $10,000 in cents
29
29
  #
30
- class NumericLiterals < Cop
30
+ class NumericLiterals < Base
31
31
  # The parameter is called MinDigits (meaning the minimum number of
32
32
  # digits for which an offense can be registered), but essentially it's
33
33
  # a Max parameter (the maximum number of something that's allowed).
34
34
  include ConfigurableMax
35
35
  include IntegerNode
36
+ extend AutoCorrector
36
37
 
37
38
  MSG = 'Use underscores(_) as thousands separator and ' \
38
39
  'separate every 3 digits with them.'
@@ -46,12 +47,6 @@ module RuboCop
46
47
  check(node)
47
48
  end
48
49
 
49
- def autocorrect(node)
50
- lambda do |corrector|
51
- corrector.replace(node, format_number(node))
52
- end
53
- end
54
-
55
50
  private
56
51
 
57
52
  def max_parameter_name
@@ -67,11 +62,19 @@ module RuboCop
67
62
 
68
63
  case int
69
64
  when /^\d+$/
70
- add_offense(node) { self.max = int.size + 1 }
65
+ return unless (self.max = int.size + 1)
66
+
67
+ register_offense(node)
71
68
  when /\d{4}/, short_group_regex
72
- add_offense(node) do
73
- self.config_to_allow_offenses = { 'Enabled' => false }
74
- end
69
+ return unless (self.config_to_allow_offenses = { 'Enabled' => false })
70
+
71
+ register_offense(node)
72
+ end
73
+ end
74
+
75
+ def register_offense(node)
76
+ add_offense(node) do |corrector|
77
+ corrector.replace(node, format_number(node))
75
78
  end
76
79
  end
77
80
 
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'parser/current'
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Style
8
+ # This cop checks for a redundant argument passed to certain methods.
9
+ #
10
+ # Limitations:
11
+ #
12
+ # 1. This cop matches for method names only and hence cannot tell apart
13
+ # methods with same name in different classes.
14
+ # 2. This cop is limited to methods with single parameter.
15
+ # 3. This cop is unsafe if certain special global variables (e.g. `$;`) are set.
16
+ # That depends on the nature of the target methods, of course.
17
+ #
18
+ # Method names and their redundant arguments can be configured like this:
19
+ #
20
+ # Methods:
21
+ # join: ''
22
+ # split: ' '
23
+ # foo: 2
24
+ #
25
+ # @example
26
+ # # bad
27
+ # array.join('')
28
+ # [1, 2, 3].join("")
29
+ # string.split(" ")
30
+ # "first\nsecond".split(" ")
31
+ # A.foo(2)
32
+ #
33
+ # # good
34
+ # array.join
35
+ # [1, 2, 3].join
36
+ # string.split
37
+ # "first second".split
38
+ # A.foo
39
+ class RedundantArgument < Base
40
+ MSG = 'Argument %<arg>s is redundant because it is implied by default.'
41
+
42
+ def on_send(node)
43
+ return if node.receiver.nil?
44
+ return if node.arguments.count != 1
45
+ return unless redundant_argument?(node)
46
+
47
+ add_offense(node, message: format(MSG, arg: node.arguments.first.source))
48
+ end
49
+
50
+ private
51
+
52
+ def redundant_argument?(node)
53
+ redundant_argument = redundant_arg_for_method(node.method_name.to_s)
54
+ return false if redundant_argument.nil?
55
+
56
+ node.arguments.first == redundant_argument
57
+ end
58
+
59
+ def redundant_arg_for_method(method_name)
60
+ return nil unless cop_config['Methods'].key?(method_name)
61
+
62
+ @mem ||= {}
63
+ @mem[method_name] ||=
64
+ begin
65
+ arg = cop_config['Methods'].fetch(method_name)
66
+ buffer = Parser::Source::Buffer.new('(string)', 1)
67
+ buffer.source = arg.inspect
68
+ builder = RuboCop::AST::Builder.new
69
+ Parser::CurrentRuby.new(builder).parse(buffer)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -131,7 +131,8 @@ module RuboCop
131
131
  end
132
132
 
133
133
  def without_argument_parentheses_method?(node)
134
- node.send_type? && !node.arguments.empty? && !node.parenthesized?
134
+ node.send_type? &&
135
+ !node.arguments.empty? && !node.parenthesized? && !node.operator_method?
135
136
  end
136
137
  end
137
138
  end
@@ -82,7 +82,7 @@ module RuboCop
82
82
 
83
83
  def each_escape(node)
84
84
  node.parsed_tree&.traverse&.reduce(0) do |char_class_depth, (event, expr)|
85
- yield(expr.text[1], expr.start_index, !char_class_depth.zero?) if expr.type == :escape
85
+ yield(expr.text[1], expr.ts, !char_class_depth.zero?) if expr.type == :escape
86
86
 
87
87
  if expr.type == :set
88
88
  char_class_depth + (event == :enter ? 1 : -1)
@@ -33,17 +33,22 @@ module RuboCop
33
33
  # end
34
34
  #
35
35
  class SoleNestedConditional < Base
36
+ include RangeHelp
37
+ extend AutoCorrector
38
+
36
39
  MSG = 'Consider merging nested conditions into '\
37
40
  'outer `%<conditional_type>s` conditions.'
38
41
 
39
42
  def on_if(node)
40
43
  return if node.ternary? || node.else? || node.elsif?
41
44
 
42
- branch = node.if_branch
43
- return unless offending_branch?(branch)
45
+ if_branch = node.if_branch
46
+ return unless offending_branch?(if_branch)
44
47
 
45
48
  message = format(MSG, conditional_type: node.keyword)
46
- add_offense(branch.loc.keyword, message: message)
49
+ add_offense(if_branch.loc.keyword, message: message) do |corrector|
50
+ autocorrect(corrector, node, if_branch)
51
+ end
47
52
  end
48
53
 
49
54
  private
@@ -57,6 +62,47 @@ module RuboCop
57
62
  !(branch.modifier_form? && allow_modifier?)
58
63
  end
59
64
 
65
+ def autocorrect(corrector, node, if_branch)
66
+ if node.unless?
67
+ corrector.replace(node.loc.keyword, 'if')
68
+ corrector.insert_before(node.condition, '!')
69
+ end
70
+
71
+ and_operator = if_branch.unless? ? ' && !' : ' && '
72
+ if if_branch.modifier_form?
73
+ correct_for_gurad_condition_style(corrector, node, if_branch, and_operator)
74
+ else
75
+ correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
76
+ end
77
+
78
+ correct_for_comment(corrector, node, if_branch)
79
+ end
80
+
81
+ def correct_for_gurad_condition_style(corrector, node, if_branch, and_operator)
82
+ corrector.insert_after(node.condition, "#{and_operator}#{if_branch.condition.source}")
83
+
84
+ range = range_between(
85
+ if_branch.loc.keyword.begin_pos, if_branch.condition.source_range.end_pos
86
+ )
87
+ corrector.remove(range_with_surrounding_space(range: range, newlines: false))
88
+ corrector.remove(if_branch.loc.keyword)
89
+ end
90
+
91
+ def correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
92
+ range = range_between(
93
+ node.condition.source_range.end_pos, if_branch.condition.source_range.begin_pos
94
+ )
95
+ corrector.replace(range, and_operator)
96
+ corrector.remove(range_by_whole_lines(node.loc.end, include_final_newline: true))
97
+ end
98
+
99
+ def correct_for_comment(corrector, node, if_branch)
100
+ comments = processed_source.comments_before_line(if_branch.source_range.line)
101
+ comment_text = comments.map(&:text).join("\n") << "\n"
102
+
103
+ corrector.insert_before(node.loc.keyword, comment_text) unless comments.empty?
104
+ end
105
+
60
106
  def allow_modifier?
61
107
  cop_config['AllowModifier']
62
108
  end
@@ -8,6 +8,7 @@ module RuboCop
8
8
  # @example
9
9
  # # bad
10
10
  # something.map { |s| s.upcase }
11
+ # something.map { _1.upcase }
11
12
  #
12
13
  # # good
13
14
  # something.map(&:upcase)
@@ -22,9 +23,9 @@ module RuboCop
22
23
 
23
24
  def_node_matcher :proc_node?, '(send (const {nil? cbase} :Proc) :new)'
24
25
  def_node_matcher :symbol_proc?, <<~PATTERN
25
- (block
26
+ ({block numblock}
26
27
  ${(send ...) (super ...) zsuper}
27
- $(args (arg _var))
28
+ ${(args (arg _)) %Integer}
28
29
  (send (lvar _var) $_))
29
30
  PATTERN
30
31
 
@@ -40,11 +41,12 @@ module RuboCop
40
41
  return if proc_node?(dispatch_node)
41
42
  return if %i[lambda proc].include?(dispatch_node.method_name)
42
43
  return if ignored_method?(dispatch_node.method_name)
43
- return if destructuring_block_argument?(arguments_node)
44
+ return if node.block_type? && destructuring_block_argument?(arguments_node)
44
45
 
45
46
  register_offense(node, method_name, dispatch_node.method_name)
46
47
  end
47
48
  end
49
+ alias on_numblock on_block
48
50
 
49
51
  def destructuring_block_argument?(argument_node)
50
52
  argument_node.one? && argument_node.source.include?(',')
@@ -53,7 +53,7 @@ module RuboCop
53
53
  end
54
54
 
55
55
  def on_node(syms, sexp, excludes = [], &block)
56
- return to_enum(:on_node, syms, sexp, excludes) unless block_given?
56
+ return to_enum(:on_node, syms, sexp, excludes) unless block
57
57
 
58
58
  yield sexp if Array(syms).include?(sexp.type)
59
59
  return if Array(excludes).include?(sexp.type)
@@ -78,7 +78,7 @@ module RuboCop
78
78
  end
79
79
 
80
80
  def each_ancestor(include_self: false, &block)
81
- return to_enum(__method__, include_self: include_self) unless block_given?
81
+ return to_enum(__method__, include_self: include_self) unless block
82
82
 
83
83
  yield self if include_self
84
84
  scan_ancestors(&block)
@@ -61,7 +61,7 @@ module RuboCop
61
61
  end
62
62
 
63
63
  def each_node(&block)
64
- return to_enum(__method__) unless block_given?
64
+ return to_enum(__method__) unless block
65
65
 
66
66
  yield node if naked_top_level?
67
67
  scan_node(node, &block)
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Extensions to the core Hash class
4
+ class Hash
5
+ unless method_defined?(:slice)
6
+ # Adds `Hash#slice` for Ruby 2.4.
7
+ # Returns a hash containing a subset of keys. If a given key is not
8
+ # in the hash, it will not be returned.
9
+ #
10
+ # @return [Hash] hash containing only the keys given.
11
+ #
12
+ # @example
13
+ # { one: 1, two: 2 }.slice(:two, :three) #=> { two: 2 }
14
+ def slice(*keys)
15
+ h = {}
16
+ keys.each { |k| h[k] = self[k] if key?(k) }
17
+ h
18
+ end
19
+ end
20
+ end
@@ -19,18 +19,13 @@ module RuboCop
19
19
  super
20
20
 
21
21
  str = with_interpolations_blanked
22
- begin
23
- @parsed_tree = Regexp::Parser.parse(str, options: options)
22
+ @parsed_tree = begin
23
+ Regexp::Parser.parse(str, options: options)
24
24
  rescue StandardError
25
- @parsed_tree = nil
26
- else
27
- origin = loc.begin.end
28
- source = @parsed_tree.to_s
29
- @parsed_tree.each_expression(true) do |e|
30
- e.origin = origin
31
- e.source = source
32
- end
25
+ nil
33
26
  end
27
+ origin = loc.begin.end
28
+ @parsed_tree&.each_expression(true) { |e| e.origin = origin }
34
29
  end
35
30
 
36
31
  def each_capture(named: ANY)