leftovers 0.3.0 → 0.4.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 +23 -0
- data/README.md +28 -45
- data/docs/Configuration.md +598 -0
- data/exe/leftovers +1 -1
- data/leftovers.gemspec +9 -6
- data/lib/config/attr_encrypted.yml +3 -4
- data/lib/config/audited.yml +9 -4
- data/lib/config/datagrid.yml +1 -1
- data/lib/config/flipper.yml +1 -3
- data/lib/config/graphql.yml +15 -13
- data/lib/config/okcomputer.yml +1 -3
- data/lib/config/parser.yml +89 -91
- data/lib/config/rails.yml +125 -107
- data/lib/config/redcarpet.yml +35 -38
- data/lib/config/rollbar.yml +1 -3
- data/lib/config/rspec.yml +18 -10
- data/lib/config/ruby.yml +40 -50
- data/lib/config/selenium-webdriver.yml +19 -21
- data/lib/config/sidekiq.yml +4 -8
- data/lib/config/will_paginate.yml +12 -14
- data/lib/leftovers.rb +38 -26
- data/lib/leftovers/ast.rb +8 -0
- data/lib/leftovers/ast/builder.rb +4 -4
- data/lib/leftovers/ast/node.rb +50 -77
- data/lib/leftovers/backports.rb +27 -23
- data/lib/leftovers/cli.rb +0 -3
- data/lib/leftovers/collector.rb +4 -8
- data/lib/leftovers/config.rb +19 -25
- data/lib/leftovers/config_validator.rb +60 -0
- data/lib/leftovers/config_validator/error_processor.rb +196 -0
- data/lib/leftovers/config_validator/schema_hash.rb +495 -0
- data/lib/leftovers/definition.rb +11 -36
- data/lib/leftovers/definition_set.rb +7 -17
- data/lib/leftovers/dynamic_processors.rb +11 -0
- data/lib/leftovers/dynamic_processors/call.rb +25 -0
- data/lib/leftovers/dynamic_processors/call_definition.rb +31 -0
- data/lib/leftovers/dynamic_processors/definition.rb +26 -0
- data/lib/leftovers/dynamic_processors/each.rb +19 -0
- data/lib/leftovers/dynamic_processors/null.rb +9 -0
- data/lib/leftovers/erb.rb +2 -2
- data/lib/leftovers/file.rb +3 -5
- data/lib/leftovers/file_collector.rb +65 -49
- data/lib/leftovers/file_list.rb +0 -1
- data/lib/leftovers/haml.rb +1 -1
- data/lib/leftovers/matcher_builders.rb +24 -0
- data/lib/leftovers/matcher_builders/and.rb +19 -0
- data/lib/leftovers/matcher_builders/and_not.rb +14 -0
- data/lib/leftovers/matcher_builders/argument_node_value.rb +21 -0
- data/lib/leftovers/matcher_builders/name.rb +29 -0
- data/lib/leftovers/matcher_builders/node.rb +40 -0
- data/lib/leftovers/matcher_builders/node_has_argument.rb +71 -0
- data/lib/leftovers/matcher_builders/node_has_keyword_argument.rb +22 -0
- data/lib/leftovers/matcher_builders/node_has_positional_argument.rb +24 -0
- data/lib/leftovers/matcher_builders/node_name.rb +15 -0
- data/lib/leftovers/matcher_builders/node_pair_name.rb +18 -0
- data/lib/leftovers/matcher_builders/node_pair_value.rb +16 -0
- data/lib/leftovers/matcher_builders/node_path.rb +14 -0
- data/lib/leftovers/matcher_builders/node_type.rb +28 -0
- data/lib/leftovers/matcher_builders/or.rb +73 -0
- data/lib/leftovers/matcher_builders/path.rb +15 -0
- data/lib/leftovers/matcher_builders/string.rb +11 -0
- data/lib/leftovers/matcher_builders/string_pattern.rb +19 -0
- data/lib/leftovers/matcher_builders/unless.rb +13 -0
- data/lib/leftovers/matchers.rb +26 -0
- data/lib/leftovers/matchers/all.rb +25 -0
- data/lib/leftovers/matchers/and.rb +24 -0
- data/lib/leftovers/matchers/any.rb +27 -0
- data/lib/leftovers/matchers/node_has_any_keyword_argument.rb +28 -0
- data/lib/leftovers/matchers/node_has_any_positional_argument_with_value.rb +25 -0
- data/lib/leftovers/matchers/node_has_positional_argument.rb +23 -0
- data/lib/leftovers/matchers/node_has_positional_argument_with_value.rb +25 -0
- data/lib/leftovers/matchers/node_name.rb +23 -0
- data/lib/leftovers/matchers/node_pair_value.rb +23 -0
- data/lib/leftovers/matchers/node_path.rb +23 -0
- data/lib/leftovers/matchers/node_scalar_value.rb +25 -0
- data/lib/leftovers/matchers/node_type.rb +23 -0
- data/lib/leftovers/matchers/not.rb +23 -0
- data/lib/leftovers/matchers/or.rb +26 -0
- data/lib/leftovers/merged_config.rb +13 -8
- data/lib/leftovers/parser.rb +1 -4
- data/lib/leftovers/processor_builders.rb +22 -0
- data/lib/leftovers/processor_builders/action.rb +63 -0
- data/lib/leftovers/processor_builders/add_prefix.rb +20 -0
- data/lib/leftovers/processor_builders/add_suffix.rb +20 -0
- data/lib/leftovers/processor_builders/argument.rb +25 -0
- data/lib/leftovers/processor_builders/dynamic.rb +27 -0
- data/lib/leftovers/processor_builders/each.rb +36 -0
- data/lib/leftovers/processor_builders/each_action.rb +51 -0
- data/lib/leftovers/processor_builders/each_dynamic.rb +54 -0
- data/lib/leftovers/processor_builders/each_for_definition_set.rb +36 -0
- data/lib/leftovers/processor_builders/itself.rb +13 -0
- data/lib/leftovers/processor_builders/keyword.rb +24 -0
- data/lib/leftovers/processor_builders/keyword_argument.rb +14 -0
- data/lib/leftovers/processor_builders/transform.rb +55 -0
- data/lib/leftovers/processor_builders/transform_chain.rb +24 -0
- data/lib/leftovers/processor_builders/transform_set.rb +47 -0
- data/lib/leftovers/processor_builders/value.rb +13 -0
- data/lib/leftovers/rake_task.rb +1 -1
- data/lib/leftovers/reporter.rb +1 -1
- data/lib/leftovers/value_processors.rb +40 -0
- data/lib/leftovers/value_processors/add_dynamic_prefix.rb +31 -0
- data/lib/leftovers/value_processors/add_dynamic_suffix.rb +31 -0
- data/lib/leftovers/value_processors/add_prefix.rb +20 -0
- data/lib/leftovers/value_processors/add_suffix.rb +20 -0
- data/lib/leftovers/value_processors/camelize.rb +24 -0
- data/lib/leftovers/value_processors/capitalize.rb +19 -0
- data/lib/leftovers/value_processors/deconstantize.rb +24 -0
- data/lib/leftovers/value_processors/delete_after.rb +22 -0
- data/lib/leftovers/value_processors/delete_before.rb +22 -0
- data/lib/leftovers/value_processors/delete_prefix.rb +26 -0
- data/lib/leftovers/value_processors/delete_suffix.rb +26 -0
- data/lib/leftovers/value_processors/demodulize.rb +24 -0
- data/lib/leftovers/value_processors/downcase.rb +19 -0
- data/lib/leftovers/value_processors/each.rb +21 -0
- data/lib/leftovers/value_processors/each_for_definition_set.rb +33 -0
- data/lib/leftovers/value_processors/each_keyword.rb +27 -0
- data/lib/leftovers/value_processors/each_keyword_argument.rb +27 -0
- data/lib/leftovers/value_processors/each_positional_argument.rb +24 -0
- data/lib/leftovers/value_processors/itself.rb +17 -0
- data/lib/leftovers/value_processors/keyword.rb +36 -0
- data/lib/leftovers/value_processors/keyword_argument.rb +36 -0
- data/lib/leftovers/value_processors/parameterize.rb +24 -0
- data/lib/leftovers/value_processors/placeholder.rb +18 -0
- data/lib/leftovers/value_processors/pluralize.rb +24 -0
- data/lib/leftovers/value_processors/positional_argument.rb +26 -0
- data/lib/leftovers/value_processors/replace_value.rb +18 -0
- data/lib/leftovers/value_processors/return_definition.rb +24 -0
- data/lib/leftovers/value_processors/return_string.rb +14 -0
- data/lib/leftovers/value_processors/singularize.rb +24 -0
- data/lib/leftovers/value_processors/split.rb +22 -0
- data/lib/leftovers/value_processors/swapcase.rb +19 -0
- data/lib/leftovers/value_processors/titleize.rb +24 -0
- data/lib/leftovers/value_processors/underscore.rb +24 -0
- data/lib/leftovers/value_processors/upcase.rb +19 -0
- data/lib/leftovers/version.rb +1 -1
- metadata +143 -24
- data/lib/leftovers/argument_rule.rb +0 -229
- data/lib/leftovers/core_ext.rb +0 -15
- data/lib/leftovers/hash_rule.rb +0 -42
- data/lib/leftovers/name_rule.rb +0 -98
- data/lib/leftovers/rule.rb +0 -82
- data/lib/leftovers/transform_rule.rb +0 -169
- data/lib/leftovers/value_rule.rb +0 -57
@@ -1,229 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'definition'
|
4
|
-
require_relative 'definition_set'
|
5
|
-
require_relative 'name_rule'
|
6
|
-
require_relative 'transform_rule'
|
7
|
-
require_relative 'hash_rule'
|
8
|
-
|
9
|
-
module Leftovers
|
10
|
-
class ArgumentRule # rubocop:disable Metrics/ClassLength
|
11
|
-
def self.wrap(rules, definer: false) # rubocop:disable Metrics/MethodLength
|
12
|
-
case rules
|
13
|
-
when Array
|
14
|
-
rules.flat_map { |r| wrap(r, definer: definer) }
|
15
|
-
when Hash
|
16
|
-
[new(**rules, definer: definer)]
|
17
|
-
else
|
18
|
-
[]
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
ADDITIONAL_VALID_KEYS = Leftovers::TransformRule::VALID_TRANSFORMS + %i{if unless}
|
23
|
-
def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength, Metrics/CyclomaticComplexity
|
24
|
-
argument: nil,
|
25
|
-
arguments: nil,
|
26
|
-
key: nil,
|
27
|
-
keys: nil,
|
28
|
-
itself: false,
|
29
|
-
linked_transforms: nil,
|
30
|
-
transforms: nil,
|
31
|
-
definer: false,
|
32
|
-
**options
|
33
|
-
)
|
34
|
-
assert_valid_keys(options, ADDITIONAL_VALID_KEYS)
|
35
|
-
prepare_argument(argument, arguments)
|
36
|
-
@key = prepare_key(key, keys)
|
37
|
-
@itself = itself
|
38
|
-
|
39
|
-
unless @positions || @keywords || @all_positions || @all_keywords || @key || @itself
|
40
|
-
raise Leftovers::ConfigError, "require at least one of 'argument(s)', 'key(s)', itself"
|
41
|
-
end
|
42
|
-
|
43
|
-
@if = prepare_condition(options.delete(:if))
|
44
|
-
@unless = prepare_condition(options.delete(:unless))
|
45
|
-
@transforms = prepare_transform(options, transforms, linked_transforms)
|
46
|
-
@definer = definer
|
47
|
-
end
|
48
|
-
|
49
|
-
def prepare_transform(options, transforms, linked_transforms) # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/AbcSize
|
50
|
-
if linked_transforms && transforms
|
51
|
-
raise Leftovers::ConfigError, 'Only use one of linked_transforms/transforms'
|
52
|
-
end
|
53
|
-
return if !linked_transforms && !transforms && options.empty?
|
54
|
-
|
55
|
-
if !(linked_transforms || transforms)
|
56
|
-
@transform = TransformRule.new(options)
|
57
|
-
else
|
58
|
-
@linked = !!linked_transforms
|
59
|
-
|
60
|
-
transforms = Leftovers.array_wrap(linked_transforms || transforms).map do |transform|
|
61
|
-
transform = { transform.to_sym => true } if transform.is_a?(String)
|
62
|
-
Leftovers::TransformRule.new(options.merge(transform))
|
63
|
-
end
|
64
|
-
|
65
|
-
if transforms.length <= 1
|
66
|
-
@transform = transforms.first
|
67
|
-
else
|
68
|
-
@transforms = transforms
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
def prepare_condition(conditions) # rubocop:disable Metrics/MethodLength
|
74
|
-
Leftovers.array_wrap(conditions).each do |cond|
|
75
|
-
unless cond.is_a?(Hash) && cond.keys == [:has_argument]
|
76
|
-
raise Leftovers::ConfigError, <<~MESSAGE
|
77
|
-
Invalid condition #{cond.inspect}. Valid condition keys are: has_argument
|
78
|
-
MESSAGE
|
79
|
-
end
|
80
|
-
|
81
|
-
cond[:has_argument] = HashRule.new(cond[:has_argument])
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
def prepare_key(key, keys)
|
86
|
-
raise Leftovers::ConfigError, 'Only use one of key/keys' if key && keys
|
87
|
-
|
88
|
-
key || keys
|
89
|
-
end
|
90
|
-
|
91
|
-
def prepare_argument(argument, arguments) # rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize
|
92
|
-
raise Leftovers::ConfigError, 'Only use one of argument/arguments' if argument && arguments
|
93
|
-
|
94
|
-
positions = Set.new
|
95
|
-
keywords = []
|
96
|
-
|
97
|
-
Leftovers.each_or_self(argument || arguments) do |arg|
|
98
|
-
case arg
|
99
|
-
when '*'
|
100
|
-
@all_positions = true
|
101
|
-
when '**'
|
102
|
-
@all_keywords = true
|
103
|
-
when Integer
|
104
|
-
positions << arg - 1
|
105
|
-
when String, Symbol, Hash
|
106
|
-
keywords << arg
|
107
|
-
else
|
108
|
-
raise Leftovers::ConfigError, <<~MESSAGE
|
109
|
-
Invalid value for argument: #{arg.inspect}. Must by a string ('*', '**', or a keyword), or a hash with the name match rules, or an integer, or an array of these
|
110
|
-
MESSAGE
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
@positions = positions unless @all_positions || positions.empty? || @all_positions
|
115
|
-
@keywords = NameRule.wrap(keywords) unless @all_keywords || keywords.empty?
|
116
|
-
end
|
117
|
-
|
118
|
-
def matches(method_node) # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/AbcSize
|
119
|
-
return [].freeze unless all_conditions_match?(method_node)
|
120
|
-
|
121
|
-
result = []
|
122
|
-
|
123
|
-
if @all_positions
|
124
|
-
result.leftovers_append values(method_node.positional_arguments, method_node)
|
125
|
-
elsif @positions
|
126
|
-
result.leftovers_append(
|
127
|
-
values(method_node.positional_arguments_at(@positions).compact, method_node)
|
128
|
-
)
|
129
|
-
end
|
130
|
-
|
131
|
-
if @keywords || @all_keywords || @key
|
132
|
-
result.leftovers_append hash_values(method_node.kwargs, method_node)
|
133
|
-
end
|
134
|
-
result << method_value(method_node) if @itself
|
135
|
-
|
136
|
-
result
|
137
|
-
end
|
138
|
-
|
139
|
-
def values(value_nodes, method_node)
|
140
|
-
value_nodes.flat_map { |value_node| value(value_node, method_node) }.compact
|
141
|
-
end
|
142
|
-
|
143
|
-
def all_conditions_match?(method_node)
|
144
|
-
@if.all? { |c| condition_match?(c, method_node) } &&
|
145
|
-
@unless.all? { |c| !condition_match?(c, method_node) }
|
146
|
-
end
|
147
|
-
|
148
|
-
def condition_match?(condition, method_node)
|
149
|
-
method_node.positional_arguments.each.with_index(1).any? do |value, index|
|
150
|
-
condition[:has_argument].match_pair?(index, value)
|
151
|
-
end || method_node.kwargs&.each_pair&.any? do |key, value|
|
152
|
-
condition[:has_argument].match_pair?(key.to_sym, value)
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
def hash_values(hash_node, method_node) # rubocop:disable Metrics/MethodLength
|
157
|
-
return unless hash_node
|
158
|
-
|
159
|
-
value_nodes = []
|
160
|
-
value_nodes.concat hash_node.keys if @key
|
161
|
-
|
162
|
-
if @all_keywords
|
163
|
-
value_nodes.concat(hash_node.values)
|
164
|
-
elsif @keywords
|
165
|
-
value_nodes.concat(hash_node.values_at_match(@keywords))
|
166
|
-
end
|
167
|
-
|
168
|
-
values(value_nodes, method_node)
|
169
|
-
end
|
170
|
-
|
171
|
-
def value(value_node, method_node) # rubocop:disable Metrics/MethodLength
|
172
|
-
value_node = value_node.unwrap_freeze
|
173
|
-
|
174
|
-
case value_node.type
|
175
|
-
when :array
|
176
|
-
values(value_node.values, method_node)
|
177
|
-
when :hash
|
178
|
-
hash_values(value_node, method_node)
|
179
|
-
when :str, :sym
|
180
|
-
symbol_values(value_node, method_node)
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
SPLIT = /[.:]+/.freeze
|
185
|
-
def symbol_values(symbol_node, method_node) # rubocop:disable Metrics/MethodLength
|
186
|
-
subnodes = Array(transform(symbol_node.to_s, method_node))
|
187
|
-
.flat_map { |s| s.to_s.split(SPLIT).map(&:to_sym) }
|
188
|
-
|
189
|
-
return subnodes unless @definer
|
190
|
-
|
191
|
-
location = symbol_node.loc.expression
|
192
|
-
if @linked
|
193
|
-
Leftovers::DefinitionSet.new(subnodes, location: location, method_node: method_node)
|
194
|
-
else
|
195
|
-
subnodes.map do |subnode|
|
196
|
-
Leftovers::Definition.new(subnode, location: location, method_node: method_node)
|
197
|
-
end
|
198
|
-
end
|
199
|
-
end
|
200
|
-
|
201
|
-
def method_value(method_node)
|
202
|
-
value = transform(method_node.to_s, method_node)
|
203
|
-
|
204
|
-
return value unless @definer
|
205
|
-
|
206
|
-
Leftovers::Definition.new(value, method_node: method_node)
|
207
|
-
end
|
208
|
-
|
209
|
-
def transform(string, method_node)
|
210
|
-
return string unless @transform || @transforms
|
211
|
-
return @transform.transform(string, method_node) if @transform
|
212
|
-
|
213
|
-
@transforms.map do |transform|
|
214
|
-
transform.transform(string, method_node)
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
def assert_valid_keys(options, keys) # rubocop:disable Metrics/MethodLength
|
219
|
-
invalid = options.keys - keys
|
220
|
-
|
221
|
-
return if invalid.empty?
|
222
|
-
|
223
|
-
raise(
|
224
|
-
Leftovers::ConfigError,
|
225
|
-
"unknown keyword#{'s' if invalid.length > 1}: #{invalid.join(', ')}"
|
226
|
-
)
|
227
|
-
end
|
228
|
-
end
|
229
|
-
end
|
data/lib/leftovers/core_ext.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'set'
|
4
|
-
|
5
|
-
class Array
|
6
|
-
def leftovers_append(other) # rubocop:disable Metrics/MethodLength
|
7
|
-
return self if other.nil?
|
8
|
-
|
9
|
-
if other.respond_to?(:to_a)
|
10
|
-
concat(other.to_a)
|
11
|
-
else
|
12
|
-
self << other
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
data/lib/leftovers/hash_rule.rb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'set'
|
4
|
-
require_relative 'value_rule'
|
5
|
-
require_relative 'name_rule'
|
6
|
-
|
7
|
-
module Leftovers
|
8
|
-
class HashRule
|
9
|
-
# :nocov:
|
10
|
-
using ::Leftovers::SetCaseEq if defined?(::Leftovers::SetCaseEq)
|
11
|
-
# :nocov:
|
12
|
-
|
13
|
-
def initialize(patterns) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
14
|
-
keys = []
|
15
|
-
pairs = []
|
16
|
-
Leftovers.each_or_self(patterns) do |pat|
|
17
|
-
if pat.is_a?(Hash) && pat[:value]
|
18
|
-
pairs << [
|
19
|
-
NameRule.wrap(pat[:keyword] || pat[:index]),
|
20
|
-
ValueRule.new(pat[:value])
|
21
|
-
]
|
22
|
-
else
|
23
|
-
keys << NameRule.wrap(pat)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
@keys = NameRule.wrap(keys, false)
|
28
|
-
|
29
|
-
@pairs = (pairs unless pairs.empty?)
|
30
|
-
|
31
|
-
freeze
|
32
|
-
end
|
33
|
-
|
34
|
-
def match_pair?(key, value_node)
|
35
|
-
return true if @keys === key
|
36
|
-
|
37
|
-
@pairs&.any? do |(key_rule, value_rule)|
|
38
|
-
key_rule === key && value_rule.match?(value_node)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
data/lib/leftovers/name_rule.rb
DELETED
@@ -1,98 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'set'
|
4
|
-
module Leftovers
|
5
|
-
class NameRule
|
6
|
-
# :nocov:
|
7
|
-
using ::Leftovers::SetCaseEq if defined?(::Leftovers::SetCaseEq)
|
8
|
-
# :nocov:
|
9
|
-
|
10
|
-
module TrueReturner
|
11
|
-
def self.===(_value)
|
12
|
-
true
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
module FalseReturner
|
17
|
-
def self.===(_value)
|
18
|
-
false
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
attr_reader :syms, :regexp
|
23
|
-
|
24
|
-
def self.wrap(patterns, default = true) # rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize
|
25
|
-
regexps = []
|
26
|
-
syms = Set.new
|
27
|
-
|
28
|
-
Leftovers.each_or_self(patterns) do |pat| # rubocop:disable Metrics/BlockLength
|
29
|
-
# can't have these as part of the case statement because case equality
|
30
|
-
next if pat == Leftovers::NameRule::TrueReturner
|
31
|
-
next if pat == Leftovers::NameRule::FalseReturner
|
32
|
-
|
33
|
-
case pat
|
34
|
-
when nil
|
35
|
-
nil # do nothing
|
36
|
-
when Leftovers::NameRule
|
37
|
-
pat.syms.is_a?(Set) ? syms.merge(pat.syms) : syms << pat.syms
|
38
|
-
regexps << pat.regexp
|
39
|
-
when String
|
40
|
-
syms.merge(pat.split(/\s+/).map(&:to_sym))
|
41
|
-
when Symbol
|
42
|
-
syms << pat
|
43
|
-
when Integer
|
44
|
-
syms << pat
|
45
|
-
when Set
|
46
|
-
syms.merge(pat)
|
47
|
-
when Regexp
|
48
|
-
regexps << pat
|
49
|
-
when Hash
|
50
|
-
if pat[:match]
|
51
|
-
regexps << /\A#{pat[:match]}\z/
|
52
|
-
elsif pat[:matches]
|
53
|
-
regexps << /\A#{pat[:matches]}\z/
|
54
|
-
elsif pat[:has_prefix] && pat[:has_suffix]
|
55
|
-
regexps << /\A#{Regexp.escape(pat[:has_prefix])}.*#{Regexp.escape(pat[:has_suffix])}\z/
|
56
|
-
elsif pat[:has_prefix]
|
57
|
-
regexps << /\A#{Regexp.escape(pat[:has_prefix])}/
|
58
|
-
elsif pat[:has_suffix]
|
59
|
-
regexps << /#{Regexp.escape(pat[:has_suffix])}\z/
|
60
|
-
else
|
61
|
-
raise Leftovers::ConfigError, "Invalid value for name #{pat}, "\
|
62
|
-
'valid keys are matches, has_prefix, has_suffix'
|
63
|
-
end
|
64
|
-
else
|
65
|
-
raise Leftovers::ConfigError, "Invalid value type for name #{pat}, "\
|
66
|
-
'valid types are a String, or an object with keys matches, has_prefix, has_suffix'
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
syms = syms.first if syms.length <= 1
|
71
|
-
|
72
|
-
regexp = if regexps.empty?
|
73
|
-
nil
|
74
|
-
else
|
75
|
-
Regexp.union(regexps).freeze
|
76
|
-
end
|
77
|
-
|
78
|
-
if syms && regexp
|
79
|
-
new(syms, regexp)
|
80
|
-
else
|
81
|
-
syms || regexp || (
|
82
|
-
default ? ::Leftovers::NameRule::TrueReturner : ::Leftovers::NameRule::FalseReturner
|
83
|
-
)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def initialize(syms, regexp)
|
88
|
-
@syms = syms
|
89
|
-
@regexp = regexp
|
90
|
-
|
91
|
-
freeze
|
92
|
-
end
|
93
|
-
|
94
|
-
def ===(sym)
|
95
|
-
@syms === sym || @regexp === sym
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
data/lib/leftovers/rule.rb
DELETED
@@ -1,82 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'name_rule'
|
4
|
-
require_relative 'argument_rule'
|
5
|
-
require 'fast_ignore'
|
6
|
-
|
7
|
-
module Leftovers
|
8
|
-
class Rule
|
9
|
-
# :nocov:
|
10
|
-
using ::Leftovers::SetCaseEq if defined?(::Leftovers::SetCaseEq)
|
11
|
-
# :nocov:
|
12
|
-
|
13
|
-
def self.wrap(rules)
|
14
|
-
case rules
|
15
|
-
when Array then rules.flat_map { |r| wrap(r) }
|
16
|
-
when nil then [].freeze
|
17
|
-
else new(**rules)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
attr_reader :skip
|
22
|
-
alias_method :skip?, :skip
|
23
|
-
|
24
|
-
def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize
|
25
|
-
name: nil,
|
26
|
-
names: nil,
|
27
|
-
calls: nil,
|
28
|
-
call: nil,
|
29
|
-
skip: false,
|
30
|
-
defines: nil,
|
31
|
-
define: nil,
|
32
|
-
path: nil,
|
33
|
-
paths: nil
|
34
|
-
)
|
35
|
-
raise Leftovers::ConfigError, 'Only use one of name/names' if name && names
|
36
|
-
raise Leftovers::ConfigError, 'Only use one of path/paths' if path && paths
|
37
|
-
raise Leftovers::ConfigError, 'Only use one of call/calls' if call && calls
|
38
|
-
raise Leftovers::ConfigError, 'Only use one of define/defines' if define && defines
|
39
|
-
if skip && (defines || calls || define || call)
|
40
|
-
raise Leftovers::ConfigError, "skip can't exist with defines or calls"
|
41
|
-
end
|
42
|
-
|
43
|
-
@name_matcher = NameRule.wrap(name || names)
|
44
|
-
if path || paths
|
45
|
-
@path = FastIgnore.new(include_rules: path || paths, gitignore: false, root: Leftovers.pwd)
|
46
|
-
end
|
47
|
-
@skip = skip
|
48
|
-
|
49
|
-
begin
|
50
|
-
@calls = ArgumentRule.wrap(calls)
|
51
|
-
rescue ArgumentError, Leftovers::ConfigError => e
|
52
|
-
raise e, "#{e.message} for calls", e.backtrace
|
53
|
-
end
|
54
|
-
|
55
|
-
begin
|
56
|
-
@defines = ArgumentRule.wrap(defines, definer: true)
|
57
|
-
rescue ArgumentError, Leftovers::ConfigError => e
|
58
|
-
raise e, "#{e.message} for defines", e.backtrace
|
59
|
-
end
|
60
|
-
rescue ArgumentError, Leftovers::ConfigError => e
|
61
|
-
raise e, "#{e.message} for #{Array(name || names).map(&:to_s).join(', ')}", e.backtrace
|
62
|
-
end
|
63
|
-
|
64
|
-
def filename?(file)
|
65
|
-
return true unless @path
|
66
|
-
|
67
|
-
@path.allowed?(file)
|
68
|
-
end
|
69
|
-
|
70
|
-
def match?(name, file)
|
71
|
-
@name_matcher === name && filename?(file)
|
72
|
-
end
|
73
|
-
|
74
|
-
def calls(node)
|
75
|
-
@calls.flat_map { |m| m.matches(node) }
|
76
|
-
end
|
77
|
-
|
78
|
-
def definitions(node)
|
79
|
-
@defines.flat_map { |m| m.matches(node) }
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|