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
@@ -20,6 +20,7 @@ module RuboCop
20
20
  # a**b
21
21
  class SpaceAroundOperators < Cop
22
22
  include PrecedingFollowingAlignment
23
+ include RangeHelp
23
24
 
24
25
  IRREGULAR_METHODS = %i[[] ! []=].freeze
25
26
 
@@ -110,34 +111,35 @@ module RuboCop
110
111
  !IRREGULAR_METHODS.include?(send_node.method_name)
111
112
  end
112
113
 
113
- def check_operator(op, right_operand)
114
- with_space = range_with_surrounding_space(range: op)
114
+ def check_operator(operator, right_operand)
115
+ with_space = range_with_surrounding_space(range: operator)
115
116
  return if with_space.source.start_with?("\n")
116
117
 
117
- offense(op, with_space, right_operand) do |msg|
118
- add_offense(with_space, location: op, message: msg)
118
+ offense(operator, with_space, right_operand) do |msg|
119
+ add_offense(with_space, location: operator, message: msg)
119
120
  end
120
121
  end
121
122
 
122
- def offense(op, with_space, right_operand)
123
- msg = offense_message(op, with_space, right_operand)
123
+ def offense(operator, with_space, right_operand)
124
+ msg = offense_message(operator, with_space, right_operand)
124
125
  yield msg if msg
125
126
  end
126
127
 
127
- def offense_message(op, with_space, right_operand)
128
- if op.is?('**')
128
+ def offense_message(operator, with_space, right_operand)
129
+ if operator.is?('**')
129
130
  'Space around operator `**` detected.' unless with_space.is?('**')
130
131
  elsif with_space.source !~ /^\s.*\s$/
131
- "Surrounding space missing for operator `#{op.source}`."
132
- elsif excess_leading_space?(op, with_space) ||
132
+ "Surrounding space missing for operator `#{operator.source}`."
133
+ elsif excess_leading_space?(operator, with_space) ||
133
134
  excess_trailing_space?(right_operand, with_space)
134
- "Operator `#{op.source}` should be surrounded by a single space."
135
+ "Operator `#{operator.source}` should be surrounded " \
136
+ 'by a single space.'
135
137
  end
136
138
  end
137
139
 
138
- def excess_leading_space?(op, with_space)
140
+ def excess_leading_space?(operator, with_space)
139
141
  with_space.source =~ /^ / &&
140
- (!allow_for_alignment? || !aligned_with_operator?(op))
142
+ (!allow_for_alignment? || !aligned_with_operator?(operator))
141
143
  end
142
144
 
143
145
  def excess_trailing_space?(right_operand, with_space)
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # Checks that block braces have or don't have a space before the opening
7
7
  # brace depending on configuration.
8
8
  #
9
- # @example
9
+ # @example EnforcedStyle: space (default)
10
10
  # # bad
11
11
  # foo.map{ |a|
12
12
  # a.bar.to_s
@@ -16,8 +16,20 @@ module RuboCop
16
16
  # foo.map { |a|
17
17
  # a.bar.to_s
18
18
  # }
19
+ #
20
+ # @example EnforcedStyle: no_space
21
+ # # bad
22
+ # foo.map { |a|
23
+ # a.bar.to_s
24
+ # }
25
+ #
26
+ # # good
27
+ # foo.map{ |a|
28
+ # a.bar.to_s
29
+ # }
19
30
  class SpaceBeforeBlockBraces < Cop
20
31
  include ConfigurableEnforcedStyle
32
+ include RangeHelp
21
33
 
22
34
  MISSING_MSG = 'Space missing to the left of {.'.freeze
23
35
  DETECTED_MSG = 'Space detected to the left of {.'.freeze
@@ -16,10 +16,12 @@ module RuboCop
16
16
  MSG = 'Put a space before an end-of-line comment.'.freeze
17
17
 
18
18
  def investigate(processed_source)
19
- processed_source.tokens.each_cons(2) do |t1, t2|
20
- next unless t2.comment?
21
- next unless t1.line == t2.line
22
- add_offense(t2.pos, location: t2.pos) if t1.pos.end == t2.pos.begin
19
+ processed_source.tokens.each_cons(2) do |token1, token2|
20
+ next unless token2.comment?
21
+ next unless token1.line == token2.line
22
+ if token1.pos.end == token2.pos.begin
23
+ add_offense(token2.pos, location: token2.pos)
24
+ end
23
25
  end
24
26
  end
25
27
 
@@ -23,6 +23,7 @@ module RuboCop
23
23
  #
24
24
  class SpaceBeforeFirstArg < Cop
25
25
  include PrecedingFollowingAlignment
26
+ include RangeHelp
26
27
 
27
28
  MSG = 'Put one space between the method name and ' \
28
29
  'the first argument.'.freeze
@@ -21,6 +21,7 @@ module RuboCop
21
21
  # a = -> (x, y) { x + y }
22
22
  class SpaceInLambdaLiteral < Cop
23
23
  include ConfigurableEnforcedStyle
24
+ include RangeHelp
24
25
 
25
26
  ARROW = '->'.freeze
26
27
  MSG_REQUIRE_SPACE = 'Use a space between `->` and opening brace ' \
@@ -16,7 +16,7 @@ module RuboCop
16
16
  # # good
17
17
  # array = [ a, b, c, d ]
18
18
  #
19
- # @example EnforcedStyle: no_space
19
+ # @example EnforcedStyle: no_space (default)
20
20
  # # The `no_space` style enforces that array literals have
21
21
  # # no surrounding space.
22
22
  #
@@ -36,6 +36,32 @@ module RuboCop
36
36
  #
37
37
  # # good
38
38
  # array = [ a, [ b, c ]]
39
+ #
40
+ #
41
+ # @example EnforcedStyleForEmptyBrackets: no_space (default)
42
+ # # The `no_space` EnforcedStyleForEmptyBrackets style enforces that
43
+ # # empty array brackets do not contain spaces.
44
+ #
45
+ # # bad
46
+ # foo = [ ]
47
+ # bar = [ ]
48
+ #
49
+ # # good
50
+ # foo = []
51
+ # bar = []
52
+ #
53
+ # @example EnforcedStyleForEmptyBrackets: space
54
+ # # The `space` EnforcedStyleForEmptyBrackets style enforces that
55
+ # # empty array brackets contain exactly one space.
56
+ #
57
+ # # bad
58
+ # foo = []
59
+ # bar = [ ]
60
+ #
61
+ # # good
62
+ # foo = [ ]
63
+ # bar = [ ]
64
+ #
39
65
  class SpaceInsideArrayLiteralBrackets < Cop
40
66
  include SurroundingSpace
41
67
  include ConfigurableEnforcedStyle
@@ -47,7 +73,7 @@ module RuboCop
47
73
  return unless node.square_brackets?
48
74
  left, right = array_brackets(node)
49
75
  if empty_brackets?(left, right)
50
- return empty_offenses(node, left, right)
76
+ return empty_offenses(node, left, right, EMPTY_MSG)
51
77
  end
52
78
 
53
79
  start_ok = next_to_newline?(node, left)
@@ -61,7 +87,8 @@ module RuboCop
61
87
 
62
88
  lambda do |corrector|
63
89
  if empty_brackets?(left, right)
64
- empty_corrections(corrector, left, right)
90
+ SpaceCorrector.empty_corrections(processed_source, corrector,
91
+ empty_config, left, right)
65
92
  elsif style == :no_space
66
93
  SpaceCorrector.remove_space(processed_source, corrector,
67
94
  left, right)
@@ -87,52 +114,10 @@ module RuboCop
87
114
  tokens(node).reverse.find(&:right_bracket?)
88
115
  end
89
116
 
90
- def empty_brackets?(left, right)
91
- processed_source.tokens.index(left) ==
92
- processed_source.tokens.index(right) - 1
93
- end
94
-
95
- def empty_offenses(node, left, right)
96
- empty_offense(node, 'Use one') if offending_empty_space?(left, right)
97
- return unless offending_empty_no_space?(left, right)
98
- empty_offense(node, 'Do not use')
99
- end
100
-
101
- def empty_offense(node, command)
102
- add_offense(node, message: format(EMPTY_MSG, command: command))
103
- end
104
-
105
- def offending_empty_space?(left, right)
106
- empty_config == 'space' && !space_between?(left, right)
107
- end
108
-
109
- def offending_empty_no_space?(left, right)
110
- empty_config == 'no_space' && !no_space_between?(left, right)
111
- end
112
-
113
- def space_between?(left, right)
114
- left.end_pos + 1 == right.begin_pos
115
- end
116
-
117
- def no_space_between?(left, right)
118
- left.end_pos == right.begin_pos
119
- end
120
-
121
117
  def empty_config
122
118
  cop_config['EnforcedStyleForEmptyBrackets']
123
119
  end
124
120
 
125
- def empty_corrections(corrector, left, right)
126
- if offending_empty_space?(left, right)
127
- range = side_space_range(range: left.pos, side: :right)
128
- corrector.remove(range)
129
- corrector.insert_after(left.pos, ' ')
130
- elsif offending_empty_no_space?(left, right)
131
- range = side_space_range(range: left.pos, side: :right)
132
- corrector.remove(range)
133
- end
134
- end
135
-
136
121
  def next_to_newline?(node, token)
137
122
  tokens(node)[index_for(node, token) + 1].line != token.line
138
123
  end
@@ -43,7 +43,7 @@ module RuboCop
43
43
  #
44
44
  # @example EnforcedStyleForEmptyBraces: space
45
45
  # # The `space` EnforcedStyleForEmptyBraces style enforces that
46
- # # block braces have at least a spece in between when empty.
46
+ # # block braces have at least a space in between when empty.
47
47
  #
48
48
  # # bad
49
49
  # some_array.each {}
@@ -65,7 +65,7 @@ module RuboCop
65
65
  # # good
66
66
  # [1, 2, 3].each { |n| n * 2 }
67
67
  #
68
- # @example SpaceBeforeBlockParameters: true
68
+ # @example SpaceBeforeBlockParameters: false
69
69
  # # The SpaceBeforeBlockParameters style set to `false` enforces that
70
70
  # # there is no space between `{` and `|`. Overrides `EnforcedStyle`
71
71
  # # if there is a conflict.
@@ -79,6 +79,7 @@ module RuboCop
79
79
  class SpaceInsideBlockBraces < Cop
80
80
  include ConfigurableEnforcedStyle
81
81
  include SurroundingSpace
82
+ include RangeHelp
82
83
 
83
84
  def on_block(node)
84
85
  return if node.keywords?
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # Checks that braces used for hash literals have or don't have
7
7
  # surrounding space depending on configuration.
8
8
  #
9
- # @example EnforcedStyle: space
9
+ # @example EnforcedStyle: space (default)
10
10
  # # The `space` style enforces that hash literals have
11
11
  # # surrounding space.
12
12
  #
@@ -36,11 +36,38 @@ module RuboCop
36
36
  #
37
37
  # # good
38
38
  # h = { a: { b: 2 }}
39
+ #
40
+ #
41
+ # @example EnforcedStyleForEmptyBraces: no_space (default)
42
+ # # The `no_space` EnforcedStyleForEmptyBraces style enforces that
43
+ # # empty hash braces do not contain spaces.
44
+ #
45
+ # # bad
46
+ # foo = { }
47
+ # bar = { }
48
+ #
49
+ # # good
50
+ # foo = {}
51
+ # bar = {}
52
+ #
53
+ # @example EnforcedStyleForEmptyBraces: space
54
+ # # The `space` EnforcedStyleForEmptyBraces style enforces that
55
+ # # empty hash braces contain space.
56
+ #
57
+ # # bad
58
+ # foo = {}
59
+ #
60
+ # # good
61
+ # foo = { }
62
+ # foo = { }
63
+ # foo = { }
64
+ #
39
65
  class SpaceInsideHashLiteralBraces < Cop
40
66
  include SurroundingSpace
41
67
  include ConfigurableEnforcedStyle
68
+ include RangeHelp
42
69
 
43
- MSG = 'Space inside %s.'.freeze
70
+ MSG = 'Space inside %<problem>s.'.freeze
44
71
 
45
72
  def on_hash(node)
46
73
  tokens = processed_source.tokens
@@ -83,24 +110,25 @@ module RuboCop
83
110
  yield begin_index, end_index
84
111
  end
85
112
 
86
- def check(t1, t2)
113
+ def check(token1, token2)
87
114
  # No offense if line break inside.
88
- return if t1.line < t2.line
89
- return if t2.comment? # Also indicates there's a line break.
115
+ return if token1.line < token2.line
116
+ return if token2.comment? # Also indicates there's a line break.
90
117
 
91
- is_empty_braces = t1.left_brace? && t2.right_curly_brace?
92
- expect_space = expect_space?(t1, t2)
118
+ is_empty_braces = token1.left_brace? && token2.right_curly_brace?
119
+ expect_space = expect_space?(token1, token2)
93
120
 
94
- if offense?(t1, expect_space)
95
- incorrect_style_detected(t1, t2, expect_space, is_empty_braces)
121
+ if offense?(token1, expect_space)
122
+ incorrect_style_detected(token1, token2,
123
+ expect_space, is_empty_braces)
96
124
  else
97
125
  correct_style_detected
98
126
  end
99
127
  end
100
128
 
101
- def expect_space?(t1, t2)
102
- is_same_braces = t1.type == t2.type
103
- is_empty_braces = t1.left_brace? && t2.right_curly_brace?
129
+ def expect_space?(token1, token2)
130
+ is_same_braces = token1.type == token2.type
131
+ is_empty_braces = token1.left_brace? && token2.right_curly_brace?
104
132
 
105
133
  if is_same_braces && style == :compact
106
134
  false
@@ -111,8 +139,9 @@ module RuboCop
111
139
  end
112
140
  end
113
141
 
114
- def incorrect_style_detected(t1, t2, expect_space, is_empty_braces)
115
- brace = (t1.text == '{' ? t1 : t2).pos
142
+ def incorrect_style_detected(token1, token2,
143
+ expect_space, is_empty_braces)
144
+ brace = (token1.text == '{' ? token1 : token2).pos
116
145
  range = expect_space ? brace : space_range(brace)
117
146
  add_offense(
118
147
  range,
@@ -120,7 +149,8 @@ module RuboCop
120
149
  message: message(brace, is_empty_braces, expect_space)
121
150
  ) do
122
151
  style = expect_space ? :no_space : :space
123
- ambiguous_or_unexpected_style_detected(style, t1.text == t2.text)
152
+ ambiguous_or_unexpected_style_detected(style,
153
+ token1.text == token2.text)
124
154
  end
125
155
  end
126
156
 
@@ -132,8 +162,8 @@ module RuboCop
132
162
  end
133
163
  end
134
164
 
135
- def offense?(t1, expect_space)
136
- has_space = t1.space_after?
165
+ def offense?(token1, expect_space)
166
+ has_space = token1.space_after?
137
167
  expect_space ? !has_space : has_space
138
168
  end
139
169
 
@@ -144,7 +174,7 @@ module RuboCop
144
174
  brace.source
145
175
  end
146
176
  problem = expect_space ? 'missing' : 'detected'
147
- format(MSG, "#{inside_what} #{problem}")
177
+ format(MSG, problem: "#{inside_what} #{problem}")
148
178
  end
149
179
 
150
180
  def space_range(token_range)
@@ -15,6 +15,7 @@ module RuboCop
15
15
  # g = (a + 3)
16
16
  class SpaceInsideParens < Cop
17
17
  include SurroundingSpace
18
+ include RangeHelp
18
19
 
19
20
  MSG = 'Space inside parentheses detected.'.freeze
20
21
 
@@ -32,20 +33,20 @@ module RuboCop
32
33
  private
33
34
 
34
35
  def each_extraneous_space(tokens)
35
- tokens.each_cons(2) do |t1, t2|
36
- next unless parens?(t1, t2)
36
+ tokens.each_cons(2) do |token1, token2|
37
+ next unless parens?(token1, token2)
37
38
 
38
39
  # If the second token is a comment, that means that a line break
39
40
  # follows, and that the rules for space inside don't apply.
40
- next if t2.comment?
41
- next unless t2.line == t1.line && t1.space_after?
41
+ next if token2.comment?
42
+ next unless token2.line == token1.line && token1.space_after?
42
43
 
43
- yield range_between(t1.end_pos, t2.begin_pos)
44
+ yield range_between(token1.end_pos, token2.begin_pos)
44
45
  end
45
46
  end
46
47
 
47
- def parens?(t1, t2)
48
- t1.left_parens? || t2.right_parens?
48
+ def parens?(token1, token2)
49
+ token1.left_parens? || token2.right_parens?
49
50
  end
50
51
  end
51
52
  end
@@ -29,17 +29,50 @@ module RuboCop
29
29
  # # good
30
30
  # hash[ :key ]
31
31
  # array[ index ]
32
+ #
33
+ #
34
+ # @example EnforcedStyleForEmptyBrackets: no_space (default)
35
+ # # The `no_space` EnforcedStyleForEmptyBrackets style enforces that
36
+ # # empty reference brackets do not contain spaces.
37
+ #
38
+ # # bad
39
+ # foo[ ]
40
+ # foo[ ]
41
+ #
42
+ # # good
43
+ # foo[]
44
+ #
45
+ # @example EnforcedStyleForEmptyBrackets: space
46
+ # # The `space` EnforcedStyleForEmptyBrackets style enforces that
47
+ # # empty reference brackets contain exactly one space.
48
+ #
49
+ # # bad
50
+ # foo[]
51
+ # foo[ ]
52
+ #
53
+ # # good
54
+ # foo[ ]
55
+ #
32
56
  class SpaceInsideReferenceBrackets < Cop
33
57
  include SurroundingSpace
34
58
  include ConfigurableEnforcedStyle
35
59
 
36
60
  MSG = '%<command>s space inside reference brackets.'.freeze
61
+ EMPTY_MSG = '%<command>s space inside empty reference brackets.'.freeze
62
+
63
+ BRACKET_METHODS = %i[[] []=].freeze
37
64
 
38
65
  def on_send(node)
39
66
  return if node.multiline?
40
- return unless left_ref_bracket(node)
41
- left_token = left_ref_bracket(node)
42
- right_token = closing_bracket(node, left_token)
67
+ return unless bracket_method?(node)
68
+ tokens = tokens(node)
69
+ left_token = left_ref_bracket(tokens)
70
+ return unless left_token
71
+ right_token = closing_bracket(tokens, left_token)
72
+
73
+ if empty_brackets?(left_token, right_token)
74
+ return empty_offenses(node, left_token, right_token, EMPTY_MSG)
75
+ end
43
76
 
44
77
  if style == :no_space
45
78
  no_space_offenses(node, left_token, right_token, MSG)
@@ -52,7 +85,10 @@ module RuboCop
52
85
  lambda do |corrector|
53
86
  left, right = reference_brackets(node)
54
87
 
55
- if style == :no_space
88
+ if empty_brackets?(left, right)
89
+ SpaceCorrector.empty_corrections(processed_source, corrector,
90
+ empty_config, left, right)
91
+ elsif style == :no_space
56
92
  SpaceCorrector.remove_space(processed_source, corrector,
57
93
  left, right)
58
94
  else
@@ -64,19 +100,25 @@ module RuboCop
64
100
  private
65
101
 
66
102
  def reference_brackets(node)
67
- left = left_ref_bracket(node)
68
- [left, closing_bracket(node, left)]
103
+ tokens = tokens(node)
104
+ left = left_ref_bracket(tokens)
105
+ [left, closing_bracket(tokens, left)]
69
106
  end
70
107
 
71
- def left_ref_bracket(node)
72
- tokens(node).reverse.find(&:left_ref_bracket?)
108
+ def bracket_method?(node)
109
+ _, method, = *node
110
+ BRACKET_METHODS.include?(method)
73
111
  end
74
112
 
75
- def closing_bracket(node, opening_bracket)
76
- i = tokens(node).index(opening_bracket)
113
+ def left_ref_bracket(tokens)
114
+ tokens.reverse.find(&:left_ref_bracket?)
115
+ end
116
+
117
+ def closing_bracket(tokens, opening_bracket)
118
+ i = tokens.index(opening_bracket)
77
119
  inner_left_brackets_needing_closure = 0
78
120
 
79
- tokens(node)[i..-1].each do |token|
121
+ tokens[i..-1].each do |token|
80
122
  inner_left_brackets_needing_closure += 1 if token.left_bracket?
81
123
  inner_left_brackets_needing_closure -= 1 if token.right_bracket?
82
124
  if inner_left_brackets_needing_closure.zero? && token.right_bracket?
@@ -84,6 +126,10 @@ module RuboCop
84
126
  end
85
127
  end
86
128
  end
129
+
130
+ def empty_config
131
+ cop_config['EnforcedStyleForEmptyBrackets']
132
+ end
87
133
  end
88
134
  end
89
135
  end