rubocop 0.85.0 → 0.88.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 (215) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +25 -17
  3. data/bin/rubocop-profile +31 -0
  4. data/config/default.yml +132 -11
  5. data/lib/rubocop.rb +17 -1
  6. data/lib/rubocop/cli.rb +2 -4
  7. data/lib/rubocop/cli/command/auto_genenerate_config.rb +42 -7
  8. data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
  9. data/lib/rubocop/cli/command/show_cops.rb +1 -1
  10. data/lib/rubocop/config.rb +1 -1
  11. data/lib/rubocop/config_loader.rb +39 -67
  12. data/lib/rubocop/config_loader_resolver.rb +1 -1
  13. data/lib/rubocop/config_obsoletion.rb +0 -1
  14. data/lib/rubocop/config_store.rb +4 -0
  15. data/lib/rubocop/cop/autocorrect_logic.rb +14 -24
  16. data/lib/rubocop/cop/badge.rb +1 -1
  17. data/lib/rubocop/cop/base.rb +407 -0
  18. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +10 -20
  19. data/lib/rubocop/cop/commissioner.rb +48 -50
  20. data/lib/rubocop/cop/cop.rb +91 -235
  21. data/lib/rubocop/cop/corrector.rb +38 -115
  22. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +26 -0
  23. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +7 -2
  24. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  25. data/lib/rubocop/cop/generator.rb +1 -1
  26. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  27. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +11 -14
  28. data/lib/rubocop/cop/layout/case_indentation.rb +18 -19
  29. data/lib/rubocop/cop/layout/class_structure.rb +2 -37
  30. data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
  31. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  32. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
  33. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +3 -8
  34. data/lib/rubocop/cop/layout/end_alignment.rb +3 -2
  35. data/lib/rubocop/cop/layout/end_of_line.rb +1 -1
  36. data/lib/rubocop/cop/layout/first_argument_indentation.rb +5 -1
  37. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
  38. data/lib/rubocop/cop/layout/hash_alignment.rb +2 -3
  39. data/lib/rubocop/cop/layout/heredoc_indentation.rb +1 -1
  40. data/lib/rubocop/cop/layout/multiline_block_layout.rb +17 -7
  41. data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
  42. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +22 -27
  43. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  44. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +27 -68
  45. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  46. data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
  47. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +4 -3
  48. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  49. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
  50. data/lib/rubocop/cop/legacy/corrections_proxy.rb +49 -0
  51. data/lib/rubocop/cop/legacy/corrector.rb +29 -0
  52. data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
  53. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +4 -4
  54. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +8 -2
  55. data/lib/rubocop/cop/lint/duplicate_elsif_condition.rb +39 -0
  56. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
  57. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  58. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +38 -2
  59. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +3 -2
  60. data/lib/rubocop/cop/lint/interpolation_check.rb +13 -0
  61. data/lib/rubocop/cop/lint/literal_as_condition.rb +11 -1
  62. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +8 -1
  63. data/lib/rubocop/cop/lint/nested_method_definition.rb +14 -20
  64. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +69 -2
  65. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +8 -3
  66. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  67. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  68. data/lib/rubocop/cop/lint/raise_exception.rb +12 -4
  69. data/lib/rubocop/cop/lint/rand_one.rb +1 -1
  70. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +31 -25
  71. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +2 -2
  72. data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
  73. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +9 -1
  74. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  75. data/lib/rubocop/cop/lint/syntax.rb +11 -26
  76. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -1
  77. data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -1
  78. data/lib/rubocop/cop/metrics/block_length.rb +22 -0
  79. data/lib/rubocop/cop/metrics/class_length.rb +25 -2
  80. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
  81. data/lib/rubocop/cop/metrics/method_length.rb +23 -0
  82. data/lib/rubocop/cop/metrics/module_length.rb +25 -2
  83. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  84. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +129 -0
  85. data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
  86. data/lib/rubocop/cop/mixin/allowed_methods.rb +19 -0
  87. data/lib/rubocop/cop/mixin/auto_corrector.rb +12 -0
  88. data/lib/rubocop/cop/mixin/code_length.rb +4 -0
  89. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -1
  90. data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
  91. data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
  92. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  93. data/lib/rubocop/cop/mixin/enforce_superclass.rb +3 -1
  94. data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
  95. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  96. data/lib/rubocop/cop/mixin/nil_methods.rb +3 -5
  97. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +6 -1
  98. data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
  99. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  100. data/lib/rubocop/cop/mixin/range_help.rb +1 -1
  101. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +27 -0
  102. data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -3
  103. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  104. data/lib/rubocop/cop/mixin/surrounding_space.rb +10 -5
  105. data/lib/rubocop/cop/mixin/too_many_lines.rb +3 -13
  106. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  107. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +6 -4
  108. data/lib/rubocop/cop/mixin/visibility_help.rb +50 -0
  109. data/lib/rubocop/cop/naming/ascii_identifiers.rb +27 -4
  110. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +3 -3
  111. data/lib/rubocop/cop/naming/file_name.rb +1 -3
  112. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  113. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  114. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  115. data/lib/rubocop/cop/naming/predicate_name.rb +3 -5
  116. data/lib/rubocop/cop/naming/variable_name.rb +1 -1
  117. data/lib/rubocop/cop/naming/variable_number.rb +1 -1
  118. data/lib/rubocop/cop/offense.rb +16 -2
  119. data/lib/rubocop/cop/registry.rb +62 -7
  120. data/lib/rubocop/cop/style/accessor_grouping.rb +147 -0
  121. data/lib/rubocop/cop/style/array_coercion.rb +63 -0
  122. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +3 -2
  123. data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
  124. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +146 -0
  125. data/lib/rubocop/cop/style/block_delimiters.rb +2 -4
  126. data/lib/rubocop/cop/style/case_like_if.rb +217 -0
  127. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  128. data/lib/rubocop/cop/style/class_vars.rb +21 -0
  129. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  130. data/lib/rubocop/cop/style/commented_keyword.rb +5 -2
  131. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  132. data/lib/rubocop/cop/style/copyright.rb +3 -3
  133. data/lib/rubocop/cop/style/date_time.rb +1 -1
  134. data/lib/rubocop/cop/style/dir.rb +2 -2
  135. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
  136. data/lib/rubocop/cop/style/documentation.rb +2 -2
  137. data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
  138. data/lib/rubocop/cop/style/empty_literal.rb +5 -5
  139. data/lib/rubocop/cop/style/encoding.rb +1 -1
  140. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
  141. data/lib/rubocop/cop/style/exponential_notation.rb +8 -10
  142. data/lib/rubocop/cop/style/float_division.rb +7 -10
  143. data/lib/rubocop/cop/style/format_string_token.rb +5 -5
  144. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  145. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +62 -0
  146. data/lib/rubocop/cop/style/hash_like_case.rb +76 -0
  147. data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
  148. data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
  149. data/lib/rubocop/cop/style/if_unless_modifier.rb +11 -11
  150. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -0
  151. data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
  152. data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
  153. data/lib/rubocop/cop/style/missing_else.rb +1 -11
  154. data/lib/rubocop/cop/style/multiline_block_chain.rb +10 -1
  155. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  156. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
  157. data/lib/rubocop/cop/style/mutable_constant.rb +4 -4
  158. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -5
  159. data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
  160. data/lib/rubocop/cop/style/next.rb +2 -2
  161. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
  162. data/lib/rubocop/cop/style/numeric_predicate.rb +3 -4
  163. data/lib/rubocop/cop/style/parallel_assignment.rb +3 -3
  164. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  165. data/lib/rubocop/cop/style/proc.rb +1 -1
  166. data/lib/rubocop/cop/style/random_with_offset.rb +4 -10
  167. data/lib/rubocop/cop/style/redundant_assignment.rb +117 -0
  168. data/lib/rubocop/cop/style/redundant_conditional.rb +4 -3
  169. data/lib/rubocop/cop/style/redundant_exception.rb +14 -10
  170. data/lib/rubocop/cop/style/redundant_fetch_block.rb +122 -0
  171. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +50 -0
  172. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  173. data/lib/rubocop/cop/style/redundant_parentheses.rb +8 -2
  174. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  175. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +4 -3
  176. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +14 -23
  177. data/lib/rubocop/cop/style/redundant_self.rb +6 -9
  178. data/lib/rubocop/cop/style/redundant_sort.rb +3 -2
  179. data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -1
  180. data/lib/rubocop/cop/style/sample.rb +1 -1
  181. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  182. data/lib/rubocop/cop/style/signal_exception.rb +1 -1
  183. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +3 -2
  184. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  185. data/lib/rubocop/cop/style/struct_inheritance.rb +23 -2
  186. data/lib/rubocop/cop/style/symbol_array.rb +5 -5
  187. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  188. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  189. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +9 -32
  190. data/lib/rubocop/cop/style/trivial_accessors.rb +8 -7
  191. data/lib/rubocop/cop/style/word_array.rb +1 -1
  192. data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
  193. data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -2
  194. data/lib/rubocop/cop/team.rb +105 -81
  195. data/lib/rubocop/cop/util.rb +2 -2
  196. data/lib/rubocop/cop/utils/format_string.rb +19 -2
  197. data/lib/rubocop/cop/variable_force/variable.rb +5 -3
  198. data/lib/rubocop/file_finder.rb +12 -12
  199. data/lib/rubocop/formatter/disabled_config_formatter.rb +2 -2
  200. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  201. data/lib/rubocop/name_similarity.rb +7 -3
  202. data/lib/rubocop/options.rb +15 -8
  203. data/lib/rubocop/path_util.rb +4 -19
  204. data/lib/rubocop/platform.rb +1 -1
  205. data/lib/rubocop/rake_task.rb +6 -9
  206. data/lib/rubocop/result_cache.rb +12 -8
  207. data/lib/rubocop/rspec/cop_helper.rb +4 -4
  208. data/lib/rubocop/rspec/expect_offense.rb +65 -21
  209. data/lib/rubocop/rspec/shared_contexts.rb +19 -16
  210. data/lib/rubocop/runner.rb +34 -33
  211. data/lib/rubocop/target_finder.rb +3 -3
  212. data/lib/rubocop/target_ruby.rb +2 -2
  213. data/lib/rubocop/version.rb +1 -1
  214. metadata +34 -9
  215. data/lib/rubocop/cop/mixin/classish_length.rb +0 -37
@@ -134,7 +134,7 @@ module RuboCop
134
134
 
135
135
  def autocorrect(range)
136
136
  lambda do |corrector|
137
- if range.source =~ /\*\*/ && !space_around_exponent_operator?
137
+ if /\*\*/.match?(range.source) && !space_around_exponent_operator?
138
138
  corrector.replace(range, '**')
139
139
  elsif range.source.end_with?("\n")
140
140
  corrector.replace(range, " #{range.source.strip}\n")
@@ -27,6 +27,20 @@ module RuboCop
27
27
  # foo.map{ |a|
28
28
  # a.bar.to_s
29
29
  # }
30
+ #
31
+ # @example EnforcedStyleForEmptyBraces: space (default)
32
+ # # bad
33
+ # 7.times{}
34
+ #
35
+ # # good
36
+ # 7.times {}
37
+ #
38
+ # @example EnforcedStyleForEmptyBraces: no_space
39
+ # # bad
40
+ # 7.times {}
41
+ #
42
+ # # good
43
+ # 7.times{}
30
44
  class SpaceBeforeBlockBraces < Cop
31
45
  include ConfigurableEnforcedStyle
32
46
  include RangeHelp
@@ -130,7 +130,7 @@ module RuboCop
130
130
  line, col = line_and_column_for(token)
131
131
  return true if col == -1
132
132
 
133
- processed_source.lines[line][0..col] !~ /\S/
133
+ !/\S/.match?(processed_source.lines[line][0..col])
134
134
  end
135
135
 
136
136
  def index_for(node, token)
@@ -142,11 +142,12 @@ module RuboCop
142
142
  end
143
143
 
144
144
  def issue_offenses(node, left, right, start_ok, end_ok)
145
- if style == :no_space
145
+ case style
146
+ when :no_space
146
147
  start_ok = next_to_comment?(node, left)
147
148
  no_space_offenses(node, left, right, MSG, start_ok: start_ok,
148
149
  end_ok: end_ok)
149
- elsif style == :space
150
+ when :space
150
151
  space_offenses(node, left, right, MSG, start_ok: start_ok,
151
152
  end_ok: end_ok)
152
153
  else
@@ -150,7 +150,7 @@ module RuboCop
150
150
  end
151
151
 
152
152
  def check_right_brace(inner, left_brace, right_brace, single_line)
153
- if single_line && inner =~ /\S$/
153
+ if single_line && /\S$/.match?(inner)
154
154
  no_space(right_brace.begin_pos, right_brace.end_pos,
155
155
  'Space missing inside }.')
156
156
  else
@@ -182,7 +182,7 @@ module RuboCop
182
182
  def range_of_space_to_the_right(range)
183
183
  src = range.source_buffer.source
184
184
  end_pos = range.end_pos
185
- end_pos += 1 while src[end_pos] =~ /[ \t]/
185
+ end_pos += 1 while /[ \t]/.match?(src[end_pos])
186
186
 
187
187
  range_between(range.begin_pos + 1, end_pos)
188
188
  end
@@ -190,7 +190,7 @@ module RuboCop
190
190
  def range_of_space_to_the_left(range)
191
191
  src = range.source_buffer.source
192
192
  begin_pos = range.begin_pos
193
- begin_pos -= 1 while src[begin_pos - 1] =~ /[ \t]/
193
+ begin_pos -= 1 while /[ \t]/.match?(src[begin_pos - 1])
194
194
 
195
195
  range_between(begin_pos, range.end_pos - 1)
196
196
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Legacy
6
+ # Legacy support for Corrector#corrections
7
+ # See https://docs.rubocop.org/rubocop/cop_api_v1_changelog.html
8
+ class CorrectionsProxy
9
+ def initialize(corrector)
10
+ @corrector = corrector
11
+ end
12
+
13
+ def <<(callable)
14
+ suppress_clobbering do
15
+ @corrector.transaction do
16
+ callable.call(@corrector)
17
+ end
18
+ end
19
+ end
20
+
21
+ def empty?
22
+ @corrector.empty?
23
+ end
24
+
25
+ def concat(corrections)
26
+ if corrections.is_a?(CorrectionsProxy)
27
+ suppress_clobbering do
28
+ corrector.merge!(corrections.corrector)
29
+ end
30
+ else
31
+ corrections.each { |correction| self << correction }
32
+ end
33
+ end
34
+
35
+ protected
36
+
37
+ attr_reader :corrector
38
+
39
+ private
40
+
41
+ def suppress_clobbering
42
+ yield
43
+ rescue ::Parser::ClobberingError
44
+ # ignore Clobbering errors
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Legacy
6
+ # Legacy Corrector for v0 API support.
7
+ # See https://docs.rubocop.org/rubocop/cop_api_v1_changelog.html
8
+ class Corrector < RuboCop::Cop::Corrector
9
+ # Support legacy second argument
10
+ def initialize(source, corr = [])
11
+ super(source)
12
+ if corr.is_a?(CorrectionsProxy)
13
+ merge!(corr.send(:corrector))
14
+ else
15
+ # warn "Corrector.new with corrections is deprecated." unless corr.empty? TODO
16
+ corr.each do |c|
17
+ corrections << c
18
+ end
19
+ end
20
+ end
21
+
22
+ def corrections
23
+ # warn "#corrections is deprecated. Open an issue if you have a valid usecase." TODO
24
+ CorrectionsProxy.new(self)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # Check that certain constants are fully qualified.
7
+ #
8
+ # This is not enabled by default because it would mark a lot of offenses
9
+ # unnecessarily.
10
+ #
11
+ # Generally, gems should fully qualify all constants to avoid conflicts with
12
+ # the code that uses the gem. Enable this cop without using `Only`/`Ignore`
13
+ #
14
+ # Large projects will over time end up with one or two constant names that
15
+ # are problematic because of a conflict with a library or just internally
16
+ # using the same name a namespace and a class. To avoid too many unnecessary
17
+ # offenses, Enable this cop with `Only: [The, Constant, Names, Causing, Issues]`
18
+ #
19
+ # @example
20
+ # # By default checks every constant
21
+ #
22
+ # # bad
23
+ # User
24
+ #
25
+ # # bad
26
+ # User::Login
27
+ #
28
+ # # good
29
+ # ::User
30
+ #
31
+ # # good
32
+ # ::User::Login
33
+ #
34
+ # @example Only: ['Login']
35
+ # # Restrict this cop to only being concerned about certain constants
36
+ #
37
+ # # bad
38
+ # Login
39
+ #
40
+ # # good
41
+ # ::Login
42
+ #
43
+ # # good
44
+ # User::Login
45
+ #
46
+ # @example Ignore: ['Login']
47
+ # # Restrict this cop not being concerned about certain constants
48
+ #
49
+ # # bad
50
+ # User
51
+ #
52
+ # # good
53
+ # ::User::Login
54
+ #
55
+ # # good
56
+ # Login
57
+ #
58
+ class ConstantResolution < Cop
59
+ MSG = 'Fully qualify this constant to avoid possibly ambiguous resolution.'
60
+
61
+ def_node_matcher :unqualified_const?, <<~PATTERN
62
+ (const nil? #const_name?)
63
+ PATTERN
64
+
65
+ def on_const(node)
66
+ return unless unqualified_const?(node)
67
+
68
+ add_offense(node)
69
+ end
70
+
71
+ private
72
+
73
+ def const_name?(name)
74
+ name = name.to_s
75
+ (allowed_names.empty? || allowed_names.include?(name)) &&
76
+ !ignored_names.include?(name)
77
+ end
78
+
79
+ def allowed_names
80
+ cop_config['Only']
81
+ end
82
+
83
+ def ignored_names
84
+ cop_config['Ignore']
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -15,7 +15,7 @@ module RuboCop
15
15
  # OpenSSL::Cipher::AES.new(128, :GCM)
16
16
  #
17
17
  # # good
18
- # OpenSSL::Cipher.new('AES-128-GCM')
18
+ # OpenSSL::Cipher.new('aes-128-gcm')
19
19
  #
20
20
  # @example
21
21
  #
@@ -127,9 +127,9 @@ module RuboCop
127
127
  end
128
128
 
129
129
  def build_cipher_arguments(node, algorithm_name)
130
- algorithm_parts = algorithm_name.split('-')
131
- size_and_mode = sanitize_arguments(node.arguments)
132
- "'#{(algorithm_parts + size_and_mode + ['CBC']).take(3).join('-')}'"
130
+ algorithm_parts = algorithm_name.downcase.split('-')
131
+ size_and_mode = sanitize_arguments(node.arguments).map(&:downcase)
132
+ "'#{(algorithm_parts + size_and_mode + ['cbc']).take(3).join('-')}'"
133
133
  end
134
134
  end
135
135
  end
@@ -22,7 +22,9 @@ module RuboCop
22
22
  # def initialize
23
23
  # @x = 1
24
24
  # end
25
- class DisjunctiveAssignmentInConstructor < Cop
25
+ class DisjunctiveAssignmentInConstructor < Base
26
+ extend AutoCorrector
27
+
26
28
  MSG = 'Unnecessary disjunctive assignment. Use plain assignment.'
27
29
 
28
30
  def on_def(node)
@@ -73,7 +75,11 @@ module RuboCop
73
75
  # @param [Node] node a disjunctive assignment
74
76
  def check_disjunctive_assignment(node)
75
77
  lhs = node.child_nodes.first
76
- add_offense(node, location: :operator) if lhs.ivasgn_type?
78
+ return unless lhs.ivasgn_type?
79
+
80
+ add_offense(node.loc.operator) do |corrector|
81
+ corrector.replace(node.loc.operator, '=')
82
+ end
77
83
  end
78
84
  end
79
85
  end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # This cop checks that there are no repeated conditions used in if 'elsif'.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # if x == 1
11
+ # do_something
12
+ # elsif x == 1
13
+ # do_something_else
14
+ # end
15
+ #
16
+ # # good
17
+ # if x == 1
18
+ # do_something
19
+ # elsif x == 2
20
+ # do_something_else
21
+ # end
22
+ #
23
+ class DuplicateElsifCondition < Base
24
+ MSG = 'Duplicate `elsif` condition detected.'
25
+
26
+ def on_if(node)
27
+ previous = []
28
+ while node.if? || node.elsif?
29
+ condition = node.condition
30
+ add_offense(condition) if previous.include?(condition)
31
+ previous << condition
32
+ node = node.else_branch
33
+ break unless node&.if_type?
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -126,7 +126,7 @@ module RuboCop
126
126
  end
127
127
 
128
128
  def message_for_dup(node, method_name)
129
- format(MSG, method: method_name, defined: @definitions[method_name],
129
+ format(MSG, method: method_name, defined: source_location(@definitions[method_name]),
130
130
  current: source_location(node))
131
131
  end
132
132
 
@@ -152,7 +152,7 @@ module RuboCop
152
152
 
153
153
  add_offense(node, location: loc, message: message)
154
154
  else
155
- @definitions[method_name] = source_location(node)
155
+ @definitions[method_name] = node
156
156
  end
157
157
  end
158
158
 
@@ -25,7 +25,7 @@ module RuboCop
25
25
  value, = *node
26
26
 
27
27
  return unless value.infinite? ||
28
- value.zero? && node.source =~ /[1-9]/
28
+ value.zero? && /[1-9]/.match?(node.source)
29
29
 
30
30
  add_offense(node)
31
31
  end
@@ -7,6 +7,10 @@ module RuboCop
7
7
  # expected fields for format/sprintf/#% and what is actually
8
8
  # passed as arguments.
9
9
  #
10
+ # In addition it checks whether different formats are used in the same
11
+ # format string. Do not mix numbered, unnumbered, and named formats in
12
+ # the same format string.
13
+ #
10
14
  # @example
11
15
  #
12
16
  # # bad
@@ -18,16 +22,37 @@ module RuboCop
18
22
  # # good
19
23
  #
20
24
  # format('A value: %s and another: %i', a_value, another)
25
+ #
26
+ # @example
27
+ #
28
+ # # bad
29
+ #
30
+ # format('Unnumbered format: %s and numbered: %2$s', a_value, another)
31
+ #
32
+ # @example
33
+ #
34
+ # # good
35
+ #
36
+ # format('Numbered format: %1$s and numbered %2$s', a_value, another)
21
37
  class FormatParameterMismatch < Cop
22
38
  # http://rubular.com/r/CvpbxkcTzy
23
39
  MSG = "Number of arguments (%<arg_num>i) to `%<method>s` doesn't " \
24
40
  'match the number of fields (%<field_num>i).'
41
+ MSG_INVALID = 'Format string is invalid because formatting sequence types ' \
42
+ '(numbered, named or unnumbered) are mixed.'
25
43
 
26
44
  KERNEL = 'Kernel'
27
45
  SHOVEL = '<<'
28
46
  STRING_TYPES = %i[str dstr].freeze
29
47
 
30
48
  def on_send(node)
49
+ return unless format_string?(node)
50
+
51
+ if invalid_format_string?(node)
52
+ add_offense(node, location: :selector, message: MSG_INVALID)
53
+ return
54
+ end
55
+
31
56
  return unless offending_node?(node)
32
57
 
33
58
  add_offense(node, location: :selector)
@@ -35,9 +60,20 @@ module RuboCop
35
60
 
36
61
  private
37
62
 
63
+ def format_string?(node)
64
+ called_on_string?(node) && method_with_format_args?(node)
65
+ end
66
+
67
+ def invalid_format_string?(node)
68
+ string = if sprintf?(node) || format?(node)
69
+ node.first_argument.source
70
+ else
71
+ node.receiver.source
72
+ end
73
+ !RuboCop::Cop::Utils::FormatString.new(string).valid?
74
+ end
75
+
38
76
  def offending_node?(node)
39
- return false unless called_on_string?(node)
40
- return false unless method_with_format_args?(node)
41
77
  return false if splat_args?(node)
42
78
 
43
79
  num_of_format_args, num_of_expected_fields = count_matches(node)
@@ -64,9 +64,10 @@ module RuboCop
64
64
 
65
65
  def ending_delimiter(str)
66
66
  # implicit string concatenation does not work with %{}, etc.
67
- if str.source[0] == "'"
67
+ case str.source[0]
68
+ when "'"
68
69
  "'"
69
- elsif str.source[0] == '"'
70
+ when '"'
70
71
  '"'
71
72
  end
72
73
  end