steep-kit 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +18 -0
  3. data/LICENSE.md +21 -0
  4. data/README.md +396 -0
  5. data/exe/steep-kit +12 -0
  6. data/lib/steep_kit/alias_skip.rb +41 -0
  7. data/lib/steep_kit/check.rb +62 -0
  8. data/lib/steep_kit/cli.rb +110 -0
  9. data/lib/steep_kit/config.rb +139 -0
  10. data/lib/steep_kit/constant_literal_inference.rb +33 -0
  11. data/lib/steep_kit/constant_table.rb +152 -0
  12. data/lib/steep_kit/conventions.rb +175 -0
  13. data/lib/steep_kit/dynamic_superclasses.rb +26 -0
  14. data/lib/steep_kit/error.rb +9 -0
  15. data/lib/steep_kit/fast.rb +113 -0
  16. data/lib/steep_kit/generate.rb +55 -0
  17. data/lib/steep_kit/lint_constant_access.rb +181 -0
  18. data/lib/steep_kit/lint_inline.rb +177 -0
  19. data/lib/steep_kit/literal_inference.rb +143 -0
  20. data/lib/steep_kit/mixin_inference/installer.rb +153 -0
  21. data/lib/steep_kit/mixin_inference.rb +149 -0
  22. data/lib/steep_kit/output_plan.rb +181 -0
  23. data/lib/steep_kit/passes.rb +282 -0
  24. data/lib/steep_kit/patch_guard.rb +18 -0
  25. data/lib/steep_kit/paths.rb +17 -0
  26. data/lib/steep_kit/project.rb +44 -0
  27. data/lib/steep_kit/project_types.rb +72 -0
  28. data/lib/steep_kit/rbs_builder.rb +141 -0
  29. data/lib/steep_kit/rbs_collection.rb +61 -0
  30. data/lib/steep_kit/rbs_rails_postprocess.rb +98 -0
  31. data/lib/steep_kit/rbs_rails_regen.rb +39 -0
  32. data/lib/steep_kit/rbs_rails_scratch.rb +169 -0
  33. data/lib/steep_kit/rbs_rails_tasks.rb +20 -0
  34. data/lib/steep_kit/runtime.rb +344 -0
  35. data/lib/steep_kit/settings.rb +96 -0
  36. data/lib/steep_kit/signature_index.rb +116 -0
  37. data/lib/steep_kit/signature_kinds.rb +68 -0
  38. data/lib/steep_kit/signature_merger.rb +149 -0
  39. data/lib/steep_kit/signature_only.rb +33 -0
  40. data/lib/steep_kit/source_analysis.rb +227 -0
  41. data/lib/steep_kit/source_cache.rb +92 -0
  42. data/lib/steep_kit/source_files.rb +66 -0
  43. data/lib/steep_kit/source_set.rb +62 -0
  44. data/lib/steep_kit/steep.rb +35 -0
  45. data/lib/steep_kit/type_name_resolver.rb +47 -0
  46. data/lib/steep_kit/value_classes.rb +89 -0
  47. data/lib/steep_kit/version.rb +3 -0
  48. data/lib/steep_kit/watch.rb +64 -0
  49. data/lib/steep_kit/workspace_graph.rb +90 -0
  50. data/lib/steep_kit.rb +31 -0
  51. metadata +137 -0
@@ -0,0 +1,55 @@
1
+ require_relative 'project'
2
+ require_relative 'project_types'
3
+ require_relative 'signature_kinds'
4
+ require_relative 'source_set'
5
+
6
+ module SteepKit
7
+ # Cold generation shares one parse across projection passes. Watch updates
8
+ # refresh notified sources and projections sharing their namespaces.
9
+ class Generate
10
+ def initialize(config:)
11
+ @config = config
12
+ @sources = SourceSet.new(config:)
13
+ @signature_kinds = SignatureKinds.new(root: config.root, projected_root: config.projected_root)
14
+ @project_types = ProjectTypes.new(config:)
15
+ end
16
+
17
+ def call(changes: nil)
18
+ paths = @sources.refresh(paths: changed_paths(changes))
19
+ @kinds = @signature_kinds.call unless paths
20
+ project = Project.new(@sources.analyses, signature_kinds: @kinds)
21
+
22
+ @project_types.call(project:, paths:)
23
+ rescue Error, SystemCallError
24
+ @kinds = nil
25
+ raise
26
+ end
27
+
28
+ private
29
+
30
+ def changed_paths(changes)
31
+ return if changes.nil? || @kinds.nil?
32
+
33
+ events = changes.filter_map do |path|
34
+ absolute = Pathname(path).expand_path(@config.root)
35
+ Paths.relative_within(absolute, @config.root)
36
+ end.uniq
37
+ return if events.any? { |path| signature_event?(path) }
38
+
39
+ events.select { |path| source_event?(path) }
40
+ end
41
+
42
+ def signature_event?(path)
43
+ return false unless path.end_with?('.rbs')
44
+ return false unless Paths.within?(path, 'sig')
45
+
46
+ !Paths.within?(path, @config.projected_root)
47
+ end
48
+
49
+ def source_event?(path)
50
+ return false unless path.end_with?('.rb')
51
+
52
+ @config.signature_paths.any? { |directory| Paths.within?(path, directory) }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,181 @@
1
+ require 'prism'
2
+ require 'rbs'
3
+ require 'rubydex'
4
+ require 'uri'
5
+ require_relative 'constant_literal_inference'
6
+ require_relative 'literal_inference'
7
+ require_relative 'paths'
8
+ require_relative 'source_analysis'
9
+ require_relative 'workspace_graph'
10
+
11
+ module SteepKit
12
+ # Rubydex-backed key check: every literal `[]`/`fetch` key at every resolved
13
+ # workspace reference to a record-typed constant must exist in the record.
14
+ class LintConstantAccess
15
+ ACCESS_METHODS = %i[[] fetch].freeze
16
+
17
+ RecordConstant = Data.define(:fqn, :keys)
18
+ Reference = Data.define(:path, :line, :column, :record)
19
+ Violation = Data.define(:path, :line, :fqn, :key, :keys)
20
+
21
+ def initialize(config:, stdout: $stdout, stderr: $stderr)
22
+ @root = config.root.realpath
23
+ @stdout = stdout
24
+ @stderr = stderr
25
+ end
26
+
27
+ def run(analyses)
28
+ records, aliases = scan_constants(analyses)
29
+ violations = records.empty? ? [] : check(records, aliases, analyses)
30
+ report(violations)
31
+ violations.empty? ? 0 : 1
32
+ end
33
+
34
+ private
35
+
36
+ def scan_constants(analyses)
37
+ records = {}
38
+ aliases = []
39
+ analyses.each do |analysis|
40
+ analysis.constant_decls.each do |decl|
41
+ entry = analysis.constant_table.entry_at(decl.node)
42
+ register(records, aliases, decl, entry) if entry
43
+ end
44
+ end
45
+ [records, aliases]
46
+ end
47
+
48
+ def register(records, aliases, decl, entry)
49
+ fqn = entry.qualified_name.delete_prefix('::')
50
+ if (type = decl.type).is_a?(RBS::Types::Record)
51
+ records[fqn] = RecordConstant.new(fqn:, keys: (records[fqn]&.keys.to_a | type.all_fields.keys).freeze)
52
+ elsif (reference = constant_reference(entry.value_node))
53
+ aliases << { fqn:, name: reference, nesting: entry.nesting }
54
+ end
55
+ end
56
+
57
+ def constant_reference(node)
58
+ node = LiteralInference.unwrap_freeze(node)
59
+ node.slice if node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)
60
+ end
61
+
62
+ def check(records, aliases, analyses)
63
+ graph = WorkspaceGraph.build(@root)
64
+ expand_aliases(graph, records, aliases)
65
+ violations(collect_references(graph, records.values), analyses)
66
+ end
67
+
68
+ # One hop only — alias chains stay out of scope.
69
+ def expand_aliases(graph, records, aliases)
70
+ aliases.each do |candidate|
71
+ target = resolve_target(graph, candidate)
72
+ record = target && records[target.name]
73
+ next unless record
74
+
75
+ records[candidate[:fqn]] ||= RecordConstant.new(fqn: candidate[:fqn], keys: record.keys)
76
+ end
77
+ end
78
+
79
+ def resolve_target(graph, candidate)
80
+ name = candidate[:name]
81
+ nesting = candidate[:nesting]
82
+ if name.start_with?('::') || nesting.empty?
83
+ graph[name.delete_prefix('::')]
84
+ else
85
+ graph.resolve_constant(name, nesting)
86
+ end
87
+ rescue StandardError
88
+ nil
89
+ end
90
+
91
+ def collect_references(graph, records)
92
+ records.flat_map do |record|
93
+ reference_locations(graph, record).filter_map do |location|
94
+ path = file_path(location)&.then { |file| Paths.relative_within(file, @root) }
95
+ Reference.new(path:, line: location.start_line + 1, column: location.start_column, record:) if path
96
+ end
97
+ end
98
+ end
99
+
100
+ def reference_locations(graph, record)
101
+ declaration = graph[record.fqn]
102
+ case declaration
103
+ when Rubydex::Constant, Rubydex::ConstantAlias, Rubydex::Namespace
104
+ declaration.references.map(&:location)
105
+ else
106
+ []
107
+ end
108
+ end
109
+
110
+ def file_path(location)
111
+ uri = location.uri
112
+ URI.decode_uri_component(uri.delete_prefix('file://')) if uri.start_with?('file://')
113
+ end
114
+
115
+ def violations(references, analyses)
116
+ analyses_by_path = analyses.to_h { |analysis| [analysis.path, analysis] }
117
+ found = references.group_by(&:path).flat_map do |path, refs|
118
+ file_violations(path, refs, analyses_by_path[path])
119
+ end
120
+ found.sort_by { |violation| [violation.path, violation.line] }
121
+ end
122
+
123
+ def file_violations(path, refs, analysis)
124
+ program = parsed_program(path, analysis)
125
+ return [] unless program
126
+
127
+ index = refs.to_h { |ref| [[ref.line, ref.column], ref.record] }
128
+ violations = []
129
+ SourceAnalysis.each_node(program) do |node|
130
+ violation = call_violation(path, index, node) if node.is_a?(Prism::CallNode)
131
+ violations << violation if violation
132
+ end
133
+ violations
134
+ end
135
+
136
+ def parsed_program(path, analysis)
137
+ (analysis || SourceAnalysis.new(root: @root, path:)).prism.value
138
+ rescue SourceError, SystemCallError
139
+ nil
140
+ end
141
+
142
+ def call_violation(path, index, call)
143
+ key = literal_key(call)
144
+ return unless key
145
+
146
+ record = referenced_record(index, call.receiver)
147
+ return if record.nil? || record.keys.include?(key)
148
+
149
+ Violation.new(path:, line: call.location.start_line, fqn: record.fqn, key:, keys: record.keys)
150
+ end
151
+
152
+ def literal_key(call)
153
+ return unless ACCESS_METHODS.include?(call.name)
154
+
155
+ argument = call.arguments&.arguments&.first
156
+ argument.unescaped.to_sym if argument.is_a?(Prism::SymbolNode)
157
+ end
158
+
159
+ def referenced_record(index, receiver)
160
+ location = case receiver
161
+ when Prism::ConstantPathNode then receiver.name_loc
162
+ when Prism::ConstantReadNode then receiver.location
163
+ end
164
+ index[[location.start_line, location.start_column]] if location
165
+ end
166
+
167
+ def report(violations)
168
+ if violations.empty?
169
+ @stdout.puts('lint-constant-access: all record-constant keys resolve')
170
+ else
171
+ @stderr.puts("lint-constant-access: #{violations.size} violation(s):")
172
+ violations.each do |violation|
173
+ @stderr.puts(
174
+ " #{violation.path}:#{violation.line}: :#{violation.key} is not a key of " \
175
+ "#{violation.fqn} (keys: #{violation.keys.sort.join(', ')})"
176
+ )
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,177 @@
1
+ require 'rbs'
2
+ require_relative 'conventions'
3
+ require_relative 'error'
4
+ require_relative 'literal_inference'
5
+ require_relative 'passes'
6
+ require_relative 'source_analysis'
7
+ require_relative 'value_classes'
8
+
9
+ module SteepKit
10
+ # Native inline-RBS diagnostics over the shared analyses, plus rules for
11
+ # inline parser pitfalls and opt-in mixin alias target typing requirements.
12
+ class LintInline
13
+ SINGLETON_MEMBER_CALLS = %i[attr_reader attr_writer attr_accessor include extend prepend].freeze
14
+ NESTED_SCOPES = [Prism::ClassNode, Prism::ModuleNode, Prism::SingletonClassNode].freeze
15
+
16
+ def initialize(stdout: $stdout, stderr: $stderr)
17
+ @stdout = stdout
18
+ @stderr = stderr
19
+ end
20
+
21
+ def run(analyses)
22
+ failures = analyses.flat_map { |analysis| lint_analysis(analysis) }
23
+ report(failures)
24
+ failures.empty? ? 0 : 1
25
+ end
26
+
27
+ private
28
+
29
+ def lint_analysis(analysis)
30
+ inline_diagnostics(analysis) + singleton_class_diagnostics(analysis) + value_class_block_diagnostics(analysis) +
31
+ required_method_type_diagnostics(analysis)
32
+ rescue SourceError => e
33
+ [e.message]
34
+ end
35
+
36
+ def inline_diagnostics(analysis)
37
+ analysis.inline_result.diagnostics.map do |diagnostic|
38
+ kind = diagnostic.class.name.split('::').last
39
+ "#{analysis.path}:#{diagnostic.location.start_line}: [#{kind}] #{diagnostic.message}"
40
+ end
41
+ end
42
+
43
+ def singleton_class_diagnostics(analysis)
44
+ return [] unless analysis.source.include?('<<')
45
+
46
+ failures = []
47
+ analysis.each_parsed_node do |node|
48
+ next unless node.is_a?(Prism::SingletonClassNode) && node.expression.is_a?(Prism::SelfNode)
49
+
50
+ each_singleton_member(analysis, node.body) { |member| failures << singleton_failure(analysis.path, member) }
51
+ end
52
+ failures.compact
53
+ end
54
+
55
+ def each_singleton_member(analysis, node, &block)
56
+ case node
57
+ when nil, *NESTED_SCOPES then nil
58
+ when Prism::DefNode, Prism::ConstantWriteNode, Prism::ConstantPathWriteNode
59
+ yield node unless analysis.skipped?(node)
60
+ when Prism::CallNode then each_singleton_call_member(analysis, node, &block)
61
+ else node.compact_child_nodes.each { |child| each_singleton_member(analysis, child, &block) }
62
+ end
63
+ end
64
+
65
+ def each_singleton_call_member(analysis, call, &block)
66
+ return unless call.receiver.nil?
67
+
68
+ if SINGLETON_MEMBER_CALLS.include?(call.name)
69
+ yield call unless analysis.skipped?(call)
70
+ else
71
+ call.compact_child_nodes.each { |child| each_singleton_member(analysis, child, &block) }
72
+ end
73
+ end
74
+
75
+ def singleton_failure(path, node)
76
+ location = "#{path}:#{node.location.start_line}:"
77
+ case node
78
+ when Prism::DefNode
79
+ return "#{location} [SingletonClassDef] define #{node.name} as `def self.#{node.name}`" if node.receiver.nil?
80
+
81
+ "#{location} [SingletonClassDef] move `def self.#{node.name}` out of `class << self`" if node.receiver.is_a?(Prism::SelfNode)
82
+ when Prism::CallNode
83
+ "#{location} [SingletonClassMember] `#{node.name}` inside `class << self` is read as an instance-side member; " \
84
+ 'move it out of `class << self` or `# @rbs skip` it'
85
+ else
86
+ name = node.is_a?(Prism::ConstantWriteNode) ? node.name : node.target.slice
87
+ "#{location} [SingletonClassMember] constant #{name} inside `class << self` is read as a constant of the enclosing class; " \
88
+ 'move it out of `class << self` or `# @rbs skip` it'
89
+ end
90
+ end
91
+
92
+ def value_class_block_diagnostics(analysis)
93
+ return [] unless analysis.source.include?('Data') || analysis.source.include?('Struct')
94
+
95
+ value_class_calls(analysis).flat_map do |call|
96
+ kind = ValueClasses.kind(call)
97
+ block_defs(call.block).map { |definition| value_class_block_failure(analysis.path, definition, kind) }
98
+ end
99
+ end
100
+
101
+ def value_class_calls(analysis)
102
+ analysis.prism_namespaces.flat_map do |namespace|
103
+ node = namespace.node
104
+ assigned = analysis.body_statements(node).filter_map { |statement| statement.value if ValueClasses.assignment_kind(statement) }
105
+ inherited = node.is_a?(Prism::ClassNode) && ValueClasses.kind(node.superclass) ? [node.superclass] : []
106
+ (assigned + inherited).map { |expression| LiteralInference.unwrap_freeze(expression) }
107
+ end
108
+ end
109
+
110
+ def block_defs(block)
111
+ definitions = []
112
+ return definitions unless block.is_a?(Prism::BlockNode) && block.body
113
+
114
+ SourceAnalysis.each_node(block.body, prune: NESTED_SCOPES) { |node| definitions << node if node.is_a?(Prism::DefNode) }
115
+ definitions
116
+ end
117
+
118
+ def value_class_block_failure(path, definition, kind)
119
+ "#{path}:#{definition.location.start_line}: [ValueClassBlockDef] define #{definition.name} in a " \
120
+ "`class CONST < #{kind == :data ? 'Data.define' : 'Struct.new'}(...)` body, where it is typed and checked"
121
+ end
122
+
123
+ def required_method_type_diagnostics(analysis)
124
+ rules = Conventions.current.aliases.select(&:require_typed)
125
+ return [] if rules.empty?
126
+
127
+ analysis.inline_namespaces.flat_map { |namespace| required_method_failures(analysis.path, namespace, rules) }
128
+ rescue SourceError
129
+ []
130
+ end
131
+
132
+ def required_method_failures(path, namespace, rules)
133
+ targets = matched_targets(namespace)
134
+ defs = Passes.instance_defs(namespace.decl.members).to_h { |member| [member.name, member] }
135
+ seen = Set.new
136
+
137
+ rules.select { |rule| targets.include?(rule.on) }.filter_map { |rule| missing_type_failure(path, rule, defs, seen) }
138
+ end
139
+
140
+ def missing_type_failure(path, rule, defs, seen)
141
+ method = defs[rule.target_name]
142
+ return unless method&.method_type&.empty? && seen.add?([method.location.start_line, rule.target_name])
143
+
144
+ "#{path}:#{method.location.start_line}: [RequiredMethodType] ##{rule.target_name} needs an inline type (#{rule.alias_name} is aliased from it)"
145
+ end
146
+
147
+ def matched_targets(namespace)
148
+ candidates = Set.new
149
+ body_scope = namespace.segments.map(&:name)
150
+ namespace.decl.members.each do |member|
151
+ next unless member.is_a?(RBS::AST::Ruby::Members::IncludeMember)
152
+
153
+ candidates.merge(lexical_candidates(member.module_name, body_scope))
154
+ end
155
+ if namespace.decl.is_a?(RBS::AST::Ruby::Declarations::ClassDecl) && namespace.decl.super_class
156
+ candidates.merge(lexical_candidates(namespace.decl.super_class.type_name, body_scope[0...-1]))
157
+ end
158
+ candidates
159
+ end
160
+
161
+ def lexical_candidates(type_name, scope)
162
+ return [type_name.to_s] if type_name.absolute?
163
+
164
+ names = [*type_name.namespace.path, type_name.name].map(&:to_s)
165
+ scope.size.downto(0).map { |depth| "::#{(scope.first(depth) + names).join('::')}" }
166
+ end
167
+
168
+ def report(failures)
169
+ if failures.empty?
170
+ @stdout.puts('lint-inline: all inline annotations are within the native grammar')
171
+ else
172
+ @stderr.puts("lint-inline: #{failures.size} diagnostic(s):")
173
+ failures.each { |failure| @stderr.puts(" #{failure}") }
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,143 @@
1
+ require 'prism'
2
+ require 'rbs'
3
+
4
+ module SteepKit
5
+ # Conservative literal-to-RBS inference behind the ConstantDecl patch (via
6
+ # ConstantTable): nil for computed, empty, or too-ambiguous values.
7
+ # Symbol-keyed hashes become records so literal-key access stays non-nil
8
+ # under implicitly-returns-nil; computed leaves degrade per-key to untyped.
9
+ class LiteralInference
10
+ MAX_UNION = 3
11
+
12
+ RECORD_KEY = /\A[A-Za-z_][A-Za-z0-9_]*\z/
13
+
14
+ SCALARS = {
15
+ Prism::StringNode => '::String',
16
+ Prism::InterpolatedStringNode => '::String',
17
+ Prism::IntegerNode => '::Integer',
18
+ Prism::FloatNode => '::Float',
19
+ Prism::SymbolNode => '::Symbol',
20
+ Prism::TrueNode => 'bool',
21
+ Prism::FalseNode => 'bool',
22
+ Prism::NilNode => 'nil'
23
+ }.freeze
24
+
25
+ def self.unwrap_freeze(node)
26
+ node.is_a?(Prism::CallNode) && node.name == :freeze && node.receiver ? node.receiver : node
27
+ end
28
+
29
+ # The helper raises on a mid-edit path (`Foo::`).
30
+ def self.constant_type_name(node)
31
+ RBS::AST::Ruby::Helpers::ConstantHelper.constant_as_type_name(node)
32
+ rescue Prism::ConstantPathNode::MissingNodesInConstantPathError
33
+ nil
34
+ end
35
+
36
+ # Names of a constant read/path/write node via RBS's own helper; nil for dynamic parts.
37
+ def self.constant_path_names(node)
38
+ type_name = constant_type_name(node) or return
39
+ [*type_name.namespace.path, type_name.name].map(&:to_s)
40
+ end
41
+
42
+ # resolver: (Array[String] names, absolute: bool) -> String type or nil
43
+ def initialize(resolver: nil)
44
+ @resolver = resolver
45
+ end
46
+
47
+ def type_for(node)
48
+ type = infer(node)
49
+ return unless type
50
+
51
+ type if RBS::Parser.parse_type(type, require_eof: true)
52
+ rescue RBS::ParsingError
53
+ nil
54
+ end
55
+
56
+ private
57
+
58
+ def infer(node)
59
+ scalar_type(node) || composite_type(node)
60
+ end
61
+
62
+ def scalar_type(node)
63
+ SCALARS[node.class]
64
+ end
65
+
66
+ def composite_type(node)
67
+ case node
68
+ when Prism::ArrayNode then array_type(node)
69
+ when Prism::HashNode then hash_type(node)
70
+ when Prism::RangeNode then range_type(node)
71
+ when Prism::ParenthesesNode then parenthesized(node)
72
+ when Prism::ConstantReadNode, Prism::ConstantPathNode then constant_reference(node)
73
+ when Prism::CallNode then infer(node.receiver) if node.name == :freeze && node.receiver
74
+ end
75
+ end
76
+
77
+ # The leading `::` lands in `absolute?` instead of the names.
78
+ def constant_reference(node)
79
+ return unless @resolver
80
+
81
+ type_name = LiteralInference.constant_type_name(node) or return
82
+ @resolver.call([*type_name.namespace.path, type_name.name].map(&:to_s), absolute: type_name.absolute?)
83
+ end
84
+
85
+ def parenthesized(node)
86
+ body = node.body
87
+ infer(body.body.first) if body.is_a?(Prism::StatementsNode) && body.body.size == 1
88
+ end
89
+
90
+ def array_type(node)
91
+ element = union(node.elements)
92
+ "::Array[#{element}]" if element
93
+ end
94
+
95
+ def hash_type(node)
96
+ return if node.elements.empty? || !node.elements.all?(Prism::AssocNode)
97
+
98
+ record_type(node) || generic_hash_type(node)
99
+ end
100
+
101
+ def record_type(node)
102
+ return unless node.elements.all? { |assoc| record_key?(assoc.key) }
103
+
104
+ # Duplicate keys collapse last-wins, matching Ruby hash semantics.
105
+ fields = node.elements.to_h { |assoc| [assoc.key.unescaped, infer(assoc.value) || 'untyped'] }
106
+ "{ #{fields.map { |key, value| "#{key}: #{value}" }.join(', ')} }"
107
+ end
108
+
109
+ def record_key?(key)
110
+ key.is_a?(Prism::SymbolNode) && key.unescaped.match?(RECORD_KEY)
111
+ end
112
+
113
+ def generic_hash_type(node)
114
+ keys = union(node.elements.map(&:key))
115
+ values = union(node.elements.map(&:value))
116
+ "::Hash[#{keys}, #{values}]" if keys && values
117
+ end
118
+
119
+ def range_type(node)
120
+ types = [node.left, node.right].compact.map { |side| infer(side) }
121
+ "::Range[#{types.first}]" if types.uniq.size == 1 && !types.first.nil?
122
+ end
123
+
124
+ def union(nodes)
125
+ return if nodes.empty?
126
+
127
+ types = nodes.map { |node| infer(node) }
128
+ return if types.any?(nil)
129
+
130
+ types = types.uniq
131
+ return optional(types) if types.include?('nil')
132
+ return if types.size > MAX_UNION
133
+
134
+ types.join(' | ')
135
+ end
136
+
137
+ # Via RBS, so a proc/union operand is parenthesized: `(^() -> String)?`.
138
+ def optional(types)
139
+ rest = types - ['nil']
140
+ RBS::Types::Optional.new(type: RBS::Parser.parse_type(rest.first, require_eof: true), location: nil).to_s if rest.size == 1
141
+ end
142
+ end
143
+ end