rubocop 0.52.1 → 0.53.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (292) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +2 -2
  4. data/config/default.yml +118 -46
  5. data/config/disabled.yml +8 -8
  6. data/config/enabled.yml +84 -28
  7. data/lib/rubocop.rb +28 -8
  8. data/lib/rubocop/ast/builder.rb +35 -37
  9. data/lib/rubocop/ast/node.rb +16 -1
  10. data/lib/rubocop/ast/node/and_node.rb +0 -8
  11. data/lib/rubocop/ast/node/block_node.rb +1 -9
  12. data/lib/rubocop/ast/node/case_node.rb +0 -8
  13. data/lib/rubocop/ast/node/ensure_node.rb +0 -8
  14. data/lib/rubocop/ast/node/for_node.rb +0 -8
  15. data/lib/rubocop/ast/node/or_node.rb +0 -8
  16. data/lib/rubocop/ast/node/pair_node.rb +0 -8
  17. data/lib/rubocop/ast/node/resbody_node.rb +0 -8
  18. data/lib/rubocop/ast/node/send_node.rb +0 -8
  19. data/lib/rubocop/ast/node/symbol_node.rb +0 -8
  20. data/lib/rubocop/ast/node/until_node.rb +0 -8
  21. data/lib/rubocop/ast/node/when_node.rb +0 -8
  22. data/lib/rubocop/ast/node/while_node.rb +0 -8
  23. data/lib/rubocop/cli.rb +17 -7
  24. data/lib/rubocop/comment_config.rb +24 -3
  25. data/lib/rubocop/config.rb +75 -6
  26. data/lib/rubocop/config_loader.rb +18 -28
  27. data/lib/rubocop/config_loader_resolver.rb +61 -9
  28. data/lib/rubocop/cop/bundler/duplicated_gem.rb +3 -1
  29. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +4 -2
  30. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
  31. data/lib/rubocop/cop/commissioner.rb +2 -2
  32. data/lib/rubocop/cop/cop.rb +4 -0
  33. data/lib/rubocop/cop/corrector.rb +11 -1
  34. data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -6
  35. data/lib/rubocop/cop/correctors/line_break_corrector.rb +59 -0
  36. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +1 -1
  37. data/lib/rubocop/cop/correctors/space_corrector.rb +13 -0
  38. data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +1 -1
  39. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +3 -1
  40. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  41. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +3 -5
  42. data/lib/rubocop/cop/generator.rb +29 -8
  43. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +2 -0
  44. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +2 -0
  45. data/lib/rubocop/cop/layout/align_hash.rb +106 -37
  46. data/lib/rubocop/cop/{lint → layout}/block_alignment.rb +8 -5
  47. data/lib/rubocop/cop/layout/block_end_newline.rb +7 -17
  48. data/lib/rubocop/cop/layout/case_indentation.rb +1 -0
  49. data/lib/rubocop/cop/layout/class_structure.rb +6 -7
  50. data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
  51. data/lib/rubocop/cop/{lint → layout}/condition_position.rb +3 -3
  52. data/lib/rubocop/cop/{lint → layout}/def_end_alignment.rb +2 -1
  53. data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
  54. data/lib/rubocop/cop/layout/empty_comment.rb +140 -0
  55. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -0
  56. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -0
  57. data/lib/rubocop/cop/layout/empty_lines.rb +3 -1
  58. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +7 -5
  59. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +20 -10
  60. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +20 -0
  61. data/lib/rubocop/cop/{lint → layout}/end_alignment.rb +37 -6
  62. data/lib/rubocop/cop/layout/end_of_line.rb +1 -0
  63. data/lib/rubocop/cop/layout/extra_spacing.rb +30 -37
  64. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -0
  65. data/lib/rubocop/cop/layout/indent_heredoc.rb +38 -2
  66. data/lib/rubocop/cop/layout/indentation_consistency.rb +105 -1
  67. data/lib/rubocop/cop/layout/indentation_width.rb +4 -3
  68. data/lib/rubocop/cop/layout/initial_indentation.rb +15 -1
  69. data/lib/rubocop/cop/layout/leading_comment_space.rb +4 -2
  70. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -0
  71. data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -0
  72. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +62 -29
  73. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  74. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +74 -33
  75. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +16 -2
  76. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -1
  77. data/lib/rubocop/cop/layout/space_after_method_name.rb +2 -0
  78. data/lib/rubocop/cop/layout/space_after_not.rb +2 -0
  79. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -0
  80. data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +15 -2
  81. data/lib/rubocop/cop/layout/space_around_operators.rb +15 -13
  82. data/lib/rubocop/cop/layout/space_before_block_braces.rb +13 -1
  83. data/lib/rubocop/cop/layout/space_before_comment.rb +6 -4
  84. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -0
  85. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -0
  86. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +30 -45
  87. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +3 -2
  88. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +48 -18
  89. data/lib/rubocop/cop/layout/space_inside_parens.rb +8 -7
  90. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +57 -11
  91. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +1 -0
  92. data/lib/rubocop/cop/layout/tab.rb +42 -16
  93. data/lib/rubocop/cop/layout/trailing_blank_lines.rb +46 -13
  94. data/lib/rubocop/cop/layout/trailing_whitespace.rb +12 -0
  95. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +5 -3
  96. data/lib/rubocop/cop/lint/big_decimal_new.rb +44 -0
  97. data/lib/rubocop/cop/lint/boolean_symbol.rb +2 -2
  98. data/lib/rubocop/cop/lint/circular_argument_reference.rb +2 -2
  99. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  100. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +5 -4
  101. data/lib/rubocop/cop/lint/duplicate_methods.rb +20 -9
  102. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +4 -3
  103. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +16 -10
  104. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +5 -4
  105. data/lib/rubocop/cop/lint/inherit_exception.rb +2 -2
  106. data/lib/rubocop/cop/lint/interpolation_check.rb +4 -3
  107. data/lib/rubocop/cop/lint/literal_as_condition.rb +2 -2
  108. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +2 -0
  109. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +7 -5
  110. data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
  111. data/lib/rubocop/cop/lint/number_conversion.rb +59 -0
  112. data/lib/rubocop/cop/lint/ordered_magic_comments.rb +86 -0
  113. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +2 -0
  114. data/lib/rubocop/cop/lint/percent_string_array.rb +0 -2
  115. data/lib/rubocop/cop/lint/rand_one.rb +2 -2
  116. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -0
  117. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -0
  118. data/lib/rubocop/cop/lint/require_parentheses.rb +2 -0
  119. data/lib/rubocop/cop/lint/rescue_type.rb +6 -3
  120. data/lib/rubocop/cop/lint/return_in_void_context.rb +2 -2
  121. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +17 -21
  122. data/lib/rubocop/cop/lint/script_permission.rb +30 -10
  123. data/lib/rubocop/cop/lint/shadowed_argument.rb +3 -3
  124. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -0
  125. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +2 -2
  126. data/lib/rubocop/cop/lint/unified_integer.rb +2 -2
  127. data/lib/rubocop/cop/lint/{unneeded_disable.rb → unneeded_cop_disable_directive.rb} +13 -7
  128. data/lib/rubocop/cop/lint/unneeded_cop_enable_directive.rb +97 -0
  129. data/lib/rubocop/cop/lint/unneeded_require_statement.rb +1 -0
  130. data/lib/rubocop/cop/lint/unreachable_code.rb +3 -3
  131. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +11 -10
  132. data/lib/rubocop/cop/lint/useless_access_modifier.rb +7 -5
  133. data/lib/rubocop/cop/lint/useless_assignment.rb +2 -2
  134. data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
  135. data/lib/rubocop/cop/lint/void.rb +49 -10
  136. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  137. data/lib/rubocop/cop/metrics/line_length.rb +5 -2
  138. data/lib/rubocop/cop/mixin/alignment.rb +4 -0
  139. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -1
  140. data/lib/rubocop/cop/mixin/def_node.rb +4 -0
  141. data/lib/rubocop/cop/mixin/documentation_comment.rb +11 -3
  142. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +12 -2
  143. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +20 -1
  144. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +4 -0
  145. data/lib/rubocop/cop/mixin/hash_alignment.rb +2 -2
  146. data/lib/rubocop/cop/mixin/match_range.rb +2 -0
  147. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +6 -0
  148. data/lib/rubocop/cop/mixin/nil_methods.rb +19 -0
  149. data/lib/rubocop/cop/mixin/percent_literal.rb +57 -9
  150. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +6 -5
  151. data/lib/rubocop/cop/mixin/range_help.rb +102 -0
  152. data/lib/rubocop/cop/mixin/rescue_node.rb +1 -1
  153. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +8 -7
  154. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -9
  155. data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -10
  156. data/lib/rubocop/cop/mixin/surrounding_space.rb +38 -8
  157. data/lib/rubocop/cop/mixin/trailing_body.rb +26 -0
  158. data/lib/rubocop/cop/mixin/trailing_comma.rb +15 -3
  159. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +104 -0
  160. data/lib/rubocop/cop/naming/ascii_identifiers.rb +3 -1
  161. data/lib/rubocop/cop/naming/file_name.rb +5 -10
  162. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +76 -0
  163. data/lib/rubocop/cop/naming/uncommunicative_block_param_name.rb +48 -0
  164. data/lib/rubocop/cop/naming/uncommunicative_method_param_name.rb +57 -0
  165. data/lib/rubocop/cop/offense.rb +3 -2
  166. data/lib/rubocop/cop/performance/case_when_splat.rb +1 -0
  167. data/lib/rubocop/cop/performance/casecmp.rb +17 -8
  168. data/lib/rubocop/cop/performance/compare_with_block.rb +2 -0
  169. data/lib/rubocop/cop/performance/count.rb +1 -0
  170. data/lib/rubocop/cop/performance/fixed_size.rb +41 -0
  171. data/lib/rubocop/cop/performance/flat_map.rb +2 -0
  172. data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -0
  173. data/lib/rubocop/cop/performance/redundant_merge.rb +1 -1
  174. data/lib/rubocop/cop/performance/redundant_sort_by.rb +2 -0
  175. data/lib/rubocop/cop/performance/regexp_match.rb +4 -0
  176. data/lib/rubocop/cop/performance/reverse_each.rb +2 -0
  177. data/lib/rubocop/cop/performance/string_replacement.rb +2 -0
  178. data/lib/rubocop/cop/rails/active_record_aliases.rb +46 -0
  179. data/lib/rubocop/cop/rails/blank.rb +3 -3
  180. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +6 -0
  181. data/lib/rubocop/cop/rails/delegate.rb +6 -6
  182. data/lib/rubocop/cop/rails/file_path.rb +7 -1
  183. data/lib/rubocop/cop/rails/find_by.rb +2 -0
  184. data/lib/rubocop/cop/rails/http_positional_arguments.rb +17 -5
  185. data/lib/rubocop/cop/rails/inverse_of.rb +21 -2
  186. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +45 -9
  187. data/lib/rubocop/cop/rails/presence.rb +8 -2
  188. data/lib/rubocop/cop/rails/present.rb +5 -5
  189. data/lib/rubocop/cop/rails/read_write_attribute.rb +4 -3
  190. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +1 -0
  191. data/lib/rubocop/cop/rails/relative_date_constant.rb +4 -3
  192. data/lib/rubocop/cop/rails/request_referer.rb +3 -2
  193. data/lib/rubocop/cop/rails/reversible_migration.rb +9 -8
  194. data/lib/rubocop/cop/rails/safe_navigation.rb +3 -2
  195. data/lib/rubocop/cop/rails/save_bang.rb +11 -12
  196. data/lib/rubocop/cop/rails/skips_model_validations.rb +2 -2
  197. data/lib/rubocop/cop/rails/time_zone.rb +38 -16
  198. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +26 -16
  199. data/lib/rubocop/cop/rails/validation.rb +30 -2
  200. data/lib/rubocop/cop/security/open.rb +48 -0
  201. data/lib/rubocop/cop/style/and_or.rb +1 -0
  202. data/lib/rubocop/cop/style/ascii_comments.rb +3 -1
  203. data/lib/rubocop/cop/style/attr.rb +2 -0
  204. data/lib/rubocop/cop/style/block_comments.rb +3 -1
  205. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +2 -5
  206. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -0
  207. data/lib/rubocop/cop/style/class_vars.rb +23 -0
  208. data/lib/rubocop/cop/style/colon_method_call.rb +1 -2
  209. data/lib/rubocop/cop/style/comment_annotation.rb +6 -4
  210. data/lib/rubocop/cop/style/commented_keyword.rb +3 -1
  211. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  212. data/lib/rubocop/cop/style/copyright.rb +3 -1
  213. data/lib/rubocop/cop/style/each_with_object.rb +15 -1
  214. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -0
  215. data/lib/rubocop/cop/style/empty_case_condition.rb +2 -0
  216. data/lib/rubocop/cop/style/empty_else.rb +9 -5
  217. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -0
  218. data/lib/rubocop/cop/style/empty_line_after_guard_clause.rb +80 -0
  219. data/lib/rubocop/cop/style/empty_literal.rb +1 -0
  220. data/lib/rubocop/cop/style/encoding.rb +2 -0
  221. data/lib/rubocop/cop/style/expand_path_arguments.rb +194 -0
  222. data/lib/rubocop/cop/style/for.rb +33 -0
  223. data/lib/rubocop/cop/style/format_string.rb +1 -1
  224. data/lib/rubocop/cop/style/format_string_token.rb +4 -5
  225. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +2 -1
  226. data/lib/rubocop/cop/style/hash_syntax.rb +1 -0
  227. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  228. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  229. data/lib/rubocop/cop/style/lambda.rb +1 -1
  230. data/lib/rubocop/cop/style/line_end_concatenation.rb +2 -0
  231. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -0
  232. data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -0
  233. data/lib/rubocop/cop/style/missing_else.rb +72 -7
  234. data/lib/rubocop/cop/style/mixin_usage.rb +3 -5
  235. data/lib/rubocop/cop/style/module_function.rb +10 -0
  236. data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -0
  237. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -0
  238. data/lib/rubocop/cop/style/nested_modifier.rb +2 -0
  239. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -0
  240. data/lib/rubocop/cop/style/next.rb +1 -0
  241. data/lib/rubocop/cop/style/not.rb +2 -0
  242. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  243. data/lib/rubocop/cop/style/one_line_conditional.rb +2 -2
  244. data/lib/rubocop/cop/style/redundant_exception.rb +8 -3
  245. data/lib/rubocop/cop/style/redundant_return.rb +37 -3
  246. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  247. data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -0
  248. data/lib/rubocop/cop/style/safe_navigation.rb +74 -32
  249. data/lib/rubocop/cop/style/semicolon.rb +3 -1
  250. data/lib/rubocop/cop/style/single_line_methods.rb +14 -23
  251. data/lib/rubocop/cop/style/stderr_puts.rb +2 -0
  252. data/lib/rubocop/cop/style/string_hash_keys.rb +12 -0
  253. data/lib/rubocop/cop/style/string_literals.rb +1 -1
  254. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +1 -1
  255. data/lib/rubocop/cop/style/symbol_array.rb +29 -0
  256. data/lib/rubocop/cop/style/symbol_proc.rb +2 -0
  257. data/lib/rubocop/cop/style/trailing_body_on_class.rb +43 -0
  258. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +7 -54
  259. data/lib/rubocop/cop/style/trailing_body_on_module.rb +43 -0
  260. data/lib/rubocop/cop/style/{trailing_comma_in_literal.rb → trailing_comma_in_array_literal.rb} +2 -20
  261. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +56 -0
  262. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +17 -20
  263. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -0
  264. data/lib/rubocop/cop/style/unless_else.rb +2 -0
  265. data/lib/rubocop/cop/style/word_array.rb +0 -1
  266. data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
  267. data/lib/rubocop/cop/team.rb +5 -5
  268. data/lib/rubocop/cop/util.rb +23 -188
  269. data/lib/rubocop/cop/variable_force.rb +1 -1
  270. data/lib/rubocop/file_finder.rb +45 -0
  271. data/lib/rubocop/formatter/disabled_config_formatter.rb +23 -14
  272. data/lib/rubocop/formatter/fuubar_style_formatter.rb +1 -1
  273. data/lib/rubocop/formatter/html_formatter.rb +12 -5
  274. data/lib/rubocop/formatter/json_formatter.rb +1 -1
  275. data/lib/rubocop/node_pattern.rb +8 -5
  276. data/lib/rubocop/options.rb +40 -33
  277. data/lib/rubocop/path_util.rb +5 -8
  278. data/lib/rubocop/processed_source.rb +53 -0
  279. data/lib/rubocop/remote_config.rb +1 -1
  280. data/lib/rubocop/result_cache.rb +1 -1
  281. data/lib/rubocop/rspec/cop_helper.rb +0 -4
  282. data/lib/rubocop/rspec/host_environment_simulation_helper.rb +0 -4
  283. data/lib/rubocop/rspec/shared_contexts.rb +3 -1
  284. data/lib/rubocop/rspec/shared_examples.rb +23 -25
  285. data/lib/rubocop/rspec/support.rb +5 -0
  286. data/lib/rubocop/runner.rb +3 -2
  287. data/lib/rubocop/string_util.rb +10 -9
  288. data/lib/rubocop/target_finder.rb +4 -1
  289. data/lib/rubocop/token.rb +26 -16
  290. data/lib/rubocop/version.rb +6 -4
  291. metadata +31 -17
  292. data/lib/rubocop/cop/performance/hash_each_methods.rb +0 -129
@@ -26,11 +26,13 @@ module RuboCop
26
26
  # # good
27
27
  # gem 'rubocop', groups: [:development, :test]
28
28
  class DuplicatedGem < Cop
29
+ include RangeHelp
30
+
29
31
  MSG = 'Gem `%<gem_name>s` requirements already given on line '\
30
32
  '%<line_of_first_occurrence>d of the Gemfile.'.freeze
31
33
 
32
34
  def investigate(processed_source)
33
- return unless processed_source.ast
35
+ return if processed_source.blank?
34
36
 
35
37
  duplicated_gem_nodes.each do |nodes|
36
38
  nodes[1..-1].each do |node|
@@ -26,6 +26,8 @@ module RuboCop
26
26
  # source 'https://rubygems.org' # strongly recommended
27
27
  # source 'http://rubygems.org'
28
28
  class InsecureProtocolSource < Cop
29
+ include RangeHelp
30
+
29
31
  MSG = 'The source `:%<source>s` is deprecated because HTTP requests ' \
30
32
  'are insecure. ' \
31
33
  "Please change your source to 'https://rubygems.org' " \
@@ -42,7 +44,7 @@ module RuboCop
42
44
 
43
45
  add_offense(
44
46
  node,
45
- location: source_range(node.first_argument.loc.expression),
47
+ location: range(node.first_argument.loc.expression),
46
48
  message: message
47
49
  )
48
50
  end
@@ -58,7 +60,7 @@ module RuboCop
58
60
 
59
61
  private
60
62
 
61
- def source_range(node)
63
+ def range(node)
62
64
  range_between(node.begin_pos, node.end_pos)
63
65
  end
64
66
  end
@@ -33,7 +33,7 @@ module RuboCop
33
33
  'Gem `%<previous>s` should appear before `%<current>s`.'.freeze
34
34
 
35
35
  def investigate(processed_source)
36
- return if processed_source.ast.nil?
36
+ return if processed_source.blank?
37
37
  gem_declarations(processed_source.ast)
38
38
  .each_cons(2) do |previous, current|
39
39
  next unless consecutive_lines(previous, current)
@@ -52,12 +52,12 @@ module RuboCop
52
52
 
53
53
  def investigate(processed_source)
54
54
  reset_errors
55
- remove_irrelevant_cops(processed_source.buffer.name)
55
+ remove_irrelevant_cops(processed_source.file_path)
56
56
  reset_callbacks
57
57
  prepare(processed_source)
58
58
  invoke_custom_processing(@cops, processed_source)
59
59
  invoke_custom_processing(@forces, processed_source)
60
- walk(processed_source.ast) if processed_source.ast
60
+ walk(processed_source.ast) unless processed_source.blank?
61
61
  @cops.flat_map(&:offenses)
62
62
  end
63
63
 
@@ -97,6 +97,8 @@ module RuboCop
97
97
 
98
98
  @offenses = []
99
99
  @corrections = []
100
+ @corrected_nodes = {}
101
+ @corrected_nodes.compare_by_identity
100
102
  @processed_source = nil
101
103
  end
102
104
 
@@ -145,7 +147,9 @@ module RuboCop
145
147
  def correct(node)
146
148
  return :unsupported unless support_autocorrect?
147
149
  return :uncorrected unless autocorrect?
150
+ return :already_corrected if @corrected_nodes.key?(node)
148
151
 
152
+ @corrected_nodes[node] = true
149
153
  correction = autocorrect(node)
150
154
  return :uncorrected unless correction
151
155
  @corrections << correction
@@ -33,7 +33,12 @@ module RuboCop
33
33
  def initialize(source_buffer, corrections = [])
34
34
  @source_buffer = source_buffer
35
35
  @corrections = corrections
36
- @source_rewriter = Parser::Source::Rewriter.new(source_buffer)
36
+ @source_rewriter = Parser::Source::TreeRewriter.new(
37
+ source_buffer,
38
+ different_replacements: :raise,
39
+ swallowed_insertions: :raise,
40
+ crossing_deletions: :accept
41
+ )
37
42
 
38
43
  @diagnostics = []
39
44
  # Don't print warnings to stderr if corrections conflict with each other
@@ -75,6 +80,11 @@ module RuboCop
75
80
  # @param [Parser::Source::Range] range
76
81
  # @param [String] content
77
82
  def insert_before(range, content)
83
+ # TODO: Fix Cops using bad ranges instead
84
+ if range.end_pos > @source_buffer.source.size
85
+ range = range.with(end_pos: @source_buffer.source.size)
86
+ end
87
+
78
88
  @source_rewriter.insert_before(range, content)
79
89
  end
80
90
 
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # the left or to the right, amount being determined by the instance
7
7
  # variable column_delta.
8
8
  class AlignmentCorrector
9
- extend Util
9
+ extend RangeHelp
10
10
  extend Alignment
11
11
 
12
12
  class << self
@@ -45,7 +45,8 @@ module RuboCop
45
45
 
46
46
  if column_delta > 0
47
47
  unless range.source == "\n"
48
- corrector.insert_before(range, ' ' * column_delta)
48
+ # TODO: Fix ranges instead of using `begin`
49
+ corrector.insert_before(range.begin, ' ' * column_delta)
49
50
  end
50
51
  elsif range.source =~ /\A[ \t]+\z/
51
52
  remove(range, corrector)
@@ -96,10 +97,6 @@ module RuboCop
96
97
  end
97
98
  end
98
99
 
99
- def begins_its_line?(range)
100
- (range.source_line =~ /\S/) == range.column
101
- end
102
-
103
100
  def whitespace_range(node)
104
101
  begin_pos = node.loc.end.begin_pos
105
102
 
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # This class handles autocorrection for code that needs to be moved
6
+ # to new lines.
7
+ class LineBreakCorrector
8
+ extend Alignment
9
+ extend TrailingBody
10
+ extend Util
11
+
12
+ class << self
13
+ attr_reader :processed_source
14
+
15
+ def correct_trailing_body(configured_width:, corrector:, node:,
16
+ processed_source:)
17
+ @processed_source = processed_source
18
+ range = first_part_of(node.to_a.last)
19
+ eol_comment = end_of_line_comment(node.source_range.line)
20
+
21
+ break_line_before(range: range, node: node, corrector: corrector,
22
+ configured_width: configured_width)
23
+ move_comment(eol_comment: eol_comment, node: node,
24
+ corrector: corrector)
25
+ remove_semicolon(node, corrector)
26
+ end
27
+
28
+ def break_line_before(range:, node:, corrector:, indent_steps: 1,
29
+ configured_width:)
30
+ corrector.insert_before(
31
+ range,
32
+ "\n" + ' ' * (node.loc.keyword.column +
33
+ indent_steps * configured_width)
34
+ )
35
+ end
36
+
37
+ def move_comment(eol_comment:, node:, corrector:)
38
+ return unless eol_comment
39
+ text = eol_comment.loc.expression.source
40
+ corrector.insert_before(node.source_range,
41
+ text + "\n" + (' ' * node.loc.keyword.column))
42
+ corrector.remove(eol_comment.loc.expression)
43
+ end
44
+
45
+ private
46
+
47
+ def remove_semicolon(node, corrector)
48
+ return unless semicolon(node)
49
+ corrector.remove(semicolon(node).pos)
50
+ end
51
+
52
+ def semicolon(node)
53
+ @semicolon ||= {}
54
+ @semicolon[node.object_id] ||= tokens(node).find(&:semicolon?)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # on the same line as the last contained elements, or a new line.
7
7
  class MultilineLiteralBraceCorrector
8
8
  extend MultilineLiteralBraceLayout
9
- extend Util
9
+ extend RangeHelp
10
10
 
11
11
  class << self
12
12
  attr_reader :processed_source
@@ -9,6 +9,19 @@ module RuboCop
9
9
  class << self
10
10
  attr_reader :processed_source
11
11
 
12
+ def empty_corrections(processed_source, corrector, empty_config,
13
+ left_token, right_token)
14
+ @processed_source = processed_source
15
+ if offending_empty_space?(empty_config, left_token, right_token)
16
+ range = side_space_range(range: left_token.pos, side: :right)
17
+ corrector.remove(range)
18
+ corrector.insert_after(left_token.pos, ' ')
19
+ elsif offending_empty_no_space?(empty_config, left_token, right_token)
20
+ range = side_space_range(range: left_token.pos, side: :right)
21
+ corrector.remove(range)
22
+ end
23
+ end
24
+
12
25
  def remove_space(processed_source, corrector, left_token, right_token)
13
26
  @processed_source = processed_source
14
27
  if left_token.space_after?
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  # This auto-corrects unused arguments.
6
6
  class UnusedArgCorrector
7
- extend Util
7
+ extend RangeHelp
8
8
 
9
9
  class << self
10
10
  attr_reader :processed_source
@@ -35,6 +35,8 @@ module RuboCop
35
35
  # spec.add_runtime_dependency('parser', '>= 2.3.3.1', '< 3.0')
36
36
  # end
37
37
  class DuplicatedAssignment < Cop
38
+ include RangeHelp
39
+
38
40
  MSG = '`%<assignment>s` method calls already given on line '\
39
41
  '%<line_of_first_occurrence>d of the gemspec.'.freeze
40
42
 
@@ -53,7 +55,7 @@ module RuboCop
53
55
  PATTERN
54
56
 
55
57
  def investigate(processed_source)
56
- return unless processed_source.ast
58
+ return if processed_source.blank?
57
59
 
58
60
  duplicated_assignment_method_nodes.each do |nodes|
59
61
  nodes[1..-1].each do |node|
@@ -60,7 +60,7 @@ module RuboCop
60
60
  .freeze
61
61
 
62
62
  def investigate(processed_source)
63
- return if processed_source.ast.nil?
63
+ return if processed_source.blank?
64
64
  dependency_declarations(processed_source.ast)
65
65
  .each_cons(2) do |previous, current|
66
66
  next unless consecutive_lines(previous, current)
@@ -38,8 +38,8 @@ module RuboCop
38
38
  class RequiredRubyVersion < Cop
39
39
  MSG = '`required_ruby_version` (%<required_ruby_version>s, ' \
40
40
  'declared in %<gemspec_filename>s) and `TargetRubyVersion` ' \
41
- '(%<target_ruby_version>s, declared in .rubocop.yml) ' \
42
- 'should be equal.'.freeze
41
+ '(%<target_ruby_version>s, which may be specified in ' \
42
+ '.rubocop.yml) should be equal.'.freeze
43
43
 
44
44
  def_node_search :required_ruby_version, <<-PATTERN
45
45
  (send _ :required_ruby_version= ${(str _) (array (str _))})
@@ -72,12 +72,10 @@ module RuboCop
72
72
  end
73
73
 
74
74
  def message(required_ruby_version, target_ruby_version)
75
- file_path = processed_source.buffer.name
76
-
77
75
  format(
78
76
  MSG,
79
77
  required_ruby_version: required_ruby_version,
80
- gemspec_filename: File.basename(file_path),
78
+ gemspec_filename: File.basename(processed_source.file_path),
81
79
  target_ruby_version: target_ruby_version
82
80
  )
83
81
  end
@@ -14,25 +14,45 @@ module RuboCop
14
14
  module RuboCop
15
15
  module Cop
16
16
  module %<department>s
17
- # TODO: Write cop description and example of bad / good code.
17
+ # TODO: Write cop description and example of bad / good code. For every
18
+ # `SupportedStyle` and unique configuration, there needs to be examples.
19
+ # Examples must have valid Ruby syntax. Do not use upticks.
20
+ #
21
+ # @example EnforcedStyle: bar (default)
22
+ # # Description of the `bar` style.
23
+ #
24
+ # # bad
25
+ # bad_bar_method
26
+ #
27
+ # # bad
28
+ # bad_bar_method(args)
29
+ #
30
+ # # good
31
+ # good_bar_method
32
+ #
33
+ # # good
34
+ # good_bar_method(args)
35
+ #
36
+ # @example EnforcedStyle: foo
37
+ # # Description of the `foo` style.
18
38
  #
19
- # @example
20
39
  # # bad
21
- # bad_method()
40
+ # bad_foo_method
22
41
  #
23
42
  # # bad
24
- # bad_method(args)
43
+ # bad_foo_method(args)
25
44
  #
26
45
  # # good
27
- # good_method()
46
+ # good_foo_method
28
47
  #
29
48
  # # good
30
- # good_method(args)
49
+ # good_foo_method(args)
50
+ #
31
51
  class %<cop_name>s < Cop
32
- # TODO: Implement the cop into here.
52
+ # TODO: Implement the cop in here.
33
53
  #
34
54
  # In many cases, you can use a node matcher for matching node pattern.
35
- # See. https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/node_pattern.rb
55
+ # See https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/node_pattern.rb
36
56
  #
37
57
  # For example
38
58
  MSG = 'Use `#good_method` instead of `#bad_method`.'.freeze
@@ -43,6 +63,7 @@ module RuboCop
43
63
 
44
64
  def on_send(node)
45
65
  return unless bad_method?(node)
66
+
46
67
  add_offense(node)
47
68
  end
48
69
  end
@@ -17,6 +17,8 @@ module RuboCop
17
17
  # add_offense(node, location: :selector)
18
18
  #
19
19
  class RedundantLocationArgument < Cop
20
+ include RangeHelp
21
+
20
22
  MSG = 'Redundant location argument to `#add_offense`.'.freeze
21
23
 
22
24
  def_node_matcher :add_offense_kwargs, <<-PATTERN
@@ -20,6 +20,8 @@ module RuboCop
20
20
  # add_offense(node, message: message(other_node))
21
21
  #
22
22
  class RedundantMessageArgument < Cop
23
+ include RangeHelp
24
+
23
25
  MSG = 'Redundant message argument to `#add_offense`.'.freeze
24
26
 
25
27
  def_node_matcher :node_type_check, <<-PATTERN
@@ -19,86 +19,155 @@ module RuboCop
19
19
  # - ignore_implicit (without curly braces)
20
20
  # - ignore_explicit (with curly braces)
21
21
  #
22
- # @example
23
- #
24
- # # EnforcedHashRocketStyle: key (default)
25
- # # EnforcedColonStyle: key (default)
26
- #
27
- # # good
22
+ # @example EnforcedHashRocketStyle: key (default)
23
+ # # bad
28
24
  # {
29
- # foo: bar,
30
- # ba: baz
25
+ # :foo => bar,
26
+ # :ba => baz
31
27
  # }
28
+ #
29
+ # # good
32
30
  # {
33
31
  # :foo => bar,
34
32
  # :ba => baz
35
33
  # }
36
34
  #
35
+ # @example EnforcedHashRocketStyle: separator
37
36
  # # bad
38
37
  # {
39
- # foo: bar,
40
- # ba: baz
38
+ # :foo => bar,
39
+ # :ba => baz
41
40
  # }
42
41
  # {
43
42
  # :foo => bar,
44
- # :ba => baz
43
+ # :ba => baz
45
44
  # }
46
45
  #
47
- # @example
48
- #
49
- # # EnforcedHashRocketStyle: separator
50
- # # EnforcedColonStyle: separator
51
- #
52
- # #good
53
- # {
54
- # foo: bar,
55
- # ba: baz
56
- # }
46
+ # # good
57
47
  # {
58
48
  # :foo => bar,
59
49
  # :ba => baz
60
50
  # }
61
51
  #
62
- # #bad
63
- # {
64
- # foo: bar,
65
- # ba: baz
66
- # }
52
+ # @example EnforcedHashRocketStyle: table
53
+ # # bad
67
54
  # {
68
55
  # :foo => bar,
69
- # :ba => baz
56
+ # :ba => baz
70
57
  # }
58
+ #
59
+ # # good
71
60
  # {
72
61
  # :foo => bar,
73
62
  # :ba => baz
74
63
  # }
75
64
  #
76
- # @example
65
+ # @example EnforcedColonStyle: key (default)
66
+ # # bad
67
+ # {
68
+ # foo: bar,
69
+ # ba: baz
70
+ # }
77
71
  #
78
- # # EnforcedHashRocketStyle: table
79
- # # EnforcedColonStyle: table
72
+ # # good
73
+ # {
74
+ # foo: bar,
75
+ # ba: baz
76
+ # }
80
77
  #
81
- # #good
78
+ # @example EnforcedColonStyle: separator
79
+ # # bad
82
80
  # {
83
81
  # foo: bar,
84
- # ba: baz
82
+ # ba: baz
85
83
  # }
84
+ #
85
+ # # good
86
86
  # {
87
- # :foo => bar,
88
- # :ba => baz
87
+ # foo: bar,
88
+ # ba: baz
89
89
  # }
90
90
  #
91
- # #bad
91
+ # @example EnforcedColonStyle: table
92
+ # # bad
92
93
  # {
93
94
  # foo: bar,
94
95
  # ba: baz
95
96
  # }
97
+ #
98
+ # # good
96
99
  # {
97
- # :foo => bar,
98
- # :ba => baz
100
+ # foo: bar,
101
+ # ba: baz
99
102
  # }
103
+ #
104
+ # @example EnforcedLastArgumentHashStyle: always_inspect (default)
105
+ # # Inspect both implicit and explicit hashes.
106
+ #
107
+ # # bad
108
+ # do_something(foo: 1,
109
+ # bar: 2)
110
+ #
111
+ # # bad
112
+ # do_something({foo: 1,
113
+ # bar: 2})
114
+ #
115
+ # # good
116
+ # do_something(foo: 1,
117
+ # bar: 2)
118
+ #
119
+ # # good
120
+ # do_something(
121
+ # foo: 1,
122
+ # bar: 2
123
+ # )
124
+ #
125
+ # # good
126
+ # do_something({foo: 1,
127
+ # bar: 2})
128
+ #
129
+ # # good
130
+ # do_something({
131
+ # foo: 1,
132
+ # bar: 2
133
+ # })
134
+ #
135
+ # @example EnforcedLastArgumentHashStyle: always_ignore
136
+ # # Ignore both implicit and explicit hashes.
137
+ #
138
+ # # good
139
+ # do_something(foo: 1,
140
+ # bar: 2)
141
+ #
142
+ # # good
143
+ # do_something({foo: 1,
144
+ # bar: 2})
145
+ #
146
+ # @example EnforcedLastArgumentHashStyle: ignore_implicit
147
+ # # Ignore only implicit hashes.
148
+ #
149
+ # # bad
150
+ # do_something({foo: 1,
151
+ # bar: 2})
152
+ #
153
+ # # good
154
+ # do_something(foo: 1,
155
+ # bar: 2)
156
+ #
157
+ # @example EnforcedLastArgumentHashStyle: ignore_explicit
158
+ # # Ignore only explicit hashes.
159
+ #
160
+ # # bad
161
+ # do_something(foo: 1,
162
+ # bar: 2)
163
+ #
164
+ # # good
165
+ # do_something({foo: 1,
166
+ # bar: 2})
167
+ #
100
168
  class AlignHash < Cop
101
169
  include HashAlignment
170
+ include RangeHelp
102
171
 
103
172
  MSG = 'Align the elements of a hash literal if they span more than ' \
104
173
  'one line.'.freeze