Skip to content

Commit 7142b76

Browse files
authored
Merge pull request #2724 from ruby/consolidate-reflection
Consolidate integer fields into a single reflection class
2 parents eeae958 + 0156057 commit 7142b76

3 files changed

Lines changed: 21 additions & 47 deletions

File tree

rbi/prism/reflection.rbi

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ module Prism::Reflection
44
end
55

66
class Prism::Reflection::Field
7-
sig { returns(Symbol) }
8-
def name; end
9-
107
sig { params(name: Symbol).void }
118
def initialize(name); end
9+
10+
sig { returns(Symbol) }
11+
def name; end
1212
end
1313

1414
class Prism::Reflection::NodeField < Prism::Reflection::Field
@@ -38,24 +38,18 @@ end
3838
class Prism::Reflection::OptionalLocationField < Prism::Reflection::Field
3939
end
4040

41-
class Prism::Reflection::UInt8Field < Prism::Reflection::Field
41+
class Prism::Reflection::IntegerField < Prism::Reflection::Field
4242
end
4343

44-
class Prism::Reflection::UInt32Field < Prism::Reflection::Field
44+
class Prism::Reflection::FloatField < Prism::Reflection::Field
4545
end
4646

4747
class Prism::Reflection::FlagsField < Prism::Reflection::Field
48-
sig { returns(T::Array[Symbol]) }
49-
def flags; end
50-
5148
sig { params(name: Symbol, flags: T::Array[Symbol]).void }
5249
def initialize(name, flags); end
53-
end
54-
55-
class Prism::Reflection::IntegerField < Prism::Reflection::Field
56-
end
5750

58-
class Prism::Reflection::DoubleField < Prism::Reflection::Field
51+
sig { returns(T::Array[Symbol]) }
52+
def flags; end
5953
end
6054

6155
module Prism::Reflection

sig/prism/reflection.rbs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ module Prism
3333
class OptionalLocationField < Field
3434
end
3535

36-
class UInt8Field < Field
36+
class IntegerField < Field
3737
end
3838

39-
class UInt32Field < Field
39+
class FloatField < Field
4040
end
4141

4242
class FlagsField < Field
@@ -45,12 +45,6 @@ module Prism
4545
def initialize: (Symbol name, Array[Symbol] flags) -> void
4646
end
4747

48-
class IntegerField < Field
49-
end
50-
51-
class DoubleField < Field
52-
end
53-
5448
def self.fields_for: (node_singleton node) -> Array[Field]
5549
end
5650
end

templates/lib/prism/reflection.rb.erb

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,16 @@ module Prism
6565
class OptionalLocationField < Field
6666
end
6767

68-
# A uint8 field represents an unsigned 8-bit integer value on a node. It
69-
# resolves to an Integer in Ruby.
70-
class UInt8Field < Field
68+
# An integer field represents an integer value. It is used to represent the
69+
# value of an integer literal, the depth of local variables, and the number
70+
# of a numbered reference. It resolves to an Integer in Ruby.
71+
class IntegerField < Field
7172
end
7273

73-
# A uint32 field represents an unsigned 32-bit integer value on a node. It
74-
# resolves to an Integer in Ruby.
75-
class UInt32Field < Field
74+
# A float field represents a double-precision floating point value. It is
75+
# used exclusively to represent the value of a floating point literal. It
76+
# resolves to a Float in Ruby.
77+
class FloatField < Field
7678
end
7779

7880
# A flags field represents a bitset of flags on a node. It resolves to an
@@ -90,18 +92,6 @@ module Prism
9092
end
9193
end
9294

93-
# An integer field represents an arbitrarily-sized integer value. It is used
94-
# exclusively to represent the value of an integer literal. It resolves to
95-
# an Integer in Ruby.
96-
class IntegerField < Field
97-
end
98-
99-
# A double field represents a double-precision floating point value. It is
100-
# used exclusively to represent the value of a floating point literal. It
101-
# resolves to a Float in Ruby.
102-
class DoubleField < Field
103-
end
104-
10595
# Returns the fields for the given node.
10696
def self.fields_for(node)
10797
case node.type
@@ -127,17 +117,13 @@ module Prism
127117
"LocationField.new(:#{field.name})"
128118
when Prism::Template::OptionalLocationField
129119
"OptionalLocationField.new(:#{field.name})"
130-
when Prism::Template::UInt8Field
131-
"UInt8Field.new(:#{field.name})"
132-
when Prism::Template::UInt32Field
133-
"UInt32Field.new(:#{field.name})"
120+
when Prism::Template::UInt8Field, Prism::Template::UInt32Field, Prism::Template::IntegerField
121+
"Integer.new(:#{field.name})"
122+
when Prism::Template::DoubleField
123+
"FloatField.new(:#{field.name})"
134124
when Prism::Template::FlagsField
135125
found = flags.find { |flag| flag.name == field.kind }.tap { |found| raise "Expected to find #{field.kind}" unless found }
136126
"FlagsField.new(:#{field.name}, [#{found.values.map { |value| ":#{value.name.downcase}?" }.join(", ")}])"
137-
when Prism::Template::IntegerField
138-
"IntegerField.new(:#{field.name})"
139-
when Prism::Template::DoubleField
140-
"DoubleField.new(:#{field.name})"
141127
else
142128
raise field.class.name
143129
end

0 commit comments

Comments
 (0)