diff --git a/lib/rbs/inline/annotation_parser.rb b/lib/rbs/inline/annotation_parser.rb index f0e7c33..2967c99 100644 --- a/lib/rbs/inline/annotation_parser.rb +++ b/lib/rbs/inline/annotation_parser.rb @@ -170,7 +170,7 @@ class Tokenizer "*" => :kSTAR, "--" => :kMINUS2, } #:: Hash[String, Symbol] - PUNCTS_RE = Regexp.union(PUNCTS.keys) + PUNCTS_RE = Regexp.union(PUNCTS.keys) #:: Regexp # @rbs scanner: StringScanner # @rbs return: void @@ -643,13 +643,8 @@ def parse_use_clause(tokenizer) if tokenizer.type?(:kAS) as_tree = AST::Tree.new(:as) - as_tree << tokenizer.advance(as_tree) - - if tokenizer.type?(:tLVAR, :tIFIDENT, :tUIDENT) - as_tree < void diff --git a/sig/rbs/inline/ast/declarations.rbs b/sig/rbs/inline/ast/declarations.rbs index db96a82..fe23374 100644 --- a/sig/rbs/inline/ast/declarations.rbs +++ b/sig/rbs/inline/ast/declarations.rbs @@ -2,7 +2,7 @@ module RBS module Inline module AST module Declarations - type t = ClassDecl | ModuleDecl + type t = ClassDecl | ModuleDecl | ConstantDecl module ConstantUtil def type_name: (Prism::Node) -> TypeName? @@ -46,6 +46,24 @@ module RBS %a{pure} def module_selfs: () -> Array[Annotations::ModuleSelf] end + + class ConstantDecl < Base + include ConstantUtil + + attr_reader node: Prism::ConstantWriteNode + + attr_reader comments: AnnotationParser::ParsingResult? + + attr_reader assertion: Annotations::Assertion? + + def initialize: (Prism::ConstantWriteNode, AnnotationParser::ParsingResult?, Annotations::Assertion?) -> void + + %a{pure} def type: () -> Types::t + + %a{pure} def literal_type: () -> Types::t? + + %a{pure} def constant_name: () -> TypeName? + end end end end diff --git a/sig/rbs/inline/parser.rbs b/sig/rbs/inline/parser.rbs index fc1f127..266951f 100644 --- a/sig/rbs/inline/parser.rbs +++ b/sig/rbs/inline/parser.rbs @@ -11,7 +11,7 @@ module RBS attr_reader decls: Array[AST::Declarations::t] - attr_reader surrounding_decls: Array[AST::Declarations::t] + attr_reader surrounding_decls: Array[AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl] attr_reader current_visibility: RBS::AST::Members::visibility? @@ -19,11 +19,12 @@ module RBS def self.parse: (ParseResult[ProgramNode]) -> [Array[AST::Annotations::Use], Array[AST::Declarations::t]]? - def current_class_module_decl: () -> AST::Declarations::t? + def current_class_module_decl: () -> (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl | nil) - def current_class_module_decl!: () -> AST::Declarations::t + def current_class_module_decl!: () -> (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl) - def push_class_module_decl: (AST::Declarations::t) { () -> void } -> void + def push_class_module_decl: (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl) { () -> void } -> void + | (AST::Declarations::ConstantDecl) -> void def ignored_node?: (Node) -> bool @@ -31,6 +32,8 @@ module RBS def application_annotation: (Node node) -> AST::Annotations::Application? + def assertion_annotation: (Node node) -> AST::Annotations::Assertion? + def push_visibility: (RBS::AST::Members::visibility | nil) { () -> void } -> void end end diff --git a/sig/rbs/inline/writer.rbs b/sig/rbs/inline/writer.rbs index 8e49872..91554b4 100644 --- a/sig/rbs/inline/writer.rbs +++ b/sig/rbs/inline/writer.rbs @@ -19,6 +19,8 @@ module RBS def translate_module_decl: (AST::Declarations::ModuleDecl) -> RBS::AST::Declarations::Module? + def translate_constant_decl: (AST::Declarations::ConstantDecl) -> RBS::AST::Declarations::Constant? + def translate_member: (AST::Members::t) -> Array[RBS::AST::Members::t]? end end diff --git a/test/rbs/inline/writer_test.rb b/test/rbs/inline/writer_test.rb index d8bb404..54f1b29 100644 --- a/test/rbs/inline/writer_test.rb +++ b/test/rbs/inline/writer_test.rb @@ -366,4 +366,25 @@ def foo: () -> untyped end RBS end + + def test_constant_decl + output = translate(<<~RUBY) + VERSION = "hogehoge" + + SIZE = [123] #:: Array[Integer] + + NAMES = __dir__ + + # @rbs skip + SKIP = 123 + RUBY + + assert_equal <<~RBS, output + VERSION: ::String + + SIZE: Array[Integer] + + NAMES: untyped + RBS + end end