rubocop 0.75.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (602) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +20 -0
  3. data/README.md +217 -0
  4. data/assets/logo.png +0 -0
  5. data/assets/output.html.erb +261 -0
  6. data/bin/console +10 -0
  7. data/bin/setup +7 -0
  8. data/config/default.yml +3939 -0
  9. data/exe/rubocop +17 -0
  10. data/lib/rubocop.rb +608 -0
  11. data/lib/rubocop/ast/builder.rb +81 -0
  12. data/lib/rubocop/ast/node.rb +640 -0
  13. data/lib/rubocop/ast/node/alias_node.rb +24 -0
  14. data/lib/rubocop/ast/node/and_node.rb +29 -0
  15. data/lib/rubocop/ast/node/args_node.rb +29 -0
  16. data/lib/rubocop/ast/node/array_node.rb +57 -0
  17. data/lib/rubocop/ast/node/block_node.rb +115 -0
  18. data/lib/rubocop/ast/node/break_node.rb +17 -0
  19. data/lib/rubocop/ast/node/case_node.rb +56 -0
  20. data/lib/rubocop/ast/node/class_node.rb +31 -0
  21. data/lib/rubocop/ast/node/def_node.rb +71 -0
  22. data/lib/rubocop/ast/node/defined_node.rb +17 -0
  23. data/lib/rubocop/ast/node/ensure_node.rb +17 -0
  24. data/lib/rubocop/ast/node/float_node.rb +12 -0
  25. data/lib/rubocop/ast/node/for_node.rb +53 -0
  26. data/lib/rubocop/ast/node/hash_node.rb +109 -0
  27. data/lib/rubocop/ast/node/if_node.rb +175 -0
  28. data/lib/rubocop/ast/node/int_node.rb +12 -0
  29. data/lib/rubocop/ast/node/keyword_splat_node.rb +45 -0
  30. data/lib/rubocop/ast/node/mixin/basic_literal_node.rb +16 -0
  31. data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +43 -0
  32. data/lib/rubocop/ast/node/mixin/collection_node.rb +15 -0
  33. data/lib/rubocop/ast/node/mixin/conditional_node.rb +45 -0
  34. data/lib/rubocop/ast/node/mixin/hash_element_node.rb +125 -0
  35. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +261 -0
  36. data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +114 -0
  37. data/lib/rubocop/ast/node/mixin/modifier_node.rb +17 -0
  38. data/lib/rubocop/ast/node/mixin/numeric_node.rb +21 -0
  39. data/lib/rubocop/ast/node/mixin/parameterized_node.rb +61 -0
  40. data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +35 -0
  41. data/lib/rubocop/ast/node/module_node.rb +24 -0
  42. data/lib/rubocop/ast/node/or_node.rb +29 -0
  43. data/lib/rubocop/ast/node/pair_node.rb +63 -0
  44. data/lib/rubocop/ast/node/range_node.rb +18 -0
  45. data/lib/rubocop/ast/node/regexp_node.rb +35 -0
  46. data/lib/rubocop/ast/node/resbody_node.rb +24 -0
  47. data/lib/rubocop/ast/node/retry_node.rb +17 -0
  48. data/lib/rubocop/ast/node/self_class_node.rb +24 -0
  49. data/lib/rubocop/ast/node/send_node.rb +13 -0
  50. data/lib/rubocop/ast/node/str_node.rb +16 -0
  51. data/lib/rubocop/ast/node/super_node.rb +21 -0
  52. data/lib/rubocop/ast/node/symbol_node.rb +12 -0
  53. data/lib/rubocop/ast/node/until_node.rb +35 -0
  54. data/lib/rubocop/ast/node/when_node.rb +53 -0
  55. data/lib/rubocop/ast/node/while_node.rb +35 -0
  56. data/lib/rubocop/ast/node/yield_node.rb +21 -0
  57. data/lib/rubocop/ast/sexp.rb +16 -0
  58. data/lib/rubocop/ast/traversal.rb +183 -0
  59. data/lib/rubocop/cached_data.rb +58 -0
  60. data/lib/rubocop/cli.rb +343 -0
  61. data/lib/rubocop/comment_config.rb +201 -0
  62. data/lib/rubocop/config.rb +244 -0
  63. data/lib/rubocop/config_loader.rb +273 -0
  64. data/lib/rubocop/config_loader_resolver.rb +194 -0
  65. data/lib/rubocop/config_obsoletion.rb +213 -0
  66. data/lib/rubocop/config_store.rb +48 -0
  67. data/lib/rubocop/config_validator.rb +239 -0
  68. data/lib/rubocop/cop/autocorrect_logic.rb +103 -0
  69. data/lib/rubocop/cop/badge.rb +73 -0
  70. data/lib/rubocop/cop/bundler/duplicated_gem.rb +73 -0
  71. data/lib/rubocop/cop/bundler/gem_comment.rb +64 -0
  72. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +69 -0
  73. data/lib/rubocop/cop/bundler/ordered_gems.rb +73 -0
  74. data/lib/rubocop/cop/commissioner.rb +137 -0
  75. data/lib/rubocop/cop/cop.rb +271 -0
  76. data/lib/rubocop/cop/corrector.rb +171 -0
  77. data/lib/rubocop/cop/correctors/alignment_corrector.rb +146 -0
  78. data/lib/rubocop/cop/correctors/condition_corrector.rb +28 -0
  79. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +53 -0
  80. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +26 -0
  81. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +73 -0
  82. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +136 -0
  83. data/lib/rubocop/cop/correctors/line_break_corrector.rb +61 -0
  84. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +68 -0
  85. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +44 -0
  86. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +31 -0
  87. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +117 -0
  88. data/lib/rubocop/cop/correctors/punctuation_corrector.rb +29 -0
  89. data/lib/rubocop/cop/correctors/space_corrector.rb +49 -0
  90. data/lib/rubocop/cop/correctors/string_literal_corrector.rb +25 -0
  91. data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +43 -0
  92. data/lib/rubocop/cop/force.rb +42 -0
  93. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +104 -0
  94. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +106 -0
  95. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +85 -0
  96. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +55 -0
  97. data/lib/rubocop/cop/generator.rb +223 -0
  98. data/lib/rubocop/cop/generator/configuration_injector.rb +66 -0
  99. data/lib/rubocop/cop/generator/require_file_injector.rb +78 -0
  100. data/lib/rubocop/cop/ignored_node.rb +38 -0
  101. data/lib/rubocop/cop/internal_affairs.rb +8 -0
  102. data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +44 -0
  103. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +44 -0
  104. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +54 -0
  105. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +48 -0
  106. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +73 -0
  107. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +52 -0
  108. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +98 -0
  109. data/lib/rubocop/cop/layout/align_arguments.rb +93 -0
  110. data/lib/rubocop/cop/layout/align_array.rb +39 -0
  111. data/lib/rubocop/cop/layout/align_hash.rb +358 -0
  112. data/lib/rubocop/cop/layout/align_parameters.rb +118 -0
  113. data/lib/rubocop/cop/layout/block_alignment.rb +244 -0
  114. data/lib/rubocop/cop/layout/block_end_newline.rb +62 -0
  115. data/lib/rubocop/cop/layout/case_indentation.rb +161 -0
  116. data/lib/rubocop/cop/layout/class_structure.rb +340 -0
  117. data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +126 -0
  118. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +203 -0
  119. data/lib/rubocop/cop/layout/comment_indentation.rb +144 -0
  120. data/lib/rubocop/cop/layout/condition_position.rb +56 -0
  121. data/lib/rubocop/cop/layout/def_end_alignment.rb +74 -0
  122. data/lib/rubocop/cop/layout/dot_position.rb +105 -0
  123. data/lib/rubocop/cop/layout/else_alignment.rb +134 -0
  124. data/lib/rubocop/cop/layout/empty_comment.rb +160 -0
  125. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +157 -0
  126. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +64 -0
  127. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +151 -0
  128. data/lib/rubocop/cop/layout/empty_lines.rb +76 -0
  129. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +215 -0
  130. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +99 -0
  131. data/lib/rubocop/cop/layout/empty_lines_around_begin_body.rb +45 -0
  132. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +41 -0
  133. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +88 -0
  134. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +136 -0
  135. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +45 -0
  136. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +62 -0
  137. data/lib/rubocop/cop/layout/end_alignment.rb +189 -0
  138. data/lib/rubocop/cop/layout/end_of_line.rb +87 -0
  139. data/lib/rubocop/cop/layout/extra_spacing.rb +201 -0
  140. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +45 -0
  141. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +37 -0
  142. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +55 -0
  143. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +46 -0
  144. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +289 -0
  145. data/lib/rubocop/cop/layout/indent_assignment.rb +55 -0
  146. data/lib/rubocop/cop/layout/indent_first_argument.rb +247 -0
  147. data/lib/rubocop/cop/layout/indent_first_array_element.rb +167 -0
  148. data/lib/rubocop/cop/layout/indent_first_hash_element.rb +184 -0
  149. data/lib/rubocop/cop/layout/indent_first_parameter.rb +100 -0
  150. data/lib/rubocop/cop/layout/indent_heredoc.rb +256 -0
  151. data/lib/rubocop/cop/layout/indentation_consistency.rb +202 -0
  152. data/lib/rubocop/cop/layout/indentation_width.rb +364 -0
  153. data/lib/rubocop/cop/layout/initial_indentation.rb +59 -0
  154. data/lib/rubocop/cop/layout/leading_blank_lines.rb +53 -0
  155. data/lib/rubocop/cop/layout/leading_comment_space.rb +88 -0
  156. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +118 -0
  157. data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +39 -0
  158. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +95 -0
  159. data/lib/rubocop/cop/layout/multiline_block_layout.rb +146 -0
  160. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +122 -0
  161. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +50 -0
  162. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +54 -0
  163. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +134 -0
  164. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +220 -0
  165. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +131 -0
  166. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +114 -0
  167. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +189 -0
  168. data/lib/rubocop/cop/layout/space_after_colon.rb +47 -0
  169. data/lib/rubocop/cop/layout/space_after_comma.rb +35 -0
  170. data/lib/rubocop/cop/layout/space_after_method_name.rb +42 -0
  171. data/lib/rubocop/cop/layout/space_after_not.rb +40 -0
  172. data/lib/rubocop/cop/layout/space_after_semicolon.rb +32 -0
  173. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +169 -0
  174. data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +92 -0
  175. data/lib/rubocop/cop/layout/space_around_keyword.rb +232 -0
  176. data/lib/rubocop/cop/layout/space_around_operators.rb +182 -0
  177. data/lib/rubocop/cop/layout/space_before_block_braces.rb +119 -0
  178. data/lib/rubocop/cop/layout/space_before_comma.rb +31 -0
  179. data/lib/rubocop/cop/layout/space_before_comment.rb +35 -0
  180. data/lib/rubocop/cop/layout/space_before_first_arg.rb +67 -0
  181. data/lib/rubocop/cop/layout/space_before_semicolon.rb +27 -0
  182. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +80 -0
  183. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +228 -0
  184. data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +53 -0
  185. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +248 -0
  186. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +207 -0
  187. data/lib/rubocop/cop/layout/space_inside_parens.rb +113 -0
  188. data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +65 -0
  189. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +63 -0
  190. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +150 -0
  191. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +65 -0
  192. data/lib/rubocop/cop/layout/tab.rb +75 -0
  193. data/lib/rubocop/cop/layout/trailing_blank_lines.rb +113 -0
  194. data/lib/rubocop/cop/layout/trailing_whitespace.rb +61 -0
  195. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +62 -0
  196. data/lib/rubocop/cop/lint/ambiguous_operator.rb +55 -0
  197. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +43 -0
  198. data/lib/rubocop/cop/lint/assignment_in_condition.rb +97 -0
  199. data/lib/rubocop/cop/lint/big_decimal_new.rb +44 -0
  200. data/lib/rubocop/cop/lint/boolean_symbol.rb +38 -0
  201. data/lib/rubocop/cop/lint/circular_argument_reference.rb +72 -0
  202. data/lib/rubocop/cop/lint/debugger.rb +77 -0
  203. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +111 -0
  204. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +81 -0
  205. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +53 -0
  206. data/lib/rubocop/cop/lint/duplicate_methods.rb +239 -0
  207. data/lib/rubocop/cop/lint/duplicated_key.rb +38 -0
  208. data/lib/rubocop/cop/lint/each_with_object_argument.rb +42 -0
  209. data/lib/rubocop/cop/lint/else_layout.rb +66 -0
  210. data/lib/rubocop/cop/lint/empty_ensure.rb +60 -0
  211. data/lib/rubocop/cop/lint/empty_expression.rb +42 -0
  212. data/lib/rubocop/cop/lint/empty_interpolation.rb +36 -0
  213. data/lib/rubocop/cop/lint/empty_when.rb +38 -0
  214. data/lib/rubocop/cop/lint/end_in_method.rb +40 -0
  215. data/lib/rubocop/cop/lint/ensure_return.rb +46 -0
  216. data/lib/rubocop/cop/lint/erb_new_arguments.rb +163 -0
  217. data/lib/rubocop/cop/lint/flip_flop.rb +32 -0
  218. data/lib/rubocop/cop/lint/float_out_of_range.rb +35 -0
  219. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +159 -0
  220. data/lib/rubocop/cop/lint/handle_exceptions.rb +95 -0
  221. data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +156 -0
  222. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +101 -0
  223. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +127 -0
  224. data/lib/rubocop/cop/lint/inherit_exception.rb +100 -0
  225. data/lib/rubocop/cop/lint/interpolation_check.rb +40 -0
  226. data/lib/rubocop/cop/lint/literal_as_condition.rb +138 -0
  227. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +98 -0
  228. data/lib/rubocop/cop/lint/loop.rb +63 -0
  229. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +84 -0
  230. data/lib/rubocop/cop/lint/multiple_compare.rb +48 -0
  231. data/lib/rubocop/cop/lint/nested_method_definition.rb +104 -0
  232. data/lib/rubocop/cop/lint/nested_percent_literal.rb +51 -0
  233. data/lib/rubocop/cop/lint/next_without_accumulator.rb +50 -0
  234. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +83 -0
  235. data/lib/rubocop/cop/lint/number_conversion.rb +81 -0
  236. data/lib/rubocop/cop/lint/ordered_magic_comments.rb +86 -0
  237. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +64 -0
  238. data/lib/rubocop/cop/lint/percent_string_array.rb +73 -0
  239. data/lib/rubocop/cop/lint/percent_symbol_array.rb +69 -0
  240. data/lib/rubocop/cop/lint/rand_one.rb +45 -0
  241. data/lib/rubocop/cop/lint/redundant_with_index.rb +82 -0
  242. data/lib/rubocop/cop/lint/redundant_with_object.rb +83 -0
  243. data/lib/rubocop/cop/lint/regexp_as_condition.rb +29 -0
  244. data/lib/rubocop/cop/lint/require_parentheses.rb +66 -0
  245. data/lib/rubocop/cop/lint/rescue_exception.rb +46 -0
  246. data/lib/rubocop/cop/lint/rescue_type.rb +94 -0
  247. data/lib/rubocop/cop/lint/return_in_void_context.rb +74 -0
  248. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +66 -0
  249. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +94 -0
  250. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +38 -0
  251. data/lib/rubocop/cop/lint/script_permission.rb +70 -0
  252. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +91 -0
  253. data/lib/rubocop/cop/lint/shadowed_argument.rb +182 -0
  254. data/lib/rubocop/cop/lint/shadowed_exception.rb +178 -0
  255. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +53 -0
  256. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +59 -0
  257. data/lib/rubocop/cop/lint/syntax.rb +59 -0
  258. data/lib/rubocop/cop/lint/to_json.rb +41 -0
  259. data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +82 -0
  260. data/lib/rubocop/cop/lint/unified_integer.rb +45 -0
  261. data/lib/rubocop/cop/lint/unneeded_cop_disable_directive.rb +263 -0
  262. data/lib/rubocop/cop/lint/unneeded_cop_enable_directive.rb +116 -0
  263. data/lib/rubocop/cop/lint/unneeded_require_statement.rb +50 -0
  264. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +172 -0
  265. data/lib/rubocop/cop/lint/unreachable_code.rb +99 -0
  266. data/lib/rubocop/cop/lint/unused_block_argument.rb +165 -0
  267. data/lib/rubocop/cop/lint/unused_method_argument.rb +86 -0
  268. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +76 -0
  269. data/lib/rubocop/cop/lint/uri_regexp.rb +73 -0
  270. data/lib/rubocop/cop/lint/useless_access_modifier.rb +240 -0
  271. data/lib/rubocop/cop/lint/useless_assignment.rb +129 -0
  272. data/lib/rubocop/cop/lint/useless_comparison.rb +28 -0
  273. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +49 -0
  274. data/lib/rubocop/cop/lint/useless_setter_call.rb +164 -0
  275. data/lib/rubocop/cop/lint/void.rb +151 -0
  276. data/lib/rubocop/cop/message_annotator.rb +129 -0
  277. data/lib/rubocop/cop/metrics/abc_size.rb +24 -0
  278. data/lib/rubocop/cop/metrics/block_length.rb +50 -0
  279. data/lib/rubocop/cop/metrics/block_nesting.rb +65 -0
  280. data/lib/rubocop/cop/metrics/class_length.rb +36 -0
  281. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +32 -0
  282. data/lib/rubocop/cop/metrics/line_length.rb +321 -0
  283. data/lib/rubocop/cop/metrics/method_length.rb +36 -0
  284. data/lib/rubocop/cop/metrics/module_length.rb +36 -0
  285. data/lib/rubocop/cop/metrics/parameter_lists.rb +54 -0
  286. data/lib/rubocop/cop/metrics/perceived_complexity.rb +61 -0
  287. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +67 -0
  288. data/lib/rubocop/cop/migration/department_name.rb +44 -0
  289. data/lib/rubocop/cop/mixin/alignment.rb +74 -0
  290. data/lib/rubocop/cop/mixin/annotation_comment.rb +37 -0
  291. data/lib/rubocop/cop/mixin/array_min_size.rb +59 -0
  292. data/lib/rubocop/cop/mixin/array_syntax.rb +17 -0
  293. data/lib/rubocop/cop/mixin/check_assignment.rb +44 -0
  294. data/lib/rubocop/cop/mixin/check_line_breakable.rb +190 -0
  295. data/lib/rubocop/cop/mixin/classish_length.rb +37 -0
  296. data/lib/rubocop/cop/mixin/code_length.rb +38 -0
  297. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +98 -0
  298. data/lib/rubocop/cop/mixin/configurable_formatting.rb +47 -0
  299. data/lib/rubocop/cop/mixin/configurable_max.rb +23 -0
  300. data/lib/rubocop/cop/mixin/configurable_naming.rb +16 -0
  301. data/lib/rubocop/cop/mixin/configurable_numbering.rb +17 -0
  302. data/lib/rubocop/cop/mixin/def_node.rb +33 -0
  303. data/lib/rubocop/cop/mixin/documentation_comment.rb +52 -0
  304. data/lib/rubocop/cop/mixin/duplication.rb +46 -0
  305. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +172 -0
  306. data/lib/rubocop/cop/mixin/empty_parameter.rb +24 -0
  307. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +67 -0
  308. data/lib/rubocop/cop/mixin/enforce_superclass.rb +30 -0
  309. data/lib/rubocop/cop/mixin/first_element_line_break.rb +46 -0
  310. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +53 -0
  311. data/lib/rubocop/cop/mixin/hash_alignment.rb +147 -0
  312. data/lib/rubocop/cop/mixin/heredoc.rb +32 -0
  313. data/lib/rubocop/cop/mixin/ignored_methods.rb +19 -0
  314. data/lib/rubocop/cop/mixin/ignored_pattern.rb +29 -0
  315. data/lib/rubocop/cop/mixin/integer_node.rb +14 -0
  316. data/lib/rubocop/cop/mixin/interpolation.rb +27 -0
  317. data/lib/rubocop/cop/mixin/match_range.rb +26 -0
  318. data/lib/rubocop/cop/mixin/method_complexity.rb +56 -0
  319. data/lib/rubocop/cop/mixin/method_preference.rb +31 -0
  320. data/lib/rubocop/cop/mixin/min_body_length.rb +21 -0
  321. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +86 -0
  322. data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +33 -0
  323. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +255 -0
  324. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +141 -0
  325. data/lib/rubocop/cop/mixin/negative_conditional.rb +32 -0
  326. data/lib/rubocop/cop/mixin/nil_methods.rb +25 -0
  327. data/lib/rubocop/cop/mixin/on_normal_if_unless.rb +14 -0
  328. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +56 -0
  329. data/lib/rubocop/cop/mixin/parentheses.rb +17 -0
  330. data/lib/rubocop/cop/mixin/parser_diagnostic.rb +37 -0
  331. data/lib/rubocop/cop/mixin/percent_array.rb +52 -0
  332. data/lib/rubocop/cop/mixin/percent_literal.rb +38 -0
  333. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +181 -0
  334. data/lib/rubocop/cop/mixin/preferred_delimiters.rb +53 -0
  335. data/lib/rubocop/cop/mixin/range_help.rb +117 -0
  336. data/lib/rubocop/cop/mixin/rescue_node.rb +22 -0
  337. data/lib/rubocop/cop/mixin/safe_assignment.rb +23 -0
  338. data/lib/rubocop/cop/mixin/safe_mode.rb +24 -0
  339. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +55 -0
  340. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +49 -0
  341. data/lib/rubocop/cop/mixin/statement_modifier.rb +68 -0
  342. data/lib/rubocop/cop/mixin/string_help.rb +35 -0
  343. data/lib/rubocop/cop/mixin/string_literals_help.rb +23 -0
  344. data/lib/rubocop/cop/mixin/surrounding_space.rb +146 -0
  345. data/lib/rubocop/cop/mixin/target_ruby_version.rb +16 -0
  346. data/lib/rubocop/cop/mixin/too_many_lines.rb +35 -0
  347. data/lib/rubocop/cop/mixin/trailing_body.rb +26 -0
  348. data/lib/rubocop/cop/mixin/trailing_comma.rb +216 -0
  349. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +111 -0
  350. data/lib/rubocop/cop/mixin/unused_argument.rb +33 -0
  351. data/lib/rubocop/cop/naming/accessor_method_name.rb +55 -0
  352. data/lib/rubocop/cop/naming/ascii_identifiers.rb +72 -0
  353. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +43 -0
  354. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +33 -0
  355. data/lib/rubocop/cop/naming/constant_name.rb +81 -0
  356. data/lib/rubocop/cop/naming/file_name.rb +205 -0
  357. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +62 -0
  358. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +55 -0
  359. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +171 -0
  360. data/lib/rubocop/cop/naming/method_name.rb +53 -0
  361. data/lib/rubocop/cop/naming/predicate_name.rb +108 -0
  362. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +112 -0
  363. data/lib/rubocop/cop/naming/uncommunicative_block_param_name.rb +49 -0
  364. data/lib/rubocop/cop/naming/uncommunicative_method_param_name.rb +58 -0
  365. data/lib/rubocop/cop/naming/variable_name.rb +52 -0
  366. data/lib/rubocop/cop/naming/variable_number.rb +61 -0
  367. data/lib/rubocop/cop/offense.rb +205 -0
  368. data/lib/rubocop/cop/registry.rb +211 -0
  369. data/lib/rubocop/cop/security/eval.rb +31 -0
  370. data/lib/rubocop/cop/security/json_load.rb +46 -0
  371. data/lib/rubocop/cop/security/marshal_load.rb +39 -0
  372. data/lib/rubocop/cop/security/open.rb +71 -0
  373. data/lib/rubocop/cop/security/yaml_load.rb +37 -0
  374. data/lib/rubocop/cop/severity.rb +77 -0
  375. data/lib/rubocop/cop/style/access_modifier_declarations.rb +112 -0
  376. data/lib/rubocop/cop/style/alias.rb +147 -0
  377. data/lib/rubocop/cop/style/and_or.rb +146 -0
  378. data/lib/rubocop/cop/style/array_join.rb +39 -0
  379. data/lib/rubocop/cop/style/ascii_comments.rb +61 -0
  380. data/lib/rubocop/cop/style/attr.rb +62 -0
  381. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +51 -0
  382. data/lib/rubocop/cop/style/bare_percent_literals.rb +78 -0
  383. data/lib/rubocop/cop/style/begin_block.rb +22 -0
  384. data/lib/rubocop/cop/style/block_comments.rb +70 -0
  385. data/lib/rubocop/cop/style/block_delimiters.rb +331 -0
  386. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +209 -0
  387. data/lib/rubocop/cop/style/case_equality.rb +30 -0
  388. data/lib/rubocop/cop/style/character_literal.rb +53 -0
  389. data/lib/rubocop/cop/style/class_and_module_children.rb +151 -0
  390. data/lib/rubocop/cop/style/class_check.rb +59 -0
  391. data/lib/rubocop/cop/style/class_methods.rb +60 -0
  392. data/lib/rubocop/cop/style/class_vars.rb +48 -0
  393. data/lib/rubocop/cop/style/collection_methods.rb +77 -0
  394. data/lib/rubocop/cop/style/colon_method_call.rb +48 -0
  395. data/lib/rubocop/cop/style/colon_method_definition.rb +37 -0
  396. data/lib/rubocop/cop/style/command_literal.rb +187 -0
  397. data/lib/rubocop/cop/style/comment_annotation.rb +97 -0
  398. data/lib/rubocop/cop/style/commented_keyword.rb +73 -0
  399. data/lib/rubocop/cop/style/conditional_assignment.rb +668 -0
  400. data/lib/rubocop/cop/style/constant_visibility.rb +77 -0
  401. data/lib/rubocop/cop/style/copyright.rb +95 -0
  402. data/lib/rubocop/cop/style/date_time.rb +77 -0
  403. data/lib/rubocop/cop/style/def_with_parentheses.rb +57 -0
  404. data/lib/rubocop/cop/style/dir.rb +48 -0
  405. data/lib/rubocop/cop/style/documentation.rb +97 -0
  406. data/lib/rubocop/cop/style/documentation_method.rb +125 -0
  407. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +55 -0
  408. data/lib/rubocop/cop/style/double_negation.rb +35 -0
  409. data/lib/rubocop/cop/style/each_for_simple_loop.rb +58 -0
  410. data/lib/rubocop/cop/style/each_with_object.rb +110 -0
  411. data/lib/rubocop/cop/style/empty_block_parameter.rb +48 -0
  412. data/lib/rubocop/cop/style/empty_case_condition.rb +107 -0
  413. data/lib/rubocop/cop/style/empty_else.rb +175 -0
  414. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +45 -0
  415. data/lib/rubocop/cop/style/empty_literal.rb +123 -0
  416. data/lib/rubocop/cop/style/empty_method.rb +115 -0
  417. data/lib/rubocop/cop/style/encoding.rb +56 -0
  418. data/lib/rubocop/cop/style/end_block.rb +25 -0
  419. data/lib/rubocop/cop/style/eval_with_location.rb +148 -0
  420. data/lib/rubocop/cop/style/even_odd.rb +58 -0
  421. data/lib/rubocop/cop/style/expand_path_arguments.rb +194 -0
  422. data/lib/rubocop/cop/style/float_division.rb +94 -0
  423. data/lib/rubocop/cop/style/for.rb +88 -0
  424. data/lib/rubocop/cop/style/format_string.rb +124 -0
  425. data/lib/rubocop/cop/style/format_string_token.rb +119 -0
  426. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +149 -0
  427. data/lib/rubocop/cop/style/global_vars.rb +80 -0
  428. data/lib/rubocop/cop/style/guard_clause.rb +121 -0
  429. data/lib/rubocop/cop/style/hash_syntax.rb +214 -0
  430. data/lib/rubocop/cop/style/identical_conditional_branches.rb +130 -0
  431. data/lib/rubocop/cop/style/if_inside_else.rb +87 -0
  432. data/lib/rubocop/cop/style/if_unless_modifier.rb +127 -0
  433. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +39 -0
  434. data/lib/rubocop/cop/style/if_with_semicolon.rb +30 -0
  435. data/lib/rubocop/cop/style/implicit_runtime_error.rb +32 -0
  436. data/lib/rubocop/cop/style/infinite_loop.rb +127 -0
  437. data/lib/rubocop/cop/style/inline_comment.rb +34 -0
  438. data/lib/rubocop/cop/style/inverse_methods.rb +187 -0
  439. data/lib/rubocop/cop/style/ip_addresses.rb +76 -0
  440. data/lib/rubocop/cop/style/lambda.rb +131 -0
  441. data/lib/rubocop/cop/style/lambda_call.rb +93 -0
  442. data/lib/rubocop/cop/style/line_end_concatenation.rb +121 -0
  443. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +388 -0
  444. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +77 -0
  445. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +46 -0
  446. data/lib/rubocop/cop/style/method_def_parentheses.rb +158 -0
  447. data/lib/rubocop/cop/style/method_missing_super.rb +34 -0
  448. data/lib/rubocop/cop/style/min_max.rb +68 -0
  449. data/lib/rubocop/cop/style/missing_else.rb +180 -0
  450. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +46 -0
  451. data/lib/rubocop/cop/style/mixin_grouping.rb +148 -0
  452. data/lib/rubocop/cop/style/mixin_usage.rb +90 -0
  453. data/lib/rubocop/cop/style/module_function.rb +104 -0
  454. data/lib/rubocop/cop/style/multiline_block_chain.rb +40 -0
  455. data/lib/rubocop/cop/style/multiline_if_modifier.rb +67 -0
  456. data/lib/rubocop/cop/style/multiline_if_then.rb +50 -0
  457. data/lib/rubocop/cop/style/multiline_memoization.rb +94 -0
  458. data/lib/rubocop/cop/style/multiline_method_signature.rb +61 -0
  459. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +38 -0
  460. data/lib/rubocop/cop/style/multiline_when_then.rb +55 -0
  461. data/lib/rubocop/cop/style/multiple_comparison.rb +92 -0
  462. data/lib/rubocop/cop/style/mutable_constant.rb +174 -0
  463. data/lib/rubocop/cop/style/negated_if.rb +99 -0
  464. data/lib/rubocop/cop/style/negated_unless.rb +89 -0
  465. data/lib/rubocop/cop/style/negated_while.rb +48 -0
  466. data/lib/rubocop/cop/style/nested_modifier.rb +105 -0
  467. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +67 -0
  468. data/lib/rubocop/cop/style/nested_ternary_operator.rb +32 -0
  469. data/lib/rubocop/cop/style/next.rb +244 -0
  470. data/lib/rubocop/cop/style/nil_comparison.rb +75 -0
  471. data/lib/rubocop/cop/style/non_nil_check.rb +124 -0
  472. data/lib/rubocop/cop/style/not.rb +82 -0
  473. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +124 -0
  474. data/lib/rubocop/cop/style/numeric_literals.rb +110 -0
  475. data/lib/rubocop/cop/style/numeric_predicate.rb +137 -0
  476. data/lib/rubocop/cop/style/one_line_conditional.rb +101 -0
  477. data/lib/rubocop/cop/style/option_hash.rb +55 -0
  478. data/lib/rubocop/cop/style/optional_arguments.rb +58 -0
  479. data/lib/rubocop/cop/style/or_assignment.rb +95 -0
  480. data/lib/rubocop/cop/style/parallel_assignment.rb +287 -0
  481. data/lib/rubocop/cop/style/parentheses_around_condition.rb +117 -0
  482. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +127 -0
  483. data/lib/rubocop/cop/style/percent_q_literals.rb +73 -0
  484. data/lib/rubocop/cop/style/perl_backrefs.rb +38 -0
  485. data/lib/rubocop/cop/style/preferred_hash_methods.rb +75 -0
  486. data/lib/rubocop/cop/style/proc.rb +34 -0
  487. data/lib/rubocop/cop/style/raise_args.rb +145 -0
  488. data/lib/rubocop/cop/style/random_with_offset.rb +158 -0
  489. data/lib/rubocop/cop/style/redundant_begin.rb +91 -0
  490. data/lib/rubocop/cop/style/redundant_conditional.rb +97 -0
  491. data/lib/rubocop/cop/style/redundant_exception.rb +60 -0
  492. data/lib/rubocop/cop/style/redundant_freeze.rb +67 -0
  493. data/lib/rubocop/cop/style/redundant_parentheses.rb +231 -0
  494. data/lib/rubocop/cop/style/redundant_return.rb +173 -0
  495. data/lib/rubocop/cop/style/redundant_self.rb +171 -0
  496. data/lib/rubocop/cop/style/redundant_sort_by.rb +50 -0
  497. data/lib/rubocop/cop/style/regexp_literal.rb +228 -0
  498. data/lib/rubocop/cop/style/rescue_modifier.rb +73 -0
  499. data/lib/rubocop/cop/style/rescue_standard_error.rb +124 -0
  500. data/lib/rubocop/cop/style/return_nil.rb +89 -0
  501. data/lib/rubocop/cop/style/safe_navigation.rb +272 -0
  502. data/lib/rubocop/cop/style/sample.rb +144 -0
  503. data/lib/rubocop/cop/style/self_assignment.rb +97 -0
  504. data/lib/rubocop/cop/style/semicolon.rb +101 -0
  505. data/lib/rubocop/cop/style/send.rb +31 -0
  506. data/lib/rubocop/cop/style/signal_exception.rb +211 -0
  507. data/lib/rubocop/cop/style/single_line_block_params.rb +95 -0
  508. data/lib/rubocop/cop/style/single_line_methods.rb +83 -0
  509. data/lib/rubocop/cop/style/special_global_vars.rb +213 -0
  510. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +86 -0
  511. data/lib/rubocop/cop/style/stderr_puts.rb +61 -0
  512. data/lib/rubocop/cop/style/string_hash_keys.rb +50 -0
  513. data/lib/rubocop/cop/style/string_literals.rb +129 -0
  514. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +49 -0
  515. data/lib/rubocop/cop/style/string_methods.rb +46 -0
  516. data/lib/rubocop/cop/style/strip.rb +46 -0
  517. data/lib/rubocop/cop/style/struct_inheritance.rb +39 -0
  518. data/lib/rubocop/cop/style/symbol_array.rb +119 -0
  519. data/lib/rubocop/cop/style/symbol_literal.rb +32 -0
  520. data/lib/rubocop/cop/style/symbol_proc.rb +110 -0
  521. data/lib/rubocop/cop/style/ternary_parentheses.rb +223 -0
  522. data/lib/rubocop/cop/style/trailing_body_on_class.rb +43 -0
  523. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +54 -0
  524. data/lib/rubocop/cop/style/trailing_body_on_module.rb +43 -0
  525. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +96 -0
  526. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +58 -0
  527. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +56 -0
  528. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +91 -0
  529. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +161 -0
  530. data/lib/rubocop/cop/style/trivial_accessors.rb +191 -0
  531. data/lib/rubocop/cop/style/unless_else.rb +55 -0
  532. data/lib/rubocop/cop/style/unneeded_capital_w.rb +51 -0
  533. data/lib/rubocop/cop/style/unneeded_condition.rb +112 -0
  534. data/lib/rubocop/cop/style/unneeded_interpolation.rb +98 -0
  535. data/lib/rubocop/cop/style/unneeded_percent_q.rb +112 -0
  536. data/lib/rubocop/cop/style/unneeded_sort.rb +165 -0
  537. data/lib/rubocop/cop/style/unpack_first.rb +65 -0
  538. data/lib/rubocop/cop/style/variable_interpolation.rb +48 -0
  539. data/lib/rubocop/cop/style/when_then.rb +37 -0
  540. data/lib/rubocop/cop/style/while_until_do.rb +59 -0
  541. data/lib/rubocop/cop/style/while_until_modifier.rb +61 -0
  542. data/lib/rubocop/cop/style/word_array.rb +102 -0
  543. data/lib/rubocop/cop/style/yoda_condition.rb +141 -0
  544. data/lib/rubocop/cop/style/zero_length_predicate.rb +117 -0
  545. data/lib/rubocop/cop/team.rb +191 -0
  546. data/lib/rubocop/cop/util.rb +127 -0
  547. data/lib/rubocop/cop/utils/format_string.rb +128 -0
  548. data/lib/rubocop/cop/variable_force.rb +464 -0
  549. data/lib/rubocop/cop/variable_force/assignment.rb +96 -0
  550. data/lib/rubocop/cop/variable_force/branch.rb +322 -0
  551. data/lib/rubocop/cop/variable_force/branchable.rb +23 -0
  552. data/lib/rubocop/cop/variable_force/reference.rb +49 -0
  553. data/lib/rubocop/cop/variable_force/scope.rb +109 -0
  554. data/lib/rubocop/cop/variable_force/variable.rb +117 -0
  555. data/lib/rubocop/cop/variable_force/variable_table.rb +129 -0
  556. data/lib/rubocop/core_ext/string.rb +23 -0
  557. data/lib/rubocop/error.rb +34 -0
  558. data/lib/rubocop/file_finder.rb +42 -0
  559. data/lib/rubocop/formatter/auto_gen_config_formatter.rb +16 -0
  560. data/lib/rubocop/formatter/base_formatter.rb +123 -0
  561. data/lib/rubocop/formatter/clang_style_formatter.rb +59 -0
  562. data/lib/rubocop/formatter/colorizable.rb +41 -0
  563. data/lib/rubocop/formatter/disabled_config_formatter.rb +224 -0
  564. data/lib/rubocop/formatter/disabled_lines_formatter.rb +57 -0
  565. data/lib/rubocop/formatter/emacs_style_formatter.rb +37 -0
  566. data/lib/rubocop/formatter/file_list_formatter.rb +20 -0
  567. data/lib/rubocop/formatter/formatter_set.rb +106 -0
  568. data/lib/rubocop/formatter/fuubar_style_formatter.rb +80 -0
  569. data/lib/rubocop/formatter/html_formatter.rb +141 -0
  570. data/lib/rubocop/formatter/json_formatter.rb +80 -0
  571. data/lib/rubocop/formatter/offense_count_formatter.rb +74 -0
  572. data/lib/rubocop/formatter/pacman_formatter.rb +80 -0
  573. data/lib/rubocop/formatter/progress_formatter.rb +63 -0
  574. data/lib/rubocop/formatter/quiet_formatter.rb +13 -0
  575. data/lib/rubocop/formatter/simple_text_formatter.rb +138 -0
  576. data/lib/rubocop/formatter/tap_formatter.rb +84 -0
  577. data/lib/rubocop/formatter/text_util.rb +20 -0
  578. data/lib/rubocop/formatter/worst_offenders_formatter.rb +62 -0
  579. data/lib/rubocop/magic_comment.rb +214 -0
  580. data/lib/rubocop/name_similarity.rb +21 -0
  581. data/lib/rubocop/node_pattern.rb +799 -0
  582. data/lib/rubocop/options.rb +454 -0
  583. data/lib/rubocop/path_util.rb +85 -0
  584. data/lib/rubocop/platform.rb +11 -0
  585. data/lib/rubocop/processed_source.rb +216 -0
  586. data/lib/rubocop/rake_task.rb +79 -0
  587. data/lib/rubocop/remote_config.rb +106 -0
  588. data/lib/rubocop/result_cache.rb +191 -0
  589. data/lib/rubocop/rspec/cop_helper.rb +94 -0
  590. data/lib/rubocop/rspec/expect_offense.rb +240 -0
  591. data/lib/rubocop/rspec/host_environment_simulation_helper.rb +28 -0
  592. data/lib/rubocop/rspec/shared_contexts.rb +90 -0
  593. data/lib/rubocop/rspec/support.rb +13 -0
  594. data/lib/rubocop/runner.rb +358 -0
  595. data/lib/rubocop/string_interpreter.rb +57 -0
  596. data/lib/rubocop/string_util.rb +14 -0
  597. data/lib/rubocop/target_finder.rb +190 -0
  598. data/lib/rubocop/token.rb +114 -0
  599. data/lib/rubocop/version.rb +21 -0
  600. data/lib/rubocop/warning.rb +11 -0
  601. data/lib/rubocop/yaml_duplication_checker.rb +39 -0
  602. metadata +770 -0
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Formatter
5
+ # Common logic for UI texts.
6
+ module TextUtil
7
+ module_function
8
+
9
+ def pluralize(number, thing, options = {})
10
+ if number.zero? && options[:no_for_zero]
11
+ "no #{thing}s"
12
+ elsif number == 1
13
+ "1 #{thing}"
14
+ else
15
+ "#{number} #{thing}s"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+
5
+ module RuboCop
6
+ module Formatter
7
+ # This formatter displays the list of offensive files, sorted by number of
8
+ # offenses with the worst offenders first.
9
+ #
10
+ # Here's the format:
11
+ #
12
+ # 26 this/file/is/really/bad.rb
13
+ # 3 just/ok.rb
14
+ # --
15
+ # 29 Total
16
+ class WorstOffendersFormatter < BaseFormatter
17
+ attr_reader :offense_counts
18
+
19
+ def started(target_files)
20
+ super
21
+ @offense_counts = {}
22
+ end
23
+
24
+ def file_finished(file, offenses)
25
+ return if offenses.empty?
26
+
27
+ path = Pathname.new(file).relative_path_from(Pathname.new(Dir.pwd))
28
+ @offense_counts[path] = offenses.size
29
+ end
30
+
31
+ def finished(_inspected_files)
32
+ report_summary(@offense_counts)
33
+ end
34
+
35
+ # rubocop:disable Metrics/AbcSize
36
+ def report_summary(offense_counts)
37
+ per_file_counts = ordered_offense_counts(offense_counts)
38
+ total_count = total_offense_count(offense_counts)
39
+
40
+ output.puts
41
+
42
+ per_file_counts.each do |file_name, count|
43
+ output.puts "#{count.to_s.ljust(total_count.to_s.length + 2)}" \
44
+ "#{file_name}\n"
45
+ end
46
+ output.puts '--'
47
+ output.puts "#{total_count} Total"
48
+
49
+ output.puts
50
+ end
51
+ # rubocop:enable Metrics/AbcSize
52
+
53
+ def ordered_offense_counts(offense_counts)
54
+ Hash[offense_counts.sort_by { |k, v| [-v, k] }]
55
+ end
56
+
57
+ def total_offense_count(offense_counts)
58
+ offense_counts.values.inject(0, :+)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,214 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # Parse different formats of magic comments.
5
+ #
6
+ # @abstract parent of three different magic comment handlers
7
+ class MagicComment
8
+ # @see https://git.io/vMC1C IRB's pattern for matching magic comment tokens
9
+ TOKEN = /[[:alnum:]\-_]+/.freeze
10
+
11
+ # Detect magic comment format and pass it to the appropriate wrapper.
12
+ #
13
+ # @param comment [String]
14
+ #
15
+ # @return [RuboCop::MagicComment]
16
+ def self.parse(comment)
17
+ case comment
18
+ when EmacsComment::FORMAT then EmacsComment.new(comment)
19
+ when VimComment::FORMAT then VimComment.new(comment)
20
+ else
21
+ SimpleComment.new(comment)
22
+ end
23
+ end
24
+
25
+ def initialize(comment)
26
+ @comment = comment
27
+ end
28
+
29
+ def any?
30
+ frozen_string_literal_specified? || encoding_specified?
31
+ end
32
+
33
+ # Does the magic comment enable the frozen string literal feature.
34
+ #
35
+ # Test whether the frozen string literal value is `true`. Cannot
36
+ # just return `frozen_string_literal` since an invalid magic comment
37
+ # like `# frozen_string_literal: yes` is possible and the truthy value
38
+ # `'yes'` does not actually enable the feature
39
+ #
40
+ # @return [Boolean]
41
+ def frozen_string_literal?
42
+ frozen_string_literal == true
43
+ end
44
+
45
+ def valid_literal_value?
46
+ [true, false].include?(frozen_string_literal)
47
+ end
48
+
49
+ # Was a magic comment for the frozen string literal found?
50
+ #
51
+ # @return [Boolean]
52
+ def frozen_string_literal_specified?
53
+ specified?(frozen_string_literal)
54
+ end
55
+
56
+ # Expose the `frozen_string_literal` value coerced to a boolean if possible.
57
+ #
58
+ # @return [Boolean] if value is `true` or `false`
59
+ # @return [nil] if frozen_string_literal comment isn't found
60
+ # @return [String] if comment is found but isn't true or false
61
+ def frozen_string_literal
62
+ return unless (setting = extract_frozen_string_literal)
63
+
64
+ case setting
65
+ when 'true' then true
66
+ when 'false' then false
67
+ else
68
+ setting
69
+ end
70
+ end
71
+
72
+ def encoding_specified?
73
+ specified?(encoding)
74
+ end
75
+
76
+ private
77
+
78
+ def specified?(value)
79
+ !value.nil?
80
+ end
81
+
82
+ # Match the entire comment string with a pattern and take the first capture.
83
+ #
84
+ # @param pattern [Regexp]
85
+ #
86
+ # @return [String] if pattern matched
87
+ # @return [nil] otherwise
88
+ def extract(pattern)
89
+ @comment[pattern, 1]
90
+ end
91
+
92
+ # Parent to Vim and Emacs magic comment handling.
93
+ #
94
+ # @abstract
95
+ class EditorComment < MagicComment
96
+ private
97
+
98
+ # Find a token starting with the provided keyword and extract its value.
99
+ #
100
+ # @param keyword [String]
101
+ #
102
+ # @return [String] extracted value if it is found
103
+ # @return [nil] otherwise
104
+ def match(keyword)
105
+ pattern = /\A#{keyword}\s*#{self.class::OPERATOR}\s*(#{TOKEN})\z/
106
+
107
+ tokens.each do |token|
108
+ next unless (value = token[pattern, 1])
109
+
110
+ return value.downcase
111
+ end
112
+
113
+ nil
114
+ end
115
+
116
+ # Individual tokens composing an editor specific comment string.
117
+ #
118
+ # @return [Array<String>]
119
+ def tokens
120
+ extract(self.class::FORMAT).split(self.class::SEPARATOR).map(&:strip)
121
+ end
122
+ end
123
+
124
+ # Wrapper for Emacs style magic comments.
125
+ #
126
+ # @example Emacs style comment
127
+ # comment = RuboCop::MagicComment.parse(
128
+ # '# -*- encoding: ASCII-8BIT -*-'
129
+ # )
130
+ #
131
+ # comment.encoding # => 'ascii-8bit'
132
+ #
133
+ # @see https://www.gnu.org/software/emacs/manual/html_node/emacs/Specify-Coding.html
134
+ # @see https://git.io/vMCXh Emacs handling in Ruby's parse.y
135
+ class EmacsComment < EditorComment
136
+ FORMAT = /\-\*\-(.+)\-\*\-/.freeze
137
+ SEPARATOR = ';'
138
+ OPERATOR = ':'
139
+
140
+ def encoding
141
+ match('(?:en)?coding')
142
+ end
143
+
144
+ private
145
+
146
+ def extract_frozen_string_literal
147
+ match('frozen[_-]string[_-]literal')
148
+ end
149
+ end
150
+
151
+ # Wrapper for Vim style magic comments.
152
+ #
153
+ # @example Vim style comment
154
+ # comment = RuboCop::MagicComment.parse(
155
+ # '# vim: filetype=ruby, fileencoding=ascii-8bit'
156
+ # )
157
+ #
158
+ # comment.encoding # => 'ascii-8bit'
159
+ class VimComment < EditorComment
160
+ FORMAT = /#\s*vim:\s*(.+)/.freeze
161
+ SEPARATOR = ', '
162
+ OPERATOR = '='
163
+
164
+ # For some reason the fileencoding keyword only works if there
165
+ # is at least one other token included in the string. For example
166
+ #
167
+ # # works
168
+ # # vim: foo=bar, fileencoding=ascii-8bit
169
+ #
170
+ # # does nothing
171
+ # # vim: foo=bar, fileencoding=ascii-8bit
172
+ #
173
+ def encoding
174
+ match('fileencoding') if tokens.size > 1
175
+ end
176
+
177
+ # Vim comments cannot specify frozen string literal behavior.
178
+ def frozen_string_literal; end
179
+ end
180
+
181
+ # Wrapper for regular magic comments not bound to an editor.
182
+ #
183
+ # Simple comments can only specify one setting per comment.
184
+ #
185
+ # @example frozen string literal comments
186
+ # comment1 = RuboCop::MagicComment.parse('# frozen_string_literal: true')
187
+ # comment1.frozen_string_literal # => true
188
+ # comment1.encoding # => nil
189
+ #
190
+ # @example encoding comments
191
+ # comment2 = RuboCop::MagicComment.parse('# encoding: utf-8')
192
+ # comment2.frozen_string_literal # => nil
193
+ # comment2.encoding # => 'utf-8'
194
+ class SimpleComment < MagicComment
195
+ # Match `encoding` or `coding`
196
+ def encoding
197
+ extract(/\A\s*\#.*\b(?:en)?coding: (#{TOKEN})/i)
198
+ end
199
+
200
+ private
201
+
202
+ # Extract `frozen_string_literal`.
203
+ #
204
+ # The `frozen_string_literal` magic comment only works if it
205
+ # is the only text in the comment.
206
+ #
207
+ # Case-insensitive and dashes/underscores are acceptable.
208
+ # @see https://git.io/vM7Mg
209
+ def extract_frozen_string_literal
210
+ extract(/\A\s*#\s*frozen[_-]string[_-]literal:\s*(#{TOKEN})\s*\z/i)
211
+ end
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # Common functionality for finding names that are similar to a given name.
5
+ module NameSimilarity
6
+ MINIMUM_SIMILARITY_TO_SUGGEST = 0.9
7
+
8
+ def find_similar_name(target_name, scope)
9
+ names = collect_variable_like_names(scope)
10
+ names.delete(target_name)
11
+
12
+ scores = names.each_with_object({}) do |name, hash|
13
+ score = StringUtil.similarity(target_name, name)
14
+ hash[name] = score if score >= MINIMUM_SIMILARITY_TO_SUGGEST
15
+ end
16
+
17
+ most_similar_name, _max_score = scores.max_by { |_, score| score }
18
+ most_similar_name
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,799 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'delegate'
4
+ require 'erb'
5
+
6
+ # rubocop:disable Metrics/ClassLength, Metrics/CyclomaticComplexity
7
+ module RuboCop
8
+ # This class performs a pattern-matching operation on an AST node.
9
+ #
10
+ # Initialize a new `NodePattern` with `NodePattern.new(pattern_string)`, then
11
+ # pass an AST node to `NodePattern#match`. Alternatively, use one of the class
12
+ # macros in `NodePattern::Macros` to define your own pattern-matching method.
13
+ #
14
+ # If the match fails, `nil` will be returned. If the match succeeds, the
15
+ # return value depends on whether a block was provided to `#match`, and
16
+ # whether the pattern contained any "captures" (values which are extracted
17
+ # from a matching AST.)
18
+ #
19
+ # - With block: #match yields the captures (if any) and passes the return
20
+ # value of the block through.
21
+ # - With no block, but one capture: the capture is returned.
22
+ # - With no block, but multiple captures: captures are returned as an array.
23
+ # - With no block and no captures: #match returns `true`.
24
+ #
25
+ # ## Pattern string format examples
26
+ #
27
+ # ':sym' # matches a literal symbol
28
+ # '1' # matches a literal integer
29
+ # 'nil' # matches a literal nil
30
+ # 'send' # matches (send ...)
31
+ # '(send)' # matches (send)
32
+ # '(send ...)' # matches (send ...)
33
+ # '(op-asgn)' # node types with hyphenated names also work
34
+ # '{send class}' # matches (send ...) or (class ...)
35
+ # '({send class})' # matches (send) or (class)
36
+ # '(send const)' # matches (send (const ...))
37
+ # '(send _ :new)' # matches (send <anything> :new)
38
+ # '(send $_ :new)' # as above, but whatever matches the $_ is captured
39
+ # '(send $_ $_)' # you can use as many captures as you want
40
+ # '(send !const ...)' # ! negates the next part of the pattern
41
+ # '$(send const ...)' # arbitrary matching can be performed on a capture
42
+ # '(send _recv _msg)' # wildcards can be named (for readability)
43
+ # '(send ... :new)' # you can match against the last children
44
+ # '(array <str sym>)' # you can match children in any order. This
45
+ # # would match `['x', :y]` as well as `[:y, 'x']
46
+ # '(_ <str sym ...>)' # will match if arguments have at least a `str` and
47
+ # # a `sym` node, but can have more.
48
+ # '(array <$str $_>)' # captures are in the order of the pattern,
49
+ # # irrespective of the actual order of the children
50
+ # '(array int*)' # will match an array of 0 or more integers
51
+ # '(array int ?)' # will match 0 or 1 integer.
52
+ # # Note: Space needed to distinguish from int?
53
+ # '(array int+)' # will match an array of 1 or more integers
54
+ # '(array (int $_)+)' # as above and will capture the numbers in an array
55
+ # '(send $...)' # capture all the children as an array
56
+ # '(send $... int)' # capture all children but the last as an array
57
+ # '(send _x :+ _x)' # unification is performed on named wildcards
58
+ # # (like Prolog variables...)
59
+ # # (#== is used to see if values unify)
60
+ # '(int odd?)' # words which end with a ? are predicate methods,
61
+ # # are are called on the target to see if it matches
62
+ # # any Ruby method which the matched object supports
63
+ # # can be used
64
+ # # if a truthy value is returned, the match succeeds
65
+ # '(int [!1 !2])' # [] contains multiple patterns, ALL of which must
66
+ # # match in that position
67
+ # # in other words, while {} is pattern union (logical
68
+ # # OR), [] is intersection (logical AND)
69
+ # '(send %1 _)' # % stands for a parameter which must be supplied to
70
+ # # #match at matching time
71
+ # # it will be compared to the corresponding value in
72
+ # # the AST using #==
73
+ # # a bare '%' is the same as '%1'
74
+ # # the number of extra parameters passed to #match
75
+ # # must equal the highest % value in the pattern
76
+ # # for consistency, %0 is the 'root node' which is
77
+ # # passed as the 1st argument to #match, where the
78
+ # # matching process starts
79
+ # '^^send' # each ^ ascends one level in the AST
80
+ # # so this matches against the grandparent node
81
+ # '#method' # we call this a 'funcall'; it calls a method in the
82
+ # # context where a pattern-matching method is defined
83
+ # # if that returns a truthy value, the match succeeds
84
+ # 'equal?(%1)' # predicates can be given 1 or more extra args
85
+ # '#method(%0, 1)' # funcalls can also be given 1 or more extra args
86
+ #
87
+ # You can nest arbitrarily deep:
88
+ #
89
+ # # matches node parsed from 'Const = Class.new' or 'Const = Module.new':
90
+ # '(casgn nil? :Const (send (const nil? {:Class :Module}) :new))'
91
+ # # matches a node parsed from an 'if', with a '==' comparison,
92
+ # # and no 'else' branch:
93
+ # '(if (send _ :== _) _ nil?)'
94
+ #
95
+ # Note that patterns like 'send' are implemented by calling `#send_type?` on
96
+ # the node being matched, 'const' by `#const_type?`, 'int' by `#int_type?`,
97
+ # and so on. Therefore, if you add methods which are named like
98
+ # `#prefix_type?` to the AST node class, then 'prefix' will become usable as
99
+ # a pattern.
100
+ #
101
+ # Also note that if you need a "guard clause" to protect against possible nils
102
+ # in a certain place in the AST, you can do it like this: `[!nil <pattern>]`
103
+ #
104
+ # The compiler code is very simple; don't be afraid to read through it!
105
+ class NodePattern
106
+ # @private
107
+ Invalid = Class.new(StandardError)
108
+
109
+ # @private
110
+ # Builds Ruby code which implements a pattern
111
+ class Compiler
112
+ SYMBOL = %r{:(?:[\w+@*/?!<>=~|%^-]+|\[\]=?)}.freeze
113
+ IDENTIFIER = /[a-zA-Z_][a-zA-Z0-9_-]*/.freeze
114
+ META = Regexp.union(
115
+ %w"( ) { } [ ] $< < > $... $ ! ^ ... + * ?"
116
+ ).freeze
117
+ NUMBER = /-?\d+(?:\.\d+)?/.freeze
118
+ STRING = /".+?"/.freeze
119
+ METHOD_NAME = /\#?#{IDENTIFIER}[\!\?]?\(?/.freeze
120
+ PARAM_NUMBER = /%\d*/.freeze
121
+
122
+ SEPARATORS = /[\s]+/.freeze
123
+ TOKENS = Regexp.union(META, PARAM_NUMBER, NUMBER,
124
+ METHOD_NAME, SYMBOL, STRING)
125
+
126
+ TOKEN = /\G(?:#{SEPARATORS}|#{TOKENS}|.)/.freeze
127
+
128
+ NODE = /\A#{IDENTIFIER}\Z/.freeze
129
+ PREDICATE = /\A#{IDENTIFIER}\?\(?\Z/.freeze
130
+ WILDCARD = /\A_(?:#{IDENTIFIER})?\Z/.freeze
131
+
132
+ FUNCALL = /\A\##{METHOD_NAME}/.freeze
133
+ LITERAL = /\A(?:#{SYMBOL}|#{NUMBER}|#{STRING})\Z/.freeze
134
+ PARAM = /\A#{PARAM_NUMBER}\Z/.freeze
135
+ CLOSING = /\A(?:\)|\}|\])\Z/.freeze
136
+
137
+ REST = '...'
138
+ CAPTURED_REST = '$...'
139
+
140
+ attr_reader :match_code, :tokens, :captures
141
+
142
+ SEQ_HEAD_INDEX = -1
143
+
144
+ # Placeholders while compiling, see with_..._context methods
145
+ CUR_PLACEHOLDER = '@@@cur'
146
+ CUR_NODE = "#{CUR_PLACEHOLDER} node@@@"
147
+ CUR_ELEMENT = "#{CUR_PLACEHOLDER} element@@@"
148
+ SEQ_HEAD_GUARD = '@@@seq guard head@@@'
149
+
150
+ line = __LINE__
151
+ ANY_ORDER_TEMPLATE = ERB.new <<~RUBY.gsub("-%>\n", '%>')
152
+ <% if capture_rest %>(<%= capture_rest %> = []) && <% end -%>
153
+ <% if capture_all %>(<%= capture_all %> = <% end -%>
154
+ <%= CUR_NODE %>.children[<%= range %>]<% if capture_all %>)<% end -%>
155
+ .each_with_object({}) { |<%= child %>, <%= matched %>|
156
+ case
157
+ <% patterns.each_with_index do |pattern, i| -%>
158
+ when !<%= matched %>[<%= i %>] && <%=
159
+ with_context(pattern, child, use_temp_node: false)
160
+ %> then <%= matched %>[<%= i %>] = true
161
+ <% end -%>
162
+ <% if !rest %> else break({})
163
+ <% elsif capture_rest %> else <%= capture_rest %> << <%= child %>
164
+ <% end -%>
165
+ end
166
+ }.size == <%= patterns.size -%>
167
+ RUBY
168
+ ANY_ORDER_TEMPLATE.location = [__FILE__, line + 1]
169
+
170
+ line = __LINE__
171
+ REPEATED_TEMPLATE = ERB.new <<~RUBY.gsub("-%>\n", '%>')
172
+ <% if captured %>(<%= accumulate %> = Array.new) && <% end %>
173
+ <%= CUR_NODE %>.children[<%= range %>].all? do |<%= child %>|
174
+ <%= with_context(expr, child, use_temp_node: false) %><% if captured %>&&
175
+ <%= accumulate %>.push(<%= captured %>)<% end %>
176
+ end <% if captured %>&&
177
+ (<%= captured %> = if <%= accumulate %>.empty?
178
+ <%= captured %>.map{[]} # Transpose hack won't work for empty case
179
+ else
180
+ <%= accumulate %>.transpose
181
+ end) <% end -%>
182
+ RUBY
183
+ REPEATED_TEMPLATE.location = [__FILE__, line + 1]
184
+
185
+ def initialize(str, node_var = 'node0')
186
+ @string = str
187
+ @root = node_var
188
+
189
+ @temps = 0 # avoid name clashes between temp variables
190
+ @captures = 0 # number of captures seen
191
+ @unify = {} # named wildcard -> temp variable number
192
+ @params = 0 # highest % (param) number seen
193
+ run(node_var)
194
+ end
195
+
196
+ def run(node_var)
197
+ @tokens = Compiler.tokens(@string)
198
+
199
+ @match_code = with_context(compile_expr, node_var, use_temp_node: false)
200
+ @match_code.prepend("(captures = Array.new(#{@captures})) && ") \
201
+ if @captures.positive?
202
+
203
+ fail_due_to('unbalanced pattern') unless tokens.empty?
204
+ end
205
+
206
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
207
+ def compile_expr(token = tokens.shift)
208
+ # read a single pattern-matching expression from the token stream,
209
+ # return Ruby code which performs the corresponding matching operation
210
+ #
211
+ # the 'pattern-matching' expression may be a composite which
212
+ # contains an arbitrary number of sub-expressions, but that composite
213
+ # must all have precedence higher or equal to that of `&&`
214
+ #
215
+ # Expressions may use placeholders like:
216
+ # CUR_NODE: Ruby code that evaluates to an AST node
217
+ # CUR_ELEMENT: Either the node or the type if in first element of
218
+ # a sequence (aka seq_head, e.g. "(seq_head first_node_arg ...")
219
+ case token
220
+ when '(' then compile_seq
221
+ when '{' then compile_union
222
+ when '[' then compile_intersect
223
+ when '!' then compile_negation
224
+ when '$' then compile_capture
225
+ when '^' then compile_ascend
226
+ when WILDCARD then compile_wildcard(token[1..-1])
227
+ when FUNCALL then compile_funcall(token)
228
+ when LITERAL then compile_literal(token)
229
+ when PREDICATE then compile_predicate(token)
230
+ when NODE then compile_nodetype(token)
231
+ when PARAM then compile_param(token[1..-1])
232
+ when CLOSING then fail_due_to("#{token} in invalid position")
233
+ when nil then fail_due_to('pattern ended prematurely')
234
+ else fail_due_to("invalid token #{token.inspect}")
235
+ end
236
+ end
237
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
238
+
239
+ def tokens_until(stop, what)
240
+ return to_enum __method__, stop, what unless block_given?
241
+
242
+ fail_due_to("empty #{what}") if tokens.first == stop && what
243
+ yield until tokens.first == stop
244
+ tokens.shift
245
+ end
246
+
247
+ def compile_seq
248
+ terms = tokens_until(')', 'sequence').map { variadic_seq_term }
249
+ Sequence.new(self, *terms).compile
250
+ end
251
+
252
+ def compile_guard_clause
253
+ "#{CUR_NODE}.is_a?(RuboCop::AST::Node)"
254
+ end
255
+
256
+ def variadic_seq_term
257
+ token = tokens.shift
258
+ case token
259
+ when CAPTURED_REST then compile_captured_ellipsis
260
+ when REST then compile_ellipsis
261
+ when '$<' then compile_any_order(next_capture)
262
+ when '<' then compile_any_order
263
+ else compile_repeated_expr(token)
264
+ end
265
+ end
266
+
267
+ def compile_repeated_expr(token)
268
+ before = @captures
269
+ expr = compile_expr(token)
270
+ min, max = parse_repetition_token
271
+ return [1, expr] if min.nil?
272
+
273
+ if @captures != before
274
+ captured = "captures[#{before}...#{@captures}]"
275
+ accumulate = next_temp_variable(:accumulate)
276
+ end
277
+ arity = min..max || Float::INFINITY
278
+
279
+ [arity, repeated_generator(expr, captured, accumulate)]
280
+ end
281
+
282
+ def repeated_generator(expr, captured, accumulate)
283
+ with_temp_variables do |child|
284
+ lambda do |range|
285
+ if range.begin == SEQ_HEAD_INDEX
286
+ fail_due_to 'repeated pattern at beginning of sequence'
287
+ end
288
+ REPEATED_TEMPLATE.result(binding)
289
+ end
290
+ end
291
+ end
292
+
293
+ def parse_repetition_token
294
+ case tokens.first
295
+ when '*' then min = 0
296
+ when '+' then min = 1
297
+ when '?' then min = 0
298
+ max = 1
299
+ else return
300
+ end
301
+ tokens.shift
302
+ [min, max]
303
+ end
304
+
305
+ # @private
306
+ # Builds Ruby code for a sequence
307
+ # (head *first_terms variadic_term *last_terms)
308
+ class Sequence < SimpleDelegator
309
+ def initialize(compiler, *arity_term_list)
310
+ @arities, @terms = arity_term_list.transpose
311
+
312
+ super(compiler)
313
+ @variadic_index = @arities.find_index { |a| a.is_a?(Range) }
314
+ fail_due_to 'multiple variable patterns in same sequence' \
315
+ if @variadic_index && !@arities.one? { |a| a.is_a?(Range) }
316
+ end
317
+
318
+ def compile
319
+ [
320
+ compile_guard_clause,
321
+ compile_child_nb_guard,
322
+ compile_seq_head,
323
+ *compile_first_terms,
324
+ compile_variadic_term,
325
+ *compile_last_terms
326
+ ].compact.join(" &&\n") << SEQ_HEAD_GUARD
327
+ end
328
+
329
+ private
330
+
331
+ def first_terms_arity
332
+ first_terms_range { |r| @arities[r].inject(0, :+) } || 0
333
+ end
334
+
335
+ def last_terms_arity
336
+ last_terms_range { |r| @arities[r].inject(0, :+) } || 0
337
+ end
338
+
339
+ def variadic_term_min_arity
340
+ @variadic_index ? @arities[@variadic_index].begin : 0
341
+ end
342
+
343
+ def first_terms_range
344
+ yield 1..(@variadic_index || @terms.size) - 1 if seq_head?
345
+ end
346
+
347
+ def last_terms_range
348
+ yield @variadic_index + 1...@terms.size if @variadic_index
349
+ end
350
+
351
+ def seq_head?
352
+ @variadic_index != 0
353
+ end
354
+
355
+ def compile_child_nb_guard
356
+ fixed = first_terms_arity + last_terms_arity
357
+ min = fixed + variadic_term_min_arity
358
+ op = if @variadic_index
359
+ max_variadic = @arities[@variadic_index].end
360
+ if max_variadic != Float::INFINITY
361
+ range = min..fixed + max_variadic
362
+ return "(#{range}).cover?(#{CUR_NODE}.children.size)"
363
+ end
364
+ '>='
365
+ else
366
+ '=='
367
+ end
368
+ "#{CUR_NODE}.children.size #{op} #{min}"
369
+ end
370
+
371
+ def term(index, range)
372
+ t = @terms[index]
373
+ if t.respond_to? :call
374
+ t.call(range)
375
+ else
376
+ with_child_context(t, range.begin)
377
+ end
378
+ end
379
+
380
+ def compile_seq_head
381
+ return unless seq_head?
382
+
383
+ fail_due_to 'sequences can not start with <' \
384
+ if @terms[0].respond_to? :call
385
+
386
+ with_seq_head_context(@terms[0])
387
+ end
388
+
389
+ def compile_first_terms
390
+ first_terms_range { |range| compile_terms(range, 0) }
391
+ end
392
+
393
+ def compile_last_terms
394
+ last_terms_range { |r| compile_terms(r, -last_terms_arity) }
395
+ end
396
+
397
+ def compile_terms(index_range, start)
398
+ index_range.map do |i|
399
+ current = start
400
+ start += @arities.fetch(i)
401
+ term(i, current..start - 1)
402
+ end
403
+ end
404
+
405
+ def compile_variadic_term
406
+ variadic_arity { |arity| term(@variadic_index, arity) }
407
+ end
408
+
409
+ def variadic_arity
410
+ return unless @variadic_index
411
+
412
+ first = @variadic_index.positive? ? first_terms_arity : SEQ_HEAD_INDEX
413
+ yield first..-last_terms_arity - 1
414
+ end
415
+ end
416
+ private_constant :Sequence
417
+
418
+ def compile_captured_ellipsis
419
+ capture = next_capture
420
+ block = lambda { |range|
421
+ # Consider ($...) like (_ $...):
422
+ range = 0..range.end if range.begin == SEQ_HEAD_INDEX
423
+ "(#{capture} = #{CUR_NODE}.children[#{range}])"
424
+ }
425
+ [0..Float::INFINITY, block]
426
+ end
427
+
428
+ def compile_ellipsis
429
+ [0..Float::INFINITY, 'true']
430
+ end
431
+
432
+ # rubocop:disable Metrics/MethodLength
433
+ def compile_any_order(capture_all = nil)
434
+ rest = capture_rest = nil
435
+ patterns = []
436
+ with_temp_variables do |child, matched|
437
+ tokens_until('>', 'any child').each do
438
+ fail_due_to 'ellipsis must be at the end of <>' if rest
439
+ token = tokens.shift
440
+ case token
441
+ when CAPTURED_REST then rest = capture_rest = next_capture
442
+ when REST then rest = true
443
+ else patterns << compile_expr(token)
444
+ end
445
+ end
446
+ [rest ? patterns.size..Float::INFINITY : patterns.size,
447
+ ->(range) { ANY_ORDER_TEMPLATE.result(binding) }]
448
+ end
449
+ end
450
+ # rubocop:enable Metrics/MethodLength
451
+
452
+ def insure_same_captures(enum, what)
453
+ return to_enum __method__, enum, what unless block_given?
454
+
455
+ captures_before = captures_after = nil
456
+ enum.each do
457
+ captures_before ||= @captures
458
+ @captures = captures_before
459
+ yield
460
+ captures_after ||= @captures
461
+ if captures_after != @captures
462
+ fail_due_to("each #{what} must have same # of captures")
463
+ end
464
+ end
465
+ end
466
+
467
+ def compile_union
468
+ # we need to ensure that each branch of the {} contains the same
469
+ # number of captures (since only one branch of the {} can actually
470
+ # match, the same variables are used to hold the captures for each
471
+ # branch)
472
+ enum = tokens_until('}', 'union')
473
+ terms = insure_same_captures(enum, 'branch of {}')
474
+ .map { compile_expr }
475
+
476
+ "(#{terms.join(' || ')})"
477
+ end
478
+
479
+ def compile_intersect
480
+ tokens_until(']', 'intersection')
481
+ .map { compile_expr }
482
+ .join(' && ')
483
+ end
484
+
485
+ def compile_capture
486
+ "(#{next_capture} = #{CUR_ELEMENT}; #{compile_expr})"
487
+ end
488
+
489
+ def compile_negation
490
+ "!(#{compile_expr})"
491
+ end
492
+
493
+ def compile_ascend
494
+ with_context("#{CUR_NODE} && #{compile_expr}", "#{CUR_NODE}.parent")
495
+ end
496
+
497
+ def compile_wildcard(name)
498
+ if name.empty?
499
+ 'true'
500
+ elsif @unify.key?(name)
501
+ # we have already seen a wildcard with this name before
502
+ # so the value it matched the first time will already be stored
503
+ # in a temp. check if this value matches the one stored in the temp
504
+ "#{CUR_ELEMENT} == temp#{@unify[name]}"
505
+ else
506
+ n = @unify[name] = next_temp_value
507
+ # double assign to temp#{n} to avoid "assigned but unused variable"
508
+ "(temp#{n} = #{CUR_ELEMENT}; " \
509
+ "temp#{n} = temp#{n}; true)"
510
+ end
511
+ end
512
+
513
+ def compile_literal(literal)
514
+ "#{CUR_ELEMENT} == #{literal}"
515
+ end
516
+
517
+ def compile_predicate(predicate)
518
+ if predicate.end_with?('(') # is there an arglist?
519
+ args = compile_args(tokens)
520
+ predicate = predicate[0..-2] # drop the trailing (
521
+ "#{CUR_ELEMENT}.#{predicate}(#{args.join(',')})"
522
+ else
523
+ "#{CUR_ELEMENT}.#{predicate}"
524
+ end
525
+ end
526
+
527
+ def compile_funcall(method)
528
+ # call a method in the context which this pattern-matching
529
+ # code is used in. pass target value as an argument
530
+ method = method[1..-1] # drop the leading #
531
+ if method.end_with?('(') # is there an arglist?
532
+ args = compile_args(tokens)
533
+ method = method[0..-2] # drop the trailing (
534
+ "#{method}(#{CUR_ELEMENT},#{args.join(',')})"
535
+ else
536
+ "#{method}(#{CUR_ELEMENT})"
537
+ end
538
+ end
539
+
540
+ def compile_nodetype(type)
541
+ "#{compile_guard_clause} && #{CUR_NODE}.#{type.tr('-', '_')}_type?"
542
+ end
543
+
544
+ def compile_param(number)
545
+ "#{CUR_ELEMENT} == #{get_param(number)}"
546
+ end
547
+
548
+ def compile_args(tokens)
549
+ index = tokens.find_index { |token| token == ')' }
550
+
551
+ tokens.slice!(0..index).each_with_object([]) do |token, args|
552
+ next if [')', ','].include?(token)
553
+
554
+ args << compile_arg(token)
555
+ end
556
+ end
557
+
558
+ def compile_arg(token)
559
+ case token
560
+ when WILDCARD then
561
+ name = token[1..-1]
562
+ number = @unify[name] || fail_due_to('invalid in arglist: ' + token)
563
+ "temp#{number}"
564
+ when LITERAL then token
565
+ when PARAM then get_param(token[1..-1])
566
+ when CLOSING then fail_due_to("#{token} in invalid position")
567
+ when nil then fail_due_to('pattern ended prematurely')
568
+ else fail_due_to("invalid token in arglist: #{token.inspect}")
569
+ end
570
+ end
571
+
572
+ def next_capture
573
+ index = @captures
574
+ @captures += 1
575
+ "captures[#{index}]"
576
+ end
577
+
578
+ def get_param(number)
579
+ number = number.empty? ? 1 : Integer(number)
580
+ @params = number if number > @params
581
+ number.zero? ? @root : "param#{number}"
582
+ end
583
+
584
+ def emit_yield_capture(when_no_capture = '')
585
+ yield_val = if @captures.zero?
586
+ when_no_capture
587
+ elsif @captures == 1
588
+ 'captures[0]' # Circumvent https://github.com/jruby/jruby/issues/5710
589
+ else
590
+ '*captures'
591
+ end
592
+ "yield(#{yield_val})"
593
+ end
594
+
595
+ def emit_retval
596
+ if @captures.zero?
597
+ 'true'
598
+ elsif @captures == 1
599
+ 'captures[0]'
600
+ else
601
+ 'captures'
602
+ end
603
+ end
604
+
605
+ def emit_param_list
606
+ (1..@params).map { |n| "param#{n}" }.join(',')
607
+ end
608
+
609
+ def emit_trailing_params
610
+ params = emit_param_list
611
+ params.empty? ? '' : ",#{params}"
612
+ end
613
+
614
+ def emit_method_code
615
+ <<~RUBY
616
+ return unless #{@match_code}
617
+ block_given? ? #{emit_yield_capture} : (return #{emit_retval})
618
+ RUBY
619
+ end
620
+
621
+ def fail_due_to(message)
622
+ raise Invalid, "Couldn't compile due to #{message}. Pattern: #{@string}"
623
+ end
624
+
625
+ def with_temp_node(cur_node)
626
+ with_temp_variables do |node|
627
+ yield "(#{node} = #{cur_node})", node
628
+ end
629
+ .gsub("\n", "\n ") # Nicer indent for debugging
630
+ end
631
+
632
+ def with_temp_variables(&block)
633
+ names = block.parameters.map { |_, name| next_temp_variable(name) }
634
+ yield(*names)
635
+ end
636
+
637
+ def next_temp_variable(name)
638
+ "#{name}#{next_temp_value}"
639
+ end
640
+
641
+ def next_temp_value
642
+ @temps += 1
643
+ end
644
+
645
+ def auto_use_temp_node?(code)
646
+ code.scan(CUR_PLACEHOLDER).count > 1
647
+ end
648
+
649
+ # with_<...>_context methods are used whenever the context,
650
+ # i.e the current node or the current element can be determined.
651
+
652
+ def with_child_context(code, child_index)
653
+ with_context(code, "#{CUR_NODE}.children[#{child_index}]")
654
+ end
655
+
656
+ def with_context(code, cur_node,
657
+ use_temp_node: auto_use_temp_node?(code))
658
+ if use_temp_node
659
+ with_temp_node(cur_node) do |init, temp_var|
660
+ substitute_cur_node(code, temp_var, first_cur_node: init)
661
+ end
662
+ else
663
+ substitute_cur_node(code, cur_node)
664
+ end
665
+ end
666
+
667
+ def with_seq_head_context(code)
668
+ if code.include?(SEQ_HEAD_GUARD)
669
+ fail_due_to('parentheses at sequence head')
670
+ end
671
+
672
+ code.gsub CUR_ELEMENT, "#{CUR_NODE}.type"
673
+ end
674
+
675
+ def substitute_cur_node(code, cur_node, first_cur_node: cur_node)
676
+ iter = 0
677
+ code
678
+ .gsub(CUR_ELEMENT, CUR_NODE)
679
+ .gsub(CUR_NODE) do
680
+ iter += 1
681
+ iter == 1 ? first_cur_node : cur_node
682
+ end
683
+ .gsub(SEQ_HEAD_GUARD, '')
684
+ end
685
+
686
+ def self.tokens(pattern)
687
+ pattern.scan(TOKEN).reject { |token| token =~ /\A#{SEPARATORS}\Z/ }
688
+ end
689
+ end
690
+ private_constant :Compiler
691
+
692
+ # Helpers for defining methods based on a pattern string
693
+ module Macros
694
+ # Define a method which applies a pattern to an AST node
695
+ #
696
+ # The new method will return nil if the node does not match
697
+ # If the node matches, and a block is provided, the new method will
698
+ # yield to the block (passing any captures as block arguments).
699
+ # If the node matches, and no block is provided, the new method will
700
+ # return the captures, or `true` if there were none.
701
+ def def_node_matcher(method_name, pattern_str)
702
+ compiler = Compiler.new(pattern_str, 'node')
703
+ src = "def #{method_name}(node = self" \
704
+ "#{compiler.emit_trailing_params});" \
705
+ "#{compiler.emit_method_code};end"
706
+
707
+ location = caller_locations(1, 1).first
708
+ class_eval(src, location.path, location.lineno)
709
+ end
710
+
711
+ # Define a method which recurses over the descendants of an AST node,
712
+ # checking whether any of them match the provided pattern
713
+ #
714
+ # If the method name ends with '?', the new method will return `true`
715
+ # as soon as it finds a descendant which matches. Otherwise, it will
716
+ # yield all descendants which match.
717
+ def def_node_search(method_name, pattern_str)
718
+ compiler = Compiler.new(pattern_str, 'node')
719
+ called_from = caller(1..1).first.split(':')
720
+
721
+ if method_name.to_s.end_with?('?')
722
+ node_search_first(method_name, compiler, called_from)
723
+ else
724
+ node_search_all(method_name, compiler, called_from)
725
+ end
726
+ end
727
+
728
+ def node_search_first(method_name, compiler, called_from)
729
+ node_search(method_name, compiler, 'return true', '', called_from)
730
+ end
731
+
732
+ def node_search_all(method_name, compiler, called_from)
733
+ yield_code = compiler.emit_yield_capture('node')
734
+ prelude = "return enum_for(:#{method_name}, node0" \
735
+ "#{compiler.emit_trailing_params}) unless block_given?"
736
+
737
+ node_search(method_name, compiler, yield_code, prelude, called_from)
738
+ end
739
+
740
+ def node_search(method_name, compiler, on_match, prelude, called_from)
741
+ src = node_search_body(method_name, compiler.emit_trailing_params,
742
+ prelude, compiler.match_code, on_match)
743
+ filename, lineno = *called_from
744
+ class_eval(src, filename, lineno.to_i)
745
+ end
746
+
747
+ def node_search_body(method_name, trailing_params, prelude, match_code,
748
+ on_match)
749
+ <<~RUBY
750
+ def #{method_name}(node0#{trailing_params})
751
+ #{prelude}
752
+ node0.each_node do |node|
753
+ if #{match_code}
754
+ #{on_match}
755
+ end
756
+ end
757
+ nil
758
+ end
759
+ RUBY
760
+ end
761
+ end
762
+
763
+ attr_reader :pattern
764
+
765
+ def initialize(str)
766
+ @pattern = str
767
+ compiler = Compiler.new(str)
768
+ src = "def match(node0#{compiler.emit_trailing_params});" \
769
+ "#{compiler.emit_method_code}end"
770
+ instance_eval(src, __FILE__, __LINE__ + 1)
771
+ end
772
+
773
+ def match(*args)
774
+ # If we're here, it's because the singleton method has not been defined,
775
+ # either because we've been dup'ed or serialized through YAML
776
+ initialize(pattern)
777
+ match(*args)
778
+ end
779
+
780
+ def marshal_load(pattern)
781
+ initialize pattern
782
+ end
783
+
784
+ def marshal_dump
785
+ pattern
786
+ end
787
+
788
+ def ==(other)
789
+ other.is_a?(NodePattern) &&
790
+ Compiler.tokens(other.pattern) == Compiler.tokens(pattern)
791
+ end
792
+ alias eql? ==
793
+
794
+ def to_s
795
+ "#<#{self.class} #{pattern}>"
796
+ end
797
+ end
798
+ end
799
+ # rubocop:enable Metrics/ClassLength, Metrics/CyclomaticComplexity