rubocop 0.49.1 → 0.50.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 (292) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -3
  3. data/bin/rubocop +1 -1
  4. data/config/default.yml +160 -99
  5. data/config/disabled.yml +4 -5
  6. data/config/enabled.yml +149 -47
  7. data/lib/rubocop.rb +41 -14
  8. data/lib/rubocop/ast/builder.rb +4 -1
  9. data/lib/rubocop/ast/node.rb +36 -42
  10. data/lib/rubocop/ast/node/args_node.rb +1 -13
  11. data/lib/rubocop/ast/node/array_node.rb +9 -0
  12. data/lib/rubocop/ast/node/block_node.rb +9 -0
  13. data/lib/rubocop/ast/node/def_node.rb +71 -0
  14. data/lib/rubocop/ast/node/for_node.rb +8 -0
  15. data/lib/rubocop/ast/node/if_node.rb +10 -2
  16. data/lib/rubocop/ast/node/mixin/collection_node.rb +15 -0
  17. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +174 -0
  18. data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +89 -0
  19. data/lib/rubocop/ast/node/mixin/parameterized_node.rb +18 -31
  20. data/lib/rubocop/ast/node/regexp_node.rb +35 -0
  21. data/lib/rubocop/ast/node/send_node.rb +1 -154
  22. data/lib/rubocop/ast/node/super_node.rb +3 -24
  23. data/lib/rubocop/ast/node/yield_node.rb +21 -0
  24. data/lib/rubocop/ast/traversal.rb +6 -6
  25. data/lib/rubocop/cli.rb +7 -3
  26. data/lib/rubocop/config.rb +45 -8
  27. data/lib/rubocop/config_loader.rb +7 -5
  28. data/lib/rubocop/cop/bundler/duplicated_gem.rb +3 -3
  29. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +64 -0
  30. data/lib/rubocop/cop/bundler/ordered_gems.rb +12 -12
  31. data/lib/rubocop/cop/commissioner.rb +8 -2
  32. data/lib/rubocop/cop/cop.rb +3 -1
  33. data/lib/rubocop/cop/generator.rb +94 -21
  34. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  35. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +14 -3
  36. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +43 -0
  37. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +46 -0
  38. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +49 -0
  39. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +1 -1
  40. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +2 -3
  41. data/lib/rubocop/cop/layout/align_array.rb +2 -2
  42. data/lib/rubocop/cop/layout/align_hash.rb +2 -2
  43. data/lib/rubocop/cop/layout/align_parameters.rb +5 -11
  44. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +5 -5
  45. data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
  46. data/lib/rubocop/cop/layout/dot_position.rb +9 -0
  47. data/lib/rubocop/cop/layout/else_alignment.rb +30 -13
  48. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +4 -0
  49. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +20 -4
  50. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +3 -3
  51. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +3 -3
  52. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +3 -3
  53. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +5 -2
  54. data/lib/rubocop/cop/layout/indent_heredoc.rb +19 -24
  55. data/lib/rubocop/cop/layout/indentation_consistency.rb +1 -2
  56. data/lib/rubocop/cop/layout/indentation_width.rb +12 -8
  57. data/lib/rubocop/cop/layout/leading_comment_space.rb +33 -18
  58. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +20 -17
  59. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +3 -3
  60. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +6 -0
  61. data/lib/rubocop/cop/layout/space_after_colon.rb +7 -0
  62. data/lib/rubocop/cop/layout/space_after_comma.rb +10 -0
  63. data/lib/rubocop/cop/layout/space_after_method_name.rb +5 -3
  64. data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
  65. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +13 -4
  66. data/lib/rubocop/cop/layout/space_around_keyword.rb +9 -5
  67. data/lib/rubocop/cop/layout/space_before_block_braces.rb +54 -5
  68. data/lib/rubocop/cop/layout/space_before_comment.rb +7 -0
  69. data/lib/rubocop/cop/layout/space_before_semicolon.rb +7 -0
  70. data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +1 -1
  71. data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
  72. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
  73. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +8 -4
  74. data/lib/rubocop/cop/layout/tab.rb +1 -1
  75. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -2
  76. data/lib/rubocop/cop/lint/assignment_in_condition.rb +15 -1
  77. data/lib/rubocop/cop/lint/block_alignment.rb +15 -6
  78. data/lib/rubocop/cop/lint/boolean_symbol.rb +38 -0
  79. data/lib/rubocop/cop/lint/condition_position.rb +5 -1
  80. data/lib/rubocop/cop/lint/debugger.rb +16 -9
  81. data/lib/rubocop/cop/lint/def_end_alignment.rb +4 -4
  82. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +3 -3
  83. data/lib/rubocop/cop/lint/duplicate_methods.rb +73 -5
  84. data/lib/rubocop/cop/lint/duplicated_key.rb +1 -1
  85. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  86. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  87. data/lib/rubocop/cop/lint/empty_expression.rb +1 -1
  88. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  89. data/lib/rubocop/cop/lint/empty_when.rb +1 -1
  90. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  91. data/lib/rubocop/cop/lint/float_out_of_range.rb +5 -5
  92. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +35 -40
  93. data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
  94. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +18 -13
  95. data/lib/rubocop/cop/lint/inherit_exception.rb +8 -7
  96. data/lib/rubocop/cop/lint/interpolation_check.rb +36 -0
  97. data/lib/rubocop/cop/lint/literal_in_condition.rb +3 -3
  98. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  99. data/lib/rubocop/cop/lint/multiple_compare.rb +1 -1
  100. data/lib/rubocop/cop/lint/nested_method_definition.rb +5 -7
  101. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
  102. data/lib/rubocop/cop/lint/percent_string_array.rb +3 -12
  103. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  104. data/lib/rubocop/cop/lint/rand_one.rb +7 -1
  105. data/lib/rubocop/cop/lint/redundant_with_index.rb +77 -0
  106. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
  107. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  108. data/lib/rubocop/cop/lint/rescue_type.rb +13 -6
  109. data/lib/rubocop/cop/lint/rescue_without_error_class.rb +38 -0
  110. data/lib/rubocop/cop/lint/return_in_void_context.rb +63 -0
  111. data/lib/rubocop/cop/lint/script_permission.rb +6 -0
  112. data/lib/rubocop/cop/lint/syntax.rb +17 -20
  113. data/lib/rubocop/cop/lint/unified_integer.rb +3 -2
  114. data/lib/rubocop/cop/lint/unneeded_disable.rb +1 -1
  115. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
  116. data/lib/rubocop/cop/lint/unreachable_code.rb +53 -8
  117. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +74 -0
  118. data/lib/rubocop/cop/lint/uri_regexp.rb +73 -0
  119. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -8
  120. data/lib/rubocop/cop/lint/useless_setter_call.rb +10 -11
  121. data/lib/rubocop/cop/lint/void.rb +29 -23
  122. data/lib/rubocop/cop/metrics/line_length.rb +2 -2
  123. data/lib/rubocop/cop/metrics/method_length.rb +8 -3
  124. data/lib/rubocop/cop/metrics/parameter_lists.rb +5 -2
  125. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +1 -1
  126. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -2
  127. data/lib/rubocop/cop/mixin/first_element_line_break.rb +12 -3
  128. data/lib/rubocop/cop/mixin/heredoc.rb +28 -0
  129. data/lib/rubocop/cop/mixin/method_complexity.rb +9 -6
  130. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +68 -31
  131. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +18 -0
  132. data/lib/rubocop/cop/mixin/negative_conditional.rb +1 -1
  133. data/lib/rubocop/cop/mixin/percent_array.rb +52 -0
  134. data/lib/rubocop/cop/mixin/string_help.rb +1 -1
  135. data/lib/rubocop/cop/{style → naming}/accessor_method_name.rb +11 -12
  136. data/lib/rubocop/cop/{style → naming}/ascii_identifiers.rb +1 -1
  137. data/lib/rubocop/cop/{style/op_method.rb → naming/binary_operator_parameter_name.rb} +2 -2
  138. data/lib/rubocop/cop/{style → naming}/class_and_module_camel_case.rb +1 -1
  139. data/lib/rubocop/cop/{style → naming}/constant_name.rb +1 -1
  140. data/lib/rubocop/cop/{style → naming}/file_name.rb +8 -4
  141. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +68 -0
  142. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +58 -0
  143. data/lib/rubocop/cop/{style → naming}/method_name.rb +1 -1
  144. data/lib/rubocop/cop/{style → naming}/predicate_name.rb +6 -7
  145. data/lib/rubocop/cop/{style → naming}/variable_name.rb +11 -15
  146. data/lib/rubocop/cop/{style → naming}/variable_number.rb +1 -1
  147. data/lib/rubocop/cop/performance/caller.rb +39 -11
  148. data/lib/rubocop/cop/performance/casecmp.rb +4 -4
  149. data/lib/rubocop/cop/performance/compare_with_block.rb +4 -4
  150. data/lib/rubocop/cop/performance/double_start_end_with.rb +4 -4
  151. data/lib/rubocop/cop/performance/end_with.rb +3 -3
  152. data/lib/rubocop/cop/performance/fixed_size.rb +1 -1
  153. data/lib/rubocop/cop/performance/hash_each_methods.rb +66 -25
  154. data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -2
  155. data/lib/rubocop/cop/performance/range_include.rb +2 -2
  156. data/lib/rubocop/cop/performance/redundant_block_call.rb +6 -6
  157. data/lib/rubocop/cop/performance/redundant_match.rb +5 -5
  158. data/lib/rubocop/cop/performance/redundant_merge.rb +39 -23
  159. data/lib/rubocop/cop/performance/redundant_sort_by.rb +2 -2
  160. data/lib/rubocop/cop/performance/regexp_match.rb +13 -5
  161. data/lib/rubocop/cop/performance/size.rb +1 -1
  162. data/lib/rubocop/cop/performance/start_with.rb +3 -3
  163. data/lib/rubocop/cop/performance/times_map.rb +23 -12
  164. data/lib/rubocop/cop/performance/unfreeze_string.rb +50 -0
  165. data/lib/rubocop/cop/performance/uri_default_parser.rb +47 -0
  166. data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -1
  167. data/lib/rubocop/cop/rails/delegate.rb +36 -7
  168. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
  169. data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -2
  170. data/lib/rubocop/cop/rails/file_path.rb +3 -4
  171. data/lib/rubocop/cop/rails/find_each.rb +1 -1
  172. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +48 -0
  173. data/lib/rubocop/cop/rails/http_positional_arguments.rb +5 -5
  174. data/lib/rubocop/cop/rails/not_null_column.rb +1 -1
  175. data/lib/rubocop/cop/rails/pluralization_grammar.rb +2 -2
  176. data/lib/rubocop/cop/rails/relative_date_constant.rb +1 -1
  177. data/lib/rubocop/cop/rails/request_referer.rb +2 -2
  178. data/lib/rubocop/cop/rails/reversible_migration.rb +12 -12
  179. data/lib/rubocop/cop/rails/save_bang.rb +8 -6
  180. data/lib/rubocop/cop/rails/scope_args.rb +1 -1
  181. data/lib/rubocop/cop/security/eval.rb +2 -2
  182. data/lib/rubocop/cop/security/json_load.rb +2 -2
  183. data/lib/rubocop/cop/security/marshal_load.rb +2 -2
  184. data/lib/rubocop/cop/security/yaml_load.rb +2 -2
  185. data/lib/rubocop/cop/style/alias.rb +44 -20
  186. data/lib/rubocop/cop/style/and_or.rb +48 -34
  187. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +1 -1
  188. data/lib/rubocop/cop/style/block_comments.rb +3 -1
  189. data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
  190. data/lib/rubocop/cop/style/command_literal.rb +20 -9
  191. data/lib/rubocop/cop/style/conditional_assignment.rb +30 -28
  192. data/lib/rubocop/cop/style/copyright.rb +10 -10
  193. data/lib/rubocop/cop/style/def_with_parentheses.rb +6 -5
  194. data/lib/rubocop/cop/style/dir.rb +52 -0
  195. data/lib/rubocop/cop/style/documentation_method.rb +2 -6
  196. data/lib/rubocop/cop/style/empty_case_condition.rb +1 -1
  197. data/lib/rubocop/cop/style/empty_else.rb +3 -2
  198. data/lib/rubocop/cop/style/empty_literal.rb +1 -2
  199. data/lib/rubocop/cop/style/empty_method.rb +27 -17
  200. data/lib/rubocop/cop/style/flip_flop.rb +2 -2
  201. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  202. data/lib/rubocop/cop/style/guard_clause.rb +4 -2
  203. data/lib/rubocop/cop/style/hash_syntax.rb +10 -10
  204. data/lib/rubocop/cop/style/identical_conditional_branches.rb +5 -1
  205. data/lib/rubocop/cop/style/if_with_semicolon.rb +1 -1
  206. data/lib/rubocop/cop/style/implicit_runtime_error.rb +4 -3
  207. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  208. data/lib/rubocop/cop/style/inverse_methods.rb +20 -8
  209. data/lib/rubocop/cop/style/lambda.rb +19 -9
  210. data/lib/rubocop/cop/style/lambda_call.rb +22 -1
  211. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +4 -20
  212. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +6 -4
  213. data/lib/rubocop/cop/style/method_def_parentheses.rb +18 -26
  214. data/lib/rubocop/cop/style/method_missing.rb +5 -18
  215. data/lib/rubocop/cop/style/min_max.rb +67 -0
  216. data/lib/rubocop/cop/style/missing_else.rb +16 -3
  217. data/lib/rubocop/cop/style/mixin_grouping.rb +2 -2
  218. data/lib/rubocop/cop/style/module_function.rb +8 -4
  219. data/lib/rubocop/cop/style/multiline_if_modifier.rb +5 -1
  220. data/lib/rubocop/cop/style/multiline_memoization.rb +25 -3
  221. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  222. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
  223. data/lib/rubocop/cop/style/mutable_constant.rb +2 -6
  224. data/lib/rubocop/cop/style/negated_if.rb +8 -4
  225. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +8 -8
  226. data/lib/rubocop/cop/style/nested_ternary_operator.rb +1 -1
  227. data/lib/rubocop/cop/style/non_nil_check.rb +14 -14
  228. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +6 -2
  229. data/lib/rubocop/cop/style/numeric_literals.rb +2 -2
  230. data/lib/rubocop/cop/style/numeric_predicate.rb +8 -4
  231. data/lib/rubocop/cop/style/one_line_conditional.rb +8 -3
  232. data/lib/rubocop/cop/style/option_hash.rb +1 -1
  233. data/lib/rubocop/cop/style/optional_arguments.rb +1 -2
  234. data/lib/rubocop/cop/style/or_assignment.rb +88 -0
  235. data/lib/rubocop/cop/style/parallel_assignment.rb +2 -2
  236. data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -11
  237. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  238. data/lib/rubocop/cop/style/percent_q_literals.rb +2 -2
  239. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  240. data/lib/rubocop/cop/style/proc.rb +1 -1
  241. data/lib/rubocop/cop/style/raise_args.rb +16 -17
  242. data/lib/rubocop/cop/style/redundant_begin.rb +6 -5
  243. data/lib/rubocop/cop/style/redundant_conditional.rb +95 -0
  244. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  245. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -11
  246. data/lib/rubocop/cop/style/redundant_return.rb +23 -11
  247. data/lib/rubocop/cop/style/redundant_self.rb +18 -9
  248. data/lib/rubocop/cop/style/regexp_literal.rb +12 -4
  249. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  250. data/lib/rubocop/cop/style/return_nil.rb +98 -0
  251. data/lib/rubocop/cop/style/safe_navigation.rb +80 -43
  252. data/lib/rubocop/cop/style/single_line_block_params.rb +14 -13
  253. data/lib/rubocop/cop/style/single_line_methods.rb +9 -13
  254. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  255. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +17 -39
  256. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +22 -1
  257. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  258. data/lib/rubocop/cop/style/symbol_array.rb +5 -25
  259. data/lib/rubocop/cop/style/symbol_literal.rb +1 -1
  260. data/lib/rubocop/cop/style/symbol_proc.rb +3 -18
  261. data/lib/rubocop/cop/style/ternary_parentheses.rb +14 -10
  262. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +28 -9
  263. data/lib/rubocop/cop/style/trivial_accessors.rb +39 -56
  264. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  265. data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -1
  266. data/lib/rubocop/cop/style/unneeded_interpolation.rb +1 -1
  267. data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
  268. data/lib/rubocop/cop/style/variable_interpolation.rb +8 -3
  269. data/lib/rubocop/cop/style/word_array.rb +7 -24
  270. data/lib/rubocop/cop/style/yoda_condition.rb +49 -14
  271. data/lib/rubocop/cop/style/zero_length_predicate.rb +25 -18
  272. data/lib/rubocop/cop/team.rb +16 -8
  273. data/lib/rubocop/cop/util.rb +11 -0
  274. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  275. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  276. data/lib/rubocop/formatter/offense_count_formatter.rb +2 -0
  277. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  278. data/lib/rubocop/formatter/tap_formatter.rb +71 -0
  279. data/lib/rubocop/formatter/worst_offenders_formatter.rb +2 -0
  280. data/lib/rubocop/node_pattern.rb +44 -26
  281. data/lib/rubocop/options.rb +1 -0
  282. data/lib/rubocop/processed_source.rb +3 -1
  283. data/lib/rubocop/remote_config.rb +5 -1
  284. data/lib/rubocop/result_cache.rb +1 -0
  285. data/lib/rubocop/rspec/cop_helper.rb +10 -10
  286. data/lib/rubocop/rspec/expect_offense.rb +6 -8
  287. data/lib/rubocop/rspec/shared_examples.rb +8 -8
  288. data/lib/rubocop/string_util.rb +2 -0
  289. data/lib/rubocop/version.rb +1 -1
  290. metadata +51 -18
  291. data/lib/rubocop/cop/mixin/access_modifier_node.rb +0 -41
  292. data/lib/rubocop/cop/mixin/on_method_def.rb +0 -44
@@ -15,7 +15,6 @@ module RuboCop
15
15
  # end
16
16
  class IndentationConsistency < Cop
17
17
  include AutocorrectAlignment
18
- include AccessModifierNode
19
18
  include ConfigurableEnforcedStyle
20
19
 
21
20
  MSG = 'Inconsistent indentation detected.'.freeze
@@ -37,7 +36,7 @@ module RuboCop
37
36
  # the AccessModifierIndentation cop. This cop uses them as dividers
38
37
  # in rails mode. Then consistency is checked only within each
39
38
  # section delimited by a modifier node.
40
- if modifier_node?(child)
39
+ if child.send_type? && child.access_modifier?
41
40
  children_to_check << [] if style == :rails
42
41
  else
43
42
  children_to_check.last << child
@@ -48,9 +48,7 @@ module RuboCop
48
48
  class IndentationWidth < Cop
49
49
  include EndKeywordAlignment
50
50
  include AutocorrectAlignment
51
- include OnMethodDef
52
51
  include CheckAssignment
53
- include AccessModifierNode
54
52
  include IgnoredPattern
55
53
 
56
54
  SPECIAL_MODIFIERS = %w[private protected].freeze
@@ -98,7 +96,7 @@ module RuboCop
98
96
 
99
97
  def on_send(node)
100
98
  super
101
- return unless modifier_and_def_on_same_line?(node)
99
+ return unless node.adjacent_def_modifier?
102
100
 
103
101
  *_, body = *node.first_argument
104
102
 
@@ -110,9 +108,12 @@ module RuboCop
110
108
  ignore_node(node.first_argument)
111
109
  end
112
110
 
113
- def on_method_def(node, _method_name, _args, body)
114
- check_indentation(node.loc.keyword, body) unless ignored_node?(node)
111
+ def on_def(node)
112
+ return if ignored_node?(node)
113
+
114
+ check_indentation(node.loc.keyword, node.body)
115
115
  end
116
+ alias on_defs on_def
116
117
 
117
118
  def on_while(node, base = node)
118
119
  return if ignored_node?(node)
@@ -157,7 +158,7 @@ module RuboCop
157
158
  def each_member(members)
158
159
  previous_modifier = nil
159
160
  members.first.children.each do |member|
160
- if special_modifier?(member)
161
+ if member.send_type? && special_modifier?(member)
161
162
  previous_modifier = member
162
163
  elsif previous_modifier
163
164
  yield member, previous_modifier.source_range
@@ -167,7 +168,7 @@ module RuboCop
167
168
  end
168
169
 
169
170
  def special_modifier?(node)
170
- modifier_node?(node) && SPECIAL_MODIFIERS.include?(node.source)
171
+ node.access_modifier? && SPECIAL_MODIFIERS.include?(node.source)
171
172
  end
172
173
 
173
174
  def indentation_consistency_style
@@ -289,7 +290,10 @@ module RuboCop
289
290
  end
290
291
 
291
292
  def starts_with_access_modifier?(body_node)
292
- body_node.begin_type? && modifier_node?(body_node.children.first)
293
+ return unless body_node.begin_type?
294
+
295
+ starting_node = body_node.children.first
296
+ starting_node.send_type? && starting_node.access_modifier?
293
297
  end
294
298
 
295
299
  def configured_indentation_width
@@ -3,39 +3,54 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Layout
6
- # This cop checks whether comments have a leading space
7
- # after the # denoting the start of the comment. The
8
- # leading space is not required for some RDoc special syntax,
9
- # like #++, #--, #:nodoc, etc. Neither is it required for
10
- # =begin/=end comments.
6
+ # This cop checks whether comments have a leading space after the
7
+ # `#` denoting the start of the comment. The leading space is not
8
+ # required for some RDoc special syntax, like `#++`, `#--`,
9
+ # `#:nodoc`, `=begin`- and `=end` comments, "shebang" directives,
10
+ # or rackup options.
11
+ #
12
+ # @example
13
+ #
14
+ # # bad
15
+ # #Some comment
16
+ #
17
+ # # good
18
+ # # Some comment
11
19
  class LeadingCommentSpace < Cop
12
- MSG = 'Missing space after #.'.freeze
20
+ MSG = 'Missing space after `#`.'.freeze
13
21
 
14
22
  def investigate(processed_source)
15
23
  processed_source.comments.each do |comment|
16
24
  next unless comment.text =~ /\A#+[^#\s=:+-]/
17
- next if comment.text.start_with?('#!') && comment.loc.line == 1
25
+ next if comment.loc.line == 1 && allowed_on_first_line?(comment)
18
26
 
19
- # in config.ru files, if the first line starts with #\ it is treated
20
- # as options (e.g. `#\ -p 8765` sets the request port to 8765)
21
- next if comment.text.start_with?('#\\') && comment.loc.line == 1 &&
22
- config_ru?(processed_source.buffer.name)
23
-
24
- add_offense(comment, :expression)
27
+ add_offense(comment)
25
28
  end
26
29
  end
27
30
 
31
+ private
32
+
28
33
  def autocorrect(comment)
29
34
  expr = comment.loc.expression
30
- b = expr.begin_pos
31
- hash_mark = range_between(b, b + 1)
35
+ hash_mark = range_between(expr.begin_pos, expr.begin_pos + 1)
36
+
32
37
  ->(corrector) { corrector.insert_after(hash_mark, ' ') }
33
38
  end
34
39
 
35
- private
40
+ def allowed_on_first_line?(comment)
41
+ shebang?(comment) || rackup_config_file? && rackup_options?(comment)
42
+ end
43
+
44
+ def shebang?(comment)
45
+ comment.text.start_with?('#!')
46
+ end
47
+
48
+ def rackup_options?(comment)
49
+ comment.text.start_with?('#\\')
50
+ end
36
51
 
37
- def config_ru?(file_path)
38
- File.basename(file_path).eql?('config.ru')
52
+ def rackup_config_file?
53
+ File.basename(processed_source.buffer.name).eql?('config.ru')
39
54
  end
40
55
  end
41
56
  end
@@ -144,25 +144,21 @@ module RuboCop
144
144
  def syntactic_alignment_base(lhs, rhs)
145
145
  # a if b
146
146
  # .c
147
- n = kw_node_with_special_indentation(lhs)
148
- if n
149
- case n.type
150
- when :for then _, expression, = *n
151
- when :return then expression, = *n
152
- when *MODIFIER_NODES then expression, = *n
153
- end
154
- return expression.source_range
147
+ kw_node_with_special_indentation(lhs) do |base|
148
+ return indented_keyword_expression(base).source_range
155
149
  end
156
150
 
157
151
  # a = b
158
152
  # .c
159
- n = part_of_assignment_rhs(lhs, rhs)
160
- return assignment_rhs(n).source_range if n
153
+ part_of_assignment_rhs(lhs, rhs) do |base|
154
+ return assignment_rhs(base).source_range
155
+ end
161
156
 
162
157
  # a + b
163
158
  # .c
164
- n = operation_rhs(lhs)
165
- return n.source_range if n
159
+ operation_rhs(lhs) do |base|
160
+ return base.source_range
161
+ end
166
162
  end
167
163
 
168
164
  # a.b
@@ -202,12 +198,19 @@ module RuboCop
202
198
 
203
199
  def operation_rhs(node)
204
200
  receiver, = *node
205
- receiver.each_ancestor(:send) do |a|
206
- _, method, args = *a
207
- return args if operator?(method) && args &&
208
- within_node?(receiver, args)
201
+
202
+ operation_rhs = receiver.each_ancestor(:send).find do |rhs|
203
+ operator_rhs?(rhs, receiver)
209
204
  end
210
- nil
205
+
206
+ return unless operation_rhs
207
+
208
+ yield operation_rhs.first_argument
209
+ end
210
+
211
+ def operator_rhs?(node, receiver)
212
+ node.operator_method? && node.arguments? &&
213
+ within_node?(receiver, node.first_argument)
211
214
  end
212
215
  end
213
216
  end
@@ -56,7 +56,6 @@ module RuboCop
56
56
  # b
57
57
  # )
58
58
  class MultilineMethodDefinitionBraceLayout < Cop
59
- include OnMethodDef
60
59
  include MultilineLiteralBraceLayout
61
60
 
62
61
  SAME_LINE_MESSAGE = 'Closing method definition brace must be on the ' \
@@ -73,9 +72,10 @@ module RuboCop
73
72
  ALWAYS_SAME_LINE_MESSAGE = 'Closing method definition brace must be ' \
74
73
  'on the same line as the last parameter.'.freeze
75
74
 
76
- def on_method_def(_node, _method_name, args, _body)
77
- check_brace_layout(args)
75
+ def on_def(node)
76
+ check_brace_layout(node.arguments)
78
77
  end
78
+ alias on_defs on_def
79
79
  end
80
80
  end
81
81
  end
@@ -12,6 +12,12 @@ module RuboCop
12
12
  # b
13
13
  # something
14
14
  # end
15
+ #
16
+ # # good
17
+ # if a +
18
+ # b
19
+ # something
20
+ # end
15
21
  class MultilineOperationIndentation < Cop
16
22
  include ConfigurableEnforcedStyle
17
23
  include AutocorrectAlignment
@@ -6,6 +6,13 @@ module RuboCop
6
6
  # Checks for colon (:) not followed by some kind of space.
7
7
  # N.B. this cop does not handle spaces after a ternary operator, which are
8
8
  # instead handled by Layout/SpaceAroundOperators.
9
+ #
10
+ # @example
11
+ # # bad
12
+ # def f(a:, b:2); {a:3}; end
13
+ #
14
+ # # good
15
+ # def f(a:, b: 2); {a: 3}; end
9
16
  class SpaceAfterColon < Cop
10
17
  MSG = 'Space missing after colon.'.freeze
11
18
 
@@ -4,6 +4,16 @@ module RuboCop
4
4
  module Cop
5
5
  module Layout
6
6
  # Checks for comma (,) not followed by some kind of space.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ # 1,2
12
+ # { foo:bar,}
13
+ #
14
+ # # good
15
+ # 1, 2
16
+ # { foo:bar, }
7
17
  class SpaceAfterComma < Cop
8
18
  include SpaceAfterPunctuation
9
19
 
@@ -13,12 +13,11 @@ module RuboCop
13
13
  # # good
14
14
  # def func(x) ... end
15
15
  class SpaceAfterMethodName < Cop
16
- include OnMethodDef
17
-
18
16
  MSG = 'Do not put a space between a method name and the opening ' \
19
17
  'parenthesis.'.freeze
20
18
 
21
- def on_method_def(_node, _method_name, args, _body)
19
+ def on_def(node)
20
+ args = node.arguments
22
21
  return unless args.loc.begin && args.loc.begin.is?('(')
23
22
  expr = args.source_range
24
23
  pos_before_left_paren = range_between(expr.begin_pos - 1,
@@ -27,6 +26,9 @@ module RuboCop
27
26
 
28
27
  add_offense(pos_before_left_paren, pos_before_left_paren)
29
28
  end
29
+ alias on_defs on_def
30
+
31
+ private
30
32
 
31
33
  def autocorrect(pos_before_left_paren)
32
34
  ->(corrector) { corrector.remove(pos_before_left_paren) }
@@ -17,7 +17,7 @@ module RuboCop
17
17
  def on_send(node)
18
18
  return unless node.keyword_bang? && whitespace_after_operator?(node)
19
19
 
20
- add_offense(node, :expression)
20
+ add_offense(node)
21
21
  end
22
22
 
23
23
  def whitespace_after_operator?(node)
@@ -75,16 +75,25 @@ module RuboCop
75
75
  end
76
76
 
77
77
  def check_space_style_inside_pipes(args, opening_pipe, closing_pipe)
78
+ check_opening_pipe_space(args, opening_pipe)
79
+ check_closing_pipe_space(args, closing_pipe)
80
+ end
81
+
82
+ def check_opening_pipe_space(args, opening_pipe)
78
83
  first = args.first.source_range
79
- last = args.last.source_range
80
- last_end_pos = last_end_pos_inside_pipes(last.end_pos)
81
84
 
82
85
  check_space(opening_pipe.end_pos, first.begin_pos, first,
83
86
  'before first block parameter')
84
- check_space(last_end_pos, closing_pipe.begin_pos, last,
85
- 'after last block parameter')
86
87
  check_no_space(opening_pipe.end_pos, first.begin_pos - 1,
87
88
  'Extra space before first')
89
+ end
90
+
91
+ def check_closing_pipe_space(args, closing_pipe)
92
+ last = args.last.source_range
93
+ last_end_pos = last_end_pos_inside_pipes(last.end_pos)
94
+
95
+ check_space(last_end_pos, closing_pipe.begin_pos, last,
96
+ 'after last block parameter')
88
97
  check_no_space(last_end_pos + 1, closing_pipe.begin_pos,
89
98
  'Extra space after last')
90
99
  end
@@ -178,16 +178,20 @@ module RuboCop
178
178
  def space_after_missing?(range)
179
179
  pos = range.end_pos
180
180
  char = range.source_buffer.source[pos]
181
- return false unless char
182
- return false if accept_left_parenthesis?(range) &&
183
- char == '('.freeze
184
- return false if accept_left_square_bracket?(range) &&
185
- char == '['.freeze
181
+
182
+ return false if accepted_opening_delimiter?(range, char)
186
183
  return false if safe_navigation_call?(range, pos)
187
184
 
188
185
  char !~ /[\s;,#\\\)\}\]\.]/
189
186
  end
190
187
 
188
+ def accepted_opening_delimiter?(range, char)
189
+ return true unless char
190
+
191
+ accept_left_square_bracket?(range) && char == '[' ||
192
+ accept_left_parenthesis?(range) && char == '('
193
+ end
194
+
191
195
  def accept_left_parenthesis?(range)
192
196
  ACCEPT_LEFT_PAREN.include?(range.source)
193
197
  end
@@ -5,9 +5,23 @@ module RuboCop
5
5
  module Layout
6
6
  # Checks that block braces have or don't have a space before the opening
7
7
  # brace depending on configuration.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # foo.map{ |a|
12
+ # a.bar.to_s
13
+ # }
14
+ #
15
+ # # good
16
+ # foo.map { |a|
17
+ # a.bar.to_s
18
+ # }
8
19
  class SpaceBeforeBlockBraces < Cop
9
20
  include ConfigurableEnforcedStyle
10
21
 
22
+ MISSING_MSG = 'Space missing to the left of {.'.freeze
23
+ DETECTED_MSG = 'Space detected to the left of {.'.freeze
24
+
11
25
  def self.autocorrect_incompatible_with
12
26
  [Style::SymbolProc]
13
27
  end
@@ -20,6 +34,31 @@ module RuboCop
20
34
  used_style =
21
35
  space_plus_brace.source.start_with?('{') ? :no_space : :space
22
36
 
37
+ if empty_braces?(node.loc)
38
+ check_empty(left_brace, space_plus_brace, used_style)
39
+ else
40
+ check_non_empty(left_brace, space_plus_brace, used_style)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def check_empty(left_brace, space_plus_brace, used_style)
47
+ return if style_for_empty_braces == used_style
48
+
49
+ config_to_allow_offenses['EnforcedStyleForEmptyBraces'] =
50
+ used_style.to_s
51
+
52
+ if style_for_empty_braces == :space
53
+ add_offense(left_brace, left_brace, MISSING_MSG)
54
+ else
55
+ space = range_between(space_plus_brace.begin_pos,
56
+ left_brace.begin_pos)
57
+ add_offense(space, space, DETECTED_MSG)
58
+ end
59
+ end
60
+
61
+ def check_non_empty(left_brace, space_plus_brace, used_style)
23
62
  case used_style
24
63
  when style then correct_style_detected
25
64
  when :space then space_detected(left_brace, space_plus_brace)
@@ -27,11 +66,8 @@ module RuboCop
27
66
  end
28
67
  end
29
68
 
30
- private
31
-
32
69
  def space_missing(left_brace)
33
- add_offense(left_brace, left_brace,
34
- 'Space missing to the left of {.') do
70
+ add_offense(left_brace, left_brace, MISSING_MSG) do
35
71
  opposite_style_detected
36
72
  end
37
73
  end
@@ -39,11 +75,20 @@ module RuboCop
39
75
  def space_detected(left_brace, space_plus_brace)
40
76
  space = range_between(space_plus_brace.begin_pos,
41
77
  left_brace.begin_pos)
42
- add_offense(space, space, 'Space detected to the left of {.') do
78
+ add_offense(space, space, DETECTED_MSG) do
43
79
  opposite_style_detected
44
80
  end
45
81
  end
46
82
 
83
+ def style_for_empty_braces
84
+ case cop_config['EnforcedStyleForEmptyBraces']
85
+ when 'space' then :space
86
+ when 'no_space' then :no_space
87
+ when nil then style
88
+ else raise 'Unknown EnforcedStyleForEmptyBraces selected!'
89
+ end
90
+ end
91
+
47
92
  def autocorrect(range)
48
93
  lambda do |corrector|
49
94
  case range.source
@@ -52,6 +97,10 @@ module RuboCop
52
97
  end
53
98
  end
54
99
  end
100
+
101
+ def empty_braces?(loc)
102
+ loc.begin.end_pos == loc.end.begin_pos
103
+ end
55
104
  end
56
105
  end
57
106
  end