rubocop 0.76.0 → 0.83.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 (289) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +6 -6
  4. data/config/default.yml +466 -306
  5. data/lib/rubocop/ast/builder.rb +45 -42
  6. data/lib/rubocop/ast/node/array_node.rb +13 -0
  7. data/lib/rubocop/ast/node/block_node.rb +7 -1
  8. data/lib/rubocop/ast/node/case_match_node.rb +56 -0
  9. data/lib/rubocop/ast/node/def_node.rb +11 -0
  10. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  11. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +8 -0
  12. data/lib/rubocop/ast/node/regexp_node.rb +2 -4
  13. data/lib/rubocop/ast/node/send_node.rb +4 -0
  14. data/lib/rubocop/ast/node.rb +13 -20
  15. data/lib/rubocop/ast/traversal.rb +29 -10
  16. data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
  17. data/lib/rubocop/cli/command/base.rb +33 -0
  18. data/lib/rubocop/cli/command/execute_runner.rb +76 -0
  19. data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
  20. data/lib/rubocop/cli/command/show_cops.rb +80 -0
  21. data/lib/rubocop/cli/command/version.rb +17 -0
  22. data/lib/rubocop/cli/command.rb +21 -0
  23. data/lib/rubocop/cli/environment.rb +21 -0
  24. data/lib/rubocop/cli.rb +20 -233
  25. data/lib/rubocop/comment_config.rb +6 -1
  26. data/lib/rubocop/config.rb +41 -11
  27. data/lib/rubocop/config_loader.rb +54 -44
  28. data/lib/rubocop/config_loader_resolver.rb +28 -1
  29. data/lib/rubocop/config_obsoletion.rb +67 -11
  30. data/lib/rubocop/config_validator.rb +74 -99
  31. data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
  32. data/lib/rubocop/cop/badge.rb +5 -5
  33. data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
  34. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +3 -3
  35. data/lib/rubocop/cop/cop.rb +21 -0
  36. data/lib/rubocop/cop/corrector.rb +48 -24
  37. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -2
  38. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -2
  39. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  40. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +3 -3
  41. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
  42. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  43. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -2
  44. data/lib/rubocop/cop/correctors/string_literal_corrector.rb +2 -2
  45. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  46. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  47. data/lib/rubocop/cop/generator.rb +6 -6
  48. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +59 -0
  49. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -1
  50. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  51. data/lib/rubocop/cop/layout/{align_arguments.rb → argument_alignment.rb} +1 -1
  52. data/lib/rubocop/cop/layout/array_alignment.rb +82 -0
  53. data/lib/rubocop/cop/layout/{indent_assignment.rb → assignment_indentation.rb} +1 -1
  54. data/lib/rubocop/cop/layout/block_end_newline.rb +5 -3
  55. data/lib/rubocop/cop/layout/condition_position.rb +12 -2
  56. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  57. data/lib/rubocop/cop/layout/else_alignment.rb +8 -0
  58. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
  59. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +68 -0
  60. data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
  61. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
  62. data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +5 -5
  63. data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +20 -14
  64. data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +4 -4
  65. data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
  66. data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +10 -6
  67. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  68. data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +7 -7
  69. data/lib/rubocop/cop/layout/{tab.rb → indentation_style.rb} +48 -6
  70. data/lib/rubocop/cop/layout/leading_comment_space.rb +34 -3
  71. data/lib/rubocop/cop/layout/{leading_blank_lines.rb → leading_empty_lines.rb} +1 -1
  72. data/lib/rubocop/cop/{metrics → layout}/line_length.rb +72 -110
  73. data/lib/rubocop/cop/layout/multiline_block_layout.rb +15 -6
  74. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +0 -4
  75. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
  76. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  77. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +13 -4
  78. data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
  79. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +3 -3
  80. data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
  81. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +133 -0
  82. data/lib/rubocop/cop/layout/space_around_operators.rb +69 -9
  83. data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
  84. data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
  85. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
  86. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -2
  87. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
  88. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +2 -2
  89. data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
  90. data/lib/rubocop/cop/layout/trailing_whitespace.rb +2 -2
  91. data/lib/rubocop/cop/lint/ambiguous_operator.rb +38 -0
  92. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +14 -0
  93. data/lib/rubocop/cop/lint/boolean_symbol.rb +12 -0
  94. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  95. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
  96. data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
  97. data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -5
  98. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  99. data/lib/rubocop/cop/lint/empty_when.rb +29 -6
  100. data/lib/rubocop/cop/lint/ensure_return.rb +18 -1
  101. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  102. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  103. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  104. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
  105. data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
  106. data/lib/rubocop/cop/lint/literal_as_condition.rb +10 -13
  107. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  108. data/lib/rubocop/cop/lint/loop.rb +6 -4
  109. data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +2 -2
  110. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  111. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
  112. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  113. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +21 -9
  114. data/lib/rubocop/cop/lint/percent_string_array.rb +2 -2
  115. data/lib/rubocop/cop/lint/raise_exception.rb +75 -0
  116. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +4 -9
  117. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +13 -8
  118. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
  119. data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +2 -2
  120. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  121. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  122. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -1
  123. data/lib/rubocop/cop/lint/struct_new_override.rb +58 -0
  124. data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +13 -29
  125. data/lib/rubocop/cop/lint/unified_integer.rb +0 -2
  126. data/lib/rubocop/cop/lint/unused_method_argument.rb +32 -6
  127. data/lib/rubocop/cop/lint/uri_regexp.rb +4 -4
  128. data/lib/rubocop/cop/lint/useless_access_modifier.rb +69 -23
  129. data/lib/rubocop/cop/lint/useless_assignment.rb +3 -2
  130. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +5 -0
  131. data/lib/rubocop/cop/lint/useless_setter_call.rb +5 -1
  132. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  133. data/lib/rubocop/cop/migration/department_name.rb +47 -6
  134. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  135. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
  136. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +6 -1
  137. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +14 -5
  138. data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
  139. data/lib/rubocop/cop/mixin/hash_transform_method.rb +178 -0
  140. data/lib/rubocop/cop/mixin/line_length_help.rb +89 -0
  141. data/lib/rubocop/cop/mixin/method_complexity.rb +5 -0
  142. data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
  143. data/lib/rubocop/cop/mixin/parser_diagnostic.rb +1 -1
  144. data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
  145. data/lib/rubocop/cop/mixin/statement_modifier.rb +9 -24
  146. data/lib/rubocop/cop/mixin/target_ruby_version.rb +5 -1
  147. data/lib/rubocop/cop/mixin/trailing_comma.rb +9 -13
  148. data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
  149. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  150. data/lib/rubocop/cop/naming/constant_name.rb +2 -1
  151. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +6 -6
  152. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  153. data/lib/rubocop/cop/naming/method_name.rb +26 -0
  154. data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +4 -4
  155. data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
  156. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  157. data/lib/rubocop/cop/offense.rb +11 -0
  158. data/lib/rubocop/cop/registry.rb +15 -3
  159. data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -6
  160. data/lib/rubocop/cop/style/alias.rb +5 -5
  161. data/lib/rubocop/cop/style/and_or.rb +5 -6
  162. data/lib/rubocop/cop/style/array_join.rb +2 -2
  163. data/lib/rubocop/cop/style/attr.rb +8 -0
  164. data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
  165. data/lib/rubocop/cop/style/case_equality.rb +24 -1
  166. data/lib/rubocop/cop/style/character_literal.rb +2 -2
  167. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  168. data/lib/rubocop/cop/style/conditional_assignment.rb +10 -10
  169. data/lib/rubocop/cop/style/copyright.rb +1 -1
  170. data/lib/rubocop/cop/style/dir.rb +1 -1
  171. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -0
  172. data/lib/rubocop/cop/style/documentation.rb +43 -5
  173. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -1
  174. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  175. data/lib/rubocop/cop/style/each_with_object.rb +3 -3
  176. data/lib/rubocop/cop/style/empty_method.rb +1 -5
  177. data/lib/rubocop/cop/style/end_block.rb +6 -0
  178. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  179. data/lib/rubocop/cop/style/even_odd.rb +2 -2
  180. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -3
  181. data/lib/rubocop/cop/style/exponential_notation.rb +119 -0
  182. data/lib/rubocop/cop/style/format_string.rb +2 -2
  183. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +89 -11
  184. data/lib/rubocop/cop/style/guard_clause.rb +28 -4
  185. data/lib/rubocop/cop/style/hash_each_methods.rb +89 -0
  186. data/lib/rubocop/cop/style/hash_syntax.rb +3 -5
  187. data/lib/rubocop/cop/style/hash_transform_keys.rb +83 -0
  188. data/lib/rubocop/cop/style/hash_transform_values.rb +80 -0
  189. data/lib/rubocop/cop/style/if_unless_modifier.rb +61 -6
  190. data/lib/rubocop/cop/style/if_with_semicolon.rb +16 -0
  191. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  192. data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
  193. data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
  194. data/lib/rubocop/cop/style/lambda.rb +3 -2
  195. data/lib/rubocop/cop/style/lambda_call.rb +1 -21
  196. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +169 -0
  197. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  198. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -205
  199. data/lib/rubocop/cop/style/mixin_grouping.rb +2 -2
  200. data/lib/rubocop/cop/style/module_function.rb +58 -12
  201. data/lib/rubocop/cop/style/multiline_if_modifier.rb +1 -1
  202. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  203. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  204. data/lib/rubocop/cop/style/multiline_when_then.rb +21 -2
  205. data/lib/rubocop/cop/style/mutable_constant.rb +2 -4
  206. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +9 -9
  207. data/lib/rubocop/cop/style/next.rb +7 -7
  208. data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
  209. data/lib/rubocop/cop/style/non_nil_check.rb +4 -4
  210. data/lib/rubocop/cop/style/not.rb +1 -1
  211. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  212. data/lib/rubocop/cop/style/numeric_literals.rb +8 -4
  213. data/lib/rubocop/cop/style/numeric_predicate.rb +5 -4
  214. data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
  215. data/lib/rubocop/cop/style/option_hash.rb +3 -3
  216. data/lib/rubocop/cop/style/optional_arguments.rb +1 -1
  217. data/lib/rubocop/cop/style/or_assignment.rb +4 -3
  218. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  219. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  220. data/lib/rubocop/cop/style/perl_backrefs.rb +2 -2
  221. data/lib/rubocop/cop/style/proc.rb +1 -1
  222. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  223. data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
  224. data/lib/rubocop/cop/style/redundant_condition.rb +18 -6
  225. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
  226. data/lib/rubocop/cop/style/redundant_exception.rb +3 -3
  227. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  228. data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -3
  229. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  230. data/lib/rubocop/cop/style/redundant_return.rb +7 -15
  231. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  232. data/lib/rubocop/cop/style/redundant_sort.rb +3 -3
  233. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  234. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  235. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  236. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  237. data/lib/rubocop/cop/style/slicing_with_range.rb +39 -0
  238. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  239. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -4
  240. data/lib/rubocop/cop/style/string_hash_keys.rb +1 -1
  241. data/lib/rubocop/cop/style/symbol_array.rb +3 -3
  242. data/lib/rubocop/cop/style/symbol_literal.rb +2 -2
  243. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -3
  244. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +35 -22
  245. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +41 -0
  246. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +88 -0
  247. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +44 -0
  248. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
  249. data/lib/rubocop/cop/style/trivial_accessors.rb +6 -6
  250. data/lib/rubocop/cop/style/unpack_first.rb +0 -4
  251. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
  252. data/lib/rubocop/cop/style/while_until_modifier.rb +2 -2
  253. data/lib/rubocop/cop/style/word_array.rb +1 -1
  254. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  255. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
  256. data/lib/rubocop/cop/team.rb +5 -0
  257. data/lib/rubocop/cop/util.rb +24 -0
  258. data/lib/rubocop/cop/variable_force/assignment.rb +1 -0
  259. data/lib/rubocop/cop/variable_force/scope.rb +1 -0
  260. data/lib/rubocop/cop/variable_force/variable.rb +1 -0
  261. data/lib/rubocop/cop/variable_force.rb +4 -1
  262. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  263. data/lib/rubocop/formatter/clang_style_formatter.rb +0 -2
  264. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  265. data/lib/rubocop/formatter/json_formatter.rb +6 -5
  266. data/lib/rubocop/formatter/junit_formatter.rb +74 -0
  267. data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
  268. data/lib/rubocop/formatter/tap_formatter.rb +0 -2
  269. data/lib/rubocop/name_similarity.rb +12 -9
  270. data/lib/rubocop/node_pattern.rb +97 -11
  271. data/lib/rubocop/options.rb +26 -13
  272. data/lib/rubocop/processed_source.rb +1 -4
  273. data/lib/rubocop/rake_task.rb +1 -0
  274. data/lib/rubocop/result_cache.rb +23 -7
  275. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  276. data/lib/rubocop/rspec/expect_offense.rb +1 -1
  277. data/lib/rubocop/rspec/shared_contexts.rb +5 -4
  278. data/lib/rubocop/runner.rb +25 -4
  279. data/lib/rubocop/target_finder.rb +6 -4
  280. data/lib/rubocop/target_ruby.rb +151 -0
  281. data/lib/rubocop/version.rb +1 -1
  282. data/lib/rubocop.rb +53 -27
  283. metadata +73 -48
  284. data/lib/rubocop/cop/layout/align_array.rb +0 -39
  285. data/lib/rubocop/cop/lint/end_in_method.rb +0 -40
  286. data/lib/rubocop/cop/mixin/safe_mode.rb +0 -24
  287. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
  288. data/lib/rubocop/formatter/disabled_lines_formatter.rb +0 -57
  289. data/lib/rubocop/string_util.rb +0 -14
@@ -2,14 +2,13 @@
2
2
 
3
3
  require 'uri'
4
4
 
5
- # rubocop:disable Metrics/ClassLength
6
5
  module RuboCop
7
6
  module Cop
8
- module Metrics
7
+ module Layout
9
8
  # This cop checks the length of lines in the source code.
10
9
  # The maximum length is configurable.
11
10
  # The tab size is configured in the `IndentationWidth`
12
- # of the `Layout/Tab` cop.
11
+ # of the `Layout/IndentationStyle` cop.
13
12
  # It also ignores a shebang line by default.
14
13
  #
15
14
  # This cop has some autocorrection capabilities.
@@ -20,19 +19,25 @@ module RuboCop
20
19
  #
21
20
  # If autocorrection is enabled, the following Layout cops
22
21
  # are recommended to further format the broken lines.
22
+ # (Many of these are enabled by default.)
23
23
  #
24
- # - AlignArray
25
- # - AlignHash
26
- # - AlignParameters
24
+ # - ArgumentAlignment
25
+ # - BlockAlignment
26
+ # - BlockDelimiters
27
+ # - BlockEndNewline
27
28
  # - ClosingParenthesisIndentation
28
- # - IndentFirstArgument
29
- # - IndentFirstArrayElement
30
- # - IndentFirstHashElement
31
- # - IndentFirstParameter
29
+ # - FirstArgumentIndentation
30
+ # - FirstArrayElementIndentation
31
+ # - FirstHashElementIndentation
32
+ # - FirstParameterIndentation
33
+ # - HashAlignment
34
+ # - IndentationWidth
32
35
  # - MultilineArrayLineBreaks
36
+ # - MultilineBlockLayout
33
37
  # - MultilineHashBraceLayout
34
38
  # - MultilineHashKeyLineBreaks
35
39
  # - MultilineMethodArgumentLineBreaks
40
+ # - ParameterAlignment
36
41
  #
37
42
  # Together, these cops will pretty print hashes, arrays,
38
43
  # method calls, etc. For example, let's say the max columns
@@ -58,9 +63,14 @@ module RuboCop
58
63
  include ConfigurableMax
59
64
  include IgnoredPattern
60
65
  include RangeHelp
66
+ include LineLengthHelp
61
67
 
62
68
  MSG = 'Line is too long. [%<length>d/%<max>d]'
63
69
 
70
+ def on_block(node)
71
+ check_for_breakable_block(node)
72
+ end
73
+
64
74
  def on_potential_breakable_node(node)
65
75
  check_for_breakable_node(node)
66
76
  end
@@ -68,6 +78,10 @@ module RuboCop
68
78
  alias on_hash on_potential_breakable_node
69
79
  alias on_send on_potential_breakable_node
70
80
 
81
+ def investigate(processed_source)
82
+ check_for_breakable_semicolons(processed_source)
83
+ end
84
+
71
85
  def investigate_post_walk(processed_source)
72
86
  processed_source.lines.each_with_index do |line, line_index|
73
87
  check_line(line, line_index)
@@ -89,33 +103,67 @@ module RuboCop
89
103
  return if breakable_node.nil?
90
104
 
91
105
  line_index = breakable_node.first_line - 1
92
- breakable_nodes_by_line_index[line_index] = breakable_node
106
+ range = breakable_node.source_range
107
+
108
+ existing = breakable_range_by_line_index[line_index]
109
+ return if existing
110
+
111
+ breakable_range_by_line_index[line_index] = range
93
112
  end
94
113
 
95
- def breakable_nodes_by_line_index
96
- @breakable_nodes_by_line_index ||= {}
114
+ def check_for_breakable_semicolons(processed_source)
115
+ tokens = processed_source.tokens.select { |t| t.type == :tSEMI }
116
+ tokens.reverse_each do |token|
117
+ range = breakable_range_after_semicolon(token)
118
+ breakable_range_by_line_index[range.line - 1] = range if range
119
+ end
97
120
  end
98
121
 
99
- def heredocs
100
- @heredocs ||= extract_heredocs(processed_source.ast)
122
+ def check_for_breakable_block(block_node)
123
+ return unless block_node.single_line?
124
+
125
+ line_index = block_node.loc.line - 1
126
+ range = breakable_block_range(block_node)
127
+ pos = range.begin_pos + 1
128
+
129
+ breakable_range_by_line_index[line_index] =
130
+ range_between(pos, pos + 1)
101
131
  end
102
132
 
103
- def tab_indentation_width
104
- config.for_cop('Layout/Tab')['IndentationWidth']
133
+ def breakable_block_range(block_node)
134
+ if block_node.arguments? && !block_node.lambda?
135
+ block_node.arguments.loc.end
136
+ else
137
+ block_node.loc.begin
138
+ end
105
139
  end
106
140
 
107
- def indentation_difference(line)
108
- return 0 unless tab_indentation_width
141
+ def breakable_range_after_semicolon(semicolon_token)
142
+ range = semicolon_token.pos
143
+ end_pos = range.end_pos
144
+ next_range = range_between(end_pos, end_pos + 1)
145
+ return nil unless next_range.line == range.line
109
146
 
110
- line.match(/^\t*/)[0].size * (tab_indentation_width - 1)
147
+ next_char = next_range.source
148
+ return nil if /[\r\n]/.match?(next_char)
149
+ return nil if next_char == ';'
150
+
151
+ next_range
152
+ end
153
+
154
+ def breakable_range_by_line_index
155
+ @breakable_range_by_line_index ||= {}
111
156
  end
112
157
 
113
- def line_length(line)
114
- line.length + indentation_difference(line)
158
+ def heredocs
159
+ @heredocs ||= extract_heredocs(processed_source.ast)
115
160
  end
116
161
 
117
162
  def highlight_start(line)
118
- max - indentation_difference(line)
163
+ # TODO: The max with 0 is a quick fix to avoid crashes when a line
164
+ # begins with many tabs, but getting a correct highlighting range
165
+ # when tabs are used for indentation doesn't work currently.
166
+ [max - indentation_difference(line), 0].max
119
167
  end
120
168
 
121
169
  def check_line(line, line_index)
@@ -147,33 +195,12 @@ module RuboCop
147
195
  def register_offense(loc, line, line_index)
148
196
  message = format(MSG, length: line_length(line), max: max)
149
197
 
150
- breakable_range = breakable_range(line, line_index)
198
+ breakable_range = breakable_range_by_line_index[line_index]
151
199
  add_offense(breakable_range, location: loc, message: message) do
152
200
  self.max = line_length(line)
153
201
  end
154
202
  end
155
203
 
156
- def breakable_range(line, line_index)
157
- return if line_in_heredoc?(line_index + 1)
158
-
159
- semicolon_range = breakable_semicolon_range(line, line_index)
160
- return semicolon_range if semicolon_range
161
-
162
- breakable_node = breakable_nodes_by_line_index[line_index]
163
- return breakable_node.source_range if breakable_node
164
- end
165
-
166
- def breakable_semicolon_range(line, line_index)
167
- semicolon_separated_parts = line.split(';')
168
- return if semicolon_separated_parts.length <= 1
169
-
170
- column = semicolon_separated_parts.first.length + 1
171
- range = source_range(processed_source.buffer, line_index, column, 1)
172
- return if processed_source.commented?(range)
173
-
174
- range
175
- end
176
-
177
204
  def excess_range(uri_range, line, line_index)
178
205
  excessive_position = if uri_range && uri_range.begin < max
179
206
  uri_range.end
@@ -222,53 +249,6 @@ module RuboCop
222
249
  end
223
250
  end
224
251
 
225
- def allow_uri?
226
- cop_config['AllowURI']
227
- end
228
-
229
- def ignore_cop_directives?
230
- cop_config['IgnoreCopDirectives']
231
- end
232
-
233
- def allowed_uri_position?(line, uri_range)
234
- uri_range.begin < max &&
235
- (uri_range.end == line_length(line) ||
236
- uri_range.end == line_length(line) - 1)
237
- end
238
-
239
- def find_excessive_uri_range(line)
240
- last_uri_match = match_uris(line).last
241
- return nil unless last_uri_match
242
-
243
- begin_position, end_position =
244
- last_uri_match.offset(0).map do |pos|
245
- pos + indentation_difference(line)
246
- end
247
- return nil if begin_position < max && end_position < max
248
-
249
- begin_position...end_position
250
- end
251
-
252
- def match_uris(string)
253
- matches = []
254
- string.scan(uri_regexp) do
255
- matches << $LAST_MATCH_INFO if valid_uri?($LAST_MATCH_INFO[0])
256
- end
257
- matches
258
- end
259
-
260
- def valid_uri?(uri_ish_string)
261
- URI.parse(uri_ish_string)
262
- true
263
- rescue URI::InvalidURIError, NoMethodError
264
- false
265
- end
266
-
267
- def uri_regexp
268
- @uri_regexp ||=
269
- URI::DEFAULT_PARSER.make_regexp(cop_config['URISchemes'])
270
- end
271
-
272
252
  def check_directive_line(line, line_index)
273
253
  return if line_length_without_directive(line) <= max
274
254
 
@@ -284,23 +264,6 @@ module RuboCop
284
264
  )
285
265
  end
286
266
 
287
- def directive_on_source_line?(line_index)
288
- source_line_number = line_index + processed_source.buffer.first_line
289
- comment =
290
- processed_source
291
- .comments
292
- .detect { |e| e.location.line == source_line_number }
293
-
294
- return false unless comment
295
-
296
- comment.text.match(CommentConfig::COMMENT_DIRECTIVE_REGEXP)
297
- end
298
-
299
- def line_length_without_directive(line)
300
- before_comment, = line.split(CommentConfig::COMMENT_DIRECTIVE_REGEXP)
301
- before_comment.rstrip.length
302
- end
303
-
304
267
  def check_uri_line(line, line_index)
305
268
  uri_range = find_excessive_uri_range(line)
306
269
  return if uri_range && allowed_uri_position?(line, uri_range)
@@ -315,4 +278,3 @@ module RuboCop
315
278
  end
316
279
  end
317
280
  end
318
- # rubocop:enable Metrics/ClassLength
@@ -97,7 +97,8 @@ module RuboCop
97
97
  def line_break_necessary_in_args?(node)
98
98
  needed_length = node.source_range.column +
99
99
  node.source.lines.first.length +
100
- block_arg_string(node.arguments).length + PIPE_SIZE
100
+ block_arg_string(node, node.arguments).length +
101
+ PIPE_SIZE
101
102
  needed_length > max_line_length
102
103
  end
103
104
 
@@ -115,7 +116,8 @@ module RuboCop
115
116
  newlines: false
116
117
  ).end_pos
117
118
  range = range_between(node.loc.begin.end.begin_pos, end_pos)
118
- corrector.replace(range, " |#{block_arg_string(node.arguments)}|")
119
+ corrector.replace(range,
120
+ " |#{block_arg_string(node, node.arguments)}|")
119
121
  end
120
122
 
121
123
  def autocorrect_body(corrector, node, block_body)
@@ -127,18 +129,25 @@ module RuboCop
127
129
 
128
130
  block_start_col = node.source_range.column
129
131
 
130
- corrector.insert_before(first_node.source_range,
132
+ corrector.insert_before(first_node,
131
133
  "\n #{' ' * block_start_col}")
132
134
  end
133
135
 
134
- def block_arg_string(args)
135
- args.children.map do |arg|
136
+ def block_arg_string(node, args)
137
+ arg_string = args.children.map do |arg|
136
138
  if arg.mlhs_type?
137
- "(#{block_arg_string(arg)})"
139
+ "(#{block_arg_string(node, arg)})"
138
140
  else
139
141
  arg.source
140
142
  end
141
143
  end.join(', ')
144
+ arg_string += ',' if include_trailing_comma?(node.arguments)
145
+ arg_string
146
+ end
147
+
148
+ def include_trailing_comma?(args)
149
+ arg_count = args.each_descendant(:arg).to_a.size
150
+ arg_count == 1 && args.source.include?(',')
142
151
  end
143
152
  end
144
153
  end
@@ -105,10 +105,6 @@ module RuboCop
105
105
  ALWAYS_SAME_LINE_MESSAGE = 'Closing hash brace must be on the same ' \
106
106
  'line as the last hash element.'
107
107
 
108
- def self.autocorrect_incompatible_with
109
- [Style::BracesAroundHashParameters]
110
- end
111
-
112
108
  def on_hash(node)
113
109
  check_brace_layout(node)
114
110
  end
@@ -26,7 +26,7 @@ module RuboCop
26
26
  'on a separate line.'
27
27
 
28
28
  def on_send(node)
29
- return if node.method_name == :[]=
29
+ return if node.method?(:[]=)
30
30
 
31
31
  args = node.arguments
32
32
 
@@ -193,7 +193,7 @@ module RuboCop
193
193
  node = node.receiver while node.receiver
194
194
  # ascend to first call which has a dot
195
195
  node = node.parent
196
- node = node.parent until node.loc.dot
196
+ node = node.parent until node.loc.respond_to?(:dot) && node.loc.dot
197
197
 
198
198
  return if node.loc.dot.line != node.first_line
199
199
 
@@ -6,30 +6,39 @@ module RuboCop
6
6
  # This cop checks the indentation of the right hand side operand in
7
7
  # binary operations that span more than one line.
8
8
  #
9
+ # The `aligned` style checks that operators are aligned if they are part
10
+ # of an `if` or `while` condition, a `return` statement, etc. In other
11
+ # contexts, the second operand should be indented regardless of enforced
12
+ # style.
13
+ #
9
14
  # @example EnforcedStyle: aligned (default)
10
15
  # # bad
11
16
  # if a +
12
17
  # b
13
- # something
18
+ # something &&
19
+ # something_else
14
20
  # end
15
21
  #
16
22
  # # good
17
23
  # if a +
18
24
  # b
19
- # something
25
+ # something &&
26
+ # something_else
20
27
  # end
21
28
  #
22
29
  # @example EnforcedStyle: indented
23
30
  # # bad
24
31
  # if a +
25
32
  # b
26
- # something
33
+ # something &&
34
+ # something_else
27
35
  # end
28
36
  #
29
37
  # # good
30
38
  # if a +
31
39
  # b
32
- # something
40
+ # something &&
41
+ # something_else
33
42
  # end
34
43
  #
35
44
  class MultilineOperationIndentation < Cop
@@ -68,7 +68,7 @@ module RuboCop
68
68
  # baz)
69
69
  # 123
70
70
  # end
71
- class AlignParameters < Cop
71
+ class ParameterAlignment < Cop
72
72
  include Alignment
73
73
 
74
74
  ALIGN_PARAMS_MSG = 'Align the parameters of a method definition if ' \
@@ -43,11 +43,11 @@ module RuboCop
43
43
  lambda do |corrector|
44
44
  if target.is_a?(RuboCop::AST::Node)
45
45
  if target.parent.children.first == target
46
- corrector.insert_before(target.source_range, ' ')
46
+ corrector.insert_before(target, ' ')
47
47
  else
48
- corrector.insert_after(target.source_range, ' ')
48
+ corrector.insert_after(target, ' ')
49
49
  end
50
- elsif target.source =~ /^\s+$/
50
+ elsif /^\s+$/.match?(target.source)
51
51
  corrector.remove(target)
52
52
  else
53
53
  corrector.insert_after(target, ' ')
@@ -30,10 +30,12 @@ module RuboCop
30
30
 
31
31
  DO = 'do'
32
32
  SAFE_NAVIGATION = '&.'
33
+ NAMESPACE_OPERATOR = '::'
33
34
  ACCEPT_LEFT_PAREN =
34
35
  %w[break defined? next not rescue return super yield].freeze
35
36
  ACCEPT_LEFT_SQUARE_BRACKET =
36
37
  %w[super yield].freeze
38
+ ACCEPT_NAMESPACE_OPERATOR = 'super'
37
39
 
38
40
  def on_and(node)
39
41
  check(node, [:operator].freeze) if node.keyword?
@@ -193,6 +195,8 @@ module RuboCop
193
195
 
194
196
  return false if accepted_opening_delimiter?(range, char)
195
197
  return false if safe_navigation_call?(range, pos)
198
+ return false if accept_namespace_operator?(range) &&
199
+ namespace_operator?(range, pos)
196
200
 
197
201
  char !~ /[\s;,#\\\)\}\]\.]/
198
202
  end
@@ -212,10 +216,18 @@ module RuboCop
212
216
  ACCEPT_LEFT_SQUARE_BRACKET.include?(range.source)
213
217
  end
214
218
 
219
+ def accept_namespace_operator?(range)
220
+ ACCEPT_NAMESPACE_OPERATOR == range.source
221
+ end
222
+
215
223
  def safe_navigation_call?(range, pos)
216
224
  range.source_buffer.source[pos, 2].start_with?(SAFE_NAVIGATION)
217
225
  end
218
226
 
227
+ def namespace_operator?(range, pos)
228
+ range.source_buffer.source[pos, 2].start_with?(NAMESPACE_OPERATOR)
229
+ end
230
+
219
231
  def preceded_by_operator?(node, _range)
220
232
  # regular dotted method calls bind more tightly than operators
221
233
  # so we need to climb up the AST past them
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Layout
6
+ # Checks method call operators to not have spaces around them.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # foo. bar
11
+ # foo .bar
12
+ # foo . bar
13
+ # foo. bar .buzz
14
+ # foo
15
+ # . bar
16
+ # . buzz
17
+ # foo&. bar
18
+ # foo &.bar
19
+ # foo &. bar
20
+ # foo &. bar&. buzz
21
+ # RuboCop:: Cop
22
+ # RuboCop:: Cop:: Cop
23
+ # :: RuboCop::Cop
24
+ #
25
+ # # good
26
+ # foo.bar
27
+ # foo.bar.buzz
28
+ # foo
29
+ # .bar
30
+ # .buzz
31
+ # foo&.bar
32
+ # foo&.bar&.buzz
33
+ # RuboCop::Cop
34
+ # RuboCop::Cop::Cop
35
+ # ::RuboCop::Cop
36
+ #
37
+ class SpaceAroundMethodCallOperator < Cop
38
+ include SurroundingSpace
39
+
40
+ MSG = 'Avoid using spaces around a method call operator.'
41
+
42
+ def on_send(node)
43
+ return unless dot_or_safe_navigation_operator?(node)
44
+
45
+ check_and_add_offense(node)
46
+ end
47
+
48
+ def on_const(node)
49
+ return unless node.loc.double_colon
50
+
51
+ check_and_add_offense(node, false)
52
+ end
53
+
54
+ def autocorrect(node)
55
+ operator = operator_token(node)
56
+ left = left_token_for_auto_correction(node, operator)
57
+ right = right_token_for_auto_correction(operator)
58
+
59
+ lambda do |corrector|
60
+ SpaceCorrector.remove_space(
61
+ processed_source, corrector, left, right
62
+ )
63
+ end
64
+ end
65
+
66
+ alias on_csend on_send
67
+
68
+ private
69
+
70
+ def check_and_add_offense(node, add_left_offense = true)
71
+ operator = operator_token(node)
72
+ left = previous_token(operator)
73
+ right = next_token(operator)
74
+
75
+ if !right.comment? && valid_right_token?(right, operator)
76
+ no_space_offenses(node, operator, right, MSG)
77
+ end
78
+ return unless valid_left_token?(left, operator)
79
+
80
+ no_space_offenses(node, left, operator, MSG) if add_left_offense
81
+ end
82
+
83
+ def operator_token(node)
84
+ operator_location =
85
+ node.const_type? ? node.loc.double_colon : node.loc.dot
86
+
87
+ processed_source.find_token do |token|
88
+ token.pos == operator_location
89
+ end
90
+ end
91
+
92
+ def previous_token(current_token)
93
+ index = processed_source.tokens.index(current_token)
94
+ index.zero? ? nil : processed_source.tokens[index - 1]
95
+ end
96
+
97
+ def next_token(current_token)
98
+ index = processed_source.tokens.index(current_token)
99
+ processed_source.tokens[index + 1]
100
+ end
101
+
102
+ def dot_or_safe_navigation_operator?(node)
103
+ node.dot? || node.safe_navigation?
104
+ end
105
+
106
+ def valid_left_token?(left, operator)
107
+ left && left.line == operator.line
108
+ end
109
+
110
+ def valid_right_token?(right, operator)
111
+ right && right.line == operator.line
112
+ end
113
+
114
+ def left_token_for_auto_correction(node, operator)
115
+ left_token = previous_token(operator)
116
+ return operator if node.const_type?
117
+ return left_token if valid_left_token?(left_token, operator)
118
+
119
+ operator
120
+ end
121
+
122
+ def right_token_for_auto_correction(operator)
123
+ right_token = next_token(operator)
124
+ if !right_token.comment? && valid_right_token?(right_token, operator)
125
+ return right_token
126
+ end
127
+
128
+ operator
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end