rubocop 0.52.1 → 0.53.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (292) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +2 -2
  4. data/config/default.yml +118 -46
  5. data/config/disabled.yml +8 -8
  6. data/config/enabled.yml +84 -28
  7. data/lib/rubocop.rb +28 -8
  8. data/lib/rubocop/ast/builder.rb +35 -37
  9. data/lib/rubocop/ast/node.rb +16 -1
  10. data/lib/rubocop/ast/node/and_node.rb +0 -8
  11. data/lib/rubocop/ast/node/block_node.rb +1 -9
  12. data/lib/rubocop/ast/node/case_node.rb +0 -8
  13. data/lib/rubocop/ast/node/ensure_node.rb +0 -8
  14. data/lib/rubocop/ast/node/for_node.rb +0 -8
  15. data/lib/rubocop/ast/node/or_node.rb +0 -8
  16. data/lib/rubocop/ast/node/pair_node.rb +0 -8
  17. data/lib/rubocop/ast/node/resbody_node.rb +0 -8
  18. data/lib/rubocop/ast/node/send_node.rb +0 -8
  19. data/lib/rubocop/ast/node/symbol_node.rb +0 -8
  20. data/lib/rubocop/ast/node/until_node.rb +0 -8
  21. data/lib/rubocop/ast/node/when_node.rb +0 -8
  22. data/lib/rubocop/ast/node/while_node.rb +0 -8
  23. data/lib/rubocop/cli.rb +17 -7
  24. data/lib/rubocop/comment_config.rb +24 -3
  25. data/lib/rubocop/config.rb +75 -6
  26. data/lib/rubocop/config_loader.rb +18 -28
  27. data/lib/rubocop/config_loader_resolver.rb +61 -9
  28. data/lib/rubocop/cop/bundler/duplicated_gem.rb +3 -1
  29. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +4 -2
  30. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
  31. data/lib/rubocop/cop/commissioner.rb +2 -2
  32. data/lib/rubocop/cop/cop.rb +4 -0
  33. data/lib/rubocop/cop/corrector.rb +11 -1
  34. data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -6
  35. data/lib/rubocop/cop/correctors/line_break_corrector.rb +59 -0
  36. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +1 -1
  37. data/lib/rubocop/cop/correctors/space_corrector.rb +13 -0
  38. data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +1 -1
  39. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +3 -1
  40. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  41. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +3 -5
  42. data/lib/rubocop/cop/generator.rb +29 -8
  43. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +2 -0
  44. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +2 -0
  45. data/lib/rubocop/cop/layout/align_hash.rb +106 -37
  46. data/lib/rubocop/cop/{lint → layout}/block_alignment.rb +8 -5
  47. data/lib/rubocop/cop/layout/block_end_newline.rb +7 -17
  48. data/lib/rubocop/cop/layout/case_indentation.rb +1 -0
  49. data/lib/rubocop/cop/layout/class_structure.rb +6 -7
  50. data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
  51. data/lib/rubocop/cop/{lint → layout}/condition_position.rb +3 -3
  52. data/lib/rubocop/cop/{lint → layout}/def_end_alignment.rb +2 -1
  53. data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
  54. data/lib/rubocop/cop/layout/empty_comment.rb +140 -0
  55. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -0
  56. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -0
  57. data/lib/rubocop/cop/layout/empty_lines.rb +3 -1
  58. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +7 -5
  59. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +20 -10
  60. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +20 -0
  61. data/lib/rubocop/cop/{lint → layout}/end_alignment.rb +37 -6
  62. data/lib/rubocop/cop/layout/end_of_line.rb +1 -0
  63. data/lib/rubocop/cop/layout/extra_spacing.rb +30 -37
  64. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -0
  65. data/lib/rubocop/cop/layout/indent_heredoc.rb +38 -2
  66. data/lib/rubocop/cop/layout/indentation_consistency.rb +105 -1
  67. data/lib/rubocop/cop/layout/indentation_width.rb +4 -3
  68. data/lib/rubocop/cop/layout/initial_indentation.rb +15 -1
  69. data/lib/rubocop/cop/layout/leading_comment_space.rb +4 -2
  70. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -0
  71. data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -0
  72. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +62 -29
  73. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  74. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +74 -33
  75. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +16 -2
  76. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -1
  77. data/lib/rubocop/cop/layout/space_after_method_name.rb +2 -0
  78. data/lib/rubocop/cop/layout/space_after_not.rb +2 -0
  79. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -0
  80. data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +15 -2
  81. data/lib/rubocop/cop/layout/space_around_operators.rb +15 -13
  82. data/lib/rubocop/cop/layout/space_before_block_braces.rb +13 -1
  83. data/lib/rubocop/cop/layout/space_before_comment.rb +6 -4
  84. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -0
  85. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -0
  86. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +30 -45
  87. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +3 -2
  88. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +48 -18
  89. data/lib/rubocop/cop/layout/space_inside_parens.rb +8 -7
  90. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +57 -11
  91. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +1 -0
  92. data/lib/rubocop/cop/layout/tab.rb +42 -16
  93. data/lib/rubocop/cop/layout/trailing_blank_lines.rb +46 -13
  94. data/lib/rubocop/cop/layout/trailing_whitespace.rb +12 -0
  95. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +5 -3
  96. data/lib/rubocop/cop/lint/big_decimal_new.rb +44 -0
  97. data/lib/rubocop/cop/lint/boolean_symbol.rb +2 -2
  98. data/lib/rubocop/cop/lint/circular_argument_reference.rb +2 -2
  99. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  100. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +5 -4
  101. data/lib/rubocop/cop/lint/duplicate_methods.rb +20 -9
  102. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +4 -3
  103. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +16 -10
  104. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +5 -4
  105. data/lib/rubocop/cop/lint/inherit_exception.rb +2 -2
  106. data/lib/rubocop/cop/lint/interpolation_check.rb +4 -3
  107. data/lib/rubocop/cop/lint/literal_as_condition.rb +2 -2
  108. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +2 -0
  109. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +7 -5
  110. data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
  111. data/lib/rubocop/cop/lint/number_conversion.rb +59 -0
  112. data/lib/rubocop/cop/lint/ordered_magic_comments.rb +86 -0
  113. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +2 -0
  114. data/lib/rubocop/cop/lint/percent_string_array.rb +0 -2
  115. data/lib/rubocop/cop/lint/rand_one.rb +2 -2
  116. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -0
  117. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -0
  118. data/lib/rubocop/cop/lint/require_parentheses.rb +2 -0
  119. data/lib/rubocop/cop/lint/rescue_type.rb +6 -3
  120. data/lib/rubocop/cop/lint/return_in_void_context.rb +2 -2
  121. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +17 -21
  122. data/lib/rubocop/cop/lint/script_permission.rb +30 -10
  123. data/lib/rubocop/cop/lint/shadowed_argument.rb +3 -3
  124. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -0
  125. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +2 -2
  126. data/lib/rubocop/cop/lint/unified_integer.rb +2 -2
  127. data/lib/rubocop/cop/lint/{unneeded_disable.rb → unneeded_cop_disable_directive.rb} +13 -7
  128. data/lib/rubocop/cop/lint/unneeded_cop_enable_directive.rb +97 -0
  129. data/lib/rubocop/cop/lint/unneeded_require_statement.rb +1 -0
  130. data/lib/rubocop/cop/lint/unreachable_code.rb +3 -3
  131. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +11 -10
  132. data/lib/rubocop/cop/lint/useless_access_modifier.rb +7 -5
  133. data/lib/rubocop/cop/lint/useless_assignment.rb +2 -2
  134. data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
  135. data/lib/rubocop/cop/lint/void.rb +49 -10
  136. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  137. data/lib/rubocop/cop/metrics/line_length.rb +5 -2
  138. data/lib/rubocop/cop/mixin/alignment.rb +4 -0
  139. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -1
  140. data/lib/rubocop/cop/mixin/def_node.rb +4 -0
  141. data/lib/rubocop/cop/mixin/documentation_comment.rb +11 -3
  142. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +12 -2
  143. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +20 -1
  144. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +4 -0
  145. data/lib/rubocop/cop/mixin/hash_alignment.rb +2 -2
  146. data/lib/rubocop/cop/mixin/match_range.rb +2 -0
  147. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +6 -0
  148. data/lib/rubocop/cop/mixin/nil_methods.rb +19 -0
  149. data/lib/rubocop/cop/mixin/percent_literal.rb +57 -9
  150. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +6 -5
  151. data/lib/rubocop/cop/mixin/range_help.rb +102 -0
  152. data/lib/rubocop/cop/mixin/rescue_node.rb +1 -1
  153. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +8 -7
  154. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -9
  155. data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -10
  156. data/lib/rubocop/cop/mixin/surrounding_space.rb +38 -8
  157. data/lib/rubocop/cop/mixin/trailing_body.rb +26 -0
  158. data/lib/rubocop/cop/mixin/trailing_comma.rb +15 -3
  159. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +104 -0
  160. data/lib/rubocop/cop/naming/ascii_identifiers.rb +3 -1
  161. data/lib/rubocop/cop/naming/file_name.rb +5 -10
  162. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +76 -0
  163. data/lib/rubocop/cop/naming/uncommunicative_block_param_name.rb +48 -0
  164. data/lib/rubocop/cop/naming/uncommunicative_method_param_name.rb +57 -0
  165. data/lib/rubocop/cop/offense.rb +3 -2
  166. data/lib/rubocop/cop/performance/case_when_splat.rb +1 -0
  167. data/lib/rubocop/cop/performance/casecmp.rb +17 -8
  168. data/lib/rubocop/cop/performance/compare_with_block.rb +2 -0
  169. data/lib/rubocop/cop/performance/count.rb +1 -0
  170. data/lib/rubocop/cop/performance/fixed_size.rb +41 -0
  171. data/lib/rubocop/cop/performance/flat_map.rb +2 -0
  172. data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -0
  173. data/lib/rubocop/cop/performance/redundant_merge.rb +1 -1
  174. data/lib/rubocop/cop/performance/redundant_sort_by.rb +2 -0
  175. data/lib/rubocop/cop/performance/regexp_match.rb +4 -0
  176. data/lib/rubocop/cop/performance/reverse_each.rb +2 -0
  177. data/lib/rubocop/cop/performance/string_replacement.rb +2 -0
  178. data/lib/rubocop/cop/rails/active_record_aliases.rb +46 -0
  179. data/lib/rubocop/cop/rails/blank.rb +3 -3
  180. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +6 -0
  181. data/lib/rubocop/cop/rails/delegate.rb +6 -6
  182. data/lib/rubocop/cop/rails/file_path.rb +7 -1
  183. data/lib/rubocop/cop/rails/find_by.rb +2 -0
  184. data/lib/rubocop/cop/rails/http_positional_arguments.rb +17 -5
  185. data/lib/rubocop/cop/rails/inverse_of.rb +21 -2
  186. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +45 -9
  187. data/lib/rubocop/cop/rails/presence.rb +8 -2
  188. data/lib/rubocop/cop/rails/present.rb +5 -5
  189. data/lib/rubocop/cop/rails/read_write_attribute.rb +4 -3
  190. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +1 -0
  191. data/lib/rubocop/cop/rails/relative_date_constant.rb +4 -3
  192. data/lib/rubocop/cop/rails/request_referer.rb +3 -2
  193. data/lib/rubocop/cop/rails/reversible_migration.rb +9 -8
  194. data/lib/rubocop/cop/rails/safe_navigation.rb +3 -2
  195. data/lib/rubocop/cop/rails/save_bang.rb +11 -12
  196. data/lib/rubocop/cop/rails/skips_model_validations.rb +2 -2
  197. data/lib/rubocop/cop/rails/time_zone.rb +38 -16
  198. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +26 -16
  199. data/lib/rubocop/cop/rails/validation.rb +30 -2
  200. data/lib/rubocop/cop/security/open.rb +48 -0
  201. data/lib/rubocop/cop/style/and_or.rb +1 -0
  202. data/lib/rubocop/cop/style/ascii_comments.rb +3 -1
  203. data/lib/rubocop/cop/style/attr.rb +2 -0
  204. data/lib/rubocop/cop/style/block_comments.rb +3 -1
  205. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +2 -5
  206. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -0
  207. data/lib/rubocop/cop/style/class_vars.rb +23 -0
  208. data/lib/rubocop/cop/style/colon_method_call.rb +1 -2
  209. data/lib/rubocop/cop/style/comment_annotation.rb +6 -4
  210. data/lib/rubocop/cop/style/commented_keyword.rb +3 -1
  211. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  212. data/lib/rubocop/cop/style/copyright.rb +3 -1
  213. data/lib/rubocop/cop/style/each_with_object.rb +15 -1
  214. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -0
  215. data/lib/rubocop/cop/style/empty_case_condition.rb +2 -0
  216. data/lib/rubocop/cop/style/empty_else.rb +9 -5
  217. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -0
  218. data/lib/rubocop/cop/style/empty_line_after_guard_clause.rb +80 -0
  219. data/lib/rubocop/cop/style/empty_literal.rb +1 -0
  220. data/lib/rubocop/cop/style/encoding.rb +2 -0
  221. data/lib/rubocop/cop/style/expand_path_arguments.rb +194 -0
  222. data/lib/rubocop/cop/style/for.rb +33 -0
  223. data/lib/rubocop/cop/style/format_string.rb +1 -1
  224. data/lib/rubocop/cop/style/format_string_token.rb +4 -5
  225. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +2 -1
  226. data/lib/rubocop/cop/style/hash_syntax.rb +1 -0
  227. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  228. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  229. data/lib/rubocop/cop/style/lambda.rb +1 -1
  230. data/lib/rubocop/cop/style/line_end_concatenation.rb +2 -0
  231. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -0
  232. data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -0
  233. data/lib/rubocop/cop/style/missing_else.rb +72 -7
  234. data/lib/rubocop/cop/style/mixin_usage.rb +3 -5
  235. data/lib/rubocop/cop/style/module_function.rb +10 -0
  236. data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -0
  237. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -0
  238. data/lib/rubocop/cop/style/nested_modifier.rb +2 -0
  239. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -0
  240. data/lib/rubocop/cop/style/next.rb +1 -0
  241. data/lib/rubocop/cop/style/not.rb +2 -0
  242. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  243. data/lib/rubocop/cop/style/one_line_conditional.rb +2 -2
  244. data/lib/rubocop/cop/style/redundant_exception.rb +8 -3
  245. data/lib/rubocop/cop/style/redundant_return.rb +37 -3
  246. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  247. data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -0
  248. data/lib/rubocop/cop/style/safe_navigation.rb +74 -32
  249. data/lib/rubocop/cop/style/semicolon.rb +3 -1
  250. data/lib/rubocop/cop/style/single_line_methods.rb +14 -23
  251. data/lib/rubocop/cop/style/stderr_puts.rb +2 -0
  252. data/lib/rubocop/cop/style/string_hash_keys.rb +12 -0
  253. data/lib/rubocop/cop/style/string_literals.rb +1 -1
  254. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +1 -1
  255. data/lib/rubocop/cop/style/symbol_array.rb +29 -0
  256. data/lib/rubocop/cop/style/symbol_proc.rb +2 -0
  257. data/lib/rubocop/cop/style/trailing_body_on_class.rb +43 -0
  258. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +7 -54
  259. data/lib/rubocop/cop/style/trailing_body_on_module.rb +43 -0
  260. data/lib/rubocop/cop/style/{trailing_comma_in_literal.rb → trailing_comma_in_array_literal.rb} +2 -20
  261. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +56 -0
  262. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +17 -20
  263. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -0
  264. data/lib/rubocop/cop/style/unless_else.rb +2 -0
  265. data/lib/rubocop/cop/style/word_array.rb +0 -1
  266. data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
  267. data/lib/rubocop/cop/team.rb +5 -5
  268. data/lib/rubocop/cop/util.rb +23 -188
  269. data/lib/rubocop/cop/variable_force.rb +1 -1
  270. data/lib/rubocop/file_finder.rb +45 -0
  271. data/lib/rubocop/formatter/disabled_config_formatter.rb +23 -14
  272. data/lib/rubocop/formatter/fuubar_style_formatter.rb +1 -1
  273. data/lib/rubocop/formatter/html_formatter.rb +12 -5
  274. data/lib/rubocop/formatter/json_formatter.rb +1 -1
  275. data/lib/rubocop/node_pattern.rb +8 -5
  276. data/lib/rubocop/options.rb +40 -33
  277. data/lib/rubocop/path_util.rb +5 -8
  278. data/lib/rubocop/processed_source.rb +53 -0
  279. data/lib/rubocop/remote_config.rb +1 -1
  280. data/lib/rubocop/result_cache.rb +1 -1
  281. data/lib/rubocop/rspec/cop_helper.rb +0 -4
  282. data/lib/rubocop/rspec/host_environment_simulation_helper.rb +0 -4
  283. data/lib/rubocop/rspec/shared_contexts.rb +3 -1
  284. data/lib/rubocop/rspec/shared_examples.rb +23 -25
  285. data/lib/rubocop/rspec/support.rb +5 -0
  286. data/lib/rubocop/runner.rb +3 -2
  287. data/lib/rubocop/string_util.rb +10 -9
  288. data/lib/rubocop/target_finder.rb +4 -1
  289. data/lib/rubocop/token.rb +26 -16
  290. data/lib/rubocop/version.rb +6 -4
  291. metadata +31 -17
  292. data/lib/rubocop/cop/performance/hash_each_methods.rb +0 -129
@@ -37,10 +37,12 @@ module RuboCop
37
37
  # params[:width_gteq]
38
38
  #
39
39
  class AsciiIdentifiers < Cop
40
+ include RangeHelp
41
+
40
42
  MSG = 'Use only ascii symbols in identifiers.'.freeze
41
43
 
42
44
  def investigate(processed_source)
43
- processed_source.tokens.each do |token|
45
+ processed_source.each_token do |token|
44
46
  next unless token.type == :tIDENTIFIER && !token.text.ascii_only?
45
47
  add_offense(token, location: first_offense_range(token))
46
48
  end
@@ -20,6 +20,8 @@ module RuboCop
20
20
  #
21
21
  # anything/using_snake_case.rake
22
22
  class FileName < Cop
23
+ include RangeHelp
24
+
23
25
  MSG_SNAKE_CASE = 'The name of this source file (`%<basename>s`) ' \
24
26
  'should use snake_case.'.freeze
25
27
  MSG_NO_DEFINITION = '%<basename>s should define a class or module ' \
@@ -29,7 +31,7 @@ module RuboCop
29
31
  SNAKE_CASE = /^[\da-z_.?!]+$/
30
32
 
31
33
  def investigate(processed_source)
32
- file_path = processed_source.buffer.name
34
+ file_path = processed_source.file_path
33
35
  return if config.file_to_include?(file_path)
34
36
 
35
37
  for_bad_filename(file_path) do |range, msg|
@@ -48,17 +50,14 @@ module RuboCop
48
50
 
49
51
  no_definition_message(basename, file_path)
50
52
  else
51
- return if ignore_executable_scripts? && shebang?(first_line)
53
+ return if ignore_executable_scripts? &&
54
+ processed_source.start_with?('#!')
52
55
  other_message(basename)
53
56
  end
54
57
 
55
58
  yield source_range(processed_source.buffer, 1, 0), msg
56
59
  end
57
60
 
58
- def first_line
59
- processed_source.lines.first
60
- end
61
-
62
61
  def no_definition_message(basename, file_path)
63
62
  format(MSG_NO_DEFINITION,
64
63
  basename: basename,
@@ -73,10 +72,6 @@ module RuboCop
73
72
  end
74
73
  end
75
74
 
76
- def shebang?(line)
77
- line && line.start_with?('#!')
78
- end
79
-
80
75
  def ignore_executable_scripts?
81
76
  cop_config['IgnoreExecutableScripts']
82
77
  end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Naming
6
+ # This cop checks for memoized methods whose instance variable name
7
+ # does not match the method name.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # # Method foo is memoized using an instance variable that is
12
+ # # not `@foo`. This can cause confusion and bugs.
13
+ # def foo
14
+ # @something ||= calculate_expensive_thing
15
+ # end
16
+ #
17
+ # # good
18
+ # def foo
19
+ # @foo ||= calculate_expensive_thing
20
+ # end
21
+ #
22
+ # # good
23
+ # def foo
24
+ # @foo ||= begin
25
+ # calculate_expensive_thing
26
+ # end
27
+ # end
28
+ #
29
+ # # good
30
+ # def foo
31
+ # helper_variable = something_we_need_to_calculate_foo
32
+ # @foo ||= calculate_expensive_thing(helper_variable)
33
+ # end
34
+ #
35
+ class MemoizedInstanceVariableName < Cop
36
+ MSG = 'Memoized variable `%<var>s` does not match ' \
37
+ 'method name `%<method>s`. Use `@%<method>s` instead.'.freeze
38
+
39
+ def self.node_pattern
40
+ memo_assign = '(or_asgn $(ivasgn _) _)'
41
+ memoized_at_end_of_method = "(begin ... #{memo_assign})"
42
+ instance_method =
43
+ "(def $_ _ {#{memo_assign} #{memoized_at_end_of_method}})"
44
+ class_method =
45
+ "(defs self $_ _ {#{memo_assign} #{memoized_at_end_of_method}})"
46
+ "{#{instance_method} #{class_method}}"
47
+ end
48
+
49
+ private_class_method :node_pattern
50
+ def_node_matcher :memoized?, node_pattern
51
+
52
+ def on_def(node)
53
+ (method_name, ivar_assign) = memoized?(node)
54
+ return if matches?(method_name, ivar_assign)
55
+ msg = format(
56
+ MSG,
57
+ var: ivar_assign.children.first.to_s,
58
+ method: method_name
59
+ )
60
+ add_offense(node, location: ivar_assign.source_range, message: msg)
61
+ end
62
+ alias on_defs on_def
63
+
64
+ private
65
+
66
+ def matches?(method_name, ivar_assign)
67
+ return true if ivar_assign.nil? || method_name == :initialize
68
+ method_name = method_name.to_s.sub('?', '')
69
+ variable = ivar_assign.children.first
70
+ variable_name = variable.to_s.sub('@', '')
71
+ variable_name == method_name
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Naming
6
+ # This cop checks block parameter names for how descriptive they
7
+ # are. It is highly configurable.
8
+ #
9
+ # The `MinNameLength` config option takes an integer. It represents
10
+ # the minimum amount of characters the name must be. Its default is 1.
11
+ # The `AllowNamesEndingInNumbers` config option takes a boolean. When
12
+ # set to false, this cop will register offenses for names ending with
13
+ # numbers. Its default is false. The `AllowedNames` config option
14
+ # takes an array of whitelisted names that will never register an
15
+ # offense. The `ForbiddenNames` config option takes an array of
16
+ # blacklisted names that will always register an offense.
17
+ #
18
+ # @example
19
+ # # bad
20
+ # bar do |varOne, varTwo|
21
+ # varOne + varTwo
22
+ # end
23
+ #
24
+ # # With `AllowNamesEndingInNumbers` set to false
25
+ # foo { |num1, num2| num1 * num2 }
26
+ #
27
+ # # With `MinParamNameLength` set to number greater than 1
28
+ # baz { |a, b, c| do_stuff(a, b, c) }
29
+ #
30
+ # # good
31
+ # bar do |thud, fred|
32
+ # thud + fred
33
+ # end
34
+ #
35
+ # foo { |speed, distance| speed * distance }
36
+ #
37
+ # baz { |age, height, gender| do_stuff(age, height, gender) }
38
+ class UncommunicativeBlockParamName < Cop
39
+ include UncommunicativeName
40
+
41
+ def on_block(node)
42
+ return unless node.arguments?
43
+ check(node, node.arguments)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Naming
6
+ # This cop checks method parameter names for how descriptive they
7
+ # are. It is highly configurable.
8
+ #
9
+ # The `MinNameLength` config option takes an integer. It represents
10
+ # the minimum amount of characters the name must be. Its default is 3.
11
+ # The `AllowNamesEndingInNumbers` config option takes a boolean. When
12
+ # set to false, this cop will register offenses for names ending with
13
+ # numbers. Its default is false. The `AllowedNames` config option
14
+ # takes an array of whitelisted names that will never register an
15
+ # offense. The `ForbiddenNames` config option takes an array of
16
+ # blacklisted names that will always register an offense.
17
+ #
18
+ # @example
19
+ # # bad
20
+ # def bar(varOne, varTwo)
21
+ # varOne + varTwo
22
+ # end
23
+ #
24
+ # # With `AllowNamesEndingInNumbers` set to false
25
+ # def foo(num1, num2)
26
+ # num1 * num2
27
+ # end
28
+ #
29
+ # # With `MinArgNameLength` set to number greater than 1
30
+ # def baz(a, b, c)
31
+ # do_stuff(a, b, c)
32
+ # end
33
+ #
34
+ # # good
35
+ # def bar(thud, fred)
36
+ # thud + fred
37
+ # end
38
+ #
39
+ # def foo(speed, distance)
40
+ # speed * distance
41
+ # end
42
+ #
43
+ # def baz(age_a, height_b, gender_c)
44
+ # do_stuff(age_a, height_b, gender_c)
45
+ # end
46
+ class UncommunicativeMethodParamName < Cop
47
+ include UncommunicativeName
48
+
49
+ def on_def(node)
50
+ return unless node.arguments?
51
+ check(node, node.arguments)
52
+ end
53
+ alias on_defs on_def
54
+ end
55
+ end
56
+ end
57
+ end
@@ -99,8 +99,9 @@ module RuboCop
99
99
  # @api private
100
100
  # This is just for debugging purpose.
101
101
  def to_s
102
- format('%s:%3d:%3d: %s',
103
- severity.code, line, real_column, message)
102
+ format('%<severity>s:%3<line>d:%3<column>d: %<message>s',
103
+ severity: severity.code, line: line,
104
+ column: real_column, message: message)
104
105
  end
105
106
 
106
107
  # @api private
@@ -52,6 +52,7 @@ module RuboCop
52
52
  # end
53
53
  class CaseWhenSplat < Cop
54
54
  include Alignment
55
+ include RangeHelp
55
56
 
56
57
  MSG = 'Place `when` conditions with a splat ' \
57
58
  'at the end of the `when` branches.'.freeze
@@ -35,6 +35,13 @@ module RuboCop
35
35
  $(send _ ${:downcase :upcase}))
36
36
  PATTERN
37
37
 
38
+ def_node_matcher :downcase_downcase, <<-PATTERN
39
+ (send
40
+ $(send _ ${:downcase :upcase})
41
+ ${:== :eql? :!=}
42
+ $(send _ ${:downcase :upcase}))
43
+ PATTERN
44
+
38
45
  def on_send(node)
39
46
  return if part_of_ignored_node?(node)
40
47
 
@@ -46,17 +53,19 @@ module RuboCop
46
53
  end
47
54
 
48
55
  def autocorrect(node)
49
- downcase_eq(node) do
56
+ if downcase_downcase(node)
57
+ receiver, method, rhs = *node
58
+ arg, = *rhs
59
+ elsif downcase_eq(node)
50
60
  receiver, method, arg = *node
51
- variable, = *receiver
52
- return correction(node, receiver, method, arg, variable)
53
- end
54
-
55
- eq_downcase(node) do
61
+ elsif eq_downcase(node)
56
62
  arg, method, receiver = *node
57
- variable, = *receiver
58
- return correction(node, receiver, method, arg, variable)
63
+ else
64
+ return
59
65
  end
66
+
67
+ variable, = *receiver
68
+ correction(node, receiver, method, arg, variable)
60
69
  end
61
70
 
62
71
  private
@@ -24,6 +24,8 @@ module RuboCop
24
24
  # array.min_by(&:foo)
25
25
  # array.sort_by { |a| a[:foo] }
26
26
  class CompareWithBlock < Cop
27
+ include RangeHelp
28
+
27
29
  MSG = 'Use `%<compare_method>s_by%<instead>s` instead of ' \
28
30
  '`%<compare_method>s { |%<var_a>s, %<var_b>s| %<str_a>s ' \
29
31
  '<=> %<str_b>s }`.'.freeze
@@ -39,6 +39,7 @@ module RuboCop
39
39
  # Model.where(id: [1, 2, 3]).to_a.count { |m| m.method == true }
40
40
  class Count < Cop
41
41
  include SafeMode
42
+ include RangeHelp
42
43
 
43
44
  MSG = 'Use `count` instead of `%<selector>s...%<counter>s`.'.freeze
44
45
 
@@ -4,6 +4,47 @@ module RuboCop
4
4
  module Cop
5
5
  module Performance
6
6
  # Do not compute the size of statically sized objects.
7
+ #
8
+ # @example
9
+ # # String methods
10
+ # # bad
11
+ # 'foo'.size
12
+ # %q[bar].count
13
+ # %(qux).length
14
+ #
15
+ # # Symbol methods
16
+ # # bad
17
+ # :fred.size
18
+ # :'baz'.length
19
+ #
20
+ # # Array methods
21
+ # # bad
22
+ # [1, 2, thud].count
23
+ # %W(1, 2, bar).size
24
+ #
25
+ # # Hash methods
26
+ # # bad
27
+ # { a: corge, b: grault }.length
28
+ #
29
+ # # good
30
+ # foo.size
31
+ # bar.count
32
+ # qux.length
33
+ #
34
+ # # good
35
+ # :"#{fred}".size
36
+ # CONST = :baz.length
37
+ #
38
+ # # good
39
+ # [1, 2, *thud].count
40
+ # garply = [1, 2, 3]
41
+ # garly.size
42
+ #
43
+ # # good
44
+ # { a: corge, **grault }.length
45
+ # waldo = { a: corge, b: grault }
46
+ # waldo.size
47
+ #
7
48
  class FixedSize < Cop
8
49
  MSG = 'Do not compute the size of statically sized objects.'.freeze
9
50
 
@@ -15,6 +15,8 @@ module RuboCop
15
15
  # [1, 2, 3, 4].map { |e| [e, e] }.flatten
16
16
  # [1, 2, 3, 4].collect { |e| [e, e] }.flatten
17
17
  class FlatMap < Cop
18
+ include RangeHelp
19
+
18
20
  MSG = 'Use `flat_map` instead of `%<method>s...%<flatten>s`.'.freeze
19
21
  FLATTEN_MULTIPLE_LEVELS = ' Beware, `flat_map` only flattens 1 level ' \
20
22
  'and `flatten` can be used to flatten ' \
@@ -14,6 +14,8 @@ module RuboCop
14
14
  # # good
15
15
  # 'abc'.strip
16
16
  class LstripRstrip < Cop
17
+ include RangeHelp
18
+
17
19
  MSG = 'Use `strip` instead of `%<methods>s`.'.freeze
18
20
 
19
21
  def_node_matcher :lstrip_rstrip, <<-PATTERN
@@ -118,7 +118,7 @@ module RuboCop
118
118
  end
119
119
 
120
120
  def max_key_value_pairs
121
- cop_config['MaxKeyValuePairs'].to_i
121
+ Integer(cop_config['MaxKeyValuePairs'])
122
122
  end
123
123
 
124
124
  # A utility class for checking the use of values within an
@@ -16,6 +16,8 @@ module RuboCop
16
16
  # # good
17
17
  # array.sort
18
18
  class RedundantSortBy < Cop
19
+ include RangeHelp
20
+
19
21
  MSG = 'Use `sort` instead of `sort_by { |%<var>s| %<var>s }`.'.freeze
20
22
 
21
23
  def_node_matcher :redundant_sort_by, <<-PATTERN
@@ -154,6 +154,10 @@ module RuboCop
154
154
  def last_match_used?(match_node)
155
155
  scope_root = scope_root(match_node)
156
156
  body = scope_root ? scope_body(scope_root) : match_node.ancestors.last
157
+
158
+ return true if match_node.parent.if_type? &&
159
+ match_node.parent.modifier_form?
160
+
157
161
  match_node_pos = match_node.loc.expression.begin_pos
158
162
 
159
163
  next_match_pos = next_match_pos(body, match_node_pos, scope_root)
@@ -13,6 +13,8 @@ module RuboCop
13
13
  # # good
14
14
  # [].reverse_each
15
15
  class ReverseEach < Cop
16
+ include RangeHelp
17
+
16
18
  MSG = 'Use `reverse_each` instead of `reverse.each`.'.freeze
17
19
  UNDERSCORE = '_'.freeze
18
20
 
@@ -19,6 +19,8 @@ module RuboCop
19
19
  # 'abc'.tr('b', 'd')
20
20
  # 'a b c'.delete(' ')
21
21
  class StringReplacement < Cop
22
+ include RangeHelp
23
+
22
24
  MSG = 'Use `%<prefer>s` instead of `%<current>s`.'.freeze
23
25
  DETERMINISTIC_REGEX = /\A(?:#{LITERAL_REGEX})+\Z/
24
26
  DELETE = 'delete'.freeze