Skip to content

Commit 91ab0d7

Browse files
authored
Merge pull request #2456 from Shopify/at-fix-block-loc
Expose and fix `Block#location`
2 parents d4fc1f0 + d996fea commit 91ab0d7

File tree

6 files changed

+34
-7
lines changed

6 files changed

+34
-7
lines changed

config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ nodes:
345345
- name: RBS::Types::Bases::Top
346346
- name: RBS::Types::Bases::Void
347347
- name: RBS::Types::Block
348-
expose_location: false
348+
expose_location: true
349349
fields:
350350
- name: type
351351
c_type: rbs_node

ext/rbs_extension/ast_translation.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan
905905
rbs_types_block_t *node = (rbs_types_block_t *)instance;
906906

907907
VALUE h = rb_hash_new();
908+
rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location));
908909
rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node
909910
rb_hash_aset(h, ID2SYM(rb_intern("required")), node->required ? Qtrue : Qfalse);
910911
rb_hash_aset(h, ID2SYM(rb_intern("self_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->self_type)); // rbs_node

lib/rbs/types.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,8 +1339,10 @@ class Block
13391339
attr_reader :type
13401340
attr_reader :required
13411341
attr_reader :self_type
1342+
attr_reader :location
13421343

1343-
def initialize(type:, required:, self_type: nil)
1344+
def initialize(location: nil, type:, required:, self_type: nil)
1345+
@location = location
13441346
@type = type
13451347
@required = required ? true : false
13461348
@self_type = self_type

sig/types.rbs

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

519519
attr_reader self_type: t?
520520

521-
def initialize: (type: function, ?self_type: t?, required: boolish) -> void
521+
type loc = Location[bot, bot]
522+
attr_reader location: loc?
523+
524+
def initialize: (?location: loc?, type: function, ?self_type: t?, required: boolish) -> void
522525

523526
def ==: (untyped other) -> bool
524527

src/parser.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -722,11 +722,17 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
722722
}
723723

724724
bool required = true;
725+
rbs_range_t block_range;
726+
725727
if (parser->next_token.type == pQUESTION && parser->next_token2.type == pLBRACE) {
726728
// Optional block
729+
block_range.start = parser->next_token.range.start;
727730
required = false;
728731
rbs_parser_advance(parser);
732+
} else if (parser->next_token.type == pLBRACE) {
733+
block_range.start = parser->next_token.range.start;
729734
}
735+
730736
if (parser->next_token.type == pLBRACE) {
731737
rbs_parser_advance(parser);
732738

@@ -746,9 +752,12 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
746752
rbs_node_t *block_return_type = NULL;
747753
CHECK_PARSE(parse_optional(parser, &block_return_type));
748754

755+
ADVANCE_ASSERT(parser, pRBRACE);
756+
757+
block_range.end = parser->current_token.range.end;
758+
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), block_range);
759+
749760
rbs_node_t *block_function = NULL;
750-
function_range.end = parser->current_token.range.end;
751-
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), function_range);
752761
if (rbs_is_untyped_params(&block_params)) {
753762
block_function = (rbs_node_t *) rbs_types_untyped_function_new(ALLOCATOR(), loc, block_return_type);
754763
} else {
@@ -767,8 +776,6 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
767776
}
768777

769778
block = rbs_types_block_new(ALLOCATOR(), loc, block_function, required, self_type);
770-
771-
ADVANCE_ASSERT(parser, pRBRACE);
772779
}
773780

774781
ADVANCE_ASSERT(parser, pARROW);

test/rbs/parser_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,20 @@ def test_parse_method_type2
733733
end
734734
end
735735

736+
def test_parse_method_type_block
737+
RBS::Parser.parse_method_type(buffer("{ -> void } -> void")).tap do |method_type|
738+
assert_equal "{ -> void }", method_type.block.location.source
739+
end
740+
741+
RBS::Parser.parse_method_type(buffer("(Integer) { (Integer) -> void } -> void")).tap do |method_type|
742+
assert_equal "{ (Integer) -> void }", method_type.block.location.source
743+
end
744+
745+
RBS::Parser.parse_method_type(buffer("() ?{ () -> void } -> void")).tap do |method_type|
746+
assert_equal "?{ () -> void }", method_type.block.location.source
747+
end
748+
end
749+
736750
def test_newline_inconsistency
737751
code = "module Test\r\nend"
738752

0 commit comments

Comments
 (0)