leftovers 0.5.5 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +40 -2
- data/README.md +22 -1
- data/docs/Configuration.md +173 -42
- data/docs/Custom-Precompilers.md +38 -0
- data/leftovers.gemspec +8 -5
- data/lib/config/actionmailer.yml +11 -11
- data/lib/config/actionpack.yml +5 -3
- data/lib/config/activesupport.yml +1 -1
- data/lib/config/haml.yml +4 -2
- data/lib/config/rails.yml +1 -1
- data/lib/config/railties.yml +11 -0
- data/lib/config/ruby.yml +76 -8
- data/lib/config/slim.yml +4 -2
- data/lib/leftovers/ast/node.rb +16 -11
- data/lib/leftovers/cli.rb +5 -0
- data/lib/leftovers/collector.rb +5 -2
- data/lib/leftovers/config.rb +3 -38
- data/lib/leftovers/config_loader/argument_position_schema.rb +11 -0
- data/lib/leftovers/config_loader/argumentless_transform_schema.rb +21 -0
- data/lib/leftovers/config_loader/attribute.rb +56 -0
- data/lib/leftovers/config_loader/built_in_precompiler_schema.rb +13 -0
- data/lib/leftovers/config_loader/document_schema.rb +48 -0
- data/lib/leftovers/config_loader/dynamic_schema.rb +17 -0
- data/lib/leftovers/config_loader/has_argument_schema.rb +13 -0
- data/lib/leftovers/config_loader/has_value_schema.rb +18 -0
- data/lib/leftovers/config_loader/inherit_schema_attributes.rb +22 -0
- data/lib/leftovers/config_loader/keep_test_only_schema.rb +13 -0
- data/lib/leftovers/config_loader/node.rb +106 -0
- data/lib/leftovers/config_loader/object_schema.rb +117 -0
- data/lib/leftovers/config_loader/precompile_schema.rb +12 -0
- data/lib/leftovers/config_loader/precompiler_schema.rb +11 -0
- data/lib/leftovers/config_loader/privacy_processor_schema.rb +10 -0
- data/lib/leftovers/config_loader/privacy_schema.rb +15 -0
- data/lib/leftovers/config_loader/require_schema.rb +11 -0
- data/lib/leftovers/config_loader/rule_pattern_schema.rb +18 -0
- data/lib/leftovers/config_loader/scalar_argument_schema.rb +14 -0
- data/lib/leftovers/config_loader/scalar_value_schema.rb +14 -0
- data/lib/leftovers/config_loader/schema.rb +23 -0
- data/lib/leftovers/config_loader/string_enum_schema.rb +62 -0
- data/lib/leftovers/config_loader/string_pattern_schema.rb +14 -0
- data/lib/leftovers/config_loader/string_schema.rb +14 -0
- data/lib/leftovers/config_loader/string_value_processor_schema.rb +11 -0
- data/lib/leftovers/config_loader/suggester.rb +22 -0
- data/lib/leftovers/config_loader/transform_schema.rb +26 -0
- data/lib/leftovers/config_loader/true_schema.rb +18 -0
- data/lib/leftovers/config_loader/value_matcher_schema.rb +18 -0
- data/lib/leftovers/config_loader/value_or_array_schema.rb +66 -0
- data/lib/leftovers/config_loader/value_or_object_schema.rb +40 -0
- data/lib/leftovers/config_loader/value_processor_schema.rb +14 -0
- data/lib/leftovers/config_loader/value_type_schema.rb +17 -0
- data/lib/leftovers/config_loader.rb +86 -0
- data/lib/leftovers/definition.rb +1 -1
- data/lib/leftovers/definition_node.rb +6 -17
- data/lib/leftovers/definition_node_set.rb +11 -0
- data/lib/leftovers/definition_to_add.rb +31 -0
- data/lib/leftovers/dynamic_processors/call.rb +4 -7
- data/lib/leftovers/dynamic_processors/call_definition.rb +14 -11
- data/lib/leftovers/dynamic_processors/definition.rb +8 -7
- data/lib/leftovers/dynamic_processors/set_default_privacy.rb +18 -0
- data/lib/leftovers/dynamic_processors/set_privacy.rb +23 -0
- data/lib/leftovers/dynamic_processors.rb +2 -0
- data/lib/leftovers/file.rb +12 -12
- data/lib/leftovers/file_collector/comments_processor.rb +57 -0
- data/lib/leftovers/file_collector/node_processor.rb +131 -0
- data/lib/leftovers/file_collector.rb +66 -169
- data/lib/leftovers/matcher_builders/and_not.rb +7 -5
- data/lib/leftovers/matcher_builders/document.rb +13 -0
- data/lib/leftovers/matcher_builders/name.rb +18 -17
- data/lib/leftovers/matcher_builders/node.rb +48 -28
- data/lib/leftovers/matcher_builders/node_has_argument.rb +48 -53
- data/lib/leftovers/matcher_builders/node_has_keyword_argument.rb +13 -10
- data/lib/leftovers/matcher_builders/node_has_positional_argument.rb +29 -15
- data/lib/leftovers/matcher_builders/node_privacy.rb +13 -0
- data/lib/leftovers/matcher_builders/node_type.rb +4 -4
- data/lib/leftovers/matcher_builders/node_value.rb +28 -23
- data/lib/leftovers/matcher_builders/or.rb +50 -50
- data/lib/leftovers/matcher_builders/string_pattern.rb +14 -5
- data/lib/leftovers/matcher_builders.rb +2 -0
- data/lib/leftovers/matchers/all.rb +0 -4
- data/lib/leftovers/matchers/and.rb +0 -4
- data/lib/leftovers/matchers/any.rb +0 -4
- data/lib/leftovers/matchers/node_has_any_keyword_argument.rb +1 -7
- data/lib/leftovers/matchers/node_has_any_positional_argument_with_value.rb +1 -7
- data/lib/leftovers/matchers/node_has_positional_argument_with_value.rb +0 -4
- data/lib/leftovers/matchers/node_name.rb +0 -4
- data/lib/leftovers/matchers/node_pair_value.rb +0 -4
- data/lib/leftovers/matchers/node_path.rb +0 -4
- data/lib/leftovers/matchers/node_privacy.rb +19 -0
- data/lib/leftovers/matchers/node_scalar_value.rb +0 -4
- data/lib/leftovers/matchers/node_type.rb +0 -4
- data/lib/leftovers/matchers/not.rb +0 -4
- data/lib/leftovers/matchers/or.rb +0 -4
- data/lib/leftovers/matchers.rb +1 -0
- data/lib/leftovers/merged_config.rb +15 -33
- data/lib/leftovers/precompilers/erb.rb +22 -0
- data/lib/leftovers/precompilers/haml.rb +15 -0
- data/lib/leftovers/precompilers/json.rb +27 -0
- data/lib/leftovers/precompilers/precompiler.rb +28 -0
- data/lib/leftovers/precompilers/slim.rb +15 -0
- data/lib/leftovers/precompilers/yaml.rb +75 -0
- data/lib/leftovers/precompilers.rb +50 -0
- data/lib/leftovers/processor_builders/action.rb +48 -39
- data/lib/leftovers/processor_builders/add_prefix.rb +2 -2
- data/lib/leftovers/processor_builders/add_suffix.rb +2 -2
- data/lib/leftovers/processor_builders/argument.rb +8 -11
- data/lib/leftovers/processor_builders/dynamic.rb +51 -16
- data/lib/leftovers/processor_builders/each.rb +2 -2
- data/lib/leftovers/processor_builders/each_action.rb +33 -33
- data/lib/leftovers/processor_builders/each_dynamic.rb +4 -8
- data/lib/leftovers/processor_builders/each_for_definition_set.rb +25 -21
- data/lib/leftovers/processor_builders/keyword.rb +3 -4
- data/lib/leftovers/processor_builders/transform.rb +4 -4
- data/lib/leftovers/processor_builders/transform_chain.rb +16 -8
- data/lib/leftovers/processor_builders/transform_set.rb +32 -28
- data/lib/leftovers/rake_task.rb +1 -1
- data/lib/leftovers/todo_reporter.rb +10 -35
- data/lib/leftovers/value_processors/add_dynamic_prefix.rb +3 -10
- data/lib/leftovers/value_processors/add_dynamic_suffix.rb +3 -10
- data/lib/leftovers/value_processors/delete_prefix.rb +0 -6
- data/lib/leftovers/value_processors/delete_suffix.rb +0 -6
- data/lib/leftovers/value_processors/each.rb +1 -1
- data/lib/leftovers/value_processors/each_for_definition_set.rb +4 -10
- data/lib/leftovers/value_processors/each_keyword.rb +1 -1
- data/lib/leftovers/value_processors/each_keyword_argument.rb +1 -1
- data/lib/leftovers/value_processors/each_positional_argument.rb +3 -2
- data/lib/leftovers/value_processors/keyword.rb +3 -11
- data/lib/leftovers/value_processors/keyword_argument.rb +2 -10
- data/lib/leftovers/value_processors/return_definition_node.rb +14 -0
- data/lib/leftovers/value_processors/{return_string.rb → return_sym.rb} +1 -1
- data/lib/leftovers/value_processors/split.rb +2 -2
- data/lib/leftovers/value_processors.rb +2 -2
- data/lib/leftovers/version.rb +1 -1
- data/lib/leftovers.rb +66 -23
- metadata +98 -39
- data/lib/config/actioncable.yml +0 -4
- data/lib/leftovers/backports.rb +0 -40
- data/lib/leftovers/config_validator/error_processor.rb +0 -196
- data/lib/leftovers/config_validator/schema_hash.rb +0 -530
- data/lib/leftovers/config_validator.rb +0 -60
- data/lib/leftovers/erb.rb +0 -20
- data/lib/leftovers/haml.rb +0 -21
- data/lib/leftovers/slim.rb +0 -21
- data/lib/leftovers/value_processors/return_definition.rb +0 -26
@@ -0,0 +1,117 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
4
|
+
class ConfigLoader
|
5
|
+
class ObjectSchema < Schema
|
6
|
+
class << self
|
7
|
+
def inherit_attributes_from(schema, require_group: true, except: nil)
|
8
|
+
attributes_and_schemas_to_inherit_from << InheritSchemaAttributes.new(
|
9
|
+
schema, require_group: require_group, except: except
|
10
|
+
)
|
11
|
+
end
|
12
|
+
|
13
|
+
def attributes
|
14
|
+
attributes_and_schemas_to_inherit_from.flat_map(&:attributes)
|
15
|
+
end
|
16
|
+
|
17
|
+
def attribute(name, value_schema, aliases: nil, require_group: nil, suggest: true)
|
18
|
+
attributes_and_schemas_to_inherit_from << Attribute.new(
|
19
|
+
name, value_schema,
|
20
|
+
aliases: aliases, require_group: require_group, suggest: suggest
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
def require_groups
|
25
|
+
attributes.group_by(&:require_group).tap { |rg| rg.delete(nil) }.values
|
26
|
+
end
|
27
|
+
|
28
|
+
def validate(node)
|
29
|
+
if node.hash?
|
30
|
+
validate_attributes(node)
|
31
|
+
else
|
32
|
+
error(node, 'be a hash')
|
33
|
+
|
34
|
+
node.valid?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_ruby(node)
|
39
|
+
node.pairs.map { |(key, value)| attribute_for_key(key).to_ruby(value) }.to_h
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def attributes_and_schemas_to_inherit_from
|
45
|
+
@attributes_and_schemas_to_inherit_from ||= []
|
46
|
+
end
|
47
|
+
|
48
|
+
def validate_attributes(node)
|
49
|
+
validate_attribute_keys(node) && validate_required_keys(node)
|
50
|
+
validate_alias_uniqueness_of_keys(node)
|
51
|
+
validate_valid_attribute_values(node)
|
52
|
+
|
53
|
+
node.children.all?(&:valid?)
|
54
|
+
end
|
55
|
+
|
56
|
+
def attribute_for_key(node)
|
57
|
+
attributes.find { |attr| attr.name?(node) }
|
58
|
+
end
|
59
|
+
|
60
|
+
def validate_attribute_keys(node)
|
61
|
+
node.each_key { |key| validate_recognized_key(key, node) }
|
62
|
+
node.keys.all?(&:valid?)
|
63
|
+
end
|
64
|
+
|
65
|
+
def validate_alias_uniqueness_of_keys(node)
|
66
|
+
node.keys.select(&:valid?)
|
67
|
+
.group_by { |key| attribute_for_key(key) }
|
68
|
+
.each_value { |keys| error_message_for_non_unique_keys(node, keys) if keys.length > 1 }
|
69
|
+
|
70
|
+
node.valid?
|
71
|
+
end
|
72
|
+
|
73
|
+
def error_message_for_non_unique_keys(node, keys)
|
74
|
+
keys.each { |k| k.error = "#{node.name_}must only use one of #{keys.uniq.join(' or ')}" }
|
75
|
+
end
|
76
|
+
|
77
|
+
def validate_recognized_key(key, node)
|
78
|
+
return true if attribute_for_key(key)
|
79
|
+
|
80
|
+
suggestions = suggestions_for_unrecognized_key(key, node)
|
81
|
+
did_you_mean = "\nDid you mean: #{suggestions.join(', ')}" unless suggestions.empty?
|
82
|
+
key.error = "unrecognized key #{key}#{" for #{node.name}" if node.name}#{did_you_mean}"
|
83
|
+
|
84
|
+
false
|
85
|
+
end
|
86
|
+
|
87
|
+
def suggester
|
88
|
+
@suggester ||= Suggester.new(suggestions)
|
89
|
+
end
|
90
|
+
|
91
|
+
def suggestions_for_unrecognized_key(key, node)
|
92
|
+
suggester.suggest(key.to_ruby) - node.keys.map { |k| attribute_for_key(k)&.name }.compact
|
93
|
+
end
|
94
|
+
|
95
|
+
def suggestions(attributes = self.attributes)
|
96
|
+
attributes.select(&:suggest?).map(&:name)
|
97
|
+
end
|
98
|
+
|
99
|
+
def validate_required_keys(node)
|
100
|
+
missing_groups = require_groups.map do |group|
|
101
|
+
next if node.keys.any? { |key| group.any? { |attr| attr.name?(key) } }
|
102
|
+
|
103
|
+
"include at least one of #{suggestions(group).join(', ')}"
|
104
|
+
end.compact
|
105
|
+
|
106
|
+
error(node, missing_groups.join(' and ')) unless missing_groups.empty?
|
107
|
+
|
108
|
+
node.valid?
|
109
|
+
end
|
110
|
+
|
111
|
+
def validate_valid_attribute_values(node)
|
112
|
+
node.pairs.each { |(k, v)| attribute_for_key(k).validate_value(v) if k.valid? }
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
4
|
+
class ConfigLoader
|
5
|
+
class PrecompileSchema < ObjectSchema
|
6
|
+
attribute :paths, ValueOrArraySchema[StringSchema],
|
7
|
+
aliases: %i{path include_paths include_path}, require_group: :paths
|
8
|
+
|
9
|
+
attribute :format, PrecompilerSchema, require_group: :precompiler
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
4
|
+
class ConfigLoader
|
5
|
+
class RulePatternSchema < ObjectSchema
|
6
|
+
attribute :names, ValueOrArraySchema[StringPatternSchema], aliases: :name,
|
7
|
+
require_group: :matcher
|
8
|
+
attribute :paths, ValueOrArraySchema[StringSchema], aliases: :path, require_group: :matcher
|
9
|
+
attribute :document, TrueSchema, require_group: :matcher
|
10
|
+
attribute :has_arguments, ValueOrArraySchema[HasArgumentSchema], aliases: :has_argument,
|
11
|
+
require_group: :matcher
|
12
|
+
attribute :has_receiver, ValueOrArraySchema[HasValueSchema], require_group: :matcher
|
13
|
+
attribute :type, ValueOrArraySchema[ValueTypeSchema], require_group: :matcher
|
14
|
+
attribute :privacy, ValueOrArraySchema[PrivacySchema], require_group: :matcher
|
15
|
+
attribute :unless, ValueOrArraySchema[RulePatternSchema], require_group: :matcher
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
4
|
+
class ConfigLoader
|
5
|
+
class ScalarArgumentSchema < Schema
|
6
|
+
class << self
|
7
|
+
def validate(node)
|
8
|
+
error(node, 'be a string or an integer') unless node.string? || node.integer?
|
9
|
+
super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
4
|
+
class ConfigLoader
|
5
|
+
class Schema
|
6
|
+
class << self
|
7
|
+
def error(node, requirement)
|
8
|
+
node.error = "#{node.name_}must #{requirement}"
|
9
|
+
|
10
|
+
false
|
11
|
+
end
|
12
|
+
|
13
|
+
def validate(node)
|
14
|
+
node.valid?
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_ruby(node)
|
18
|
+
node.to_ruby
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
4
|
+
class ConfigLoader
|
5
|
+
class StringEnumSchema < Schema
|
6
|
+
class << self
|
7
|
+
def value(value, aliases: nil)
|
8
|
+
values << value
|
9
|
+
Leftovers.each_or_self(aliases) do |alias_name|
|
10
|
+
self.aliases[alias_name] = value
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def aliases
|
15
|
+
@aliases ||= {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def aliases_for(value)
|
19
|
+
aliases.select { |_k, v| v == value }.keys
|
20
|
+
end
|
21
|
+
|
22
|
+
def values
|
23
|
+
@values ||= []
|
24
|
+
end
|
25
|
+
|
26
|
+
def each_value(&block)
|
27
|
+
@values.each(&block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_ruby(node)
|
31
|
+
aliases[node.to_sym]&.to_s || node.to_ruby
|
32
|
+
end
|
33
|
+
|
34
|
+
def validate(node)
|
35
|
+
if node.string?
|
36
|
+
node.error = error_message_with_suggestions(node) unless valid_value?(node.to_sym)
|
37
|
+
else
|
38
|
+
error(node, 'be a string')
|
39
|
+
end
|
40
|
+
|
41
|
+
super
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def valid_value?(val)
|
47
|
+
values.include?(val) || aliases.key?(val)
|
48
|
+
end
|
49
|
+
|
50
|
+
def suggester
|
51
|
+
@suggester ||= Suggester.new(values)
|
52
|
+
end
|
53
|
+
|
54
|
+
def error_message_with_suggestions(node)
|
55
|
+
suggestions = suggester.suggest(node.to_ruby)
|
56
|
+
|
57
|
+
"unrecognized value #{node} for #{node.name}\nDid you mean: #{suggestions.join(', ')}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
4
|
+
class ConfigLoader
|
5
|
+
class StringPatternSchema < ValueOrObjectSchema
|
6
|
+
attribute :match, StringSchema, aliases: :matches, require_group: :matcher
|
7
|
+
attribute :has_prefix, StringSchema, require_group: :matcher
|
8
|
+
attribute :has_suffix, StringSchema, require_group: :matcher
|
9
|
+
attribute :unless, ValueOrArraySchema[StringPatternSchema], require_group: :matcher
|
10
|
+
|
11
|
+
self.or_value_schema = StringSchema
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
4
|
+
class ConfigLoader
|
5
|
+
class Suggester
|
6
|
+
def initialize(words)
|
7
|
+
@words = words
|
8
|
+
@did_you_mean = ::DidYouMean::SpellChecker.new(dictionary: words) if defined?(::DidYouMean)
|
9
|
+
end
|
10
|
+
|
11
|
+
def suggest(word)
|
12
|
+
suggestions = did_you_mean.correct(word) if did_you_mean
|
13
|
+
suggestions = words if !suggestions || suggestions.empty?
|
14
|
+
suggestions
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_reader :words, :did_you_mean
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
4
|
+
class ConfigLoader
|
5
|
+
class TransformSchema < ValueOrObjectSchema
|
6
|
+
ArgumentlessTransformSchema.each_value do |transform|
|
7
|
+
attribute(
|
8
|
+
transform, TrueSchema,
|
9
|
+
aliases: ArgumentlessTransformSchema.aliases_for(transform),
|
10
|
+
require_group: :processor
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
attribute :add_prefix, StringValueProcessorSchema, require_group: :processor
|
15
|
+
attribute :add_suffix, StringValueProcessorSchema, require_group: :processor
|
16
|
+
|
17
|
+
attribute :split, StringSchema, require_group: :processor
|
18
|
+
attribute :delete_prefix, StringSchema, require_group: :processor
|
19
|
+
attribute :delete_suffix, StringSchema, require_group: :processor
|
20
|
+
attribute :delete_before, StringSchema, require_group: :processor
|
21
|
+
attribute :delete_after, StringSchema, require_group: :processor
|
22
|
+
|
23
|
+
self.or_value_schema = ArgumentlessTransformSchema
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
4
|
+
class ConfigLoader
|
5
|
+
class TrueSchema < Schema
|
6
|
+
class << self
|
7
|
+
def validate(node)
|
8
|
+
error(node, 'be true') unless to_ruby(node)
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_ruby(node)
|
13
|
+
node.to_ruby == true || node.to_ruby == 'true'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
4
|
+
class ConfigLoader
|
5
|
+
class ValueMatcherSchema < ValueOrObjectSchema
|
6
|
+
attribute :arguments, ValueOrArraySchema[ArgumentPositionSchema], aliases: :argument,
|
7
|
+
require_group: :matcher
|
8
|
+
attribute :keywords, ValueOrArraySchema[StringPatternSchema], aliases: :keyword,
|
9
|
+
require_group: :matcher
|
10
|
+
attribute :itself, TrueSchema, require_group: :matcher
|
11
|
+
attribute :nested, ValueOrArraySchema[ValueMatcherSchema]
|
12
|
+
attribute :value, StringSchema, require_group: :matcher
|
13
|
+
attribute :recursive, TrueSchema
|
14
|
+
|
15
|
+
self.or_value_schema = ScalarArgumentSchema
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
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
|
+
|
20
|
+
def validate(node)
|
21
|
+
if node.array?
|
22
|
+
validate_length(node) && validate_values(node)
|
23
|
+
else
|
24
|
+
validate_or_schema(node)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_ruby(node)
|
29
|
+
if node.array?
|
30
|
+
Leftovers.unwrap_array(
|
31
|
+
node.children.map do |value|
|
32
|
+
value_schema.to_ruby(value)
|
33
|
+
end
|
34
|
+
)
|
35
|
+
else
|
36
|
+
value_schema.to_ruby(node)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def validate_or_schema(node)
|
43
|
+
value_schema.validate(node)
|
44
|
+
return true if node.valid?
|
45
|
+
|
46
|
+
node.error += ' or an array'
|
47
|
+
|
48
|
+
false
|
49
|
+
end
|
50
|
+
|
51
|
+
def validate_length(node)
|
52
|
+
self.class.error(node, 'not be empty') if node.children.empty?
|
53
|
+
|
54
|
+
node.valid?
|
55
|
+
end
|
56
|
+
|
57
|
+
def validate_values(node)
|
58
|
+
node.children.each do |value|
|
59
|
+
value_schema.validate(value)
|
60
|
+
end
|
61
|
+
|
62
|
+
node.children.all?(&:valid?)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
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
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
4
|
+
class ConfigLoader
|
5
|
+
class ValueProcessorSchema < ValueOrObjectSchema
|
6
|
+
inherit_attributes_from ValueMatcherSchema
|
7
|
+
|
8
|
+
attribute :transforms, ValueOrArraySchema[TransformSchema]
|
9
|
+
inherit_attributes_from TransformSchema, require_group: nil
|
10
|
+
|
11
|
+
self.or_value_schema = ScalarArgumentSchema
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Leftovers
|
4
|
+
class ConfigLoader
|
5
|
+
class ValueTypeSchema < StringEnumSchema
|
6
|
+
value :String
|
7
|
+
value :Symbol
|
8
|
+
value :Integer
|
9
|
+
value :Float
|
10
|
+
value :Array
|
11
|
+
value :Hash
|
12
|
+
value :Proc
|
13
|
+
value :Method
|
14
|
+
value :Constant
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
module Leftovers
|
6
|
+
class ConfigLoader
|
7
|
+
autoload(:ArgumentPositionSchema, "#{__dir__}/config_loader/argument_position_schema")
|
8
|
+
autoload(:ArgumentlessTransformSchema, "#{__dir__}/config_loader/argumentless_transform_schema")
|
9
|
+
autoload(:Attribute, "#{__dir__}/config_loader/attribute")
|
10
|
+
autoload(:BuiltInPrecompilerSchema, "#{__dir__}/config_loader/built_in_precompiler_schema")
|
11
|
+
autoload(:DocumentSchema, "#{__dir__}/config_loader/document_schema")
|
12
|
+
autoload(:DynamicSchema, "#{__dir__}/config_loader/dynamic_schema")
|
13
|
+
autoload(:InheritSchemaAttributes, "#{__dir__}/config_loader/inherit_schema_attributes")
|
14
|
+
autoload(:HasArgumentSchema, "#{__dir__}/config_loader/has_argument_schema")
|
15
|
+
autoload(:HasValueSchema, "#{__dir__}/config_loader/has_value_schema")
|
16
|
+
autoload(:KeepTestOnlySchema, "#{__dir__}/config_loader/keep_test_only_schema")
|
17
|
+
autoload(:Node, "#{__dir__}/config_loader/node")
|
18
|
+
autoload(:ObjectSchema, "#{__dir__}/config_loader/object_schema")
|
19
|
+
autoload(:PrecompileSchema, "#{__dir__}/config_loader/precompile_schema")
|
20
|
+
autoload(:PrecompilerSchema, "#{__dir__}/config_loader/precompiler_schema")
|
21
|
+
autoload(:PrivacyProcessorSchema, "#{__dir__}/config_loader/privacy_processor_schema")
|
22
|
+
autoload(:PrivacySchema, "#{__dir__}/config_loader/privacy_schema")
|
23
|
+
autoload(:RequireSchema, "#{__dir__}/config_loader/require_schema")
|
24
|
+
autoload(:RulePatternSchema, "#{__dir__}/config_loader/rule_pattern_schema")
|
25
|
+
autoload(:ScalarArgumentSchema, "#{__dir__}/config_loader/scalar_argument_schema")
|
26
|
+
autoload(:ScalarValueSchema, "#{__dir__}/config_loader/scalar_value_schema")
|
27
|
+
autoload(:Schema, "#{__dir__}/config_loader/schema")
|
28
|
+
autoload(:Suggester, "#{__dir__}/config_loader/suggester")
|
29
|
+
autoload(:StringEnumSchema, "#{__dir__}/config_loader/string_enum_schema")
|
30
|
+
autoload(:StringPatternSchema, "#{__dir__}/config_loader/string_pattern_schema")
|
31
|
+
autoload(:StringSchema, "#{__dir__}/config_loader/string_schema")
|
32
|
+
autoload(:StringValueProcessorSchema, "#{__dir__}/config_loader/string_value_processor_schema")
|
33
|
+
autoload(:TransformSchema, "#{__dir__}/config_loader/transform_schema")
|
34
|
+
autoload(:TrueSchema, "#{__dir__}/config_loader/true_schema")
|
35
|
+
autoload(:ValueMatcherSchema, "#{__dir__}/config_loader/value_matcher_schema")
|
36
|
+
autoload(:ValueOrArraySchema, "#{__dir__}/config_loader/value_or_array_schema")
|
37
|
+
autoload(:ValueOrObjectSchema, "#{__dir__}/config_loader/value_or_object_schema")
|
38
|
+
autoload(:ValueProcessorSchema, "#{__dir__}/config_loader/value_processor_schema")
|
39
|
+
autoload(:ValueTypeSchema, "#{__dir__}/config_loader/value_type_schema")
|
40
|
+
|
41
|
+
def self.load(name, path: nil, content: nil)
|
42
|
+
new(name, path: path, content: content).load
|
43
|
+
end
|
44
|
+
|
45
|
+
attr_reader :name
|
46
|
+
|
47
|
+
def initialize(name, path: nil, content: nil)
|
48
|
+
@name = name
|
49
|
+
@path = path
|
50
|
+
@content = content
|
51
|
+
end
|
52
|
+
|
53
|
+
def load
|
54
|
+
document = ::Leftovers::ConfigLoader::Node.new(parse, file)
|
55
|
+
DocumentSchema.validate(document)
|
56
|
+
|
57
|
+
all_errors = document.all_errors
|
58
|
+
return DocumentSchema.to_ruby(document) if all_errors.empty?
|
59
|
+
|
60
|
+
Leftovers.error(all_errors.join("\n"))
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def path
|
66
|
+
@path ||= ::File.expand_path("../config/#{name}.yml", __dir__)
|
67
|
+
end
|
68
|
+
|
69
|
+
def file
|
70
|
+
@file ||= ::Leftovers::File.new(path)
|
71
|
+
end
|
72
|
+
|
73
|
+
def content
|
74
|
+
@content ||= file.exist? ? file.read : ''
|
75
|
+
end
|
76
|
+
|
77
|
+
def parse
|
78
|
+
parsed = Psych.parse(content)
|
79
|
+
parsed ||= Psych.parse('{}')
|
80
|
+
parsed.children.first
|
81
|
+
rescue ::Psych::SyntaxError => e
|
82
|
+
message = [e.problem, e.context].compact.join(' ')
|
83
|
+
Leftovers.error "Config SyntaxError: #{file.relative_path}:#{e.line}:#{e.column} #{message}"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|