lib/parlour/rbi_generator/constant.rb in parlour-4.0.1 vs lib/parlour/rbi_generator/constant.rb in parlour-5.0.0.beta.1

- old
+ new

@@ -1,15 +1,15 @@ # typed: true module Parlour - class RbiGenerator + class RbiGenerator < Generator # Represents a constant definition. class Constant < RbiObject sig do params( - generator: RbiGenerator, + generator: Generator, name: String, - value: String, + value: Types::TypeLike, eigen_constant: T::Boolean, block: T.nilable(T.proc.params(x: Constant).void) ).void end # Creates a new constant definition. @@ -23,12 +23,12 @@ @value = value @eigen_constant = eigen_constant yield_self(&block) if block end - # @return [String] The value of the constant, as a Ruby code string. - sig { returns(String) } + # @return [String] The value or type of the constant. + sig { returns(Types::TypeLike) } attr_reader :value # @return [Boolean] Whether this constant is defined on the eigenclass # of the current namespace. attr_reader :eigen_constant @@ -54,11 +54,15 @@ # # @param indent_level [Integer] The indentation level to generate the lines at. # @param options [Options] The formatting options to use. # @return [Array<String>] The RBI lines, formatted as specified. def generate_rbi(indent_level, options) - [options.indented(indent_level, "#{name} = #{value}")] + if String === @value + [options.indented(indent_level, "#{name} = #{@value}")] + else + [options.indented(indent_level, "#{name} = T.let(nil, #{@value.generate_rbi})")] + end end sig do override.params( others: T::Array[RbiGenerator::RbiObject] @@ -95,9 +99,16 @@ # Returns a human-readable brief string description of this code. # # @return [String] def describe "Constant (#{name} = #{value})" + end + + sig { override.void } + def generalize_from_rbi! + # There's a good change this is an untyped constant, so rescue + # ParseError and use untyped + @value = (TypeParser.parse_single_type(@value) if String === @value) rescue Types::Untyped.new end end end end