leftovers 0.6.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (235) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +51 -3
  3. data/README.md +8 -7
  4. data/docs/Configuration.md +241 -105
  5. data/docs/Custom-Precompilers.md +44 -0
  6. data/leftovers.gemspec +3 -2
  7. data/lib/config/actioncable.yml +36 -4
  8. data/lib/config/actionmailbox.yml +28 -0
  9. data/lib/config/actionmailer.yml +92 -16
  10. data/lib/config/actionpack.yml +130 -32
  11. data/lib/config/actiontext.yml +56 -0
  12. data/lib/config/actionview.yml +194 -44
  13. data/lib/config/activejob.yml +15 -8
  14. data/lib/config/activemodel.yml +175 -18
  15. data/lib/config/activerecord.yml +397 -86
  16. data/lib/config/activestorage.yml +26 -0
  17. data/lib/config/activesupport.yml +168 -25
  18. data/lib/config/haml.yml +4 -2
  19. data/lib/config/leftovers.yml +48 -0
  20. data/lib/config/rails.yml +8 -4
  21. data/lib/config/railties.yml +18 -0
  22. data/lib/config/ruby.yml +473 -57
  23. data/lib/config/slim.yml +4 -2
  24. data/lib/config/test-unit.yml +8 -0
  25. data/lib/leftovers/ast/array_node.rb +12 -0
  26. data/lib/leftovers/ast/block_node.rb +12 -0
  27. data/lib/leftovers/ast/builder.rb +24 -5
  28. data/lib/leftovers/ast/casgn_node.rb +20 -0
  29. data/lib/leftovers/ast/const_node.rb +15 -0
  30. data/lib/leftovers/ast/def_node.rb +15 -0
  31. data/lib/leftovers/ast/defs_node.rb +15 -0
  32. data/lib/leftovers/ast/false_node.rb +24 -0
  33. data/lib/leftovers/ast/has_arguments.rb +31 -0
  34. data/lib/leftovers/ast/hash_node.rb +17 -0
  35. data/lib/leftovers/ast/module_node.rb +16 -0
  36. data/lib/leftovers/ast/nil_node.rb +23 -0
  37. data/lib/leftovers/ast/node.rb +39 -91
  38. data/lib/leftovers/ast/numeric_node.rb +22 -0
  39. data/lib/leftovers/ast/send_node.rb +25 -0
  40. data/lib/leftovers/ast/str_node.rb +24 -0
  41. data/lib/leftovers/ast/sym_node.rb +25 -0
  42. data/lib/leftovers/ast/true_node.rb +24 -0
  43. data/lib/leftovers/ast/var_node.rb +14 -0
  44. data/lib/leftovers/ast/vasgn_node.rb +20 -0
  45. data/lib/leftovers/ast.rb +18 -0
  46. data/lib/leftovers/cli.rb +12 -1
  47. data/lib/leftovers/collector.rb +2 -1
  48. data/lib/leftovers/comparable_instance.rb +18 -0
  49. data/lib/leftovers/config.rb +3 -42
  50. data/lib/leftovers/config_loader/argument_position_schema.rb +13 -0
  51. data/lib/leftovers/config_loader/argumentless_transform_schema.rb +21 -0
  52. data/lib/leftovers/config_loader/array_schema.rb +53 -0
  53. data/lib/leftovers/config_loader/attribute.rb +56 -0
  54. data/lib/leftovers/config_loader/built_in_precompiler_schema.rb +13 -0
  55. data/lib/leftovers/config_loader/document_schema.rb +49 -0
  56. data/lib/leftovers/config_loader/dynamic_schema.rb +18 -0
  57. data/lib/leftovers/config_loader/has_argument_schema.rb +13 -0
  58. data/lib/leftovers/config_loader/has_value_schema.rb +22 -0
  59. data/lib/leftovers/config_loader/inherit_schema_attributes.rb +22 -0
  60. data/lib/leftovers/config_loader/keep_test_only_schema.rb +13 -0
  61. data/lib/leftovers/config_loader/keyword_argument_schema.rb +13 -0
  62. data/lib/leftovers/config_loader/node.rb +106 -0
  63. data/lib/leftovers/config_loader/object_schema.rb +117 -0
  64. data/lib/leftovers/config_loader/precompile_schema.rb +12 -0
  65. data/lib/leftovers/config_loader/precompiler_schema.rb +11 -0
  66. data/lib/leftovers/config_loader/privacy_processor_schema.rb +10 -0
  67. data/lib/leftovers/config_loader/privacy_schema.rb +15 -0
  68. data/lib/leftovers/config_loader/regexp_schema.rb +27 -0
  69. data/lib/leftovers/config_loader/require_schema.rb +11 -0
  70. data/lib/leftovers/config_loader/rule_pattern_schema.rb +20 -0
  71. data/lib/leftovers/config_loader/scalar_argument_schema.rb +14 -0
  72. data/lib/leftovers/config_loader/scalar_value_schema.rb +22 -0
  73. data/lib/leftovers/config_loader/schema.rb +33 -0
  74. data/lib/leftovers/config_loader/string_enum_schema.rb +62 -0
  75. data/lib/leftovers/config_loader/string_pattern_schema.rb +14 -0
  76. data/lib/leftovers/config_loader/string_schema.rb +14 -0
  77. data/lib/leftovers/config_loader/string_value_processor_schema.rb +11 -0
  78. data/lib/leftovers/config_loader/suggester.rb +22 -0
  79. data/lib/leftovers/config_loader/transform_schema.rb +32 -0
  80. data/lib/leftovers/config_loader/true_schema.rb +18 -0
  81. data/lib/leftovers/config_loader/value_matcher_condition_schema.rb +13 -0
  82. data/lib/leftovers/config_loader/value_matcher_schema.rb +21 -0
  83. data/lib/leftovers/config_loader/value_or_array_schema.rb +34 -0
  84. data/lib/leftovers/config_loader/value_or_object_schema.rb +40 -0
  85. data/lib/leftovers/config_loader/value_processor_schema.rb +14 -0
  86. data/lib/leftovers/config_loader/value_type_schema.rb +17 -0
  87. data/lib/leftovers/config_loader.rb +93 -0
  88. data/lib/leftovers/definition_collection.rb +37 -0
  89. data/lib/leftovers/definition_node.rb +6 -17
  90. data/lib/leftovers/definition_node_set.rb +19 -0
  91. data/lib/leftovers/definition_to_add.rb +31 -0
  92. data/lib/leftovers/file.rb +13 -55
  93. data/lib/leftovers/file_collector/comments_processor.rb +57 -0
  94. data/lib/leftovers/file_collector/node_processor.rb +131 -0
  95. data/lib/leftovers/file_collector.rb +62 -202
  96. data/lib/leftovers/file_list.rb +3 -2
  97. data/lib/leftovers/matcher_builders/and.rb +26 -9
  98. data/lib/leftovers/matcher_builders/and_not.rb +7 -5
  99. data/lib/leftovers/matcher_builders/name.rb +18 -17
  100. data/lib/leftovers/matcher_builders/node.rb +60 -30
  101. data/lib/leftovers/matcher_builders/node_has_argument.rb +48 -52
  102. data/lib/leftovers/matcher_builders/node_has_keyword_argument.rb +16 -11
  103. data/lib/leftovers/matcher_builders/node_has_positional_argument.rb +29 -15
  104. data/lib/leftovers/matcher_builders/node_pair_key.rb +16 -0
  105. data/lib/leftovers/matcher_builders/node_privacy.rb +13 -0
  106. data/lib/leftovers/matcher_builders/node_type.rb +11 -11
  107. data/lib/leftovers/matcher_builders/node_value.rb +44 -25
  108. data/lib/leftovers/matcher_builders/or.rb +64 -49
  109. data/lib/leftovers/matcher_builders/path.rb +3 -1
  110. data/lib/leftovers/matcher_builders/string_pattern.rb +14 -5
  111. data/lib/leftovers/matcher_builders.rb +2 -1
  112. data/lib/leftovers/matchers/all.rb +4 -0
  113. data/lib/leftovers/matchers/and.rb +4 -0
  114. data/lib/leftovers/matchers/any.rb +2 -0
  115. data/lib/leftovers/matchers/node_has_any_keyword_argument.rb +7 -4
  116. data/lib/leftovers/matchers/node_has_any_positional_argument_with_value.rb +5 -4
  117. data/lib/leftovers/matchers/node_has_positional_argument.rb +5 -1
  118. data/lib/leftovers/matchers/node_has_positional_argument_with_value.rb +6 -1
  119. data/lib/leftovers/matchers/node_has_receiver.rb +4 -0
  120. data/lib/leftovers/matchers/node_is_proc.rb +13 -0
  121. data/lib/leftovers/matchers/node_name.rb +9 -3
  122. data/lib/leftovers/matchers/node_pair_key.rb +23 -0
  123. data/lib/leftovers/matchers/node_pair_value.rb +7 -3
  124. data/lib/leftovers/matchers/node_path.rb +7 -3
  125. data/lib/leftovers/matchers/node_privacy.rb +23 -0
  126. data/lib/leftovers/matchers/node_scalar_value.rb +6 -1
  127. data/lib/leftovers/matchers/node_type.rb +7 -3
  128. data/lib/leftovers/matchers/not.rb +2 -0
  129. data/lib/leftovers/matchers/or.rb +2 -0
  130. data/lib/leftovers/matchers/path.rb +21 -0
  131. data/lib/leftovers/matchers.rb +4 -1
  132. data/lib/leftovers/merged_config.rb +40 -75
  133. data/lib/leftovers/parser.rb +7 -4
  134. data/lib/leftovers/precompilers/erb.rb +22 -0
  135. data/lib/leftovers/precompilers/haml.rb +15 -0
  136. data/lib/leftovers/precompilers/json.rb +27 -0
  137. data/lib/leftovers/precompilers/precompiler.rb +28 -0
  138. data/lib/leftovers/precompilers/slim.rb +15 -0
  139. data/lib/leftovers/precompilers/yaml.rb +75 -0
  140. data/lib/leftovers/precompilers.rb +50 -0
  141. data/lib/leftovers/processor_builders/action.rb +69 -42
  142. data/lib/leftovers/processor_builders/add_prefix.rb +18 -10
  143. data/lib/leftovers/processor_builders/add_suffix.rb +18 -10
  144. data/lib/leftovers/processor_builders/argument.rb +28 -14
  145. data/lib/leftovers/processor_builders/dynamic.rb +57 -17
  146. data/lib/leftovers/processor_builders/each.rb +83 -11
  147. data/lib/leftovers/processor_builders/itself.rb +2 -2
  148. data/lib/leftovers/processor_builders/keyword.rb +9 -9
  149. data/lib/leftovers/processor_builders/keyword_argument.rb +4 -2
  150. data/lib/leftovers/processor_builders/receiver.rb +13 -0
  151. data/lib/leftovers/processor_builders/transform.rb +55 -44
  152. data/lib/leftovers/processor_builders/transform_chain.rb +16 -8
  153. data/lib/leftovers/processor_builders/transform_set.rb +16 -32
  154. data/lib/leftovers/processor_builders/value.rb +4 -4
  155. data/lib/leftovers/processor_builders.rb +1 -3
  156. data/lib/leftovers/processors/add_call.rb +14 -0
  157. data/lib/leftovers/processors/add_definition_node.rb +16 -0
  158. data/lib/leftovers/processors/add_dynamic_prefix.rb +29 -0
  159. data/lib/leftovers/processors/add_dynamic_suffix.rb +29 -0
  160. data/lib/leftovers/{value_processors → processors}/add_prefix.rb +7 -3
  161. data/lib/leftovers/{value_processors → processors}/add_suffix.rb +7 -3
  162. data/lib/leftovers/processors/append_sym.rb +13 -0
  163. data/lib/leftovers/{value_processors → processors}/camelize.rb +7 -3
  164. data/lib/leftovers/{value_processors → processors}/capitalize.rb +7 -3
  165. data/lib/leftovers/{value_processors → processors}/deconstantize.rb +7 -3
  166. data/lib/leftovers/{value_processors → processors}/delete_after.rb +9 -5
  167. data/lib/leftovers/processors/delete_after_last.rb +26 -0
  168. data/lib/leftovers/processors/delete_before.rb +27 -0
  169. data/lib/leftovers/processors/delete_before_last.rb +27 -0
  170. data/lib/leftovers/{value_processors → processors}/delete_prefix.rb +7 -3
  171. data/lib/leftovers/{value_processors → processors}/delete_suffix.rb +7 -3
  172. data/lib/leftovers/{value_processors → processors}/demodulize.rb +7 -3
  173. data/lib/leftovers/{value_processors → processors}/downcase.rb +7 -3
  174. data/lib/leftovers/processors/each.rb +25 -0
  175. data/lib/leftovers/processors/each_for_definition_set.rb +33 -0
  176. data/lib/leftovers/processors/each_keyword.rb +29 -0
  177. data/lib/leftovers/processors/each_keyword_argument.rb +29 -0
  178. data/lib/leftovers/processors/each_positional_argument.rb +27 -0
  179. data/lib/leftovers/processors/each_positional_argument_from.rb +30 -0
  180. data/lib/leftovers/processors/eval.rb +16 -0
  181. data/lib/leftovers/processors/itself.rb +21 -0
  182. data/lib/leftovers/{value_processors → processors}/keyword_argument.rb +9 -11
  183. data/lib/leftovers/processors/match_current_node.rb +26 -0
  184. data/lib/leftovers/processors/match_matched_node.rb +26 -0
  185. data/lib/leftovers/{value_processors → processors}/parameterize.rb +7 -3
  186. data/lib/leftovers/{value_processors → processors}/placeholder.rb +5 -4
  187. data/lib/leftovers/{value_processors → processors}/pluralize.rb +7 -3
  188. data/lib/leftovers/{value_processors → processors}/positional_argument.rb +8 -6
  189. data/lib/leftovers/processors/receiver.rb +24 -0
  190. data/lib/leftovers/{value_processors → processors}/replace_value.rb +7 -3
  191. data/lib/leftovers/processors/set_default_privacy.rb +21 -0
  192. data/lib/leftovers/processors/set_privacy.rb +23 -0
  193. data/lib/leftovers/{value_processors → processors}/singularize.rb +7 -3
  194. data/lib/leftovers/{value_processors → processors}/split.rb +8 -4
  195. data/lib/leftovers/{value_processors → processors}/swapcase.rb +7 -3
  196. data/lib/leftovers/{value_processors → processors}/titleize.rb +7 -3
  197. data/lib/leftovers/{value_processors → processors}/underscore.rb +7 -3
  198. data/lib/leftovers/{value_processors → processors}/upcase.rb +7 -3
  199. data/lib/leftovers/processors.rb +49 -0
  200. data/lib/leftovers/rake_task.rb +1 -1
  201. data/lib/leftovers/version.rb +1 -1
  202. data/lib/leftovers.rb +55 -20
  203. metadata +159 -72
  204. data/lib/leftovers/config_validator/error_processor.rb +0 -196
  205. data/lib/leftovers/config_validator/schema_hash.rb +0 -551
  206. data/lib/leftovers/config_validator.rb +0 -61
  207. data/lib/leftovers/dynamic_processors/call.rb +0 -21
  208. data/lib/leftovers/dynamic_processors/call_definition.rb +0 -27
  209. data/lib/leftovers/dynamic_processors/definition.rb +0 -22
  210. data/lib/leftovers/dynamic_processors/each.rb +0 -19
  211. data/lib/leftovers/dynamic_processors/null.rb +0 -9
  212. data/lib/leftovers/dynamic_processors.rb +0 -11
  213. data/lib/leftovers/erb.rb +0 -20
  214. data/lib/leftovers/haml.rb +0 -21
  215. data/lib/leftovers/json.rb +0 -28
  216. data/lib/leftovers/matcher_builders/node_pair_name.rb +0 -18
  217. data/lib/leftovers/matchers/predicate.rb +0 -19
  218. data/lib/leftovers/processor_builders/each_action.rb +0 -51
  219. data/lib/leftovers/processor_builders/each_dynamic.rb +0 -54
  220. data/lib/leftovers/processor_builders/each_for_definition_set.rb +0 -36
  221. data/lib/leftovers/slim.rb +0 -21
  222. data/lib/leftovers/value_processors/add_dynamic_prefix.rb +0 -31
  223. data/lib/leftovers/value_processors/add_dynamic_suffix.rb +0 -31
  224. data/lib/leftovers/value_processors/delete_before.rb +0 -22
  225. data/lib/leftovers/value_processors/each.rb +0 -21
  226. data/lib/leftovers/value_processors/each_for_definition_set.rb +0 -29
  227. data/lib/leftovers/value_processors/each_keyword.rb +0 -27
  228. data/lib/leftovers/value_processors/each_keyword_argument.rb +0 -27
  229. data/lib/leftovers/value_processors/each_positional_argument.rb +0 -24
  230. data/lib/leftovers/value_processors/itself.rb +0 -17
  231. data/lib/leftovers/value_processors/keyword.rb +0 -32
  232. data/lib/leftovers/value_processors/return_definition.rb +0 -22
  233. data/lib/leftovers/value_processors/return_string.rb +0 -14
  234. data/lib/leftovers/value_processors.rb +0 -40
  235. data/lib/leftovers/yaml.rb +0 -73
@@ -0,0 +1,57 @@
1
+ # frozen-string-literal: true
2
+
3
+ module Leftovers
4
+ class FileCollector
5
+ module CommentsProcessor
6
+ METHOD_NAME_RE = /[[:alpha:]_][[:alnum:]_]*\b[?!=]?/.freeze
7
+ NON_ALNUM_METHOD_NAME_RE = Regexp.union(%w{
8
+ []= [] ** ~ +@ -@ * / % + - >> << &
9
+ ^ | <=> <= >= < > === == != =~ !~ !
10
+ }.map { |op| /#{Regexp.escape(op)}/ })
11
+ CONSTANT_NAME_RE = /[[:upper:]][[:alnum:]_]*\b/.freeze
12
+ NAME_RE = Regexp.union(METHOD_NAME_RE, NON_ALNUM_METHOD_NAME_RE, CONSTANT_NAME_RE)
13
+ NAME_LIST_RE = /#{NAME_RE}(?:[, :]+#{NAME_RE})*/.freeze
14
+ LEFTOVERS_CALL_RE = /\bleftovers:call(?:s|e(?:d|rs?))? (#{NAME_LIST_RE})/.freeze
15
+ LEFTOVERS_ALLOW_RE = /\bleftovers:(?:keeps?|skip(?:s|ped|)|allow(?:s|ed|))\b/.freeze
16
+ LEFTOVERS_TEST_RE = /\bleftovers:(?:for_tests?|tests?|testing|test_only)\b/.freeze
17
+ LEFTOVERS_DYNAMIC_RE = /\bleftovers:dynamic[: ](#{NAME_LIST_RE})/.freeze
18
+
19
+ class << self
20
+ def process(comments, collector)
21
+ comments.each do |comment|
22
+ process_leftovers_keep_comment(comment, collector)
23
+ process_leftovers_test_comment(comment, collector)
24
+ process_leftovers_dynamic_comment(comment, collector)
25
+ process_leftovers_call_comment(comment, collector)
26
+ end
27
+ end
28
+
29
+ def process_leftovers_keep_comment(comment, collector)
30
+ return unless comment.text.match?(LEFTOVERS_ALLOW_RE)
31
+
32
+ collector.allow_lines << comment.loc.line
33
+ end
34
+
35
+ def process_leftovers_test_comment(comment, collector)
36
+ return unless comment.text.match?(LEFTOVERS_TEST_RE)
37
+
38
+ collector.test_lines << comment.loc.line
39
+ end
40
+
41
+ def process_leftovers_dynamic_comment(comment, collector)
42
+ match = comment.text.match(LEFTOVERS_DYNAMIC_RE)
43
+ return unless match
44
+
45
+ collector.dynamic_lines[comment.loc.line] = match[1].scan(NAME_RE)
46
+ end
47
+
48
+ def process_leftovers_call_comment(comment, collector)
49
+ match = comment.text.match(LEFTOVERS_CALL_RE)
50
+ return unless match
51
+
52
+ match[1].scan(NAME_RE).each { |s| collector.calls << s.to_sym }
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,131 @@
1
+ # frozen-string-literal: true
2
+
3
+ require 'parser'
4
+
5
+ module Leftovers
6
+ class FileCollector
7
+ class NodeProcessor < ::Parser::AST::Processor
8
+ def initialize(collector) # rubocop:disable Lint/MissingSuper # there isn't one to call
9
+ @collector = collector
10
+ end
11
+
12
+ def on_def(node)
13
+ node.privacy = @collector.default_method_privacy
14
+ @collector.add_definition(node)
15
+ super
16
+ end
17
+
18
+ def on_defs(node)
19
+ @collector.add_definition(node)
20
+ super
21
+ end
22
+
23
+ def on_ivasgn(node)
24
+ @collector.collect_variable_assign(node)
25
+ super
26
+ end
27
+
28
+ def on_gvasgn(node)
29
+ @collector.collect_variable_assign(node)
30
+ super
31
+ end
32
+
33
+ def on_cvasgn(node)
34
+ @collector.collect_variable_assign(node)
35
+ super
36
+ end
37
+
38
+ def on_ivar(node)
39
+ @collector.calls << node.name
40
+ super
41
+ end
42
+
43
+ def on_gvar(node)
44
+ @collector.calls << node.name
45
+ super
46
+ end
47
+
48
+ def on_cvar(node)
49
+ @collector.calls << node.name
50
+ super
51
+ end
52
+
53
+ def on_op_asgn(node)
54
+ @collector.collect_op_asgn(node)
55
+ super
56
+ end
57
+
58
+ def on_and_asgn(node)
59
+ @collector.collect_op_asgn(node)
60
+ super
61
+ end
62
+
63
+ def on_or_asgn(node)
64
+ @collector.collect_op_asgn(node)
65
+ super
66
+ end
67
+
68
+ def on_send(node)
69
+ super
70
+ @collector.collect_send(node)
71
+ end
72
+
73
+ def on_csend(node)
74
+ super
75
+ @collector.collect_send(node)
76
+ end
77
+
78
+ def on_const(node)
79
+ super
80
+ @collector.calls << node.name
81
+ end
82
+
83
+ def on_array(node)
84
+ super
85
+ @collector.collect_commented_dynamic(node)
86
+ end
87
+
88
+ def on_hash(node)
89
+ super
90
+ @collector.collect_commented_dynamic(node)
91
+ end
92
+
93
+ # grab e.g. :to_s in each(&:to_s)
94
+ def on_block_pass(node)
95
+ super
96
+ return unless node.first.sym?
97
+
98
+ @collector.calls << node.first.to_sym
99
+ end
100
+
101
+ # grab class Constant or module Constant
102
+ def on_class(node)
103
+ # don't call super so we don't interpret the class name as being called by its definition
104
+ process_all(node.children.drop(1))
105
+
106
+ node = node.children.first
107
+
108
+ @collector.add_definition(node)
109
+ end
110
+ alias_method :on_module, :on_class
111
+
112
+ # grab Constant = Class.new or CONSTANT = 'string'.freeze
113
+ def on_casgn(node)
114
+ super
115
+
116
+ @collector.add_definition(node)
117
+ @collector.collect_dynamic(node)
118
+ end
119
+
120
+ # grab calls to `alias new_method original_method`
121
+ def on_alias(node)
122
+ super
123
+ new_method, original_method = node.children
124
+ @collector.add_definition(
125
+ new_method, name: new_method.children.first, loc: new_method.loc.expression
126
+ )
127
+ @collector.calls << original_method.children.first
128
+ end
129
+ end
130
+ end
131
+ end
@@ -1,20 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'set'
4
- require 'parser'
5
4
 
6
5
  module Leftovers
7
- class FileCollector < ::Parser::AST::Processor # rubocop:disable Metrics/ClassLength
8
- attr_reader :calls, :definitions
6
+ class FileCollector
7
+ autoload(:CommentsProcessor, "#{__dir__}/file_collector/comments_processor.rb")
8
+ autoload(:NodeProcessor, "#{__dir__}/file_collector/node_processor.rb")
9
9
 
10
- def initialize(ruby, file) # rubocop:disable Lint/MissingSuper
10
+ attr_reader :calls, :allow_lines, :test_lines, :dynamic_lines
11
+ attr_accessor :default_method_privacy
12
+
13
+ def initialize(ruby, file)
11
14
  @calls = []
12
- @definitions = []
15
+ @definition_collection = Leftovers::DefinitionCollection.new
13
16
  @allow_lines = Set.new.compare_by_identity
14
17
  @test_lines = Set.new.compare_by_identity
15
18
  @dynamic_lines = {}
16
19
  @ruby = ruby
17
20
  @file = file
21
+ @default_method_privacy = :public
18
22
  end
19
23
 
20
24
  def filename
@@ -22,247 +26,103 @@ module Leftovers
22
26
  end
23
27
 
24
28
  def to_h
25
- squash!
26
-
27
- {
28
- test?: @file.test?,
29
- calls: calls,
30
- definitions: definitions
31
- }
29
+ { test?: @file.test?, calls: squash!(calls), definitions: squash!(definitions) }
32
30
  end
33
31
 
34
- def squash!
35
- calls.flatten!
36
- calls.compact!
37
- calls.uniq!
38
- definitions.flatten!
39
- definitions.compact!
40
- definitions.uniq!
41
- definitions.reject! { |v| v == :keep }
32
+ def squash!(list)
33
+ list.flatten!
34
+ list.compact!
35
+ list.uniq!
36
+ list
42
37
  end
43
38
 
44
- def collect
45
- ast, comments = Leftovers::Parser.parse_with_comments(@ruby, @file.relative_path)
46
- process_comments(comments)
47
- process(ast)
39
+ def collect(ruby = @ruby, line = 1)
40
+ ast, comments = Leftovers::Parser.parse_with_comments(ruby, @file.relative_path, line)
41
+ CommentsProcessor.process(comments, self)
42
+ NodeProcessor.new(self).process(ast)
48
43
  rescue ::Parser::SyntaxError => e
49
- Leftovers.warn "\e[31m#{filename}:#{e.diagnostic.location.line}:#{e.diagnostic.location.column} SyntaxError: #{e.message}\e[0m" # rubocop:disable Layout/LineLength
50
- end
51
-
52
- METHOD_NAME_RE = /[[:alpha:]_][[:alnum:]_]*\b[?!=]?/.freeze
53
- NON_ALNUM_METHOD_NAME_RE = Regexp.union(%w{
54
- []= [] ** ~ +@ -@ * / % + - >> << &
55
- ^ | <=> <= >= < > === == != =~ !~ !
56
- }.map { |op| /#{Regexp.escape(op)}/ })
57
- CONSTANT_NAME_RE = /[[:upper:]][[:alnum:]_]*\b/.freeze
58
- NAME_RE = Regexp.union(METHOD_NAME_RE, NON_ALNUM_METHOD_NAME_RE, CONSTANT_NAME_RE)
59
- LEFTOVERS_CALL_RE = /\bleftovers:call(?:s|ed|er|ers|) (#{NAME_RE}(?:[, :]+#{NAME_RE})*)/.freeze
60
- LEFTOVERS_ALLOW_RE = /\bleftovers:(?:keeps?|skip(?:s|ped|)|allow(?:s|ed|))\b/.freeze
61
- LEFTOVERS_TEST_RE = /\bleftovers:(?:for_tests?|tests?|testing|test_only)\b/.freeze
62
- LEFTOVERS_DYNAMIC_RE = /\bleftovers:dynamic:(#{NAME_RE})\b/.freeze
63
-
64
- def process_comments(comments) # rubocop:disable Metrics/AbcSize
65
- comments.each do |comment|
66
- @allow_lines << comment.loc.line if comment.text.match?(LEFTOVERS_ALLOW_RE)
67
- @test_lines << comment.loc.line if comment.text.match?(LEFTOVERS_TEST_RE)
68
- dynamic_match = comment.text.match(LEFTOVERS_DYNAMIC_RE)
69
- @dynamic_lines[comment.loc.line] = dynamic_match[1] if dynamic_match
70
-
71
- next unless (call_match = comment.text.match(LEFTOVERS_CALL_RE))
72
-
73
- call_match[1].scan(NAME_RE).each { |s| add_call(s.to_sym) }
74
- end
75
- end
76
-
77
- # grab method definitions
78
- def on_def(node)
79
- add_definition(node)
80
- super
81
- end
82
-
83
- def on_ivasgn(node)
84
- collect_variable_assign(node)
85
- super
86
- end
87
-
88
- def on_gvasgn(node)
89
- collect_variable_assign(node)
90
- super
91
- end
92
-
93
- def on_cvasgn(node)
94
- collect_variable_assign(node)
95
- super
96
- end
97
-
98
- def on_ivar(node)
99
- add_call(node.name)
100
- super
101
- end
102
-
103
- def on_gvar(node)
104
- add_call(node.name)
105
- super
106
- end
107
-
108
- def on_cvar(node)
109
- add_call(node.name)
110
- super
111
- end
112
-
113
- def on_op_asgn(node)
114
- collect_op_asgn(node)
115
- super
116
- end
117
-
118
- def on_and_asgn(node)
119
- collect_op_asgn(node)
120
- super
121
- end
122
-
123
- def on_or_asgn(node)
124
- collect_op_asgn(node)
125
- super
126
- end
127
-
128
- # grab method calls
129
- def on_send(node)
130
- super
131
- collect_send(node)
132
- end
133
-
134
- def on_csend(node)
135
- super
136
- collect_send(node)
137
- end
138
-
139
- def on_const(node)
140
- super
141
- add_call(node.name)
44
+ Leftovers.warn(
45
+ "\e[31m#{filename}:#{e.diagnostic.location.line}:#{e.diagnostic.location.column} " \
46
+ "SyntaxError: #{e.message}\e[0m"
47
+ )
142
48
  end
143
49
 
144
- def on_array(node)
145
- super
146
- collect_commented_dynamic(node)
50
+ def collect_subfile(string, location)
51
+ string = (' ' * location.column) + string # match indentation
52
+ collect(string, location.line)
147
53
  end
148
54
 
149
- def on_hash(node)
150
- super
151
- collect_commented_dynamic(node)
55
+ def test_line?(line)
56
+ @file.test? || @test_lines.include?(line)
152
57
  end
153
58
 
154
- # grab e.g. :to_s in each(&:to_s)
155
- def on_block_pass(node)
156
- super
157
- add_call(node.children.first.to_sym) if node.children.first.string_or_symbol?
59
+ def keep_line?(line)
60
+ @allow_lines.include?(line)
158
61
  end
159
62
 
160
- # grab class Constant or module Constant
161
- def on_class(node)
162
- # don't call super so we don't process the class name
163
- # !!! (# wtf does this mean dana? what would happen instead?)
164
- process_all(node.children.drop(1))
165
-
166
- node = node.children.first
167
-
168
- add_definition(node)
169
- end
170
- alias_method :on_module, :on_class
171
-
172
- # grab Constant = Class.new or CONSTANT = 'string'.freeze
173
- def on_casgn(node)
174
- super
175
- add_definition(node)
176
- collect_dynamic(node)
63
+ def add_definition_set(definition_node_set)
64
+ @definition_collection.add_definition_set(definition_node_set)
177
65
  end
178
66
 
179
- # grab calls to `alias new_method original_method`
180
- def on_alias(node)
181
- super
182
- new_method, original_method = node.children
183
- add_definition(new_method, name: new_method.children.first, loc: new_method.loc.expression)
184
- add_call(original_method.children.first)
185
- end
186
-
187
- private
188
-
189
- def test_line?(loc)
190
- @file.test? ||
191
- @test_lines.include?(loc.line)
192
- end
193
-
194
- def test_node?(node, loc)
195
- test_line?(loc) || ::Leftovers.config.test_only === node
67
+ def set_privacy(node, to)
68
+ @definition_collection.set_privacy(node, to)
196
69
  end
197
70
 
198
71
  def add_definition(node, name: node.name, loc: node.loc.name)
199
- return if @allow_lines.include?(loc.line)
200
- return if Leftovers.config.keep === node
72
+ @definition_collection.add(node, name: name, loc: loc)
73
+ end
201
74
 
202
- definitions << Leftovers::Definition.new(name, location: loc, test: test_node?(node, loc))
75
+ def add_definition_node(definition_node)
76
+ @definition_collection.add_definition_node(definition_node)
203
77
  end
204
78
 
205
- def add_call(name)
206
- calls << name
79
+ def definitions
80
+ @definitions ||= @definition_collection.to_definitions(self)
207
81
  end
208
82
 
209
83
  def collect_send(node)
210
- add_call(node.name)
84
+ calls << node.name
211
85
  collect_dynamic(node)
212
86
  end
213
87
 
214
- # just collects the call, super will collect the definition
215
- def collect_var_op_asgn(node)
216
- name = node.children.first
217
-
218
- add_call(name)
219
- end
220
-
221
- def collect_send_op_asgn(node)
222
- name = node.children[1]
223
-
224
- add_call(:"#{name}=")
225
- end
226
-
227
88
  def collect_variable_assign(node)
228
89
  add_definition(node)
229
-
230
90
  collect_dynamic(node)
231
91
  end
232
92
 
233
93
  def collect_op_asgn(node)
234
- node = node.children.first
235
- # :nocov: # don't need else, it's exhaustive for callers
94
+ node = node.first
236
95
  case node.type
96
+ # just collects the :call=, super will collect the :call
97
+ when :send, :csend then calls << :"#{node.name}="
98
+ # just collects the call, super will collect the definition
99
+ when :ivasgn, :gvasgn, :cvasgn then calls << node.name
100
+ when :lvasgn then nil # we don't care about lvasgn
237
101
  # :nocov:
238
- when :send then collect_send_op_asgn(node)
239
- when :ivasgn, :gvasgn, :cvasgn then collect_var_op_asgn(node)
102
+ else raise Leftovers::UnexpectedCase, "Unhandled value #{node.type.inspect}"
103
+ # :nocov:
240
104
  end
241
105
  end
242
106
 
243
107
  def collect_commented_dynamic(node)
244
- fake_method_name = @dynamic_lines[node.loc.line]
245
- return unless fake_method_name
246
-
247
- node = build_send_wrapper_for(node, fake_method_name)
248
- collect_dynamic(node)
108
+ @dynamic_lines[node.loc.line]&.each do |fake_method_name|
109
+ collect_dynamic(build_send_wrapper_for(node, fake_method_name))
110
+ end
249
111
  end
250
112
 
251
- def build_send_wrapper_for(node, name)
252
- ::Leftovers::AST::Node.new(
253
- :send,
254
- [nil, name.to_sym, *node.arguments],
255
- location: node.location
256
- )
113
+ def collect_dynamic(node)
114
+ Leftovers.config.dynamic.process(nil, node, node, self)
115
+ rescue StandardError => e
116
+ raise ::Leftovers::Error, "#{e.class}: #{e.message}\n" \
117
+ "when processing #{node} at #{filename}:#{node.loc.line}:#{node.loc.column}", e.backtrace
257
118
  end
258
119
 
259
- def collect_dynamic(node) # rubocop:disable Metrics/AbcSize
260
- node.keep_line = @allow_lines.include?(node.loc.line)
261
- node.test_line = test_line?(node.loc) unless node.keep_line?
120
+ private
262
121
 
263
- Leftovers.config.dynamic.process(node, self)
264
- rescue StandardError => e
265
- raise ::Leftovers::Error, "#{e.class}: #{e.message}\nwhen processing #{node} at #{filename}:#{node.loc.line}:#{node.loc.column}", e.backtrace # rubocop:disable Layout/LineLength
122
+ def build_send_wrapper_for(node, name)
123
+ ::Leftovers::AST::SendNode.new(
124
+ :send, [nil, name.to_sym, *node.arguments], location: node.location
125
+ )
266
126
  end
267
127
  end
268
128
  end
@@ -4,11 +4,12 @@ require 'fast_ignore'
4
4
 
5
5
  module Leftovers
6
6
  class FileList < ::FastIgnore
7
- def initialize
7
+ def initialize(**arguments)
8
8
  super(
9
9
  ignore_rules: Leftovers.config.exclude_paths,
10
10
  include_rules: Leftovers.config.include_paths,
11
- root: Leftovers.pwd
11
+ root: Leftovers.pwd,
12
+ **arguments
12
13
  )
13
14
  end
14
15
 
@@ -3,15 +3,32 @@
3
3
  module Leftovers
4
4
  module MatcherBuilders
5
5
  module And
6
- def self.build(matchers)
7
- matchers = matchers.compact
8
- case matchers.length
9
- # :nocov:
10
- when 0 then nil
11
- # :nocov:
12
- when 1 then matchers.first
13
- when 2 then ::Leftovers::Matchers::And.new(matchers.first, matchers[1])
14
- else ::Leftovers::Matchers::All.new(matchers.dup)
6
+ class << self
7
+ def build(matchers)
8
+ matchers = flatten(matchers).compact
9
+ case matchers.length
10
+ when 0 then nil
11
+ when 1 then matchers.first
12
+ when 2 then ::Leftovers::Matchers::And.new(matchers.first, matchers[1])
13
+ else ::Leftovers::Matchers::All.new(matchers.dup)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def flatten(value)
20
+ case value
21
+ when ::Leftovers::Matchers::And
22
+ [*flatten(value.lhs), *flatten(value.rhs)]
23
+ # :nocov: # not sure how to make this happen
24
+ when ::Leftovers::Matchers::All
25
+ flatten(value.matchers)
26
+ # :nocov:
27
+ when Array
28
+ value.flat_map { |v| flatten(v) }
29
+ else
30
+ [value]
31
+ end
15
32
  end
16
33
  end
17
34
  end
@@ -3,11 +3,13 @@
3
3
  module Leftovers
4
4
  module MatcherBuilders
5
5
  module AndNot
6
- def self.build(positive_matcher, negative_matcher)
7
- ::Leftovers::MatcherBuilders::And.build([
8
- positive_matcher,
9
- ::Leftovers::MatcherBuilders::Unless.build(negative_matcher)
10
- ])
6
+ class << self
7
+ def build(positive_matcher, negative_matcher)
8
+ ::Leftovers::MatcherBuilders::And.build([
9
+ positive_matcher,
10
+ ::Leftovers::MatcherBuilders::Unless.build(negative_matcher)
11
+ ])
12
+ end
11
13
  end
12
14
  end
13
15
  end
@@ -3,26 +3,27 @@
3
3
  module Leftovers
4
4
  module MatcherBuilders
5
5
  module Name
6
- def self.build(patterns) # rubocop:disable Metrics/MethodLength
7
- ::Leftovers::MatcherBuilders::Or.each_or_self(patterns) do |pat|
8
- case pat
9
- when nil
10
- when ::Array
11
- ::Leftovers::MatcherBuilders::Name.build(pat)
12
- when ::String
13
- ::Leftovers::MatcherBuilders::String.build(pat)
14
- when ::Hash
15
- unless_arg = pat.delete(:unless_arg)
16
-
17
- ::Leftovers::MatcherBuilders::AndNot.build(
18
- ::Leftovers::MatcherBuilders::StringPattern.build(**pat),
19
- ::Leftovers::MatcherBuilders::Name.build(unless_arg)
20
- )
21
- # :nocov:
22
- else raise
6
+ class << self
7
+ def build(patterns)
8
+ ::Leftovers::MatcherBuilders::Or.each_or_self(patterns) do |pat|
9
+ case pat
10
+ when ::String then ::Leftovers::MatcherBuilders::String.build(pat)
11
+ when ::Hash then build_from_hash(**pat)
23
12
  # :nocov:
13
+ else raise Leftovers::UnexpectedCase, "Unhandled value #{pat.inspect}"
14
+ # :nocov:
15
+ end
24
16
  end
25
17
  end
18
+
19
+ private
20
+
21
+ def build_from_hash(unless_arg: nil, **pattern)
22
+ ::Leftovers::MatcherBuilders::AndNot.build(
23
+ ::Leftovers::MatcherBuilders::StringPattern.build(**pattern),
24
+ ::Leftovers::MatcherBuilders::Name.build(unless_arg)
25
+ )
26
+ end
26
27
  end
27
28
  end
28
29
  end