rubocop 0.49.1 → 0.50.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/README.md +5 -3
  3. data/bin/rubocop +1 -1
  4. data/config/default.yml +160 -99
  5. data/config/disabled.yml +4 -5
  6. data/config/enabled.yml +149 -47
  7. data/lib/rubocop.rb +41 -14
  8. data/lib/rubocop/ast/builder.rb +4 -1
  9. data/lib/rubocop/ast/node.rb +36 -42
  10. data/lib/rubocop/ast/node/args_node.rb +1 -13
  11. data/lib/rubocop/ast/node/array_node.rb +9 -0
  12. data/lib/rubocop/ast/node/block_node.rb +9 -0
  13. data/lib/rubocop/ast/node/def_node.rb +71 -0
  14. data/lib/rubocop/ast/node/for_node.rb +8 -0
  15. data/lib/rubocop/ast/node/if_node.rb +10 -2
  16. data/lib/rubocop/ast/node/mixin/collection_node.rb +15 -0
  17. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +174 -0
  18. data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +89 -0
  19. data/lib/rubocop/ast/node/mixin/parameterized_node.rb +18 -31
  20. data/lib/rubocop/ast/node/regexp_node.rb +35 -0
  21. data/lib/rubocop/ast/node/send_node.rb +1 -154
  22. data/lib/rubocop/ast/node/super_node.rb +3 -24
  23. data/lib/rubocop/ast/node/yield_node.rb +21 -0
  24. data/lib/rubocop/ast/traversal.rb +6 -6
  25. data/lib/rubocop/cli.rb +7 -3
  26. data/lib/rubocop/config.rb +45 -8
  27. data/lib/rubocop/config_loader.rb +7 -5
  28. data/lib/rubocop/cop/bundler/duplicated_gem.rb +3 -3
  29. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +64 -0
  30. data/lib/rubocop/cop/bundler/ordered_gems.rb +12 -12
  31. data/lib/rubocop/cop/commissioner.rb +8 -2
  32. data/lib/rubocop/cop/cop.rb +3 -1
  33. data/lib/rubocop/cop/generator.rb +94 -21
  34. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  35. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +14 -3
  36. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +43 -0
  37. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +46 -0
  38. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +49 -0
  39. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +1 -1
  40. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +2 -3
  41. data/lib/rubocop/cop/layout/align_array.rb +2 -2
  42. data/lib/rubocop/cop/layout/align_hash.rb +2 -2
  43. data/lib/rubocop/cop/layout/align_parameters.rb +5 -11
  44. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +5 -5
  45. data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
  46. data/lib/rubocop/cop/layout/dot_position.rb +9 -0
  47. data/lib/rubocop/cop/layout/else_alignment.rb +30 -13
  48. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +4 -0
  49. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +20 -4
  50. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +3 -3
  51. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +3 -3
  52. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +3 -3
  53. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +5 -2
  54. data/lib/rubocop/cop/layout/indent_heredoc.rb +19 -24
  55. data/lib/rubocop/cop/layout/indentation_consistency.rb +1 -2
  56. data/lib/rubocop/cop/layout/indentation_width.rb +12 -8
  57. data/lib/rubocop/cop/layout/leading_comment_space.rb +33 -18
  58. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +20 -17
  59. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +3 -3
  60. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +6 -0
  61. data/lib/rubocop/cop/layout/space_after_colon.rb +7 -0
  62. data/lib/rubocop/cop/layout/space_after_comma.rb +10 -0
  63. data/lib/rubocop/cop/layout/space_after_method_name.rb +5 -3
  64. data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
  65. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +13 -4
  66. data/lib/rubocop/cop/layout/space_around_keyword.rb +9 -5
  67. data/lib/rubocop/cop/layout/space_before_block_braces.rb +54 -5
  68. data/lib/rubocop/cop/layout/space_before_comment.rb +7 -0
  69. data/lib/rubocop/cop/layout/space_before_semicolon.rb +7 -0
  70. data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +1 -1
  71. data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
  72. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
  73. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +8 -4
  74. data/lib/rubocop/cop/layout/tab.rb +1 -1
  75. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -2
  76. data/lib/rubocop/cop/lint/assignment_in_condition.rb +15 -1
  77. data/lib/rubocop/cop/lint/block_alignment.rb +15 -6
  78. data/lib/rubocop/cop/lint/boolean_symbol.rb +38 -0
  79. data/lib/rubocop/cop/lint/condition_position.rb +5 -1
  80. data/lib/rubocop/cop/lint/debugger.rb +16 -9
  81. data/lib/rubocop/cop/lint/def_end_alignment.rb +4 -4
  82. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +3 -3
  83. data/lib/rubocop/cop/lint/duplicate_methods.rb +73 -5
  84. data/lib/rubocop/cop/lint/duplicated_key.rb +1 -1
  85. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  86. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  87. data/lib/rubocop/cop/lint/empty_expression.rb +1 -1
  88. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  89. data/lib/rubocop/cop/lint/empty_when.rb +1 -1
  90. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  91. data/lib/rubocop/cop/lint/float_out_of_range.rb +5 -5
  92. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +35 -40
  93. data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
  94. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +18 -13
  95. data/lib/rubocop/cop/lint/inherit_exception.rb +8 -7
  96. data/lib/rubocop/cop/lint/interpolation_check.rb +36 -0
  97. data/lib/rubocop/cop/lint/literal_in_condition.rb +3 -3
  98. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  99. data/lib/rubocop/cop/lint/multiple_compare.rb +1 -1
  100. data/lib/rubocop/cop/lint/nested_method_definition.rb +5 -7
  101. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
  102. data/lib/rubocop/cop/lint/percent_string_array.rb +3 -12
  103. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  104. data/lib/rubocop/cop/lint/rand_one.rb +7 -1
  105. data/lib/rubocop/cop/lint/redundant_with_index.rb +77 -0
  106. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
  107. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  108. data/lib/rubocop/cop/lint/rescue_type.rb +13 -6
  109. data/lib/rubocop/cop/lint/rescue_without_error_class.rb +38 -0
  110. data/lib/rubocop/cop/lint/return_in_void_context.rb +63 -0
  111. data/lib/rubocop/cop/lint/script_permission.rb +6 -0
  112. data/lib/rubocop/cop/lint/syntax.rb +17 -20
  113. data/lib/rubocop/cop/lint/unified_integer.rb +3 -2
  114. data/lib/rubocop/cop/lint/unneeded_disable.rb +1 -1
  115. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
  116. data/lib/rubocop/cop/lint/unreachable_code.rb +53 -8
  117. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +74 -0
  118. data/lib/rubocop/cop/lint/uri_regexp.rb +73 -0
  119. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -8
  120. data/lib/rubocop/cop/lint/useless_setter_call.rb +10 -11
  121. data/lib/rubocop/cop/lint/void.rb +29 -23
  122. data/lib/rubocop/cop/metrics/line_length.rb +2 -2
  123. data/lib/rubocop/cop/metrics/method_length.rb +8 -3
  124. data/lib/rubocop/cop/metrics/parameter_lists.rb +5 -2
  125. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +1 -1
  126. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -2
  127. data/lib/rubocop/cop/mixin/first_element_line_break.rb +12 -3
  128. data/lib/rubocop/cop/mixin/heredoc.rb +28 -0
  129. data/lib/rubocop/cop/mixin/method_complexity.rb +9 -6
  130. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +68 -31
  131. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +18 -0
  132. data/lib/rubocop/cop/mixin/negative_conditional.rb +1 -1
  133. data/lib/rubocop/cop/mixin/percent_array.rb +52 -0
  134. data/lib/rubocop/cop/mixin/string_help.rb +1 -1
  135. data/lib/rubocop/cop/{style → naming}/accessor_method_name.rb +11 -12
  136. data/lib/rubocop/cop/{style → naming}/ascii_identifiers.rb +1 -1
  137. data/lib/rubocop/cop/{style/op_method.rb → naming/binary_operator_parameter_name.rb} +2 -2
  138. data/lib/rubocop/cop/{style → naming}/class_and_module_camel_case.rb +1 -1
  139. data/lib/rubocop/cop/{style → naming}/constant_name.rb +1 -1
  140. data/lib/rubocop/cop/{style → naming}/file_name.rb +8 -4
  141. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +68 -0
  142. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +58 -0
  143. data/lib/rubocop/cop/{style → naming}/method_name.rb +1 -1
  144. data/lib/rubocop/cop/{style → naming}/predicate_name.rb +6 -7
  145. data/lib/rubocop/cop/{style → naming}/variable_name.rb +11 -15
  146. data/lib/rubocop/cop/{style → naming}/variable_number.rb +1 -1
  147. data/lib/rubocop/cop/performance/caller.rb +39 -11
  148. data/lib/rubocop/cop/performance/casecmp.rb +4 -4
  149. data/lib/rubocop/cop/performance/compare_with_block.rb +4 -4
  150. data/lib/rubocop/cop/performance/double_start_end_with.rb +4 -4
  151. data/lib/rubocop/cop/performance/end_with.rb +3 -3
  152. data/lib/rubocop/cop/performance/fixed_size.rb +1 -1
  153. data/lib/rubocop/cop/performance/hash_each_methods.rb +66 -25
  154. data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -2
  155. data/lib/rubocop/cop/performance/range_include.rb +2 -2
  156. data/lib/rubocop/cop/performance/redundant_block_call.rb +6 -6
  157. data/lib/rubocop/cop/performance/redundant_match.rb +5 -5
  158. data/lib/rubocop/cop/performance/redundant_merge.rb +39 -23
  159. data/lib/rubocop/cop/performance/redundant_sort_by.rb +2 -2
  160. data/lib/rubocop/cop/performance/regexp_match.rb +13 -5
  161. data/lib/rubocop/cop/performance/size.rb +1 -1
  162. data/lib/rubocop/cop/performance/start_with.rb +3 -3
  163. data/lib/rubocop/cop/performance/times_map.rb +23 -12
  164. data/lib/rubocop/cop/performance/unfreeze_string.rb +50 -0
  165. data/lib/rubocop/cop/performance/uri_default_parser.rb +47 -0
  166. data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -1
  167. data/lib/rubocop/cop/rails/delegate.rb +36 -7
  168. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
  169. data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -2
  170. data/lib/rubocop/cop/rails/file_path.rb +3 -4
  171. data/lib/rubocop/cop/rails/find_each.rb +1 -1
  172. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +48 -0
  173. data/lib/rubocop/cop/rails/http_positional_arguments.rb +5 -5
  174. data/lib/rubocop/cop/rails/not_null_column.rb +1 -1
  175. data/lib/rubocop/cop/rails/pluralization_grammar.rb +2 -2
  176. data/lib/rubocop/cop/rails/relative_date_constant.rb +1 -1
  177. data/lib/rubocop/cop/rails/request_referer.rb +2 -2
  178. data/lib/rubocop/cop/rails/reversible_migration.rb +12 -12
  179. data/lib/rubocop/cop/rails/save_bang.rb +8 -6
  180. data/lib/rubocop/cop/rails/scope_args.rb +1 -1
  181. data/lib/rubocop/cop/security/eval.rb +2 -2
  182. data/lib/rubocop/cop/security/json_load.rb +2 -2
  183. data/lib/rubocop/cop/security/marshal_load.rb +2 -2
  184. data/lib/rubocop/cop/security/yaml_load.rb +2 -2
  185. data/lib/rubocop/cop/style/alias.rb +44 -20
  186. data/lib/rubocop/cop/style/and_or.rb +48 -34
  187. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +1 -1
  188. data/lib/rubocop/cop/style/block_comments.rb +3 -1
  189. data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
  190. data/lib/rubocop/cop/style/command_literal.rb +20 -9
  191. data/lib/rubocop/cop/style/conditional_assignment.rb +30 -28
  192. data/lib/rubocop/cop/style/copyright.rb +10 -10
  193. data/lib/rubocop/cop/style/def_with_parentheses.rb +6 -5
  194. data/lib/rubocop/cop/style/dir.rb +52 -0
  195. data/lib/rubocop/cop/style/documentation_method.rb +2 -6
  196. data/lib/rubocop/cop/style/empty_case_condition.rb +1 -1
  197. data/lib/rubocop/cop/style/empty_else.rb +3 -2
  198. data/lib/rubocop/cop/style/empty_literal.rb +1 -2
  199. data/lib/rubocop/cop/style/empty_method.rb +27 -17
  200. data/lib/rubocop/cop/style/flip_flop.rb +2 -2
  201. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  202. data/lib/rubocop/cop/style/guard_clause.rb +4 -2
  203. data/lib/rubocop/cop/style/hash_syntax.rb +10 -10
  204. data/lib/rubocop/cop/style/identical_conditional_branches.rb +5 -1
  205. data/lib/rubocop/cop/style/if_with_semicolon.rb +1 -1
  206. data/lib/rubocop/cop/style/implicit_runtime_error.rb +4 -3
  207. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  208. data/lib/rubocop/cop/style/inverse_methods.rb +20 -8
  209. data/lib/rubocop/cop/style/lambda.rb +19 -9
  210. data/lib/rubocop/cop/style/lambda_call.rb +22 -1
  211. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +4 -20
  212. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +6 -4
  213. data/lib/rubocop/cop/style/method_def_parentheses.rb +18 -26
  214. data/lib/rubocop/cop/style/method_missing.rb +5 -18
  215. data/lib/rubocop/cop/style/min_max.rb +67 -0
  216. data/lib/rubocop/cop/style/missing_else.rb +16 -3
  217. data/lib/rubocop/cop/style/mixin_grouping.rb +2 -2
  218. data/lib/rubocop/cop/style/module_function.rb +8 -4
  219. data/lib/rubocop/cop/style/multiline_if_modifier.rb +5 -1
  220. data/lib/rubocop/cop/style/multiline_memoization.rb +25 -3
  221. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  222. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
  223. data/lib/rubocop/cop/style/mutable_constant.rb +2 -6
  224. data/lib/rubocop/cop/style/negated_if.rb +8 -4
  225. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +8 -8
  226. data/lib/rubocop/cop/style/nested_ternary_operator.rb +1 -1
  227. data/lib/rubocop/cop/style/non_nil_check.rb +14 -14
  228. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +6 -2
  229. data/lib/rubocop/cop/style/numeric_literals.rb +2 -2
  230. data/lib/rubocop/cop/style/numeric_predicate.rb +8 -4
  231. data/lib/rubocop/cop/style/one_line_conditional.rb +8 -3
  232. data/lib/rubocop/cop/style/option_hash.rb +1 -1
  233. data/lib/rubocop/cop/style/optional_arguments.rb +1 -2
  234. data/lib/rubocop/cop/style/or_assignment.rb +88 -0
  235. data/lib/rubocop/cop/style/parallel_assignment.rb +2 -2
  236. data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -11
  237. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  238. data/lib/rubocop/cop/style/percent_q_literals.rb +2 -2
  239. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  240. data/lib/rubocop/cop/style/proc.rb +1 -1
  241. data/lib/rubocop/cop/style/raise_args.rb +16 -17
  242. data/lib/rubocop/cop/style/redundant_begin.rb +6 -5
  243. data/lib/rubocop/cop/style/redundant_conditional.rb +95 -0
  244. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  245. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -11
  246. data/lib/rubocop/cop/style/redundant_return.rb +23 -11
  247. data/lib/rubocop/cop/style/redundant_self.rb +18 -9
  248. data/lib/rubocop/cop/style/regexp_literal.rb +12 -4
  249. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  250. data/lib/rubocop/cop/style/return_nil.rb +98 -0
  251. data/lib/rubocop/cop/style/safe_navigation.rb +80 -43
  252. data/lib/rubocop/cop/style/single_line_block_params.rb +14 -13
  253. data/lib/rubocop/cop/style/single_line_methods.rb +9 -13
  254. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  255. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +17 -39
  256. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +22 -1
  257. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  258. data/lib/rubocop/cop/style/symbol_array.rb +5 -25
  259. data/lib/rubocop/cop/style/symbol_literal.rb +1 -1
  260. data/lib/rubocop/cop/style/symbol_proc.rb +3 -18
  261. data/lib/rubocop/cop/style/ternary_parentheses.rb +14 -10
  262. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +28 -9
  263. data/lib/rubocop/cop/style/trivial_accessors.rb +39 -56
  264. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  265. data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -1
  266. data/lib/rubocop/cop/style/unneeded_interpolation.rb +1 -1
  267. data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
  268. data/lib/rubocop/cop/style/variable_interpolation.rb +8 -3
  269. data/lib/rubocop/cop/style/word_array.rb +7 -24
  270. data/lib/rubocop/cop/style/yoda_condition.rb +49 -14
  271. data/lib/rubocop/cop/style/zero_length_predicate.rb +25 -18
  272. data/lib/rubocop/cop/team.rb +16 -8
  273. data/lib/rubocop/cop/util.rb +11 -0
  274. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  275. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  276. data/lib/rubocop/formatter/offense_count_formatter.rb +2 -0
  277. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  278. data/lib/rubocop/formatter/tap_formatter.rb +71 -0
  279. data/lib/rubocop/formatter/worst_offenders_formatter.rb +2 -0
  280. data/lib/rubocop/node_pattern.rb +44 -26
  281. data/lib/rubocop/options.rb +1 -0
  282. data/lib/rubocop/processed_source.rb +3 -1
  283. data/lib/rubocop/remote_config.rb +5 -1
  284. data/lib/rubocop/result_cache.rb +1 -0
  285. data/lib/rubocop/rspec/cop_helper.rb +10 -10
  286. data/lib/rubocop/rspec/expect_offense.rb +6 -8
  287. data/lib/rubocop/rspec/shared_examples.rb +8 -8
  288. data/lib/rubocop/string_util.rb +2 -0
  289. data/lib/rubocop/version.rb +1 -1
  290. metadata +51 -18
  291. data/lib/rubocop/cop/mixin/access_modifier_node.rb +0 -41
  292. data/lib/rubocop/cop/mixin/on_method_def.rb +0 -44
@@ -9,18 +9,34 @@ module RuboCop
9
9
  #
10
10
  # @example
11
11
  #
12
+ # # EnforcedStyle: all_comparison_operators
13
+ #
12
14
  # # bad
13
15
  # 99 == foo
14
- # "bar" == foo
16
+ # "bar" != foo
15
17
  # 42 >= foo
16
- #
17
- # @example
18
+ # 10 < bar
18
19
  #
19
20
  # # good
20
21
  # foo == 99
21
22
  # foo == "bar"
22
- # for <= 42
23
+ # foo <= 42
24
+ # bar > 10
25
+ #
26
+ # @example
27
+ #
28
+ # # EnforcedStyle: equality_operators_only
29
+ #
30
+ # # bad
31
+ # 99 == foo
32
+ # "bar" != foo
33
+ #
34
+ # # good
35
+ # 99 >= foo
36
+ # 3 < a && a < 5
23
37
  class YodaCondition < Cop
38
+ include ConfigurableEnforcedStyle
39
+
24
40
  MSG = 'Reverse the order of the operands `%s`.'.freeze
25
41
 
26
42
  REVERSE_COMPARISON = {
@@ -30,26 +46,33 @@ module RuboCop
30
46
  '>=' => '<='
31
47
  }.freeze
32
48
 
49
+ EQUALITY_OPERATORS = %i[== !=].freeze
50
+
51
+ NONCOMMUTATIVE_OPERATORS = %i[===].freeze
52
+
33
53
  def on_send(node)
34
54
  return unless yoda_condition?(node)
35
55
 
36
- register_offense(node)
56
+ add_offense(node)
37
57
  end
38
58
 
39
59
  private
40
60
 
41
61
  def yoda_condition?(node)
42
- return false unless comparison_operator?(node)
62
+ return false unless node.comparison_method?
43
63
 
44
- node.receiver.literal? && !node.arguments.first.literal?
45
- end
64
+ lhs, operator, rhs = *node
65
+ if check_equality_only?
66
+ return false if non_equality_operator?(operator)
67
+ end
68
+
69
+ return false if noncommutative_operator?(operator)
46
70
 
47
- def comparison_operator?(node)
48
- RuboCop::AST::Node::COMPARISON_OPERATORS.include?(node.method_name)
71
+ lhs.literal? && !rhs.literal?
49
72
  end
50
73
 
51
- def register_offense(node)
52
- add_offense(node, :expression, format(MSG, node.source))
74
+ def message(node)
75
+ format(MSG, node.source)
53
76
  end
54
77
 
55
78
  def autocorrect(node)
@@ -59,8 +82,8 @@ module RuboCop
59
82
  end
60
83
 
61
84
  def corrected_code(node)
62
- first, operator, last = node.children
63
- "#{last.source} #{reverse_comparison(operator)} #{first.source}"
85
+ lhs, operator, rhs = *node
86
+ "#{rhs.source} #{reverse_comparison(operator)} #{lhs.source}"
64
87
  end
65
88
 
66
89
  def actual_code_range(node)
@@ -72,6 +95,18 @@ module RuboCop
72
95
  def reverse_comparison(operator)
73
96
  REVERSE_COMPARISON.fetch(operator.to_s, operator)
74
97
  end
98
+
99
+ def check_equality_only?
100
+ style == :equality_operators_only
101
+ end
102
+
103
+ def non_equality_operator?(operator)
104
+ !EQUALITY_OPERATORS.include?(operator)
105
+ end
106
+
107
+ def noncommutative_operator?(operator)
108
+ NONCOMMUTATIVE_OPERATORS.include?(operator)
109
+ end
75
110
  end
76
111
  end
77
112
  end
@@ -31,34 +31,41 @@ module RuboCop
31
31
  NONZERO_MSG = 'Use `!empty?` instead of `%s %s %s`.'.freeze
32
32
 
33
33
  def on_send(node)
34
+ check_zero_length_predicate(node)
35
+ check_nonzero_length_predicate(node)
36
+ end
37
+
38
+ private
39
+
40
+ def check_zero_length_predicate(node)
34
41
  zero_length_predicate = zero_length_predicate(node)
35
42
 
36
- if zero_length_predicate
37
- add_offense(node, :expression,
38
- format(ZERO_MSG, *zero_length_predicate))
39
- end
43
+ return unless zero_length_predicate
40
44
 
45
+ add_offense(node, :expression,
46
+ format(ZERO_MSG, *zero_length_predicate))
47
+ end
48
+
49
+ def check_nonzero_length_predicate(node)
41
50
  nonzero_length_predicate = nonzero_length_predicate(node)
42
51
 
43
- # rubocop:disable Style/GuardClause
44
- if nonzero_length_predicate
45
- add_offense(node, :expression,
46
- format(NONZERO_MSG, *nonzero_length_predicate))
47
- end
48
- # rubocop:enable Style/GuardClause
52
+ return unless nonzero_length_predicate
53
+
54
+ add_offense(node, :expression,
55
+ format(NONZERO_MSG, *nonzero_length_predicate))
49
56
  end
50
57
 
51
- def_node_matcher :zero_length_predicate, <<-END
58
+ def_node_matcher :zero_length_predicate, <<-PATTERN
52
59
  {(send (send (...) ${:length :size}) $:== (int $0))
53
60
  (send (int $0) $:== (send (...) ${:length :size}))
54
61
  (send (send (...) ${:length :size}) $:< (int $1))
55
62
  (send (int $1) $:> (send (...) ${:length :size}))}
56
- END
63
+ PATTERN
57
64
 
58
- def_node_matcher :nonzero_length_predicate, <<-END
65
+ def_node_matcher :nonzero_length_predicate, <<-PATTERN
59
66
  {(send (send (...) ${:length :size}) ${:> :!=} (int $0))
60
67
  (send (int $0) ${:< :!=} (send (...) ${:length :size}))}
61
- END
68
+ PATTERN
62
69
 
63
70
  def autocorrect(node)
64
71
  lambda do |corrector|
@@ -73,17 +80,17 @@ module RuboCop
73
80
  "!#{other_receiver(node).source}.empty?"
74
81
  end
75
82
 
76
- def_node_matcher :zero_length_receiver, <<-END
83
+ def_node_matcher :zero_length_receiver, <<-PATTERN
77
84
  {(send (send $_ _) :== (int 0))
78
85
  (send (int 0) :== (send $_ _))
79
86
  (send (send $_ _) :< (int 1))
80
87
  (send (int 1) :> (send $_ _))}
81
- END
88
+ PATTERN
82
89
 
83
- def_node_matcher :other_receiver, <<-END
90
+ def_node_matcher :other_receiver, <<-PATTERN
84
91
  {(send (send $_ _) _ _)
85
92
  (send _ _ (send $_ _))}
86
- END
93
+ PATTERN
87
94
  end
88
95
  end
89
96
  end
@@ -36,7 +36,9 @@ module RuboCop
36
36
  def inspect_file(processed_source)
37
37
  # If we got any syntax errors, return only the syntax offenses.
38
38
  unless processed_source.valid_syntax?
39
- return Lint::Syntax.offenses_from_processed_source(processed_source)
39
+ return Lint::Syntax.offenses_from_processed_source(
40
+ processed_source, @config, @options
41
+ )
40
42
  end
41
43
 
42
44
  offenses(processed_source)
@@ -117,14 +119,8 @@ module RuboCop
117
119
 
118
120
  def autocorrect_all_cops(buffer, cops)
119
121
  corrector = Corrector.new(buffer)
120
- skip = Set.new
121
122
 
122
- cops.each do |cop|
123
- next if cop.corrections.empty?
124
- next if skip.include?(cop.class)
125
- corrector.corrections.concat(cop.corrections)
126
- skip.merge(cop.class.autocorrect_incompatible_with)
127
- end
123
+ collate_corrections(corrector, cops)
128
124
 
129
125
  if !corrector.corrections.empty?
130
126
  corrector.rewrite
@@ -133,6 +129,18 @@ module RuboCop
133
129
  end
134
130
  end
135
131
 
132
+ def collate_corrections(corrector, cops)
133
+ skips = Set.new
134
+
135
+ cops.each do |cop|
136
+ next if cop.corrections.empty?
137
+ next if skips.include?(cop.class)
138
+
139
+ corrector.corrections.concat(cop.corrections)
140
+ skips.merge(cop.class.autocorrect_incompatible_with)
141
+ end
142
+ end
143
+
136
144
  def validate_config
137
145
  cops.each do |cop|
138
146
  cop.validate_config if cop.respond_to?(:validate_config)
@@ -304,6 +304,17 @@ module RuboCop
304
304
  .sub(/^Enforced/, 'Supported')
305
305
  .sub('Style', 'Styles')
306
306
  end
307
+
308
+ def scrub_string(string)
309
+ if string.respond_to?(:scrub)
310
+ string.scrub
311
+ else
312
+ string
313
+ .encode('UTF-16BE', 'UTF-8',
314
+ invalid: :replace, undef: :replace, replace: '?')
315
+ .encode('UTF-8')
316
+ end
317
+ end
307
318
  end
308
319
  end
309
320
  end
@@ -50,7 +50,7 @@ module RuboCop
50
50
  output.puts HEADING % command
51
51
 
52
52
  # Syntax isn't a real cop and it can't be disabled.
53
- @cops_with_offenses.delete('Syntax')
53
+ @cops_with_offenses.delete('Lint/Syntax')
54
54
 
55
55
  output_offenses
56
56
 
@@ -19,7 +19,8 @@ module RuboCop
19
19
  'files' => FileListFormatter,
20
20
  'offenses' => OffenseCountFormatter,
21
21
  'disabled' => DisabledLinesFormatter,
22
- 'worst' => WorstOffendersFormatter
22
+ 'worst' => WorstOffendersFormatter,
23
+ 'tap' => TapFormatter
23
24
  }.freeze
24
25
 
25
26
  FORMATTER_APIS = %i[started finished].freeze
@@ -27,6 +27,7 @@ module RuboCop
27
27
  report_summary(@offense_counts)
28
28
  end
29
29
 
30
+ # rubocop:disable Metrics/AbcSize
30
31
  def report_summary(offense_counts)
31
32
  per_cop_counts = ordered_offense_counts(offense_counts)
32
33
  total_count = total_offense_count(offense_counts)
@@ -42,6 +43,7 @@ module RuboCop
42
43
 
43
44
  output.puts
44
45
  end
46
+ # rubocop:enable Metrics/AbcSize
45
47
 
46
48
  def ordered_offense_counts(offense_counts)
47
49
  Hash[offense_counts.sort_by { |k, v| [-v, k] }]
@@ -70,7 +70,7 @@ module RuboCop
70
70
  end
71
71
 
72
72
  def annotate_message(msg)
73
- msg.gsub(/`(.*?)`/, yellow('\1'))
73
+ msg.gsub(/`(.*?)`/m, yellow('\1'))
74
74
  end
75
75
 
76
76
  def message(offense)
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Formatter
5
+ # This formatter formats report data using the Test Anything Protocol.
6
+ # TAP allows for to communicate tests results in a language agnostics way.
7
+ class TapFormatter < ClangStyleFormatter
8
+ def started(target_files)
9
+ super
10
+ @progress_count = 1
11
+ output.puts "1..#{target_files.size}"
12
+ end
13
+
14
+ def file_finished(file, offenses)
15
+ if offenses.empty?
16
+ output.puts "ok #{@progress_count} - #{smart_path(file)}"
17
+ else
18
+ output.puts "not ok #{@progress_count} - #{smart_path(file)}"
19
+
20
+ count_stats(offenses)
21
+ report_file(file, offenses)
22
+ end
23
+
24
+ @progress_count += 1
25
+ end
26
+
27
+ private
28
+
29
+ def report_line(location)
30
+ source_line = location.source_line
31
+
32
+ if location.first_line == location.last_line
33
+ output.puts("# #{source_line}")
34
+ else
35
+ output.puts("# #{source_line} #{yellow(ELLIPSES)}")
36
+ end
37
+ end
38
+
39
+ def report_highlighted_area(highlighted_area)
40
+ output.puts("# #{' ' * highlighted_area.begin_pos}" \
41
+ "#{'^' * highlighted_area.size}")
42
+ end
43
+
44
+ def report_offense(file, offense)
45
+ output.printf("# %s:%d:%d: %s: %s\n",
46
+ cyan(smart_path(file)), offense.line, offense.real_column,
47
+ colored_severity_code(offense), message(offense))
48
+
49
+ # rubocop:disable Lint/HandleExceptions
50
+ begin
51
+ return unless valid_line?(offense)
52
+
53
+ report_line(offense.location)
54
+ report_highlighted_area(offense.highlighted_area)
55
+ rescue IndexError
56
+ # range is not on a valid line; perhaps the source file is empty
57
+ end
58
+ # rubocop:enable Lint/HandleExceptions
59
+ end
60
+
61
+ def annotate_message(msg)
62
+ msg.gsub(/`(.*?)`/, '\1')
63
+ end
64
+
65
+ def message(offense)
66
+ message = offense.corrected? ? '[Corrected] ' : ''
67
+ "#{message}#{annotate_message(offense.message)}"
68
+ end
69
+ end
70
+ end
71
+ end
@@ -32,6 +32,7 @@ module RuboCop
32
32
  report_summary(@offense_counts)
33
33
  end
34
34
 
35
+ # rubocop:disable Metrics/AbcSize
35
36
  def report_summary(offense_counts)
36
37
  per_file_counts = ordered_offense_counts(offense_counts)
37
38
  total_count = total_offense_count(offense_counts)
@@ -47,6 +48,7 @@ module RuboCop
47
48
 
48
49
  output.puts
49
50
  end
51
+ # rubocop:enable Metrics/AbcSize
50
52
 
51
53
  def ordered_offense_counts(offense_counts)
52
54
  Hash[offense_counts.sort_by { |k, v| [-v, k] }]
@@ -98,19 +98,26 @@ module RuboCop
98
98
  # @private
99
99
  # Builds Ruby code which implements a pattern
100
100
  class Compiler
101
- RSYM = %r{:(?:[\w+@*/?!<>=~|%^-]+|\[\]=?)}
102
- ID_CHAR = /[a-zA-Z_-]/
103
- META = /\(|\)|\{|\}|\[|\]|\$\.\.\.|\$|!|\^|\.\.\./
104
- NUMBER = /-?\d+(?:\.\d+)?/
105
- TOKEN =
106
- /\G(?:[\s,]+|#{META}|%\d*|#{NUMBER}|\#?#{ID_CHAR}+[\!\?]?\(?|#{RSYM}|.)/
107
-
108
- NODE = /\A#{ID_CHAR}+\Z/
109
- PREDICATE = /\A#{ID_CHAR}+\?\(?\Z/
110
- WILDCARD = /\A_#{ID_CHAR}*\Z/
111
- FUNCALL = /\A\##{ID_CHAR}+[\!\?]?\(?\Z/
112
- LITERAL = /\A(?:#{RSYM}|#{NUMBER}|nil)\Z/
113
- PARAM = /\A%\d*\Z/
101
+ SYMBOL = %r{:(?:[\w+@*/?!<>=~|%^-]+|\[\]=?)}
102
+ IDENTIFIER = /[a-zA-Z_-]/
103
+ META = /\(|\)|\{|\}|\[|\]|\$\.\.\.|\$|!|\^|\.\.\./
104
+ NUMBER = /-?\d+(?:\.\d+)?/
105
+ STRING = /".+?"/
106
+ METHOD_NAME = /\#?#{IDENTIFIER}+[\!\?]?\(?/
107
+ PARAM_NUMBER = /%\d*/
108
+
109
+ SEPARATORS = /[\s]+/
110
+ TOKENS = Regexp.union(META, PARAM_NUMBER, NUMBER,
111
+ METHOD_NAME, SYMBOL, STRING)
112
+
113
+ TOKEN = /\G(?:#{SEPARATORS}|#{TOKENS}|.)/
114
+
115
+ NODE = /\A#{IDENTIFIER}+\Z/
116
+ PREDICATE = /\A#{IDENTIFIER}+\?\(?\Z/
117
+ WILDCARD = /\A_#{IDENTIFIER}*\Z/
118
+ FUNCALL = /\A\##{METHOD_NAME}/
119
+ LITERAL = /\A(?:#{SYMBOL}|#{NUMBER}|#{STRING}|nil)\Z/
120
+ PARAM = /\A#{PARAM_NUMBER}\Z/
114
121
  CLOSING = /\A(?:\)|\}|\])\Z/
115
122
 
116
123
  attr_reader :match_code
@@ -128,9 +135,11 @@ module RuboCop
128
135
  end
129
136
 
130
137
  def run(node_var)
131
- tokens = @string.scan(TOKEN)
132
- tokens.reject! { |token| token =~ /\A[\s,]+\Z/ } # drop whitespace
138
+ tokens =
139
+ @string.scan(TOKEN).reject { |token| token =~ /\A#{SEPARATORS}\Z/ }
140
+
133
141
  @match_code = compile_expr(tokens, node_var, false)
142
+
134
143
  fail_due_to('unbalanced pattern') unless tokens.empty?
135
144
  end
136
145
 
@@ -158,7 +167,7 @@ module RuboCop
158
167
  when PARAM then compile_param(cur_node, token[1..-1], seq_head)
159
168
  when CLOSING then fail_due_to("#{token} in invalid position")
160
169
  when nil then fail_due_to('pattern ended prematurely')
161
- else fail_due_to("invalid token #{token.inspect}")
170
+ else fail_due_to("invalid token #{token.inspect}")
162
171
  end
163
172
  end
164
173
  # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
@@ -336,7 +345,8 @@ module RuboCop
336
345
  else
337
346
  n = @unify[name] = next_temp_value
338
347
  # double assign to temp#{n} to avoid "assigned but unused variable"
339
- "(temp#{n} = temp#{n} = #{cur_node}#{'.type' if seq_head}; true)"
348
+ "(temp#{n} = #{cur_node}#{'.type' if seq_head}; " \
349
+ "temp#{n} = temp#{n}; true)"
340
350
  end
341
351
  end
342
352
 
@@ -433,11 +443,17 @@ module RuboCop
433
443
  params.empty? ? '' : ",#{params}"
434
444
  end
435
445
 
446
+ def emit_guard_clause
447
+ <<-RUBY
448
+ return unless node.is_a?(RuboCop::AST::Node)
449
+ RUBY
450
+ end
451
+
436
452
  def emit_method_code
437
- <<-CODE
438
- return nil unless #{@match_code}
453
+ <<-RUBY
454
+ return unless #{@match_code}
439
455
  block_given? ? yield(#{emit_capture_list}) : (return #{emit_retval})
440
- CODE
456
+ RUBY
441
457
  end
442
458
 
443
459
  def fail_due_to(message)
@@ -447,7 +463,7 @@ module RuboCop
447
463
  def with_temp_node(cur_node)
448
464
  with_temp_variable do |temp_var|
449
465
  # double assign to temp#{n} to avoid "assigned but unused variable"
450
- yield "#{temp_var} = #{temp_var} = #{cur_node}", temp_var
466
+ yield "#{temp_var} = #{cur_node}; #{temp_var} = #{temp_var}", temp_var
451
467
  end
452
468
  end
453
469
 
@@ -459,6 +475,7 @@ module RuboCop
459
475
  @temps += 1
460
476
  end
461
477
  end
478
+ private_constant :Compiler
462
479
 
463
480
  # Helpers for defining methods based on a pattern string
464
481
  module Macros
@@ -470,9 +487,10 @@ module RuboCop
470
487
  # If the node matches, and no block is provided, the new method will
471
488
  # return the captures, or `true` if there were none.
472
489
  def def_node_matcher(method_name, pattern_str)
473
- compiler = RuboCop::NodePattern::Compiler.new(pattern_str, 'node')
474
- src = "def #{method_name}(node" \
490
+ compiler = Compiler.new(pattern_str, 'node')
491
+ src = "def #{method_name}(node = self" \
475
492
  "#{compiler.emit_trailing_params});" \
493
+ "#{compiler.emit_guard_clause}" \
476
494
  "#{compiler.emit_method_code};end"
477
495
 
478
496
  location = caller_locations(1, 1).first
@@ -486,7 +504,7 @@ module RuboCop
486
504
  # as soon as it finds a descendant which matches. Otherwise, it will
487
505
  # yield all descendants which match.
488
506
  def def_node_search(method_name, pattern_str)
489
- compiler = RuboCop::NodePattern::Compiler.new(pattern_str, 'node')
507
+ compiler = Compiler.new(pattern_str, 'node')
490
508
  called_from = caller(1..1).first.split(':')
491
509
 
492
510
  if method_name.to_s.end_with?('?')
@@ -519,7 +537,7 @@ module RuboCop
519
537
 
520
538
  def node_search_body(method_name, trailing_params, prelude, match_code,
521
539
  on_match)
522
- <<-END
540
+ <<-RUBY
523
541
  def #{method_name}(node0#{trailing_params})
524
542
  #{prelude}
525
543
  node0.each_node do |node|
@@ -529,7 +547,7 @@ module RuboCop
529
547
  end
530
548
  nil
531
549
  end
532
- END
550
+ RUBY
533
551
  end
534
552
  end
535
553