Skip to content

Commit 6f8d0f3

Browse files
committed
Let class/module alias declarations be annotated
1 parent d2da22d commit 6f8d0f3

File tree

7 files changed

+37
-9
lines changed

7 files changed

+37
-9
lines changed

config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ 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
@@ -67,6 +68,7 @@ nodes:
6768
- name: old_name
6869
- name: location
6970
- name: comment
71+
- name: annotations
7072
- name: RBS::AST::Declarations::TypeAlias
7173
fields:
7274
- name: name

ext/rbs_extension/parser.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

include/rbs/ruby_objs.h

Lines changed: 2 additions & 2 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);
17+
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);
1919
VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment);
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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,14 @@ def to_json(state = _ = nil)
417417
end
418418

419419
class AliasDecl < Base
420-
attr_reader :new_name, :old_name, :location, :comment
420+
attr_reader :new_name, :old_name, :location, :comment, :annotations
421421

422-
def initialize(new_name:, old_name:, location:, comment:)
422+
def initialize(new_name:, old_name:, location:, comment:, annotations: [])
423423
@new_name = new_name
424424
@old_name = old_name
425425
@location = location
426426
@comment = comment
427+
@annotations = annotations
427428
end
428429

429430
def ==(other)

sig/declarations.rbs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ module RBS
250250

251251
attr_reader comment: Comment?
252252

253-
def initialize: (new_name: TypeName, old_name: TypeName, location: loc?, comment: Comment?) -> void
253+
attr_reader annotations: Array[Annotation]
254+
255+
def initialize: (new_name: TypeName, old_name: TypeName, location: loc?, comment: Comment?, annotations: Array[Annotation]) -> void
256+
| %a{deprecated} (new_name: TypeName, old_name: TypeName, location: loc?, comment: Comment?) -> void
254257

255258
include _HashEqual
256259
end

src/ruby_objs.c

Lines changed: 4 additions & 2 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,
@@ -159,12 +160,13 @@ VALUE rbs_ast_decl_module_self(VALUE name, VALUE args, VALUE location) {
159160
);
160161
}
161162

162-
VALUE rbs_ast_decl_module_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment) {
163+
VALUE rbs_ast_decl_module_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment, VALUE annotations) {
163164
VALUE _init_kwargs = rb_hash_new();
164165
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name);
165166
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("old_name")), old_name);
166167
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location);
167168
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment);
169+
rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations);
168170

169171
return CLASS_NEW_INSTANCE(
170172
RBS_AST_Declarations_ModuleAlias,

test/rbs/signature_parsing_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,4 +2220,24 @@ 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
22232243
end

0 commit comments

Comments
 (0)