rubocop 0.79.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 (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
@@ -184,14 +184,14 @@ module RuboCop
184
184
  def adjust_minus(corrector, node)
185
185
  heredoc_beginning = node.loc.expression.source
186
186
  corrected = heredoc_beginning.sub(/<<-?/, '<<~')
187
- corrector.replace(node.loc.expression, corrected)
187
+ corrector.replace(node, corrected)
188
188
  end
189
189
 
190
190
  def correct_by_library(node)
191
191
  lambda do |corrector|
192
192
  corrector.replace(node.loc.heredoc_body, indented_body(node))
193
193
  corrected = ".#{STRIP_METHODS[style]}"
194
- corrector.insert_after(node.loc.expression, corrected)
194
+ corrector.insert_after(node, corrected)
195
195
  end
196
196
  end
197
197
 
@@ -5,9 +5,10 @@ require 'set'
5
5
  module RuboCop
6
6
  module Cop
7
7
  module Layout
8
- # This cop checks for tabs inside the source code.
8
+ # This cop checks that the indentation method is consistent.
9
+ # Either tabs only or spaces only are used for indentation.
9
10
  #
10
- # @example
11
+ # @example EnforcedStyle: spaces (default)
11
12
  # # bad
12
13
  # # This example uses a tab to indent bar.
13
14
  # def foo
@@ -20,17 +21,30 @@ module RuboCop
20
21
  # bar
21
22
  # end
22
23
  #
23
- class Tab < Cop
24
+ # @example EnforcedStyle: tabs
25
+ # # bad
26
+ # # This example uses spaces to indent bar.
27
+ # def foo
28
+ # bar
29
+ # end
30
+ #
31
+ # # good
32
+ # # This example uses a tab to indent bar.
33
+ # def foo
34
+ # bar
35
+ # end
36
+ class IndentationStyle < Cop
24
37
  include Alignment
38
+ include ConfigurableEnforcedStyle
25
39
  include RangeHelp
26
40
 
27
- MSG = 'Tab detected.'
41
+ MSG = '%<type>s detected in indentation.'
28
42
 
29
43
  def investigate(processed_source)
30
44
  str_ranges = string_literal_ranges(processed_source.ast)
31
45
 
32
46
  processed_source.lines.each.with_index(1) do |line, lineno|
33
- match = line.match(/\t+/)
47
+ match = find_offence(line)
34
48
  next unless match
35
49
 
36
50
  range = source_range(processed_source.buffer,
@@ -43,13 +57,37 @@ module RuboCop
43
57
  end
44
58
 
45
59
  def autocorrect(range)
60
+ if range.source.include?("\t")
61
+ autocorrect_lambda_for_tabs(range)
62
+ else
63
+ autocorrect_lambda_for_spaces(range)
64
+ end
65
+ end
66
+
67
+ private
68
+
69
+ def find_offence(line)
70
+ if style == :spaces
71
+ line.match(/\A\s*\t+/)
72
+ else
73
+ line.match(/\A\s* +/)
74
+ end
75
+ end
76
+
77
+ def autocorrect_lambda_for_tabs(range)
46
78
  lambda do |corrector|
47
79
  spaces = ' ' * configured_indentation_width
48
80
  corrector.replace(range, range.source.gsub(/\t/, spaces))
49
81
  end
50
82
  end
51
83
 
52
- private
84
+ def autocorrect_lambda_for_spaces(range)
85
+ lambda do |corrector|
86
+ corrector.replace(range, range.source.gsub(/\A\s+/) do |match|
87
+ "\t" * (match.size / configured_indentation_width)
88
+ end)
89
+ end
90
+ end
53
91
 
54
92
  def in_string_literal?(ranges, tabs_range)
55
93
  ranges.any? { |range| range.contains?(tabs_range) }
@@ -69,6 +107,10 @@ module RuboCop
69
107
  end
70
108
  end
71
109
  end
110
+
111
+ def message(_node)
112
+ format(MSG, type: style == :spaces ? 'Tab' : 'Space')
113
+ end
72
114
  end
73
115
  end
74
116
  end
@@ -35,6 +35,20 @@ module RuboCop
35
35
  # # Another line of comment
36
36
  # #*
37
37
  #
38
+ # @example AllowGemfileRubyComment: false (default)
39
+ #
40
+ # # bad
41
+ #
42
+ # #ruby=2.7.0
43
+ # #ruby-gemset=myproject
44
+ #
45
+ # @example AllowGemfileRubyComment: true
46
+ #
47
+ # # good
48
+ #
49
+ # #ruby=2.7.0
50
+ # #ruby-gemset=myproject
51
+ #
38
52
  class LeadingCommentSpace < Cop
39
53
  include RangeHelp
40
54
 
@@ -42,9 +56,10 @@ module RuboCop
42
56
 
43
57
  def investigate(processed_source)
44
58
  processed_source.each_comment do |comment|
45
- next unless comment.text =~ /\A#+[^#\s=:+-]/
59
+ next unless /\A#+[^#\s=:+-]/.match?(comment.text)
46
60
  next if comment.loc.line == 1 && allowed_on_first_line?(comment)
47
- next if allow_doxygen_comment? && doxygen_comment_style?(comment)
61
+ next if doxygen_comment_style?(comment)
62
+ next if gemfile_ruby_comment?(comment)
48
63
 
49
64
  add_offense(comment)
50
65
  end
@@ -80,7 +95,23 @@ module RuboCop
80
95
  end
81
96
 
82
97
  def doxygen_comment_style?(comment)
83
- comment.text.start_with?('#*')
98
+ allow_doxygen_comment? && comment.text.start_with?('#*')
99
+ end
100
+
101
+ def allow_gemfile_ruby_comment?
102
+ cop_config['AllowGemfileRubyComment']
103
+ end
104
+
105
+ def gemfile?
106
+ File.basename(processed_source.file_path).eql?('Gemfile')
107
+ end
108
+
109
+ def ruby_comment_in_gemfile?(comment)
110
+ gemfile? && comment.text.start_with?('#ruby')
111
+ end
112
+
113
+ def gemfile_ruby_comment?(comment)
114
+ allow_gemfile_ruby_comment? && ruby_comment_in_gemfile?(comment)
84
115
  end
85
116
  end
86
117
  end
@@ -8,7 +8,7 @@ module RuboCop
8
8
  # This cop checks the length of lines in the source code.
9
9
  # The maximum length is configurable.
10
10
  # The tab size is configured in the `IndentationWidth`
11
- # of the `Layout/Tab` cop.
11
+ # of the `Layout/IndentationStyle` cop.
12
12
  # It also ignores a shebang line by default.
13
13
  #
14
14
  # This cop has some autocorrection capabilities.
@@ -19,19 +19,25 @@ module RuboCop
19
19
  #
20
20
  # If autocorrection is enabled, the following Layout cops
21
21
  # are recommended to further format the broken lines.
22
+ # (Many of these are enabled by default.)
22
23
  #
23
- # - ParameterAlignment
24
24
  # - ArgumentAlignment
25
+ # - BlockAlignment
26
+ # - BlockDelimiters
27
+ # - BlockEndNewline
25
28
  # - ClosingParenthesisIndentation
26
29
  # - FirstArgumentIndentation
27
30
  # - FirstArrayElementIndentation
28
31
  # - FirstHashElementIndentation
29
32
  # - FirstParameterIndentation
30
33
  # - HashAlignment
34
+ # - IndentationWidth
31
35
  # - MultilineArrayLineBreaks
36
+ # - MultilineBlockLayout
32
37
  # - MultilineHashBraceLayout
33
38
  # - MultilineHashKeyLineBreaks
34
39
  # - MultilineMethodArgumentLineBreaks
40
+ # - ParameterAlignment
35
41
  #
36
42
  # Together, these cops will pretty print hashes, arrays,
37
43
  # method calls, etc. For example, let's say the max columns
@@ -61,6 +67,10 @@ module RuboCop
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
@@ -109,6 +119,25 @@ module RuboCop
109
119
  end
110
120
  end
111
121
 
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)
131
+ end
132
+
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
139
+ end
140
+
112
141
  def breakable_range_after_semicolon(semicolon_token)
113
142
  range = semicolon_token.pos
114
143
  end_pos = range.end_pos
@@ -116,7 +145,7 @@ module RuboCop
116
145
  return nil unless next_range.line == range.line
117
146
 
118
147
  next_char = next_range.source
119
- return nil if /[\r\n]/ =~ next_char
148
+ return nil if /[\r\n]/.match?(next_char)
120
149
  return nil if next_char == ';'
121
150
 
122
151
  next_range
@@ -131,7 +160,10 @@ module RuboCop
131
160
  end
132
161
 
133
162
  def highlight_start(line)
134
- 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
135
167
  end
136
168
 
137
169
  def check_line(line, line_index)
@@ -129,7 +129,7 @@ module RuboCop
129
129
 
130
130
  block_start_col = node.source_range.column
131
131
 
132
- corrector.insert_before(first_node.source_range,
132
+ corrector.insert_before(first_node,
133
133
  "\n #{' ' * block_start_col}")
134
134
  end
135
135
 
@@ -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
@@ -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
@@ -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, ' ')
@@ -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