Skip to content

Commit 5499302

Browse files
committed
Merge pull request #2456 from Shopify/at-fix-block-loc
Expose and fix `Block#location`
1 parent 1849cbe commit 5499302

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
@@ -785,6 +785,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan
785785
rbs_types_block_t *node = (rbs_types_block_t *) instance;
786786

787787
VALUE h = rb_hash_new();
788+
rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location));
788789
rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node
789790
rb_hash_aset(h, ID2SYM(rb_intern("required")), node->required ? Qtrue : Qfalse);
790791
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
@@ -713,11 +713,17 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
713713
}
714714

715715
bool required = true;
716+
rbs_range_t block_range;
717+
716718
if (parser->next_token.type == pQUESTION && parser->next_token2.type == pLBRACE) {
717719
// Optional block
720+
block_range.start = parser->next_token.range.start;
718721
required = false;
719722
rbs_parser_advance(parser);
723+
} else if (parser->next_token.type == pLBRACE) {
724+
block_range.start = parser->next_token.range.start;
720725
}
726+
721727
if (parser->next_token.type == pLBRACE) {
722728
rbs_parser_advance(parser);
723729

@@ -737,9 +743,12 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
737743
rbs_node_t *block_return_type = NULL;
738744
CHECK_PARSE(parse_optional(parser, &block_return_type));
739745

746+
ADVANCE_ASSERT(parser, pRBRACE);
747+
748+
block_range.end = parser->current_token.range.end;
749+
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), block_range);
750+
740751
rbs_node_t *block_function = NULL;
741-
function_range.end = parser->current_token.range.end;
742-
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), function_range);
743752
if (rbs_is_untyped_params(&block_params)) {
744753
block_function = (rbs_node_t *) rbs_types_untyped_function_new(ALLOCATOR(), loc, block_return_type);
745754
} else {
@@ -758,8 +767,6 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
758767
}
759768

760769
block = rbs_types_block_new(ALLOCATOR(), loc, block_function, required, self_type);
761-
762-
ADVANCE_ASSERT(parser, pRBRACE);
763770
}
764771

765772
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)