Skip to content

Commit

Permalink
Merge pull request #2724 from ruby/consolidate-reflection
Browse files Browse the repository at this point in the history
Consolidate integer fields into a single reflection class
  • Loading branch information
kddnewton authored Apr 22, 2024
2 parents eeae958 + 0156057 commit 7142b76
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 47 deletions.
20 changes: 7 additions & 13 deletions rbi/prism/reflection.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ module Prism::Reflection
end

class Prism::Reflection::Field
sig { returns(Symbol) }
def name; end

sig { params(name: Symbol).void }
def initialize(name); end

sig { returns(Symbol) }
def name; end
end

class Prism::Reflection::NodeField < Prism::Reflection::Field
Expand Down Expand Up @@ -38,24 +38,18 @@ end
class Prism::Reflection::OptionalLocationField < Prism::Reflection::Field
end

class Prism::Reflection::UInt8Field < Prism::Reflection::Field
class Prism::Reflection::IntegerField < Prism::Reflection::Field
end

class Prism::Reflection::UInt32Field < Prism::Reflection::Field
class Prism::Reflection::FloatField < Prism::Reflection::Field
end

class Prism::Reflection::FlagsField < Prism::Reflection::Field
sig { returns(T::Array[Symbol]) }
def flags; end

sig { params(name: Symbol, flags: T::Array[Symbol]).void }
def initialize(name, flags); end
end

class Prism::Reflection::IntegerField < Prism::Reflection::Field
end

class Prism::Reflection::DoubleField < Prism::Reflection::Field
sig { returns(T::Array[Symbol]) }
def flags; end
end

module Prism::Reflection
Expand Down
10 changes: 2 additions & 8 deletions sig/prism/reflection.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ module Prism
class OptionalLocationField < Field
end

class UInt8Field < Field
class IntegerField < Field
end

class UInt32Field < Field
class FloatField < Field
end

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

class IntegerField < Field
end

class DoubleField < Field
end

def self.fields_for: (node_singleton node) -> Array[Field]
end
end
38 changes: 12 additions & 26 deletions templates/lib/prism/reflection.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ module Prism
class OptionalLocationField < Field
end

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

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

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

# An integer field represents an arbitrarily-sized integer value. It is used
# exclusively to represent the value of an integer literal. It resolves to
# an Integer in Ruby.
class IntegerField < Field
end

# A double field represents a double-precision floating point value. It is
# used exclusively to represent the value of a floating point literal. It
# resolves to a Float in Ruby.
class DoubleField < Field
end

# Returns the fields for the given node.
def self.fields_for(node)
case node.type
Expand All @@ -127,17 +117,13 @@ module Prism
"LocationField.new(:#{field.name})"
when Prism::Template::OptionalLocationField
"OptionalLocationField.new(:#{field.name})"
when Prism::Template::UInt8Field
"UInt8Field.new(:#{field.name})"
when Prism::Template::UInt32Field
"UInt32Field.new(:#{field.name})"
when Prism::Template::UInt8Field, Prism::Template::UInt32Field, Prism::Template::IntegerField
"Integer.new(:#{field.name})"
when Prism::Template::DoubleField
"FloatField.new(:#{field.name})"
when Prism::Template::FlagsField
found = flags.find { |flag| flag.name == field.kind }.tap { |found| raise "Expected to find #{field.kind}" unless found }
"FlagsField.new(:#{field.name}, [#{found.values.map { |value| ":#{value.name.downcase}?" }.join(", ")}])"
when Prism::Template::IntegerField
"IntegerField.new(:#{field.name})"
when Prism::Template::DoubleField
"DoubleField.new(:#{field.name})"
else
raise field.class.name
end
Expand Down

0 comments on commit 7142b76

Please sign in to comment.