rigortype 0.4.8 → 0.4.9

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 (349) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/docs/handbook/01-getting-started.md +1 -1
  4. data/docs/handbook/04-tuples-and-shapes.md +6 -1
  5. data/docs/handbook/12-lightweight-hkt.md +1 -1
  6. data/docs/handbook/appendix-typescript.md +28 -17
  7. data/docs/manual/02-cli-reference.md +10 -0
  8. data/docs/manual/03-configuration.md +1 -1
  9. data/docs/manual/04-diagnostics.md +1 -1
  10. data/docs/manual/10-mcp-server.md +122 -10
  11. data/docs/manual/plugins/rigor-activerecord.md +7 -0
  12. data/lib/rigor/analysis/check_rules/always_raises.rb +95 -0
  13. data/lib/rigor/analysis/check_rules/always_truthy_condition_collector.rb +32 -2
  14. data/lib/rigor/analysis/check_rules/argument_type.rb +802 -0
  15. data/lib/rigor/analysis/check_rules/call_facts.rb +2 -2
  16. data/lib/rigor/analysis/check_rules/dead_assignment_collector.rb +30 -1
  17. data/lib/rigor/analysis/check_rules/declaration_sourced_guard.rb +4 -5
  18. data/lib/rigor/analysis/check_rules/duplicate_hash_key_collector.rb +34 -0
  19. data/lib/rigor/analysis/check_rules/ivar_write_collector.rb +131 -4
  20. data/lib/rigor/analysis/check_rules/nil_receiver.rb +195 -0
  21. data/lib/rigor/analysis/check_rules/override_param_narrowed.rb +111 -0
  22. data/lib/rigor/analysis/check_rules/override_return_widened.rb +88 -0
  23. data/lib/rigor/analysis/check_rules/override_support.rb +246 -0
  24. data/lib/rigor/analysis/check_rules/override_visibility.rb +135 -0
  25. data/lib/rigor/analysis/check_rules/published_constant_guard.rb +75 -31
  26. data/lib/rigor/analysis/check_rules/raise_non_exception.rb +198 -0
  27. data/lib/rigor/analysis/check_rules/receiver_predicates.rb +576 -0
  28. data/lib/rigor/analysis/check_rules/return_in_ensure_collector.rb +52 -4
  29. data/lib/rigor/analysis/check_rules/return_type.rb +225 -0
  30. data/lib/rigor/analysis/check_rules/rigor_testing.rb +119 -0
  31. data/lib/rigor/analysis/check_rules/rule_ids.rb +7 -7
  32. data/lib/rigor/analysis/check_rules/rule_walk.rb +4 -3
  33. data/lib/rigor/analysis/check_rules/self_closedness_scanner.rb +2 -0
  34. data/lib/rigor/analysis/check_rules/self_undefined_method.rb +165 -0
  35. data/lib/rigor/analysis/check_rules/shadowed_rescue_collector.rb +34 -0
  36. data/lib/rigor/analysis/check_rules/source_arity.rb +2 -2
  37. data/lib/rigor/analysis/check_rules/special_global_write.rb +256 -0
  38. data/lib/rigor/analysis/check_rules/suppression.rb +275 -0
  39. data/lib/rigor/analysis/check_rules/undefined_method.rb +882 -0
  40. data/lib/rigor/analysis/check_rules/unreachable_branch.rb +97 -0
  41. data/lib/rigor/analysis/check_rules/unreachable_clause_collector.rb +42 -0
  42. data/lib/rigor/analysis/check_rules/unresolved_toplevel.rb +140 -0
  43. data/lib/rigor/analysis/check_rules/visibility_mismatch.rb +252 -0
  44. data/lib/rigor/analysis/check_rules/void_value_use_collector.rb +27 -0
  45. data/lib/rigor/analysis/check_rules/wrong_arity.rb +303 -0
  46. data/lib/rigor/analysis/check_rules.rb +66 -4931
  47. data/lib/rigor/analysis/dependency_recorder.rb +1 -1
  48. data/lib/rigor/analysis/dependency_source_inference/builder.rb +2 -4
  49. data/lib/rigor/analysis/dependency_source_inference/gem_resolver.rb +1 -2
  50. data/lib/rigor/analysis/dependency_source_inference/walker.rb +15 -15
  51. data/lib/rigor/analysis/incremental_session.rb +4 -4
  52. data/lib/rigor/analysis/path_expansion.rb +1 -1
  53. data/lib/rigor/analysis/plugin_fact_fingerprint.rb +5 -5
  54. data/lib/rigor/analysis/reachability/graph.rb +2 -3
  55. data/lib/rigor/analysis/reachability/plugin_roots.rb +2 -2
  56. data/lib/rigor/analysis/reachability/scan.rb +2 -2
  57. data/lib/rigor/analysis/rule_catalog.rb +2 -1
  58. data/lib/rigor/analysis/run_cache_key.rb +9 -8
  59. data/lib/rigor/analysis/runner/buffer_pool_dispatcher.rb +2 -2
  60. data/lib/rigor/analysis/runner/dependency_rows.rb +206 -0
  61. data/lib/rigor/analysis/runner/diagnostic_aggregator.rb +24 -22
  62. data/lib/rigor/analysis/runner/effect_cache_entries.rb +69 -0
  63. data/lib/rigor/analysis/runner/pool_coordinator.rb +6 -8
  64. data/lib/rigor/analysis/runner/project_pre_passes.rb +3 -5
  65. data/lib/rigor/analysis/runner/scope_seed.rb +144 -0
  66. data/lib/rigor/analysis/runner.rb +49 -296
  67. data/lib/rigor/analysis/template_unit_collector.rb +1 -1
  68. data/lib/rigor/analysis/template_units.rb +10 -10
  69. data/lib/rigor/analysis/worker_session.rb +7 -7
  70. data/lib/rigor/bleeding_edge.rb +3 -2
  71. data/lib/rigor/builtins/hkt_builtins.rb +17 -17
  72. data/lib/rigor/builtins/imported_refinements.rb +8 -16
  73. data/lib/rigor/builtins/regex_refinement.rb +3 -3
  74. data/lib/rigor/builtins/static_return_refinements.rb +143 -19
  75. data/lib/rigor/cache/incremental_snapshot.rb +21 -8
  76. data/lib/rigor/cache/rbs_descriptor.rb +18 -1
  77. data/lib/rigor/cli/coverage_renderer.rb +15 -1
  78. data/lib/rigor/cli/coverage_report.rb +19 -1
  79. data/lib/rigor/cli/diagnostic_formats.rb +3 -2
  80. data/lib/rigor/cli/docs_command.rb +5 -5
  81. data/lib/rigor/cli/effects_command.rb +7 -6
  82. data/lib/rigor/cli/fused_protection_report.rb +7 -7
  83. data/lib/rigor/cli/lsp_command.rb +1 -1
  84. data/lib/rigor/cli/mcp_command.rb +5 -2
  85. data/lib/rigor/cli/options.rb +4 -0
  86. data/lib/rigor/cli/plugins_command.rb +1 -1
  87. data/lib/rigor/cli/sig_gen_command.rb +1 -1
  88. data/lib/rigor/cli/skill_command.rb +5 -4
  89. data/lib/rigor/cli/triage_renderer.rb +3 -2
  90. data/lib/rigor/cli/unused_command.rb +4 -4
  91. data/lib/rigor/cli.rb +14 -0
  92. data/lib/rigor/configuration/coercion.rb +418 -0
  93. data/lib/rigor/configuration/defaults.rb +248 -0
  94. data/lib/rigor/configuration/dependencies.rb +1 -1
  95. data/lib/rigor/configuration/loader.rb +354 -0
  96. data/lib/rigor/configuration/severity_profile.rb +4 -4
  97. data/lib/rigor/configuration.rb +51 -860
  98. data/lib/rigor/effects/attribution.rb +1 -1
  99. data/lib/rigor/effects/effect_table.rb +1 -1
  100. data/lib/rigor/effects/identity.rb +3 -3
  101. data/lib/rigor/effects/local_ownership.rb +2 -2
  102. data/lib/rigor/effects/snapshot.rb +1 -1
  103. data/lib/rigor/effects/summary.rb +5 -4
  104. data/lib/rigor/environment/member_consistency.rb +10 -0
  105. data/lib/rigor/environment/rbs_loader.rb +19 -22
  106. data/lib/rigor/environment/reflection.rb +1 -1
  107. data/lib/rigor/environment/unreadable_signature.rb +21 -0
  108. data/lib/rigor/environment.rb +8 -8
  109. data/lib/rigor/flow_contribution/merger.rb +1 -0
  110. data/lib/rigor/hashing/xxh3.rb +1 -1
  111. data/lib/rigor/inference/acceptance.rb +23 -2
  112. data/lib/rigor/inference/alias_mutation_floor/site_collector.rb +5 -0
  113. data/lib/rigor/inference/alias_mutation_floor.rb +0 -5
  114. data/lib/rigor/inference/anonymous_meta_class.rb +2 -2
  115. data/lib/rigor/inference/block_call_timing.rb +6 -0
  116. data/lib/rigor/inference/budget_trace.rb +3 -2
  117. data/lib/rigor/inference/builtins/date_catalog.rb +3 -3
  118. data/lib/rigor/inference/builtins/method_catalog.rb +3 -3
  119. data/lib/rigor/inference/builtins/time_catalog.rb +3 -3
  120. data/lib/rigor/inference/call_ladder/literal_send.rb +98 -0
  121. data/lib/rigor/inference/call_ladder/overriding_def.rb +98 -0
  122. data/lib/rigor/inference/call_ladder/project_singleton.rb +362 -0
  123. data/lib/rigor/inference/call_ladder/refined.rb +212 -0
  124. data/lib/rigor/inference/call_ladder/top_level_def.rb +222 -0
  125. data/lib/rigor/inference/call_ladder/user_method.rb +247 -0
  126. data/lib/rigor/inference/call_ladder.rb +219 -0
  127. data/lib/rigor/inference/callee_write_census.rb +2 -2
  128. data/lib/rigor/inference/captured_locals.rb +2 -2
  129. data/lib/rigor/inference/clause_widening.rb +63 -1
  130. data/lib/rigor/inference/declaration_walk/context.rb +12 -12
  131. data/lib/rigor/inference/declaration_walk/rules.rb +750 -0
  132. data/lib/rigor/inference/declaration_walk/traversal.rb +9 -8
  133. data/lib/rigor/inference/declaration_walk.rb +4 -3
  134. data/lib/rigor/inference/def_node_resolver.rb +2 -2
  135. data/lib/rigor/inference/def_return_typer.rb +14 -6
  136. data/lib/rigor/inference/deferred_mutation.rb +120 -0
  137. data/lib/rigor/inference/define_method_block_self.rb +1 -1
  138. data/lib/rigor/inference/definer_resolution.rb +4 -4
  139. data/lib/rigor/inference/error_info.rb +8 -8
  140. data/lib/rigor/inference/expression_typer/composites.rb +348 -0
  141. data/lib/rigor/inference/expression_typer/per_element_assemblers.rb +233 -0
  142. data/lib/rigor/inference/expression_typer/writes.rb +218 -0
  143. data/lib/rigor/inference/expression_typer.rb +275 -1825
  144. data/lib/rigor/inference/external_ancestor_resolution.rb +2 -2
  145. data/lib/rigor/inference/fallback.rb +1 -0
  146. data/lib/rigor/inference/for_yield_slots.rb +126 -0
  147. data/lib/rigor/inference/genuine_send.rb +92 -0
  148. data/lib/rigor/inference/global_write_census.rb +2 -2
  149. data/lib/rigor/inference/hkt_body.rb +2 -0
  150. data/lib/rigor/inference/in_effect_refinements/walk.rb +2 -2
  151. data/lib/rigor/inference/index_write_widening.rb +4 -4
  152. data/lib/rigor/inference/indexed_narrowing.rb +1 -1
  153. data/lib/rigor/inference/intervening_ivar_reset.rb +46 -9
  154. data/lib/rigor/inference/ivar_writer_census.rb +1 -1
  155. data/lib/rigor/inference/loop_store_sites.rb +239 -0
  156. data/lib/rigor/inference/macro_block_self_type.rb +4 -3
  157. data/lib/rigor/inference/method_dispatcher/block_folding.rb +3 -2
  158. data/lib/rigor/inference/method_dispatcher/call_context.rb +84 -14
  159. data/lib/rigor/inference/method_dispatcher/composite_receiver.rb +144 -0
  160. data/lib/rigor/inference/method_dispatcher/constant_folding.rb +7 -5
  161. data/lib/rigor/inference/method_dispatcher/declared_constructor.rb +1 -1
  162. data/lib/rigor/inference/method_dispatcher/dependency_source.rb +90 -0
  163. data/lib/rigor/inference/method_dispatcher/discovered_method.rb +78 -0
  164. data/lib/rigor/inference/method_dispatcher/file_folding.rb +2 -2
  165. data/lib/rigor/inference/method_dispatcher/hkt_builtin_return.rb +40 -0
  166. data/lib/rigor/inference/method_dispatcher/iterator_dispatch.rb +1 -6
  167. data/lib/rigor/inference/method_dispatcher/literal_string_folding.rb +4 -2
  168. data/lib/rigor/inference/method_dispatcher/match_data_folding.rb +1 -1
  169. data/lib/rigor/inference/method_dispatcher/meta_introspection.rb +506 -0
  170. data/lib/rigor/inference/method_dispatcher/overload_selector.rb +2 -0
  171. data/lib/rigor/inference/method_dispatcher/plugin_contribution.rb +134 -0
  172. data/lib/rigor/inference/method_dispatcher/project_patched.rb +134 -0
  173. data/lib/rigor/inference/method_dispatcher/rbs_dispatch.rb +125 -43
  174. data/lib/rigor/inference/method_dispatcher/reduce_folding.rb +3 -4
  175. data/lib/rigor/inference/method_dispatcher/regexp_folding.rb +1 -1
  176. data/lib/rigor/inference/method_dispatcher/set_folding.rb +1 -1
  177. data/lib/rigor/inference/method_dispatcher/shape_dispatch.rb +19 -6
  178. data/lib/rigor/inference/method_dispatcher/singleton_folding.rb +3 -3
  179. data/lib/rigor/inference/method_dispatcher/singleton_mixin_dispatch.rb +7 -4
  180. data/lib/rigor/inference/method_dispatcher/static_refinement.rb +106 -0
  181. data/lib/rigor/inference/method_dispatcher/synthesized_stub.rb +41 -0
  182. data/lib/rigor/inference/method_dispatcher/synthetic_method_dispatch.rb +133 -0
  183. data/lib/rigor/inference/method_dispatcher/universal_object_dispatch.rb +2 -1
  184. data/lib/rigor/inference/method_dispatcher/user_class_fallback.rb +216 -0
  185. data/lib/rigor/inference/method_dispatcher.rb +175 -1361
  186. data/lib/rigor/inference/method_parameter_binder.rb +18 -18
  187. data/lib/rigor/inference/module_function_state.rb +5 -4
  188. data/lib/rigor/inference/mutation_widening.rb +20 -7
  189. data/lib/rigor/inference/narrowing/case_literals.rb +58 -0
  190. data/lib/rigor/inference/narrowing/class_dispatch.rb +302 -0
  191. data/lib/rigor/inference/narrowing/complements.rb +322 -0
  192. data/lib/rigor/inference/narrowing/presence_predicates.rb +205 -0
  193. data/lib/rigor/inference/narrowing/regex_match.rb +499 -0
  194. data/lib/rigor/inference/narrowing.rb +86 -1269
  195. data/lib/rigor/inference/object_mixins.rb +2 -2
  196. data/lib/rigor/inference/opaque_mutation.rb +214 -0
  197. data/lib/rigor/inference/optimistic_origin.rb +52 -14
  198. data/lib/rigor/inference/parameter_inference_collector.rb +1 -1
  199. data/lib/rigor/inference/pre_eval_constants.rb +11 -9
  200. data/lib/rigor/inference/precision_scanner.rb +27 -2
  201. data/lib/rigor/inference/project_patched_methods.rb +3 -3
  202. data/lib/rigor/inference/range_constant.rb +3 -3
  203. data/lib/rigor/inference/rbs_type_translator.rb +2 -2
  204. data/lib/rigor/inference/refine_self.rb +8 -7
  205. data/lib/rigor/inference/refined_dispatch.rb +2 -2
  206. data/lib/rigor/inference/return_barrier.rb +24 -9
  207. data/lib/rigor/inference/scope_indexer/alias_def_nodes.rb +311 -0
  208. data/lib/rigor/inference/scope_indexer/census_scope.rb +47 -0
  209. data/lib/rigor/inference/scope_indexer/certainty.rb +2 -2
  210. data/lib/rigor/inference/scope_indexer/class_cvar_census.rb +282 -0
  211. data/lib/rigor/inference/scope_indexer/class_cvars_collector.rb +12 -5
  212. data/lib/rigor/inference/scope_indexer/class_declarations.rb +228 -0
  213. data/lib/rigor/inference/scope_indexer/class_ivar_index.rb +2028 -0
  214. data/lib/rigor/inference/scope_indexer/constant_ivar_value.rb +4 -3
  215. data/lib/rigor/inference/scope_indexer/constant_tables.rb +844 -0
  216. data/lib/rigor/inference/scope_indexer/cvar_hierarchy.rb +159 -0
  217. data/lib/rigor/inference/scope_indexer/declaration_artifacts.rb +219 -0
  218. data/lib/rigor/inference/scope_indexer/declaration_signature.rb +212 -0
  219. data/lib/rigor/inference/scope_indexer/def_nestings_collector.rb +2 -2
  220. data/lib/rigor/inference/scope_indexer/factory_call_resolver.rb +53 -0
  221. data/lib/rigor/inference/scope_indexer/literal_receiver_mutations.rb +266 -0
  222. data/lib/rigor/inference/scope_indexer/member_layouts_collector.rb +8 -7
  223. data/lib/rigor/inference/scope_indexer/mixin_accumulator.rb +1 -1
  224. data/lib/rigor/inference/scope_indexer/mixin_effects.rb +470 -0
  225. data/lib/rigor/inference/scope_indexer/mixin_tables.rb +388 -0
  226. data/lib/rigor/inference/scope_indexer/mutated_carrier_widening.rb +163 -0
  227. data/lib/rigor/inference/scope_indexer/mutated_element_census.rb +211 -0
  228. data/lib/rigor/inference/scope_indexer/node_propagation.rb +520 -0
  229. data/lib/rigor/inference/scope_indexer/program_globals.rb +154 -0
  230. data/lib/rigor/inference/scope_indexer/published_constant_cvars.rb +146 -0
  231. data/lib/rigor/inference/scope_indexer/published_constant_ivars.rb +181 -0
  232. data/lib/rigor/inference/scope_indexer/shared_walk_tables.rb +823 -0
  233. data/lib/rigor/inference/scope_indexer/singleton_class_sources.rb +85 -0
  234. data/lib/rigor/inference/scope_indexer/superclasses_collector.rb +5 -5
  235. data/lib/rigor/inference/scope_indexer.rb +2657 -9558
  236. data/lib/rigor/inference/self_call_ivar_writes.rb +22 -3
  237. data/lib/rigor/inference/set_mutation.rb +2 -2
  238. data/lib/rigor/inference/singleton_hook_definers.rb +185 -0
  239. data/lib/rigor/inference/statement_evaluator/def_arguments_and_assertions.rb +365 -0
  240. data/lib/rigor/inference/statement_evaluator/pattern_bindings.rb +581 -0
  241. data/lib/rigor/inference/statement_evaluator.rb +201 -1008
  242. data/lib/rigor/inference/synthetic_method_scanner.rb +0 -3
  243. data/lib/rigor/language_server/completion_provider.rb +8 -8
  244. data/lib/rigor/language_server/diagnostic_publisher.rb +6 -7
  245. data/lib/rigor/language_server/document_symbol_provider.rb +4 -6
  246. data/lib/rigor/language_server/folding_range_provider.rb +3 -3
  247. data/lib/rigor/language_server/hover_provider.rb +2 -2
  248. data/lib/rigor/language_server/loop.rb +2 -2
  249. data/lib/rigor/language_server/server.rb +3 -2
  250. data/lib/rigor/language_server/signature_help_provider.rb +7 -9
  251. data/lib/rigor/lens/document.rb +10 -1
  252. data/lib/rigor/lens/file_reader.rb +2 -1
  253. data/lib/rigor/mcp/loop.rb +48 -6
  254. data/lib/rigor/mcp/server.rb +248 -23
  255. data/lib/rigor/mcp/skill_catalog.rb +157 -0
  256. data/lib/rigor/plugin/base.rb +22 -4
  257. data/lib/rigor/plugin/effect_attribution.rb +1 -1
  258. data/lib/rigor/plugin/inflector.rb +2 -2
  259. data/lib/rigor/plugin/io_boundary.rb +1 -1
  260. data/lib/rigor/plugin/loader.rb +4 -4
  261. data/lib/rigor/plugin/macro/heredoc_template.rb +4 -3
  262. data/lib/rigor/plugin/optimistic_return.rb +20 -0
  263. data/lib/rigor/plugin/registry.rb +2 -2
  264. data/lib/rigor/plugin/template_unit.rb +4 -2
  265. data/lib/rigor/plugin/trust_policy.rb +2 -1
  266. data/lib/rigor/plugin.rb +1 -1
  267. data/lib/rigor/protection/mutator.rb +8 -9
  268. data/lib/rigor/rbs_extended/reporter.rb +2 -2
  269. data/lib/rigor/rbs_extended.rb +2 -2
  270. data/lib/rigor/reflection/constant_ancestors.rb +1 -1
  271. data/lib/rigor/reflection/project_method.rb +2 -2
  272. data/lib/rigor/reflection.rb +4 -4
  273. data/lib/rigor/scope/discovery_index.rb +15 -10
  274. data/lib/rigor/scope/resolution_chain.rb +29 -6
  275. data/lib/rigor/scope.rb +89 -32
  276. data/lib/rigor/sig_gen/generator.rb +5 -4
  277. data/lib/rigor/sig_gen/renderer.rb +3 -3
  278. data/lib/rigor/sig_gen/writer.rb +8 -6
  279. data/lib/rigor/source/parameter_envelope.rb +1 -1
  280. data/lib/rigor/triage/catalogue.rb +4 -4
  281. data/lib/rigor/type/combinator.rb +2 -2
  282. data/lib/rigor/type/data_instance.rb +2 -2
  283. data/lib/rigor/type/struct_instance.rb +3 -3
  284. data/lib/rigor/type_node/generic.rb +2 -1
  285. data/lib/rigor/type_of_memo.rb +2 -1
  286. data/lib/rigor/typing_state.rb +3 -2
  287. data/lib/rigor/version.rb +1 -1
  288. data/plugins/rigor-actioncable/lib/rigor/plugin/actioncable/analyzer.rb +2 -2
  289. data/plugins/rigor-actioncable/lib/rigor/plugin/actioncable/channel_discoverer.rb +1 -1
  290. data/plugins/rigor-actionmailer/lib/rigor/plugin/actionmailer/mailer_discoverer.rb +4 -4
  291. data/plugins/rigor-actionmailer/lib/rigor/plugin/actionmailer.rb +1 -1
  292. data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/controller_discoverer.rb +4 -3
  293. data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/effects.rb +7 -5
  294. data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/erb_compiler.rb +4 -2
  295. data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/render_locals.rb +1 -1
  296. data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/view_assigns.rb +3 -2
  297. data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack.rb +7 -7
  298. data/plugins/rigor-activejob/lib/rigor/plugin/activejob/job_discoverer.rb +1 -1
  299. data/plugins/rigor-activejob/lib/rigor/plugin/activejob.rb +2 -2
  300. data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/model_discoverer.rb +9 -8
  301. data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/model_index.rb +3 -2
  302. data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/schema_parser.rb +15 -7
  303. data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/schema_table.rb +10 -3
  304. data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/structure_sql_parser.rb +12 -1
  305. data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord.rb +20 -5
  306. data/plugins/rigor-activestorage/lib/rigor/plugin/activestorage/attachment_discoverer.rb +1 -1
  307. data/plugins/rigor-activestorage/lib/rigor/plugin/activestorage.rb +3 -2
  308. data/plugins/rigor-activesupport-core-ext/lib/rigor/plugin/activesupport_core_ext/effects.rb +1 -1
  309. data/plugins/rigor-activesupport-core-ext/lib/rigor/plugin/activesupport_core_ext.rb +8 -7
  310. data/plugins/rigor-dry-schema/lib/rigor/plugin/dry_schema.rb +1 -1
  311. data/plugins/rigor-dry-types/lib/rigor/plugin/dry_types/alias_scanner.rb +1 -1
  312. data/plugins/rigor-dry-types/lib/rigor/plugin/dry_types.rb +6 -7
  313. data/plugins/rigor-factorybot/lib/rigor/plugin/factorybot/factory_discoverer.rb +3 -2
  314. data/plugins/rigor-graphql/lib/rigor/plugin/graphql/type_scanner.rb +4 -3
  315. data/plugins/rigor-hanami/lib/rigor/plugin/hanami.rb +4 -2
  316. data/plugins/rigor-minitest/lib/rigor/plugin/minitest.rb +1 -1
  317. data/plugins/rigor-pundit/lib/rigor/plugin/pundit.rb +1 -1
  318. data/plugins/rigor-rails-i18n/lib/rigor/plugin/rails_i18n/analyzer.rb +5 -3
  319. data/plugins/rigor-rails-i18n/lib/rigor/plugin/rails_i18n.rb +1 -1
  320. data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/devise_routes.rb +4 -5
  321. data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/doorkeeper_routes.rb +2 -2
  322. data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/helper_discoverer.rb +5 -4
  323. data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/helper_table.rb +3 -2
  324. data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/routes_parser.rb +30 -31
  325. data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes.rb +13 -15
  326. data/plugins/rigor-rbnacl/lib/rigor/plugin/rbnacl.rb +1 -1
  327. data/plugins/rigor-rspec/lib/rigor/plugin/rspec/let_scope_index.rb +4 -3
  328. data/plugins/rigor-rspec/lib/rigor/plugin/rspec/matcher_analyzer.rb +16 -13
  329. data/plugins/rigor-rspec/lib/rigor/plugin/rspec/scope_walker.rb +3 -2
  330. data/plugins/rigor-shoulda-matchers/lib/rigor/plugin/shoulda_matchers/analyzer.rb +6 -6
  331. data/plugins/rigor-sidekiq/lib/rigor/plugin/sidekiq/analyzer.rb +3 -3
  332. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/assertion_recognizer.rb +2 -2
  333. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/catalog.rb +3 -2
  334. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/catalog_walker.rb +6 -5
  335. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/sig_parser.rb +2 -2
  336. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/sigil_detector.rb +4 -5
  337. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/type_translator.rb +1 -1
  338. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet.rb +3 -4
  339. data/sig/rigor/analysis/check_rules/suppression.rbs +9 -0
  340. data/sig/rigor/analysis/dependency_recorder.rbs +9 -0
  341. data/sig/rigor/inference.rbs +275 -11
  342. data/sig/rigor/plugin/base.rbs +14 -5
  343. data/sig/rigor/plugin/optimistic_return.rbs +16 -0
  344. data/sig/rigor/scope.rbs +7 -1
  345. data/sig/rigor/source.rbs +4 -0
  346. data/skills/rigor-mcp-setup/SKILL.md +20 -6
  347. data/skills/rigor-plugin-review/SKILL.md +3 -3
  348. data/skills/rigor-type-oracle/references/01-oracle-commands.md +1 -1
  349. metadata +90 -3
@@ -0,0 +1,470 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "../../source/constant_path"
6
+ require_relative "../../source/node_children"
7
+ require_relative "../../source/node_walker"
8
+ require_relative "../declaration_walk/rules"
9
+
10
+ module Rigor
11
+ module Inference
12
+ module ScopeIndexer
13
+ # The mixin effects and taints, moved out of `scope_indexer.rb` whole (ADR-116 WD9, #3400): what the include
14
+ # walk ({MixinTables}) and the extend walk (`ScopeIndexer.extend_tables`) both ask about a call that mixes a
15
+ # module in, or may. Neither walk is here: each hands these functions the call, the owner it resolved and the
16
+ # {MixinAccumulator} it fills.
17
+ #
18
+ # Eight functions are public, because both walks call each. {.mixin_effects} names the ancestry sides a mixin
19
+ # call reshapes and their owners. {.taint_unrecorded_mixin}, {.taint_computed_mixin_send} and
20
+ # {.taint_opaque_eval} taint an owner side for a call the tables cannot hold: a mixin call the walk did not
21
+ # record (#2991), a `send` whose name is computed (#3006), and an eval block on a receiver nothing names.
22
+ # {.hook_def_params} lists the parameters of a `self.included(base)`-style hook, {.prepend_call_receiver} names
23
+ # the class a constant receiver targets (#1123), {.meta_new_mixin_owner} names the owner a constant write that
24
+ # creates a class gives its block, and {.program_statements} answers the top-level statements of a parsed
25
+ # file. The other 23 are private: the two arms of {.mixin_effects}, the four receiver predicates, the two scans
26
+ # of an eval block, and the fifteen functions that read a computed `send`, which are the hook's taint, the name
27
+ # argument, and the scan for the literals a local or an iterated block parameter can hold (#3024).
28
+ #
29
+ # It asks {DeclarationWalk::Rules} for a receiver's prefix and a class-creating write's, and calls three
30
+ # functions left in `scope_indexer.rb`, recognisers the other walkers ask too, at four sites:
31
+ # `ScopeIndexer.opaque_mixin_argument?` (one), `ScopeIndexer.mixin_call_view` (two) and
32
+ # `ScopeIndexer.lexical_self_receiver?` (one). Three constants moved with it, the ones only it read, private as
33
+ # they were: `LOCAL_SCOPE_BOUNDARIES`, `HOOK_DEFS` and `MIXIN_EVAL_CALLS`. It reads
34
+ # `ScopeIndexer::ARRAY_ITERATORS`, `INSTANCE_MIXIN_NAMES`, `MIXIN_KINDS` and `SEND_CALL_NAMES` lexically, as
35
+ # {NodePropagation} reads the indexer's constants: other rows of `scope_indexer.rb` read each.
36
+ #
37
+ # `scope_indexer.rb` requires this file, and this file does not require it back. It requires the file of what
38
+ # else it names. So the file loads alone, and until `scope_indexer.rb` has loaded it answers where it reaches
39
+ # none of those seven names. {.program_statements}, {.hook_def_params}, {.prepend_call_receiver},
40
+ # {.meta_new_mixin_owner} and {.mixin_effects} answer as they do. {.taint_unrecorded_mixin} answers for no
41
+ # call: one with an argument is a `NoMethodError` (`opaque_mixin_argument?`) and one with none a `NameError`
42
+ # (`INSTANCE_MIXIN_NAMES`). {.taint_computed_mixin_send} answers only when it is handed no class
43
+ # (`SEND_CALL_NAMES`), and {.taint_opaque_eval} until its scan of an eval block meets a call (`MIXIN_KINDS`).
44
+ module MixinEffects # rubocop:disable Metrics/ModuleLength
45
+ module_function
46
+
47
+ # The top-level statements of a parsed file: the one body whose declarations need no enclosing
48
+ # declaration to be direct ({MixinAccumulator}).
49
+ def program_statements(root)
50
+ root.is_a?(Prism::ProgramNode) ? root.statements : root
51
+ end
52
+
53
+ # Issue #2991 — an unrecorded `include` / `prepend` that reaches the instance side (its receiver is not a
54
+ # `singleton_class` call) leaves that side's module SET unknown when an argument is not a constant
55
+ # (`include(*MODS)`, `include helper`) or the receiver is a hook parameter (`base.include M`), which lists
56
+ # {MixinAccumulator::UNNAMED}. A `Module.new { … }` argument keeps its #2975 reading. A hook's `base.extend M`
57
+ # or `base.singleton_class.include M` taints the instance side's order alone: it mixes nothing into it.
58
+ #
59
+ # Issue #2995 — an instance-side call whose arguments are all constants is recorded wherever the walk names its
60
+ # owner (`self.include M` and a receiverless `send(:include, M)` included), so none reaches here with a module
61
+ # the tables do not hold. The extends walk's taint of `Const.include M` inside `class << self` is the one
62
+ # constant-argument call that does, and the include walk records that edge under `Const`.
63
+ #
64
+ # Issue #2994 — the singleton side lists {MixinAccumulator::UNNAMED} too when an unrecorded call reaching it has
65
+ # an argument that is not a constant: `extend(*EMODS)`, `extend helper`, `include(*MODS)` inside
66
+ # `class << self`, or a `self` argument. A hook parameter's `base.extend(*X)` extends the includer, not the
67
+ # module the hook is written in, so it leaves that module's singleton side unnamed-free.
68
+ def taint_unrecorded_mixin(node, kind, arguments, effects, accumulator)
69
+ opaque = arguments.any? { |arg| ScopeIndexer.opaque_mixin_argument?(arg) }
70
+ hook = hook_parameter_receiver?(node.receiver, accumulator)
71
+ unnamed = INSTANCE_MIXIN_NAMES.include?(kind) && !singleton_class_eval_receiver?(node.receiver) &&
72
+ (hook || opaque)
73
+ effects.each do |owner, side|
74
+ accumulator.taint(owner, side, unnamed: side == :include ? unnamed : opaque && !hook)
75
+ end
76
+ end
77
+
78
+ # A hook parameter itself (`base` in `def self.included(base)`): its class is the includer, which the walk
79
+ # cannot name, so a mixin on it is read as unnamed rather than by its constant (#2991).
80
+ def hook_parameter_receiver?(receiver, accumulator)
81
+ receiver.is_a?(Prism::LocalVariableReadNode) && accumulator.hook_local?(receiver.name)
82
+ end
83
+
84
+ # Issue #3006 — a receiverless or `self.` `send` / `public_send` / `__send__` written in a class body
85
+ # ({MixinAccumulator#class_body_scope}) whose name is computed and which passes at least one further argument
86
+ # may be an `include`, `prepend` or `extend` the walk cannot record: it lists {MixinAccumulator::UNNAMED} on
87
+ # `side` of the enclosing class when the name may be one of `kinds`. A name that is a local of the call's scope
88
+ # bound only to name literals is read as those names ({.local_send_names}), so `m = :attr_reader; send(m, :x)`
89
+ # marks nothing. A hook parameter's `base.send(meth, M)` is read wherever the hook is, as
90
+ # {.taint_unrecorded_mixin} reads `base.send(:include, M)`.
91
+ def taint_computed_mixin_send(node, current_class, accumulator, kinds, side, in_singleton: false)
92
+ return unless current_class
93
+
94
+ name = computed_send_name(node, accumulator)
95
+ return if name.nil?
96
+
97
+ scope = accumulator.class_body_scope(node)
98
+ hook = hook_parameter_receiver?(node.receiver, accumulator)
99
+ return if scope.nil? && !hook
100
+
101
+ names = scope && local_send_names(name, scope)
102
+ # Inside `class << self` the instance walk is ownerless, so this walk keeps the hook's instance-side taint
103
+ # too, as {ScopeIndexer.record_extend_call} does for `base.send(:include, M)`.
104
+ sides = in_singleton ? [side, :include] : [side]
105
+ return taint_computed_hook_send(current_class, accumulator, names, sides) if hook
106
+ return if names&.none? { |literal| kinds.any? { |kind| kind.name == literal } }
107
+
108
+ accumulator.taint(current_class, side, unnamed: true)
109
+ end
110
+
111
+ # A hook parameter's computed `send`, read as {.taint_unrecorded_mixin} reads the literal one: it taints the
112
+ # enclosing module's `sides` when the name may be any mixin's, and lists the unnamed marker on the instance side
113
+ # alone, when the name may be `include` or `prepend`.
114
+ def taint_computed_hook_send(current_class, accumulator, names, sides)
115
+ return if names&.none? { |literal| MIXIN_KINDS.any? { |kind| kind.name == literal } }
116
+
117
+ instance = names.nil? || names.any? { |literal| INSTANCE_MIXIN_NAMES.any? { |kind| kind.name == literal } }
118
+ sides.each { |side| accumulator.taint(current_class, side, unnamed: side == :include && instance) }
119
+ end
120
+
121
+ # The name argument of a receiverless, `self.` or hook-parameter `send`-family call that passes at least one
122
+ # further argument, when no literal spells it; else nil.
123
+ def computed_send_name(node, accumulator)
124
+ return unless SEND_CALL_NAMES.include?(node.name)
125
+ return unless ScopeIndexer.lexical_self_receiver?(node.receiver) ||
126
+ hook_parameter_receiver?(node.receiver, accumulator)
127
+
128
+ arguments = node.arguments&.arguments
129
+ return if arguments.nil? || arguments.size < 2
130
+
131
+ first = arguments.first
132
+ first unless literal_send_name(first)
133
+ end
134
+
135
+ LOCAL_SCOPE_BOUNDARIES = [Prism::DefNode, Prism::ClassNode, Prism::ModuleNode, Prism::SingletonClassNode].freeze
136
+ private_constant :LOCAL_SCOPE_BOUNDARIES
137
+
138
+ # The names a local `send` argument can hold: every literal a `local = :name` / `local = "name"` in `scope`
139
+ # writes, or nil when the argument is not a local, or when the local has no such write or any other binding in
140
+ # `scope` — a parameter (the `def`'s or a block's, which may shadow it), a multiple or operator write, a pattern
141
+ # or a regexp capture. Blocks share their scope's locals, so they are searched; a nested `def` or declaration
142
+ # opens a scope of its own, so it is not.
143
+ #
144
+ # Issue #3024 — one block parameter is not opaque: as the FIRST parameter of `each` / `each_with_index` /
145
+ # `map` over a literal array (or a constant the same scope assigns a literal array), it holds each element in
146
+ # turn, so the elements are read as the candidate names ({.iterated_element_source}). An element that is not
147
+ # a name literal still means "may be anything".
148
+ def local_send_names(argument, scope)
149
+ return nil unless argument.is_a?(Prism::LocalVariableReadNode)
150
+
151
+ names = []
152
+ root = scope.is_a?(Prism::DefNode) ? scope.compact_child_nodes : [scope]
153
+ return nil unless collect_local_send_names(root, argument.name, names, root)
154
+
155
+ names.empty? ? nil : names
156
+ end
157
+
158
+ def collect_local_send_names(nodes, local, names, scope_nodes)
159
+ nodes.all? do |node|
160
+ next true if LOCAL_SCOPE_BOUNDARIES.any? { |kind| node.is_a?(kind) }
161
+
162
+ iterated = iterated_element_names(node, local, scope_nodes)
163
+ next false if iterated == false
164
+
165
+ if iterated
166
+ names.concat(iterated)
167
+ next collect_local_send_names(iterated_element_children(node), local, names, scope_nodes)
168
+ end
169
+
170
+ literal = local_send_name_literal(node, local)
171
+ next false if literal == :clash
172
+
173
+ names << literal if literal
174
+
175
+ collect_local_send_names(node.compact_child_nodes, local, names, scope_nodes)
176
+ end
177
+ end
178
+
179
+ # Issue #3024 — the `each` / `each_with_index` / `map` arm of {.collect_local_send_names}: when `node` is
180
+ # such an iterator whose first block parameter is `local`, the iterated array's elements are `local`'s
181
+ # candidate names ({.iterated_element_source}), each read as {.literal_send_name}. False when an element
182
+ # may spell any name; nil when `node` is not that iterator, so the caller's plain-write and binding checks
183
+ # apply.
184
+ def iterated_element_names(node, local, scope_nodes)
185
+ elements = iterated_element_source(node, local, scope_nodes)
186
+ return if elements.nil?
187
+ return false if elements == :unknown
188
+
189
+ names = elements.map { |element| literal_send_name(element) }
190
+ return false if names.any?(&:nil?)
191
+
192
+ names
193
+ end
194
+
195
+ # The name literal a `local = :name` / `local = "name"` write binds `local` to for
196
+ # {.collect_local_send_names}, `:clash` when `node` binds `local` to anything else — a non-literal write,
197
+ # a target, an operator write, a parameter — or nil when `node` does not bind `local` at all.
198
+ def local_send_name_literal(node, local)
199
+ if node.is_a?(Prism::LocalVariableWriteNode) && node.name == local
200
+ name = literal_send_name(node.value)
201
+ return :clash if name.nil?
202
+
203
+ return name
204
+ end
205
+ :clash if local_binding?(node, local)
206
+ end
207
+
208
+ # Issue #3024 — the array elements `node` iterates when it is an `each` / `each_with_index` / `map` call
209
+ # whose FIRST block parameter is `local`: a literal `ArrayNode` receiver's elements, or a bare constant the
210
+ # same scope assigns a literal array exactly once ({.constant_array_elements}). `:unknown` when the call is
211
+ # such an iterator on `local` but its elements cannot be read — a non-literal receiver, or a constant that
212
+ # is unwritten, multiply written or not array-valued — so the parameter may hold anything; nil when `node`
213
+ # is not the iterator or `local` is not its first parameter.
214
+ def iterated_element_source(node, local, scope_nodes)
215
+ return unless node.is_a?(Prism::CallNode) && ARRAY_ITERATORS.include?(node.name)
216
+
217
+ block = node.block
218
+ return unless block.is_a?(Prism::BlockNode)
219
+ return unless iterated_first_parameter?(block, local)
220
+
221
+ iterated_receiver_elements(node.receiver, scope_nodes) || :unknown
222
+ end
223
+
224
+ # Whether `local` is the first parameter of `block` and no other parameter binds it too (`|m, m = x|` — the
225
+ # later binding wins, so the first is not the element).
226
+ def iterated_first_parameter?(block, local)
227
+ declared = block.parameters
228
+ return false unless declared.is_a?(Prism::BlockParametersNode)
229
+
230
+ parameters = declared.parameters
231
+ first = parameters&.requireds&.first
232
+ return false unless first.is_a?(Prism::RequiredParameterNode) && first.name == local
233
+
234
+ Source::NodeWalker.each(parameters).none? do |param|
235
+ !param.equal?(first) && local_binding?(param, local)
236
+ end
237
+ end
238
+
239
+ # The elements of the array `receiver` denotes for {.iterated_element_source}: a literal array's own, or a
240
+ # bare constant's when the same scope assigns it a literal array exactly once. Nil when it denotes anything
241
+ # else — a qualified or rooted constant, a local, a call — so the iterated value may be anything.
242
+ def iterated_receiver_elements(receiver, scope_nodes)
243
+ return receiver.elements if receiver.is_a?(Prism::ArrayNode)
244
+ return unless receiver.is_a?(Prism::ConstantReadNode)
245
+
246
+ constant_array_elements(receiver.name, scope_nodes)
247
+ end
248
+
249
+ # The elements of the literal array `name` is assigned in `nodes`' scope, or nil when it is written any
250
+ # other number of times or its value is not an array literal — the constant may then hold anything. Only
251
+ # a single plain `NAME = […]` write closes; in-place mutation (`KINDS << :include`) is unmodeled.
252
+ def constant_array_elements(name, nodes)
253
+ writes = constant_write_nodes(nodes, name)
254
+ return unless writes.one? && writes.first.is_a?(Prism::ConstantWriteNode)
255
+
256
+ value = writes.first.value
257
+ value.elements if value.is_a?(Prism::ArrayNode)
258
+ end
259
+
260
+ # Every write binding the bare constant `name` in `nodes`' scope — a plain `NAME = …`, a multi-assign
261
+ # target or an operator / `&&=` / `||=` write, mirroring {.local_binding?} — stopping where
262
+ # {.collect_local_send_names} stops, since a nested `def` or declaration binds its own constants — so
263
+ # {.constant_array_elements} can tell one literal-array assignment from none or several.
264
+ def constant_write_nodes(nodes, name)
265
+ nodes.flat_map do |node|
266
+ next [] if LOCAL_SCOPE_BOUNDARIES.any? { |kind| node.is_a?(kind) }
267
+
268
+ own = constant_binding?(node, name) ? [node] : []
269
+ own + constant_write_nodes(node.compact_child_nodes, name)
270
+ end
271
+ end
272
+
273
+ # A node that binds the bare constant `name`: every `Prism::Constant*` binding class — write, target,
274
+ # operator, `&&=` and `||=` — except the `ConstantReadNode` and `ConstantPath*` spellings, which read
275
+ # `name` or bind a qualified `A::NAME` rather than `name` itself.
276
+ def constant_binding?(node, name)
277
+ return false unless node.respond_to?(:name) && node.name == name
278
+
279
+ klass = node.class.name
280
+ klass.start_with?("Prism::Constant") && !klass.start_with?("Prism::ConstantPath") &&
281
+ !node.is_a?(Prism::ConstantReadNode)
282
+ end
283
+
284
+ # The children of an iterator call {.iterated_element_source} matched, minus its block's parameters — the
285
+ # first is `local` bound to the elements and the rest bind other names — so the usual scan still sees every
286
+ # other binding of `local` in the receiver, arguments and body.
287
+ def iterated_element_children(node)
288
+ [node.receiver, node.arguments, node.block.body].compact
289
+ end
290
+
291
+ # The method name a `send` argument spells: a name literal's, or `""` — no mixin's — for an interpolation with a
292
+ # literal part no mixin name contains (`"#{name}_from_params"`). Nil when it may spell any name.
293
+ def literal_send_name(value)
294
+ return value.unescaped if value.is_a?(Prism::SymbolNode) || value.is_a?(Prism::StringNode)
295
+ return nil unless value.is_a?(Prism::InterpolatedStringNode) || value.is_a?(Prism::InterpolatedSymbolNode)
296
+
297
+ excluded = value.parts.any? do |part|
298
+ part.is_a?(Prism::StringNode) && MIXIN_KINDS.none? { |kind| kind.name.include?(part.unescaped) }
299
+ end
300
+ excluded ? "" : nil
301
+ end
302
+
303
+ # A node other than a plain write that binds `local`: a target, an operator write or a parameter.
304
+ def local_binding?(node, local)
305
+ return false if node.is_a?(Prism::LocalVariableReadNode) || !node.respond_to?(:name) || node.name != local
306
+
307
+ node.class.name.start_with?("Prism::LocalVariable") || node.class.name.end_with?("ParameterNode") ||
308
+ node.is_a?(Prism::BlockLocalVariableNode)
309
+ end
310
+
311
+ # `[[owner, side], …]` — the ancestry sides a mixin call reshapes. `self` and the implicit receiver
312
+ # are the enclosing class (`:include` for `include` / `prepend`, `:extend` for `extend`; inside
313
+ # `class << self` `include` / `prepend` flip to `:extend` and `extend` names no owner); `singleton_class` is the
314
+ # singleton side; a constant is that class; any other receiver reshapes nothing, except a hook parameter
315
+ # (`base.include M` in `self.included(base)`), which taints both sides of the enclosing class.
316
+ def mixin_effects(node, kind, qualified_prefix, current_class, accumulator, in_singleton:)
317
+ receiver = node.receiver
318
+ side = kind == :extend ? :extend : :include
319
+ pairs =
320
+ if receiver.nil? || receiver.is_a?(Prism::SelfNode)
321
+ own_mixin_effects(kind, side, current_class, in_singleton)
322
+ else
323
+ receiver_mixin_effects(node, side, qualified_prefix, current_class, accumulator)
324
+ end
325
+ pairs.reject { |owner, _side| owner.nil? }
326
+ end
327
+
328
+ # The implicit-receiver / `self` arm of {.mixin_effects}: inside `class << self`, `include` / `prepend`
329
+ # are singleton-side and `extend` reaches the singleton's own singleton, which nothing names.
330
+ def own_mixin_effects(kind, side, current_class, in_singleton)
331
+ return [] if in_singleton && kind == :extend
332
+
333
+ [[current_class, in_singleton ? :extend : side]]
334
+ end
335
+
336
+ # A receiver that is not `self` reshapes the class it names, or the singleton of `singleton_class`. Any
337
+ # other receiver is an object of its own (`ids.prepend(1)`, `obj.extend(Decorator)`) and reshapes none of
338
+ # the enclosing class's ancestry — except in a `self.included(base)`-style hook, where `base` IS the
339
+ # class that includes the enclosing module and a call on it taints both sides of the enclosing class.
340
+ def receiver_mixin_effects(node, side, qualified_prefix, current_class, accumulator)
341
+ receiver = node.receiver
342
+ return [[current_class, :extend]] if singleton_class_receiver?(receiver)
343
+
344
+ owner = prepend_call_receiver(node, qualified_prefix)
345
+ return [[owner, side]] if owner
346
+ return [] unless hook_receiver?(receiver, accumulator)
347
+
348
+ [[current_class, :include], [current_class, :extend]]
349
+ end
350
+
351
+ HOOK_DEFS = %i[included extended prepended inherited].freeze
352
+ MIXIN_EVAL_CALLS = %i[class_eval module_eval class_exec module_exec instance_eval instance_exec].freeze
353
+ private_constant :HOOK_DEFS, :MIXIN_EVAL_CALLS
354
+
355
+ # The parameter names of a `def included(base)`-style hook (a hook is written `def self.included`, or
356
+ # `def included` inside `class << self`), else nil.
357
+ def hook_def_params(node)
358
+ return nil unless node.is_a?(Prism::DefNode) && HOOK_DEFS.include?(node.name)
359
+
360
+ parameters = node.parameters
361
+ return [] unless parameters
362
+
363
+ (parameters.requireds + parameters.optionals).filter_map do |parameter|
364
+ parameter.name if parameter.respond_to?(:name)
365
+ end
366
+ end
367
+
368
+ # Whether `receiver` is a hook parameter, or `singleton_class` / a no-argument call chain off one.
369
+ def hook_receiver?(receiver, accumulator)
370
+ case receiver
371
+ when Prism::LocalVariableReadNode then accumulator.hook_local?(receiver.name)
372
+ when Prism::CallNode
373
+ receiver.arguments.nil? && receiver.receiver && hook_receiver?(receiver.receiver, accumulator)
374
+ else false
375
+ end
376
+ end
377
+
378
+ # `base.class_eval { include X }` in a hook, and `opaque.module_eval { include B }` anywhere: the block
379
+ # opens a class this walk cannot name, so its mixin calls record nowhere, yet the enclosing class's
380
+ # ancestry is what the reader was asked about. Taint both sides of the enclosing class. Outside a hook
381
+ # the block is scanned for a mixin call first, so an ordinary `obj.instance_eval { … }` DSL taints nothing.
382
+ def taint_opaque_eval(node, current_class, accumulator)
383
+ return unless current_class && node.block.is_a?(Prism::BlockNode) && MIXIN_EVAL_CALLS.include?(node.name)
384
+
385
+ receiver = node.receiver
386
+ return if receiver.nil? || receiver.is_a?(Prism::SelfNode)
387
+ return if Source::ConstantPath.qualified_name_or_nil(receiver)
388
+
389
+ hook = hook_receiver?(receiver, accumulator)
390
+ return unless hook || block_mixes_in?(node.block)
391
+
392
+ # Issue #2991 — the set is unknown only where the block may mix into the class itself: a hook's `base`, or a
393
+ # receiverless `include` / `prepend` in the block of a receiver other than a `singleton_class`.
394
+ unnamed = hook || (!singleton_class_eval_receiver?(receiver) && block_mixes_into_self?(node.block))
395
+ accumulator.taint(current_class, :include, unnamed: unnamed)
396
+ accumulator.taint(current_class, :extend)
397
+ end
398
+
399
+ def block_mixes_in?(node)
400
+ return true if node.is_a?(Prism::CallNode) &&
401
+ (MIXIN_KINDS.include?(node.name) || ScopeIndexer.mixin_call_view(node))
402
+
403
+ found = false
404
+ node.rigor_each_child { |child| found ||= block_mixes_in?(child) }
405
+ found
406
+ end
407
+
408
+ # A receiverless or `self.` `include` / `prepend` (or its `send` spelling) anywhere in `node`.
409
+ def block_mixes_into_self?(node)
410
+ if node.is_a?(Prism::CallNode) && (node.receiver.nil? || node.receiver.is_a?(Prism::SelfNode))
411
+ view = ScopeIndexer.mixin_call_view(node)
412
+ return true if view && INSTANCE_MIXIN_NAMES.include?(view.first)
413
+ end
414
+
415
+ found = false
416
+ node.rigor_each_child { |child| found ||= block_mixes_into_self?(child) }
417
+ found
418
+ end
419
+
420
+ def singleton_class_eval_receiver?(receiver)
421
+ receiver.is_a?(Prism::CallNode) && receiver.name == :singleton_class
422
+ end
423
+
424
+ def singleton_class_receiver?(receiver)
425
+ receiver.is_a?(Prism::CallNode) && receiver.name == :singleton_class && receiver.arguments.nil? &&
426
+ (receiver.receiver.nil? || receiver.receiver.is_a?(Prism::SelfNode))
427
+ end
428
+
429
+ # Issue #1123 — the class a `Recv.prepend(M)` call form targets, as the qualified name the prepend
430
+ # table is keyed by, or nil when the receiver names no static class. The receiver is a constant READ
431
+ # at the call site, so it resolves in the `Module.nesting` the call is written in — the same rule (and
432
+ # the same helper) `class_eval` receivers follow: `::B` names the top level wherever it is written, an
433
+ # unqualified `X` is `<nesting entry>::X` for the innermost entry the FILE declares, and the
434
+ # as-written name stands when no rung declares it. A receiver that names no project class keys a table
435
+ # entry nothing reads, which is the same silence as not recording it at all.
436
+ def prepend_call_receiver(node, lexical_prefix)
437
+ receiver = node.receiver
438
+ return nil if receiver.is_a?(Prism::SelfNode)
439
+
440
+ rendered = Source::ConstantPath.qualified_name_or_nil(receiver)
441
+ return nil if rendered.nil?
442
+ return rendered if Source::ConstantPath.rooted?(receiver) || lexical_prefix.empty?
443
+
444
+ DeclarationWalk::Rules.eval_constant_receiver_prefix(node, receiver, rendered, lexical_prefix).join("::")
445
+ end
446
+
447
+ # The owner a meta-new write gives the block for the MIXIN tables — every definite
448
+ # write form, including `||=` and path-or-write: an `||=` block still runs on the
449
+ # class the write names when it runs, so `K ||= Class.new { include I }` mixes I
450
+ # into K exactly the way its `def`s land `K#m`. The census keeps the narrower
451
+ # {ConstantTables.meta_new_block_owner} / {ConstantTables.meta_new_owner_under_cref} pair because its `||=`
452
+ # rows are unpublished (#963). Nil where the write names nothing below an
453
+ # unnameable cref.
454
+ def meta_new_mixin_owner(node, qualified_prefix, singleton_cref, self_base = nil)
455
+ return nil if singleton_cref && !DeclarationWalk::Rules.meta_new_path_target_nameable?(node, self_base)
456
+
457
+ DeclarationWalk::Rules.meta_new_child_prefix(node, qualified_prefix, self_base)&.join("::")
458
+ end
459
+
460
+ private_class_method :hook_parameter_receiver?, :taint_computed_hook_send, :computed_send_name,
461
+ :local_send_names, :collect_local_send_names, :iterated_element_names,
462
+ :local_send_name_literal, :iterated_element_source, :iterated_first_parameter?,
463
+ :iterated_receiver_elements, :constant_array_elements, :constant_write_nodes,
464
+ :constant_binding?, :iterated_element_children, :literal_send_name, :local_binding?,
465
+ :own_mixin_effects, :receiver_mixin_effects, :hook_receiver?, :block_mixes_in?,
466
+ :block_mixes_into_self?, :singleton_class_eval_receiver?, :singleton_class_receiver?
467
+ end
468
+ end
469
+ end
470
+ end