rubocop 0.79.0 → 0.83.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (225) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +5 -5
  4. data/config/default.yml +184 -50
  5. data/lib/rubocop.rb +15 -5
  6. data/lib/rubocop/ast/builder.rb +2 -0
  7. data/lib/rubocop/ast/node.rb +12 -19
  8. data/lib/rubocop/ast/node/array_node.rb +13 -0
  9. data/lib/rubocop/ast/node/block_node.rb +5 -1
  10. data/lib/rubocop/ast/node/case_match_node.rb +56 -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/traversal.rb +20 -9
  15. data/lib/rubocop/cli.rb +11 -5
  16. data/lib/rubocop/comment_config.rb +6 -1
  17. data/lib/rubocop/config.rb +40 -10
  18. data/lib/rubocop/config_loader.rb +43 -33
  19. data/lib/rubocop/config_loader_resolver.rb +28 -1
  20. data/lib/rubocop/config_obsoletion.rb +4 -1
  21. data/lib/rubocop/config_validator.rb +18 -1
  22. data/lib/rubocop/cop/badge.rb +5 -5
  23. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -1
  24. data/lib/rubocop/cop/corrector.rb +48 -24
  25. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -2
  26. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -2
  27. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  28. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +3 -3
  29. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
  30. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  31. data/lib/rubocop/cop/correctors/string_literal_corrector.rb +2 -2
  32. data/lib/rubocop/cop/generator.rb +3 -2
  33. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -1
  34. data/lib/rubocop/cop/layout/array_alignment.rb +53 -10
  35. data/lib/rubocop/cop/layout/block_end_newline.rb +5 -3
  36. data/lib/rubocop/cop/layout/condition_position.rb +12 -2
  37. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  38. data/lib/rubocop/cop/layout/else_alignment.rb +8 -0
  39. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
  40. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +68 -0
  41. data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
  42. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +16 -10
  43. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
  44. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  45. data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
  46. data/lib/rubocop/cop/layout/{tab.rb → indentation_style.rb} +48 -6
  47. data/lib/rubocop/cop/layout/leading_comment_space.rb +34 -3
  48. data/lib/rubocop/cop/layout/line_length.rb +36 -4
  49. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  50. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +0 -4
  51. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  52. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +13 -4
  53. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +3 -3
  54. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +133 -0
  55. data/lib/rubocop/cop/layout/space_around_operators.rb +37 -2
  56. data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
  57. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
  58. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -2
  59. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
  60. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +2 -2
  61. data/lib/rubocop/cop/layout/trailing_whitespace.rb +2 -2
  62. data/lib/rubocop/cop/lint/ambiguous_operator.rb +38 -0
  63. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +14 -0
  64. data/lib/rubocop/cop/lint/boolean_symbol.rb +12 -0
  65. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  66. data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -5
  67. data/lib/rubocop/cop/lint/empty_when.rb +29 -6
  68. data/lib/rubocop/cop/lint/ensure_return.rb +18 -1
  69. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  70. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  71. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
  72. data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
  73. data/lib/rubocop/cop/lint/literal_as_condition.rb +10 -13
  74. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  75. data/lib/rubocop/cop/lint/loop.rb +6 -4
  76. data/lib/rubocop/cop/lint/multiple_comparison.rb +1 -1
  77. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  78. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
  79. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  80. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +21 -9
  81. data/lib/rubocop/cop/lint/percent_string_array.rb +2 -2
  82. data/lib/rubocop/cop/lint/raise_exception.rb +75 -0
  83. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -6
  84. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +12 -7
  85. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
  86. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -1
  87. data/lib/rubocop/cop/lint/struct_new_override.rb +58 -0
  88. data/lib/rubocop/cop/lint/suppressed_exception.rb +12 -28
  89. data/lib/rubocop/cop/lint/unified_integer.rb +0 -2
  90. data/lib/rubocop/cop/lint/unused_method_argument.rb +32 -6
  91. data/lib/rubocop/cop/lint/uri_regexp.rb +4 -4
  92. data/lib/rubocop/cop/lint/useless_access_modifier.rb +12 -0
  93. data/lib/rubocop/cop/lint/useless_assignment.rb +3 -2
  94. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +5 -0
  95. data/lib/rubocop/cop/lint/useless_setter_call.rb +4 -0
  96. data/lib/rubocop/cop/migration/department_name.rb +36 -10
  97. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
  98. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +6 -1
  99. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +16 -1
  100. data/lib/rubocop/cop/mixin/hash_transform_method.rb +178 -0
  101. data/lib/rubocop/cop/mixin/line_length_help.rb +2 -1
  102. data/lib/rubocop/cop/mixin/method_complexity.rb +5 -0
  103. data/lib/rubocop/cop/mixin/parser_diagnostic.rb +1 -1
  104. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -22
  105. data/lib/rubocop/cop/mixin/target_ruby_version.rb +5 -1
  106. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -10
  107. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  108. data/lib/rubocop/cop/naming/constant_name.rb +2 -1
  109. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  110. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  111. data/lib/rubocop/cop/naming/method_name.rb +26 -0
  112. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  113. data/lib/rubocop/cop/registry.rb +11 -4
  114. data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -6
  115. data/lib/rubocop/cop/style/alias.rb +4 -4
  116. data/lib/rubocop/cop/style/and_or.rb +5 -6
  117. data/lib/rubocop/cop/style/array_join.rb +1 -1
  118. data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
  119. data/lib/rubocop/cop/style/case_equality.rb +24 -1
  120. data/lib/rubocop/cop/style/character_literal.rb +2 -2
  121. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  122. data/lib/rubocop/cop/style/conditional_assignment.rb +8 -8
  123. data/lib/rubocop/cop/style/copyright.rb +1 -1
  124. data/lib/rubocop/cop/style/dir.rb +1 -1
  125. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -0
  126. data/lib/rubocop/cop/style/documentation.rb +43 -5
  127. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -1
  128. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  129. data/lib/rubocop/cop/style/each_with_object.rb +3 -3
  130. data/lib/rubocop/cop/style/empty_method.rb +1 -5
  131. data/lib/rubocop/cop/style/end_block.rb +6 -0
  132. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  133. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -3
  134. data/lib/rubocop/cop/style/exponential_notation.rb +119 -0
  135. data/lib/rubocop/cop/style/format_string.rb +2 -2
  136. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +89 -11
  137. data/lib/rubocop/cop/style/guard_clause.rb +25 -2
  138. data/lib/rubocop/cop/style/hash_each_methods.rb +89 -0
  139. data/lib/rubocop/cop/style/hash_syntax.rb +3 -5
  140. data/lib/rubocop/cop/style/hash_transform_keys.rb +83 -0
  141. data/lib/rubocop/cop/style/hash_transform_values.rb +80 -0
  142. data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -3
  143. data/lib/rubocop/cop/style/if_with_semicolon.rb +16 -0
  144. data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
  145. data/lib/rubocop/cop/style/lambda.rb +3 -2
  146. data/lib/rubocop/cop/style/lambda_call.rb +1 -21
  147. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +5 -0
  148. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +5 -4
  149. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  150. data/lib/rubocop/cop/style/module_function.rb +58 -12
  151. data/lib/rubocop/cop/style/multiline_if_modifier.rb +1 -1
  152. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  153. data/lib/rubocop/cop/style/multiline_when_then.rb +16 -1
  154. data/lib/rubocop/cop/style/mutable_constant.rb +2 -4
  155. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +4 -4
  156. data/lib/rubocop/cop/style/next.rb +2 -2
  157. data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
  158. data/lib/rubocop/cop/style/non_nil_check.rb +4 -4
  159. data/lib/rubocop/cop/style/not.rb +1 -1
  160. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  161. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  162. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
  163. data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
  164. data/lib/rubocop/cop/style/optional_arguments.rb +1 -1
  165. data/lib/rubocop/cop/style/or_assignment.rb +4 -3
  166. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  167. data/lib/rubocop/cop/style/perl_backrefs.rb +2 -2
  168. data/lib/rubocop/cop/style/proc.rb +1 -1
  169. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  170. data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
  171. data/lib/rubocop/cop/style/redundant_condition.rb +18 -6
  172. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
  173. data/lib/rubocop/cop/style/redundant_exception.rb +3 -3
  174. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  175. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  176. data/lib/rubocop/cop/style/redundant_return.rb +5 -7
  177. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  178. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  179. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  180. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  181. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  182. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  183. data/lib/rubocop/cop/style/slicing_with_range.rb +39 -0
  184. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  185. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -4
  186. data/lib/rubocop/cop/style/string_hash_keys.rb +1 -1
  187. data/lib/rubocop/cop/style/symbol_array.rb +3 -3
  188. data/lib/rubocop/cop/style/symbol_literal.rb +2 -2
  189. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -3
  190. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +35 -22
  191. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +41 -0
  192. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +88 -0
  193. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +44 -0
  194. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  195. data/lib/rubocop/cop/style/unpack_first.rb +0 -4
  196. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
  197. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  198. data/lib/rubocop/cop/style/word_array.rb +1 -1
  199. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
  200. data/lib/rubocop/cop/util.rb +24 -0
  201. data/lib/rubocop/cop/variable_force.rb +4 -1
  202. data/lib/rubocop/cop/variable_force/assignment.rb +1 -0
  203. data/lib/rubocop/cop/variable_force/scope.rb +1 -0
  204. data/lib/rubocop/cop/variable_force/variable.rb +1 -0
  205. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  206. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  207. data/lib/rubocop/formatter/junit_formatter.rb +74 -0
  208. data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
  209. data/lib/rubocop/formatter/tap_formatter.rb +1 -1
  210. data/lib/rubocop/name_similarity.rb +12 -9
  211. data/lib/rubocop/node_pattern.rb +96 -10
  212. data/lib/rubocop/options.rb +18 -5
  213. data/lib/rubocop/processed_source.rb +1 -4
  214. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  215. data/lib/rubocop/rspec/expect_offense.rb +1 -1
  216. data/lib/rubocop/rspec/shared_contexts.rb +0 -4
  217. data/lib/rubocop/runner.rb +7 -2
  218. data/lib/rubocop/target_finder.rb +6 -4
  219. data/lib/rubocop/target_ruby.rb +2 -2
  220. data/lib/rubocop/version.rb +1 -1
  221. metadata +35 -25
  222. data/lib/rubocop/cop/lint/end_in_method.rb +0 -40
  223. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
  224. data/lib/rubocop/formatter/disabled_lines_formatter.rb +0 -57
  225. data/lib/rubocop/string_util.rb +0 -14
@@ -51,7 +51,7 @@ module RuboCop
51
51
  unless range.resize(1).source == "\n"
52
52
  corrector.insert_before(range, ' ' * column_delta)
53
53
  end
54
- elsif range.source =~ /\A[ \t]+\z/
54
+ elsif /\A[ \t]+\z/.match?(range.source)
55
55
  remove(range, corrector)
56
56
  end
57
57
  end
@@ -112,7 +112,7 @@ module RuboCop
112
112
  corrector.remove(range)
113
113
  rescue RuntimeError
114
114
  range = range_between(range.begin_pos + 1, range.end_pos + 1)
115
- retry if range.source =~ /^ +$/
115
+ retry if /^ +$/.match?(range.source)
116
116
  ensure
117
117
  $stderr = original_stderr
118
118
  end
@@ -10,8 +10,7 @@ module RuboCop
10
10
 
11
11
  lambda do |corrector|
12
12
  corrector.replace(node.loc.keyword, node.inverse_keyword)
13
- corrector.replace(condition.source_range,
14
- condition.children.first.source)
13
+ corrector.replace(condition, condition.children.first.source)
15
14
  end
16
15
  end
17
16
 
@@ -18,7 +18,7 @@ module RuboCop
18
18
  end
19
19
 
20
20
  def insert_before(node)
21
- ->(corrector) { corrector.insert_before(node.source_range, "\n") }
21
+ ->(corrector) { corrector.insert_before(node, "\n") }
22
22
  end
23
23
  end
24
24
  end
@@ -44,13 +44,13 @@ module RuboCop
44
44
  end
45
45
 
46
46
  def replace_selector(corrector)
47
- corrector.replace(method.source_range, 'lambda')
47
+ corrector.replace(method, 'lambda')
48
48
  end
49
49
 
50
50
  def remove_arguments(corrector)
51
51
  return if arguments.empty_and_without_delimiters?
52
52
 
53
- corrector.remove(arguments.source_range)
53
+ corrector.remove(arguments)
54
54
  end
55
55
 
56
56
  def insert_arguments(corrector)
@@ -62,7 +62,7 @@ module RuboCop
62
62
 
63
63
  def remove_leading_whitespace(corrector)
64
64
  corrector.remove_preceding(
65
- arguments.source_range,
65
+ arguments,
66
66
  arguments.source_range.begin_pos -
67
67
  block_node.send_node.source_range.end_pos
68
68
  )
@@ -38,9 +38,9 @@ module RuboCop
38
38
  return unless eol_comment
39
39
 
40
40
  text = eol_comment.loc.expression.source
41
- corrector.insert_before(node.source_range,
41
+ corrector.insert_before(node,
42
42
  text + "\n" + (' ' * node.loc.keyword.column))
43
- corrector.remove(eol_comment.loc.expression)
43
+ corrector.remove(eol_comment)
44
44
  end
45
45
 
46
46
  private
@@ -26,7 +26,7 @@ module RuboCop
26
26
  def wrap_contents(node, contents, char, delimiters)
27
27
  lambda do |corrector|
28
28
  corrector.replace(
29
- node.source_range,
29
+ node,
30
30
  "%#{char}#{delimiters[0]}#{contents}#{delimiters[1]}"
31
31
  )
32
32
  end
@@ -13,9 +13,9 @@ module RuboCop
13
13
  lambda do |corrector|
14
14
  str = node.str_content
15
15
  if style == :single_quotes
16
- corrector.replace(node.source_range, to_string_literal(str))
16
+ corrector.replace(node, to_string_literal(str))
17
17
  else
18
- corrector.replace(node.source_range, str.inspect)
18
+ corrector.replace(node, str.inspect)
19
19
  end
20
20
  end
21
21
  end
@@ -132,11 +132,12 @@ module RuboCop
132
132
  ).inject
133
133
  end
134
134
 
135
- def inject_config(config_file_path: 'config/default.yml')
135
+ def inject_config(config_file_path: 'config/default.yml',
136
+ version_added: bump_minor_version)
136
137
  injector =
137
138
  ConfigurationInjector.new(configuration_file_path: config_file_path,
138
139
  badge: badge,
139
- version_added: bump_minor_version)
140
+ version_added: version_added)
140
141
 
141
142
  injector.inject do
142
143
  output.puts(format(CONFIGURATION_ADDED_MESSAGE,
@@ -28,7 +28,7 @@ module RuboCop
28
28
  def autocorrect(node)
29
29
  (*, keyword) = offending_location_argument(node.parent)
30
30
 
31
- ->(corrector) { corrector.replace(node.source_range, ":#{keyword}") }
31
+ ->(corrector) { corrector.replace(node, ":#{keyword}") }
32
32
  end
33
33
 
34
34
  private
@@ -6,33 +6,76 @@ module RuboCop
6
6
  # Here we check if the elements of a multi-line array literal are
7
7
  # aligned.
8
8
  #
9
- # @example
9
+ # @example EnforcedStyle: with_first_element (default)
10
+ # # good
11
+ #
12
+ # array = [1, 2, 3,
13
+ # 4, 5, 6]
14
+ # array = ['run',
15
+ # 'forrest',
16
+ # 'run']
17
+ #
10
18
  # # bad
11
- # a = [1, 2, 3,
19
+ #
20
+ # array = [1, 2, 3,
12
21
  # 4, 5, 6]
13
22
  # array = ['run',
14
23
  # 'forrest',
15
24
  # 'run']
16
25
  #
26
+ # @example EnforcedStyle: with_fixed_indentation
17
27
  # # good
18
- # a = [1, 2, 3,
19
- # 4, 5, 6]
20
- # a = ['run',
21
- # 'forrest',
22
- # 'run']
28
+ #
29
+ # array = [1, 2, 3,
30
+ # 4, 5, 6]
31
+ #
32
+ # # bad
33
+ #
34
+ # array = [1, 2, 3,
35
+ # 4, 5, 6]
23
36
  class ArrayAlignment < Cop
24
37
  include Alignment
25
38
 
26
- MSG = 'Align the elements of an array literal if they span more ' \
27
- 'than one line.'
39
+ ALIGN_ELEMENTS_MSG = 'Align the elements of an array literal ' \
40
+ 'if they span more than one line.'
41
+
42
+ FIXED_INDENT_MSG = 'Use one level of indentation for elements ' \
43
+ 'following the first line of a multi-line array.'
28
44
 
29
45
  def on_array(node)
30
- check_alignment(node.children)
46
+ return if node.children.size < 2
47
+
48
+ check_alignment(node.children, base_column(node, node.children))
31
49
  end
32
50
 
33
51
  def autocorrect(node)
34
52
  AlignmentCorrector.correct(processed_source, node, column_delta)
35
53
  end
54
+
55
+ private
56
+
57
+ def message(_node)
58
+ fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_ELEMENTS_MSG
59
+ end
60
+
61
+ def fixed_indentation?
62
+ cop_config['EnforcedStyle'] == 'with_fixed_indentation'
63
+ end
64
+
65
+ def base_column(node, args)
66
+ if fixed_indentation?
67
+ lineno = target_method_lineno(node)
68
+ line = node.source_range.source_buffer.source_line(lineno)
69
+ indentation_of_line = /\S.*/.match(line).begin(0)
70
+ indentation_of_line + configured_indentation_width
71
+ else
72
+ display_column(args.first.source_range)
73
+ end
74
+ end
75
+
76
+ def target_method_lineno(node)
77
+ node.loc.line
78
+ end
36
79
  end
37
80
  end
38
81
  end
@@ -52,9 +52,11 @@ module RuboCop
52
52
  end
53
53
 
54
54
  def delimiter_range(node)
55
- Parser::Source::Range.new(node.loc.expression.source_buffer,
56
- node.children.last.loc.expression.end_pos,
57
- node.loc.expression.end_pos)
55
+ Parser::Source::Range.new(
56
+ node.loc.expression.source_buffer,
57
+ node.children.compact.last.loc.expression.end_pos,
58
+ node.loc.expression.end_pos
59
+ )
58
60
  end
59
61
  end
60
62
  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
@@ -44,7 +44,7 @@ module RuboCop
44
44
  when :leading
45
45
  corrector.insert_before(selector_range(node), dot)
46
46
  when :trailing
47
- corrector.insert_after(node.receiver.source_range, dot)
47
+ corrector.insert_after(node.receiver, dot)
48
48
  end
49
49
  end
50
50
  end
@@ -59,6 +59,14 @@ module RuboCop
59
59
  check_alignment(node.when_branches.last.loc.keyword, node.loc.else)
60
60
  end
61
61
 
62
+ def on_case_match(node)
63
+ return unless node.else?
64
+
65
+ check_alignment(
66
+ node.in_pattern_branches.last.loc.keyword, node.loc.else
67
+ )
68
+ end
69
+
62
70
  def autocorrect(node)
63
71
  AlignmentCorrector.correct(processed_source, node, column_delta)
64
72
  end
@@ -56,7 +56,8 @@ module RuboCop
56
56
  return if nodes.all?(&:single_line?) &&
57
57
  cop_config['AllowAdjacentOneLineDefs']
58
58
 
59
- add_offense(nodes.last, location: :keyword)
59
+ location = nodes.last.loc.keyword.join(nodes.last.loc.name)
60
+ add_offense(nodes.last, location: location)
60
61
  end
61
62
 
62
63
  def autocorrect(node)
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Layout
6
+ # Checks for a newline after attribute accessor.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # attr_accessor :foo
11
+ # def do_something
12
+ # end
13
+ #
14
+ # # good
15
+ # attr_accessor :foo
16
+ #
17
+ # def do_something
18
+ # end
19
+ #
20
+ # # good
21
+ # attr_accessor :foo
22
+ # attr_reader :bar
23
+ # attr_writer :baz
24
+ # attr :qux
25
+ #
26
+ # def do_something
27
+ # end
28
+ #
29
+ class EmptyLinesAroundAttributeAccessor < Cop
30
+ include RangeHelp
31
+
32
+ MSG = 'Add an empty line after attribute accessor.'
33
+
34
+ def on_send(node)
35
+ return unless node.attribute_accessor?
36
+ return if next_line_empty?(node.last_line)
37
+
38
+ next_line_node = next_line_node(node)
39
+ return if next_line_node.nil? || attribute_accessor?(next_line_node)
40
+
41
+ add_offense(node)
42
+ end
43
+
44
+ def autocorrect(node)
45
+ lambda do |corrector|
46
+ range = range_by_whole_lines(node.source_range)
47
+
48
+ corrector.insert_after(range, "\n")
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def next_line_empty?(line)
55
+ processed_source[line].blank?
56
+ end
57
+
58
+ def next_line_node(node)
59
+ node.parent.children[node.sibling_index + 1]
60
+ end
61
+
62
+ def attribute_accessor?(node)
63
+ node.send_type? && node.attribute_accessor?
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -75,8 +75,8 @@ module RuboCop
75
75
  style
76
76
  end
77
77
  case effective_style
78
- when :lf then MSG_DETECTED if line =~ /\r$/
79
- else MSG_MISSING if line !~ /\r$/
78
+ when :lf then MSG_DETECTED if /\r$/.match?(line)
79
+ else MSG_MISSING unless /\r$/.match?(line)
80
80
  end
81
81
  end
82
82
 
@@ -125,21 +125,15 @@ 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
- return if right_bracket.source_line[0...right_bracket.column] =~ /\S/
128
+ if /\S/.match?(right_bracket.source_line[0...right_bracket.column])
129
+ return
130
+ end
129
131
 
130
132
  expected_column = base_column(left_bracket, left_parenthesis)
131
133
  @column_delta = expected_column - right_bracket.column
132
134
  return if @column_delta.zero?
133
135
 
134
- msg = if style == :align_brackets
135
- 'Indent the right bracket the same as the left bracket.'
136
- elsif style == :special_inside_parentheses && left_parenthesis
137
- 'Indent the right bracket the same as the first position ' \
138
- 'after the preceding left parenthesis.'
139
- else
140
- 'Indent the right bracket the same as the start of the line' \
141
- ' where the left bracket is.'
142
- end
136
+ msg = msg(left_parenthesis)
143
137
  add_offense(right_bracket, location: right_bracket, message: msg)
144
138
  end
145
139
 
@@ -161,6 +155,18 @@ module RuboCop
161
155
  base_description: base_description
162
156
  )
163
157
  end
158
+
159
+ def msg(left_parenthesis)
160
+ if style == :align_brackets
161
+ 'Indent the right bracket the same as the left bracket.'
162
+ elsif style == :special_inside_parentheses && left_parenthesis
163
+ 'Indent the right bracket the same as the first position ' \
164
+ 'after the preceding left parenthesis.'
165
+ else
166
+ 'Indent the right bracket the same as the start of the line' \
167
+ ' where the left bracket is.'
168
+ end
169
+ end
164
170
  end
165
171
  end
166
172
  end
@@ -128,7 +128,7 @@ module RuboCop
128
128
 
129
129
  def check_right_brace(right_brace, left_brace, left_parenthesis)
130
130
  # if the right brace is on the same line as the last value, accept
131
- return if right_brace.source_line[0...right_brace.column] =~ /\S/
131
+ return if /\S/.match?(right_brace.source_line[0...right_brace.column])
132
132
 
133
133
  expected_column = base_column(left_brace, left_parenthesis)
134
134
  @column_delta = expected_column - right_brace.column
@@ -169,7 +169,7 @@ module RuboCop
169
169
  end
170
170
 
171
171
  def add_correct_closing_paren(node, corrector)
172
- corrector.insert_after(node.arguments.last.source_range, ')')
172
+ corrector.insert_after(node.arguments.last, ')')
173
173
  end
174
174
 
175
175
  def remove_incorrect_closing_paren(node, corrector)
@@ -249,7 +249,7 @@ module RuboCop
249
249
  def add_correct_external_trailing_comma(node, corrector)
250
250
  return unless external_trailing_comma?(node)
251
251
 
252
- corrector.insert_after(node.arguments.last.source_range, ',')
252
+ corrector.insert_after(node.arguments.last, ',')
253
253
  end
254
254
 
255
255
  def remove_incorrect_external_trailing_comma(node, corrector)