Skip to content

Commit 1bc9bb4

Browse files
committed
Let global decls be annotated
1 parent 6f8d0f3 commit 1bc9bb4

File tree

7 files changed

+28
-8
lines changed

7 files changed

+28
-8
lines changed

config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ nodes:
4040
- name: type
4141
- name: location
4242
- name: comment
43+
- name: annotations
4344
- name: RBS::AST::Declarations::Interface
4445
fields:
4546
- name: name

ext/rbs_extension/parser.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ VALUE parse_method_type(parserstate *state) {
13141314
/*
13151315
global_decl ::= {tGIDENT} `:` <type>
13161316
*/
1317-
static VALUE parse_global_decl(parserstate *state) {
1317+
static VALUE parse_global_decl(parserstate *state, VALUE annotations) {
13181318
range decl_range;
13191319
decl_range.start = state->current_token.range.start;
13201320

@@ -1334,7 +1334,7 @@ static VALUE parse_global_decl(parserstate *state) {
13341334
rbs_loc_add_required_child(loc, INTERN("name"), name_range);
13351335
rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
13361336

1337-
return rbs_ast_decl_global(typename, type, location, comment);
1337+
return rbs_ast_decl_global(typename, type, location, comment, annotations);
13381338
}
13391339

13401340
/*
@@ -2606,7 +2606,7 @@ static VALUE parse_nested_decl(parserstate *state, const char *nested_in, positi
26062606
break;
26072607
}
26082608
case tGIDENT: {
2609-
decl = parse_global_decl(state);
2609+
decl = parse_global_decl(state, annotations);
26102610
break;
26112611
}
26122612
case kTYPE: {
@@ -2651,7 +2651,7 @@ static VALUE parse_decl(parserstate *state) {
26512651
return parse_const_decl(state);
26522652
}
26532653
case tGIDENT: {
2654-
return parse_global_decl(state);
2654+
return parse_global_decl(state, annotations);
26552655
}
26562656
case kTYPE: {
26572657
return parse_type_decl(state, annot_pos, annotations);

include/rbs/ruby_objs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ VALUE rbs_ast_decl_class(VALUE name, VALUE type_params, VALUE super_class, VALUE
1616
VALUE rbs_ast_decl_class_super(VALUE name, VALUE args, VALUE location);
1717
VALUE rbs_ast_decl_class_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment, VALUE annotations);
1818
VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE comment);
19-
VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment);
19+
VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment, VALUE annotations);
2020
VALUE rbs_ast_decl_interface(VALUE name, VALUE type_params, VALUE members, VALUE annotations, VALUE location, VALUE comment);
2121
VALUE rbs_ast_decl_module(VALUE name, VALUE type_params, VALUE self_types, VALUE members, VALUE annotations, VALUE location, VALUE comment);
2222
VALUE rbs_ast_decl_module_self(VALUE name, VALUE args, VALUE location);

lib/rbs/ast/declarations.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,14 @@ class Global < Base
385385
attr_reader :type
386386
attr_reader :location
387387
attr_reader :comment
388+
attr_reader :annotations
388389

389-
def initialize(name:, type:, location:, comment:)
390+
def initialize(name:, type:, location:, comment:, annotations: [])
390391
@name = name
391392
@type = type
392393
@location = location
393394
@comment = comment
395+
@annotations = annotations
394396
end
395397

396398
def ==(other)

sig/declarations.rbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,10 @@ module RBS
223223
attr_reader type: Types::t
224224
attr_reader location: loc?
225225
attr_reader comment: Comment?
226+
attr_reader annotations: Array[Annotation]
226227

227-
def initialize: (name: Symbol, type: Types::t, location: loc?, comment: Comment?) -> void
228+
def initialize: (name: Symbol, type: Types::t, location: loc?, comment: Comment?, annotations: Array[Annotation]) -> void
229+
| %a{deprecated} (name: Symbol, type: Types::t, location: loc?, comment: Comment?) -> void
228230

229231
include _HashEqual
230232
include _ToJson

src/ruby_objs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,13 @@ VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE commen
100100
);
101101
}
102102

103-
VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment) {
103+
VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment, VALUE annotations) {
104104
VALUE _init_kwargs = rb_hash_new();
105105
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name);
106106
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type);
107107
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location);
108108
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment);
109+
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations);
109110

110111
return CLASS_NEW_INSTANCE(
111112
RBS_AST_Declarations_Global,

test/rbs/signature_parsing_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,4 +2240,18 @@ module Foo = Kernel
22402240
end
22412241
end
22422242
end
2243+
2244+
def test_global__annotation
2245+
Parser.parse_signature(<<~RBS).tap do |_, _, decls|
2246+
%a{annotation}
2247+
$FOO: String
2248+
RBS
2249+
2250+
assert_equal 1, decls.size
2251+
decls[0].tap do |decl|
2252+
assert_instance_of RBS::AST::Declarations::Global, decl
2253+
assert_equal ["annotation"], decl.annotations.map(&:string)
2254+
end
2255+
end
2256+
end
22432257
end

0 commit comments

Comments
 (0)