rubocop 1.14.0 → 1.18.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 (98) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -1
  3. data/config/default.yml +120 -34
  4. data/lib/rubocop.rb +8 -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/example_description.rb +1 -1
  14. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
  15. data/lib/rubocop/cop/layout/argument_alignment.rb +30 -12
  16. data/lib/rubocop/cop/layout/array_alignment.rb +2 -2
  17. data/lib/rubocop/cop/layout/block_alignment.rb +1 -1
  18. data/lib/rubocop/cop/layout/case_indentation.rb +57 -9
  19. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +7 -1
  20. data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
  21. data/lib/rubocop/cop/layout/dot_position.rb +7 -1
  22. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +13 -15
  23. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -2
  24. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +14 -2
  25. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
  26. data/lib/rubocop/cop/layout/hash_alignment.rb +40 -14
  27. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  28. data/lib/rubocop/cop/layout/indentation_width.rb +13 -2
  29. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +122 -0
  30. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +6 -6
  31. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +2 -2
  32. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +6 -6
  33. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +6 -6
  34. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +6 -6
  35. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +3 -3
  36. data/lib/rubocop/cop/layout/parameter_alignment.rb +2 -2
  37. data/lib/rubocop/cop/layout/redundant_line_break.rb +11 -9
  38. data/lib/rubocop/cop/layout/space_around_keyword.rb +28 -0
  39. data/lib/rubocop/cop/layout/space_around_operators.rb +7 -1
  40. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -0
  41. data/lib/rubocop/cop/lint/empty_block.rb +18 -2
  42. data/lib/rubocop/cop/lint/empty_in_pattern.rb +62 -0
  43. data/lib/rubocop/cop/lint/literal_as_condition.rb +13 -1
  44. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +32 -17
  45. data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
  46. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  47. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  48. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +105 -74
  49. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +5 -0
  50. data/lib/rubocop/cop/lint/symbol_conversion.rb +3 -13
  51. data/lib/rubocop/cop/lint/unused_block_argument.rb +1 -1
  52. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
  53. data/lib/rubocop/cop/lint/void.rb +1 -1
  54. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  55. data/lib/rubocop/cop/migration/department_name.rb +3 -1
  56. data/lib/rubocop/cop/mixin/check_line_breakable.rb +28 -3
  57. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +6 -0
  58. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +14 -3
  59. data/lib/rubocop/cop/mixin/string_literals_help.rb +2 -4
  60. data/lib/rubocop/cop/mixin/symbol_help.rb +13 -0
  61. data/lib/rubocop/cop/naming/inclusive_language.rb +249 -0
  62. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +2 -2
  63. data/lib/rubocop/cop/style/class_and_module_children.rb +28 -2
  64. data/lib/rubocop/cop/style/empty_literal.rb +8 -1
  65. data/lib/rubocop/cop/style/hash_each_methods.rb +18 -1
  66. data/lib/rubocop/cop/style/identical_conditional_branches.rb +58 -8
  67. data/lib/rubocop/cop/style/in_pattern_then.rb +56 -0
  68. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -1
  69. data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +62 -0
  70. data/lib/rubocop/cop/style/multiline_when_then.rb +2 -11
  71. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
  72. data/lib/rubocop/cop/style/nil_lambda.rb +29 -12
  73. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  74. data/lib/rubocop/cop/style/quoted_symbols.rb +110 -0
  75. data/lib/rubocop/cop/style/raise_args.rb +2 -0
  76. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  77. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +1 -1
  78. data/lib/rubocop/cop/style/redundant_self.rb +24 -2
  79. data/lib/rubocop/cop/style/regexp_literal.rb +10 -1
  80. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  81. data/lib/rubocop/cop/style/string_concatenation.rb +32 -5
  82. data/lib/rubocop/cop/style/string_literals.rb +3 -2
  83. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +1 -0
  84. data/lib/rubocop/cop/style/swap_values.rb +1 -1
  85. data/lib/rubocop/cop/style/top_level_method_definition.rb +83 -0
  86. data/lib/rubocop/cop/style/trivial_accessors.rb +65 -0
  87. data/lib/rubocop/cop/style/unpack_first.rb +1 -1
  88. data/lib/rubocop/cop/style/when_then.rb +6 -2
  89. data/lib/rubocop/cop/variable_force/variable_table.rb +1 -1
  90. data/lib/rubocop/directive_comment.rb +58 -6
  91. data/lib/rubocop/formatter/junit_formatter.rb +21 -6
  92. data/lib/rubocop/options.rb +18 -24
  93. data/lib/rubocop/rake_task.rb +1 -1
  94. data/lib/rubocop/remote_config.rb +10 -2
  95. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  96. data/lib/rubocop/rspec/expect_offense.rb +1 -1
  97. data/lib/rubocop/version.rb +1 -1
  98. metadata +13 -5
@@ -23,6 +23,7 @@ module RuboCop
23
23
  # # good
24
24
  # raise StandardError, 'message'
25
25
  # fail 'message'
26
+ # raise MyCustomError
26
27
  # raise MyCustomError.new(arg1, arg2, arg3)
27
28
  # raise MyKwArgError.new(key1: val1, key2: val2)
28
29
  #
@@ -37,6 +38,7 @@ module RuboCop
37
38
  #
38
39
  # # good
39
40
  # raise StandardError.new('message')
41
+ # raise MyCustomError
40
42
  # raise MyCustomError.new(arg1, arg2, arg3)
41
43
  # fail 'message'
42
44
  class RaiseArgs < Base
@@ -110,7 +110,7 @@ module RuboCop
110
110
  first_child = node.children.first
111
111
 
112
112
  source = first_child.source
113
- source = "(#{source})" if first_child.if_type?
113
+ source = "(#{source})" if first_child.if_type? && first_child.modifier_form?
114
114
 
115
115
  corrector.replace(offense_range, source)
116
116
  corrector.remove(range_between(offense_range.end_pos, first_child.source_range.end_pos))
@@ -32,7 +32,7 @@ module RuboCop
32
32
 
33
33
  REQUIRES_ESCAPE_OUTSIDE_CHAR_CLASS_CHARS = '.*+?{}()|$'.chars.freeze
34
34
  MSG_REDUNDANT_CHARACTER_CLASS = 'Redundant single-element character class, ' \
35
- '`%<char_class>s` can be replaced with `%<element>s`.'
35
+ '`%<char_class>s` can be replaced with `%<element>s`.'
36
36
 
37
37
  def on_regexp(node)
38
38
  each_redundant_character_class(node) do |loc|
@@ -92,7 +92,7 @@ module RuboCop
92
92
 
93
93
  def on_masgn(node)
94
94
  lhs, rhs = *node
95
- lhs.children.each { |child| add_lhs_to_local_variables_scopes(rhs, child.to_a.first) }
95
+ add_masgn_lhs_variables(rhs, lhs)
96
96
  end
97
97
 
98
98
  def on_lvasgn(node)
@@ -106,7 +106,7 @@ module RuboCop
106
106
 
107
107
  return if allowed_send_node?(node)
108
108
 
109
- add_offense(node) do |corrector|
109
+ add_offense(node.receiver) do |corrector|
110
110
  corrector.remove(node.receiver)
111
111
  corrector.remove(node.loc.dot)
112
112
  end
@@ -116,6 +116,22 @@ module RuboCop
116
116
  add_scope(node, @local_variables_scopes[node])
117
117
  end
118
118
 
119
+ def on_if(node)
120
+ # Allow conditional nodes to use `self` in the condition if that variable
121
+ # name is used in an `lvasgn` or `masgn` within the `if`.
122
+ node.child_nodes.each do |child_node|
123
+ lhs, _rhs = *child_node
124
+
125
+ if child_node.lvasgn_type?
126
+ add_lhs_to_local_variables_scopes(node.condition, lhs)
127
+ elsif child_node.masgn_type?
128
+ add_masgn_lhs_variables(node.condition, lhs)
129
+ end
130
+ end
131
+ end
132
+ alias on_while on_if
133
+ alias on_until on_if
134
+
119
135
  private
120
136
 
121
137
  def add_scope(node, local_variables = [])
@@ -163,6 +179,12 @@ module RuboCop
163
179
  @local_variables_scopes[rhs] << lhs
164
180
  end
165
181
  end
182
+
183
+ def add_masgn_lhs_variables(rhs, lhs)
184
+ lhs.children.each do |child|
185
+ add_lhs_to_local_variables_scopes(rhs, child.to_a.first)
186
+ end
187
+ end
166
188
  end
167
189
  end
168
190
  end
@@ -117,7 +117,7 @@ module RuboCop
117
117
  def allowed_percent_r_literal?(node)
118
118
  style == :slashes && contains_disallowed_slash?(node) ||
119
119
  style == :percent_r ||
120
- allowed_mixed_percent_r?(node)
120
+ allowed_mixed_percent_r?(node) || allowed_omit_parentheses_with_percent_r_literal?(node)
121
121
  end
122
122
 
123
123
  def allowed_mixed_percent_r?(node)
@@ -149,6 +149,15 @@ module RuboCop
149
149
  config.for_cop('Style/PercentLiteralDelimiters') ['PreferredDelimiters']['%r'].chars
150
150
  end
151
151
 
152
+ def allowed_omit_parentheses_with_percent_r_literal?(node)
153
+ return false unless node.parent&.call_type?
154
+ return true if node.content.start_with?(' ')
155
+
156
+ enforced_style = config.for_cop('Style/MethodCallWithArgsParentheses')['EnforcedStyle']
157
+
158
+ enforced_style == 'omit_parentheses'
159
+ end
160
+
152
161
  def correct_delimiters(node, corrector)
153
162
  replacement = calculate_replacement(node)
154
163
  corrector.replace(node.loc.begin, replacement.first)
@@ -53,10 +53,10 @@ module RuboCop
53
53
  extend AutoCorrector
54
54
 
55
55
  MSG_BOTH = 'Prefer `%<prefer>s` from the stdlib \'English\' ' \
56
- 'module (don\'t forget to require it) or `%<regular>s` over ' \
57
- '`%<global>s`.'
56
+ 'module (don\'t forget to require it) or `%<regular>s` over ' \
57
+ '`%<global>s`.'
58
58
  MSG_ENGLISH = 'Prefer `%<prefer>s` from the stdlib \'English\' ' \
59
- 'module (don\'t forget to require it) over `%<global>s`.'
59
+ 'module (don\'t forget to require it) over `%<global>s`.'
60
60
  MSG_REGULAR = 'Prefer `%<prefer>s` over `%<global>s`.'
61
61
 
62
62
  ENGLISH_VARS = { # rubocop:disable Style/MutableConstant
@@ -15,18 +15,37 @@ module RuboCop
15
15
  # lines, this cop does not register an offense; instead,
16
16
  # `Style/LineEndConcatenation` will pick up the offense if enabled.
17
17
  #
18
- # @example
18
+ # Two modes are supported:
19
+ # 1. `aggressive` style checks and corrects all occurrences of `+` where
20
+ # either the left or right side of `+` is a string literal.
21
+ # 2. `conservative` style on the other hand, checks and corrects only if
22
+ # left side (receiver of `+` method call) is a string literal.
23
+ # This is useful when the receiver is some expression that returns string like `Pathname`
24
+ # instead of a string literal.
25
+ #
26
+ # @example Mode: aggressive (default)
19
27
  # # bad
20
28
  # email_with_name = user.name + ' <' + user.email + '>'
29
+ # Pathname.new('/') + 'test'
21
30
  #
22
31
  # # good
23
32
  # email_with_name = "#{user.name} <#{user.email}>"
24
33
  # email_with_name = format('%s <%s>', user.name, user.email)
34
+ # "#{Pathname.new('/')}test"
25
35
  #
26
36
  # # accepted, line-end concatenation
27
37
  # name = 'First' +
28
38
  # 'Last'
29
39
  #
40
+ # @example Mode: conservative
41
+ # # bad
42
+ # 'Hello' + user.name
43
+ #
44
+ # # good
45
+ # "Hello #{user.name}"
46
+ # user.name + '!!'
47
+ # Pathname.new('/') + 'test'
48
+ #
30
49
  class StringConcatenation < Base
31
50
  include Util
32
51
  include RangeHelp
@@ -52,10 +71,15 @@ module RuboCop
52
71
  return if line_end_concatenation?(node)
53
72
 
54
73
  topmost_plus_node = find_topmost_plus_node(node)
74
+ parts = collect_parts(topmost_plus_node)
75
+ return unless parts[0..-2].any? { |receiver_node| offensive_for_mode?(receiver_node) }
55
76
 
56
- parts = []
57
- collect_parts(topmost_plus_node, parts)
77
+ register_offense(topmost_plus_node, parts)
78
+ end
79
+
80
+ private
58
81
 
82
+ def register_offense(topmost_plus_node, parts)
59
83
  add_offense(topmost_plus_node) do |corrector|
60
84
  correctable_parts = parts.none? { |part| uncorrectable?(part) }
61
85
  if correctable_parts && !corrected_ancestor?(topmost_plus_node)
@@ -67,7 +91,10 @@ module RuboCop
67
91
  end
68
92
  end
69
93
 
70
- private
94
+ def offensive_for_mode?(receiver_node)
95
+ mode = cop_config['Mode'].to_sym
96
+ mode == :aggressive || mode == :conservative && receiver_node.str_type?
97
+ end
71
98
 
72
99
  def line_end_concatenation?(node)
73
100
  # If the concatenation happens at the end of the line,
@@ -87,7 +114,7 @@ module RuboCop
87
114
  current
88
115
  end
89
116
 
90
- def collect_parts(node, parts)
117
+ def collect_parts(node, parts = [])
91
118
  return unless node
92
119
 
93
120
  if plus_node?(node)
@@ -29,6 +29,7 @@ module RuboCop
29
29
  class StringLiterals < Base
30
30
  include ConfigurableEnforcedStyle
31
31
  include StringLiteralsHelp
32
+ include StringHelp
32
33
  extend AutoCorrector
33
34
 
34
35
  MSG_INCONSISTENT = 'Inconsistent quote style.'
@@ -86,10 +87,10 @@ module RuboCop
86
87
  def message(_node)
87
88
  if style == :single_quotes
88
89
  "Prefer single-quoted strings when you don't need string " \
89
- 'interpolation or special symbols.'
90
+ 'interpolation or special symbols.'
90
91
  else
91
92
  'Prefer double-quoted strings unless you need single quotes to ' \
92
- 'avoid extra backslashes for escaping.'
93
+ 'avoid extra backslashes for escaping.'
93
94
  end
94
95
  end
95
96
 
@@ -22,6 +22,7 @@ module RuboCop
22
22
  class StringLiteralsInInterpolation < Base
23
23
  include ConfigurableEnforcedStyle
24
24
  include StringLiteralsHelp
25
+ include StringHelp
25
26
  extend AutoCorrector
26
27
 
27
28
  def autocorrect(corrector, node)
@@ -21,7 +21,7 @@ module RuboCop
21
21
  extend AutoCorrector
22
22
 
23
23
  MSG = 'Replace this and assignments at lines %<x_line>d '\
24
- 'and %<y_line>d with `%<replacement>s`.'
24
+ 'and %<y_line>d with `%<replacement>s`.'
25
25
 
26
26
  SIMPLE_ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn casgn].to_set.freeze
27
27
 
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Newcomers to ruby applications may write top-level methods,
7
+ # when ideally they should be organized in appropriate classes or modules.
8
+ # This cop looks for definitions of top-level methods and warns about them.
9
+ #
10
+ # However for ruby scripts it is perfectly fine to use top-level methods.
11
+ # Hence this cop is disabled by default.
12
+ #
13
+ # @example
14
+ # # bad
15
+ # def some_method
16
+ # end
17
+ #
18
+ # # bad
19
+ # def self.some_method
20
+ # end
21
+ #
22
+ # # bad
23
+ # define_method(:foo) { puts 1 }
24
+ #
25
+ # # good
26
+ # module Foo
27
+ # def some_method
28
+ # end
29
+ # end
30
+ #
31
+ # # good
32
+ # class Foo
33
+ # def self.some_method
34
+ # end
35
+ # end
36
+ #
37
+ # # good
38
+ # Struct.new do
39
+ # def some_method
40
+ # end
41
+ # end
42
+ #
43
+ # # good
44
+ # class Foo
45
+ # define_method(:foo) { puts 1 }
46
+ # end
47
+ class TopLevelMethodDefinition < Base
48
+ MSG = 'Do not define methods at the top-level.'
49
+
50
+ RESTRICT_ON_SEND = %i[define_method].freeze
51
+
52
+ def on_def(node)
53
+ return unless top_level_method_definition?(node)
54
+
55
+ add_offense(node)
56
+ end
57
+ alias on_defs on_def
58
+ alias on_send on_def
59
+
60
+ def on_block(node)
61
+ return unless define_method_block?(node) && top_level_method_definition?(node)
62
+
63
+ add_offense(node)
64
+ end
65
+
66
+ private
67
+
68
+ def top_level_method_definition?(node)
69
+ if node.parent&.begin_type?
70
+ node.parent.root?
71
+ else
72
+ node.root?
73
+ end
74
+ end
75
+
76
+ # @!method define_method_block?(node)
77
+ def_node_matcher :define_method_block?, <<~PATTERN
78
+ (block (send _ {:define_method} _) ...)
79
+ PATTERN
80
+ end
81
+ end
82
+ end
83
+ end
@@ -27,6 +27,71 @@ module RuboCop
27
27
  # class << self
28
28
  # attr_reader :baz
29
29
  # end
30
+ #
31
+ # @example ExactNameMatch: true (default)
32
+ # # good
33
+ # def name
34
+ # @other_name
35
+ # end
36
+ #
37
+ # @example ExactNameMatch: false
38
+ # # bad
39
+ # def name
40
+ # @other_name
41
+ # end
42
+ #
43
+ # @example AllowPredicates: true (default)
44
+ # # good
45
+ # def foo?
46
+ # @foo
47
+ # end
48
+ #
49
+ # @example AllowPredicates: false
50
+ # # bad
51
+ # def foo?
52
+ # @foo
53
+ # end
54
+ #
55
+ # # good
56
+ # attr_reader :foo
57
+ #
58
+ # @example AllowDSLWriters: true (default)
59
+ # # good
60
+ # def on_exception(action)
61
+ # @on_exception=action
62
+ # end
63
+ #
64
+ # @example AllowDSLWriters: false
65
+ # # bad
66
+ # def on_exception(action)
67
+ # @on_exception=action
68
+ # end
69
+ #
70
+ # # good
71
+ # attr_writer :on_exception
72
+ #
73
+ # @example IgnoreClassMethods: false (default)
74
+ # # bad
75
+ # def self.foo
76
+ # @foo
77
+ # end
78
+ #
79
+ # # good
80
+ # class << self
81
+ # attr_reader :foo
82
+ # end
83
+ #
84
+ # @example IgnoreClassMethods: true
85
+ # # good
86
+ # def self.foo
87
+ # @foo
88
+ # end
89
+ #
90
+ # @example AllowedMethods: ['allowed_method']
91
+ # # good
92
+ # def allowed_method
93
+ # @foo
94
+ # end
30
95
  class TrivialAccessors < Base
31
96
  include AllowedMethods
32
97
  extend AutoCorrector
@@ -21,7 +21,7 @@ module RuboCop
21
21
  extend AutoCorrector
22
22
 
23
23
  MSG = 'Use `%<receiver>s.unpack1(%<format>s)` instead of '\
24
- '`%<receiver>s.unpack(%<format>s)%<method>s`.'
24
+ '`%<receiver>s.unpack(%<format>s)%<method>s`.'
25
25
  RESTRICT_ON_SEND = %i[first [] slice at].freeze
26
26
 
27
27
  # @!method unpack_and_first_element?(node)
@@ -20,12 +20,16 @@ module RuboCop
20
20
  class WhenThen < Base
21
21
  extend AutoCorrector
22
22
 
23
- MSG = 'Do not use `when x;`. Use `when x then` instead.'
23
+ MSG = 'Do not use `when %<expression>s;`. Use `when %<expression>s then` instead.'
24
24
 
25
25
  def on_when(node)
26
26
  return if node.multiline? || node.then? || !node.body
27
27
 
28
- add_offense(node.loc.begin) { |corrector| corrector.replace(node.loc.begin, ' then') }
28
+ message = format(MSG, expression: node.conditions.map(&:source).join(', '))
29
+
30
+ add_offense(node.loc.begin, message: message) do |corrector|
31
+ corrector.replace(node.loc.begin, ' then')
32
+ end
29
33
  end
30
34
  end
31
35
  end
@@ -58,7 +58,7 @@ module RuboCop
58
58
 
59
59
  unless variable
60
60
  raise "Assigning to undeclared local variable \"#{name}\" " \
61
- "at #{node.source_range}, #{node.inspect}"
61
+ "at #{node.source_range}, #{node.inspect}"
62
62
  end
63
63
 
64
64
  variable.assign(node)
@@ -6,7 +6,9 @@ module RuboCop
6
6
  # cops it contains.
7
7
  class DirectiveComment
8
8
  # @api private
9
- REDUNDANT_COP = 'Lint/RedundantCopDisableDirective'
9
+ REDUNDANT_DIRECTIVE_COP_DEPARTMENT = 'Lint'
10
+ # @api private
11
+ REDUNDANT_DIRECTIVE_COP = "#{REDUNDANT_DIRECTIVE_COP_DEPARTMENT}/RedundantCopDisableDirective"
10
12
  # @api private
11
13
  COP_NAME_PATTERN = '([A-Z]\w+/)*(?:[A-Z]\w+)'
12
14
  # @api private
@@ -23,10 +25,11 @@ module RuboCop
23
25
  line.split(DIRECTIVE_COMMENT_REGEXP).first
24
26
  end
25
27
 
26
- attr_reader :comment, :mode, :cops
28
+ attr_reader :comment, :cop_registry, :mode, :cops
27
29
 
28
- def initialize(comment)
30
+ def initialize(comment, cop_registry = Cop::Registry.global)
29
31
  @comment = comment
32
+ @cop_registry = cop_registry
30
33
  @mode, @cops = match_captures
31
34
  end
32
35
 
@@ -41,7 +44,11 @@ module RuboCop
41
44
  end
42
45
 
43
46
  def range
44
- comment.location.expression
47
+ match = comment.text.match(DIRECTIVE_COMMENT_REGEXP)
48
+ begin_pos = comment.loc.expression.begin_pos
49
+ Parser::Source::Range.new(
50
+ comment.loc.expression.source_buffer, begin_pos + match.begin(0), begin_pos + match.end(0)
51
+ )
45
52
  end
46
53
 
47
54
  # Returns match captures to directive comment pattern
@@ -64,6 +71,11 @@ module RuboCop
64
71
  !disabled? && all_cops?
65
72
  end
66
73
 
74
+ # Checks if this directive disables all cops
75
+ def disabled_all?
76
+ disabled? && all_cops?
77
+ end
78
+
67
79
  # Checks if all cops specified in this directive
68
80
  def all_cops?
69
81
  cops == 'all'
@@ -74,6 +86,26 @@ module RuboCop
74
86
  @cop_names ||= all_cops? ? all_cop_names : parsed_cop_names
75
87
  end
76
88
 
89
+ # Returns array of specified in this directive department names
90
+ # when all department disabled
91
+ def department_names
92
+ splitted_cops_string.select { |cop| department?(cop) }
93
+ end
94
+
95
+ # Checks if directive departments include cop
96
+ def in_directive_department?(cop)
97
+ department_names.any? { |department| cop.start_with?(department) }
98
+ end
99
+
100
+ # Checks if cop department has already used in directive comment
101
+ def overridden_by_department?(cop)
102
+ in_directive_department?(cop) && splitted_cops_string.include?(cop)
103
+ end
104
+
105
+ def directive_count
106
+ splitted_cops_string.count
107
+ end
108
+
77
109
  # Returns line number for directive
78
110
  def line_number
79
111
  comment.loc.expression.line
@@ -81,12 +113,32 @@ module RuboCop
81
113
 
82
114
  private
83
115
 
84
- def parsed_cop_names
116
+ def splitted_cops_string
85
117
  (cops || '').split(/,\s*/)
86
118
  end
87
119
 
120
+ def parsed_cop_names
121
+ splitted_cops_string.map do |name|
122
+ department?(name) ? cop_names_for_department(name) : name
123
+ end.flatten
124
+ end
125
+
126
+ def department?(name)
127
+ cop_registry.department?(name)
128
+ end
129
+
88
130
  def all_cop_names
89
- Cop::Registry.global.names - [REDUNDANT_COP]
131
+ exclude_redundant_directive_cop(cop_registry.names)
132
+ end
133
+
134
+ def cop_names_for_department(department)
135
+ names = cop_registry.names_for_department(department)
136
+ has_redundant_directive_cop = department == REDUNDANT_DIRECTIVE_COP_DEPARTMENT
137
+ has_redundant_directive_cop ? exclude_redundant_directive_cop(names) : names
138
+ end
139
+
140
+ def exclude_redundant_directive_cop(cops)
141
+ cops - [REDUNDANT_DIRECTIVE_COP]
90
142
  end
91
143
  end
92
144
  end