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,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: exercised through ../static_spec.rb, which drives CodeVisitor directly and the Static
4
+ # index built on it.
5
+
6
+ require "runtime_analysis/ruby_parsing"
7
+ require_relative "method"
8
+ require_relative "klass"
9
+ require "runtime_analysis/query/nodes"
10
+ require_relative "params"
11
+ require_relative "visibility"
12
+
13
+ module RuntimeAnalysis
14
+ module Query
15
+ module Index
16
+ class Static
17
+ class CodeVisitor < RubyParsing.visitor
18
+ ATTRS = { attr_reader: [""], attr_writer: ["="], attr_accessor: ["", "="] }.freeze
19
+ VISIBILITIES = %i[public private protected].freeze
20
+ Scope = Struct.new(:klass, :namespace, :singleton, :visibility)
21
+
22
+ include Visibility
23
+
24
+ attr_reader :methods, :classes
25
+
26
+ def initialize(file)
27
+ super()
28
+ @file = file
29
+ @methods = []
30
+ @classes = []
31
+ @scopes = [Scope.new]
32
+ end
33
+
34
+ def visit_class_node(node)
35
+ enter(node) { super }
36
+ end
37
+
38
+ def visit_module_node(node)
39
+ enter(node) { super }
40
+ end
41
+
42
+ def visit_constant_write_node(node)
43
+ return super unless Nodes.class_block?(node)
44
+
45
+ within([scope.namespace, node.name].compact.join("::"), node, namespace: scope.namespace) { super }
46
+ end
47
+
48
+ def visit_singleton_class_node(node)
49
+ return unless RubyParsing.self?(node.expression)
50
+
51
+ singleton { super }
52
+ end
53
+
54
+ def visit_def_node(node)
55
+ on_self = RubyParsing.self?(node.receiver)
56
+ add_method(node.name, node, class_method: on_self || scope.singleton) if on_self || node.receiver.nil?
57
+ super
58
+ end
59
+
60
+ def visit_call_node(node)
61
+ handle_attr(node) if RubyParsing.bare_call?(node, ATTRS)
62
+ return handle_visibility(node) if RubyParsing.bare_call?(node, VISIBILITIES)
63
+ return singleton { super } if Nodes.class_methods_block?(node)
64
+
65
+ super
66
+ end
67
+
68
+ private
69
+
70
+ def enter(node, &)
71
+ name = qualified_name(node)
72
+ name ? within(name, node, &) : yield
73
+ end
74
+
75
+ def within(name, node, namespace: name, &)
76
+ @classes << Klass.new(name: name, file: @file, line: node.start_line)
77
+ push(name, namespace, false, &)
78
+ end
79
+
80
+ def singleton(&)
81
+ push(scope.klass, scope.namespace, true, &)
82
+ end
83
+
84
+ def push(klass, namespace, singleton)
85
+ @scopes << Scope.new(klass, namespace, singleton, :public)
86
+ yield
87
+ @scopes.pop
88
+ end
89
+
90
+ def qualified_name(node)
91
+ path = constant_name(node) or return nil
92
+ outer = scope.namespace
93
+ outer && !path.start_with?("::") ? "#{outer}::#{path}" : path.delete_prefix("::")
94
+ end
95
+
96
+ def constant_name(node)
97
+ RubyParsing.full_name(node.constant_path)
98
+ end
99
+
100
+ def scope
101
+ @scopes.last
102
+ end
103
+
104
+ def add_method(name, node, class_method:)
105
+ return unless scope.klass
106
+
107
+ @methods << Method.new(**naming(name, class_method), **placement(node), params: params(node))
108
+ end
109
+
110
+ def naming(name, class_method)
111
+ { identity: identity(name, class_method), klass: scope.klass, name: name.to_s,
112
+ class_method: class_method, visibility: scope.visibility }
113
+ end
114
+
115
+ def placement(node)
116
+ { file: @file, line: node.start_line, end_line: node.end_line }
117
+ end
118
+
119
+ def identity(name, class_method)
120
+ "#{scope.klass}#{class_method ? "." : "#"}#{name}"
121
+ end
122
+
123
+ def params(node)
124
+ return [] unless RubyParsing.definition?(node) && node.parameters
125
+
126
+ Params.list(node.parameters)
127
+ end
128
+
129
+ def handle_attr(node)
130
+ symbols(node).each do |base|
131
+ ATTRS.fetch(node.name).each do |suffix|
132
+ add_method("#{base}#{suffix}", node, class_method: scope.singleton)
133
+ end
134
+ end
135
+ end
136
+
137
+ def symbols(node)
138
+ Nodes.arguments(node).select { |argument| RubyParsing.symbol?(argument) }.map(&:unescaped)
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../static_spec.rb, through Static#classes.
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Index
8
+ class Static
9
+ Klass = Struct.new(:name, :file, :line, keyword_init: true)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../static_spec.rb, whose #methods and #changed_methods examples read each field.
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Index
8
+ class Static
9
+ Method = Struct.new(:identity, :klass, :name, :class_method, :visibility, :file, :line, :end_line, :params,
10
+ keyword_init: true)
11
+
12
+ class Method
13
+ def public? = visibility == :public
14
+ def covers?(from, to) = line <= to && end_line >= from
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../static_spec.rb, whose #methods examples read each kind of parameter.
4
+
5
+ require "runtime_analysis/ruby_parsing"
6
+
7
+ module RuntimeAnalysis
8
+ module Query
9
+ module Index
10
+ class Static
11
+ module Params
12
+ module_function
13
+
14
+ def list(parameters)
15
+ [*required(parameters), *optional(parameters), *rest(parameters), *keywords(parameters),
16
+ *keyword_rest(parameters), *block(parameters)]
17
+ end
18
+
19
+ def required(parameters)
20
+ parameters.requireds.map { |param| [param.respond_to?(:name) ? param.name : :destructured, :req] }
21
+ end
22
+
23
+ def optional(parameters)
24
+ parameters.optionals.map { |param| [param.name, :opt] }
25
+ end
26
+
27
+ def rest(parameters)
28
+ named(parameters.rest, :rest)
29
+ end
30
+
31
+ def keywords(parameters)
32
+ parameters.keywords.map do |param|
33
+ [param.name, RubyParsing.kind(param) == :required_keyword_parameter_node ? :keyreq : :key]
34
+ end
35
+ end
36
+
37
+ def keyword_rest(parameters)
38
+ named(parameters.keyword_rest, :keyrest)
39
+ end
40
+
41
+ def block(parameters)
42
+ named(parameters.block, :block)
43
+ end
44
+
45
+ def named(node, kind)
46
+ [[node.name, kind]] if node.respond_to?(:name) && node.name
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../../index_spec.rb, and through crystallise_spec.rb, which writes each sig
4
+ # above the method it finds here.
5
+
6
+ require "runtime_analysis/ruby_parsing"
7
+ require "runtime_analysis/query/index/signatures"
8
+ require_relative "code_visitor"
9
+
10
+ module RuntimeAnalysis
11
+ module Query
12
+ module Index
13
+ class Static
14
+ class Source
15
+ class Definitions < RubyParsing.visitor
16
+ attr_reader :by_line, :signed
17
+
18
+ def initialize
19
+ super()
20
+ @by_line = {}
21
+ @signed = []
22
+ end
23
+
24
+ def visit_statements_node(node)
25
+ node.body.each_cons(2) { |above, below| @signed << below.start_line if sig?(above) }
26
+ super
27
+ end
28
+
29
+ def sig?(node)
30
+ RubyParsing.call?(node) && Signatures.sig?(node)
31
+ end
32
+
33
+ def visit_def_node(node)
34
+ @by_line[node.start_line] = node
35
+ super
36
+ end
37
+ end
38
+
39
+ def initialize(text)
40
+ @tree = RubyParsing.tree_of(text)
41
+ end
42
+
43
+ def methods
44
+ @methods ||= visit(CodeVisitor.new(nil)).methods
45
+ end
46
+
47
+ def definition(method)
48
+ definitions.by_line[method.line]
49
+ end
50
+
51
+ def signed?(definition)
52
+ definitions.signed.include?(definition.start_line)
53
+ end
54
+
55
+ def visit(visitor)
56
+ @tree.accept(visitor)
57
+ visitor
58
+ end
59
+
60
+ private
61
+
62
+ def definitions
63
+ @definitions ||= visit(Definitions.new)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../static_spec.rb, whose #stubs examples read each field.
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Index
8
+ class Static
9
+ Stub = Struct.new(:kind, :klass, :receiver, :message, :returns, :file, :line, :example_location,
10
+ :description, keyword_init: true)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../static_spec.rb, whose #stubs examples drive it.
4
+
5
+ require "runtime_analysis/ruby_parsing"
6
+ require_relative "stub"
7
+ require "runtime_analysis/query/nodes"
8
+ require_relative "values"
9
+
10
+ module RuntimeAnalysis
11
+ module Query
12
+ module Index
13
+ class Static
14
+ class StubVisitor < RubyParsing.visitor
15
+ GROUPS = %i[describe context feature shared_examples shared_context].freeze
16
+ EXAMPLES = %i[it specify example scenario].freeze
17
+ LETS = %i[let let!].freeze
18
+ STUB_KINDS = { allow: :allow_receive, expect: :expect_receive, allow_any_instance_of: :allow_any_instance,
19
+ expect_any_instance_of: :expect_any_instance }.freeze
20
+ STUBBERS = STUB_KINDS.keys.freeze
21
+ DOUBLE_KINDS = { instance_double: :instance_double, class_double: :class_double,
22
+ object_double: :instance_double, double: :double }.freeze
23
+
24
+ attr_reader :stubs
25
+
26
+ def initialize(file)
27
+ super()
28
+ @file = file
29
+ @stubs = []
30
+ @descriptions = []
31
+ @lets = {}
32
+ end
33
+
34
+ def visit_call_node(node)
35
+ return within_group(node) { super } if group?(node)
36
+ return within_example(node) { super } if example?(node)
37
+
38
+ record(node)
39
+ super
40
+ end
41
+
42
+ private
43
+
44
+ def record(node)
45
+ record_let(node) if RubyParsing.bare_call?(node, LETS)
46
+ record_expectation(node) if expectation?(node)
47
+ record_double(node) if RubyParsing.bare_call?(node, DOUBLE_KINDS)
48
+ end
49
+
50
+ def group?(node)
51
+ GROUPS.include?(node.name) && node.block
52
+ end
53
+
54
+ def example?(node)
55
+ EXAMPLES.include?(node.name) && node.block
56
+ end
57
+
58
+ def expectation?(node)
59
+ node.name == :to && RubyParsing.bare_call?(node.receiver, STUBBERS)
60
+ end
61
+
62
+ def within_group(node)
63
+ @descriptions << description(node)
64
+ yield
65
+ @descriptions.pop
66
+ end
67
+
68
+ def within_example(node, &)
69
+ @example = "#{@file}:#{node.start_line}"
70
+ within_group(node, &)
71
+ @example = nil
72
+ end
73
+
74
+ def description(node)
75
+ first = Nodes.first_argument(node)
76
+ Nodes.symbol_or_string(first) || first&.slice
77
+ end
78
+
79
+ def record_let(node)
80
+ name = Nodes.symbol_or_string(Nodes.first_argument(node))
81
+ value = Nodes.block_value(node)
82
+ return unless name
83
+
84
+ klass = Values.klass_of(value, @lets)
85
+ @lets[name.to_sym] = klass unless Values.unknown?(klass)
86
+ end
87
+
88
+ def record_expectation(node)
89
+ target = Nodes.first_argument(node.receiver)
90
+ kind = STUB_KINDS.fetch(node.receiver.name)
91
+ messages(Nodes.first_argument(node)).each { |message, returns| add(kind, target, message, returns, node) }
92
+ end
93
+
94
+ def messages(matcher)
95
+ chain = Nodes.call_chain(matcher)
96
+ receive = chain.find { |call| call.name == :receive }
97
+ return received(receive, chain) if receive
98
+
99
+ chain.select { |call| call.name == :receive_messages }
100
+ .flat_map { |call| Nodes.pairs(Nodes.first_argument(call)) }
101
+ end
102
+
103
+ def received(receive, chain)
104
+ and_return = chain.find { |call| call.name == :and_return }
105
+ returns = and_return ? Nodes.first_argument(and_return) : Nodes.block_value(receive)
106
+ [[Nodes.symbol_or_string(Nodes.first_argument(receive)), returns]]
107
+ end
108
+
109
+ def record_double(node)
110
+ kind = DOUBLE_KINDS.fetch(node.name)
111
+ messages = Nodes.pairs(Nodes.arguments(node).last)
112
+ add(kind, node, nil, nil, node) if messages.empty?
113
+ messages.each { |message, value| add(kind, node, message, value, node) }
114
+ end
115
+
116
+ def add(kind, target, message, returns, node)
117
+ @stubs << Stub.new(kind: kind, klass: klass_for(target), receiver: target&.slice, message: message,
118
+ returns: returns && Values.classify(returns, @lets), file: @file,
119
+ line: node.start_line, example_location: @example,
120
+ description: @descriptions.compact.join(" "))
121
+ end
122
+
123
+ def klass_for(target)
124
+ return RubyParsing.full_name(target) if RubyParsing.constant?(target)
125
+
126
+ klass = Values.klass_of(target, @lets)
127
+ klass unless Values.unknown?(klass)
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../static_spec.rb (#stubs) and ../../tools/verify_mock_spec.rb, which reads unknown?.
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Index
8
+ class Static
9
+ Value = Struct.new(:klass, :elements, :source, keyword_init: true)
10
+
11
+ class Value
12
+ def unknown? = klass == "unknown"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/ruby_parsing"
4
+ require "runtime_analysis/query/nodes"
5
+ require_relative "value"
6
+
7
+ module RuntimeAnalysis
8
+ module Query
9
+ module Index
10
+ class Static
11
+ module Values
12
+ LITERALS = RubyParsing.by_kind(
13
+ symbol_node: "Symbol", string_node: "String", interpolated_string_node: "String",
14
+ integer_node: "Integer", float_node: "Float", nil_node: "NilClass",
15
+ true_node: "TrueClass", false_node: "FalseClass", hash_node: "Hash",
16
+ keyword_hash_node: "Hash", regular_expression_node: "Regexp", range_node: "Range",
17
+ array_node: "Array"
18
+ )
19
+
20
+ DOUBLES = %i[instance_double class_double object_double].freeze
21
+ UNKNOWN = "unknown"
22
+
23
+ module_function
24
+
25
+ def unknown?(klass) = klass == UNKNOWN
26
+
27
+ def classify(node, lets)
28
+ Value.new(klass: klass_of(node, lets), elements: elements_of(node, lets), source: node.slice)
29
+ end
30
+
31
+ def klass_of(node, lets)
32
+ LITERALS[RubyParsing.kind(node)] || constructed(node) || double_class(node) || bound(node, lets) || UNKNOWN
33
+ end
34
+
35
+ def constructed(node)
36
+ return unless RubyParsing.call?(node) && node.name == :new
37
+
38
+ RubyParsing.full_name(node.receiver)
39
+ end
40
+
41
+ def double_class(node)
42
+ return unless RubyParsing.bare_call?(node, DOUBLES)
43
+
44
+ first = Nodes.first_argument(node)
45
+ RubyParsing.constant?(first) ? RubyParsing.full_name(first) : Nodes.symbol_or_string(first)
46
+ end
47
+
48
+ def bound(node, lets)
49
+ lets[local_name(node)]
50
+ end
51
+
52
+ def local_name(node)
53
+ return node.name if RubyParsing.kind(node) == :local_variable_read_node
54
+
55
+ bare_name(node) if RubyParsing.call?(node)
56
+ end
57
+
58
+ def bare_name(call)
59
+ call.name if call.receiver.nil? && call.arguments.nil? && call.block.nil?
60
+ end
61
+
62
+ def elements_of(node, lets)
63
+ return [] unless RubyParsing.array?(node)
64
+
65
+ node.elements.map { |element| klass_of(element, lets) }.uniq
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through static_spec.rb, which reads private sections and inline modifiers.
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Index
8
+ class Static
9
+ module Visibility
10
+ private
11
+
12
+ def handle_visibility(node)
13
+ args = Nodes.arguments(node)
14
+ scope.visibility = node.name if args.empty?
15
+ args.each { |arg| apply_visibility(node.name, arg) }
16
+ end
17
+
18
+ def apply_visibility(visibility, arg)
19
+ case RubyParsing.kind(arg)
20
+ when :def_node then visit_modified_def(arg, visibility)
21
+ when :symbol_node then mark(arg.unescaped, visibility)
22
+ when :array_node then arg.elements.each { |element| apply_visibility(visibility, element) }
23
+ end
24
+ end
25
+
26
+ def visit_modified_def(def_node, visibility)
27
+ def_node.accept(self)
28
+ @methods.last.visibility = visibility if @methods.last&.line == def_node.start_line
29
+ end
30
+
31
+ def mark(name, visibility)
32
+ @methods.each do |method|
33
+ method.visibility = visibility if method.klass == scope.klass && method.name == name
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/ruby_parsing"
4
+ require_relative "static/method"
5
+ require_relative "static/code_visitor"
6
+ require_relative "static/stub_visitor"
7
+ require_relative "static/changes"
8
+ require_relative "static/source"
9
+
10
+ module RuntimeAnalysis
11
+ module Query
12
+ module Index
13
+ class Static
14
+ CODE_DIRS = %w[app lib].freeze
15
+ SPEC_DIRS = %w[spec].freeze
16
+ SPEC_FILE = /_(spec|test)\.rb\z/
17
+
18
+ attr_reader :project, :code_dirs, :spec_dirs
19
+
20
+ def self.method_entry(**) = Method.new(**)
21
+ def self.source(text) = Source.new(text)
22
+
23
+ def initialize(project, code_dirs: CODE_DIRS, spec_dirs: SPEC_DIRS)
24
+ @project = project
25
+ @code_dirs = code_dirs
26
+ @spec_dirs = spec_dirs
27
+ end
28
+
29
+ def methods
30
+ @methods ||= code_visitors.flat_map(&:methods)
31
+ end
32
+
33
+ def classes
34
+ @classes ||= code_visitors.flat_map(&:classes)
35
+ end
36
+
37
+ def method(identity)
38
+ by_identity[identity]
39
+ end
40
+
41
+ def public_methods_of(klass)
42
+ methods.select { |method| method.klass == klass && method.public? }
43
+ end
44
+
45
+ def implementers_of(message)
46
+ methods.select { |method| method.name == message.delete_prefix("#") && !method.class_method }
47
+ .map(&:klass).uniq
48
+ end
49
+
50
+ def stubs
51
+ @stubs ||= spec_files.flat_map { |file| visit(StubVisitor, file).stubs }
52
+ end
53
+
54
+ def changed_methods(since:)
55
+ Changes.since(since, root: project.root, methods: methods, spec_dirs: spec_dirs)
56
+ end
57
+
58
+ private
59
+
60
+ def by_identity
61
+ @by_identity ||= methods.to_h { |method| [method.identity, method] }
62
+ end
63
+
64
+ def code_visitors
65
+ @code_visitors ||= files(code_dirs).grep_v(SPEC_FILE) { |file| visit(CodeVisitor, file) }
66
+ end
67
+
68
+ def spec_files
69
+ (files(spec_dirs) + files(code_dirs).grep(SPEC_FILE)).uniq
70
+ end
71
+
72
+ def files(dirs)
73
+ paths = dirs.flat_map { |dir| Dir.glob(File.join(project.root, dir, "**", "*.rb")) }
74
+ paths.sort.map { |path| project.relative(path) }
75
+ end
76
+
77
+ def visit(visitor_class, file)
78
+ visitor = visitor_class.new(file)
79
+ RubyParsing.tree_of_file(File.join(project.root, file)).accept(visitor)
80
+ visitor
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "index/graph"
4
+ require_relative "index/graph_cache"
5
+ require_relative "index/shapes"
6
+ require_relative "index/source_named_graph"
7
+ require_relative "index/signatures"
8
+ require_relative "index/static"
9
+
10
+ module RuntimeAnalysis
11
+ module Query
12
+ module Index
13
+ MAIN = "(main)"
14
+
15
+ def self.load(dir) = Graph.load(dir)
16
+ def self.cache = GraphCache.new
17
+ def self.shapes(graph, classes_only: false) = Shapes.new(graph, messages: !classes_only)
18
+ def self.shape(**) = Shapes.shape(**)
19
+ def self.segments(dir) = Reading.segments(dir)
20
+
21
+ def self.named(graph, project, declared: nil) = SourceNamedGraph.new(graph, project, declared: declared)
22
+
23
+ def self.as_named(graph, project) = SourceNamedGraph.over(graph, project)
24
+
25
+ def self.declared(root) = Signatures.declared_in(root)
26
+
27
+ def self.static(project) = Static.new(project)
28
+
29
+ def self.source(text) = Static.source(text)
30
+ end
31
+ end
32
+ end