Skip to content

Commit

Permalink
Merge pull request #395 from Shopify/fix-#394
Browse files Browse the repository at this point in the history
Correctly translate no-message calls' message locations
  • Loading branch information
st0012 authored Jan 6, 2025
2 parents 23cd73f + 453c767 commit 29be814
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
10 changes: 9 additions & 1 deletion parser/prism/Translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,19 @@ unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
auto callNode = down_cast<pm_call_node>(node);

auto loc = location;
auto messageLoc = translateLoc(callNode->message_loc);

auto name = parser.resolveConstant(callNode->name);
auto receiver = translate(callNode->receiver);

core::LocOffsets messageLoc;

// When the message is empty, like `foo.()`, the message location is the same as the call operator location
if (callNode->message_loc.start == nullptr && callNode->message_loc.end == nullptr) {
messageLoc = translateLoc(callNode->call_operator_loc);
} else {
messageLoc = translateLoc(callNode->message_loc);
}

// Handle `~[Integer]`, like `~42`
// Unlike `-[Integer]`, Prism treats `~[Integer]` as a method call
// But Sorbet's legacy parser treats both `~[Integer]` and `-[Integer]` as integer literals
Expand Down
12 changes: 11 additions & 1 deletion test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,11 @@ pipeline_tests(
"testdata/local_vars/**/*.exp",
"testdata/namer/**/*.rb",
"testdata/namer/**/*.exp",
"testdata/lsp/**/*.rb",
"testdata/lsp/**/*.exp",
],
exclude = [
# Parser tests having to do with tree differences in invalid Ruby code; will address later
# Tests having to do with tree differences in invalid Ruby code; will address later
"testdata/parser/error_recovery/**",
"testdata/parser/crash_block_pass_suggestion.rb",
"testdata/parser/invalid_fatal.rb",
Expand All @@ -283,6 +285,14 @@ pipeline_tests(
"testdata/parser/misc.rb",
"testdata/parser/offset0.rb",
"testdata/parser/ruby_25.rb",
"testdata/lsp/completion/bad_arguments.rb",
"testdata/lsp/completion/bad_list_elems.rb",
"testdata/lsp/completion/case_1.rb",
"testdata/lsp/completion/case_2.rb",
"testdata/lsp/completion/eof.rb",
"testdata/lsp/completion/missing_const_name.rb",
"testdata/lsp/completion/missing_fun.rb",
"testdata/lsp/completion/self_receiver.rb",

# Desugar tests having to do with tree differences; will address later
"testdata/desugar/constant_error.rb",
Expand Down
11 changes: 11 additions & 0 deletions test/prism_regression/call.parse-tree.exp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ Begin {
args = [
]
}
Send {
receiver = Send {
receiver = NULL
method = <U foo>
args = [
]
}
method = <U call>
args = [
]
}
Send {
receiver = Send {
receiver = NULL
Expand Down
1 change: 1 addition & 0 deletions test/prism_regression/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

foo
foo()
foo.()
foo[:bar]
foo[:baz] = 1

0 comments on commit 29be814

Please sign in to comment.