Skip to content

Commit

Permalink
Merge pull request #210 from Shopify/Alex/translate-splat
Browse files Browse the repository at this point in the history
Implement Prism -> Sorbet translation for Array splats
  • Loading branch information
amomchilov authored Aug 22, 2024
2 parents 236c723 + 7b5114b commit ebea65c
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 11 deletions.
9 changes: 8 additions & 1 deletion parser/prism/Translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,14 @@ std::unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
return make_unique<parser::SClass>(parser.translateLocation(loc), parser.translateLocation(declLoc),
std::move(expr), std::move(body));
}
case PM_SPLAT_NODE: {
auto splatNode = reinterpret_cast<pm_splat_node *>(node);
pm_location_t *loc = &splatNode->base.location;

auto expr = translate(splatNode->expression);

return make_unique<parser::Splat>(parser.translateLocation(loc), std::move(expr));
}
case PM_STATEMENTS_NODE: {
auto inlineIfSingle = true;
return translateStatements(reinterpret_cast<pm_statements_node *>(node), inlineIfSingle);
Expand Down Expand Up @@ -631,7 +639,6 @@ std::unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
case PM_SOURCE_ENCODING_NODE:
case PM_SOURCE_FILE_NODE:
case PM_SOURCE_LINE_NODE:
case PM_SPLAT_NODE:
case PM_UNDEF_NODE:
case PM_WHEN_NODE:
case PM_WHILE_NODE:
Expand Down
19 changes: 19 additions & 0 deletions test/prism_regression/array.parse-tree.exp
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,24 @@ Begin {
}
]
}
Array {
elts = [
Integer {
val = "1"
}
Integer {
val = "2"
}
Splat {
var = Const {
scope = NULL
name = <C <U SPLATTED>>
}
}
Integer {
val = "3"
}
]
}
]
}
3 changes: 3 additions & 0 deletions test/prism_regression/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
%w[string4 string5 string6]

%W[string7 string8 string9]

[1, 2, *SPLATTED, 3]
# ^^^^^^^^ error: Unable to resolve constant `SPLATTED`
30 changes: 24 additions & 6 deletions test/prism_regression/call_all_params.parse-tree.exp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ Begin {
Integer {
val = "2"
}
Integer {
val = "3"
Splat {
var = Array {
elts = [
Integer {
val = "3"
}
]
}
}
Hash {
kwargs = true
Expand Down Expand Up @@ -73,8 +79,14 @@ Begin {
Integer {
val = "2"
}
Integer {
val = "3"
Splat {
var = Array {
elts = [
Integer {
val = "3"
}
]
}
}
Hash {
kwargs = true
Expand Down Expand Up @@ -152,8 +164,14 @@ Begin {
Integer {
val = "2"
}
Integer {
val = "3"
Splat {
var = Array {
elts = [
Integer {
val = "3"
}
]
}
}
Hash {
kwargs = true
Expand Down
8 changes: 4 additions & 4 deletions test/prism_regression/call_all_params.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# typed: true

receiver.foo(1, 2, 3, d: 4, e: 5, f: 6, &forwarded_block)
receiver.foo(1, 2, *[3], d: 4, e: 5, f: 6, &forwarded_block)
#^^^^^^^^ error: Method `receiver` does not exist on `T.class_of(<root>)`
# ^^^^^^^^^^^^^^^ error: Method `forwarded_block` does not exist on `T.class_of(<root>)`
# ^^^^^^^^^^^^^^^ error: Method `forwarded_block` does not exist on `T.class_of(<root>)`

receiver.foo(1, 2, 3, d: 4, e: 5, f: 6) { |a, b, c, d:, e:, f:, &block| "inline block" }
receiver.foo(1, 2, *[3], d: 4, e: 5, f: 6) { |a, b, c, d:, e:, f:, &block| "inline block" }
#^^^^^^^^ error: Method `receiver` does not exist on `T.class_of(<root>)`

receiver.foo(1, 2, 3, d: 4, e: 5, f: 6) do |a, b, c, d:, e:, f:, &block|
receiver.foo(1, 2, *[3], d: 4, e: 5, f: 6) do |a, b, c, d:, e:, f:, &block|
#^^^^^^^^ error: Method `receiver` does not exist on `T.class_of(<root>)`
"do-end block"
end
11 changes: 11 additions & 0 deletions test/prism_regression/call_kw_rest_params.parse-tree.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DefMethod {
name = <U foo>
args = Args {
args = [
Kwrestarg {
name = <U a>
}
]
}
body = NULL
}
3 changes: 3 additions & 0 deletions test/prism_regression/call_kw_rest_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# typed: true

def foo(**a); end
32 changes: 32 additions & 0 deletions test/prism_regression/call_rest_params.parse-tree.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Begin {
stmts = [
Send {
receiver = NULL
method = <U foo>
args = [
Splat {
var = Send {
receiver = NULL
method = <U a>
args = [
]
}
}
]
}
Send {
receiver = NULL
method = <U foo>
args = [
Splat {
var = Send {
receiver = NULL
method = <U a>
args = [
]
}
}
]
}
]
}
6 changes: 6 additions & 0 deletions test/prism_regression/call_rest_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

# typed: false

foo(*a)

foo *a

0 comments on commit ebea65c

Please sign in to comment.