rubocop 0.78.0 → 0.82.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 (209) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +4 -4
  4. data/config/default.yml +158 -48
  5. data/lib/rubocop.rb +19 -4
  6. data/lib/rubocop/ast/builder.rb +45 -42
  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/def_node.rb +11 -0
  12. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  13. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +8 -0
  14. data/lib/rubocop/ast/node/regexp_node.rb +2 -4
  15. data/lib/rubocop/ast/traversal.rb +29 -10
  16. data/lib/rubocop/cli.rb +10 -4
  17. data/lib/rubocop/cli/command/show_cops.rb +11 -4
  18. data/lib/rubocop/comment_config.rb +6 -1
  19. data/lib/rubocop/config.rb +36 -10
  20. data/lib/rubocop/config_loader.rb +42 -33
  21. data/lib/rubocop/config_loader_resolver.rb +1 -1
  22. data/lib/rubocop/config_obsoletion.rb +4 -1
  23. data/lib/rubocop/config_validator.rb +66 -92
  24. data/lib/rubocop/cop/autocorrect_logic.rb +6 -3
  25. data/lib/rubocop/cop/badge.rb +5 -5
  26. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -1
  27. data/lib/rubocop/cop/corrector.rb +48 -24
  28. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -2
  29. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -2
  30. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  31. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +3 -3
  32. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
  33. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  34. data/lib/rubocop/cop/correctors/string_literal_corrector.rb +2 -2
  35. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  36. data/lib/rubocop/cop/generator.rb +3 -4
  37. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  38. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -1
  39. data/lib/rubocop/cop/layout/array_alignment.rb +53 -10
  40. data/lib/rubocop/cop/layout/block_end_newline.rb +5 -3
  41. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  42. data/lib/rubocop/cop/layout/else_alignment.rb +8 -0
  43. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
  44. data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
  45. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +16 -10
  46. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
  47. data/lib/rubocop/cop/layout/hash_alignment.rb +8 -4
  48. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  49. data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
  50. data/lib/rubocop/cop/layout/{tab.rb → indentation_style.rb} +48 -6
  51. data/lib/rubocop/cop/layout/leading_comment_space.rb +34 -3
  52. data/lib/rubocop/cop/layout/line_length.rb +32 -3
  53. data/lib/rubocop/cop/layout/multiline_block_layout.rb +15 -6
  54. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +0 -4
  55. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  56. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +3 -3
  57. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +133 -0
  58. data/lib/rubocop/cop/layout/space_around_operators.rb +19 -1
  59. data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -2
  60. data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
  61. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
  62. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -2
  63. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
  64. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +2 -2
  65. data/lib/rubocop/cop/lint/boolean_symbol.rb +12 -0
  66. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  67. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  68. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  69. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
  70. data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
  71. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  72. data/lib/rubocop/cop/lint/loop.rb +6 -4
  73. data/lib/rubocop/cop/lint/multiple_comparison.rb +1 -1
  74. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  75. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
  76. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  77. data/lib/rubocop/cop/lint/percent_string_array.rb +2 -2
  78. data/lib/rubocop/cop/lint/raise_exception.rb +75 -0
  79. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -1
  80. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +12 -7
  81. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
  82. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -1
  83. data/lib/rubocop/cop/lint/struct_new_override.rb +58 -0
  84. data/lib/rubocop/cop/lint/suppressed_exception.rb +12 -22
  85. data/lib/rubocop/cop/lint/unified_integer.rb +0 -2
  86. data/lib/rubocop/cop/lint/unused_method_argument.rb +32 -6
  87. data/lib/rubocop/cop/lint/uri_regexp.rb +4 -4
  88. data/lib/rubocop/cop/lint/useless_setter_call.rb +4 -0
  89. data/lib/rubocop/cop/migration/department_name.rb +47 -6
  90. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
  91. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +6 -1
  92. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +7 -7
  93. data/lib/rubocop/cop/mixin/hash_transform_method.rb +171 -0
  94. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
  95. data/lib/rubocop/cop/mixin/method_complexity.rb +5 -0
  96. data/lib/rubocop/cop/mixin/statement_modifier.rb +4 -3
  97. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -10
  98. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  99. data/lib/rubocop/cop/naming/constant_name.rb +2 -1
  100. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  101. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  102. data/lib/rubocop/cop/naming/method_name.rb +30 -0
  103. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  104. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  105. data/lib/rubocop/cop/registry.rb +15 -3
  106. data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -6
  107. data/lib/rubocop/cop/style/alias.rb +4 -4
  108. data/lib/rubocop/cop/style/and_or.rb +5 -6
  109. data/lib/rubocop/cop/style/array_join.rb +1 -1
  110. data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
  111. data/lib/rubocop/cop/style/case_equality.rb +24 -1
  112. data/lib/rubocop/cop/style/character_literal.rb +2 -2
  113. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  114. data/lib/rubocop/cop/style/conditional_assignment.rb +8 -8
  115. data/lib/rubocop/cop/style/copyright.rb +1 -1
  116. data/lib/rubocop/cop/style/dir.rb +1 -1
  117. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -0
  118. data/lib/rubocop/cop/style/documentation.rb +43 -5
  119. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -1
  120. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  121. data/lib/rubocop/cop/style/each_with_object.rb +3 -3
  122. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  123. data/lib/rubocop/cop/style/end_block.rb +6 -0
  124. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  125. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -3
  126. data/lib/rubocop/cop/style/exponential_notation.rb +119 -0
  127. data/lib/rubocop/cop/style/format_string.rb +2 -2
  128. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +89 -11
  129. data/lib/rubocop/cop/style/hash_each_methods.rb +89 -0
  130. data/lib/rubocop/cop/style/hash_syntax.rb +3 -5
  131. data/lib/rubocop/cop/style/hash_transform_keys.rb +83 -0
  132. data/lib/rubocop/cop/style/hash_transform_values.rb +80 -0
  133. data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -3
  134. data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
  135. data/lib/rubocop/cop/style/lambda.rb +3 -2
  136. data/lib/rubocop/cop/style/lambda_call.rb +2 -2
  137. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -205
  138. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +169 -0
  139. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  140. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  141. data/lib/rubocop/cop/style/module_function.rb +58 -12
  142. data/lib/rubocop/cop/style/multiline_if_modifier.rb +1 -1
  143. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  144. data/lib/rubocop/cop/style/multiline_when_then.rb +5 -1
  145. data/lib/rubocop/cop/style/mutable_constant.rb +2 -4
  146. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +4 -4
  147. data/lib/rubocop/cop/style/next.rb +2 -2
  148. data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
  149. data/lib/rubocop/cop/style/non_nil_check.rb +4 -4
  150. data/lib/rubocop/cop/style/not.rb +1 -1
  151. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  152. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  153. data/lib/rubocop/cop/style/numeric_predicate.rb +5 -4
  154. data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
  155. data/lib/rubocop/cop/style/or_assignment.rb +4 -3
  156. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  157. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  158. data/lib/rubocop/cop/style/perl_backrefs.rb +2 -2
  159. data/lib/rubocop/cop/style/proc.rb +1 -1
  160. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  161. data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
  162. data/lib/rubocop/cop/style/redundant_condition.rb +18 -6
  163. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
  164. data/lib/rubocop/cop/style/redundant_exception.rb +3 -3
  165. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  166. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  167. data/lib/rubocop/cop/style/redundant_return.rb +5 -7
  168. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  169. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  170. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  171. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  172. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  173. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  174. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  175. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -4
  176. data/lib/rubocop/cop/style/string_hash_keys.rb +1 -1
  177. data/lib/rubocop/cop/style/symbol_array.rb +3 -3
  178. data/lib/rubocop/cop/style/symbol_literal.rb +2 -2
  179. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -3
  180. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +35 -22
  181. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +41 -0
  182. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +88 -0
  183. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +44 -0
  184. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  185. data/lib/rubocop/cop/style/unpack_first.rb +0 -4
  186. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
  187. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  188. data/lib/rubocop/cop/style/word_array.rb +1 -1
  189. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  190. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
  191. data/lib/rubocop/cop/variable_force.rb +4 -1
  192. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  193. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  194. data/lib/rubocop/formatter/junit_formatter.rb +74 -0
  195. data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
  196. data/lib/rubocop/formatter/tap_formatter.rb +1 -1
  197. data/lib/rubocop/node_pattern.rb +96 -10
  198. data/lib/rubocop/options.rb +7 -1
  199. data/lib/rubocop/processed_source.rb +1 -4
  200. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  201. data/lib/rubocop/rspec/expect_offense.rb +1 -1
  202. data/lib/rubocop/rspec/shared_contexts.rb +5 -4
  203. data/lib/rubocop/runner.rb +1 -1
  204. data/lib/rubocop/target_ruby.rb +151 -0
  205. data/lib/rubocop/version.rb +1 -1
  206. metadata +39 -12
  207. data/lib/rubocop/cop/lint/end_in_method.rb +0 -40
  208. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
  209. data/lib/rubocop/formatter/disabled_lines_formatter.rb +0 -57
@@ -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)
@@ -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
@@ -179,8 +179,12 @@ module RuboCop
179
179
  include HashAlignmentStyles
180
180
  include RangeHelp
181
181
 
182
- MSG = 'Align the elements of a hash literal if they span more than ' \
183
- 'one line.'
182
+ MESSAGES = { KeyAlignment => 'Align the keys of a hash literal if ' \
183
+ 'they span more than one line.',
184
+ SeparatorAlignment => 'Align the separators of a hash ' \
185
+ 'literal if they span more than one line.',
186
+ TableAlignment => 'Align the keys and values of a hash ' \
187
+ 'literal if they span more than one line.' }.freeze
184
188
 
185
189
  def on_send(node)
186
190
  return if double_splat?(node)
@@ -249,9 +253,9 @@ module RuboCop
249
253
  end
250
254
 
251
255
  def add_offences
252
- _format, offences = offences_by.min_by { |_, v| v.length }
256
+ format, offences = offences_by.min_by { |_, v| v.length }
253
257
  (offences || []).each do |offence|
254
- add_offense offence
258
+ add_offense(offence, message: MESSAGES[format])
255
259
  end
256
260
  end
257
261
 
@@ -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)
@@ -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
@@ -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
@@ -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
 
@@ -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, ' ')