Skip to content

Commit aa767dd

Browse files
jesse-shopifyMorriaregiurleoChallaHalla
committed
Handle RBS type aliases within an empty Class or Module definition
Co-authored-by: Alexandre Terrasa <[email protected]> Co-authored-by: Emily Samp <[email protected]> Co-authored-by: Sid Challa <[email protected]>
1 parent 2e0401d commit aa767dd

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

rbs/CommentsAssociator.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,25 @@ void CommentsAssociator::processTrailingComments(parser::Node *node, parser::Nod
279279
}
280280

281281
unique_ptr<parser::Node> CommentsAssociator::walkBody(parser::Node *node, unique_ptr<parser::Node> body) {
282+
bool allowTypeAlias = false;
283+
if (!contextAllowingTypeAlias.empty()) {
284+
allowTypeAlias = contextAllowingTypeAlias.back().first;
285+
}
286+
287+
if (allowTypeAlias && body == nullptr) {
288+
int endLine = core::Loc::pos2Detail(ctx.file.data(ctx), node->loc.endPos()).line;
289+
auto nodes = parser::NodeVec();
290+
291+
maybeInsertStandalonePlaceholders(nodes, 0, lastLine, endLine);
292+
lastLine = endLine;
293+
294+
if (!nodes.empty()) {
295+
return make_unique<parser::Begin>(node->loc, move(nodes));
296+
}
297+
298+
return nullptr;
299+
}
300+
282301
if (body == nullptr) {
283302
auto nodes = parser::NodeVec();
284303
processTrailingComments(node, nodes);

test/testdata/rbs/signatures_type_aliases.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,19 @@ module TrailingTypeAlias
161161
module EmptyTypeAlias
162162
#: type a = Integer
163163
end
164+
165+
#: -> EmptyTypeAlias::a
166+
def empty_module_type_alias
167+
"42"
168+
# ^^^^ error: Expected `Integer` but found `String("42")` for method result type
169+
end
170+
171+
class EmptyClass
172+
#: type b = String
173+
end
174+
175+
#: -> EmptyClass::b
176+
def empty_class_alias_type
177+
42
178+
# ^^ error: Expected `String` but found `Integer(42)` for method result type
179+
end

test/testdata/rbs/signatures_type_aliases.rb.rewrite-tree.exp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
class <emptyTree><<C <root>>> < (::<todo sym>)
2+
::T::Sig::WithoutRuntime.sig() do ||
3+
<self>.returns(<emptyTree>::<C EmptyTypeAlias>::<C type a>)
4+
end
5+
6+
def empty_module_type_alias<<todo method>>(&<blk>)
7+
"42"
8+
end
9+
10+
::T::Sig::WithoutRuntime.sig() do ||
11+
<self>.returns(<emptyTree>::<C EmptyClass>::<C type b>)
12+
end
13+
14+
def empty_class_alias_type<<todo method>>(&<blk>)
15+
42
16+
end
17+
218
module <emptyTree>::<C Errors><<C <todo sym>>> < ()
319
::T::Sig::WithoutRuntime.sig() do ||
420
<self>.void()
@@ -252,4 +268,14 @@ class <emptyTree><<C <root>>> < (::<todo sym>)
252268
<emptyTree>::<C Integer>
253269
end
254270
end
271+
272+
<runtime method definition of empty_module_type_alias>
273+
274+
class <emptyTree>::<C EmptyClass><<C <todo sym>>> < (::<todo sym>)
275+
<emptyTree>::<C type b> = ::<root>::<C T>.type_alias() do ||
276+
<emptyTree>::<C String>
277+
end
278+
end
279+
280+
<runtime method definition of empty_class_alias_type>
255281
end

0 commit comments

Comments
 (0)