runtime_analysis 1.0.0

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 (267) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +115 -0
  3. data/LICENSE.txt +96 -0
  4. data/README.md +164 -0
  5. data/editor/nvim/README.md +278 -0
  6. data/editor/nvim/lua/runtime_analysis/connected.lua +565 -0
  7. data/editor/nvim/lua/runtime_analysis/go_to_class.lua +63 -0
  8. data/editor/nvim/lua/runtime_analysis/inline.lua +138 -0
  9. data/editor/nvim/lua/runtime_analysis/lens.lua +50 -0
  10. data/editor/nvim/lua/runtime_analysis/lsp.lua +92 -0
  11. data/editor/nvim/lua/runtime_analysis/menu.lua +98 -0
  12. data/editor/nvim/lua/runtime_analysis/preview.lua +70 -0
  13. data/editor/nvim/lua/runtime_analysis/project.lua +28 -0
  14. data/editor/nvim/lua/runtime_analysis/rerun.lua +39 -0
  15. data/editor/nvim/lua/runtime_analysis/text.lua +23 -0
  16. data/editor/nvim/lua/runtime_analysis/value_popup.lua +45 -0
  17. data/editor/nvim/plugin/runtime_analysis.lua +71 -0
  18. data/examples/trackers/PROMPT.md +86 -0
  19. data/examples/trackers/honeybadger.rb +19 -0
  20. data/examples/trackers/sentry.rb +16 -0
  21. data/ext/runtime_analysis/extconf.rb +68 -0
  22. data/ext/runtime_analysis/fast.c +775 -0
  23. data/ext/runtime_analysis/fast.h +59 -0
  24. data/ext/runtime_analysis/hold.c +560 -0
  25. data/ext/runtime_analysis/init.c +36 -0
  26. data/ext/runtime_analysis/names.c +78 -0
  27. data/ext/runtime_analysis/names.h +28 -0
  28. data/ext/runtime_analysis/novelty.c +139 -0
  29. data/ext/runtime_analysis/novelty.h +22 -0
  30. data/ext/runtime_analysis/os.c +73 -0
  31. data/ext/runtime_analysis/os.h +32 -0
  32. data/ext/runtime_analysis/ring.c +445 -0
  33. data/ext/runtime_analysis/rows.c +304 -0
  34. data/ext/runtime_analysis/sink.c +485 -0
  35. data/ext/runtime_analysis/sink.h +240 -0
  36. data/ext/runtime_analysis/state.c +254 -0
  37. data/ext/runtime_analysis/state.h +57 -0
  38. data/ext/runtime_analysis/wrapper.c +278 -0
  39. data/ext/runtime_analysis/wrapper.h +24 -0
  40. data/lib/runtime_analysis/errors.rb +34 -0
  41. data/lib/runtime_analysis/failsafe.rb +123 -0
  42. data/lib/runtime_analysis/host/controller_entry_point.rb +18 -0
  43. data/lib/runtime_analysis/host/declared.rb +152 -0
  44. data/lib/runtime_analysis/host/inject.rb +90 -0
  45. data/lib/runtime_analysis/host/job_runners.rb +27 -0
  46. data/lib/runtime_analysis/host/middleware.rb +23 -0
  47. data/lib/runtime_analysis/host/minitest_integration.rb +22 -0
  48. data/lib/runtime_analysis/host/railtie.rb +42 -0
  49. data/lib/runtime_analysis/host/reports.rb +54 -0
  50. data/lib/runtime_analysis/host/rspec_integration.rb +17 -0
  51. data/lib/runtime_analysis/host/web.rb +22 -0
  52. data/lib/runtime_analysis/host.rb +34 -0
  53. data/lib/runtime_analysis/inject.rb +5 -0
  54. data/lib/runtime_analysis/minitest.rb +9 -0
  55. data/lib/runtime_analysis/native/build.rb +17 -0
  56. data/lib/runtime_analysis/native/byte_size.rb +27 -0
  57. data/lib/runtime_analysis/native/call_site_logger/definitions.rb +51 -0
  58. data/lib/runtime_analysis/native/call_site_logger/held.rb +35 -0
  59. data/lib/runtime_analysis/native/call_site_logger/rows.rb +110 -0
  60. data/lib/runtime_analysis/native/call_site_logger/segments.rb +100 -0
  61. data/lib/runtime_analysis/native/call_site_logger/streams.rb +110 -0
  62. data/lib/runtime_analysis/native/call_site_logger.rb +80 -0
  63. data/lib/runtime_analysis/native/rotation_policy.rb +28 -0
  64. data/lib/runtime_analysis/native/sanitizer_runtime.rb +50 -0
  65. data/lib/runtime_analysis/native/sink.rb +12 -0
  66. data/lib/runtime_analysis/native.rb +74 -0
  67. data/lib/runtime_analysis/platform.rb +30 -0
  68. data/lib/runtime_analysis/query/index/anonymise.rb +98 -0
  69. data/lib/runtime_analysis/query/index/graph.rb +107 -0
  70. data/lib/runtime_analysis/query/index/graph_cache.rb +32 -0
  71. data/lib/runtime_analysis/query/index/reading/declared.rb +42 -0
  72. data/lib/runtime_analysis/query/index/reading/dict.rb +43 -0
  73. data/lib/runtime_analysis/query/index/reading/frame.rb +27 -0
  74. data/lib/runtime_analysis/query/index/reading/node.rb +45 -0
  75. data/lib/runtime_analysis/query/index/reading/path.rb +21 -0
  76. data/lib/runtime_analysis/query/index/reading/reruns.rb +30 -0
  77. data/lib/runtime_analysis/query/index/reading/rows.rb +128 -0
  78. data/lib/runtime_analysis/query/index/reading/stream.rb +82 -0
  79. data/lib/runtime_analysis/query/index/reading/tree.rb +66 -0
  80. data/lib/runtime_analysis/query/index/reading/tree_builder.rb +135 -0
  81. data/lib/runtime_analysis/query/index/reading/values.rb +49 -0
  82. data/lib/runtime_analysis/query/index/reading.rb +80 -0
  83. data/lib/runtime_analysis/query/index/shapes.rb +142 -0
  84. data/lib/runtime_analysis/query/index/signatures/active_support.rb +31 -0
  85. data/lib/runtime_analysis/query/index/signatures/core.json +1 -0
  86. data/lib/runtime_analysis/query/index/signatures/layers.rb +29 -0
  87. data/lib/runtime_analysis/query/index/signatures/lineage.rb +57 -0
  88. data/lib/runtime_analysis/query/index/signatures/overloads.rb +32 -0
  89. data/lib/runtime_analysis/query/index/signatures/rbs/entry.rb +95 -0
  90. data/lib/runtime_analysis/query/index/signatures/rbs/export.rb +76 -0
  91. data/lib/runtime_analysis/query/index/signatures/rbs/library.rb +43 -0
  92. data/lib/runtime_analysis/query/index/signatures/rbs/overload.rb +66 -0
  93. data/lib/runtime_analysis/query/index/signatures/rbs/project.rb +69 -0
  94. data/lib/runtime_analysis/query/index/signatures/rbs/types.rb +79 -0
  95. data/lib/runtime_analysis/query/index/signatures/rbs.rb +57 -0
  96. data/lib/runtime_analysis/query/index/signatures/sorbet/sig.rb +48 -0
  97. data/lib/runtime_analysis/query/index/signatures/sorbet/signed.rb +69 -0
  98. data/lib/runtime_analysis/query/index/signatures/sorbet/table.rb +43 -0
  99. data/lib/runtime_analysis/query/index/signatures/sorbet/types.rb +96 -0
  100. data/lib/runtime_analysis/query/index/signatures/sorbet.rb +150 -0
  101. data/lib/runtime_analysis/query/index/signatures/sources.rb +80 -0
  102. data/lib/runtime_analysis/query/index/signatures/written.rb +45 -0
  103. data/lib/runtime_analysis/query/index/signatures.rb +93 -0
  104. data/lib/runtime_analysis/query/index/source_named_graph.rb +84 -0
  105. data/lib/runtime_analysis/query/index/static/change_set.rb +20 -0
  106. data/lib/runtime_analysis/query/index/static/changes.rb +80 -0
  107. data/lib/runtime_analysis/query/index/static/code_visitor.rb +144 -0
  108. data/lib/runtime_analysis/query/index/static/klass.rb +13 -0
  109. data/lib/runtime_analysis/query/index/static/method.rb +19 -0
  110. data/lib/runtime_analysis/query/index/static/params.rb +52 -0
  111. data/lib/runtime_analysis/query/index/static/source.rb +69 -0
  112. data/lib/runtime_analysis/query/index/static/stub.rb +14 -0
  113. data/lib/runtime_analysis/query/index/static/stub_visitor.rb +133 -0
  114. data/lib/runtime_analysis/query/index/static/value.rb +17 -0
  115. data/lib/runtime_analysis/query/index/static/values.rb +71 -0
  116. data/lib/runtime_analysis/query/index/static/visibility.rb +40 -0
  117. data/lib/runtime_analysis/query/index/static.rb +85 -0
  118. data/lib/runtime_analysis/query/index.rb +32 -0
  119. data/lib/runtime_analysis/query/nodes.rb +60 -0
  120. data/lib/runtime_analysis/query/project/config.rb +108 -0
  121. data/lib/runtime_analysis/query/project.rb +40 -0
  122. data/lib/runtime_analysis/query/report/document.rb +84 -0
  123. data/lib/runtime_analysis/query/report/finding.rb +15 -0
  124. data/lib/runtime_analysis/query/report/json_renderer.rb +51 -0
  125. data/lib/runtime_analysis/query/report/list.rb +15 -0
  126. data/lib/runtime_analysis/query/report/location.rb +14 -0
  127. data/lib/runtime_analysis/query/report/one_row.rb +14 -0
  128. data/lib/runtime_analysis/query/report/outline.rb +15 -0
  129. data/lib/runtime_analysis/query/report/paint.rb +34 -0
  130. data/lib/runtime_analysis/query/report/related.rb +11 -0
  131. data/lib/runtime_analysis/query/report/table.rb +15 -0
  132. data/lib/runtime_analysis/query/report/text.rb +13 -0
  133. data/lib/runtime_analysis/query/report/text_renderer.rb +102 -0
  134. data/lib/runtime_analysis/query/report.rb +35 -0
  135. data/lib/runtime_analysis/query/surfaces/class_value.rb +17 -0
  136. data/lib/runtime_analysis/query/surfaces/cli/answer.rb +32 -0
  137. data/lib/runtime_analysis/query/surfaces/cli/command_line.rb +77 -0
  138. data/lib/runtime_analysis/query/surfaces/cli/usage.rb +32 -0
  139. data/lib/runtime_analysis/query/surfaces/cli.rb +101 -0
  140. data/lib/runtime_analysis/query/surfaces/crystallise/lexical.rb +44 -0
  141. data/lib/runtime_analysis/query/surfaces/crystallise/notation.rb +33 -0
  142. data/lib/runtime_analysis/query/surfaces/crystallise/observed.rb +102 -0
  143. data/lib/runtime_analysis/query/surfaces/crystallise/outline.rb +111 -0
  144. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_file.rb +100 -0
  145. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_member.rb +50 -0
  146. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_notation.rb +44 -0
  147. data/lib/runtime_analysis/query/surfaces/crystallise/seen.rb +74 -0
  148. data/lib/runtime_analysis/query/surfaces/crystallise/sig_text.rb +49 -0
  149. data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_edit.rb +106 -0
  150. data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_notation.rb +48 -0
  151. data/lib/runtime_analysis/query/surfaces/crystallise/subject.rb +55 -0
  152. data/lib/runtime_analysis/query/surfaces/crystallise.rb +94 -0
  153. data/lib/runtime_analysis/query/surfaces/mcp.rb +89 -0
  154. data/lib/runtime_analysis/query/surfaces/record/commands.rb +45 -0
  155. data/lib/runtime_analysis/query/surfaces/record/environment.rb +48 -0
  156. data/lib/runtime_analysis/query/surfaces/record/ignore.rb +62 -0
  157. data/lib/runtime_analysis/query/surfaces/record/question.rb +55 -0
  158. data/lib/runtime_analysis/query/surfaces/record/run.rb +30 -0
  159. data/lib/runtime_analysis/query/surfaces/record.rb +122 -0
  160. data/lib/runtime_analysis/query/surfaces/rerun.rb +87 -0
  161. data/lib/runtime_analysis/query/surfaces/setup/agents.rb +76 -0
  162. data/lib/runtime_analysis/query/surfaces/setup/changes.rb +107 -0
  163. data/lib/runtime_analysis/query/surfaces/setup/project.rb +80 -0
  164. data/lib/runtime_analysis/query/surfaces/setup/steps.md +205 -0
  165. data/lib/runtime_analysis/query/surfaces/setup.rb +98 -0
  166. data/lib/runtime_analysis/query/surfaces.rb +14 -0
  167. data/lib/runtime_analysis/query/tools/base.rb +86 -0
  168. data/lib/runtime_analysis/query/tools/method_lookup.rb +37 -0
  169. data/lib/runtime_analysis/query/tools/namespaces.rb +36 -0
  170. data/lib/runtime_analysis/query/tools/origins.rb +47 -0
  171. data/lib/runtime_analysis/query/tools/stubs.rb +53 -0
  172. data/lib/runtime_analysis/query/tools/unknown_method.rb +11 -0
  173. data/lib/runtime_analysis/query/tools/verify_mock.rb +145 -0
  174. data/lib/runtime_analysis/query/tools/what_calls.rb +67 -0
  175. data/lib/runtime_analysis/query/tools/what_it_receives.rb +142 -0
  176. data/lib/runtime_analysis/query/tools/where_nil_comes_from.rb +105 -0
  177. data/lib/runtime_analysis/query/tools/who_actually_answered.rb +82 -0
  178. data/lib/runtime_analysis/query/tools.rb +34 -0
  179. data/lib/runtime_analysis/query.rb +33 -0
  180. data/lib/runtime_analysis/recording/blob.rb +13 -0
  181. data/lib/runtime_analysis/recording/format.rb +32 -0
  182. data/lib/runtime_analysis/recording/opcodes.rb +30 -0
  183. data/lib/runtime_analysis/recording/raise_ids.rb +15 -0
  184. data/lib/runtime_analysis/recording/ruby_sink.rb +176 -0
  185. data/lib/runtime_analysis/recording/run_manifest.rb +110 -0
  186. data/lib/runtime_analysis/recording/seen/nothing.rb +15 -0
  187. data/lib/runtime_analysis/recording/seen/tally.rb +48 -0
  188. data/lib/runtime_analysis/recording/seen.rb +63 -0
  189. data/lib/runtime_analysis/recording/sink.rb +19 -0
  190. data/lib/runtime_analysis/recording/source_blob.rb +20 -0
  191. data/lib/runtime_analysis/recording.rb +52 -0
  192. data/lib/runtime_analysis/rspec.rb +8 -0
  193. data/lib/runtime_analysis/ruby_parsing/packages.rb +17 -0
  194. data/lib/runtime_analysis/ruby_parsing.rb +98 -0
  195. data/lib/runtime_analysis/trace/configuration/capturing.rb +32 -0
  196. data/lib/runtime_analysis/trace/configuration/per_run.rb +31 -0
  197. data/lib/runtime_analysis/trace/configuration/tier.rb +27 -0
  198. data/lib/runtime_analysis/trace/configuration.rb +115 -0
  199. data/lib/runtime_analysis/trace/entries.rb +47 -0
  200. data/lib/runtime_analysis/trace/hold/raises.rb +28 -0
  201. data/lib/runtime_analysis/trace/hold/requests.rb +47 -0
  202. data/lib/runtime_analysis/trace/hold/telling.rb +45 -0
  203. data/lib/runtime_analysis/trace/hold/unwound.rb +30 -0
  204. data/lib/runtime_analysis/trace/hold.rb +142 -0
  205. data/lib/runtime_analysis/trace/inclusion.rb +46 -0
  206. data/lib/runtime_analysis/trace/installer/alias_repair.rb +68 -0
  207. data/lib/runtime_analysis/trace/installer/answers.rb +24 -0
  208. data/lib/runtime_analysis/trace/installer/body_sync.rb +25 -0
  209. data/lib/runtime_analysis/trace/installer/built_classes.rb +43 -0
  210. data/lib/runtime_analysis/trace/installer/called_names.rb +33 -0
  211. data/lib/runtime_analysis/trace/installer/class_builders.rb +50 -0
  212. data/lib/runtime_analysis/trace/installer/deferral.rb +42 -0
  213. data/lib/runtime_analysis/trace/installer/hook.rb +170 -0
  214. data/lib/runtime_analysis/trace/installer/included_gems.rb +53 -0
  215. data/lib/runtime_analysis/trace/installer/probe.rb +28 -0
  216. data/lib/runtime_analysis/trace/installer/reflection.rb +50 -0
  217. data/lib/runtime_analysis/trace/installer/registry.rb +78 -0
  218. data/lib/runtime_analysis/trace/installer/sorbet.rb +29 -0
  219. data/lib/runtime_analysis/trace/installer/wrapping/ivar_writes.rb +62 -0
  220. data/lib/runtime_analysis/trace/installer/wrapping.rb +66 -0
  221. data/lib/runtime_analysis/trace/installer.rb +149 -0
  222. data/lib/runtime_analysis/trace/mechanism/block_entries.rb +29 -0
  223. data/lib/runtime_analysis/trace/mechanism/frame_readers.rb +61 -0
  224. data/lib/runtime_analysis/trace/mechanism/native_asks.rb +36 -0
  225. data/lib/runtime_analysis/trace/mechanism/native_sync.rb +28 -0
  226. data/lib/runtime_analysis/trace/mechanism/pure_wrapper.rb +41 -0
  227. data/lib/runtime_analysis/trace/mechanism/ruby_wrapper.rb +141 -0
  228. data/lib/runtime_analysis/trace/mechanism/wrapper.rb +79 -0
  229. data/lib/runtime_analysis/trace/mechanism/wrapper_shape.rb +40 -0
  230. data/lib/runtime_analysis/trace/mechanism.rb +28 -0
  231. data/lib/runtime_analysis/trace/nil_origin/answers.rb +24 -0
  232. data/lib/runtime_analysis/trace/nil_origin/beyond.rb +182 -0
  233. data/lib/runtime_analysis/trace/nil_origin/index.rb +164 -0
  234. data/lib/runtime_analysis/trace/nil_origin/lifetime.rb +29 -0
  235. data/lib/runtime_analysis/trace/nil_origin/named.rb +48 -0
  236. data/lib/runtime_analysis/trace/nil_origin/objects.rb +78 -0
  237. data/lib/runtime_analysis/trace/nil_origin/provenance.rb +136 -0
  238. data/lib/runtime_analysis/trace/nil_origin/receiver.rb +121 -0
  239. data/lib/runtime_analysis/trace/nil_origin/rows.rb +43 -0
  240. data/lib/runtime_analysis/trace/nil_origin/source.rb +315 -0
  241. data/lib/runtime_analysis/trace/nil_origin/stored.rb +159 -0
  242. data/lib/runtime_analysis/trace/nil_origin/told.rb +152 -0
  243. data/lib/runtime_analysis/trace/nil_origin/values.rb +47 -0
  244. data/lib/runtime_analysis/trace/nil_origin/weighed.rb +70 -0
  245. data/lib/runtime_analysis/trace/nil_origin/writers.rb +79 -0
  246. data/lib/runtime_analysis/trace/nil_origin.rb +278 -0
  247. data/lib/runtime_analysis/trace/novelty.rb +17 -0
  248. data/lib/runtime_analysis/trace/pure/novelty.rb +43 -0
  249. data/lib/runtime_analysis/trace/pure/state.rb +94 -0
  250. data/lib/runtime_analysis/trace/pure.rb +15 -0
  251. data/lib/runtime_analysis/trace/recorder/class_name.rb +63 -0
  252. data/lib/runtime_analysis/trace/recorder/fingerprint.rb +39 -0
  253. data/lib/runtime_analysis/trace/recorder/frame_key.rb +27 -0
  254. data/lib/runtime_analysis/trace/recorder/paths.rb +89 -0
  255. data/lib/runtime_analysis/trace/recorder/raise_site.rb +36 -0
  256. data/lib/runtime_analysis/trace/recorder/reachability.rb +46 -0
  257. data/lib/runtime_analysis/trace/recorder/row_emission.rb +137 -0
  258. data/lib/runtime_analysis/trace/recorder/safe_steps.rb +84 -0
  259. data/lib/runtime_analysis/trace/recorder/tree_steps.rb +42 -0
  260. data/lib/runtime_analysis/trace/recorder/unwinding.rb +23 -0
  261. data/lib/runtime_analysis/trace/recorder.rb +149 -0
  262. data/lib/runtime_analysis/trace/state.rb +16 -0
  263. data/lib/runtime_analysis/trace.rb +142 -0
  264. data/lib/runtime_analysis/version.rb +6 -0
  265. data/lib/runtime_analysis/web.rb +6 -0
  266. data/lib/runtime_analysis.rb +125 -0
  267. metadata +330 -0
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Query
5
+ module Index
6
+ class Signatures
7
+ module Overloads
8
+ FIRST_ARGUMENT_CLASS = 3
9
+
10
+ module_function
11
+
12
+ def chosen(overloads, arguments: nil, first: nil, block: false)
13
+ fitting = fitting(overloads, arguments, block)
14
+ exact = first ? fitting.select { |overload| overload.fetch(FIRST_ARGUMENT_CLASS) == first } : []
15
+ (exact.empty? ? fitting : exact).flat_map(&:last).uniq
16
+ end
17
+
18
+ def fitting(overloads, arguments, block)
19
+ fitting = overloads.select { |overload| fits?(overload, arguments, block) }
20
+ fitting.empty? ? overloads : fitting
21
+ end
22
+
23
+ def fits?((takes_block, fewest, most, _first, _returns), arguments, block)
24
+ return false unless (takes_block == 1) == block
25
+
26
+ arguments.nil? || (arguments >= fewest && (most == -1 || arguments <= most))
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../rbs_spec.rb and project_spec.rb, which read the declarations each entry gives.
4
+
5
+ require_relative "library"
6
+ require_relative "types"
7
+ require_relative "overload"
8
+
9
+ module RuntimeAnalysis
10
+ module Query
11
+ module Index
12
+ class Signatures
13
+ module Rbs
14
+ class Entry
15
+ PARTS = { instance: [%w[methods params]], singleton: [%w[class_methods class_params]],
16
+ singleton_instance: [%w[methods params], %w[class_methods class_params]] }.freeze
17
+ READERS = { method: :method, reader: :reader, writer: :writer, accessor: :accessor, include: :included,
18
+ prepend: :included, extend: :extended, alias: :aliased }.freeze
19
+
20
+ def initialize(types)
21
+ @types = types
22
+ @entry = { "ancestors" => [], "extends" => [], "methods" => {}, "class_methods" => {},
23
+ "params" => {}, "class_params" => {} }
24
+ end
25
+
26
+ def to_h
27
+ @entry
28
+ end
29
+
30
+ def read(declaration)
31
+ inherit(declaration)
32
+ aliases_last(declaration.members).each { |member| __send__(READERS.fetch(Library.kind(member)), member) }
33
+ end
34
+
35
+ private
36
+
37
+ def aliases_last(members)
38
+ aliases, others = members.partition { |member| Library.kind(member) == :alias }
39
+ [*others, *aliases].select { |member| READERS.key?(Library.kind(member)) }
40
+ end
41
+
42
+ def inherit(declaration)
43
+ above = declaration.super_class if declaration.respond_to?(:super_class)
44
+ @entry["superclass"] = @types.named(above.name) if above
45
+ end
46
+
47
+ def method(member)
48
+ overloads = member.overloads.map { |each| Overload.new(each.method_type, @types) }
49
+ PARTS.fetch(member.kind).each { |methods, params| declare(member.name.to_s, overloads, methods, params) }
50
+ end
51
+
52
+ def declare(name, overloads, methods, params)
53
+ returns = overloads.map(&:to_a)
54
+ taken = overloads.first.parameters
55
+ @entry.fetch(methods)[name] = returns.uniq if returns.all?
56
+ @entry.fetch(params)[name] = taken if taken
57
+ end
58
+
59
+ def reader(member)
60
+ type = @types.of(member.type)
61
+ methods(member)[member.name.to_s] = [[0, 0, 0, nil, type]] if type
62
+ end
63
+
64
+ def writer(member)
65
+ type = @types.of(member.type)
66
+ methods(member)["#{member.name}="] = [[0, 1, 1, nil, type]] if type
67
+ end
68
+
69
+ def accessor(member)
70
+ reader(member)
71
+ writer(member)
72
+ end
73
+
74
+ def methods(member)
75
+ @entry.fetch(member.kind == :singleton ? "class_methods" : "methods")
76
+ end
77
+
78
+ def included(member)
79
+ @entry.fetch("ancestors").unshift(@types.named(member.name)) unless member.name.interface?
80
+ end
81
+
82
+ def extended(member)
83
+ @entry.fetch("extends").unshift(@types.named(member.name)) unless member.name.interface?
84
+ end
85
+
86
+ def aliased(member)
87
+ known = methods(member)[member.old_name.to_s]
88
+ methods(member)[member.new_name.to_s] = known if known
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "library"
4
+ require_relative "types"
5
+ require_relative "overload"
6
+
7
+ module RuntimeAnalysis
8
+ module Query
9
+ module Index
10
+ class Signatures
11
+ module Rbs
12
+ class Export
13
+ attr_reader :skipped
14
+
15
+ def initialize(env)
16
+ @env = env
17
+ @types = Types.new(env)
18
+ @skipped = []
19
+ end
20
+
21
+ def to_h
22
+ @env.class_decls.keys.sort_by(&:to_s).filter_map { |type_name| named_entry(type_name) }.to_h
23
+ end
24
+
25
+ private
26
+
27
+ def named_entry(type_name)
28
+ entry = entry(type_name)
29
+ [Types.plain(type_name), entry] if entry.fetch("methods").any? || entry.fetch("class_methods").any?
30
+ rescue StandardError => e
31
+ @skipped << "#{type_name}: #{e.class}"
32
+ nil
33
+ end
34
+
35
+ def builder
36
+ @builder ||= Library.definition_builder(@env)
37
+ end
38
+
39
+ def entry(type_name)
40
+ instance = own(builder.build_instance(type_name), type_name)
41
+ singleton = own(builder.build_singleton(type_name), type_name)
42
+ { "ancestors" => ancestors(type_name), **parents(type_name), "methods" => returns(instance),
43
+ "class_methods" => returns(singleton) }
44
+ end
45
+
46
+ def ancestors(type_name)
47
+ names = builder.ancestor_builder.instance_ancestors(type_name).ancestors.map(&:name)
48
+ (names - [type_name]).map { |name| Types.plain(name) }
49
+ end
50
+
51
+ def parents(type_name)
52
+ above = builder.ancestor_builder.one_instance_ancestors(type_name).super_class
53
+ { "superclass" => above && Types.plain(above.name), "extends" => extended(type_name) }.compact
54
+ end
55
+
56
+ def extended(type_name)
57
+ modules = builder.ancestor_builder.one_singleton_ancestors(type_name).extended_modules
58
+ modules.map { |each| Types.plain(each.name) } unless modules.empty?
59
+ end
60
+
61
+ def own(definition, type_name)
62
+ definition.methods.except(:new).select { |_name, method| method.defined_in == type_name }
63
+ end
64
+
65
+ def returns(methods)
66
+ methods.filter_map do |name, method|
67
+ overloads = method.method_types.map { |each| Overload.new(each, @types).to_a }
68
+ [name.to_s, overloads.uniq] if overloads.all?
69
+ end.to_h
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rbs"
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Index
8
+ class Signatures
9
+ module Rbs
10
+ module Library
11
+ KINDS = {
12
+ RBS::AST::Members::MethodDefinition => :method, RBS::AST::Members::AttrReader => :reader,
13
+ RBS::AST::Members::AttrWriter => :writer, RBS::AST::Members::AttrAccessor => :accessor,
14
+ RBS::AST::Members::Include => :include, RBS::AST::Members::Prepend => :prepend,
15
+ RBS::AST::Members::Extend => :extend, RBS::AST::Members::Alias => :alias,
16
+ RBS::Types::Bases::Self => :self, RBS::Types::Bases::Instance => :instance,
17
+ RBS::Types::Bases::Bool => :bool, RBS::Types::Bases::Nil => :nil, RBS::Types::Tuple => :tuple,
18
+ RBS::Types::Record => :record, RBS::Types::Proc => :proc, RBS::Types::ClassInstance => :class_instance,
19
+ RBS::Types::Optional => :optional, RBS::Types::Union => :union, RBS::Types::Literal => :literal,
20
+ RBS::Types::Alias => :type_alias, RBS::Types::ClassSingleton => :class_singleton
21
+ }.freeze
22
+
23
+ module_function
24
+
25
+ def kind(written) = KINDS[written.class]
26
+
27
+ def core_root = RBS::EnvironmentLoader::DEFAULT_CORE_ROOT.to_s
28
+
29
+ def loader(**) = RBS::EnvironmentLoader.new(**)
30
+
31
+ def environment(loader) = RBS::Environment.from_loader(loader).resolve_type_names
32
+
33
+ def definition_builder(environment) = RBS::DefinitionBuilder.new(env: environment)
34
+
35
+ def lockfile(path, data) = RBS::Collection::Config::Lockfile.from_lockfile(lockfile_path: path, data: data)
36
+
37
+ def declarations_in(text) = RBS::Parser.parse_signature(text)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../rbs_spec.rb, whose overloaded methods it reads.
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Index
8
+ class Signatures
9
+ module Rbs
10
+ class Overload
11
+ def initialize(method_type, types)
12
+ @function = method_type.type
13
+ @block = method_type.block
14
+ @types = types
15
+ end
16
+
17
+ def to_a
18
+ returns = positional? && @types.of(@function.return_type)
19
+ returns && [takes_block, fewest, most, first, returns.sort_by(&:to_s)]
20
+ end
21
+
22
+ def parameters
23
+ return nil unless positional?
24
+
25
+ taken = { "positional" => positional, "keywords" => keywords }
26
+ taken unless positional.none? && keywords.empty?
27
+ end
28
+
29
+ private
30
+
31
+ def positional?
32
+ @function.respond_to?(:required_positionals)
33
+ end
34
+
35
+ def takes_block
36
+ @block&.required ? 1 : 0
37
+ end
38
+
39
+ def positional
40
+ [*@function.required_positionals, *@function.optional_positionals].map { |each| @types.of(each.type) }
41
+ end
42
+
43
+ def keywords
44
+ named = @function.required_keywords.merge(@function.optional_keywords)
45
+ named.to_h { |name, each| [name.to_s, @types.of(each.type)] }.compact
46
+ end
47
+
48
+ def fewest
49
+ @function.required_positionals.size + @function.trailing_positionals.size
50
+ end
51
+
52
+ def most
53
+ @function.rest_positionals ? -1 : fewest + @function.optional_positionals.size
54
+ end
55
+
56
+ def first
57
+ parameter = @function.required_positionals.first
58
+ names = parameter && @types.of(parameter.type)
59
+ [*names.first].first if names&.size == 1
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+ require "yaml"
5
+ require "runtime_analysis/query/index/signatures/rbs"
6
+ require_relative "entry"
7
+
8
+ module RuntimeAnalysis
9
+ module Query
10
+ module Index
11
+ class Signatures
12
+ module Rbs
13
+ module Project
14
+ CORE = Library.core_root
15
+ ADDED_TO_EVERY_PROJECT = File.expand_path("../stdlib/stringio", CORE)
16
+ SHIPPED = [CORE, ADDED_TO_EVERY_PROJECT].freeze
17
+
18
+ module_function
19
+
20
+ def table(root)
21
+ env = environment(Pathname(root))
22
+ env.class_decls.filter_map { |type_name, entry| named(env, Types.plain(type_name), own(entry)) }.sort.to_h
23
+ end
24
+
25
+ def own(entry)
26
+ declarations(entry).reject { |each| shipped?(each) }
27
+ end
28
+
29
+ def named(env, name, declarations)
30
+ [name, read(declarations, Types.new(env, name))] unless declarations.empty?
31
+ end
32
+
33
+ def read(declarations, types)
34
+ entry = Entry.new(types)
35
+ declarations.each { |each| entry.read(each) }
36
+ entry.to_h
37
+ end
38
+
39
+ def environment(root)
40
+ loader = Library.loader
41
+ loader.add(path: root / SIG)
42
+ add_collection(loader, root / LOCK)
43
+ Library.environment(loader)
44
+ end
45
+
46
+ def add_collection(loader, lock)
47
+ data = YAML.load_file(lock)
48
+ loader.add_collection(Library.lockfile(lock, data))
49
+ rescue StandardError
50
+ nil
51
+ end
52
+
53
+ def shipped?(declaration)
54
+ declaration.location.to_s.start_with?(*SHIPPED)
55
+ end
56
+
57
+ def declarations(entry)
58
+ entry.respond_to?(:each_decl) ? entry.each_decl : rbs_3_declarations(entry)
59
+ end
60
+
61
+ def rbs_3_declarations(entry)
62
+ entry.decls.map(&:decl)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../rbs_spec.rb, which reads each kind of RBS type as a class name.
4
+
5
+ require_relative "library"
6
+ require "runtime_analysis/query/index/signatures/written"
7
+
8
+ module RuntimeAnalysis
9
+ module Query
10
+ module Index
11
+ class Signatures
12
+ module Rbs
13
+ class Types
14
+ FIXED = { self: ["self"], instance: ["self"], bool: ["bool"], nil: ["nil"], tuple: ["Array"],
15
+ record: ["Hash"], proc: ["Proc"] }.freeze
16
+ BUILT = { class_instance: :instance, optional: :optional, union: :union, literal: :literal,
17
+ type_alias: :aliased, class_singleton: :singleton }.freeze
18
+ LITERALS = { TrueClass => "bool", FalseClass => "bool", Symbol => "Symbol", Integer => "Integer",
19
+ String => "String" }.freeze
20
+ DEEPEST_ALIAS = 4
21
+
22
+ def self.plain(type_name)
23
+ type_name.to_s.delete_prefix("::")
24
+ end
25
+
26
+ def initialize(env, nesting = nil)
27
+ @env = env
28
+ @nesting = nesting
29
+ end
30
+
31
+ def named(type_name)
32
+ Written.within(type_name.to_s, @nesting)
33
+ end
34
+
35
+ def of(type, depth = 0)
36
+ kind = Library.kind(type)
37
+ FIXED.fetch(kind) { built(kind, type, depth) }
38
+ end
39
+
40
+ private
41
+
42
+ def built(kind, type, depth)
43
+ reader = BUILT[kind]
44
+ __send__(reader, type, depth) if reader
45
+ end
46
+
47
+ def instance(type, depth)
48
+ name = named(type.name)
49
+ held = type.args.map { |each| of(each, depth) }
50
+ [held.any? ? [name, *held] : name]
51
+ end
52
+
53
+ def singleton(type, _depth)
54
+ [["Class", [named(type.name)]]]
55
+ end
56
+
57
+ def optional(type, depth)
58
+ of(type.type, depth)&.+(["nil"])
59
+ end
60
+
61
+ def union(type, depth)
62
+ names = type.types.map { |each| of(each, depth) }
63
+ names.flatten(1).uniq unless names.any?(&:nil?)
64
+ end
65
+
66
+ def literal(type, _depth)
67
+ [LITERALS.fetch(type.literal.class)]
68
+ end
69
+
70
+ def aliased(type, depth)
71
+ declared = @env.type_alias_decls[type.name] if depth < DEEPEST_ALIAS
72
+ of(declared.decl.type, depth + 1) if declared
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "open3"
5
+ require "rbconfig"
6
+ require "runtime_analysis/platform"
7
+
8
+ module RuntimeAnalysis
9
+ module Query
10
+ module Index
11
+ class Signatures
12
+ module Rbs
13
+ SIG = "sig"
14
+ LOCK = "rbs_collection.lock.yaml"
15
+ READER = File.expand_path("rbs/project", __dir__)
16
+ PRINT = "puts JSON.generate(RuntimeAnalysis::Query::Index::Signatures::Rbs::Project.table(ARGV.fetch(0)))"
17
+ LOADING = ["-I", Platform.lib, "-r", READER].freeze
18
+ autoload :Project, READER
19
+
20
+ module_function
21
+
22
+ def read(root)
23
+ stamp(root).empty? ? {} : Project.table(root)
24
+ rescue LoadError
25
+ outside(root)
26
+ rescue StandardError => e
27
+ unreadable(root, e)
28
+ end
29
+
30
+ def stamp(root)
31
+ files = Dir.glob(File.join(root, SIG, "**", "*.rbs")) + Dir.glob(File.join(root, LOCK))
32
+ files.sort.map { |file| [file, File.size(file), File.mtime(file)] }
33
+ end
34
+
35
+ def outside(root)
36
+ printed, said, status = unbundled { Open3.capture3(RbConfig.ruby, *LOADING, "-e", PRINT, root) }
37
+ status.success? ? passed_on(printed, said) : unreadable(root, said.lines.last.to_s.strip)
38
+ end
39
+
40
+ def passed_on(printed, said)
41
+ warn said unless said.empty?
42
+ JSON.parse(printed)
43
+ end
44
+
45
+ def unbundled(&)
46
+ defined?(Bundler) ? Bundler.with_unbundled_env(&) : yield
47
+ end
48
+
49
+ def unreadable(root, why)
50
+ warn "runtime_analysis: the RBS files of #{root} were not read: #{why}"
51
+ {}
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../sorbet_spec.rb, which reads sigs written each way.
4
+
5
+ require "runtime_analysis/ruby_parsing"
6
+
7
+ module RuntimeAnalysis
8
+ module Query
9
+ module Index
10
+ class Signatures
11
+ module Sorbet
12
+ class Sig
13
+ attr_reader :parameters, :result
14
+
15
+ def self.written?(node)
16
+ node.name == :sig && RubyParsing.block?(node.block)
17
+ end
18
+
19
+ def initialize(node)
20
+ @parameters = {}
21
+ body = node.block.body
22
+ read(body.body.last) if RubyParsing.statements?(body)
23
+ end
24
+
25
+ private
26
+
27
+ def read(node)
28
+ return unless RubyParsing.call?(node)
29
+
30
+ note(node.name, node.arguments&.arguments&.first)
31
+ read(node.receiver)
32
+ end
33
+
34
+ def note(name, argument)
35
+ @result = argument if name == :returns
36
+ @parameters = named(argument) if name == :params && RubyParsing.keyword_hash?(argument)
37
+ end
38
+
39
+ def named(hash)
40
+ pairs = hash.elements.select { |each| RubyParsing.pair?(each) && RubyParsing.symbol?(each.key) }
41
+ pairs.to_h { |pair| [pair.key.unescaped.to_sym, pair.value] }
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../sorbet_spec.rb, which reads each parameter a sig types.
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Index
8
+ class Signatures
9
+ module Sorbet
10
+ class Signed
11
+ def initialize(definition, sig, types)
12
+ @name = definition.name.to_s
13
+ @list = definition.parameters
14
+ @sig = sig
15
+ @types = types
16
+ end
17
+
18
+ def into(table, klass, class_method)
19
+ table.returns(klass, @name, overload, class_method)
20
+ table.takes(klass, @name, parameters, class_method)
21
+ end
22
+
23
+ private
24
+
25
+ def overload
26
+ returns = @types.read(@sig.result)
27
+ returns && [0, fewest, most, first, returns]
28
+ end
29
+
30
+ def parameters
31
+ taken = { "positional" => positional, "keywords" => keywords }
32
+ taken unless positional.none? && keywords.empty?
33
+ end
34
+
35
+ def positional
36
+ [*list(:requireds), *list(:optionals)].map { |each| type_of(each) }
37
+ end
38
+
39
+ def keywords
40
+ list(:keywords).to_h { |each| [each.name.to_s, type_of(each)] }.compact
41
+ end
42
+
43
+ def list(kind)
44
+ @list ? @list.public_send(kind) : []
45
+ end
46
+
47
+ def type_of(parameter)
48
+ written = @sig.parameters[parameter.name] if parameter.respond_to?(:name)
49
+ @types.read(written)
50
+ end
51
+
52
+ def fewest
53
+ list(:requireds).size + list(:posts).size
54
+ end
55
+
56
+ def most
57
+ @list&.rest ? -1 : fewest + list(:optionals).size
58
+ end
59
+
60
+ def first
61
+ type = type_of(list(:requireds).first)
62
+ type.first if type&.size == 1 && type.first.instance_of?(String)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../sorbet_spec.rb, which reads the classes a file declares.
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Index
8
+ class Signatures
9
+ module Sorbet
10
+ class Table
11
+ attr_reader :classes
12
+
13
+ def initialize
14
+ @classes = {}
15
+ end
16
+
17
+ def entry(klass)
18
+ @classes[klass || "Object"] ||= {}
19
+ end
20
+
21
+ def returns(klass, name, overload, class_method)
22
+ part(klass, class_method ? "class_methods" : "methods")[name] = [overload] if overload
23
+ end
24
+
25
+ def takes(klass, name, parameters, class_method)
26
+ part(klass, class_method ? "class_params" : "params")[name] = parameters if parameters
27
+ end
28
+
29
+ def mixed(klass, part, names)
30
+ entry(klass)[part] = (names + entry(klass).fetch(part, [])).uniq
31
+ end
32
+
33
+ private
34
+
35
+ def part(klass, name)
36
+ entry(klass)[name] ||= {}
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end