Skip to content

Commit 075d27c

Browse files
committed
Let constant decls be annotated
1 parent 1bc9bb4 commit 075d27c

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
@@ -34,6 +34,7 @@ nodes:
3434
- name: type
3535
- name: location
3636
- name: comment
37+
- name: annotations
3738
- name: RBS::AST::Declarations::Global
3839
fields:
3940
- name: name

ext/rbs_extension/parser.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ static VALUE parse_global_decl(parserstate *state, VALUE annotations) {
13401340
/*
13411341
const_decl ::= {const_name} `:` <type>
13421342
*/
1343-
static VALUE parse_const_decl(parserstate *state) {
1343+
static VALUE parse_const_decl(parserstate *state, VALUE annotations) {
13441344
range decl_range;
13451345

13461346
decl_range.start = state->current_token.range.start;
@@ -1361,7 +1361,7 @@ static VALUE parse_const_decl(parserstate *state) {
13611361
rbs_loc_add_required_child(loc, INTERN("name"), name_range);
13621362
rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
13631363

1364-
return rbs_ast_decl_constant(typename, type, location, comment);
1364+
return rbs_ast_decl_constant(typename, type, location, comment, annotations);
13651365
}
13661366

13671367
/*
@@ -2602,7 +2602,7 @@ static VALUE parse_nested_decl(parserstate *state, const char *nested_in, positi
26022602
switch (state->current_token.type) {
26032603
case tUIDENT:
26042604
case pCOLON2: {
2605-
decl = parse_const_decl(state);
2605+
decl = parse_const_decl(state, annotations);
26062606
break;
26072607
}
26082608
case tGIDENT: {
@@ -2648,7 +2648,7 @@ static VALUE parse_decl(parserstate *state) {
26482648
switch (state->current_token.type) {
26492649
case tUIDENT:
26502650
case pCOLON2: {
2651-
return parse_const_decl(state);
2651+
return parse_const_decl(state, annotations);
26522652
}
26532653
case tGIDENT: {
26542654
return parse_global_decl(state, annotations);

include/rbs/ruby_objs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ VALUE rbs_ast_comment(VALUE string, VALUE location);
1515
VALUE rbs_ast_decl_class(VALUE name, VALUE type_params, VALUE super_class, VALUE members, VALUE annotations, VALUE location, VALUE comment);
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);
18-
VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE comment);
18+
VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE comment, VALUE annotations);
1919
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);

lib/rbs/ast/declarations.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,14 @@ class Constant < Base
349349
attr_reader :type
350350
attr_reader :location
351351
attr_reader :comment
352+
attr_reader :annotations
352353

353-
def initialize(name:, type:, location:, comment:)
354+
def initialize(name:, type:, location:, comment:, annotations: [])
354355
@name = name
355356
@type = type
356357
@location = location
357358
@comment = comment
359+
@annotations = annotations || []
358360
end
359361

360362
def ==(other)

sig/declarations.rbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ module RBS
205205
attr_reader type: Types::t
206206
attr_reader location: loc?
207207
attr_reader comment: Comment?
208+
attr_reader annotations: Array[Annotation]
208209

209-
def initialize: (name: TypeName, type: Types::t, location: loc?, comment: Comment?) -> void
210+
def initialize: (name: TypeName, type: Types::t, location: loc?, comment: Comment?, annotations: Array[Annotation]) -> void
211+
| %a{deprecated} (name: TypeName, type: Types::t, location: loc?, comment: Comment?) -> void
210212

211213
include _HashEqual
212214
include _ToJson

src/ruby_objs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ VALUE rbs_ast_decl_class_alias(VALUE new_name, VALUE old_name, VALUE location, V
8686
);
8787
}
8888

89-
VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE comment) {
89+
VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE comment, VALUE annotations) {
9090
VALUE _init_kwargs = rb_hash_new();
9191
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name);
9292
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type);
9393
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location);
9494
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment);
95+
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations);
9596

9697
return CLASS_NEW_INSTANCE(
9798
RBS_AST_Declarations_Constant,

test/rbs/signature_parsing_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,4 +2254,18 @@ def test_global__annotation
22542254
end
22552255
end
22562256
end
2257+
2258+
def test_constant__annotation
2259+
Parser.parse_signature(<<~RBS).tap do |_, _, decls|
2260+
%a{annotation}
2261+
FOO: String
2262+
RBS
2263+
2264+
assert_equal 1, decls.size
2265+
decls[0].tap do |decl|
2266+
assert_instance_of RBS::AST::Declarations::Constant, decl
2267+
assert_equal ["annotation"], decl.annotations.map(&:string)
2268+
end
2269+
end
2270+
end
22572271
end

0 commit comments

Comments
 (0)