leftovers 0.7.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (227) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +39 -0
  3. data/README.md +8 -7
  4. data/docs/Configuration.md +187 -93
  5. data/docs/Custom-Precompilers.md +44 -0
  6. data/leftovers.gemspec +2 -1
  7. data/lib/config/actioncable.yml +36 -0
  8. data/lib/config/actionmailbox.yml +28 -0
  9. data/lib/config/actionmailer.yml +87 -11
  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 +422 -89
  16. data/lib/config/activestorage.yml +26 -0
  17. data/lib/config/activesupport.yml +167 -24
  18. data/lib/config/haml.yml +4 -2
  19. data/lib/config/leftovers.yml +48 -0
  20. data/lib/config/rails.yml +7 -3
  21. data/lib/config/railties.yml +7 -0
  22. data/lib/config/ruby.yml +439 -95
  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 +33 -90
  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 +2 -18
  50. data/lib/leftovers/config_loader/argument_position_schema.rb +5 -3
  51. data/lib/leftovers/config_loader/array_schema.rb +53 -0
  52. data/lib/leftovers/config_loader/attribute.rb +36 -10
  53. data/lib/leftovers/config_loader/built_in_precompiler_schema.rb +13 -0
  54. data/lib/leftovers/config_loader/document_schema.rb +33 -5
  55. data/lib/leftovers/config_loader/dynamic_schema.rb +1 -0
  56. data/lib/leftovers/config_loader/has_argument_schema.rb +2 -2
  57. data/lib/leftovers/config_loader/has_receiver_schema.rb +15 -0
  58. data/lib/leftovers/config_loader/has_value_schema.rb +7 -3
  59. data/lib/leftovers/config_loader/inherit_schema_attributes.rb +22 -0
  60. data/lib/leftovers/config_loader/keep_test_only_schema.rb +2 -2
  61. data/lib/leftovers/config_loader/keyword_argument_schema.rb +13 -0
  62. data/lib/leftovers/config_loader/object_schema.rb +33 -105
  63. data/lib/leftovers/config_loader/precompile_schema.rb +12 -0
  64. data/lib/leftovers/config_loader/precompiler_schema.rb +11 -0
  65. data/lib/leftovers/config_loader/privacy_processor_schema.rb +0 -2
  66. data/lib/leftovers/config_loader/regexp_schema.rb +27 -0
  67. data/lib/leftovers/config_loader/require_schema.rb +3 -3
  68. data/lib/leftovers/config_loader/rule_pattern_schema.rb +3 -1
  69. data/lib/leftovers/config_loader/scalar_value_schema.rb +8 -0
  70. data/lib/leftovers/config_loader/schema.rb +12 -0
  71. data/lib/leftovers/config_loader/string_enum_schema.rb +1 -1
  72. data/lib/leftovers/config_loader/string_pattern_schema.rb +3 -3
  73. data/lib/leftovers/config_loader/string_value_processor_schema.rb +2 -2
  74. data/lib/leftovers/config_loader/transform_schema.rb +10 -6
  75. data/lib/leftovers/config_loader/value_matcher_condition_schema.rb +13 -0
  76. data/lib/leftovers/config_loader/value_matcher_schema.rb +6 -3
  77. data/lib/leftovers/config_loader/value_or_array_schema.rb +2 -32
  78. data/lib/leftovers/config_loader/value_or_object_schema.rb +40 -0
  79. data/lib/leftovers/config_loader/value_processor_schema.rb +4 -4
  80. data/lib/leftovers/config_loader.rb +15 -3
  81. data/lib/leftovers/definition_collection.rb +37 -0
  82. data/lib/leftovers/definition_node.rb +8 -14
  83. data/lib/leftovers/definition_node_set.rb +10 -2
  84. data/lib/leftovers/file.rb +8 -54
  85. data/lib/leftovers/file_collector/comments_processor.rb +57 -0
  86. data/lib/leftovers/file_collector/node_processor.rb +131 -0
  87. data/lib/leftovers/file_collector.rb +56 -223
  88. data/lib/leftovers/file_list.rb +3 -2
  89. data/lib/leftovers/matcher_builders/and.rb +26 -9
  90. data/lib/leftovers/matcher_builders/and_not.rb +7 -5
  91. data/lib/leftovers/matcher_builders/name.rb +18 -17
  92. data/lib/leftovers/matcher_builders/node.rb +60 -34
  93. data/lib/leftovers/matcher_builders/node_has_argument.rb +48 -52
  94. data/lib/leftovers/matcher_builders/node_has_keyword_argument.rb +16 -11
  95. data/lib/leftovers/matcher_builders/node_has_positional_argument.rb +29 -15
  96. data/lib/leftovers/matcher_builders/node_has_receiver.rb +12 -3
  97. data/lib/leftovers/matcher_builders/node_pair_key.rb +16 -0
  98. data/lib/leftovers/matcher_builders/node_type.rb +10 -10
  99. data/lib/leftovers/matcher_builders/node_value.rb +46 -25
  100. data/lib/leftovers/matcher_builders/or.rb +64 -49
  101. data/lib/leftovers/matcher_builders/path.rb +3 -1
  102. data/lib/leftovers/matcher_builders/string_pattern.rb +1 -1
  103. data/lib/leftovers/matcher_builders.rb +1 -1
  104. data/lib/leftovers/matchers/all.rb +4 -0
  105. data/lib/leftovers/matchers/and.rb +4 -0
  106. data/lib/leftovers/matchers/any.rb +2 -0
  107. data/lib/leftovers/matchers/node_has_any_keyword_argument.rb +7 -4
  108. data/lib/leftovers/matchers/node_has_any_positional_argument_with_value.rb +5 -4
  109. data/lib/leftovers/matchers/node_has_any_receiver.rb +13 -0
  110. data/lib/leftovers/matchers/node_has_positional_argument.rb +5 -1
  111. data/lib/leftovers/matchers/node_has_positional_argument_with_value.rb +6 -1
  112. data/lib/leftovers/matchers/node_has_receiver.rb +4 -0
  113. data/lib/leftovers/matchers/node_is_proc.rb +13 -0
  114. data/lib/leftovers/matchers/node_name.rb +9 -3
  115. data/lib/leftovers/matchers/node_pair_key.rb +23 -0
  116. data/lib/leftovers/matchers/node_pair_value.rb +7 -3
  117. data/lib/leftovers/matchers/node_path.rb +7 -3
  118. data/lib/leftovers/matchers/node_privacy.rb +7 -3
  119. data/lib/leftovers/matchers/node_scalar_value.rb +6 -1
  120. data/lib/leftovers/matchers/node_type.rb +7 -3
  121. data/lib/leftovers/matchers/not.rb +2 -0
  122. data/lib/leftovers/matchers/or.rb +2 -0
  123. data/lib/leftovers/matchers/path.rb +21 -0
  124. data/lib/leftovers/matchers.rb +4 -1
  125. data/lib/leftovers/merged_config.rb +28 -47
  126. data/lib/leftovers/parser.rb +7 -4
  127. data/lib/leftovers/precompilers/erb.rb +22 -0
  128. data/lib/leftovers/precompilers/haml.rb +15 -0
  129. data/lib/leftovers/precompilers/json.rb +27 -0
  130. data/lib/leftovers/precompilers/precompiler.rb +28 -0
  131. data/lib/leftovers/precompilers/slim.rb +15 -0
  132. data/lib/leftovers/precompilers/yaml.rb +75 -0
  133. data/lib/leftovers/precompilers.rb +50 -0
  134. data/lib/leftovers/processor_builders/action.rb +69 -42
  135. data/lib/leftovers/processor_builders/add_prefix.rb +18 -10
  136. data/lib/leftovers/processor_builders/add_suffix.rb +18 -10
  137. data/lib/leftovers/processor_builders/argument.rb +28 -14
  138. data/lib/leftovers/processor_builders/dynamic.rb +37 -31
  139. data/lib/leftovers/processor_builders/each.rb +83 -11
  140. data/lib/leftovers/processor_builders/itself.rb +2 -2
  141. data/lib/leftovers/processor_builders/keyword.rb +9 -9
  142. data/lib/leftovers/processor_builders/keyword_argument.rb +4 -2
  143. data/lib/leftovers/processor_builders/receiver.rb +13 -0
  144. data/lib/leftovers/processor_builders/transform.rb +55 -44
  145. data/lib/leftovers/processor_builders/transform_chain.rb +16 -8
  146. data/lib/leftovers/processor_builders/transform_set.rb +16 -32
  147. data/lib/leftovers/processor_builders/value.rb +4 -4
  148. data/lib/leftovers/processor_builders.rb +1 -3
  149. data/lib/leftovers/processors/add_call.rb +14 -0
  150. data/lib/leftovers/processors/add_definition_node.rb +16 -0
  151. data/lib/leftovers/processors/add_dynamic_prefix.rb +29 -0
  152. data/lib/leftovers/processors/add_dynamic_suffix.rb +29 -0
  153. data/lib/leftovers/{value_processors → processors}/add_prefix.rb +7 -3
  154. data/lib/leftovers/{value_processors → processors}/add_suffix.rb +7 -3
  155. data/lib/leftovers/processors/append_sym.rb +13 -0
  156. data/lib/leftovers/{value_processors → processors}/camelize.rb +7 -3
  157. data/lib/leftovers/{value_processors → processors}/capitalize.rb +7 -3
  158. data/lib/leftovers/{value_processors → processors}/deconstantize.rb +7 -3
  159. data/lib/leftovers/{value_processors → processors}/delete_after.rb +9 -5
  160. data/lib/leftovers/processors/delete_after_last.rb +26 -0
  161. data/lib/leftovers/processors/delete_before.rb +27 -0
  162. data/lib/leftovers/processors/delete_before_last.rb +27 -0
  163. data/lib/leftovers/{value_processors → processors}/delete_prefix.rb +7 -3
  164. data/lib/leftovers/{value_processors → processors}/delete_suffix.rb +7 -3
  165. data/lib/leftovers/{value_processors → processors}/demodulize.rb +7 -3
  166. data/lib/leftovers/{value_processors → processors}/downcase.rb +7 -3
  167. data/lib/leftovers/processors/each.rb +25 -0
  168. data/lib/leftovers/processors/each_for_definition_set.rb +33 -0
  169. data/lib/leftovers/processors/each_keyword.rb +29 -0
  170. data/lib/leftovers/processors/each_keyword_argument.rb +29 -0
  171. data/lib/leftovers/processors/each_positional_argument.rb +27 -0
  172. data/lib/leftovers/processors/each_positional_argument_from.rb +30 -0
  173. data/lib/leftovers/processors/eval.rb +16 -0
  174. data/lib/leftovers/processors/itself.rb +21 -0
  175. data/lib/leftovers/{value_processors → processors}/keyword_argument.rb +9 -11
  176. data/lib/leftovers/processors/match_current_node.rb +26 -0
  177. data/lib/leftovers/processors/match_matched_node.rb +26 -0
  178. data/lib/leftovers/{value_processors → processors}/parameterize.rb +7 -3
  179. data/lib/leftovers/{value_processors → processors}/placeholder.rb +5 -4
  180. data/lib/leftovers/{value_processors → processors}/pluralize.rb +7 -3
  181. data/lib/leftovers/{value_processors → processors}/positional_argument.rb +8 -6
  182. data/lib/leftovers/processors/receiver.rb +24 -0
  183. data/lib/leftovers/{value_processors → processors}/replace_value.rb +7 -3
  184. data/lib/leftovers/processors/set_default_privacy.rb +21 -0
  185. data/lib/leftovers/processors/set_privacy.rb +23 -0
  186. data/lib/leftovers/{value_processors → processors}/singularize.rb +7 -3
  187. data/lib/leftovers/{value_processors → processors}/split.rb +8 -4
  188. data/lib/leftovers/{value_processors → processors}/swapcase.rb +7 -3
  189. data/lib/leftovers/{value_processors → processors}/titleize.rb +7 -3
  190. data/lib/leftovers/{value_processors → processors}/underscore.rb +7 -3
  191. data/lib/leftovers/{value_processors → processors}/upcase.rb +7 -3
  192. data/lib/leftovers/processors.rb +49 -0
  193. data/lib/leftovers/rake_task.rb +1 -1
  194. data/lib/leftovers/version.rb +1 -1
  195. data/lib/leftovers.rb +34 -8
  196. metadata +116 -58
  197. data/lib/leftovers/dynamic_processors/call.rb +0 -22
  198. data/lib/leftovers/dynamic_processors/call_definition.rb +0 -34
  199. data/lib/leftovers/dynamic_processors/definition.rb +0 -27
  200. data/lib/leftovers/dynamic_processors/each.rb +0 -19
  201. data/lib/leftovers/dynamic_processors/null.rb +0 -9
  202. data/lib/leftovers/dynamic_processors/set_default_privacy.rb +0 -18
  203. data/lib/leftovers/dynamic_processors/set_privacy.rb +0 -23
  204. data/lib/leftovers/dynamic_processors.rb +0 -13
  205. data/lib/leftovers/erb.rb +0 -20
  206. data/lib/leftovers/haml.rb +0 -21
  207. data/lib/leftovers/json.rb +0 -28
  208. data/lib/leftovers/matcher_builders/node_pair_name.rb +0 -18
  209. data/lib/leftovers/matchers/predicate.rb +0 -19
  210. data/lib/leftovers/processor_builders/each_action.rb +0 -51
  211. data/lib/leftovers/processor_builders/each_dynamic.rb +0 -54
  212. data/lib/leftovers/processor_builders/each_for_definition_set.rb +0 -36
  213. data/lib/leftovers/slim.rb +0 -21
  214. data/lib/leftovers/value_processors/add_dynamic_prefix.rb +0 -31
  215. data/lib/leftovers/value_processors/add_dynamic_suffix.rb +0 -31
  216. data/lib/leftovers/value_processors/delete_before.rb +0 -22
  217. data/lib/leftovers/value_processors/each.rb +0 -21
  218. data/lib/leftovers/value_processors/each_for_definition_set.rb +0 -26
  219. data/lib/leftovers/value_processors/each_keyword.rb +0 -27
  220. data/lib/leftovers/value_processors/each_keyword_argument.rb +0 -27
  221. data/lib/leftovers/value_processors/each_positional_argument.rb +0 -24
  222. data/lib/leftovers/value_processors/itself.rb +0 -17
  223. data/lib/leftovers/value_processors/keyword.rb +0 -32
  224. data/lib/leftovers/value_processors/return_definition_node.rb +0 -14
  225. data/lib/leftovers/value_processors/return_sym.rb +0 -14
  226. data/lib/leftovers/value_processors.rb +0 -40
  227. data/lib/leftovers/yaml.rb +0 -73
@@ -2,13 +2,13 @@
2
2
 
3
3
  module Leftovers
4
4
  class ConfigLoader
5
- class StringPatternSchema < ObjectSchema
6
- attribute :match, StringSchema, aliases: :matches, require_group: :matcher
5
+ class StringPatternSchema < ValueOrObjectSchema
6
+ attribute :match, RegexpSchema, aliases: :matches, require_group: :matcher
7
7
  attribute :has_prefix, StringSchema, require_group: :matcher
8
8
  attribute :has_suffix, StringSchema, require_group: :matcher
9
9
  attribute :unless, ValueOrArraySchema[StringPatternSchema], require_group: :matcher
10
10
 
11
- self.or_schema = StringSchema
11
+ self.or_value_schema = StringSchema
12
12
  end
13
13
  end
14
14
  end
@@ -2,10 +2,10 @@
2
2
 
3
3
  module Leftovers
4
4
  class ConfigLoader
5
- class StringValueProcessorSchema < ObjectSchema
5
+ class StringValueProcessorSchema < ValueOrObjectSchema
6
6
  inherit_attributes_from ValueProcessorSchema
7
7
 
8
- self.or_schema = StringSchema
8
+ self.or_value_schema = StringSchema
9
9
  end
10
10
  end
11
11
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Leftovers
4
4
  class ConfigLoader
5
- class TransformSchema < ObjectSchema
5
+ class TransformSchema < ValueOrObjectSchema
6
6
  ArgumentlessTransformSchema.each_value do |transform|
7
7
  attribute(
8
8
  transform, TrueSchema,
@@ -17,12 +17,16 @@ module Leftovers
17
17
  require_group: :processor
18
18
 
19
19
  attribute :split, StringSchema, require_group: :processor
20
- attribute :delete_prefix, StringSchema, require_group: :processor
21
- attribute :delete_suffix, StringSchema, require_group: :processor
22
- attribute :delete_before, StringSchema, require_group: :processor
23
- attribute :delete_after, StringSchema, require_group: :processor
20
+ attribute :delete_prefix, ValueOrArraySchema[StringSchema], require_group: :processor
21
+ attribute :delete_suffix, ValueOrArraySchema[StringSchema], require_group: :processor
22
+ attribute :delete_before, ValueOrArraySchema[StringSchema], require_group: :processor
23
+ attribute :delete_before_last, ValueOrArraySchema[StringSchema], require_group: :processor
24
+ attribute :delete_after, ValueOrArraySchema[StringSchema], require_group: :processor
25
+ attribute :delete_after_last, ValueOrArraySchema[StringSchema], require_group: :processor
26
+ attribute :transforms, ValueOrArraySchema[TransformSchema], require_group: :processor,
27
+ aliases: :transform
24
28
 
25
- self.or_schema = ArgumentlessTransformSchema
29
+ self.or_value_schema = ArgumentlessTransformSchema
26
30
  end
27
31
  end
28
32
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Leftovers
4
+ class ConfigLoader
5
+ class ValueMatcherConditionSchema < ObjectSchema
6
+ attribute :has_arguments, ValueOrArraySchema[HasArgumentSchema], aliases: :has_argument
7
+ attribute :has_receiver, ValueOrArraySchema[HasReceiverSchema]
8
+ attribute :unless, ValueOrArraySchema[ValueMatcherConditionSchema]
9
+ attribute :all, ArraySchema[ValueMatcherConditionSchema]
10
+ attribute :any, ArraySchema[ValueMatcherConditionSchema]
11
+ end
12
+ end
13
+ end
@@ -2,17 +2,20 @@
2
2
 
3
3
  module Leftovers
4
4
  class ConfigLoader
5
- class ValueMatcherSchema < ObjectSchema
5
+ class ValueMatcherSchema < ValueOrObjectSchema
6
6
  attribute :arguments, ValueOrArraySchema[ArgumentPositionSchema], aliases: :argument,
7
7
  require_group: :matcher
8
8
  attribute :keywords, ValueOrArraySchema[StringPatternSchema], aliases: :keyword,
9
9
  require_group: :matcher
10
10
  attribute :itself, TrueSchema, require_group: :matcher
11
11
  attribute :nested, ValueOrArraySchema[ValueMatcherSchema]
12
- attribute :value, StringSchema, require_group: :matcher
12
+ attribute :value, ValueOrArraySchema[StringSchema], require_group: :matcher, aliases: :values
13
+ attribute :receiver, TrueSchema, require_group: :matcher
13
14
  attribute :recursive, TrueSchema
14
15
 
15
- self.or_schema = ScalarArgumentSchema
16
+ inherit_attributes_from ValueMatcherConditionSchema
17
+
18
+ self.or_value_schema = ScalarArgumentSchema
16
19
  end
17
20
  end
18
21
  end
@@ -2,21 +2,7 @@
2
2
 
3
3
  module Leftovers
4
4
  class ConfigLoader
5
- class ValueOrArraySchema < Schema
6
- class << self
7
- def [](value_schema)
8
- new(value_schema)
9
- end
10
- end
11
-
12
- attr_reader :value_schema
13
-
14
- def initialize(value_schema)
15
- @value_schema = value_schema
16
-
17
- super()
18
- end
19
-
5
+ class ValueOrArraySchema < ArraySchema
20
6
  def validate(node)
21
7
  if node.array?
22
8
  validate_length(node) && validate_values(node)
@@ -27,9 +13,7 @@ module Leftovers
27
13
 
28
14
  def to_ruby(node)
29
15
  if node.array?
30
- node.children.map do |value|
31
- value_schema.to_ruby(value)
32
- end
16
+ Leftovers.unwrap_array(super)
33
17
  else
34
18
  value_schema.to_ruby(node)
35
19
  end
@@ -45,20 +29,6 @@ module Leftovers
45
29
 
46
30
  false
47
31
  end
48
-
49
- def validate_length(node)
50
- self.class.error(node, 'not be empty') if node.children.empty?
51
-
52
- node.valid?
53
- end
54
-
55
- def validate_values(node)
56
- node.children.each do |value|
57
- value_schema.validate(value)
58
- end
59
-
60
- node.children.all?(&:valid?)
61
- end
62
32
  end
63
33
  end
64
34
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Leftovers
4
+ class ConfigLoader
5
+ class ValueOrObjectSchema < ObjectSchema
6
+ class << self
7
+ attr_accessor :or_value_schema
8
+
9
+ def validate(node)
10
+ if node.hash?
11
+ super(node)
12
+ else
13
+ validate_or_value_schema(node)
14
+ end
15
+ end
16
+
17
+ def to_ruby(node)
18
+ if node.hash?
19
+ super
20
+ else
21
+ or_value_schema.to_ruby(node)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def validate_or_value_schema(node)
28
+ or_value_schema.validate(node)
29
+ return true if node.valid?
30
+
31
+ if node.string? && attribute_for_key(node)
32
+ node.error = "#{node.name_}#{node.to_sym} must be a hash key"
33
+ else
34
+ node.error += " or a hash with any of #{suggestions.join(', ')}"
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -2,13 +2,13 @@
2
2
 
3
3
  module Leftovers
4
4
  class ConfigLoader
5
- class ValueProcessorSchema < ObjectSchema
5
+ class ValueProcessorSchema < ValueOrObjectSchema
6
6
  inherit_attributes_from ValueMatcherSchema
7
7
 
8
- attribute :transforms, ValueOrArraySchema[TransformSchema]
9
- inherit_attributes_from TransformSchema, require_group: nil
8
+ attribute :transforms, ValueOrArraySchema[TransformSchema], aliases: :transform
9
+ inherit_attributes_from TransformSchema, require_group: nil, except: :transforms
10
10
 
11
- self.or_schema = ScalarArgumentSchema
11
+ self.or_value_schema = ScalarArgumentSchema
12
12
  end
13
13
  end
14
14
  end
@@ -1,36 +1,48 @@
1
1
  # frozen-string-literal: true
2
2
 
3
3
  require 'yaml'
4
- require 'leftovers'
5
4
 
6
5
  module Leftovers
7
6
  class ConfigLoader
8
- autoload(:ArgumentPositionSchema, "#{__dir__}/config_loader/argument_position_schema")
9
7
  autoload(:ArgumentlessTransformSchema, "#{__dir__}/config_loader/argumentless_transform_schema")
8
+ autoload(:ArgumentPositionSchema, "#{__dir__}/config_loader/argument_position_schema")
9
+ autoload(:ArraySchema, "#{__dir__}/config_loader/array_schema")
10
10
  autoload(:Attribute, "#{__dir__}/config_loader/attribute")
11
+ autoload(:BuiltInPrecompilerSchema, "#{__dir__}/config_loader/built_in_precompiler_schema")
11
12
  autoload(:DocumentSchema, "#{__dir__}/config_loader/document_schema")
12
13
  autoload(:DynamicSchema, "#{__dir__}/config_loader/dynamic_schema")
13
14
  autoload(:HasArgumentSchema, "#{__dir__}/config_loader/has_argument_schema")
14
15
  autoload(:HasValueSchema, "#{__dir__}/config_loader/has_value_schema")
16
+ autoload(:HasReceiverSchema, "#{__dir__}/config_loader/has_receiver_schema")
17
+ autoload(:InheritSchemaAttributes, "#{__dir__}/config_loader/inherit_schema_attributes")
15
18
  autoload(:KeepTestOnlySchema, "#{__dir__}/config_loader/keep_test_only_schema")
19
+ autoload(:KeywordArgumentSchema, "#{__dir__}/config_loader/keyword_argument_schema")
16
20
  autoload(:Node, "#{__dir__}/config_loader/node")
17
21
  autoload(:ObjectSchema, "#{__dir__}/config_loader/object_schema")
22
+ autoload(:PrecompilerSchema, "#{__dir__}/config_loader/precompiler_schema")
23
+ autoload(:PrecompileSchema, "#{__dir__}/config_loader/precompile_schema")
18
24
  autoload(:PrivacyProcessorSchema, "#{__dir__}/config_loader/privacy_processor_schema")
19
25
  autoload(:PrivacySchema, "#{__dir__}/config_loader/privacy_schema")
26
+ autoload(:RegexpSchema, "#{__dir__}/config_loader/regexp_schema")
20
27
  autoload(:RequireSchema, "#{__dir__}/config_loader/require_schema")
21
28
  autoload(:RulePatternSchema, "#{__dir__}/config_loader/rule_pattern_schema")
22
29
  autoload(:ScalarArgumentSchema, "#{__dir__}/config_loader/scalar_argument_schema")
23
30
  autoload(:ScalarValueSchema, "#{__dir__}/config_loader/scalar_value_schema")
24
31
  autoload(:Schema, "#{__dir__}/config_loader/schema")
25
- autoload(:Suggester, "#{__dir__}/config_loader/suggester")
26
32
  autoload(:StringEnumSchema, "#{__dir__}/config_loader/string_enum_schema")
27
33
  autoload(:StringPatternSchema, "#{__dir__}/config_loader/string_pattern_schema")
28
34
  autoload(:StringSchema, "#{__dir__}/config_loader/string_schema")
29
35
  autoload(:StringValueProcessorSchema, "#{__dir__}/config_loader/string_value_processor_schema")
36
+ autoload(:Suggester, "#{__dir__}/config_loader/suggester")
30
37
  autoload(:TransformSchema, "#{__dir__}/config_loader/transform_schema")
31
38
  autoload(:TrueSchema, "#{__dir__}/config_loader/true_schema")
39
+ autoload(
40
+ :ValueMatcherConditionSchema,
41
+ "#{__dir__}/config_loader/value_matcher_condition_schema"
42
+ )
32
43
  autoload(:ValueMatcherSchema, "#{__dir__}/config_loader/value_matcher_schema")
33
44
  autoload(:ValueOrArraySchema, "#{__dir__}/config_loader/value_or_array_schema")
45
+ autoload(:ValueOrObjectSchema, "#{__dir__}/config_loader/value_or_object_schema")
34
46
  autoload(:ValueProcessorSchema, "#{__dir__}/config_loader/value_processor_schema")
35
47
  autoload(:ValueTypeSchema, "#{__dir__}/config_loader/value_type_schema")
36
48
 
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Leftovers
4
+ class DefinitionCollection
5
+ def initialize
6
+ @definitions_to_add = {}
7
+ @definition_sets_to_add = []
8
+ end
9
+
10
+ def add_definition_node(definition_node)
11
+ add(definition_node, loc: definition_node.loc)
12
+ end
13
+
14
+ def add(node, name: node.name, loc: node.loc.name)
15
+ @definitions_to_add[name] = ::Leftovers::DefinitionToAdd.new(node, name: name, location: loc)
16
+ end
17
+
18
+ def add_definition_set(definition_node_set)
19
+ @definition_sets_to_add << definition_node_set.definitions.map do |definition_node|
20
+ ::Leftovers::DefinitionToAdd.new(definition_node, location: definition_node.loc)
21
+ end
22
+ end
23
+
24
+ def set_privacy(name, to)
25
+ @definitions_to_add[name]&.privacy = to
26
+ end
27
+
28
+ def to_definitions(file_collector)
29
+ @definitions_to_add.each_value.map { |d| d.to_definition(file_collector) } +
30
+ @definition_sets_to_add.map do |definition_set|
31
+ next if definition_set.any? { |d| d.keep?(file_collector) }
32
+
33
+ ::Leftovers::DefinitionSet.new(definition_set.map { |d| d.to_definition(file_collector) })
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,25 +1,19 @@
1
1
  # frozen-string-literal: true
2
2
 
3
3
  module Leftovers
4
- class DefinitionNode
5
- attr_reader :name, :loc, :node
4
+ class DefinitionNode < ::Leftovers::AST::Node
5
+ attr_reader :name, :path
6
+ alias_method :to_sym, :name
6
7
 
7
8
  def initialize(node, name:, location: node.loc.expression)
8
- @node = node
9
9
  @name = name
10
- @loc = location
10
+ @path = node.path
11
+ @location = location
12
+ super(:leftovers_definition)
11
13
  end
12
14
 
13
- def kwargs
14
- nil
15
- end
16
-
17
- def positional_arguments
18
- nil
19
- end
20
-
21
- def path
22
- node.path
15
+ def to_s
16
+ name.to_s
23
17
  end
24
18
  end
25
19
  end
@@ -4,8 +4,16 @@ module Leftovers
4
4
  class DefinitionNodeSet
5
5
  attr_reader :definitions
6
6
 
7
- def initialize(definitions)
8
- @definitions = definitions
7
+ def initialize
8
+ @definitions = []
9
+ end
10
+
11
+ def add_definition_node(definition_node)
12
+ @definitions << definition_node
13
+ end
14
+
15
+ def add_definition_set(definition_node_set)
16
+ @definitions.concat(definition_node_set.definitions)
9
17
  end
10
18
  end
11
19
  end
@@ -15,65 +15,19 @@ module Leftovers
15
15
  def test?
16
16
  return @test if defined?(@test)
17
17
 
18
- @test = Leftovers.config.test_paths.allowed?(relative_path)
18
+ @test = Leftovers.config.test_paths === relative_path
19
19
  end
20
20
 
21
- def ruby # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
22
- precompiled = []
23
- precompile = false
21
+ def ruby
22
+ read = self.read
24
23
 
25
- if haml?
26
- precompiled << ::Leftovers::Haml.precompile(read, self)
27
- precompile = true
28
- end
29
-
30
- if json?
31
- precompiled << ::Leftovers::JSON.precompile(read, self)
32
- precompile = true
33
- end
34
-
35
- if erb?
36
- precompiled << ::Leftovers::ERB.precompile(read, self)
37
- precompile = true
38
- end
39
-
40
- if slim?
41
- precompiled << ::Leftovers::Slim.precompile(read, self)
42
- precompile = true
43
- end
44
-
45
- if yaml?
46
- precompiled << ::Leftovers::YAML.precompile(read, self)
47
- precompile = true
48
- end
49
-
50
- if precompile
51
- precompiled.join("\n")
52
- else
53
- read
54
- end
55
- end
56
-
57
- private
58
-
59
- def erb?
60
- Leftovers.config.erb_paths.allowed?(relative_path)
61
- end
24
+ precompiled = ::Leftovers.config.precompilers.map do |precompiler|
25
+ precompiler.precompile(read, self)
26
+ end.compact
62
27
 
63
- def haml?
64
- Leftovers.config.haml_paths.allowed?(relative_path)
65
- end
66
-
67
- def yaml?
68
- Leftovers.config.yaml_paths.allowed?(relative_path)
69
- end
70
-
71
- def json?
72
- Leftovers.config.json_paths.allowed?(relative_path)
73
- end
28
+ return read if precompiled.empty?
74
29
 
75
- def slim?
76
- Leftovers.config.slim_paths.allowed?(relative_path)
30
+ precompiled.join("\n")
77
31
  end
78
32
  end
79
33
  end
@@ -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