rubocop 0.83.0 → 0.87.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (312) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +26 -18
  3. data/config/default.yml +128 -23
  4. data/lib/rubocop.rb +21 -59
  5. data/lib/rubocop/ast_aliases.rb +8 -0
  6. data/lib/rubocop/cli.rb +2 -4
  7. data/lib/rubocop/cli/command/auto_genenerate_config.rb +42 -7
  8. data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
  9. data/lib/rubocop/cli/command/show_cops.rb +2 -6
  10. data/lib/rubocop/comment_config.rb +1 -1
  11. data/lib/rubocop/config.rb +1 -1
  12. data/lib/rubocop/config_loader.rb +24 -70
  13. data/lib/rubocop/config_loader_resolver.rb +21 -9
  14. data/lib/rubocop/config_obsoletion.rb +0 -1
  15. data/lib/rubocop/config_store.rb +12 -2
  16. data/lib/rubocop/cop/autocorrect_logic.rb +14 -25
  17. data/lib/rubocop/cop/base.rb +399 -0
  18. data/lib/rubocop/cop/bundler/gem_comment.rb +70 -1
  19. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +10 -20
  20. data/lib/rubocop/cop/commissioner.rb +48 -71
  21. data/lib/rubocop/cop/cop.rb +91 -227
  22. data/lib/rubocop/cop/corrector.rb +38 -115
  23. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -6
  24. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +1 -1
  25. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -3
  26. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -3
  27. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
  28. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  29. data/lib/rubocop/cop/generator.rb +2 -2
  30. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  31. data/lib/rubocop/cop/ignored_node.rb +1 -3
  32. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +11 -14
  33. data/lib/rubocop/cop/layout/case_indentation.rb +21 -22
  34. data/lib/rubocop/cop/layout/class_structure.rb +19 -16
  35. data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
  36. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  37. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -6
  38. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +57 -4
  39. data/lib/rubocop/cop/layout/end_of_line.rb +3 -3
  40. data/lib/rubocop/cop/layout/first_argument_indentation.rb +6 -4
  41. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +1 -3
  42. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
  43. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +1 -3
  44. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
  45. data/lib/rubocop/cop/layout/hash_alignment.rb +8 -9
  46. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +3 -7
  47. data/lib/rubocop/cop/layout/heredoc_indentation.rb +20 -103
  48. data/lib/rubocop/cop/layout/indentation_width.rb +1 -3
  49. data/lib/rubocop/cop/layout/line_length.rb +17 -17
  50. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -2
  51. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -3
  52. data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
  53. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +19 -25
  54. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  55. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -3
  56. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  57. data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
  58. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -3
  59. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +2 -4
  60. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  61. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
  62. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -3
  63. data/lib/rubocop/cop/legacy/corrections_proxy.rb +49 -0
  64. data/lib/rubocop/cop/legacy/corrector.rb +29 -0
  65. data/lib/rubocop/cop/lint/ambiguous_operator.rb +5 -2
  66. data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
  67. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +137 -0
  68. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  69. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -3
  70. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  71. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +38 -2
  72. data/lib/rubocop/cop/lint/interpolation_check.rb +13 -0
  73. data/lib/rubocop/cop/lint/loop.rb +1 -1
  74. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +69 -0
  75. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
  76. data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
  77. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
  78. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +7 -7
  79. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +20 -5
  80. data/lib/rubocop/cop/lint/percent_string_array.rb +2 -4
  81. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  82. data/lib/rubocop/cop/lint/raise_exception.rb +12 -4
  83. data/lib/rubocop/cop/lint/rand_one.rb +1 -1
  84. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +31 -25
  85. data/lib/rubocop/cop/lint/redundant_require_statement.rb +3 -3
  86. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +2 -2
  87. data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
  88. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  89. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +9 -1
  90. data/lib/rubocop/cop/lint/suppressed_exception.rb +15 -2
  91. data/lib/rubocop/cop/lint/syntax.rb +11 -28
  92. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  93. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -1
  94. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -4
  95. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +1 -1
  96. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  97. data/lib/rubocop/cop/metrics/block_length.rb +22 -0
  98. data/lib/rubocop/cop/metrics/class_length.rb +25 -2
  99. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
  100. data/lib/rubocop/cop/metrics/method_length.rb +23 -0
  101. data/lib/rubocop/cop/metrics/module_length.rb +25 -2
  102. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  103. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +129 -0
  104. data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
  105. data/lib/rubocop/cop/migration/department_name.rb +7 -7
  106. data/lib/rubocop/cop/mixin/alignment.rb +1 -3
  107. data/lib/rubocop/cop/mixin/allowed_methods.rb +19 -0
  108. data/lib/rubocop/cop/mixin/array_min_size.rb +1 -3
  109. data/lib/rubocop/cop/mixin/auto_corrector.rb +12 -0
  110. data/lib/rubocop/cop/mixin/check_line_breakable.rb +3 -9
  111. data/lib/rubocop/cop/mixin/code_length.rb +4 -0
  112. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -3
  113. data/lib/rubocop/cop/mixin/configurable_formatting.rb +3 -5
  114. data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
  115. data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
  116. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  117. data/lib/rubocop/cop/mixin/enforce_superclass.rb +3 -1
  118. data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
  119. data/lib/rubocop/cop/mixin/ignored_pattern.rb +1 -1
  120. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
  121. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  122. data/lib/rubocop/cop/mixin/nil_methods.rb +3 -5
  123. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +6 -1
  124. data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
  125. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  126. data/lib/rubocop/cop/mixin/range_help.rb +1 -1
  127. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +43 -0
  128. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  129. data/lib/rubocop/cop/mixin/surrounding_space.rb +10 -5
  130. data/lib/rubocop/cop/mixin/too_many_lines.rb +3 -13
  131. data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -4
  132. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +7 -5
  133. data/lib/rubocop/cop/naming/ascii_identifiers.rb +27 -4
  134. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +3 -3
  135. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +11 -1
  136. data/lib/rubocop/cop/naming/file_name.rb +28 -17
  137. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  138. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  139. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  140. data/lib/rubocop/cop/naming/predicate_name.rb +4 -6
  141. data/lib/rubocop/cop/naming/variable_name.rb +1 -1
  142. data/lib/rubocop/cop/naming/variable_number.rb +1 -1
  143. data/lib/rubocop/cop/offense.rb +16 -2
  144. data/lib/rubocop/cop/registry.rb +63 -10
  145. data/lib/rubocop/cop/severity.rb +1 -3
  146. data/lib/rubocop/cop/style/accessor_grouping.rb +136 -0
  147. data/lib/rubocop/cop/style/and_or.rb +2 -2
  148. data/lib/rubocop/cop/style/array_join.rb +1 -1
  149. data/lib/rubocop/cop/style/attr.rb +1 -3
  150. data/lib/rubocop/cop/style/bare_percent_literals.rb +2 -2
  151. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +121 -0
  152. data/lib/rubocop/cop/style/block_delimiters.rb +4 -12
  153. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  154. data/lib/rubocop/cop/style/class_vars.rb +21 -0
  155. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  156. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  157. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -4
  158. data/lib/rubocop/cop/style/copyright.rb +5 -5
  159. data/lib/rubocop/cop/style/date_time.rb +1 -1
  160. data/lib/rubocop/cop/style/dir.rb +2 -2
  161. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
  162. data/lib/rubocop/cop/style/documentation.rb +2 -2
  163. data/lib/rubocop/cop/style/double_negation.rb +41 -4
  164. data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
  165. data/lib/rubocop/cop/style/empty_literal.rb +6 -8
  166. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  167. data/lib/rubocop/cop/style/encoding.rb +1 -1
  168. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
  169. data/lib/rubocop/cop/style/exponential_notation.rb +5 -5
  170. data/lib/rubocop/cop/style/format_string_token.rb +2 -3
  171. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -6
  172. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -1
  173. data/lib/rubocop/cop/style/hash_syntax.rb +16 -7
  174. data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
  175. data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
  176. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -0
  177. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  178. data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
  179. data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
  180. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +1 -3
  181. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +1 -3
  182. data/lib/rubocop/cop/style/multiline_block_chain.rb +10 -1
  183. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  184. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  185. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
  186. data/lib/rubocop/cop/style/mutable_constant.rb +4 -4
  187. data/lib/rubocop/cop/style/negated_if.rb +3 -3
  188. data/lib/rubocop/cop/style/negated_unless.rb +3 -3
  189. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -5
  190. data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
  191. data/lib/rubocop/cop/style/next.rb +2 -2
  192. data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
  193. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
  194. data/lib/rubocop/cop/style/one_line_conditional.rb +2 -6
  195. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  196. data/lib/rubocop/cop/style/proc.rb +1 -1
  197. data/lib/rubocop/cop/style/random_with_offset.rb +4 -10
  198. data/lib/rubocop/cop/style/redundant_assignment.rb +117 -0
  199. data/lib/rubocop/cop/style/redundant_conditional.rb +4 -3
  200. data/lib/rubocop/cop/style/redundant_exception.rb +14 -10
  201. data/lib/rubocop/cop/style/redundant_fetch_block.rb +122 -0
  202. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  203. data/lib/rubocop/cop/style/redundant_parentheses.rb +10 -8
  204. data/lib/rubocop/cop/style/redundant_percent_q.rb +3 -3
  205. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +90 -0
  206. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +121 -0
  207. data/lib/rubocop/cop/style/redundant_self.rb +6 -9
  208. data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -1
  209. data/lib/rubocop/cop/style/safe_navigation.rb +2 -6
  210. data/lib/rubocop/cop/style/sample.rb +1 -1
  211. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  212. data/lib/rubocop/cop/style/signal_exception.rb +1 -1
  213. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -1
  214. data/lib/rubocop/cop/style/special_global_vars.rb +2 -6
  215. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  216. data/lib/rubocop/cop/style/struct_inheritance.rb +23 -2
  217. data/lib/rubocop/cop/style/symbol_array.rb +5 -5
  218. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  219. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -4
  220. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +3 -3
  221. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +3 -3
  222. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +13 -13
  223. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +3 -3
  224. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -3
  225. data/lib/rubocop/cop/style/trivial_accessors.rb +8 -7
  226. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  227. data/lib/rubocop/cop/style/when_then.rb +1 -1
  228. data/lib/rubocop/cop/style/word_array.rb +1 -1
  229. data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
  230. data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -2
  231. data/lib/rubocop/cop/team.rb +141 -81
  232. data/lib/rubocop/cop/util.rb +3 -3
  233. data/lib/rubocop/cop/utils/format_string.rb +19 -2
  234. data/lib/rubocop/cop/variable_force.rb +3 -9
  235. data/lib/rubocop/cop/variable_force/branch.rb +1 -3
  236. data/lib/rubocop/cop/variable_force/variable.rb +2 -6
  237. data/lib/rubocop/ext/processed_source.rb +18 -0
  238. data/lib/rubocop/formatter/base_formatter.rb +0 -4
  239. data/lib/rubocop/formatter/disabled_config_formatter.rb +5 -13
  240. data/lib/rubocop/formatter/formatter_set.rb +2 -4
  241. data/lib/rubocop/formatter/junit_formatter.rb +14 -4
  242. data/lib/rubocop/magic_comment.rb +1 -1
  243. data/lib/rubocop/name_similarity.rb +7 -3
  244. data/lib/rubocop/options.rb +30 -15
  245. data/lib/rubocop/path_util.rb +2 -2
  246. data/lib/rubocop/platform.rb +1 -1
  247. data/lib/rubocop/rake_task.rb +6 -9
  248. data/lib/rubocop/remote_config.rb +1 -3
  249. data/lib/rubocop/result_cache.rb +5 -7
  250. data/lib/rubocop/rspec/cop_helper.rb +5 -28
  251. data/lib/rubocop/rspec/expect_offense.rb +60 -23
  252. data/lib/rubocop/rspec/shared_contexts.rb +55 -17
  253. data/lib/rubocop/runner.rb +42 -38
  254. data/lib/rubocop/target_finder.rb +2 -4
  255. data/lib/rubocop/target_ruby.rb +5 -2
  256. data/lib/rubocop/version.rb +5 -3
  257. metadata +60 -62
  258. data/lib/rubocop/ast/builder.rb +0 -85
  259. data/lib/rubocop/ast/node.rb +0 -637
  260. data/lib/rubocop/ast/node/alias_node.rb +0 -24
  261. data/lib/rubocop/ast/node/and_node.rb +0 -29
  262. data/lib/rubocop/ast/node/args_node.rb +0 -29
  263. data/lib/rubocop/ast/node/array_node.rb +0 -70
  264. data/lib/rubocop/ast/node/block_node.rb +0 -121
  265. data/lib/rubocop/ast/node/break_node.rb +0 -17
  266. data/lib/rubocop/ast/node/case_match_node.rb +0 -56
  267. data/lib/rubocop/ast/node/case_node.rb +0 -56
  268. data/lib/rubocop/ast/node/class_node.rb +0 -31
  269. data/lib/rubocop/ast/node/def_node.rb +0 -82
  270. data/lib/rubocop/ast/node/defined_node.rb +0 -17
  271. data/lib/rubocop/ast/node/ensure_node.rb +0 -17
  272. data/lib/rubocop/ast/node/float_node.rb +0 -12
  273. data/lib/rubocop/ast/node/for_node.rb +0 -53
  274. data/lib/rubocop/ast/node/forward_args_node.rb +0 -18
  275. data/lib/rubocop/ast/node/hash_node.rb +0 -109
  276. data/lib/rubocop/ast/node/if_node.rb +0 -175
  277. data/lib/rubocop/ast/node/int_node.rb +0 -12
  278. data/lib/rubocop/ast/node/keyword_splat_node.rb +0 -45
  279. data/lib/rubocop/ast/node/mixin/basic_literal_node.rb +0 -16
  280. data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +0 -43
  281. data/lib/rubocop/ast/node/mixin/collection_node.rb +0 -15
  282. data/lib/rubocop/ast/node/mixin/conditional_node.rb +0 -45
  283. data/lib/rubocop/ast/node/mixin/hash_element_node.rb +0 -125
  284. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +0 -269
  285. data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +0 -114
  286. data/lib/rubocop/ast/node/mixin/modifier_node.rb +0 -17
  287. data/lib/rubocop/ast/node/mixin/numeric_node.rb +0 -21
  288. data/lib/rubocop/ast/node/mixin/parameterized_node.rb +0 -61
  289. data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +0 -35
  290. data/lib/rubocop/ast/node/module_node.rb +0 -24
  291. data/lib/rubocop/ast/node/or_node.rb +0 -29
  292. data/lib/rubocop/ast/node/pair_node.rb +0 -63
  293. data/lib/rubocop/ast/node/range_node.rb +0 -18
  294. data/lib/rubocop/ast/node/regexp_node.rb +0 -33
  295. data/lib/rubocop/ast/node/resbody_node.rb +0 -24
  296. data/lib/rubocop/ast/node/retry_node.rb +0 -17
  297. data/lib/rubocop/ast/node/return_node.rb +0 -24
  298. data/lib/rubocop/ast/node/self_class_node.rb +0 -24
  299. data/lib/rubocop/ast/node/send_node.rb +0 -17
  300. data/lib/rubocop/ast/node/str_node.rb +0 -16
  301. data/lib/rubocop/ast/node/super_node.rb +0 -21
  302. data/lib/rubocop/ast/node/symbol_node.rb +0 -12
  303. data/lib/rubocop/ast/node/until_node.rb +0 -35
  304. data/lib/rubocop/ast/node/when_node.rb +0 -53
  305. data/lib/rubocop/ast/node/while_node.rb +0 -35
  306. data/lib/rubocop/ast/node/yield_node.rb +0 -21
  307. data/lib/rubocop/ast/sexp.rb +0 -16
  308. data/lib/rubocop/ast/traversal.rb +0 -202
  309. data/lib/rubocop/cop/mixin/classish_length.rb +0 -37
  310. data/lib/rubocop/node_pattern.rb +0 -887
  311. data/lib/rubocop/processed_source.rb +0 -213
  312. data/lib/rubocop/token.rb +0 -114
@@ -71,9 +71,7 @@ module RuboCop
71
71
  end
72
72
  end
73
73
 
74
- if disabled_by_default
75
- config = handle_disabled_by_default(config, default_configuration)
76
- end
74
+ config = handle_disabled_by_default(config, default_configuration) if disabled_by_default
77
75
 
78
76
  opts = { inherit_mode: config['inherit_mode'] || {},
79
77
  unset_nil: unset_nil }
@@ -117,9 +115,7 @@ module RuboCop
117
115
  # internal setting that's not documented in the manual. It will cause a
118
116
  # cop to be enabled later, when logic surrounding enabled/disabled it
119
117
  # run, even though its department is disabled.
120
- if derived_hash[key]['Enabled']
121
- derived_hash[key]['Enabled'] = 'override_department'
122
- end
118
+ derived_hash[key]['Enabled'] = 'override_department' if derived_hash[key]['Enabled']
123
119
  end
124
120
  end
125
121
 
@@ -175,10 +171,20 @@ module RuboCop
175
171
 
176
172
  def inherited_file(path, inherit_from, file)
177
173
  if remote_file?(inherit_from)
174
+ # A remote configuration, e.g. `inherit_from: http://example.com/rubocop.yml`.
178
175
  RemoteConfig.new(inherit_from, File.dirname(path))
176
+ elsif Pathname.new(inherit_from).absolute?
177
+ # An absolute path to a config, e.g. `inherit_from: /Users/me/rubocop.yml`.
178
+ # The path may come from `inherit_gem` option, where a gem name is expanded
179
+ # to an absolute path to that gem.
180
+ print 'Inheriting ' if ConfigLoader.debug?
181
+ inherit_from
179
182
  elsif file.is_a?(RemoteConfig)
183
+ # A path relative to a URL, e.g. `inherit_from: configs/default.yml`
184
+ # in a config included with `inherit_from: http://example.com/rubocop.yml`
180
185
  file.inherit_from_remote(inherit_from, path)
181
186
  else
187
+ # A local relative path, e.g. `inherit_from: default.yml`
182
188
  print 'Inheriting ' if ConfigLoader.debug?
183
189
  File.expand_path(inherit_from, File.dirname(path))
184
190
  end
@@ -186,7 +192,7 @@ module RuboCop
186
192
 
187
193
  def remote_file?(uri)
188
194
  regex = URI::DEFAULT_PARSER.make_regexp(%w[http https])
189
- uri =~ /\A#{regex}\z/
195
+ /\A#{regex}\z/.match?(uri)
190
196
  end
191
197
 
192
198
  def handle_disabled_by_default(config, new_default_configuration)
@@ -212,8 +218,14 @@ module RuboCop
212
218
  end
213
219
 
214
220
  def gem_config_path(gem_name, relative_config_path)
215
- spec = Gem::Specification.find_by_name(gem_name)
216
- File.join(spec.gem_dir, relative_config_path)
221
+ if defined?(Bundler)
222
+ gem = Bundler.load.specs[gem_name].first
223
+ gem_path = gem.full_gem_path if gem
224
+ end
225
+
226
+ gem_path ||= Gem::Specification.find_by_name(gem_name).gem_dir
227
+
228
+ File.join(gem_path, relative_config_path)
217
229
  rescue Gem::LoadError => e
218
230
  raise Gem::LoadError,
219
231
  "Unable to find gem #{gem_name}; is the gem installed? #{e}"
@@ -63,7 +63,6 @@ module RuboCop
63
63
  'Layout/SpaceAfterControlKeyword' => 'Layout/SpaceAroundKeyword',
64
64
  'Layout/SpaceBeforeModifierKeyword' => 'Layout/SpaceAroundKeyword',
65
65
  'Lint/RescueWithoutErrorClass' => 'Style/RescueStandardError',
66
- 'Rails/DefaultScope' => nil,
67
66
  'Style/SpaceAfterControlKeyword' => 'Layout/SpaceAroundKeyword',
68
67
  'Style/SpaceBeforeModifierKeyword' => 'Layout/SpaceAroundKeyword',
69
68
  'Style/TrailingComma' => 'Style/TrailingCommaInArguments, ' \
@@ -29,14 +29,24 @@ module RuboCop
29
29
  @options_config = ConfigLoader.default_configuration
30
30
  end
31
31
 
32
- def for(file_or_dir)
33
- return @options_config if @options_config
32
+ def for_file(file)
33
+ for_dir(File.dirname(file))
34
+ end
34
35
 
36
+ # If type (file/dir) is known beforehand,
37
+ # prefer using #for_file or #for_dir for improved performance
38
+ def for(file_or_dir)
35
39
  dir = if File.directory?(file_or_dir)
36
40
  file_or_dir
37
41
  else
38
42
  File.dirname(file_or_dir)
39
43
  end
44
+ for_dir(dir)
45
+ end
46
+
47
+ def for_dir(dir)
48
+ return @options_config if @options_config
49
+
40
50
  @path_cache[dir] ||= ConfigLoader.configuration_file_for(dir)
41
51
  path = @path_cache[dir]
42
52
  @object_cache[path] ||= begin
@@ -13,11 +13,7 @@ module RuboCop
13
13
  end
14
14
 
15
15
  def correctable?
16
- support_autocorrect? || disable_uncorrectable?
17
- end
18
-
19
- def support_autocorrect?
20
- respond_to?(:autocorrect)
16
+ self.class.support_autocorrect? || disable_uncorrectable?
21
17
  end
22
18
 
23
19
  def disable_uncorrectable?
@@ -40,11 +36,11 @@ module RuboCop
40
36
  true
41
37
  end
42
38
 
43
- def disable_offense(node)
44
- range = node.location.expression
39
+ private
40
+
41
+ def disable_offense(range)
45
42
  eol_comment = " # rubocop:todo #{cop_name}"
46
- needed_line_length = range.column +
47
- (range.source_line + eol_comment).length
43
+ needed_line_length = (range.source_line + eol_comment).length
48
44
  if needed_line_length <= max_line_length
49
45
  disable_offense_at_end_of_line(range_of_first_line(range),
50
46
  eol_comment)
@@ -53,8 +49,6 @@ module RuboCop
53
49
  end
54
50
  end
55
51
 
56
- private
57
-
58
52
  def range_of_first_line(range)
59
53
  begin_of_first_line = range.begin_pos - range.column
60
54
  end_of_first_line = begin_of_first_line + range.source_line.length
@@ -83,23 +77,18 @@ module RuboCop
83
77
  end
84
78
 
85
79
  def disable_offense_at_end_of_line(range, eol_comment)
86
- ->(corrector) { corrector.insert_after(range, eol_comment) }
80
+ Corrector.new(range).insert_after(range, eol_comment)
87
81
  end
88
82
 
89
83
  def disable_offense_before_and_after(range_by_lines)
90
- lambda do |corrector|
91
- range_with_newline = range_by_lines.resize(range_by_lines.size + 1)
92
- leading_whitespace = range_by_lines.source_line[/^\s*/]
93
-
94
- corrector.insert_before(
95
- range_with_newline,
96
- "#{leading_whitespace}# rubocop:todo #{cop_name}\n"
97
- )
98
- corrector.insert_after(
99
- range_with_newline,
100
- "#{leading_whitespace}# rubocop:enable #{cop_name}\n"
101
- )
102
- end
84
+ range_with_newline = range_by_lines.resize(range_by_lines.size + 1)
85
+ leading_whitespace = range_by_lines.source_line[/^\s*/]
86
+
87
+ Corrector.new(range_by_lines).wrap(
88
+ range_with_newline,
89
+ "#{leading_whitespace}# rubocop:todo #{cop_name}\n",
90
+ "#{leading_whitespace}# rubocop:enable #{cop_name}\n"
91
+ )
103
92
  end
104
93
  end
105
94
  end
@@ -0,0 +1,399 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # A scaffold for concrete cops.
6
+ #
7
+ # The Cop::Base class is meant to be extended.
8
+ #
9
+ # Cops track offenses and can autocorrect them on the fly.
10
+ #
11
+ # A commissioner object is responsible for traversing the AST and invoking
12
+ # the specific callbacks on each cop.
13
+ #
14
+ # First the callback `on_new_investigation` is called;
15
+ # if a cop needs to do its own processing of the AST or depends on
16
+ # something else.
17
+ #
18
+ # Then callbacks like `on_def`, `on_send` (see AST::Traversal) are called
19
+ # with their respective nodes.
20
+ #
21
+ # Finally the callback `on_investigation_end` is called.
22
+ #
23
+ # Within these callbacks, cops are meant to call `add_offense` or
24
+ # `add_global_offense`. Use the `processed_source` method to
25
+ # get the currently processed source being investigated.
26
+ #
27
+ # In case of invalid syntax / unparseable content,
28
+ # the callback `on_other_file` is called instead of all the other
29
+ # `on_...` callbacks.
30
+ #
31
+ # Private methods are not meant for custom cops consumption,
32
+ # nor are any instance variables.
33
+ #
34
+ class Base # rubocop:disable Metrics/ClassLength
35
+ extend RuboCop::AST::Sexp
36
+ extend NodePattern::Macros
37
+ include RuboCop::AST::Sexp
38
+ include Util
39
+ include IgnoredNode
40
+ include AutocorrectLogic
41
+
42
+ attr_reader :config, :processed_source
43
+
44
+ # Reports of an investigation.
45
+ # Immutable
46
+ # Consider creation API private
47
+ InvestigationReport = Struct.new(:cop, :processed_source, :offenses, :corrector)
48
+
49
+ # List of cops that should not try to autocorrect at the same
50
+ # time as this cop
51
+ #
52
+ # @return [Array<RuboCop::Cop::Cop>]
53
+ #
54
+ # @api public
55
+ def self.autocorrect_incompatible_with
56
+ []
57
+ end
58
+
59
+ def initialize(config = nil, options = nil)
60
+ @config = config || Config.new
61
+ @options = options || { debug: false }
62
+ reset_investigation
63
+ end
64
+
65
+ # Called before all on_... have been called
66
+ # When refining this method, always call `super`
67
+ def on_new_investigation
68
+ # Typically do nothing here
69
+ end
70
+
71
+ # Called after all on_... have been called
72
+ # When refining this method, always call `super`
73
+ def on_investigation_end
74
+ # Typically do nothing here
75
+ end
76
+
77
+ # Called instead of all on_... callbacks for unrecognized files / syntax errors
78
+ # When refining this method, always call `super`
79
+ def on_other_file
80
+ # Typically do nothing here
81
+ end
82
+
83
+ # Override and return the Force class(es) you need to join
84
+ def self.joining_forces; end
85
+
86
+ # Gets called if no message is specified when calling `add_offense` or
87
+ # `add_global_offense`
88
+ # Cops are discouraged to override this; instead pass your message directly
89
+ def message(_range = nil)
90
+ self.class::MSG
91
+ end
92
+
93
+ # Adds an offense that has no particular location.
94
+ # No correction can be applied to global offenses
95
+ def add_global_offense(message = nil, severity: nil)
96
+ severity = find_severity(nil, severity)
97
+ message = find_message(nil, message)
98
+ @current_offenses <<
99
+ Offense.new(severity, Offense::NO_LOCATION, message, name, :unsupported)
100
+ end
101
+
102
+ # Adds an offense on the specified range (or node with an expression)
103
+ # Unless that offense is disabled for this range, a corrector will be yielded
104
+ # to provide the cop the opportunity to autocorrect the offense.
105
+ # If message is not specified, the method `message` will be called.
106
+ def add_offense(node_or_range, message: nil, severity: nil, &block)
107
+ range = range_from_node_or_range(node_or_range)
108
+ return unless @current_offense_locations.add?(range)
109
+
110
+ range_to_pass = callback_argument(range)
111
+
112
+ severity = find_severity(range_to_pass, severity)
113
+ message = find_message(range_to_pass, message)
114
+
115
+ status, corrector = enabled_line?(range.line) ? correct(range, &block) : :disabled
116
+
117
+ @current_offenses << Offense.new(severity, range, message, name, status, corrector)
118
+ end
119
+
120
+ # This method should be overridden when a cop's behavior depends
121
+ # on state that lives outside of these locations:
122
+ #
123
+ # (1) the file under inspection
124
+ # (2) the cop's source code
125
+ # (3) the config (eg a .rubocop.yml file)
126
+ #
127
+ # For example, some cops may want to look at other parts of
128
+ # the codebase being inspected to find violations. A cop may
129
+ # use the presence or absence of file `foo.rb` to determine
130
+ # whether a certain violation exists in `bar.rb`.
131
+ #
132
+ # Overriding this method allows the cop to indicate to RuboCop's
133
+ # ResultCache system when those external dependencies change,
134
+ # ie when the ResultCache should be invalidated.
135
+ def external_dependency_checksum
136
+ nil
137
+ end
138
+
139
+ def self.inherited(subclass)
140
+ Registry.global.enlist(subclass)
141
+ end
142
+
143
+ # Call for abstract Cop classes
144
+ def self.exclude_from_registry
145
+ Registry.global.dismiss(self)
146
+ end
147
+
148
+ # Returns if class supports auto_correct.
149
+ # It is recommended to extend AutoCorrector instead of overriding
150
+ def self.support_autocorrect?
151
+ false
152
+ end
153
+
154
+ ### Naming
155
+
156
+ def self.badge
157
+ @badge ||= Badge.for(name)
158
+ end
159
+
160
+ def self.cop_name
161
+ badge.to_s
162
+ end
163
+
164
+ def self.department
165
+ badge.department
166
+ end
167
+
168
+ def self.lint?
169
+ department == :Lint
170
+ end
171
+
172
+ # Returns true if the cop name or the cop namespace matches any of the
173
+ # given names.
174
+ def self.match?(given_names)
175
+ return false unless given_names
176
+
177
+ given_names.include?(cop_name) ||
178
+ given_names.include?(department.to_s)
179
+ end
180
+
181
+ def cop_name
182
+ @cop_name ||= self.class.cop_name
183
+ end
184
+
185
+ alias name cop_name
186
+
187
+ ### Configuration Helpers
188
+
189
+ def cop_config
190
+ # Use department configuration as basis, but let individual cop
191
+ # configuration override.
192
+ @cop_config ||= @config.for_cop(self.class.department.to_s)
193
+ .merge(@config.for_cop(self))
194
+ end
195
+
196
+ def config_to_allow_offenses
197
+ Formatter::DisabledConfigFormatter
198
+ .config_to_allow_offenses[cop_name] ||= {}
199
+ end
200
+
201
+ def config_to_allow_offenses=(hash)
202
+ Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] =
203
+ hash
204
+ end
205
+
206
+ def target_ruby_version
207
+ @config.target_ruby_version
208
+ end
209
+
210
+ def target_rails_version
211
+ @config.target_rails_version
212
+ end
213
+
214
+ def relevant_file?(file)
215
+ file == RuboCop::AST::ProcessedSource::STRING_SOURCE_NAME ||
216
+ file_name_matches_any?(file, 'Include', true) &&
217
+ !file_name_matches_any?(file, 'Exclude', false)
218
+ end
219
+
220
+ def excluded_file?(file)
221
+ !relevant_file?(file)
222
+ end
223
+
224
+ ### Persistence
225
+
226
+ # Override if your cop should be called repeatedly for multiple investigations
227
+ # Between calls to `on_new_investigation` and `on_investigation_end`,
228
+ # the result of `processed_source` will remain constant.
229
+ # You should invalidate any caches that depend on the current `processed_source`
230
+ # in the `on_new_investigation` callback.
231
+ # If your cop does autocorrections, be aware that your instance may be called
232
+ # multiple times with the same `processed_source.path` but different content.
233
+ def self.support_multiple_source?
234
+ false
235
+ end
236
+
237
+ # @api private
238
+ # Called between investigations
239
+ def ready
240
+ return self if self.class.support_multiple_source?
241
+
242
+ self.class.new(@config, @options)
243
+ end
244
+
245
+ ### Reserved for Cop::Cop
246
+
247
+ # @deprecated Make potential errors with previous API more obvious
248
+ def offenses
249
+ raise 'The offenses are not directly available; ' \
250
+ 'they are returned as the result of the investigation'
251
+ end
252
+
253
+ private
254
+
255
+ ### Reserved for Cop::Cop
256
+
257
+ def callback_argument(range)
258
+ range
259
+ end
260
+
261
+ def apply_correction(corrector)
262
+ @current_corrector&.merge!(corrector) if corrector
263
+ end
264
+
265
+ def correction_strategy
266
+ return :unsupported unless correctable?
267
+ return :uncorrected unless autocorrect?
268
+
269
+ :attempt_correction
270
+ end
271
+
272
+ ### Reserved for Commissioner:
273
+
274
+ # Called before any investigation
275
+ def begin_investigation(processed_source)
276
+ @current_offenses = []
277
+ @current_offense_locations = Set[]
278
+ @currently_disabled_lines = Set[]
279
+ @processed_source = processed_source
280
+ @current_corrector = Corrector.new(@processed_source) if @processed_source.valid_syntax?
281
+ end
282
+
283
+ # Called to complete an investigation
284
+ def complete_investigation
285
+ InvestigationReport.new(self, processed_source, @current_offenses, @current_corrector)
286
+ ensure
287
+ reset_investigation
288
+ end
289
+
290
+ ### Actually private methods
291
+
292
+ def reset_investigation
293
+ @currently_disabled_lines = @current_offenses = @processed_source = @current_corrector = nil
294
+ end
295
+
296
+ # @return [Symbol, Corrector] offense status
297
+ def correct(range)
298
+ status = correction_strategy
299
+
300
+ if block_given?
301
+ corrector = Corrector.new(self)
302
+ yield corrector
303
+ if !corrector.empty? && !self.class.support_autocorrect?
304
+ raise "The Cop #{name} must `extend AutoCorrector` to be able to autocorrect"
305
+ end
306
+ end
307
+
308
+ status = attempt_correction(range, corrector) if status == :attempt_correction
309
+
310
+ [status, corrector]
311
+ end
312
+
313
+ # @return [Symbol] offense status
314
+ def attempt_correction(range, corrector)
315
+ if corrector && !corrector.empty?
316
+ status = :corrected
317
+ elsif disable_uncorrectable?
318
+ corrector = disable_uncorrectable(range)
319
+ status = :corrected_with_todo
320
+ else
321
+ return :uncorrected
322
+ end
323
+
324
+ apply_correction(corrector) if corrector
325
+ status
326
+ end
327
+
328
+ def disable_uncorrectable(range)
329
+ line = range.line
330
+ return unless @currently_disabled_lines.add?(line)
331
+
332
+ disable_offense(range)
333
+ end
334
+
335
+ def range_from_node_or_range(node_or_range)
336
+ if node_or_range.respond_to?(:loc)
337
+ node_or_range.loc.expression
338
+ elsif node_or_range.is_a?(::Parser::Source::Range)
339
+ node_or_range
340
+ else
341
+ extra = ' (call `add_global_offense`)' if node_or_range.nil?
342
+ raise "Expected a Source::Range, got #{node_or_range.inspect}#{extra}"
343
+ end
344
+ end
345
+
346
+ def find_message(range, message)
347
+ annotate(message || message(range))
348
+ end
349
+
350
+ def annotate(message)
351
+ RuboCop::Cop::MessageAnnotator.new(
352
+ config, cop_name, cop_config, @options
353
+ ).annotate(message)
354
+ end
355
+
356
+ def file_name_matches_any?(file, parameter, default_result)
357
+ patterns = cop_config[parameter]
358
+ return default_result unless patterns
359
+
360
+ path = nil
361
+ patterns.any? do |pattern|
362
+ # Try to match the absolute path, as Exclude properties are absolute.
363
+ next true if match_path?(pattern, file)
364
+
365
+ # Try with relative path.
366
+ path ||= config.path_relative_to_config(file)
367
+ match_path?(pattern, path)
368
+ end
369
+ end
370
+
371
+ def enabled_line?(line_number)
372
+ return true if @options[:ignore_disable_comments] || !@processed_source
373
+
374
+ @processed_source.comment_config.cop_enabled_at_line?(self, line_number)
375
+ end
376
+
377
+ def find_severity(_range, severity)
378
+ custom_severity || severity || default_severity
379
+ end
380
+
381
+ def default_severity
382
+ self.class.lint? ? :warning : :convention
383
+ end
384
+
385
+ def custom_severity
386
+ severity = cop_config['Severity']
387
+ return unless severity
388
+
389
+ if Severity::NAMES.include?(severity.to_sym)
390
+ severity.to_sym
391
+ else
392
+ message = "Warning: Invalid severity '#{severity}'. " \
393
+ "Valid severities are #{Severity::NAMES.join(', ')}."
394
+ warn(Rainbow(message).red)
395
+ end
396
+ end
397
+ end
398
+ end
399
+ end