rigortype 0.1.4 → 0.1.6

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 (107) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +69 -56
  3. data/lib/rigor/analysis/buffer_binding.rb +36 -0
  4. data/lib/rigor/analysis/check_rules.rb +11 -1
  5. data/lib/rigor/analysis/dependency_source_inference/index.rb +14 -1
  6. data/lib/rigor/analysis/dependency_source_inference/return_type_heuristic.rb +105 -0
  7. data/lib/rigor/analysis/dependency_source_inference/walker.rb +32 -12
  8. data/lib/rigor/analysis/fact_store.rb +15 -3
  9. data/lib/rigor/analysis/project_scan.rb +39 -0
  10. data/lib/rigor/analysis/result.rb +11 -3
  11. data/lib/rigor/analysis/run_stats.rb +193 -0
  12. data/lib/rigor/analysis/runner.rb +681 -19
  13. data/lib/rigor/analysis/worker_session.rb +339 -0
  14. data/lib/rigor/builtins/hkt_builtins.rb +342 -0
  15. data/lib/rigor/builtins/imported_refinements.rb +6 -2
  16. data/lib/rigor/builtins/regex_refinement.rb +17 -12
  17. data/lib/rigor/builtins/static_return_refinements.rb +120 -0
  18. data/lib/rigor/cache/rbs_descriptor.rb +3 -1
  19. data/lib/rigor/cache/store.rb +72 -9
  20. data/lib/rigor/cli/lsp_command.rb +129 -0
  21. data/lib/rigor/cli/type_of_command.rb +44 -5
  22. data/lib/rigor/cli.rb +122 -10
  23. data/lib/rigor/configuration.rb +168 -7
  24. data/lib/rigor/environment/bundle_sig_discovery.rb +198 -0
  25. data/lib/rigor/environment/class_registry.rb +12 -3
  26. data/lib/rigor/environment/hkt_registry_holder.rb +33 -0
  27. data/lib/rigor/environment/lockfile_resolver.rb +125 -0
  28. data/lib/rigor/environment/rbs_collection_discovery.rb +126 -0
  29. data/lib/rigor/environment/rbs_coverage_report.rb +112 -0
  30. data/lib/rigor/environment/rbs_loader.rb +238 -7
  31. data/lib/rigor/environment/reflection.rb +152 -0
  32. data/lib/rigor/environment/reporters.rb +40 -0
  33. data/lib/rigor/environment.rb +179 -10
  34. data/lib/rigor/inference/acceptance.rb +83 -4
  35. data/lib/rigor/inference/builtins/method_catalog.rb +12 -5
  36. data/lib/rigor/inference/builtins/numeric_catalog.rb +15 -4
  37. data/lib/rigor/inference/expression_typer.rb +59 -2
  38. data/lib/rigor/inference/hkt_body.rb +171 -0
  39. data/lib/rigor/inference/hkt_body_parser.rb +363 -0
  40. data/lib/rigor/inference/hkt_reducer.rb +256 -0
  41. data/lib/rigor/inference/hkt_registry.rb +223 -0
  42. data/lib/rigor/inference/macro_block_self_type.rb +96 -0
  43. data/lib/rigor/inference/method_dispatcher/constant_folding.rb +29 -29
  44. data/lib/rigor/inference/method_dispatcher/kernel_dispatch.rb +4 -4
  45. data/lib/rigor/inference/method_dispatcher/method_folding.rb +18 -1
  46. data/lib/rigor/inference/method_dispatcher/overload_selector.rb +126 -31
  47. data/lib/rigor/inference/method_dispatcher/receiver_affinity.rb +87 -0
  48. data/lib/rigor/inference/method_dispatcher/shape_dispatch.rb +46 -40
  49. data/lib/rigor/inference/method_dispatcher.rb +282 -6
  50. data/lib/rigor/inference/method_parameter_binder.rb +21 -11
  51. data/lib/rigor/inference/narrowing.rb +127 -8
  52. data/lib/rigor/inference/project_patched_methods.rb +70 -0
  53. data/lib/rigor/inference/project_patched_scanner.rb +210 -0
  54. data/lib/rigor/inference/scope_indexer.rb +156 -12
  55. data/lib/rigor/inference/statement_evaluator.rb +106 -6
  56. data/lib/rigor/inference/synthetic_method.rb +86 -0
  57. data/lib/rigor/inference/synthetic_method_index.rb +82 -0
  58. data/lib/rigor/inference/synthetic_method_scanner.rb +599 -0
  59. data/lib/rigor/language_server/buffer_table.rb +63 -0
  60. data/lib/rigor/language_server/completion_provider.rb +438 -0
  61. data/lib/rigor/language_server/debouncer.rb +86 -0
  62. data/lib/rigor/language_server/diagnostic_publisher.rb +167 -0
  63. data/lib/rigor/language_server/document_symbol_provider.rb +142 -0
  64. data/lib/rigor/language_server/folding_range_provider.rb +75 -0
  65. data/lib/rigor/language_server/hover_provider.rb +74 -0
  66. data/lib/rigor/language_server/hover_renderer.rb +312 -0
  67. data/lib/rigor/language_server/loop.rb +71 -0
  68. data/lib/rigor/language_server/project_context.rb +145 -0
  69. data/lib/rigor/language_server/selection_range_provider.rb +93 -0
  70. data/lib/rigor/language_server/server.rb +384 -0
  71. data/lib/rigor/language_server/signature_help_provider.rb +249 -0
  72. data/lib/rigor/language_server/synchronized_writer.rb +28 -0
  73. data/lib/rigor/language_server/uri.rb +40 -0
  74. data/lib/rigor/language_server.rb +29 -0
  75. data/lib/rigor/plugin/base.rb +63 -0
  76. data/lib/rigor/plugin/blueprint.rb +60 -0
  77. data/lib/rigor/plugin/loader.rb +3 -1
  78. data/lib/rigor/plugin/macro/block_as_method.rb +131 -0
  79. data/lib/rigor/plugin/macro/external_file.rb +143 -0
  80. data/lib/rigor/plugin/macro/heredoc_template.rb +315 -0
  81. data/lib/rigor/plugin/macro/trait_registry.rb +198 -0
  82. data/lib/rigor/plugin/macro.rb +31 -0
  83. data/lib/rigor/plugin/manifest.rb +127 -9
  84. data/lib/rigor/plugin/registry.rb +51 -2
  85. data/lib/rigor/plugin.rb +1 -0
  86. data/lib/rigor/rbs_extended/hkt_directives.rb +326 -0
  87. data/lib/rigor/rbs_extended.rb +82 -2
  88. data/lib/rigor/sig_gen/generator.rb +12 -3
  89. data/lib/rigor/trinary.rb +15 -11
  90. data/lib/rigor/type/app.rb +107 -0
  91. data/lib/rigor/type/bot.rb +6 -3
  92. data/lib/rigor/type/combinator.rb +12 -1
  93. data/lib/rigor/type/integer_range.rb +7 -7
  94. data/lib/rigor/type/refined.rb +18 -12
  95. data/lib/rigor/type/top.rb +4 -3
  96. data/lib/rigor/type.rb +1 -0
  97. data/lib/rigor/type_node/generic.rb +7 -1
  98. data/lib/rigor/type_node/identifier.rb +9 -1
  99. data/lib/rigor/type_node/string_literal.rb +4 -1
  100. data/lib/rigor/version.rb +1 -1
  101. data/sig/rigor/environment.rbs +11 -4
  102. data/sig/rigor/inference.rbs +2 -0
  103. data/sig/rigor/plugin/blueprint.rbs +7 -0
  104. data/sig/rigor/plugin/manifest.rbs +1 -1
  105. data/sig/rigor/plugin/registry.rbs +14 -1
  106. data/sig/rigor.rbs +37 -2
  107. metadata +92 -1
@@ -0,0 +1,315 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rigor
4
+ module Plugin
5
+ module Macro
6
+ # ADR-16 Tier C declaration: "the class-level DSL call
7
+ # `<receiver_constraint>.<method_name>(name_arg, ...)` emits
8
+ # synthetic methods on the calling class, with names
9
+ # interpolating the source-visible literal argument at
10
+ # `symbol_arg_position`."
11
+ #
12
+ # Textbook target — dry-struct's `attribute :name, T` and
13
+ # ActiveStorage's `has_one_attached :avatar` both have this
14
+ # shape: a class-level call enumerates a literal Symbol
15
+ # argument; the framework `class_eval`s a heredoc
16
+ # interpolating that Symbol; the emit table is fixed.
17
+ #
18
+ # ## Authoring shape
19
+ #
20
+ # manifest(
21
+ # id: "activestorage",
22
+ # version: "0.1.0",
23
+ # heredoc_templates: [
24
+ # Rigor::Plugin::Macro::HeredocTemplate.new(
25
+ # receiver_constraint: "ActiveRecord::Base",
26
+ # method_name: :has_one_attached,
27
+ # symbol_arg_position: 0,
28
+ # emit: [
29
+ # { name: "#{name}", returns: "ActiveStorage::Attached::One" },
30
+ # { name: "#{name}_attachment", returns: "ActiveStorage::Attachment" },
31
+ # { name: "#{name}_blob", returns: "ActiveStorage::Blob" }
32
+ # ],
33
+ # class_level_emit: [
34
+ # { name: "with_attached_#{name}", returns: "ActiveRecord::Relation" }
35
+ # ]
36
+ # )
37
+ # ]
38
+ # )
39
+ #
40
+ # ## Fields
41
+ #
42
+ # - `receiver_constraint` — fully-qualified class name (String).
43
+ # Synthesis fires when the call's lexical receiver class
44
+ # equals or inherits from this constraint.
45
+ # - `method_name` — Symbol naming the DSL method (e.g.
46
+ # `:has_one_attached`, `:attribute`).
47
+ # - `symbol_arg_position` — Integer (default 0) — the
48
+ # argument index whose literal Symbol value becomes the
49
+ # `name` interpolated into each emit row's `name:`
50
+ # template. Slice 2a accepts non-negative integers only.
51
+ # - `emit` — Array of `Emit` (or coerced Hash) — instance
52
+ # methods to synthesise on the calling class.
53
+ # - `class_level_emit` — same shape, but the synthesised
54
+ # methods are singleton (class-level) methods.
55
+ #
56
+ # ## Floor / ceiling per ADR-16 WD13
57
+ #
58
+ # Slice 2 ships at the **floor**: each emit row's `name:`
59
+ # is the source of truth for the synthetic method's name
60
+ # (a single `"\#{name}"` placeholder gets interpolated with
61
+ # the literal symbol argument at `symbol_arg_position`).
62
+ # The `returns:` strings are **recorded in the manifest but
63
+ # not resolved**; the engine emits synthetic methods with
64
+ # `Dynamic[T]` returns plus a
65
+ # `macro.tier_c.unresolved-return` provenance marker.
66
+ # Precise return-type resolution via ADR-13's
67
+ # `Plugin::TypeNodeResolver` is the **ceiling**, deferred
68
+ # to a later slice — the `returns:` declarations cost
69
+ # nothing to write today and unlock precision then.
70
+ #
71
+ # ## Slice 2a scope
72
+ #
73
+ # This file ships the value class only. Slice 2b wires the
74
+ # pre-pass that scans Tier C call sites + the
75
+ # `SyntheticMethodIndex` the dispatcher consults; slice 2c
76
+ # authors `examples/rigor-dry-struct/` and
77
+ # `examples/rigor-dry-types/` as the worked consumers.
78
+ class HeredocTemplate
79
+ NAME_PLACEHOLDER = "\#{name}"
80
+
81
+ attr_reader :receiver_constraint, :method_name, :symbol_arg_position, :emit, :class_level_emit
82
+
83
+ def initialize(receiver_constraint:, method_name:, symbol_arg_position: 0, emit: [], class_level_emit: [])
84
+ validate_receiver_constraint!(receiver_constraint)
85
+ validate_method_name!(method_name)
86
+ validate_symbol_arg_position!(symbol_arg_position)
87
+
88
+ @receiver_constraint = receiver_constraint.dup.freeze
89
+ @method_name = method_name.to_sym
90
+ @symbol_arg_position = symbol_arg_position
91
+ @emit = coerce_emit_list!(emit, "emit")
92
+ @class_level_emit = coerce_emit_list!(class_level_emit, "class_level_emit")
93
+ freeze
94
+ end
95
+
96
+ def to_h
97
+ {
98
+ "receiver_constraint" => receiver_constraint,
99
+ "method_name" => method_name.to_s,
100
+ "symbol_arg_position" => symbol_arg_position,
101
+ "emit" => emit.map(&:to_h),
102
+ "class_level_emit" => class_level_emit.map(&:to_h)
103
+ }
104
+ end
105
+
106
+ def ==(other)
107
+ other.is_a?(HeredocTemplate) && to_h == other.to_h
108
+ end
109
+ alias eql? ==
110
+
111
+ def hash
112
+ to_h.hash
113
+ end
114
+
115
+ # One row of an emit table: the synthetic method's
116
+ # name-template (the analyzer interpolates `\#{name}` with
117
+ # the call-site literal symbol) and its declared return
118
+ # type. The return type can be a static String (resolved
119
+ # via `Environment#nominal_for_name` per ADR-16 slice 6b)
120
+ # or a per-call-site lookup ({ReturnsFromArg}) — see
121
+ # [ADR-18](../../../../../docs/adr/18-substrate-per-call-site-return-type.md).
122
+ # When both are nil, the synthesised method's return type
123
+ # falls back to `Dynamic[Top]`.
124
+ class Emit
125
+ attr_reader :name, :returns, :returns_from_arg
126
+
127
+ def initialize(name:, returns: nil, returns_from_arg: nil)
128
+ unless name.is_a?(String) && !name.empty?
129
+ raise ArgumentError,
130
+ "Macro::HeredocTemplate::Emit#name must be a non-empty String, got #{name.inspect}"
131
+ end
132
+ unless returns.nil? || (returns.is_a?(String) && !returns.empty?)
133
+ raise ArgumentError,
134
+ "Macro::HeredocTemplate::Emit#returns must be a non-empty String or nil, got #{returns.inspect}"
135
+ end
136
+
137
+ @name = name.dup.freeze
138
+ @returns = returns.nil? ? nil : returns.dup.freeze
139
+ @returns_from_arg = ReturnsFromArg.coerce(returns_from_arg)
140
+ freeze
141
+ end
142
+
143
+ def to_h
144
+ {
145
+ "name" => name,
146
+ "returns" => returns,
147
+ "returns_from_arg" => returns_from_arg&.to_h
148
+ }.compact
149
+ end
150
+
151
+ def ==(other)
152
+ other.is_a?(Emit) && to_h == other.to_h
153
+ end
154
+ alias eql? ==
155
+
156
+ def hash
157
+ to_h.hash
158
+ end
159
+ end
160
+
161
+ # ADR-18 — per-call-site return-type DSL. Declares which
162
+ # call-site argument's source representation to look up
163
+ # in a cross-plugin fact channel for the synthesised
164
+ # method's return type.
165
+ #
166
+ # Authoring shape:
167
+ #
168
+ # returns_from_arg: {
169
+ # position: 1,
170
+ # lookup_via: { plugin_id: "dry-types", fact: :dry_type_aliases }
171
+ # }
172
+ #
173
+ # Slice 1 (this file) ships the value class + validation
174
+ # only. The scanner-side arg-position extraction +
175
+ # fact-store lookup land in slice 2 / 3.
176
+ class ReturnsFromArg
177
+ attr_reader :position, :plugin_id, :fact
178
+
179
+ # @return [ReturnsFromArg, nil] coerced value class
180
+ # for a Hash / nil / ReturnsFromArg input. Raises on
181
+ # any other shape so manifest authoring failures
182
+ # surface at construction time.
183
+ def self.coerce(value)
184
+ return nil if value.nil?
185
+ return value if value.is_a?(ReturnsFromArg)
186
+ return new_from_hash(value) if value.is_a?(Hash)
187
+
188
+ raise ArgumentError,
189
+ "Macro::HeredocTemplate::Emit#returns_from_arg must be a Hash or ReturnsFromArg, " \
190
+ "got #{value.inspect}"
191
+ end
192
+
193
+ def self.new_from_hash(hash)
194
+ position = hash[:position] || hash["position"]
195
+ lookup_via = hash[:lookup_via] || hash["lookup_via"]
196
+ unless lookup_via.is_a?(Hash)
197
+ raise ArgumentError,
198
+ "Macro::HeredocTemplate::Emit#returns_from_arg requires a `lookup_via:` Hash, " \
199
+ "got #{hash.inspect}"
200
+ end
201
+ new(
202
+ position: position,
203
+ plugin_id: lookup_via[:plugin_id] || lookup_via["plugin_id"],
204
+ fact: lookup_via[:fact] || lookup_via["fact"]
205
+ )
206
+ end
207
+
208
+ def initialize(position:, plugin_id:, fact:)
209
+ validate_position!(position)
210
+ validate_plugin_id!(plugin_id)
211
+ validate_fact!(fact)
212
+
213
+ @position = position
214
+ @plugin_id = plugin_id.dup.freeze
215
+ @fact = fact.to_sym
216
+ freeze
217
+ end
218
+
219
+ def to_h
220
+ {
221
+ "position" => position,
222
+ "lookup_via" => {
223
+ "plugin_id" => plugin_id,
224
+ "fact" => fact.to_s
225
+ }
226
+ }
227
+ end
228
+
229
+ def ==(other)
230
+ other.is_a?(ReturnsFromArg) && to_h == other.to_h
231
+ end
232
+ alias eql? ==
233
+
234
+ def hash
235
+ to_h.hash
236
+ end
237
+
238
+ private
239
+
240
+ def validate_position!(value)
241
+ return if value.is_a?(Integer) && value >= 0
242
+
243
+ raise ArgumentError,
244
+ "ReturnsFromArg#position must be a non-negative Integer, got #{value.inspect}"
245
+ end
246
+
247
+ def validate_plugin_id!(value)
248
+ return if value.is_a?(String) && !value.empty?
249
+
250
+ raise ArgumentError,
251
+ "ReturnsFromArg#plugin_id must be a non-empty String, got #{value.inspect}"
252
+ end
253
+
254
+ def validate_fact!(value)
255
+ return if value.is_a?(Symbol) || (value.is_a?(String) && !value.empty?)
256
+
257
+ raise ArgumentError,
258
+ "ReturnsFromArg#fact must be a Symbol or non-empty String, got #{value.inspect}"
259
+ end
260
+ end
261
+
262
+ private
263
+
264
+ def validate_receiver_constraint!(value)
265
+ return if value.is_a?(String) && !value.empty?
266
+
267
+ raise ArgumentError,
268
+ "Plugin::Macro::HeredocTemplate#receiver_constraint must be a non-empty String, " \
269
+ "got #{value.inspect}"
270
+ end
271
+
272
+ def validate_method_name!(value)
273
+ return if value.is_a?(Symbol) || (value.is_a?(String) && !value.empty?)
274
+
275
+ raise ArgumentError,
276
+ "Plugin::Macro::HeredocTemplate#method_name must be Symbol or non-empty String, " \
277
+ "got #{value.inspect}"
278
+ end
279
+
280
+ def validate_symbol_arg_position!(value)
281
+ return if value.is_a?(Integer) && value >= 0
282
+
283
+ raise ArgumentError,
284
+ "Plugin::Macro::HeredocTemplate#symbol_arg_position must be a non-negative Integer, " \
285
+ "got #{value.inspect}"
286
+ end
287
+
288
+ def coerce_emit_list!(entries, label)
289
+ unless entries.is_a?(Array)
290
+ raise ArgumentError,
291
+ "Plugin::Macro::HeredocTemplate##{label} must be an Array, got #{entries.inspect}"
292
+ end
293
+
294
+ entries.map { |entry| coerce_emit_entry!(entry, label) }.freeze
295
+ end
296
+
297
+ def coerce_emit_entry!(entry, label)
298
+ case entry
299
+ when Emit then entry
300
+ when Hash
301
+ Emit.new(
302
+ name: entry[:name] || entry["name"],
303
+ returns: entry[:returns] || entry["returns"],
304
+ returns_from_arg: entry[:returns_from_arg] || entry["returns_from_arg"]
305
+ )
306
+ else
307
+ raise ArgumentError,
308
+ "Plugin::Macro::HeredocTemplate##{label} entry must be an Emit or Hash, " \
309
+ "got #{entry.inspect}"
310
+ end
311
+ end
312
+ end
313
+ end
314
+ end
315
+ end
@@ -0,0 +1,198 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rigor
4
+ module Plugin
5
+ module Macro
6
+ # ADR-16 Tier B declaration: "the class-level DSL call
7
+ # `<receiver_constraint>.<method_name>(:trait_a, :trait_b, ...)`
8
+ # effectively includes the modules named in
9
+ # `modules_by_symbol[:trait_a]` + `[:trait_b]` (plus any
10
+ # `always_included` modules) on the calling class."
11
+ #
12
+ # Worked target: Devise's model-side `devise :database_authenticatable,
13
+ # :recoverable` DSL (per-library survey § Devise). The bundled
14
+ # registry mirrors `lib/devise/modules.rb`'s symbol → module table;
15
+ # `always_included` carries the modules Devise always mixes in
16
+ # regardless of selection (e.g. `Devise::Models::Authenticatable`).
17
+ #
18
+ # ## Authoring shape
19
+ #
20
+ # manifest(
21
+ # id: "devise",
22
+ # version: "0.1.0",
23
+ # trait_registries: [
24
+ # Rigor::Plugin::Macro::TraitRegistry.new(
25
+ # receiver_constraint: "ActiveRecord::Base",
26
+ # method_name: :devise,
27
+ # symbol_arg_position: :rest,
28
+ # modules_by_symbol: {
29
+ # database_authenticatable: "Devise::Models::DatabaseAuthenticatable",
30
+ # recoverable: "Devise::Models::Recoverable",
31
+ # rememberable: "Devise::Models::Rememberable"
32
+ # },
33
+ # always_included: ["Devise::Models::Authenticatable"]
34
+ # )
35
+ # ]
36
+ # )
37
+ #
38
+ # ## Fields
39
+ #
40
+ # - `receiver_constraint` — fully-qualified class name (String).
41
+ # Synthesis fires when the call's lexical receiver class
42
+ # equals or inherits from this constraint.
43
+ # - `method_name` — Symbol naming the DSL method
44
+ # (e.g. `:devise`).
45
+ # - `symbol_arg_position` — `:rest` (all positional Symbol args
46
+ # are traits, slice 3a's only supported form) or a
47
+ # non-negative Integer (the index of a single trait symbol —
48
+ # reserved for future shapes; not yet honoured by the
49
+ # scanner).
50
+ # - `modules_by_symbol` — Hash<Symbol, String>. Maps each
51
+ # recognised trait symbol to a fully-qualified module name.
52
+ # Symbols not in the table fall through (silent skip; the
53
+ # scanner emits a `macro.tier_b.unknown-trait` `:info`
54
+ # provenance marker per WD9 / WD13).
55
+ # - `always_included` — Array<String>. Fully-qualified module
56
+ # names that are added to every call site (even when no
57
+ # symbols match). Mirrors Devise's `always_include` modules.
58
+ #
59
+ # ## Floor / ceiling per ADR-16 WD13
60
+ #
61
+ # Slice 3 ships at the **floor**: the substrate per-method-
62
+ # explodes each included module's RBS instance methods into
63
+ # the existing `SyntheticMethodIndex` (slice 2b primitive).
64
+ # The synthesised methods adopt the module's authored RBS
65
+ # return types — Tier B is NOT subject to the Tier C
66
+ # `Dynamic[T]` floor because the source-of-truth (the
67
+ # module's authored RBS) is not a manifest-declared string.
68
+ # Per ADR-5 robustness, the substrate does not fabricate
69
+ # precision; it simply replays the modules's signatures.
70
+ #
71
+ # **Out of scope for slice 3** (deferred follow-ups):
72
+ # - `class_methods_module:` per-trait (Devise's `ClassMethods`
73
+ # extend-pattern); slice 3 covers instance methods only.
74
+ # - `sort_key:` for controlled include ordering across traits;
75
+ # slice 3 uses plugin-registration order then registry
76
+ # declaration order.
77
+ # - `included_do_digest:` — the per-module `included do` block
78
+ # facts (attr_reader / after_save / etc.); slice 3 emits
79
+ # only the module's plain instance methods.
80
+ #
81
+ # ## Slice 3a scope
82
+ #
83
+ # This file ships the value class only. Slice 3b wires the
84
+ # scanner that walks Tier B call sites + the per-method
85
+ # explosion via `SyntheticMethodIndex`; slice 3c authors
86
+ # `examples/rigor-devise/` model side as the worked consumer.
87
+ class TraitRegistry
88
+ REST_POSITION = :rest
89
+
90
+ attr_reader :receiver_constraint, :method_name, :symbol_arg_position, :modules_by_symbol, :always_included
91
+
92
+ def initialize(receiver_constraint:, method_name:, symbol_arg_position: REST_POSITION,
93
+ modules_by_symbol: {}, always_included: [])
94
+ validate_receiver_constraint!(receiver_constraint)
95
+ validate_method_name!(method_name)
96
+ validate_symbol_arg_position!(symbol_arg_position)
97
+ validate_modules_by_symbol!(modules_by_symbol)
98
+ validate_always_included!(always_included)
99
+
100
+ @receiver_constraint = receiver_constraint.dup.freeze
101
+ @method_name = method_name.to_sym
102
+ @symbol_arg_position = symbol_arg_position
103
+ @modules_by_symbol = modules_by_symbol.to_h { |k, v| [k.to_sym, v.dup.freeze] }.freeze
104
+ @always_included = always_included.map { |m| m.dup.freeze }.freeze
105
+ freeze
106
+ end
107
+
108
+ def to_h
109
+ {
110
+ "receiver_constraint" => receiver_constraint,
111
+ "method_name" => method_name.to_s,
112
+ "symbol_arg_position" => symbol_arg_position.to_s,
113
+ "modules_by_symbol" => modules_by_symbol.to_h { |k, v| [k.to_s, v] },
114
+ "always_included" => always_included
115
+ }
116
+ end
117
+
118
+ def ==(other)
119
+ other.is_a?(TraitRegistry) && to_h == other.to_h
120
+ end
121
+ alias eql? ==
122
+
123
+ def hash
124
+ to_h.hash
125
+ end
126
+
127
+ # @return [String, nil] fully-qualified module name for the
128
+ # given trait symbol, or nil when the registry doesn't
129
+ # know the symbol (caller emits a tier_b.unknown-trait
130
+ # provenance marker and falls through).
131
+ def module_for(symbol)
132
+ modules_by_symbol[symbol.to_sym]
133
+ end
134
+
135
+ private
136
+
137
+ def validate_receiver_constraint!(value)
138
+ return if value.is_a?(String) && !value.empty?
139
+
140
+ raise ArgumentError,
141
+ "Plugin::Macro::TraitRegistry#receiver_constraint must be a non-empty String, " \
142
+ "got #{value.inspect}"
143
+ end
144
+
145
+ def validate_method_name!(value)
146
+ return if value.is_a?(Symbol) || (value.is_a?(String) && !value.empty?)
147
+
148
+ raise ArgumentError,
149
+ "Plugin::Macro::TraitRegistry#method_name must be Symbol or non-empty String, " \
150
+ "got #{value.inspect}"
151
+ end
152
+
153
+ def validate_symbol_arg_position!(value)
154
+ return if value == REST_POSITION || (value.is_a?(Integer) && value >= 0)
155
+
156
+ raise ArgumentError,
157
+ "Plugin::Macro::TraitRegistry#symbol_arg_position must be :rest or a non-negative Integer, " \
158
+ "got #{value.inspect}"
159
+ end
160
+
161
+ def validate_modules_by_symbol!(value)
162
+ unless value.is_a?(Hash)
163
+ raise ArgumentError,
164
+ "Plugin::Macro::TraitRegistry#modules_by_symbol must be a Hash, got #{value.inspect}"
165
+ end
166
+
167
+ value.each do |k, v|
168
+ unless k.is_a?(Symbol) || (k.is_a?(String) && !k.empty?)
169
+ raise ArgumentError,
170
+ "Plugin::Macro::TraitRegistry#modules_by_symbol key must be Symbol/non-empty String, " \
171
+ "got #{k.inspect}"
172
+ end
173
+ next if v.is_a?(String) && !v.empty?
174
+
175
+ raise ArgumentError,
176
+ "Plugin::Macro::TraitRegistry#modules_by_symbol value must be a non-empty String, " \
177
+ "got #{v.inspect}"
178
+ end
179
+ end
180
+
181
+ def validate_always_included!(value)
182
+ unless value.is_a?(Array)
183
+ raise ArgumentError,
184
+ "Plugin::Macro::TraitRegistry#always_included must be an Array, got #{value.inspect}"
185
+ end
186
+
187
+ value.each do |m|
188
+ next if m.is_a?(String) && !m.empty?
189
+
190
+ raise ArgumentError,
191
+ "Plugin::Macro::TraitRegistry#always_included entry must be a non-empty String, " \
192
+ "got #{m.inspect}"
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end
198
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "macro/block_as_method"
4
+ require_relative "macro/external_file"
5
+ require_relative "macro/heredoc_template"
6
+ require_relative "macro/trait_registry"
7
+
8
+ module Rigor
9
+ module Plugin
10
+ # Substrate declarations for the macro / DSL expansion tiers
11
+ # introduced by ADR-16. Plugin authors declare entries under
12
+ # `Plugin::Manifest` slots (`block_as_methods:`,
13
+ # `trait_registries:`, `heredoc_macros:`,
14
+ # `external_file_inclusions:`) and the substrate consumes them
15
+ # to recognise the call shapes a library exposes to its users.
16
+ #
17
+ # Slice 1a (this file's first delivery) ships the Tier A value
18
+ # class only. The other tiers' value classes + their manifest
19
+ # slots arrive in subsequent slices per ADR-16 § Implementation
20
+ # slicing. The namespace is reserved here so subsequent slices
21
+ # add files alongside `block_as_method.rb` without churn.
22
+ #
23
+ # Per ADR-16 § WD13, substrate-produced output ships at a
24
+ # **floor** in v0.1.x ("substrate-affected code parses cleanly
25
+ # and has its identifiers resolved"); precise return-type
26
+ # emission is the ceiling and arrives in a later slice through
27
+ # the ADR-13 `Plugin::TypeNodeResolver` chain.
28
+ module Macro
29
+ end
30
+ end
31
+ end