rubocop 0.74.0 → 0.75.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 (79) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -1
  3. data/config/default.yml +27 -3
  4. data/lib/rubocop.rb +6 -1
  5. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +1 -12
  6. data/lib/rubocop/comment_config.rb +3 -2
  7. data/lib/rubocop/config.rb +4 -0
  8. data/lib/rubocop/config_loader.rb +20 -2
  9. data/lib/rubocop/config_loader_resolver.rb +2 -2
  10. data/lib/rubocop/config_obsoletion.rb +12 -0
  11. data/lib/rubocop/cop/autocorrect_logic.rb +2 -2
  12. data/lib/rubocop/cop/cop.rb +4 -3
  13. data/lib/rubocop/cop/correctors/alignment_corrector.rb +43 -17
  14. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +2 -2
  15. data/lib/rubocop/cop/generator.rb +3 -3
  16. data/lib/rubocop/cop/generator/configuration_injector.rb +9 -4
  17. data/lib/rubocop/cop/generator/require_file_injector.rb +1 -1
  18. data/lib/rubocop/cop/layout/block_alignment.rb +2 -2
  19. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +1 -1
  20. data/lib/rubocop/cop/layout/extra_spacing.rb +0 -6
  21. data/lib/rubocop/cop/layout/indent_assignment.rb +9 -1
  22. data/lib/rubocop/cop/layout/indent_heredoc.rb +1 -1
  23. data/lib/rubocop/cop/layout/multiline_block_layout.rb +24 -2
  24. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +5 -1
  25. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +1 -1
  26. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +7 -0
  27. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +2 -0
  28. data/lib/rubocop/cop/lint/assignment_in_condition.rb +17 -4
  29. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  30. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +10 -36
  31. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  32. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +91 -0
  33. data/lib/rubocop/cop/lint/unneeded_cop_disable_directive.rb +1 -1
  34. data/lib/rubocop/cop/message_annotator.rb +16 -7
  35. data/lib/rubocop/cop/migration/department_name.rb +44 -0
  36. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  37. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
  38. data/lib/rubocop/cop/mixin/safe_mode.rb +2 -0
  39. data/lib/rubocop/cop/naming/method_name.rb +12 -1
  40. data/lib/rubocop/cop/naming/variable_name.rb +1 -0
  41. data/lib/rubocop/cop/offense.rb +18 -7
  42. data/lib/rubocop/cop/registry.rb +22 -1
  43. data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -0
  44. data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
  45. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +29 -10
  46. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  47. data/lib/rubocop/cop/style/commented_keyword.rb +8 -2
  48. data/lib/rubocop/cop/style/conditional_assignment.rb +4 -4
  49. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +8 -2
  50. data/lib/rubocop/cop/style/format_string_token.rb +10 -40
  51. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +18 -33
  52. data/lib/rubocop/cop/style/if_unless_modifier.rb +51 -15
  53. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  54. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +5 -5
  55. data/lib/rubocop/cop/style/mixin_usage.rb +11 -1
  56. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  57. data/lib/rubocop/cop/style/nested_modifier.rb +18 -2
  58. data/lib/rubocop/cop/style/or_assignment.rb +6 -1
  59. data/lib/rubocop/cop/style/parentheses_around_condition.rb +14 -0
  60. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -4
  61. data/lib/rubocop/cop/style/redundant_self.rb +18 -1
  62. data/lib/rubocop/cop/style/rescue_modifier.rb +24 -0
  63. data/lib/rubocop/cop/style/safe_navigation.rb +9 -0
  64. data/lib/rubocop/cop/style/single_line_methods.rb +8 -1
  65. data/lib/rubocop/cop/style/ternary_parentheses.rb +19 -0
  66. data/lib/rubocop/cop/utils/format_string.rb +128 -0
  67. data/lib/rubocop/cop/variable_force/variable.rb +15 -2
  68. data/lib/rubocop/core_ext/string.rb +0 -24
  69. data/lib/rubocop/formatter/emacs_style_formatter.rb +8 -5
  70. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  71. data/lib/rubocop/formatter/pacman_formatter.rb +80 -0
  72. data/lib/rubocop/formatter/simple_text_formatter.rb +9 -1
  73. data/lib/rubocop/formatter/tap_formatter.rb +9 -1
  74. data/lib/rubocop/magic_comment.rb +4 -0
  75. data/lib/rubocop/options.rb +2 -1
  76. data/lib/rubocop/runner.rb +14 -8
  77. data/lib/rubocop/version.rb +1 -1
  78. metadata +6 -3
  79. data/lib/rubocop/cop/mixin/ignored_method_patterns.rb +0 -19
@@ -120,6 +120,7 @@ module RuboCop
120
120
  corrector.remove(begin_range(node, body))
121
121
  corrector.remove(end_range(node, body))
122
122
  corrector.insert_before(method_call.loc.dot, '&')
123
+ handle_comments(corrector, method_call)
123
124
 
124
125
  add_safe_nav_to_all_methods_in_chain(corrector, method_call, body)
125
126
  end
@@ -127,6 +128,14 @@ module RuboCop
127
128
 
128
129
  private
129
130
 
131
+ def handle_comments(corrector, method_call)
132
+ return if processed_source.comments.empty?
133
+
134
+ comments = processed_source.comments.map(&:text).join("\n")
135
+ corrector.insert_before(method_call.loc.expression,
136
+ comments + "\n")
137
+ end
138
+
130
139
  def allowed_if_condition?(node)
131
140
  node.else? || node.elsif? || node.ternary?
132
141
  end
@@ -13,10 +13,17 @@ module RuboCop
13
13
  # def @table.columns; super; end
14
14
  #
15
15
  # # good
16
- # def no_op; end
17
16
  # def self.resource_class=(klass); end
18
17
  # def @table.columns; end
19
18
  #
19
+ # @example AllowIfMethodIsEmpty: true (default)
20
+ # # good
21
+ # def no_op; end
22
+ #
23
+ # @example AllowIfMethodIsEmpty: false
24
+ # # bad
25
+ # def no_op; end
26
+ #
20
27
  class SingleLineMethods < Cop
21
28
  include Alignment
22
29
 
@@ -8,6 +8,11 @@ module RuboCop
8
8
  # parentheses using `EnforcedStyle`. Omission is only enforced when
9
9
  # removing the parentheses won't cause a different behavior.
10
10
  #
11
+ # `AllowSafeAssignment` option for safe assignment.
12
+ # By safe assignment we mean putting parentheses around
13
+ # an assignment to indicate "I know I'm using an assignment
14
+ # as a condition. It's not a mistake."
15
+ #
11
16
  # @example EnforcedStyle: require_no_parentheses (default)
12
17
  # # bad
13
18
  # foo = (bar?) ? a : b
@@ -40,6 +45,15 @@ module RuboCop
40
45
  # foo = bar? ? a : b
41
46
  # foo = bar.baz? ? a : b
42
47
  # foo = (bar && baz) ? a : b
48
+ #
49
+ # @example AllowSafeAssignment: true (default)
50
+ # # good
51
+ # foo = (bar = baz) ? a : b
52
+ #
53
+ # @example AllowSafeAssignment: false
54
+ # # bad
55
+ # foo = (bar = baz) ? a : b
56
+ #
43
57
  class TernaryParentheses < Cop
44
58
  include SafeAssignment
45
59
  include ConfigurableEnforcedStyle
@@ -53,11 +67,16 @@ module RuboCop
53
67
  ' complex conditions.'
54
68
 
55
69
  def on_if(node)
70
+ return if only_closing_parenthesis_is_last_line?(node.condition)
56
71
  return unless node.ternary? && !infinite_loop? && offense?(node)
57
72
 
58
73
  add_offense(node, location: node.source_range)
59
74
  end
60
75
 
76
+ def only_closing_parenthesis_is_last_line?(condition)
77
+ condition.source.split("\n").last == ')'
78
+ end
79
+
61
80
  def autocorrect(node)
62
81
  condition = node.condition
63
82
 
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Utils
6
+ # Parses {Kernel#sprintf} format strings.
7
+ class FormatString
8
+ DIGIT_DOLLAR = /(\d+)\$/.freeze
9
+ FLAG = /[ #0+-]|#{DIGIT_DOLLAR}/.freeze
10
+ NUMBER_ARG = /\*#{DIGIT_DOLLAR}?/.freeze
11
+ NUMBER = /\d+|#{NUMBER_ARG}/.freeze
12
+ WIDTH = /(?<width>#{NUMBER})/.freeze
13
+ PRECISION = /\.(?<precision>#{NUMBER})/.freeze
14
+ TYPE = /(?<type>[bBdiouxXeEfgGaAcps])/.freeze
15
+ NAME = /<(?<name>\w+)>/.freeze
16
+ TEMPLATE_NAME = /\{(?<name>\w+)\}/.freeze
17
+
18
+ SEQUENCE = /
19
+ % (?<type>%)
20
+ | % (?<flags>#{FLAG}*)
21
+ (?:
22
+ (?: #{WIDTH}? #{PRECISION}? #{NAME}?
23
+ | #{WIDTH}? #{NAME} #{PRECISION}?
24
+ | #{NAME} (?<more_flags>#{FLAG}*) #{WIDTH}? #{PRECISION}?
25
+ ) #{TYPE}
26
+ | #{WIDTH}? #{PRECISION}? #{TEMPLATE_NAME}
27
+ )
28
+ /x.freeze
29
+
30
+ # The syntax of a format sequence is as follows.
31
+ #
32
+ # ```
33
+ # %[flags][width][.precision]type
34
+ # ```
35
+ #
36
+ # A format sequence consists of a percent sign, followed by optional
37
+ # flags, width, and precision indicators, then terminated with a field
38
+ # type character.
39
+ #
40
+ # For more complex formatting, Ruby supports a reference by name.
41
+ #
42
+ # @see https://ruby-doc.org/core-2.6.3/Kernel.html#method-i-format
43
+ class FormatSequence
44
+ attr_reader :begin_pos, :end_pos
45
+ attr_reader :flags, :width, :precision, :name, :type
46
+
47
+ def initialize(string, **opts)
48
+ @source = string
49
+ @begin_pos = opts[:begin_pos]
50
+ @end_pos = opts[:end_pos]
51
+ @flags = opts[:flags]
52
+ @width = opts[:width]
53
+ @precision = opts[:precision]
54
+ @name = opts[:name]
55
+ @type = opts[:type]
56
+ end
57
+
58
+ def percent?
59
+ type == '%'
60
+ end
61
+
62
+ def annotated?
63
+ name && @source.include?('<')
64
+ end
65
+
66
+ def template?
67
+ name && @source.include?('{')
68
+ end
69
+
70
+ # Number of arguments required for the format sequence
71
+ def arity
72
+ @source.scan('*').count + 1
73
+ end
74
+
75
+ def max_digit_dollar_num
76
+ @source.scan(DIGIT_DOLLAR).map do |(digit_dollar_num)|
77
+ digit_dollar_num.to_i
78
+ end.max
79
+ end
80
+
81
+ def style
82
+ if annotated?
83
+ :annotated
84
+ elsif template?
85
+ :template
86
+ else
87
+ :unannotated
88
+ end
89
+ end
90
+ end
91
+
92
+ def initialize(string)
93
+ @source = string
94
+ end
95
+
96
+ def format_sequences
97
+ @format_sequences ||= parse
98
+ end
99
+
100
+ def named_interpolation?
101
+ format_sequences.any?(&:name)
102
+ end
103
+
104
+ def max_digit_dollar_num
105
+ format_sequences.map(&:max_digit_dollar_num).max
106
+ end
107
+
108
+ private
109
+
110
+ def parse
111
+ @source.to_enum(:scan, SEQUENCE).map do
112
+ match = Regexp.last_match
113
+ FormatSequence.new(
114
+ match[0],
115
+ begin_pos: match.begin(0),
116
+ end_pos: match.end(0),
117
+ flags: match[:flags].to_s + match[:more_flags].to_s,
118
+ width: match[:width],
119
+ precision: match[:precision],
120
+ name: match[:name],
121
+ type: match[:type]
122
+ )
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
@@ -37,7 +37,7 @@ module RuboCop
37
37
  !@references.empty?
38
38
  end
39
39
 
40
- # rubocop:disable Metrics/AbcSize
40
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
41
41
  def reference!(node)
42
42
  reference = Reference.new(node, @scope)
43
43
  @references << reference
@@ -50,6 +50,13 @@ module RuboCop
50
50
  assignment.reference!(node)
51
51
  end
52
52
 
53
+ # Modifier if/unless conditions are special. Assignments made in
54
+ # them do not put the assigned variable in scope to the left of the
55
+ # if/unless keyword. A preceding assignment is needed to put the
56
+ # variable in scope. For this reason we skip to the next assignment
57
+ # here.
58
+ next if in_modifier_if?(assignment)
59
+
53
60
  break if !assignment.branch || assignment.branch == reference.branch
54
61
 
55
62
  unless assignment.branch.may_run_incompletely?
@@ -57,7 +64,13 @@ module RuboCop
57
64
  end
58
65
  end
59
66
  end
60
- # rubocop:enable Metrics/AbcSize
67
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
68
+
69
+ def in_modifier_if?(assignment)
70
+ parent = assignment.node.parent
71
+ parent = parent.parent if parent&.begin_type?
72
+ parent&.if_type? && parent&.modifier_form?
73
+ end
61
74
 
62
75
  def capture_with_block!
63
76
  @captured_by_block = true
@@ -20,28 +20,4 @@ class String
20
20
  empty? || strip.empty?
21
21
  end
22
22
  end
23
-
24
- unless method_defined? :strip_indent
25
- # The method strips the whitespace preceding the base indentation.
26
- # Useful for HEREDOCs and other multi-line strings.
27
- #
28
- # @example
29
- #
30
- # code = <<~END
31
- # def test
32
- # some_method
33
- # other_method
34
- # end
35
- # END
36
- #
37
- # #=> "def\n some_method\n \nother_method\nend"
38
- #
39
- # @todo Replace call sites with squiggly heredocs when required Ruby
40
- # version is >= 2.3.0
41
- def strip_indent
42
- leading_space = scan(/^[ \t]*(?=\S)/).min
43
- indent = leading_space ? leading_space.size : 0
44
- gsub(/^[ \t]{#{indent}}/, '')
45
- end
46
- end
47
23
  end
@@ -8,11 +8,14 @@ module RuboCop
8
8
  class EmacsStyleFormatter < BaseFormatter
9
9
  def file_finished(file, offenses)
10
10
  offenses.each do |o|
11
- message = if o.corrected?
12
- "[Corrected] #{o.message}"
13
- else
14
- o.message
15
- end
11
+ message =
12
+ if o.corrected_with_todo?
13
+ "[Todo] #{o.message}"
14
+ elsif o.corrected?
15
+ "[Corrected] #{o.message}"
16
+ else
17
+ o.message
18
+ end
16
19
 
17
20
  output.printf("%s:%d:%d: %s: %s\n",
18
21
  file, o.line, o.real_column, o.severity.code,
@@ -22,7 +22,8 @@ module RuboCop
22
22
  'worst' => WorstOffendersFormatter,
23
23
  'tap' => TapFormatter,
24
24
  'quiet' => QuietFormatter,
25
- 'autogenconf' => AutoGenConfigFormatter
25
+ 'autogenconf' => AutoGenConfigFormatter,
26
+ 'pacman' => PacmanFormatter
26
27
  }.freeze
27
28
 
28
29
  FORMATTER_APIS = %i[started finished].freeze
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Formatter
5
+ # This formatter prints a PACDOT per every file to be analized.
6
+ # Pacman will "eat" one PACDOT per file when no offense is detected.
7
+ # Otherwise it will print a Ghost.
8
+ # This is inspired by the Pacman formatter for RSpec by Carlos Rojas.
9
+ # https://github.com/go-labs/rspec_pacman_formatter
10
+ class PacmanFormatter < ClangStyleFormatter
11
+ include TextUtil
12
+ attr_accessor :progress_line
13
+
14
+ FALLBACK_TERMINAL_WIDTH = 80
15
+ GHOST = 'ᗣ'
16
+ PACMAN = Rainbow('ᗧ').yellow.bright
17
+ PACDOT = Rainbow('•').yellow.bright
18
+
19
+ def initialize(output, options = {})
20
+ super
21
+ @progress_line = ''
22
+ @total_files = 0
23
+ @repetitions = 0
24
+ end
25
+
26
+ def started(target_files)
27
+ super
28
+ @total_files = target_files.size
29
+ output.puts "Eating #{pluralize(target_files.size, 'file')}"
30
+ update_progress_line
31
+ end
32
+
33
+ def file_started(_file, _options)
34
+ step(PACMAN)
35
+ end
36
+
37
+ def file_finished(file, offenses)
38
+ count_stats(offenses) unless offenses.empty?
39
+ next_step(offenses)
40
+ report_file(file, offenses)
41
+ end
42
+
43
+ def next_step(offenses)
44
+ return step('.') if offenses.empty?
45
+
46
+ ghost_color = COLOR_FOR_SEVERITY[offenses.last.severity.name]
47
+ step(colorize(GHOST, ghost_color))
48
+ end
49
+
50
+ def cols
51
+ @cols ||= begin
52
+ width = `tput cols`.chomp.to_i
53
+ width.nil? || width.zero? ? FALLBACK_TERMINAL_WIDTH : width
54
+ end
55
+ end
56
+
57
+ def update_progress_line
58
+ return pacdots(@total_files) unless @total_files > cols
59
+ return pacdots(cols) unless (@total_files / cols).eql?(@repetitions)
60
+
61
+ pacdots((@total_files - (cols * @repetitions)))
62
+ end
63
+
64
+ def pacdots(number)
65
+ @progress_line = PACDOT * number
66
+ end
67
+
68
+ def step(character)
69
+ regex = /#{Regexp.quote(PACMAN)}|#{Regexp.quote(PACDOT)}/
70
+ @progress_line = @progress_line.sub(regex, character)
71
+ output.printf("%s\r", @progress_line)
72
+ return unless @progress_line[-1] =~ /ᗣ|\./
73
+
74
+ @repetitions += 1
75
+ output.puts
76
+ update_progress_line
77
+ end
78
+ end
79
+ end
80
+ end
@@ -75,7 +75,15 @@ module RuboCop
75
75
  end
76
76
 
77
77
  def message(offense)
78
- message = offense.corrected? ? green('[Corrected] ') : ''
78
+ message =
79
+ if offense.corrected_with_todo?
80
+ green('[Todo] ')
81
+ elsif offense.corrected?
82
+ green('[Corrected] ')
83
+ else
84
+ ''
85
+ end
86
+
79
87
  "#{message}#{annotate_message(offense.message)}"
80
88
  end
81
89
 
@@ -63,7 +63,15 @@ module RuboCop
63
63
  end
64
64
 
65
65
  def message(offense)
66
- message = offense.corrected? ? '[Corrected] ' : ''
66
+ message =
67
+ if offense.corrected_with_todo?
68
+ '[Todo] '
69
+ elsif offense.corrected?
70
+ '[Corrected] '
71
+ else
72
+ ''
73
+ end
74
+
67
75
  "#{message}#{annotate_message(offense.message)}"
68
76
  end
69
77
  end
@@ -42,6 +42,10 @@ module RuboCop
42
42
  frozen_string_literal == true
43
43
  end
44
44
 
45
+ def valid_literal_value?
46
+ [true, false].include?(frozen_string_literal)
47
+ end
48
+
45
49
  # Was a magic comment for the frozen string literal found?
46
50
  #
47
51
  # @return [Boolean]
@@ -394,7 +394,7 @@ module RuboCop
394
394
  "properties to generate. Default is #{MAX_EXCL}."],
395
395
  disable_uncorrectable: ['Used with --auto-correct to annotate any',
396
396
  'offenses that do not support autocorrect',
397
- 'with `rubocop:disable` comments.'],
397
+ 'with `rubocop:todo` comments.'],
398
398
  force_exclusion: ['Force excluding files specified in the',
399
399
  'configuration `Exclude` even if they are',
400
400
  'explicitly passed as arguments.'],
@@ -412,6 +412,7 @@ module RuboCop
412
412
  ' [c]lang',
413
413
  ' [d]isabled cops via inline comments',
414
414
  ' [fu]ubar',
415
+ ' [pa]cman',
415
416
  ' [e]macs',
416
417
  ' [j]son',
417
418
  ' [h]tml',