Skip to content

Commit 6f2dd07

Browse files
authored
Merge pull request #2302 from ruby/parse-annotations
Let class/module alias decls, global decls, and constant decls be annotated
2 parents d2da22d + 075d27c commit 6f2dd07

File tree

7 files changed

+93
-25
lines changed

7 files changed

+93
-25
lines changed

config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,21 @@ nodes:
2727
- name: old_name
2828
- name: location
2929
- name: comment
30+
- name: annotations
3031
- name: RBS::AST::Declarations::Constant
3132
fields:
3233
- name: name
3334
- name: type
3435
- name: location
3536
- name: comment
37+
- name: annotations
3638
- name: RBS::AST::Declarations::Global
3739
fields:
3840
- name: name
3941
- name: type
4042
- name: location
4143
- name: comment
44+
- name: annotations
4245
- name: RBS::AST::Declarations::Interface
4346
fields:
4447
- name: name
@@ -67,6 +70,7 @@ nodes:
6770
- name: old_name
6871
- name: location
6972
- name: comment
73+
- name: annotations
7074
- name: RBS::AST::Declarations::TypeAlias
7175
fields:
7276
- name: name

ext/rbs_extension/parser.c

Lines changed: 10 additions & 10 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,13 +1334,13 @@ 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
/*
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
/*
@@ -2465,7 +2465,7 @@ static VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE a
24652465
rbs_loc_add_required_child(loc, INTERN("eq"), eq_range);
24662466
rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range);
24672467

2468-
return rbs_ast_decl_module_alias(module_name, old_name, location, comment);
2468+
return rbs_ast_decl_module_alias(module_name, old_name, location, comment, annotations);
24692469
} else {
24702470
return parse_module_decl0(state, keyword_range, module_name, module_name_range, comment, annotations);
24712471
}
@@ -2582,7 +2582,7 @@ static VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE an
25822582
rbs_loc_add_required_child(loc, INTERN("eq"), eq_range);
25832583
rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range);
25842584

2585-
return rbs_ast_decl_class_alias(class_name, old_name, location, comment);
2585+
return rbs_ast_decl_class_alias(class_name, old_name, location, comment, annotations);
25862586
} else {
25872587
return parse_class_decl0(state, keyword_range, class_name, class_name_range, comment, annotations);
25882588
}
@@ -2602,11 +2602,11 @@ 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: {
2609-
decl = parse_global_decl(state);
2609+
decl = parse_global_decl(state, annotations);
26102610
break;
26112611
}
26122612
case kTYPE: {
@@ -2648,10 +2648,10 @@ 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: {
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ VALUE rbs_ast_annotation(VALUE string, VALUE location);
1414
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);
17-
VALUE rbs_ast_decl_class_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment);
18-
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);
17+
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, VALUE annotations);
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);
23-
VALUE rbs_ast_decl_module_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment);
23+
VALUE rbs_ast_decl_module_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment, VALUE annotations);
2424
VALUE rbs_ast_decl_type_alias(VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment);
2525
VALUE rbs_ast_directives_use(VALUE clauses, VALUE location);
2626
VALUE rbs_ast_directives_use_single_clause(VALUE type_name, VALUE new_name, VALUE location);

lib/rbs/ast/declarations.rb

Lines changed: 9 additions & 4 deletions
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)
@@ -385,12 +387,14 @@ class Global < Base
385387
attr_reader :type
386388
attr_reader :location
387389
attr_reader :comment
390+
attr_reader :annotations
388391

389-
def initialize(name:, type:, location:, comment:)
392+
def initialize(name:, type:, location:, comment:, annotations: [])
390393
@name = name
391394
@type = type
392395
@location = location
393396
@comment = comment
397+
@annotations = annotations
394398
end
395399

396400
def ==(other)
@@ -417,13 +421,14 @@ def to_json(state = _ = nil)
417421
end
418422

419423
class AliasDecl < Base
420-
attr_reader :new_name, :old_name, :location, :comment
424+
attr_reader :new_name, :old_name, :location, :comment, :annotations
421425

422-
def initialize(new_name:, old_name:, location:, comment:)
426+
def initialize(new_name:, old_name:, location:, comment:, annotations: [])
423427
@new_name = new_name
424428
@old_name = old_name
425429
@location = location
426430
@comment = comment
431+
@annotations = annotations
427432
end
428433

429434
def ==(other)

sig/declarations.rbs

Lines changed: 10 additions & 3 deletions
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
@@ -223,8 +225,10 @@ module RBS
223225
attr_reader type: Types::t
224226
attr_reader location: loc?
225227
attr_reader comment: Comment?
228+
attr_reader annotations: Array[Annotation]
226229

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

229233
include _HashEqual
230234
include _ToJson
@@ -250,7 +254,10 @@ module RBS
250254

251255
attr_reader comment: Comment?
252256

253-
def initialize: (new_name: TypeName, old_name: TypeName, location: loc?, comment: Comment?) -> void
257+
attr_reader annotations: Array[Annotation]
258+
259+
def initialize: (new_name: TypeName, old_name: TypeName, location: loc?, comment: Comment?, annotations: Array[Annotation]) -> void
260+
| %a{deprecated} (new_name: TypeName, old_name: TypeName, location: loc?, comment: Comment?) -> void
254261

255262
include _HashEqual
256263
end

src/ruby_objs.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ VALUE rbs_ast_decl_class_super(VALUE name, VALUE args, VALUE location) {
7171
);
7272
}
7373

74-
VALUE rbs_ast_decl_class_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment) {
74+
VALUE rbs_ast_decl_class_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment, VALUE annotations) {
7575
VALUE _init_kwargs = rb_hash_new();
7676
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name);
7777
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("old_name")), old_name);
7878
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location);
7979
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment);
80+
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations);
8081

8182
return CLASS_NEW_INSTANCE(
8283
RBS_AST_Declarations_ClassAlias,
@@ -85,12 +86,13 @@ VALUE rbs_ast_decl_class_alias(VALUE new_name, VALUE old_name, VALUE location, V
8586
);
8687
}
8788

88-
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) {
8990
VALUE _init_kwargs = rb_hash_new();
9091
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name);
9192
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type);
9293
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location);
9394
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment);
95+
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations);
9496

9597
return CLASS_NEW_INSTANCE(
9698
RBS_AST_Declarations_Constant,
@@ -99,12 +101,13 @@ VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE commen
99101
);
100102
}
101103

102-
VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment) {
104+
VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment, VALUE annotations) {
103105
VALUE _init_kwargs = rb_hash_new();
104106
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name);
105107
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type);
106108
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location);
107109
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment);
110+
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations);
108111

109112
return CLASS_NEW_INSTANCE(
110113
RBS_AST_Declarations_Global,
@@ -159,12 +162,13 @@ VALUE rbs_ast_decl_module_self(VALUE name, VALUE args, VALUE location) {
159162
);
160163
}
161164

162-
VALUE rbs_ast_decl_module_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment) {
165+
VALUE rbs_ast_decl_module_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment, VALUE annotations) {
163166
VALUE _init_kwargs = rb_hash_new();
164167
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name);
165168
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("old_name")), old_name);
166169
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location);
167170
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment);
171+
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations);
168172

169173
return CLASS_NEW_INSTANCE(
170174
RBS_AST_Declarations_ModuleAlias,

test/rbs/signature_parsing_test.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,4 +2220,52 @@ module Baz
22202220
assert_empty dirs
22212221
end
22222222
end
2223+
2224+
def test_class_module_alias__annotation
2225+
Parser.parse_signature(<<~RBS).tap do |_, _, decls|
2226+
%a{module}
2227+
module Foo = Kernel
2228+
2229+
%a{class} class Bar = Object
2230+
RBS
2231+
2232+
assert_equal 2, decls.size
2233+
decls[0].tap do |decl|
2234+
assert_instance_of RBS::AST::Declarations::ModuleAlias, decl
2235+
assert_equal ["module"], decl.annotations.map(&:string)
2236+
end
2237+
decls[1].tap do |decl|
2238+
assert_instance_of RBS::AST::Declarations::ClassAlias, decl
2239+
assert_equal ["class"], decl.annotations.map(&:string)
2240+
end
2241+
end
2242+
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
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
22232271
end

0 commit comments

Comments
 (0)