api_recipes 3.0.0.beta3 → 3.0.0.beta5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +377 -76
  3. data/UPGRADING.md +223 -59
  4. data/lib/api_recipes/cli/calls.rb +27 -0
  5. data/lib/api_recipes/cli/code.rb +153 -0
  6. data/lib/api_recipes/cli/convert/migration.rb +819 -0
  7. data/lib/api_recipes/cli/convert/snippet.rb +67 -0
  8. data/lib/api_recipes/cli/convert.rb +614 -0
  9. data/lib/api_recipes/cli/description.rb +65 -0
  10. data/lib/api_recipes/cli/import/server.rb +20 -9
  11. data/lib/api_recipes/cli/import/writer.rb +16 -4
  12. data/lib/api_recipes/cli/import.rb +23 -9
  13. data/lib/api_recipes/cli/recipes/summary.rb +25 -6
  14. data/lib/api_recipes/cli/recipes.rb +5 -2
  15. data/lib/api_recipes/cli/routes.rb +63 -8
  16. data/lib/api_recipes/cli/tags.rb +19 -0
  17. data/lib/api_recipes/cli/upgrade/fix.rb +206 -0
  18. data/lib/api_recipes/cli/upgrade/project.rb +194 -0
  19. data/lib/api_recipes/cli/upgrade/screen.rb +165 -0
  20. data/lib/api_recipes/cli/upgrade/tree_rules.rb +290 -0
  21. data/lib/api_recipes/cli/upgrade.rb +706 -57
  22. data/lib/api_recipes/cli.rb +171 -17
  23. data/lib/api_recipes/configuration.rb +17 -95
  24. data/lib/api_recipes/endpoint.rb +1 -1
  25. data/lib/api_recipes/internals/compiler.rb +102 -67
  26. data/lib/api_recipes/internals/context.rb +35 -6
  27. data/lib/api_recipes/internals/declaration.rb +17 -15
  28. data/lib/api_recipes/internals/definition.rb +16 -3
  29. data/lib/api_recipes/internals/option_value.rb +92 -0
  30. data/lib/api_recipes/internals/recipe.rb +1 -1
  31. data/lib/api_recipes/internals/recipe_lock.rb +12 -9
  32. data/lib/api_recipes/internals/request.rb +17 -6
  33. data/lib/api_recipes/version.rb +1 -1
  34. data/lib/api_recipes.rb +1 -0
  35. metadata +13 -1
@@ -0,0 +1,290 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiRecipes
4
+ module CLI
5
+ class Upgrade
6
+ # What changed in 3.0 and is found by reading the Ruby of a file as a
7
+ # tree, where a line alone does not tell. Nothing is run.
8
+ module TreeRules
9
+ Found = Data.define(:line, :kind, :message)
10
+
11
+ WRITERS = %i[authorization= basic_auth=].freeze
12
+ SENDS = %i[send public_send __send__].freeze
13
+ ADDRESS = %w[host protocol port].freeze
14
+ CREDENTIALS = %w[authorization basic_auth].freeze
15
+ WRITER = 'authorization= and basic_auth= now work on an object only: on ApiRecipes.name, on a class and on a module ' \
16
+ 'they raise. Give the credential to config.values, to `api :name, authorization: ...`, or to with(...) for one request'
17
+ NOT_TEXT = 'a credential must be a String: a Symbol or a number raises InvalidCredential, convert it with to_s'
18
+ RUN_VALUE = 'run with a block now returns the Response, not what the block returns: take the value inside the block, or from the response'
19
+ RESPONSE = {
20
+ request: 'request was removed from the response, because the request holds the credentials: read uri, or original_response',
21
+ to_s: 'to_s on a response now returns the body, as the http.rb response does: it was the default text of the object',
22
+ respond_to?: 'respond_to? on a response now tells the truth: true for the methods the response forwards, false for the others'
23
+ }.freeze
24
+
25
+ # What gives an api in a file: the names an api is called by, and
26
+ # the variables that hold one.
27
+ Scope = Data.define(:names, :holders)
28
+
29
+ # uses_gem: whether the code names ApiRecipes or calls an api. The
30
+ # rules that also match other code are applied only then, and only
31
+ # to what an api gives.
32
+ # reported: the lines where a rule on lines already found a writer.
33
+ # apis: the names the apis of the project are called by; those that
34
+ # the file declares with `api :name` are added.
35
+ def self.findings(text, uses_gem:, reported: [], apis: [])
36
+ tree = Code.tree(text)
37
+ return [] unless tree
38
+
39
+ found = []
40
+ scope = uses_gem ? scope_of(tree, apis) : nil
41
+ responses = uses_gem ? responses_of(tree, scope) : []
42
+ each(tree) do |node, parents|
43
+ constants(node, found) if node.is_a?(Prism::ClassNode) || node.is_a?(Prism::ModuleNode)
44
+ next unless uses_gem && node.is_a?(Prism::CallNode)
45
+
46
+ declaration(node, found)
47
+ writer(node, found, reported, scope)
48
+ block_value(node, parents, found, scope)
49
+ response(node, responses, found)
50
+ end
51
+ found.uniq.sort_by(&:line)
52
+ end
53
+
54
+ def self.scope_of(tree, apis)
55
+ names = apis.map(&:to_sym)
56
+ each(tree) do |node, _|
57
+ next unless node.is_a?(Prism::CallNode) && node.name == :api && node.receiver.nil?
58
+
59
+ first = node.arguments&.arguments&.first
60
+ names << first.unescaped.to_sym if first.is_a?(Prism::SymbolNode)
61
+ as = keywords(node).to_h['as']
62
+ names << as.unescaped.to_sym if as.is_a?(Prism::SymbolNode)
63
+ end
64
+ scope = Scope.new(names: names.uniq, holders: [])
65
+ # A variable or a method that gives an api can give another one:
66
+ # twice through the file finds those.
67
+ 2.times do
68
+ each(tree) do |node, _|
69
+ scope.holders << node.name if HOLDER.match?(node.class.name) && api?(node.value, scope)
70
+ scope.names << node.name if node.is_a?(Prism::DefNode) && api?(returned(node), scope)
71
+ end
72
+ end
73
+ scope
74
+ end
75
+
76
+ # What assigns a variable: =, ||=, &&= and the others.
77
+ HOLDER = /::(Local|Instance|Class|Global)Variable(Or|And|Operator)?WriteNode\z/
78
+
79
+ # What a method returns: the last statement of its body.
80
+ def self.returned(definition)
81
+ body = definition.body
82
+ body.is_a?(Prism::StatementsNode) ? body.body.last : body
83
+ end
84
+
85
+ # Whether node gives an api, or a part of one: ApiRecipes, a call of
86
+ # an api by its name, or a variable that holds one.
87
+ def self.api?(node, scope)
88
+ case node
89
+ when Prism::CallNode
90
+ scope.names.include?(node.name) || (node.receiver ? api?(node.receiver, scope) : false)
91
+ when Prism::ConstantReadNode, Prism::ConstantPathNode then node.slice.delete_prefix('::') == 'ApiRecipes'
92
+ when Prism::LocalVariableReadNode, Prism::InstanceVariableReadNode then scope.holders.include?(node.name)
93
+ else false
94
+ end
95
+ end
96
+
97
+ # Every node of the tree with the nodes that contain it, the nearest last.
98
+ def self.each(node, parents = [], &block)
99
+ return unless node
100
+
101
+ block.call(node, parents)
102
+ inside = parents + [node]
103
+ node.compact_child_nodes.each { |child| each(child, inside, &block) }
104
+ end
105
+
106
+ def self.at(node) = node.location.start_line
107
+
108
+ # The keys of the keywords of a call, when they are written as names.
109
+ def self.keywords(call)
110
+ hash = call.arguments&.arguments&.last
111
+ return [] unless hash.is_a?(Prism::KeywordHashNode)
112
+
113
+ hash.elements.filter_map { |pair| pair.is_a?(Prism::AssocNode) && pair.key.is_a?(Prism::SymbolNode) ? [pair.key.unescaped, pair.value] : nil }
114
+ end
115
+
116
+ # `api :name`: a name that is already a method, an address of its own
117
+ # without a credential of its own, a credential that is not text.
118
+ def self.declaration(call, found)
119
+ name = call.arguments&.arguments&.first
120
+ return unless call.name == :api && call.receiver.nil? && name.is_a?(Prism::SymbolNode)
121
+
122
+ given = keywords(call)
123
+ keys = given.map(&:first)
124
+ if !keys.include?('as') && taken?(name.unescaped)
125
+ found << Found.new(at(call), :breaks, "api :#{shown(name.unescaped)} raises ApiNameClashError, because a class already has a method " \
126
+ "with that name: give another one, as in `api :#{shown(name.unescaped)}, as: :another_name`")
127
+ end
128
+ if (keys & ADDRESS).any? && (keys & CREDENTIALS).empty?
129
+ found << Found.new(at(call), :check, "this class gives the api its own #{(keys & ADDRESS).join(', ')}: the credential of the application is " \
130
+ 'not sent to another address, so give the class its own, as in `api :name, host: ..., authorization: ...`')
131
+ end
132
+ given.each { |key, value| found << Found.new(at(value), :breaks, NOT_TEXT) if key == 'authorization' && not_text?(value) }
133
+ end
134
+
135
+ def self.shown(name) = name.match?(/\A\w{1,40}[?!]?\z/) ? name : 'name'
136
+
137
+ # Whether a class cannot have a method of that name for an api:
138
+ # what Declaration checks, on a class with nothing of its own.
139
+ def self.taken?(name)
140
+ return false unless Internals::Compiler::METHOD_NAME.match?(name)
141
+
142
+ probe = Class.new
143
+ Internals::Handles::RESERVED.include?(name.to_sym) || probe.respond_to?(name, true) || probe.method_defined?(name) ||
144
+ probe.private_method_defined?(name)
145
+ end
146
+
147
+ def self.not_text?(value) = [Prism::SymbolNode, Prism::IntegerNode, Prism::FloatNode].any? { |kind| value.is_a?(kind) }
148
+
149
+ # A credential assigned with a writer: where a rule on lines did not
150
+ # see it (in the body of a class, through a variable, over two
151
+ # lines, with send), and a value that is not text.
152
+ def self.writer(call, found, reported, scope)
153
+ return unless api?(call.receiver, scope)
154
+
155
+ if WRITERS.include?(call.name)
156
+ value = call.arguments&.arguments&.first
157
+ found << Found.new(at(call), :breaks, NOT_TEXT) if call.name == :authorization= && value && not_text?(value)
158
+ found << Found.new(at(call), :check, WRITER) unless reported.include?(at(call)) || reported.include?(call.location.end_line)
159
+ elsif SENDS.include?(call.name)
160
+ name = call.arguments&.arguments&.first
161
+ found << Found.new(at(call), :check, WRITER) if name.is_a?(Prism::SymbolNode) && WRITERS.include?(name.unescaped.to_sym)
162
+ end
163
+ end
164
+
165
+ # run with a block, where what it returns is assigned, returned,
166
+ # passed on or called.
167
+ def self.block_value(call, parents, found, scope)
168
+ return unless call.name == :run && call.block.is_a?(Prism::BlockNode) && api?(call.receiver, scope)
169
+
170
+ parent = parents.last
171
+ used = parent.class.name.end_with?('WriteNode') || parent.is_a?(Prism::ArgumentsNode) ||
172
+ (parent.is_a?(Prism::CallNode) && parent.receiver.equal?(call))
173
+ found << Found.new(at(call), :check, RUN_VALUE) if used
174
+ end
175
+
176
+ # The names of the variables that hold a response: what run returned,
177
+ # and what run passes to its block.
178
+ def self.responses_of(tree, scope)
179
+ names = []
180
+ each(tree) do |node, _|
181
+ if (node.is_a?(Prism::LocalVariableWriteNode) || node.is_a?(Prism::InstanceVariableWriteNode)) && run?(node.value, scope)
182
+ names << node.name
183
+ elsif run?(node, scope) && node.block.is_a?(Prism::BlockNode)
184
+ inner = node.block.parameters
185
+ first = inner.is_a?(Prism::BlockParametersNode) ? inner.parameters&.requireds&.first : nil
186
+ names << first.name if first.respond_to?(:name)
187
+ end
188
+ end
189
+ names.uniq
190
+ end
191
+
192
+ def self.run?(node, scope) = node.is_a?(Prism::CallNode) && node.name == :run && api?(node.receiver, scope)
193
+
194
+ def self.response(call, responses, found)
195
+ holder = call.receiver
196
+ return unless RESPONSE.key?(call.name) && (holder.is_a?(Prism::LocalVariableReadNode) || holder.is_a?(Prism::InstanceVariableReadNode))
197
+ return unless responses.include?(holder.name)
198
+ # `response.request` is found by the rule on lines.
199
+ return if call.name == :request && holder.name == :response
200
+
201
+ found << Found.new(at(call), :check, RESPONSE[call.name])
202
+ end
203
+
204
+ # The constants of the gem that a class finds first once it includes
205
+ # ApiRecipes: the errors, Internals and VERSION.
206
+ def self.shadowing
207
+ @shadowing ||= ApiRecipes.constants.select do |name|
208
+ value = ApiRecipes.const_get(name)
209
+ %i[Internals VERSION].include?(name) || (value.is_a?(Class) && value <= ApiRecipes::Error)
210
+ end
211
+ end
212
+
213
+ # In the body of a class or a module that includes ApiRecipes, a
214
+ # constant written alone that the gem has too, and that the body
215
+ # does not define.
216
+ def self.constants(owner, found)
217
+ body = owner.body.is_a?(Prism::StatementsNode) ? owner.body.body : []
218
+ includes = body.any? do |node|
219
+ node.is_a?(Prism::CallNode) && node.name == :include && node.receiver.nil? &&
220
+ node.arguments&.arguments&.any? { |one| one.slice.delete_prefix('::') == 'ApiRecipes' }
221
+ end
222
+ return unless includes
223
+
224
+ defined = body.filter_map do |node|
225
+ next node.name if node.is_a?(Prism::ConstantWriteNode)
226
+
227
+ node.constant_path.slice.to_sym if node.is_a?(Prism::ClassNode) || node.is_a?(Prism::ModuleNode)
228
+ end
229
+ each(owner.body) do |node, parents|
230
+ next unless node.is_a?(Prism::ConstantReadNode) && shadowing.include?(node.name) && !defined.include?(node.name)
231
+ # A class or a module written inside has its own constants.
232
+ next if parents.any? { |one| one.is_a?(Prism::ClassNode) || one.is_a?(Prism::ModuleNode) }
233
+ # In Name::Error only Name is looked for from here. An older
234
+ # Prism gives the last name of a path as a node of its own.
235
+ path = parents.last
236
+ next if path.is_a?(Prism::ConstantPathNode) && !path.parent.equal?(node)
237
+
238
+ found << Found.new(at(node), :check, "#{node.name} written alone in a class that includes ApiRecipes is now ApiRecipes::#{node.name}: " \
239
+ "write ::#{node.name} for a constant of the application with that name")
240
+ end
241
+ end
242
+ end
243
+
244
+ # What changed in 3.0 and is found in a file that describes apis.
245
+ module ConfigRules
246
+ INHERITED = %w[timeout on_bad_code mime_type verify_with].freeze
247
+
248
+ # [message, ...] for the description of one file: { name => options }.
249
+ def self.findings(apis)
250
+ found = []
251
+ apis.each { |name, options| walk(options, [place(name)], found, true) if options.is_a?(Hash) }
252
+ found
253
+ end
254
+
255
+ def self.place(name) = name.to_s.gsub(/[^[:print:]]/, '?')[0, 40]
256
+
257
+ def self.value(options, key) = options.key?(key) ? options[key] : options[key.to_sym]
258
+
259
+ def self.walk(options, path, found, root)
260
+ endpoints = value(options, 'endpoints')
261
+ return unless endpoints.is_a?(Hash)
262
+
263
+ endpoints.each do |name, body|
264
+ here = path + ['endpoints', place(name)]
265
+ base = root ? ApiRecipes::Api : ApiRecipes::Endpoint
266
+ if Internals::Compiler::METHOD_NAME.match?(name.to_s) && Internals::Handles.taken?(base, name.to_sym)
267
+ found << "#{here.join('.')}: an endpoint with this name has no method of its own, because #{place(name)} is a method of the " \
268
+ "object that would have it: reach it with [:#{place(name)}], or give it another name and its `path`"
269
+ end
270
+ next unless body.is_a?(Hash)
271
+
272
+ inside = value(body, 'endpoints')
273
+ if inside.is_a?(Hash)
274
+ INHERITED.each do |key|
275
+ next if value(body, key).nil?
276
+
277
+ without = inside.select { |_, child| !child.is_a?(Hash) || value(child, key).nil? }.keys.map { |child| place(child) }
278
+ next if without.empty?
279
+
280
+ found << "#{here.join('.')}: #{key} is set here, and the endpoints it contains that do not set it now find it here " \
281
+ "(#{without.first(5).join(', ')}#{', ...' if without.size > 5}): in 2.x they took the one of the api"
282
+ end
283
+ end
284
+ walk(body, here, found, false)
285
+ end
286
+ end
287
+ end
288
+ end
289
+ end
290
+ end