rubocop 0.82.0 → 0.86.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (280) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +27 -19
  3. data/config/default.yml +111 -23
  4. data/lib/rubocop.rb +16 -59
  5. data/lib/rubocop/ast_aliases.rb +8 -0
  6. data/lib/rubocop/cli.rb +3 -3
  7. data/lib/rubocop/cli/command/auto_genenerate_config.rb +2 -2
  8. data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
  9. data/lib/rubocop/cli/command/show_cops.rb +2 -6
  10. data/lib/rubocop/comment_config.rb +1 -1
  11. data/lib/rubocop/config.rb +6 -2
  12. data/lib/rubocop/config_loader.rb +19 -24
  13. data/lib/rubocop/config_loader_resolver.rb +45 -6
  14. data/lib/rubocop/config_store.rb +12 -2
  15. data/lib/rubocop/config_validator.rb +2 -1
  16. data/lib/rubocop/cop/autocorrect_logic.rb +1 -2
  17. data/lib/rubocop/cop/bundler/gem_comment.rb +70 -1
  18. data/lib/rubocop/cop/commissioner.rb +0 -21
  19. data/lib/rubocop/cop/cop.rb +36 -21
  20. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -6
  21. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +1 -1
  22. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -3
  23. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -3
  24. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
  25. data/lib/rubocop/cop/generator.rb +4 -3
  26. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  27. data/lib/rubocop/cop/ignored_node.rb +1 -3
  28. data/lib/rubocop/cop/layout/case_indentation.rb +3 -3
  29. data/lib/rubocop/cop/layout/class_structure.rb +19 -16
  30. data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
  31. data/lib/rubocop/cop/layout/condition_position.rb +12 -2
  32. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  33. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -6
  34. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +128 -0
  35. data/lib/rubocop/cop/layout/end_of_line.rb +3 -3
  36. data/lib/rubocop/cop/layout/first_argument_indentation.rb +2 -4
  37. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +1 -3
  38. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
  39. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +1 -3
  40. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
  41. data/lib/rubocop/cop/layout/hash_alignment.rb +7 -7
  42. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +3 -7
  43. data/lib/rubocop/cop/layout/heredoc_indentation.rb +20 -103
  44. data/lib/rubocop/cop/layout/indentation_width.rb +1 -3
  45. data/lib/rubocop/cop/layout/line_length.rb +21 -18
  46. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  47. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -3
  48. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +13 -4
  49. data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
  50. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  51. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -3
  52. data/lib/rubocop/cop/layout/space_around_operators.rb +19 -2
  53. data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
  54. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -3
  55. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +2 -4
  56. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  57. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
  58. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -3
  59. data/lib/rubocop/cop/layout/trailing_whitespace.rb +2 -2
  60. data/lib/rubocop/cop/lint/ambiguous_operator.rb +41 -0
  61. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +14 -0
  62. data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
  63. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +137 -0
  64. data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -5
  65. data/lib/rubocop/cop/lint/empty_when.rb +29 -6
  66. data/lib/rubocop/cop/lint/ensure_return.rb +19 -2
  67. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -3
  68. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  69. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +38 -2
  70. data/lib/rubocop/cop/lint/literal_as_condition.rb +10 -13
  71. data/lib/rubocop/cop/lint/loop.rb +1 -1
  72. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +69 -0
  73. data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
  74. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +7 -7
  75. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +33 -11
  76. data/lib/rubocop/cop/lint/percent_string_array.rb +2 -4
  77. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  78. data/lib/rubocop/cop/lint/raise_exception.rb +12 -4
  79. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +5 -8
  80. data/lib/rubocop/cop/lint/redundant_require_statement.rb +3 -3
  81. data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
  82. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  83. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
  84. data/lib/rubocop/cop/lint/suppressed_exception.rb +11 -4
  85. data/lib/rubocop/cop/lint/syntax.rb +1 -3
  86. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  87. data/lib/rubocop/cop/lint/useless_access_modifier.rb +13 -3
  88. data/lib/rubocop/cop/lint/useless_assignment.rb +3 -2
  89. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +6 -1
  90. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  91. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
  92. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  93. data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
  94. data/lib/rubocop/cop/migration/department_name.rb +7 -7
  95. data/lib/rubocop/cop/mixin/alignment.rb +1 -3
  96. data/lib/rubocop/cop/mixin/array_min_size.rb +1 -3
  97. data/lib/rubocop/cop/mixin/check_line_breakable.rb +3 -9
  98. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -3
  99. data/lib/rubocop/cop/mixin/configurable_formatting.rb +2 -4
  100. data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
  101. data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
  102. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  103. data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
  104. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +10 -1
  105. data/lib/rubocop/cop/mixin/hash_transform_method.rb +8 -1
  106. data/lib/rubocop/cop/mixin/ignored_pattern.rb +1 -1
  107. data/lib/rubocop/cop/mixin/line_length_help.rb +3 -2
  108. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  109. data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
  110. data/lib/rubocop/cop/mixin/parser_diagnostic.rb +1 -1
  111. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  112. data/lib/rubocop/cop/mixin/range_help.rb +1 -1
  113. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +43 -0
  114. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -23
  115. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  116. data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
  117. data/lib/rubocop/cop/mixin/target_ruby_version.rb +5 -1
  118. data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -4
  119. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +3 -3
  120. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  121. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +11 -1
  122. data/lib/rubocop/cop/naming/file_name.rb +28 -17
  123. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  124. data/lib/rubocop/cop/naming/method_name.rb +1 -5
  125. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  126. data/lib/rubocop/cop/registry.rb +63 -10
  127. data/lib/rubocop/cop/severity.rb +1 -3
  128. data/lib/rubocop/cop/style/and_or.rb +2 -2
  129. data/lib/rubocop/cop/style/array_join.rb +1 -1
  130. data/lib/rubocop/cop/style/attr.rb +1 -3
  131. data/lib/rubocop/cop/style/bare_percent_literals.rb +2 -2
  132. data/lib/rubocop/cop/style/block_delimiters.rb +4 -12
  133. data/lib/rubocop/cop/style/case_equality.rb +1 -1
  134. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  135. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  136. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  137. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -4
  138. data/lib/rubocop/cop/style/copyright.rb +5 -5
  139. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
  140. data/lib/rubocop/cop/style/documentation.rb +2 -2
  141. data/lib/rubocop/cop/style/double_negation.rb +41 -4
  142. data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
  143. data/lib/rubocop/cop/style/empty_literal.rb +1 -3
  144. data/lib/rubocop/cop/style/empty_method.rb +1 -5
  145. data/lib/rubocop/cop/style/encoding.rb +1 -1
  146. data/lib/rubocop/cop/style/exponential_notation.rb +5 -5
  147. data/lib/rubocop/cop/style/format_string_token.rb +2 -3
  148. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -6
  149. data/lib/rubocop/cop/style/guard_clause.rb +25 -2
  150. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -1
  151. data/lib/rubocop/cop/style/hash_syntax.rb +16 -7
  152. data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
  153. data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
  154. data/lib/rubocop/cop/style/if_with_semicolon.rb +16 -0
  155. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  156. data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
  157. data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
  158. data/lib/rubocop/cop/style/lambda_call.rb +0 -20
  159. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +1 -3
  160. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +1 -3
  161. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  162. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  163. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
  164. data/lib/rubocop/cop/style/multiline_when_then.rb +16 -1
  165. data/lib/rubocop/cop/style/negated_if.rb +3 -3
  166. data/lib/rubocop/cop/style/negated_unless.rb +3 -3
  167. data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
  168. data/lib/rubocop/cop/style/next.rb +2 -2
  169. data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
  170. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
  171. data/lib/rubocop/cop/style/one_line_conditional.rb +2 -6
  172. data/lib/rubocop/cop/style/optional_arguments.rb +1 -1
  173. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  174. data/lib/rubocop/cop/style/redundant_conditional.rb +4 -3
  175. data/lib/rubocop/cop/style/redundant_fetch_block.rb +103 -0
  176. data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -7
  177. data/lib/rubocop/cop/style/redundant_percent_q.rb +3 -3
  178. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +89 -0
  179. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +121 -0
  180. data/lib/rubocop/cop/style/redundant_self.rb +6 -9
  181. data/lib/rubocop/cop/style/safe_navigation.rb +2 -6
  182. data/lib/rubocop/cop/style/sample.rb +1 -1
  183. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  184. data/lib/rubocop/cop/style/slicing_with_range.rb +39 -0
  185. data/lib/rubocop/cop/style/special_global_vars.rb +2 -6
  186. data/lib/rubocop/cop/style/struct_inheritance.rb +21 -0
  187. data/lib/rubocop/cop/style/symbol_array.rb +5 -5
  188. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -4
  189. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +3 -3
  190. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +3 -3
  191. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +13 -13
  192. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +3 -3
  193. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -3
  194. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  195. data/lib/rubocop/cop/style/when_then.rb +1 -1
  196. data/lib/rubocop/cop/style/word_array.rb +1 -1
  197. data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
  198. data/lib/rubocop/cop/team.rb +69 -25
  199. data/lib/rubocop/cop/util.rb +27 -3
  200. data/lib/rubocop/cop/utils/format_string.rb +18 -0
  201. data/lib/rubocop/cop/variable_force.rb +3 -9
  202. data/lib/rubocop/cop/variable_force/assignment.rb +1 -0
  203. data/lib/rubocop/cop/variable_force/branch.rb +1 -3
  204. data/lib/rubocop/cop/variable_force/scope.rb +1 -0
  205. data/lib/rubocop/cop/variable_force/variable.rb +3 -6
  206. data/lib/rubocop/ext/processed_source.rb +18 -0
  207. data/lib/rubocop/formatter/base_formatter.rb +0 -4
  208. data/lib/rubocop/formatter/disabled_config_formatter.rb +5 -13
  209. data/lib/rubocop/formatter/formatter_set.rb +2 -4
  210. data/lib/rubocop/formatter/junit_formatter.rb +14 -4
  211. data/lib/rubocop/magic_comment.rb +1 -1
  212. data/lib/rubocop/name_similarity.rb +18 -9
  213. data/lib/rubocop/options.rb +26 -11
  214. data/lib/rubocop/path_util.rb +2 -2
  215. data/lib/rubocop/platform.rb +1 -1
  216. data/lib/rubocop/remote_config.rb +1 -3
  217. data/lib/rubocop/result_cache.rb +5 -7
  218. data/lib/rubocop/rspec/cop_helper.rb +2 -25
  219. data/lib/rubocop/rspec/expect_offense.rb +58 -15
  220. data/lib/rubocop/rspec/shared_contexts.rb +54 -16
  221. data/lib/rubocop/runner.rb +20 -13
  222. data/lib/rubocop/target_finder.rb +8 -8
  223. data/lib/rubocop/target_ruby.rb +4 -1
  224. data/lib/rubocop/version.rb +5 -3
  225. metadata +51 -74
  226. data/lib/rubocop/ast/builder.rb +0 -85
  227. data/lib/rubocop/ast/node.rb +0 -637
  228. data/lib/rubocop/ast/node/alias_node.rb +0 -24
  229. data/lib/rubocop/ast/node/and_node.rb +0 -29
  230. data/lib/rubocop/ast/node/args_node.rb +0 -29
  231. data/lib/rubocop/ast/node/array_node.rb +0 -70
  232. data/lib/rubocop/ast/node/block_node.rb +0 -121
  233. data/lib/rubocop/ast/node/break_node.rb +0 -17
  234. data/lib/rubocop/ast/node/case_match_node.rb +0 -56
  235. data/lib/rubocop/ast/node/case_node.rb +0 -56
  236. data/lib/rubocop/ast/node/class_node.rb +0 -31
  237. data/lib/rubocop/ast/node/def_node.rb +0 -82
  238. data/lib/rubocop/ast/node/defined_node.rb +0 -17
  239. data/lib/rubocop/ast/node/ensure_node.rb +0 -17
  240. data/lib/rubocop/ast/node/float_node.rb +0 -12
  241. data/lib/rubocop/ast/node/for_node.rb +0 -53
  242. data/lib/rubocop/ast/node/forward_args_node.rb +0 -18
  243. data/lib/rubocop/ast/node/hash_node.rb +0 -109
  244. data/lib/rubocop/ast/node/if_node.rb +0 -175
  245. data/lib/rubocop/ast/node/int_node.rb +0 -12
  246. data/lib/rubocop/ast/node/keyword_splat_node.rb +0 -45
  247. data/lib/rubocop/ast/node/mixin/basic_literal_node.rb +0 -16
  248. data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +0 -43
  249. data/lib/rubocop/ast/node/mixin/collection_node.rb +0 -15
  250. data/lib/rubocop/ast/node/mixin/conditional_node.rb +0 -45
  251. data/lib/rubocop/ast/node/mixin/hash_element_node.rb +0 -125
  252. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +0 -269
  253. data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +0 -114
  254. data/lib/rubocop/ast/node/mixin/modifier_node.rb +0 -17
  255. data/lib/rubocop/ast/node/mixin/numeric_node.rb +0 -21
  256. data/lib/rubocop/ast/node/mixin/parameterized_node.rb +0 -61
  257. data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +0 -35
  258. data/lib/rubocop/ast/node/module_node.rb +0 -24
  259. data/lib/rubocop/ast/node/or_node.rb +0 -29
  260. data/lib/rubocop/ast/node/pair_node.rb +0 -63
  261. data/lib/rubocop/ast/node/range_node.rb +0 -18
  262. data/lib/rubocop/ast/node/regexp_node.rb +0 -33
  263. data/lib/rubocop/ast/node/resbody_node.rb +0 -24
  264. data/lib/rubocop/ast/node/retry_node.rb +0 -17
  265. data/lib/rubocop/ast/node/return_node.rb +0 -24
  266. data/lib/rubocop/ast/node/self_class_node.rb +0 -24
  267. data/lib/rubocop/ast/node/send_node.rb +0 -13
  268. data/lib/rubocop/ast/node/str_node.rb +0 -16
  269. data/lib/rubocop/ast/node/super_node.rb +0 -21
  270. data/lib/rubocop/ast/node/symbol_node.rb +0 -12
  271. data/lib/rubocop/ast/node/until_node.rb +0 -35
  272. data/lib/rubocop/ast/node/when_node.rb +0 -53
  273. data/lib/rubocop/ast/node/while_node.rb +0 -35
  274. data/lib/rubocop/ast/node/yield_node.rb +0 -21
  275. data/lib/rubocop/ast/sexp.rb +0 -16
  276. data/lib/rubocop/ast/traversal.rb +0 -202
  277. data/lib/rubocop/node_pattern.rb +0 -887
  278. data/lib/rubocop/processed_source.rb +0 -213
  279. data/lib/rubocop/string_util.rb +0 -14
  280. data/lib/rubocop/token.rb +0 -114
@@ -109,7 +109,7 @@ module RuboCop
109
109
 
110
110
  def own_line_comment?(comment)
111
111
  own_line = processed_source.lines[comment.loc.line - 1]
112
- own_line =~ /\A\s*#/
112
+ /\A\s*#/.match?(own_line)
113
113
  end
114
114
 
115
115
  def line_after_comment(comment)
@@ -129,11 +129,11 @@ module RuboCop
129
129
  end
130
130
 
131
131
  def less_indented?(line)
132
- line =~ /^\s*(end\b|[)}\]])/
132
+ /^\s*(end\b|[)}\]])/.match?(line)
133
133
  end
134
134
 
135
135
  def two_alternatives?(line)
136
- line =~ /^\s*(else|elsif|when|rescue|ensure)\b/
136
+ /^\s*(else|elsif|when|rescue|ensure)\b/.match?(line)
137
137
  end
138
138
  end
139
139
  end
@@ -23,6 +23,8 @@ module RuboCop
23
23
  # do_something
24
24
  # end
25
25
  class ConditionPosition < Cop
26
+ include RangeHelp
27
+
26
28
  MSG = 'Place the condition on the same line as `%<keyword>s`.'
27
29
 
28
30
  def on_if(node)
@@ -34,9 +36,17 @@ module RuboCop
34
36
  def on_while(node)
35
37
  check(node)
36
38
  end
39
+ alias on_until on_while
37
40
 
38
- def on_until(node)
39
- check(node)
41
+ def autocorrect(node)
42
+ lambda do |corrector|
43
+ range = range_by_whole_lines(
44
+ node.source_range, include_final_newline: true
45
+ )
46
+
47
+ corrector.insert_after(node.parent.loc.keyword, " #{node.source}")
48
+ corrector.remove(range)
49
+ end
40
50
  end
41
51
 
42
52
  private
@@ -119,7 +119,7 @@ module RuboCop
119
119
  /\A(#+\n)+\z/
120
120
  end
121
121
 
122
- !(comment_text =~ empty_comment_pattern).nil?
122
+ empty_comment_pattern.match?(comment_text)
123
123
  end
124
124
 
125
125
  def comment_text(comment)
@@ -99,9 +99,7 @@ module RuboCop
99
99
  lambda do |corrector|
100
100
  line = range_by_whole_lines(node.source_range)
101
101
 
102
- unless previous_line_empty?(node.first_line)
103
- corrector.insert_before(line, "\n")
104
- end
102
+ corrector.insert_before(line, "\n") unless previous_line_empty?(node.first_line)
105
103
 
106
104
  correct_next_line_if_denied_style(corrector, node, line)
107
105
  end
@@ -120,9 +118,7 @@ module RuboCop
120
118
  def correct_next_line_if_denied_style(corrector, node, line)
121
119
  case style
122
120
  when :around
123
- unless next_line_empty?(node.last_line)
124
- corrector.insert_after(line, "\n")
125
- end
121
+ corrector.insert_after(line, "\n") unless next_line_empty?(node.last_line)
126
122
  when :only_before
127
123
  if next_line_empty?(node.last_line)
128
124
  range = next_empty_line_range(node)
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Layout
6
+ # Checks for a newline after an attribute accessor or a group of them.
7
+ # `alias` syntax and `alias_method`, `public`, `protected`, and `private` methods are allowed
8
+ # by default. These are customizable with `AllowAliasSyntax` and `AllowedMethods` options.
9
+ #
10
+ # @example
11
+ # # bad
12
+ # attr_accessor :foo
13
+ # def do_something
14
+ # end
15
+ #
16
+ # # good
17
+ # attr_accessor :foo
18
+ #
19
+ # def do_something
20
+ # end
21
+ #
22
+ # # good
23
+ # attr_accessor :foo
24
+ # attr_reader :bar
25
+ # attr_writer :baz
26
+ # attr :qux
27
+ #
28
+ # def do_something
29
+ # end
30
+ #
31
+ # @example AllowAliasSyntax: true (default)
32
+ # # good
33
+ # attr_accessor :foo
34
+ # alias :foo? :foo
35
+ #
36
+ # def do_something
37
+ # end
38
+ #
39
+ # @example AllowAliasSyntax: false
40
+ # # bad
41
+ # attr_accessor :foo
42
+ # alias :foo? :foo
43
+ #
44
+ # def do_something
45
+ # end
46
+ #
47
+ # # good
48
+ # attr_accessor :foo
49
+ #
50
+ # alias :foo? :foo
51
+ #
52
+ # def do_something
53
+ # end
54
+ #
55
+ # @example AllowedMethods: ['private']
56
+ # # good
57
+ # attr_accessor :foo
58
+ # private :foo
59
+ #
60
+ # def do_something
61
+ # end
62
+ #
63
+ class EmptyLinesAroundAttributeAccessor < Cop
64
+ include RangeHelp
65
+
66
+ MSG = 'Add an empty line after attribute accessor.'
67
+
68
+ def on_send(node)
69
+ return unless node.attribute_accessor?
70
+ return if next_line_empty?(node.last_line)
71
+
72
+ next_line_node = next_line_node(node)
73
+ return unless require_empty_line?(next_line_node)
74
+
75
+ add_offense(node)
76
+ end
77
+
78
+ def autocorrect(node)
79
+ lambda do |corrector|
80
+ range = range_by_whole_lines(node.source_range)
81
+
82
+ corrector.insert_after(range, "\n")
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def next_line_empty?(line)
89
+ processed_source[line].blank?
90
+ end
91
+
92
+ def require_empty_line?(node)
93
+ return false unless node&.respond_to?(:type)
94
+
95
+ !allow_alias?(node) && !attribute_or_allowed_method?(node)
96
+ end
97
+
98
+ def next_line_node(node)
99
+ return if node.parent.if_type?
100
+
101
+ node.parent.children[node.sibling_index + 1]
102
+ end
103
+
104
+ def allow_alias?(node)
105
+ allow_alias_syntax? && node.alias_type?
106
+ end
107
+
108
+ def attribute_or_allowed_method?(node)
109
+ return false unless node.send_type?
110
+
111
+ node.attribute_accessor? || allowed_method?(node.method_name)
112
+ end
113
+
114
+ def allow_alias_syntax?
115
+ cop_config.fetch('AllowAliasSyntax', true)
116
+ end
117
+
118
+ def allowed_method?(name)
119
+ allowed_methods.include?(name.to_s)
120
+ end
121
+
122
+ def allowed_methods
123
+ cop_config.fetch('AllowedMethods', [])
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
@@ -65,7 +65,7 @@ module RuboCop
65
65
 
66
66
  # If there is no LF on the last line, we don't care if there's no CR.
67
67
  def unimportant_missing_cr?(index, last_line, line)
68
- style == :crlf && index == last_line - 1 && line !~ /\n$/
68
+ style == :crlf && index == last_line - 1 && !/\n$/.match?(line)
69
69
  end
70
70
 
71
71
  def offense_message(line)
@@ -75,8 +75,8 @@ module RuboCop
75
75
  style
76
76
  end
77
77
  case effective_style
78
- when :lf then MSG_DETECTED if /\r$/.match?(line)
79
- else MSG_MISSING unless /\r$/.match?(line)
78
+ when :lf then MSG_DETECTED if line.end_with?("\r", "\r\n")
79
+ else MSG_MISSING unless line.end_with?("\r\n")
80
80
  end
81
81
  end
82
82
 
@@ -2,13 +2,12 @@
2
2
 
3
3
  module RuboCop
4
4
  module Cop
5
- # rubocop:disable Layout/LineLength
6
5
  module Layout
7
6
  # This cop checks the indentation of the first argument in a method call.
8
7
  # Arguments after the first one are checked by Layout/ArgumentAlignment,
9
8
  # not by this cop.
10
9
  #
11
- # For indenting the first parameter of method *definitions*, check out
10
+ # For indenting the first parameter of method _definitions_, check out
12
11
  # Layout/FirstParameterIndentation.
13
12
  #
14
13
  # @example
@@ -143,7 +142,6 @@ module RuboCop
143
142
  # second_param
144
143
  #
145
144
  class FirstArgumentIndentation < Cop
146
- # rubocop:enable Layout/LineLength
147
145
  include Alignment
148
146
  include ConfigurableEnforcedStyle
149
147
  include RangeHelp
@@ -170,7 +168,7 @@ module RuboCop
170
168
 
171
169
  send_node = arg_node.parent
172
170
  text = base_range(send_node, arg_node).source.strip
173
- base = if text !~ /\n/ && special_inner_call_indentation?(send_node)
171
+ base = if !/\n/.match?(text) && special_inner_call_indentation?(send_node)
174
172
  "`#{text}`"
175
173
  elsif comment_line?(text.lines.reverse_each.first)
176
174
  'the start of the previous line (not counting the comment)'
@@ -125,9 +125,7 @@ module RuboCop
125
125
 
126
126
  def check_right_bracket(right_bracket, left_bracket, left_parenthesis)
127
127
  # if the right bracket is on the same line as the last value, accept
128
- if /\S/.match?(right_bracket.source_line[0...right_bracket.column])
129
- return
130
- end
128
+ return if /\S/.match?(right_bracket.source_line[0...right_bracket.column])
131
129
 
132
130
  expected_column = base_column(left_bracket, left_parenthesis)
133
131
  @column_delta = expected_column - right_bracket.column
@@ -37,7 +37,7 @@ module RuboCop
37
37
 
38
38
  def assignment_on_same_line?(node)
39
39
  source = node.source_range.source_line[0...node.loc.column]
40
- source =~ /\s*\=\s*$/
40
+ /\s*=\s*$/.match?(source)
41
41
  end
42
42
  end
43
43
  end
@@ -36,9 +36,7 @@ module RuboCop
36
36
  # ...then each key/value pair is treated as a method 'argument'
37
37
  # when determining where line breaks should appear.
38
38
  if (last_arg = args.last)
39
- if last_arg.hash_type? && !last_arg.braces?
40
- args = args.concat(args.pop.children)
41
- end
39
+ args = args.concat(args.pop.children) if last_arg.hash_type? && !last_arg.braces?
42
40
  end
43
41
 
44
42
  check_method_line_break(node, args)
@@ -7,9 +7,9 @@ module RuboCop
7
7
  # definition. Parameters after the first one are checked by
8
8
  # Layout/ParameterAlignment, not by this cop.
9
9
  #
10
- # For indenting the first argument of method *calls*, check out
10
+ # For indenting the first argument of method _calls_, check out
11
11
  # Layout/FirstArgumentIndentation, which supports options related to
12
- # nesting that are irrelevant for method *definitions*.
12
+ # nesting that are irrelevant for method _definitions_.
13
13
  #
14
14
  # @example
15
15
  #
@@ -7,16 +7,16 @@ module RuboCop
7
7
  # literal are aligned according to configuration. The configuration
8
8
  # options are:
9
9
  #
10
- # - key (left align keys, one space before hash rockets and values)
11
- # - separator (align hash rockets and colons, right align keys)
12
- # - table (left align keys, hash rockets, and values)
10
+ # * key (left align keys, one space before hash rockets and values)
11
+ # * separator (align hash rockets and colons, right align keys)
12
+ # * table (left align keys, hash rockets, and values)
13
13
  #
14
14
  # The treatment of hashes passed as the last argument to a method call
15
15
  # can also be configured. The options are:
16
16
  #
17
- # - always_inspect
18
- # - always_ignore
19
- # - ignore_implicit (without curly braces)
17
+ # * always_inspect
18
+ # * always_ignore
19
+ # * ignore_implicit (without curly braces)
20
20
  #
21
21
  # Alternatively you can specify multiple allowed styles. That's done by
22
22
  # passing a list of styles to EnforcedStyles.
@@ -200,7 +200,7 @@ module RuboCop
200
200
  alias on_super on_send
201
201
  alias on_yield on_send
202
202
 
203
- def on_hash(node)
203
+ def on_hash(node) # rubocop:todo Metrics/CyclomaticComplexity
204
204
  return if ignored_node?(node)
205
205
  return if node.pairs.empty? || node.single_line?
206
206
 
@@ -98,13 +98,9 @@ module RuboCop
98
98
  lambda do |corrector|
99
99
  fix_closing_parenthesis(node, corrector)
100
100
 
101
- if internal_trailing_comma?(node)
102
- remove_internal_trailing_comma(node, corrector)
103
- end
101
+ remove_internal_trailing_comma(node, corrector) if internal_trailing_comma?(node)
104
102
 
105
- if external_trailing_comma?(node)
106
- fix_external_trailing_comma(node, corrector)
107
- end
103
+ fix_external_trailing_comma(node, corrector) if external_trailing_comma?(node)
108
104
  end
109
105
  end
110
106
 
@@ -194,7 +190,7 @@ module RuboCop
194
190
  def safe_to_remove_line_containing_closing_paren?(node)
195
191
  last_line = processed_source[node.loc.end.line - 1]
196
192
  # Safe to remove if last line only contains `)`, `,`, and whitespace.
197
- last_line.match(/^[ ]*\)[ ]{0,20},{0,1}[ ]*$/)
193
+ last_line.match?(/^ *\) {0,20},{0,1} *$/)
198
194
  end
199
195
 
200
196
  def incorrect_parenthesis_removal_end(node)
@@ -5,66 +5,29 @@ module RuboCop
5
5
  module Layout
6
6
  # This cop checks the indentation of the here document bodies. The bodies
7
7
  # are indented one step.
8
- # In Ruby 2.3 or newer, squiggly heredocs (`<<~`) should be used. If you
9
- # use the older rubies, you should introduce some library to your project
10
- # (e.g. ActiveSupport, Powerpack or Unindent).
11
- # Note: When `Layout/LineLength`'s `AllowHeredoc` is false (not default),
8
+ #
9
+ # Note: When ``Layout/LineLength``'s `AllowHeredoc` is false (not default),
12
10
  # this cop does not add any offenses for long here documents to
13
11
  # avoid `Layout/LineLength`'s offenses.
14
12
  #
15
- # @example EnforcedStyle: squiggly (default)
13
+ # @example
16
14
  # # bad
17
15
  # <<-RUBY
18
16
  # something
19
17
  # RUBY
20
18
  #
21
19
  # # good
22
- # # When EnforcedStyle is squiggly, bad code is auto-corrected to the
23
- # # following code.
24
20
  # <<~RUBY
25
21
  # something
26
22
  # RUBY
27
23
  #
28
- # @example EnforcedStyle: active_support
29
- # # good
30
- # # When EnforcedStyle is active_support, bad code is auto-corrected to
31
- # # the following code.
32
- # <<-RUBY.strip_heredoc
33
- # something
34
- # RUBY
35
- #
36
- # @example EnforcedStyle: powerpack
37
- # # good
38
- # # When EnforcedStyle is powerpack, bad code is auto-corrected to
39
- # # the following code.
40
- # <<-RUBY.strip_indent
41
- # something
42
- # RUBY
43
- #
44
- # @example EnforcedStyle: unindent
45
- # # good
46
- # # When EnforcedStyle is unindent, bad code is auto-corrected to
47
- # # the following code.
48
- # <<-RUBY.unindent
49
- # something
50
- # RUBY
51
24
  #
52
25
  class HeredocIndentation < Cop
53
26
  include Heredoc
54
- include ConfigurableEnforcedStyle
55
27
 
56
- RUBY23_TYPE_MSG = 'Use %<indentation_width>d spaces for indentation ' \
57
- 'in a heredoc by using `<<~` instead of ' \
58
- '`%<current_indent_type>s`.'
59
- RUBY23_WIDTH_MSG = 'Use %<indentation_width>d spaces for '\
60
- 'indentation in a heredoc.'
61
- LIBRARY_MSG = 'Use %<indentation_width>d spaces for indentation in a ' \
62
- 'heredoc by using %<method>s.'
63
- STRIP_METHODS = {
64
- unindent: 'unindent',
65
- active_support: 'strip_heredoc',
66
- powerpack: 'strip_indent'
67
- }.freeze
28
+ TYPE_MSG = 'Use %<indentation_width>d spaces for indentation in a ' \
29
+ 'heredoc by using `<<~` instead of `%<current_indent_type>s`.'
30
+ WIDTH_MSG = 'Use %<indentation_width>d spaces for indentation in a heredoc.'
68
31
 
69
32
  def on_heredoc(node)
70
33
  body = heredoc_body(node)
@@ -85,59 +48,38 @@ module RuboCop
85
48
  end
86
49
 
87
50
  def autocorrect(node)
88
- check_style!
89
-
90
- case style
91
- when :squiggly
92
- correct_by_squiggly(node)
93
- else
94
- correct_by_library(node)
51
+ lambda do |corrector|
52
+ if heredoc_indent_type(node) == '~'
53
+ adjust_squiggly(corrector, node)
54
+ else
55
+ adjust_minus(corrector, node)
56
+ end
95
57
  end
96
58
  end
97
59
 
98
60
  private
99
61
 
100
62
  def message(node)
101
- case style
102
- when :squiggly
103
- current_indent_type = "<<#{heredoc_indent_type(node)}"
104
- ruby23_message(indentation_width, current_indent_type)
105
- when nil
106
- method = "some library(e.g. ActiveSupport's `String#strip_heredoc`)"
107
- library_message(indentation_width, method)
108
- else
109
- method = "`String##{STRIP_METHODS[style]}`"
110
- library_message(indentation_width, method)
111
- end
112
- end
113
-
114
- def library_message(indentation_width, method)
115
- format(
116
- LIBRARY_MSG,
117
- indentation_width: indentation_width,
118
- method: method
119
- )
120
- end
63
+ current_indent_type = "<<#{heredoc_indent_type(node)}"
121
64
 
122
- def ruby23_message(indentation_width, current_indent_type)
123
65
  if current_indent_type == '<<~'
124
- ruby23_width_message(indentation_width)
66
+ width_message(indentation_width)
125
67
  else
126
- ruby23_type_message(indentation_width, current_indent_type)
68
+ type_message(indentation_width, current_indent_type)
127
69
  end
128
70
  end
129
71
 
130
- def ruby23_type_message(indentation_width, current_indent_type)
72
+ def type_message(indentation_width, current_indent_type)
131
73
  format(
132
- RUBY23_TYPE_MSG,
74
+ TYPE_MSG,
133
75
  indentation_width: indentation_width,
134
76
  current_indent_type: current_indent_type
135
77
  )
136
78
  end
137
79
 
138
- def ruby23_width_message(indentation_width)
80
+ def width_message(indentation_width)
139
81
  format(
140
- RUBY23_WIDTH_MSG,
82
+ WIDTH_MSG,
141
83
  indentation_width: indentation_width
142
84
  )
143
85
  end
@@ -166,16 +108,6 @@ module RuboCop
166
108
  config.for_cop('Layout/LineLength')['Max']
167
109
  end
168
110
 
169
- def correct_by_squiggly(node)
170
- lambda do |corrector|
171
- if heredoc_indent_type(node) == '~'
172
- adjust_squiggly(corrector, node)
173
- else
174
- adjust_minus(corrector, node)
175
- end
176
- end
177
- end
178
-
179
111
  def adjust_squiggly(corrector, node)
180
112
  corrector.replace(node.loc.heredoc_body, indented_body(node))
181
113
  corrector.replace(node.loc.heredoc_end, indented_end(node))
@@ -187,21 +119,6 @@ module RuboCop
187
119
  corrector.replace(node, corrected)
188
120
  end
189
121
 
190
- def correct_by_library(node)
191
- lambda do |corrector|
192
- corrector.replace(node.loc.heredoc_body, indented_body(node))
193
- corrected = ".#{STRIP_METHODS[style]}"
194
- corrector.insert_after(node, corrected)
195
- end
196
- end
197
-
198
- def check_style!
199
- return if style
200
-
201
- raise Warning, "Auto-correction does not work for #{cop_name}. " \
202
- 'Please configure EnforcedStyle.'
203
- end
204
-
205
122
  def indented_body(node)
206
123
  body = heredoc_body(node)
207
124
  body_indent_level = indent_level(body)
@@ -230,7 +147,7 @@ module RuboCop
230
147
  def indent_level(str)
231
148
  indentations = str.lines
232
149
  .map { |line| line[/^\s*/] }
233
- .reject { |line| line == "\n" }
150
+ .reject { |line| line.end_with?("\n") }
234
151
  indentations.empty? ? 0 : indentations.min_by(&:size).size
235
152
  end
236
153