rubocop 0.74.0 → 0.75.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -1
  3. data/config/default.yml +28 -4
  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/correctors/percent_literal_corrector.rb +1 -1
  16. data/lib/rubocop/cop/generator.rb +3 -3
  17. data/lib/rubocop/cop/generator/configuration_injector.rb +9 -4
  18. data/lib/rubocop/cop/generator/require_file_injector.rb +1 -1
  19. data/lib/rubocop/cop/layout/block_alignment.rb +2 -2
  20. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +1 -1
  21. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +22 -7
  22. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -2
  23. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +2 -2
  24. data/lib/rubocop/cop/layout/extra_spacing.rb +0 -6
  25. data/lib/rubocop/cop/layout/indent_assignment.rb +10 -1
  26. data/lib/rubocop/cop/layout/indent_heredoc.rb +1 -1
  27. data/lib/rubocop/cop/layout/multiline_block_layout.rb +24 -2
  28. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +5 -1
  29. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +1 -1
  30. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +7 -0
  31. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +2 -0
  32. data/lib/rubocop/cop/lint/assignment_in_condition.rb +17 -4
  33. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  34. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +10 -36
  35. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  36. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +91 -0
  37. data/lib/rubocop/cop/lint/unneeded_cop_disable_directive.rb +1 -1
  38. data/lib/rubocop/cop/lint/unused_block_argument.rb +22 -6
  39. data/lib/rubocop/cop/lint/unused_method_argument.rb +23 -5
  40. data/lib/rubocop/cop/lint/void.rb +3 -22
  41. data/lib/rubocop/cop/message_annotator.rb +16 -7
  42. data/lib/rubocop/cop/migration/department_name.rb +44 -0
  43. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  44. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
  45. data/lib/rubocop/cop/mixin/safe_mode.rb +2 -0
  46. data/lib/rubocop/cop/naming/method_name.rb +12 -1
  47. data/lib/rubocop/cop/naming/variable_name.rb +1 -0
  48. data/lib/rubocop/cop/offense.rb +18 -7
  49. data/lib/rubocop/cop/registry.rb +22 -1
  50. data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -0
  51. data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
  52. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +29 -10
  53. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  54. data/lib/rubocop/cop/style/commented_keyword.rb +8 -2
  55. data/lib/rubocop/cop/style/conditional_assignment.rb +4 -4
  56. data/lib/rubocop/cop/style/documentation_method.rb +44 -0
  57. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +8 -2
  58. data/lib/rubocop/cop/style/expand_path_arguments.rb +1 -1
  59. data/lib/rubocop/cop/style/format_string_token.rb +18 -69
  60. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +18 -33
  61. data/lib/rubocop/cop/style/if_unless_modifier.rb +51 -15
  62. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  63. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +25 -25
  64. data/lib/rubocop/cop/style/mixin_usage.rb +11 -1
  65. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  66. data/lib/rubocop/cop/style/nested_modifier.rb +18 -2
  67. data/lib/rubocop/cop/style/or_assignment.rb +6 -1
  68. data/lib/rubocop/cop/style/parentheses_around_condition.rb +14 -0
  69. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -4
  70. data/lib/rubocop/cop/style/redundant_return.rb +12 -0
  71. data/lib/rubocop/cop/style/redundant_self.rb +18 -1
  72. data/lib/rubocop/cop/style/rescue_modifier.rb +24 -0
  73. data/lib/rubocop/cop/style/safe_navigation.rb +17 -0
  74. data/lib/rubocop/cop/style/semicolon.rb +11 -0
  75. data/lib/rubocop/cop/style/single_line_methods.rb +8 -1
  76. data/lib/rubocop/cop/style/ternary_parentheses.rb +19 -0
  77. data/lib/rubocop/cop/utils/format_string.rb +128 -0
  78. data/lib/rubocop/cop/variable_force/variable.rb +15 -2
  79. data/lib/rubocop/core_ext/string.rb +0 -24
  80. data/lib/rubocop/formatter/clang_style_formatter.rb +8 -3
  81. data/lib/rubocop/formatter/emacs_style_formatter.rb +22 -9
  82. data/lib/rubocop/formatter/file_list_formatter.rb +1 -1
  83. data/lib/rubocop/formatter/formatter_set.rb +16 -15
  84. data/lib/rubocop/formatter/pacman_formatter.rb +80 -0
  85. data/lib/rubocop/formatter/simple_text_formatter.rb +16 -4
  86. data/lib/rubocop/formatter/tap_formatter.rb +17 -4
  87. data/lib/rubocop/magic_comment.rb +4 -0
  88. data/lib/rubocop/options.rb +5 -16
  89. data/lib/rubocop/runner.rb +14 -8
  90. data/lib/rubocop/version.rb +1 -1
  91. metadata +6 -3
  92. data/lib/rubocop/cop/mixin/ignored_method_patterns.rb +0 -19
@@ -41,7 +41,7 @@ module RuboCop
41
41
 
42
42
  def on_send(node)
43
43
  to_method(node) do |receiver, to_method|
44
- next if date_time_object?(receiver)
44
+ next if receiver.nil? || date_time_object?(receiver)
45
45
 
46
46
  message = format(
47
47
  MSG,
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ #
7
+ # This cop checks for `send`, `public_send`, and `__send__` methods
8
+ # when using mix-in.
9
+ #
10
+ # `include` and `prepend` methods were private methods until Ruby 2.0,
11
+ # they were mixed-in via `send` method. This cop uses Ruby 2.1 or
12
+ # higher style that can be called by public methods.
13
+ # And `extend` method that was originally a public method is also targeted
14
+ # for style unification.
15
+ #
16
+ # @example
17
+ # # bad
18
+ # Foo.send(:include, Bar)
19
+ # Foo.send(:prepend, Bar)
20
+ # Foo.send(:extend, Bar)
21
+ #
22
+ # # bad
23
+ # Foo.public_send(:include, Bar)
24
+ # Foo.public_send(:prepend, Bar)
25
+ # Foo.public_send(:extend, Bar)
26
+ #
27
+ # # bad
28
+ # Foo.__send__(:include, Bar)
29
+ # Foo.__send__(:prepend, Bar)
30
+ # Foo.__send__(:extend, Bar)
31
+ #
32
+ # # good
33
+ # Foo.include Bar
34
+ # Foo.prepend Bar
35
+ # Foo.extend Bar
36
+ #
37
+ class SendWithMixinArgument < Cop
38
+ include RangeHelp
39
+
40
+ MSG = 'Use `%<method>s %<module_name>s` instead of `%<bad_method>s`.'
41
+ MIXIN_METHODS = %i[include prepend extend].freeze
42
+
43
+ def_node_matcher :send_with_mixin_argument?, <<~PATTERN
44
+ (send
45
+ (const _ _) {:send :public_send :__send__}
46
+ ({sym str} $#mixin_method?)
47
+ $(const _ _))
48
+ PATTERN
49
+
50
+ def on_send(node)
51
+ send_with_mixin_argument?(node) do |method, module_name|
52
+ message = message(
53
+ method, module_name.source, bad_location(node).source
54
+ )
55
+
56
+ add_offense(node, location: bad_location(node), message: message)
57
+ end
58
+ end
59
+
60
+ def autocorrect(node)
61
+ send_with_mixin_argument?(node) do |method, module_name|
62
+ lambda do |corrector|
63
+ corrector.replace(
64
+ bad_location(node), "#{method} #{module_name.source}"
65
+ )
66
+ end
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def bad_location(node)
73
+ loc = node.loc
74
+
75
+ range_between(loc.selector.begin_pos, loc.expression.end_pos)
76
+ end
77
+
78
+ def message(method, module_name, bad_method)
79
+ format(
80
+ MSG,
81
+ method: method, module_name: module_name, bad_method: bad_method
82
+ )
83
+ end
84
+
85
+ def mixin_method?(node)
86
+ MIXIN_METHODS.include?(node.to_sym)
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -155,7 +155,7 @@ module RuboCop
155
155
  end
156
156
 
157
157
  def all_disabled?(comment)
158
- comment.text =~ /rubocop\s*:\s*disable\s+all\b/
158
+ comment.text =~ /rubocop\s*:\s*(?:disable|todo)\s+all\b/
159
159
  end
160
160
 
161
161
  def ignore_offense?(disabled_ranges, line_range)
@@ -6,9 +6,7 @@ module RuboCop
6
6
  # This cop checks for unused block arguments.
7
7
  #
8
8
  # @example
9
- #
10
9
  # # bad
11
- #
12
10
  # do_something do |used, unused|
13
11
  # puts used
14
12
  # end
@@ -21,10 +19,7 @@ module RuboCop
21
19
  # puts :baz
22
20
  # end
23
21
  #
24
- # @example
25
- #
26
- # #good
27
- #
22
+ # # good
28
23
  # do_something do |used, _unused|
29
24
  # puts used
30
25
  # end
@@ -36,6 +31,27 @@ module RuboCop
36
31
  # define_method(:foo) do |_bar|
37
32
  # puts :baz
38
33
  # end
34
+ #
35
+ # @example IgnoreEmptyBlocks: true (default)
36
+ # # good
37
+ # do_something { |unused| }
38
+ #
39
+ # @example IgnoreEmptyBlocks: false
40
+ # # bad
41
+ # do_something { |unused| }
42
+ #
43
+ # @example AllowUnusedKeywordArguments: false (default)
44
+ # # bad
45
+ # do_something do |unused: 42|
46
+ # foo
47
+ # end
48
+ #
49
+ # @example AllowUnusedKeywordArguments: true
50
+ # # good
51
+ # do_something do |unused: 42|
52
+ # foo
53
+ # end
54
+ #
39
55
  class UnusedBlockArgument < Cop
40
56
  include UnusedArgument
41
57
 
@@ -6,20 +6,38 @@ module RuboCop
6
6
  # This cop checks for unused method arguments.
7
7
  #
8
8
  # @example
9
- #
10
9
  # # bad
11
- #
12
10
  # def some_method(used, unused, _unused_but_allowed)
13
11
  # puts used
14
12
  # end
15
13
  #
16
- # @example
17
- #
18
14
  # # good
19
- #
20
15
  # def some_method(used, _unused, _unused_but_allowed)
21
16
  # puts used
22
17
  # end
18
+ #
19
+ # @example AllowUnusedKeywordArguments: false (default)
20
+ # # bad
21
+ # def do_something(used, unused: 42)
22
+ # used
23
+ # end
24
+ #
25
+ # @example AllowUnusedKeywordArguments: true
26
+ # # good
27
+ # def do_something(used, unused: 42)
28
+ # used
29
+ # end
30
+ #
31
+ # @example IgnoreEmptyMethods: true (default)
32
+ # # good
33
+ # def do_something(unused)
34
+ # end
35
+ #
36
+ # @example IgnoreEmptyMethods: false
37
+ # # bad
38
+ # def do_something(unused)
39
+ # end
40
+ #
23
41
  class UnusedMethodArgument < Cop
24
42
  include UnusedArgument
25
43
 
@@ -6,55 +6,36 @@ module RuboCop
6
6
  # This cop checks for operators, variables, literals, and nonmutating
7
7
  # methods used in void context.
8
8
  #
9
- # @example
10
- #
9
+ # @example CheckForMethodsWithNoSideEffects: false (default)
11
10
  # # bad
12
- #
13
11
  # def some_method
14
12
  # some_num * 10
15
13
  # do_something
16
14
  # end
17
15
  #
18
- # @example
19
- #
20
- # # bad
21
- #
22
16
  # def some_method(some_var)
23
17
  # some_var
24
18
  # do_something
25
19
  # end
26
20
  #
27
- # @example
28
- #
29
- # # bad, when CheckForMethodsWithNoSideEffects is set true
30
- #
21
+ # @example CheckForMethodsWithNoSideEffects: true
22
+ # # bad
31
23
  # def some_method(some_array)
32
24
  # some_array.sort
33
25
  # do_something(some_array)
34
26
  # end
35
27
  #
36
- # @example
37
- #
38
28
  # # good
39
- #
40
29
  # def some_method
41
30
  # do_something
42
31
  # some_num * 10
43
32
  # end
44
33
  #
45
- # @example
46
- #
47
- # # good
48
- #
49
34
  # def some_method(some_var)
50
35
  # do_something
51
36
  # some_var
52
37
  # end
53
38
  #
54
- # @example
55
- #
56
- # # good, when CheckForMethodsWithNoSideEffects is set true
57
- #
58
39
  # def some_method(some_array)
59
40
  # some_array.sort!
60
41
  # do_something(some_array)
@@ -9,11 +9,11 @@ module RuboCop
9
9
  #
10
10
  # @example
11
11
  # RuboCop::Cop::MessageAnnotator.new(
12
- # config, cop_config, @options
13
- # ).annotate('message', 'Cop/CopName')
12
+ # config, cop_name, cop_config, @options
13
+ # ).annotate('message')
14
14
  # #=> 'Cop/CopName: message (http://example.org/styleguide)'
15
15
  class MessageAnnotator
16
- attr_reader :options, :config, :cop_config
16
+ attr_reader :options, :config, :cop_name, :cop_config
17
17
 
18
18
  @style_guide_urls = {}
19
19
 
@@ -29,6 +29,7 @@ module RuboCop
29
29
  # :ExtraDetails [Boolean] Include cop details
30
30
  # :DisplayCopNames [Boolean] Include cop name
31
31
  #
32
+ # @param [String] cop_name for specific cop name
32
33
  # @param [Hash] cop_config configs for specific cop, from config#for_cop
33
34
  # @option cop_config [String] :StyleGuide Extension of base styleguide URL
34
35
  # @option cop_config [String] :Reference Full reference URL
@@ -43,8 +44,9 @@ module RuboCop
43
44
  # Include debug output
44
45
  # @option options [Boolean] :display_cop_names
45
46
  # Include cop name
46
- def initialize(config, cop_config, options)
47
+ def initialize(config, cop_name, cop_config, options)
47
48
  @config = config
49
+ @cop_name = cop_name
48
50
  @cop_config = cop_config || {}
49
51
  @options = options
50
52
  end
@@ -53,8 +55,8 @@ module RuboCop
53
55
  # based on params passed into initializer
54
56
  #
55
57
  # @return [String] annotated message
56
- def annotate(message, name)
57
- message = "#{name}: #{message}" if display_cop_names?
58
+ def annotate(message)
59
+ message = "#{cop_name}: #{message}" if display_cop_names?
58
60
  message += " #{details}" if extra_details? && details
59
61
  if display_style_guide?
60
62
  links = urls.join(', ')
@@ -74,7 +76,7 @@ module RuboCop
74
76
  return nil if url.nil? || url.empty?
75
77
 
76
78
  self.class.style_guide_urls[url] ||= begin
77
- base_url = config.for_all_cops['StyleGuideBaseURL']
79
+ base_url = style_guide_base_url
78
80
  if base_url.nil? || base_url.empty?
79
81
  url
80
82
  else
@@ -83,6 +85,13 @@ module RuboCop
83
85
  end
84
86
  end
85
87
 
88
+ def style_guide_base_url
89
+ department_name = cop_name.split('/').first
90
+
91
+ config.for_department(department_name)['StyleGuideBaseURL'] ||
92
+ config.for_all_cops['StyleGuideBaseURL']
93
+ end
94
+
86
95
  def display_style_guide?
87
96
  (options[:display_style_guide] ||
88
97
  config.for_all_cops['DisplayStyleGuide']) &&
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Migration
6
+ # Check that cop names in rubocop:disable comments are given with
7
+ # department name.
8
+ class DepartmentName < Cop
9
+ include RangeHelp
10
+
11
+ MSG = 'Department name is missing.'
12
+
13
+ def investigate(processed_source)
14
+ processed_source.each_comment do |comment|
15
+ next if comment.text !~ /\A(# *rubocop:((dis|en)able|todo) +)(.*)/
16
+
17
+ offset = Regexp.last_match(1).length
18
+ Regexp.last_match(4).scan(%r{[\w/]+|\W+}) do |name|
19
+ check_cop_name(name, comment, offset)
20
+ offset += name.length
21
+ end
22
+ end
23
+ end
24
+
25
+ def autocorrect(range)
26
+ shall_warn = false
27
+ qualified_cop_name = Cop.registry.qualified_cop_name(range.source,
28
+ nil, shall_warn)
29
+ ->(corrector) { corrector.replace(range, qualified_cop_name) }
30
+ end
31
+
32
+ private
33
+
34
+ def check_cop_name(name, comment, offset)
35
+ return if name !~ /^[A-Z]/ || name =~ %r{/}
36
+
37
+ start = comment.location.expression.begin_pos + offset
38
+ range = range_between(start, start + name.length)
39
+ add_offense(range, location: range)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -13,7 +13,7 @@ module RuboCop
13
13
 
14
14
  def configured_indentation_width
15
15
  cop_config['IndentationWidth'] ||
16
- config.for_cop('IndentationWidth')['Width']
16
+ config.for_cop('Layout/IndentationWidth')['Width']
17
17
  end
18
18
 
19
19
  def indentation(node)
@@ -12,7 +12,7 @@ module RuboCop
12
12
 
13
13
  def frozen_string_literal_comment_exists?
14
14
  leading_comment_lines.any? do |line|
15
- MagicComment.parse(line).frozen_string_literal_specified?
15
+ MagicComment.parse(line).valid_literal_value?
16
16
  end
17
17
  end
18
18
 
@@ -3,6 +3,8 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  # Common functionality for Rails safe mode.
6
+ #
7
+ # This module can be removed from RuboCop 0.76.
6
8
  module SafeMode
7
9
  private
8
10
 
@@ -6,6 +6,15 @@ module RuboCop
6
6
  # This cop makes sure that all methods use the configured style,
7
7
  # snake_case or camelCase, for their names.
8
8
  #
9
+ # This cop has `IgnoredPatterns` configuration option.
10
+ #
11
+ # Naming/MethodName:
12
+ # IgnoredPatterns:
13
+ # - '\A\s*onSelectionBulkChange\s*'
14
+ # - '\A\s*onSelectionCleared\s*'
15
+ #
16
+ # Method names matching patterns are always allowed.
17
+ #
9
18
  # @example EnforcedStyle: snake_case (default)
10
19
  # # bad
11
20
  # def fooBar; end
@@ -21,11 +30,13 @@ module RuboCop
21
30
  # def fooBar; end
22
31
  class MethodName < Cop
23
32
  include ConfigurableNaming
33
+ include IgnoredPattern
24
34
 
25
35
  MSG = 'Use %<style>s for method names.'
26
36
 
27
37
  def on_def(node)
28
- return if node.operator_method?
38
+ return if node.operator_method? ||
39
+ matches_ignored_pattern?(node.method_name)
29
40
 
30
41
  check_name(node, node.method_name, node.loc.name)
31
42
  end
@@ -39,6 +39,7 @@ module RuboCop
39
39
  alias on_kwarg on_lvasgn
40
40
  alias on_kwrestarg on_lvasgn
41
41
  alias on_blockarg on_lvasgn
42
+ alias on_lvar on_lvasgn
42
43
 
43
44
  private
44
45
 
@@ -67,23 +67,34 @@ module RuboCop
67
67
 
68
68
  # @api public
69
69
  #
70
- # @!attribute [r] corrected
70
+ # @!attribute [r] corrected?
71
71
  #
72
72
  # @return [Boolean]
73
- # whether this offense is automatically corrected.
74
- def corrected
75
- @status == :corrected
73
+ # whether this offense is automatically corrected via
74
+ # autocorrect or a todo.
75
+ def corrected?
76
+ @status == :corrected || @status == :corrected_with_todo
77
+ end
78
+
79
+ # @api public
80
+ #
81
+ # @!attribute [r] corrected_with_todo?
82
+ #
83
+ # @return [Boolean]
84
+ # whether this offense is automatically disabled via a todo.
85
+ def corrected_with_todo?
86
+ @status == :corrected_with_todo
76
87
  end
77
- alias corrected? corrected
78
88
 
79
89
  # @api public
80
90
  #
81
91
  # @!attribute [r] disabled?
82
92
  #
83
93
  # @return [Boolean]
84
- # whether this offense was locally disabled where it occurred
94
+ # whether this offense was locally disabled with a
95
+ # disable or todo where it occurred.
85
96
  def disabled?
86
- @status == :disabled
97
+ @status == :disabled || @status == :todo
87
98
  end
88
99
 
89
100
  # @api public