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
@@ -5,6 +5,8 @@ require "prism"
5
5
  require_relative "../../reflection"
6
6
  require_relative "../../source/constant_path"
7
7
  require_relative "../../source/node_children"
8
+ require_relative "../diagnostic"
9
+ require_relative "rule_ids"
8
10
 
9
11
  module Rigor
10
12
  module Analysis
@@ -75,6 +77,38 @@ module Rigor
75
77
  @results.freeze
76
78
  end
77
79
 
80
+ # ADR-116 WD1 — the message builder for this collector's results, moved here from `CheckRules`.
81
+ # {.diagnostics} is called by `CheckRules.diagnose` through its collector table; the rest is private to it.
82
+
83
+ # `flow.shadowed-rescue-clause` — one diagnostic per rescue clause every earlier-comparable class of
84
+ # which is already caught by an earlier clause of the same chain (see {ShadowedRescueCollector} for
85
+ # the ancestry-certainty envelope).
86
+ def self.diagnostics(path, results)
87
+ results.map do |result|
88
+ build_shadowed_rescue_diagnostic(path, result)
89
+ end
90
+ end
91
+
92
+ def self.build_shadowed_rescue_diagnostic(path, result)
93
+ Diagnostic.from_node(
94
+ result.clause,
95
+ rule: RULE_SHADOWED_RESCUE_CLAUSE,
96
+ path: path,
97
+ message: shadowed_rescue_message(result),
98
+ severity: :warning
99
+ )
100
+ end
101
+
102
+ def self.shadowed_rescue_message(result)
103
+ earlier = result.earlier_sources.each_with_index.map do |source, index|
104
+ "`#{source}' (line #{result.earlier_lines[index]})"
105
+ end
106
+ "shadowed `#{result.clause_source}': every exception class it names is already caught " \
107
+ "by the earlier #{earlier.join(' and ')} clause#{'s' if earlier.size > 1}, so this clause can never run"
108
+ end
109
+
110
+ private_class_method :build_shadowed_rescue_diagnostic, :shadowed_rescue_message
111
+
78
112
  private
79
113
 
80
114
  def walk(node, prefix)
@@ -32,7 +32,7 @@ module Rigor
32
32
  #
33
33
  # Deliberately NOT in scope: a constructor reached through `Class#new` (nothing records `initialize` as
34
34
  # `[:singleton, :new]`, so `.new` finds no owner unless the class writes `def self.new` itself); keyword
35
- # arity (the envelope's required-keyword flag declines, as `compute_arity_envelope` declines an RBS
35
+ # arity (the envelope's required-keyword flag declines, as `WrongArity.compute_arity_envelope` declines an RBS
36
36
  # function with required keywords); and a `class_eval` whose receiver is not a constant, which the
37
37
  # discovery walk cannot name.
38
38
  class SourceArity # rubocop:disable Metrics/ClassLength
@@ -329,7 +329,7 @@ module Rigor
329
329
 
330
330
  # Issue #1797 — a class whose body rewrites its method table in a way no literal names. A `refine` of the
331
331
  # class (`ENVELOPE_REFINED_MARK`) is not one here: whether a refinement answers a call is decided per call
332
- # site, name and `using` by `CheckRules#refined_redefinition_in_effect?`, which also declines where a
332
+ # site, name and `using` by `ReceiverPredicates.refined_redefinition_in_effect?`, which also declines where a
333
333
  # refinement of a subclass of the receiver's class may answer. Counting the mark silenced every name on
334
334
  # every subclass of a refined class, with or without a `using`.
335
335
  def dynamic_surface?(class_name)
@@ -0,0 +1,256 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+ require_relative "../../reflection"
5
+ require_relative "../diagnostic"
6
+ require_relative "special_global_setters"
7
+
8
+ module Rigor
9
+ module Analysis
10
+ module CheckRules
11
+ # ADR-116 WD1 — the `global.*` rules for writes to Ruby's special globals (`$stdout`, `$,`, `$/`, `$;`
12
+ # …): a write to a read-only global and a write whose literal value the setter's contract rejects. Owns
13
+ # the census of what the program may answer to the setter's protocol.
14
+ #
15
+ # The one entry point is {.diagnostics}, called by `CheckRules.main_pass_node_diagnostics`. It reads
16
+ # {ReceiverPredicates}, {SpecialGlobalSetters} and `Inference::GlobalWriteCensus`, and calls nothing
17
+ # else in `CheckRules`.
18
+ module SpecialGlobalWrite
19
+ module_function
20
+
21
+ # Issue #1367 (ADR-117 Decision point 1, WD2) — `global.write-type-mismatch` and `global.readonly-write`: a
22
+ # write to a special global that the interpreter's setter rejects, so the write raises every time it runs.
23
+ # The envelope is {SpecialGlobalSetters}: the setter, not the global's RBS declaration.
24
+ #
25
+ # - `global.readonly-write` needs no type. `$g = v`, `$g op= v` and a target of a multiple assignment
26
+ # (`$g, x = …`, `*$g`) always write, so a write to a read-only special raises.
27
+ # - `global.write-type-mismatch` checks `$g = v` only when `v` is a literal node
28
+ # ({SpecialGlobalSetters.literal_class}), whose class the syntax fixes. Any other value is never reported,
29
+ # whatever its inferred type: a class RBS declares without `write` or `to_str` does not prove the object
30
+ # lacks it (a singleton `def`, an `extend`, or a run-time `alias to_str to_s`, as URI has).
31
+ # - `$g ||= v` and `$g &&= v` write only when the current value is falsy (truthy), so neither rule checks
32
+ # them, nor a `for` index or a `rescue => $g` reference.
33
+ # - A special that any project file aliases (`alias $stdout $out`, either side) is exempt from both.
34
+ def diagnostics(path, node, scope_index, lexical_sites)
35
+ case node
36
+ when Prism::GlobalVariableWriteNode
37
+ [readonly_global_write_diagnostic(path, node, scope_index) ||
38
+ global_write_type_diagnostic(path, node, scope_index, lexical_sites)].compact
39
+ when Prism::GlobalVariableOperatorWriteNode
40
+ [readonly_global_write_diagnostic(path, node, scope_index)].compact
41
+ when Prism::MultiWriteNode
42
+ multi_write_global_targets(node).filter_map do |target|
43
+ readonly_global_write_diagnostic(path, target, scope_index)
44
+ end
45
+ else
46
+ []
47
+ end
48
+ end
49
+
50
+ # The `GlobalVariableTargetNode`s a multiple assignment writes, through a splat (`*$g`) and nested
51
+ # destructuring (`(a, $g), b = …`).
52
+ def multi_write_global_targets(node)
53
+ [*node.lefts, node.rest, *node.rights].flat_map do |target|
54
+ case target
55
+ when Prism::GlobalVariableTargetNode then [target]
56
+ when Prism::SplatNode
57
+ target.expression.is_a?(Prism::GlobalVariableTargetNode) ? [target.expression] : []
58
+ when Prism::MultiTargetNode then multi_write_global_targets(target)
59
+ else []
60
+ end
61
+ end
62
+ end
63
+
64
+ def readonly_global_write_diagnostic(path, node, scope_index)
65
+ return nil unless SpecialGlobalSetters.read_only?(node.name)
66
+
67
+ scope = scope_index[node]
68
+ return nil if scope.nil? || global_aliased?(scope, node.name)
69
+
70
+ location = node.respond_to?(:name_loc) ? node.name_loc : node.location
71
+ Diagnostic.from_location(
72
+ location,
73
+ rule: RULE_GLOBAL_READONLY_WRITE,
74
+ path: path,
75
+ message: "`#{node.name}' is a read-only variable; this write raises NameError at runtime",
76
+ severity: :error
77
+ )
78
+ end
79
+
80
+ def global_write_type_diagnostic(path, node, scope_index, lexical_sites)
81
+ contract = SpecialGlobalSetters.contract_for(node.name)
82
+ return nil if contract.nil?
83
+
84
+ class_name = SpecialGlobalSetters.literal_class(node.value)
85
+ return nil if class_name.nil?
86
+
87
+ scope = scope_index[node]
88
+ return nil if scope.nil?
89
+ return nil unless literal_rejected?(class_name, contract, node, scope, lexical_sites)
90
+ return nil if global_aliased?(scope, node.name)
91
+
92
+ build_global_write_type_diagnostic(path, node, contract, class_name)
93
+ end
94
+
95
+ # The project's {Inference::GlobalWriteCensus} with the `pre_eval:` files' own.
96
+ def global_write_census(scope)
97
+ census = scope.discovered_global_write_census
98
+ pre_eval = scope.environment&.project_patched_methods&.write_census
99
+ pre_eval.nil? || pre_eval.empty? ? census : census | pre_eval
100
+ end
101
+
102
+ # Whether any project or `pre_eval:` file aliases `name`, on either side, which can make it another variable.
103
+ def global_aliased?(scope, name)
104
+ record_global_census(Inference::GlobalWriteCensus.alias_entry(name))
105
+ Inference::GlobalWriteCensus.aliased?(global_write_census(scope), name)
106
+ end
107
+
108
+ # Issue #2158 — a read of the whole-program census records the `global_census:` key of each entry it asks
109
+ # about ({Inference::GlobalWriteCensus.dependency_key}): a file that starts or stops writing the entry (a `def
110
+ # write`, an `alias $stdout $x`, a computed `define_method`) re-checks the write. A `pre_eval:` file moves the
111
+ # snapshot's own fingerprint.
112
+ def record_global_census(*entries)
113
+ return unless DependencyRecorder.active?
114
+
115
+ entries.each do |entry|
116
+ DependencyRecorder.read_name(:global_census, Inference::GlobalWriteCensus.dependency_key(entry))
117
+ end
118
+ end
119
+
120
+ # Whether the census holds any of `entries`, recording only what can flip the answer: the entries present
121
+ # when it holds one (the answer stays true until every one of them vanishes, and another appearing changes
122
+ # nothing), every entry when it holds none.
123
+ #
124
+ # This rests on an invariant: every census entry can only move a write toward being silenced, and a reported
125
+ # write is always re-checked through `IncrementalSession#global_write_reporters`, so recording only the
126
+ # present entries on a yes is sound. A future census entry that makes a write reported would break it, and
127
+ # its reader would have to record every entry. Recording every entry on a no is defence in depth, since the
128
+ # write that answer leaves reported re-checks through `global_write_reporters` anyway.
129
+ def census_holds_any?(census, entries)
130
+ present = entries.select { |entry| census.include?(entry) }
131
+ record_global_census(*(present.empty? ? entries : present))
132
+ !present.empty?
133
+ end
134
+
135
+ # A literal is rejected when its class is none the setter takes and, for a setter that also converts
136
+ # (`to_str`, `to_int`) or asks for `write`, the literal's object cannot answer that method.
137
+ def literal_rejected?(class_name, contract, node, scope, lexical_sites)
138
+ return false if contract.accepts_class?(class_name)
139
+ return true if contract.conversion.nil?
140
+
141
+ !literal_may_answer?(class_name, contract.conversion, node, scope, lexical_sites)
142
+ end
143
+
144
+ # The escape hatches through which an object answers a method its class does not define: the setter's
145
+ # `respond_to?` and implicit conversion both consult them.
146
+ GLOBAL_WRITE_HATCHES = %i[method_missing respond_to_missing? respond_to?].freeze
147
+ private_constant :GLOBAL_WRITE_HATCHES
148
+
149
+ # Where RBS core declares the hatches every object inherits unchanged.
150
+ DEFAULT_HATCH_OWNERS = %w[::BasicObject ::Kernel ::Object].freeze
151
+ private_constant :DEFAULT_HATCH_OWNERS
152
+
153
+ # Whether the literal's object may answer `method_name` after all:
154
+ #
155
+ # - RBS gives its class the method, or a hatch of its own (a project `sig/` reopening or `include` counts);
156
+ # - the program defines the method or a hatch anywhere, in any spelling, on any receiver
157
+ # ({Inference::GlobalWriteCensus}): no census of where a definition lands can be complete
158
+ # (`class << nil`, `K = Integer; class K`, `[Integer].each { |k| k.define_method(:write) }`);
159
+ # - an ancestor's surface is rewritten from outside (`Integer.include(M)`, `Object.include(M)`, a string
160
+ # `class_eval`: the `ENVELOPE_DYNAMIC_MARK`), or it or `Object` mixes in a module RBS does not rule out;
161
+ # - for `write` only, a refinement that may add it is in effect at the write ({#refined_writer_in_effect?}).
162
+ def literal_may_answer?(class_name, method_name, node, scope, lexical_sites)
163
+ definition = Rigor::Reflection.instance_definition(class_name, scope: scope)
164
+ return true if definition.nil?
165
+ return true if definition.methods.key?(method_name) || overridden_hatch?(definition)
166
+
167
+ ancestors = definition.ancestors.ancestors.to_set { |ancestor| ancestor.name.to_s.delete_prefix("::") }
168
+ names = [method_name, *GLOBAL_WRITE_HATCHES]
169
+ census = global_write_census(scope)
170
+ program_may_answer?(ancestors, names, census, scope) ||
171
+ (method_name == :write && refined_writer_in_effect?(census, node, scope, lexical_sites))
172
+ end
173
+
174
+ def overridden_hatch?(definition)
175
+ GLOBAL_WRITE_HATCHES.any? do |name|
176
+ method = definition.methods[name]
177
+ method && !DEFAULT_HATCH_OWNERS.include?(method.defined_in.to_s)
178
+ end
179
+ end
180
+
181
+ def program_may_answer?(ancestors, names, census, scope)
182
+ defines = [Inference::GlobalWriteCensus::DEFINES_ANY, *names.map { |name| [:defines, name] }]
183
+ return true if census_holds_any?(census, defines)
184
+
185
+ dynamic_mark = Scope::DiscoveryIndex::ENVELOPE_DYNAMIC_MARK
186
+ return true if ancestors.any? { |owner| scope.parameter_envelopes_of(owner).key?(dynamic_mark) }
187
+
188
+ # One key for every top-level mixin, a deliberate trade-off: the reader asks about each, so a mixin
189
+ # appearing or vanishing anywhere re-checks every write that reached this step.
190
+ record_global_census(%i[main_mixin *])
191
+ main_mixins = Inference::GlobalWriteCensus.main_mixins(census)
192
+ main_mixins.any? { |mixin| rbs_mixin_may_answer?(mixin, names, scope) } ||
193
+ mixin_may_answer?(ancestors, names, scope)
194
+ end
195
+
196
+ # A module an ancestor of the literal's class mixes in through a class body (`class Integer; include
197
+ # Writable; end`), followed through the modules it mixes in in turn. A project module answers when its
198
+ # surface is rewritten beyond literal names (a `send(:include, …)`, a non-constant mixin); its literal
199
+ # definitions are the census's. Any other module answers when RBS does not know it or declares one of
200
+ # `names`.
201
+ def mixin_may_answer?(ancestors, names, scope)
202
+ queue = ancestors.flat_map { |owner| scope.includes_of(owner) }
203
+ seen = Set.new
204
+ until queue.empty?
205
+ mixin = queue.shift
206
+ next unless seen.add?(mixin)
207
+
208
+ bucket = scope.parameter_envelopes_of(mixin)
209
+ if bucket.key?(Scope::DiscoveryIndex::ENVELOPE_MODULE_MARK)
210
+ return true if bucket.key?(Scope::DiscoveryIndex::ENVELOPE_DYNAMIC_MARK)
211
+
212
+ queue.concat(scope.includes_of(mixin))
213
+ elsif rbs_mixin_may_answer?(mixin, names, scope)
214
+ return true
215
+ end
216
+ end
217
+ false
218
+ end
219
+
220
+ def rbs_mixin_may_answer?(mixin, names, scope)
221
+ definition = Rigor::Reflection.instance_definition(mixin, scope: scope)
222
+ definition.nil? || definition.methods.key?(names.first) || overridden_hatch?(definition)
223
+ end
224
+
225
+ # For `write` only: some refinement may add `write` (a `def`, `define_method` or `alias_method` in a `refine`
226
+ # block, or an `import_methods`), and a `using` is in effect at the write. Which class it refines is not asked:
227
+ # the target may be computed (`[Array].each { |k| refine(k) { … } }`) or a constant alias (`T = Array;
228
+ # refine(T)`), so a refined `write` on any class declines every literal. `respond_to?(:write)` honours an
229
+ # active refinement on Ruby 4.0.5; a refined `respond_to_missing?` or `respond_to?`, and every implicit
230
+ # conversion, do not, so a refinement that adds only those leaves the write reported.
231
+ def refined_writer_in_effect?(census, node, scope, lexical_sites)
232
+ census_holds_any?(census, [Inference::GlobalWriteCensus::REFINES_ANY, %i[refines write]]) &&
233
+ (lexical_sites.nil? || !scope.declared_refinements.empty? ||
234
+ lexical_sites.refinements.any_at?(node.location.start_offset))
235
+ end
236
+
237
+ def build_global_write_type_diagnostic(path, node, contract, class_name)
238
+ Diagnostic.from_name_loc(
239
+ node,
240
+ rule: RULE_GLOBAL_WRITE_TYPE_MISMATCH,
241
+ path: path,
242
+ message: "`#{node.name}' accepts only #{contract.accepts}; this write assigns " \
243
+ "#{SpecialGlobalSetters.literal_description(class_name)}, which raises TypeError at runtime",
244
+ severity: :error
245
+ )
246
+ end
247
+
248
+ private_class_method :multi_write_global_targets, :readonly_global_write_diagnostic,
249
+ :global_write_type_diagnostic, :global_write_census, :global_aliased?,
250
+ :record_global_census, :census_holds_any?, :literal_rejected?, :literal_may_answer?,
251
+ :overridden_hatch?, :program_may_answer?, :mixin_may_answer?, :rbs_mixin_may_answer?,
252
+ :refined_writer_in_effect?, :build_global_write_type_diagnostic
253
+ end
254
+ end
255
+ end
256
+ end
@@ -0,0 +1,275 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../diagnostic"
4
+ require_relative "rule_ids"
5
+
6
+ module Rigor
7
+ module Analysis
8
+ module CheckRules
9
+ # ADR-116 WD1 — the `# rigor:disable` / `# rigor:disable-file` markers: reading them out of a file's comments,
10
+ # dropping the diagnostics they cover, and the `suppression.*` diagnostics about a malformed marker.
11
+ #
12
+ # Entry points: {.filter_suppressed} (built-in rows), {.filter_plugin_suppressed} (plugin rows) and
13
+ # {.suppression_marker_diagnostics}. The token vocabulary (`resolve_rule_token`, `expand_rule_tokens`,
14
+ # `config_disabled?`, `plugin_row_disabled?`) stays in `rule_ids.rb`, which {SeverityStamp} reads without the
15
+ # engine; this module calls into it and nothing else in `CheckRules`.
16
+ module Suppression
17
+ module_function
18
+
19
+ # v0.0.2 #6 — diagnostic suppression. Three kinds of
20
+ # suppression compose:
21
+ #
22
+ # - **Project-level**: `disabled_rules` is the
23
+ # project's `.rigor.yml` `disable:` list. Any
24
+ # diagnostic whose `rule` is in the list is dropped.
25
+ # - **File-level** (v0.1.2): `# rigor:disable-file <rule1>,
26
+ # <rule2>` anywhere in the file suppresses the matching
27
+ # diagnostic for every line. `# rigor:disable-file all`
28
+ # suppresses every built-in rule across the file. Convention is
29
+ # to put the comment near the top, but Rigor accepts it
30
+ # anywhere — the comment scope is "this file" regardless
31
+ # of position.
32
+ # - **In-source line**: `# rigor:disable <rule1>, <rule2>`
33
+ # on the same line as the offending expression
34
+ # suppresses the matching diagnostic for that line
35
+ # only. `# rigor:disable all` on a line suppresses
36
+ # every built-in rule on that line. A plugin's rows are
37
+ # not passed here; see {.filter_plugin_suppressed}.
38
+ #
39
+ # Both in-source forms are recognised only when the
40
+ # marker is the FIRST thing in the comment, so a
41
+ # directive quoted inside ordinary prose — as the bullets
42
+ # above quote it — is not a directive. See the pattern
43
+ # constants below.
44
+ #
45
+ # Diagnostics with `rule == nil` (parse errors, path
46
+ # errors, internal analyzer errors) are NEVER
47
+ # suppressed — they represent failures the user cannot
48
+ # silence away.
49
+ def filter_suppressed(diagnostics, comments:, disabled_rules:)
50
+ line_suppressions, file_suppressions = parse_suppression_comments(comments)
51
+ disabled = CheckRules.expand_rule_tokens(disabled_rules)
52
+
53
+ diagnostics.reject do |diagnostic|
54
+ rule = diagnostic.rule
55
+ next false if rule.nil?
56
+ next true if CheckRules.config_disabled?(rule, disabled)
57
+ next true if file_suppressions.include?("all") || file_suppressions.include?(rule)
58
+
59
+ line_rules = line_suppressions[diagnostic.line]
60
+ line_rules && (line_rules.include?("all") || line_rules.include?(rule))
61
+ end
62
+ end
63
+
64
+ # Issue #1953 — the in-source markers over the rows a plugin emitted while `path` was analysed
65
+ # (`#diagnostics_for_file` and node rules). A row answers to a `plugin.<id>.<rule>` or `plugin.<id>` token
66
+ # only ({.plugin_row_disabled?}); a bare id and `all` keep reaching built-in rows alone. `comments` are
67
+ # `path`'s, so a row the plugin positioned in another file is left to the `disable:` list and the baseline.
68
+ # The `disable:` list itself is applied to these rows by {SeverityStamp}, on every run shape.
69
+ def filter_plugin_suppressed(diagnostics, path:, comments:)
70
+ return diagnostics if diagnostics.empty?
71
+
72
+ line_suppressions, file_suppressions = parse_suppression_comments(comments)
73
+ return diagnostics if line_suppressions.empty? && file_suppressions.empty?
74
+
75
+ diagnostics.reject do |diagnostic|
76
+ next false unless diagnostic.path == path
77
+ next true if CheckRules.plugin_row_disabled?(diagnostic, file_suppressions)
78
+
79
+ line_rules = line_suppressions.fetch(diagnostic.line, nil)
80
+ !line_rules.nil? && CheckRules.plugin_row_disabled?(diagnostic, line_rules)
81
+ end
82
+ end
83
+
84
+ # Every suppression-recognition pattern below is anchored with `\A` against the COMMENT SLICE —
85
+ # Prism hands us a slice that starts at the `#`, so `\A#` means "the marker is the first thing in
86
+ # the comment". A real directive always has that shape, in both the whole-line form and the
87
+ # trailing `code # rigor:disable <rule>` form; prose that merely quotes a directive
88
+ # (`... like `# rigor:disable-file all` ...`) has text before the inner `#` and no longer
89
+ # activates anything. Two consequences worth naming: a doc-tool `##` comment never activates (the
90
+ # second `#` is neither whitespace nor the marker word), and an `=begin`/`=end` block comment
91
+ # never activates either (its slice starts at `=begin`). Unanchored, these patterns silently
92
+ # file-suppressed this very file — see issue #306.
93
+ LINE_SUPPRESSION_PATTERN = /\A#\s*rigor:disable(?!-file)\s+(?<rules>[\w.,\s-]+)/
94
+ private_constant :LINE_SUPPRESSION_PATTERN
95
+
96
+ FILE_SUPPRESSION_PATTERN = /\A#\s*rigor:disable-file\s+(?<rules>[\w.,\s-]+)/
97
+ private_constant :FILE_SUPPRESSION_PATTERN
98
+
99
+ # A `rigor:disable[-file]` marker word regardless of whether any rule tokens follow. Used only by the
100
+ # `suppression.empty` detection — the two suppression patterns above require at least one token
101
+ # character, so a bare `# rigor:disable` never reaches them. The lookahead keeps
102
+ # `rigor:disable-something-else` from counting as a marker.
103
+ BARE_SUPPRESSION_MARKER = /\A#\s*rigor:disable(?<file>-file)?(?![\w-])(?<rest>.*)/
104
+ private_constant :BARE_SUPPRESSION_MARKER
105
+
106
+ # A `rigor:` marker word that is NOT part of Rigor's suppression grammar but reads like an attempted
107
+ # suppression — the RuboCop-reflex spellings `rigor:disable-next-line <rules>` and
108
+ # `rigor:enable <rules>`. These are invisible to both suppression patterns above (the hyphenated
109
+ # suffix fails LINE_SUPPRESSION_PATTERN's `\s+` and BARE_SUPPRESSION_MARKER's lookahead), so without
110
+ # surveillance they silently suppress nothing. Matches `disable-<suffix>` for any suffix other than
111
+ # `file`, and `enable` with or without a suffix.
112
+ UNKNOWN_SUPPRESSION_MARKER =
113
+ /\A#\s*rigor:(?<marker>disable-(?!file(?![\w-]))[\w-]+|enable(?:-[\w-]+)?)(?![\w-])(?<rest>.*)/
114
+ private_constant :UNKNOWN_SUPPRESSION_MARKER
115
+
116
+ # @return pair of
117
+ # `(line_suppressions, file_suppressions)`. Line
118
+ # suppressions are keyed by source line number; file
119
+ # suppressions apply to every line.
120
+ def parse_suppression_comments(comments)
121
+ line_suppressions = Hash.new { |h, k| h[k] = Set.new }
122
+ file_suppressions = Set.new
123
+ comments.each do |comment|
124
+ source = comment_text(comment)
125
+ if (match = FILE_SUPPRESSION_PATTERN.match(source))
126
+ absorb_suppression_tokens(match[:rules], file_suppressions)
127
+ elsif (match = LINE_SUPPRESSION_PATTERN.match(source))
128
+ absorb_suppression_tokens(match[:rules], line_suppressions[comment.location.start_line])
129
+ end
130
+ end
131
+ [line_suppressions, file_suppressions]
132
+ end
133
+
134
+ # The comment's text for the suppression regexps. A comment is not validated by Ruby, so it may hold a
135
+ # byte that is invalid in the file's encoding, and a regexp raises on such a string; the marker grammar
136
+ # is ASCII, so scrubbing the bad bytes loses nothing. Positions come from `comment.location`, which is
137
+ # byte-offset based and untouched.
138
+ def comment_text(comment)
139
+ text = comment.location.slice
140
+ text.valid_encoding? ? text : text.scrub("?")
141
+ end
142
+
143
+ def absorb_suppression_tokens(raw, target)
144
+ raw.to_s.split(/[\s,]+/).reject(&:empty?).each do |token|
145
+ target.merge(CheckRules.expand_token(token))
146
+ end
147
+ end
148
+
149
+ # PHPStan-`IgnoreParseErrorRule`-modelled surveillance over the suppression markers themselves: a
150
+ # malformed or ineffective `# rigor:disable` / `# rigor:disable-file` comment must not silently no-op
151
+ # (the typo'd `# rigor:disable call.undefined-metod` suppresses nothing and the user never learns).
152
+ # Emits `suppression.unknown-rule` for every marker token that resolves to no known identifier, and
153
+ # `suppression.empty` for a marker that lists no rules at all. The MATCHING semantics are deliberately
154
+ # unchanged — an unknown token is still kept verbatim per the diagnostic-policy spec — so this is
155
+ # additive surveillance only. Both diagnostics run before {.filter_suppressed} and flow through it,
156
+ # so they are themselves suppressible (`# rigor:disable suppression.unknown-rule`) with no regress:
157
+ # that token is known, so acknowledging it never re-fires the rule.
158
+ def suppression_marker_diagnostics(path, comments)
159
+ comments.each_with_object([]) do |comment, diagnostics|
160
+ source = comment_text(comment)
161
+ if (match = FILE_SUPPRESSION_PATTERN.match(source))
162
+ validate_suppression_tokens(match[:rules], "rigor:disable-file", path, comment, diagnostics)
163
+ elsif (match = LINE_SUPPRESSION_PATTERN.match(source))
164
+ validate_suppression_tokens(match[:rules], "rigor:disable", path, comment, diagnostics)
165
+ else
166
+ diagnose_bare_suppression_marker(path, comment, source, diagnostics)
167
+ end
168
+ end
169
+ end
170
+
171
+ def validate_suppression_tokens(raw, marker, path, comment, diagnostics)
172
+ tokens = raw.to_s.split(/[\s,]+/).reject(&:empty?)
173
+ if tokens.empty?
174
+ diagnostics << empty_suppression_diagnostic(path, comment, marker)
175
+ return
176
+ end
177
+
178
+ tokens.each do |token|
179
+ next if known_suppression_token?(token)
180
+
181
+ diagnostics << unknown_suppression_rule_diagnostic(path, comment, marker, token)
182
+ end
183
+ end
184
+
185
+ # A comment carrying the marker word but not the token-bearing suppression grammar. A remainder of
186
+ # nothing but whitespace / commas is a genuinely empty marker (`# rigor:disable`); anything else is
187
+ # left alone as an ordinary comment, matching the parse path, which never treats it as a
188
+ # suppression either. Prose that merely quotes a marker is already excluded a step earlier by the
189
+ # `\A` anchor, since the quotation is not at the start of the comment.
190
+ def diagnose_bare_suppression_marker(path, comment, source, diagnostics)
191
+ bare = BARE_SUPPRESSION_MARKER.match(source)
192
+ if bare
193
+ return unless bare[:rest].match?(/\A[\s,]*\z/)
194
+
195
+ marker = bare[:file] ? "rigor:disable-file" : "rigor:disable"
196
+ diagnostics << empty_suppression_diagnostic(path, comment, marker)
197
+ return
198
+ end
199
+
200
+ diagnose_unknown_suppression_marker(path, comment, source, diagnostics)
201
+ end
202
+
203
+ # `# rigor:disable-next-line <rule>` / `# rigor:enable <rule>` — a marker word Rigor's grammar does
204
+ # not recognise but that reads as an attempted suppression (the RuboCop reflex). Fires only when the
205
+ # marker opens the comment and the remainder is empty or looks like a rule list, so prose mentioning
206
+ # the spelling in backticks stays an ordinary comment — the same escape the empty-marker detection
207
+ # observes.
208
+ def diagnose_unknown_suppression_marker(path, comment, source, diagnostics)
209
+ unknown = UNKNOWN_SUPPRESSION_MARKER.match(source)
210
+ return if unknown.nil?
211
+
212
+ rest = unknown[:rest]
213
+ return unless rest.match?(/\A[\s,]*\z/) || rest.match?(/\A\s+[\w.,\s-]+\z/)
214
+
215
+ diagnostics << unknown_suppression_marker_diagnostic(path, comment, unknown[:marker])
216
+ end
217
+
218
+ # True when a suppression token resolves to a diagnostic identifier some producer can emit: a
219
+ # canonical CheckRules id, a legacy alias, the `all` wildcard, a family wildcard, a bare
220
+ # non-catalogue engine id, or a dotted id under a known non-check family (`rbs_extended.*`,
221
+ # `dynamic.*`, `rbs.*`, `pre-eval.*`, and any `plugin.`-prefixed id — plugins load dynamically, so
222
+ # their rule vocabulary cannot be enumerated here and under-warning is the FP-safe direction).
223
+ def known_suppression_token?(token)
224
+ return true if %w[all pre-eval].include?(token)
225
+ return true if ALL_RULES.include?(token) || LEGACY_RULE_ALIASES.key?(token) ||
226
+ RULE_FAMILIES.include?(token) || NON_CHECK_DIAGNOSTIC_IDS.include?(token)
227
+
228
+ family, rest = token.split(".", 2)
229
+ !rest.nil? && NON_CHECK_DIAGNOSTIC_FAMILIES.include?(family)
230
+ end
231
+
232
+ def unknown_suppression_rule_diagnostic(path, comment, marker, token)
233
+ Diagnostic.new(
234
+ path: path,
235
+ line: comment.location.start_line,
236
+ column: comment.location.start_column + 1,
237
+ message: "unknown rule `#{token}` in `# #{marker}` — the token matches no known rule, alias, " \
238
+ "or family, so this suppression has no effect. Likely a typo; `rigor explain <rule>` " \
239
+ "lists the canonical ids.",
240
+ severity: :warning,
241
+ rule: RULE_SUPPRESSION_UNKNOWN_RULE,
242
+ source_family: :builtin
243
+ )
244
+ end
245
+
246
+ def unknown_suppression_marker_diagnostic(path, comment, marker)
247
+ Diagnostic.new(
248
+ path: path,
249
+ line: comment.location.start_line,
250
+ column: comment.location.start_column + 1,
251
+ message: "unrecognised suppression marker `rigor:#{marker}` — Rigor's markers are " \
252
+ "`# rigor:disable <rules>` (suppresses on its own line) and " \
253
+ "`# rigor:disable-file <rules>`, so this comment suppresses nothing.",
254
+ severity: :warning,
255
+ rule: RULE_SUPPRESSION_UNKNOWN_MARKER,
256
+ source_family: :builtin
257
+ )
258
+ end
259
+
260
+ def empty_suppression_diagnostic(path, comment, marker)
261
+ Diagnostic.new(
262
+ path: path,
263
+ line: comment.location.start_line,
264
+ column: comment.location.start_column + 1,
265
+ message: "`# #{marker}` lists no rules, so this suppression has no effect. Name the rules to " \
266
+ "suppress (`# #{marker} call.undefined-method`) or use `# #{marker} all`.",
267
+ severity: :warning,
268
+ rule: RULE_SUPPRESSION_EMPTY,
269
+ source_family: :builtin
270
+ )
271
+ end
272
+ end
273
+ end
274
+ end
275
+ end