Skip to content

Commit 006101a

Browse files
authored
Merge pull request #1710 from benrfairless/fix/ruby-namespaced-constructor-callee
fix(extraction): reduce A::B.new callee to the constant
2 parents 2658a18 + 849e74f commit 006101a

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2249,11 +2249,15 @@ static char *extract_callee_name(CBMArena *a, TSNode node, const char *source, C
22492249
// method is `new`. The constructor body lives in `initialize`, so a callee
22502250
// of "new" never resolves. Redirect to the receiver type name so the call
22512251
// links to the class/constructor like every other language's `new T()`.
2252+
// Scope-resolved receivers (`Admin::User.new`) get the same treatment —
2253+
// the callee carries the full constant path ("Admin::User"), whose bare
2254+
// leaf ("User") is what resolution joins on.
22522255
if (lang == CBM_LANG_RUBY) {
22532256
TSNode m = ts_node_child_by_field_name(node, TS_FIELD("method"));
22542257
TSNode recv = ts_node_child_by_field_name(node, TS_FIELD("receiver"));
22552258
if (!ts_node_is_null(m) && !ts_node_is_null(recv) &&
2256-
strcmp(ts_node_type(recv), "constant") == 0) {
2259+
(strcmp(ts_node_type(recv), "constant") == 0 ||
2260+
strcmp(ts_node_type(recv), "scope_resolution") == 0)) {
22572261
char *mt = cbm_node_text(a, m, source);
22582262
if (mt && strcmp(mt, "new") == 0) {
22592263
char *rt = cbm_node_text(a, recv, source);

‎tests/test_extraction.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,57 @@ TEST(ruby_module) {
857857
PASS();
858858
}
859859

860+
/* A Ruby `Const.new` call is rewritten to name the CONSTANT, not the method:
861+
* the constructor body lives in `initialize`, so the call should link the class
862+
* like every other language's `new T()`. The rewrite fired only for bare
863+
* `constant` receivers, so a scope-resolved receiver kept the whole call text
864+
* as its callee ("Admin::User.new"), which resolves to nothing and cannot join
865+
* the Ruby LSP's constructor rows, whose callee is the class QN.
866+
*
867+
* Both spellings must land on the bare constant path; a retained ".new" suffix
868+
* is the bug's signature. */
869+
TEST(ruby_constructor_callee_is_constant_path) {
870+
CBMFileResult *r = extract("module Admin\n class User\n end\nend\n"
871+
"class Widget\nend\n"
872+
"class Maker\n"
873+
" def build\n"
874+
" Admin::User.new\n"
875+
" Widget.new\n"
876+
" end\n"
877+
"end\n",
878+
CBM_LANG_RUBY, "t", "maker.rb");
879+
ASSERT_NOT_NULL(r);
880+
ASSERT_FALSE(r->has_error);
881+
882+
int scoped = 0, bare = 0, kept_new = 0;
883+
for (int i = 0; i < r->calls.count; i++) {
884+
const char *callee = r->calls.items[i].callee_name;
885+
if (strcmp(callee, "Admin::User") == 0)
886+
scoped = 1;
887+
if (strcmp(callee, "Widget") == 0)
888+
bare = 1;
889+
/* Either failure mode: the method name survived on its own, or the
890+
* whole `Recv.new` expression did. */
891+
size_t n = strlen(callee);
892+
if (strcmp(callee, "new") == 0 || (n >= 4 && strcmp(callee + n - 4, ".new") == 0))
893+
kept_new = 1;
894+
}
895+
if (!scoped || !bare || kept_new) {
896+
printf(" ruby callees (%d):\n", r->calls.count);
897+
for (int i = 0; i < r->calls.count; i++)
898+
printf(" '%s'\n", r->calls.items[i].callee_name);
899+
}
900+
/* Scope-resolved receiver reduces to the constant path; its bare leaf is
901+
* what resolution joins on. */
902+
ASSERT(scoped);
903+
/* Bare receiver: pre-existing behaviour, guarded against regression. */
904+
ASSERT(bare);
905+
ASSERT_FALSE(kept_new);
906+
907+
cbm_free_result(r);
908+
PASS();
909+
}
910+
860911
/* --- C# --- */
861912
TEST(csharp_class) {
862913
CBMFileResult *r = extract("namespace App { public class Service { public void Run() {} public "
@@ -13493,6 +13544,7 @@ SUITE(extraction) {
1349313544
RUN_TEST(php_function);
1349413545
RUN_TEST(ruby_class);
1349513546
RUN_TEST(ruby_module);
13547+
RUN_TEST(ruby_constructor_callee_is_constant_path);
1349613548
RUN_TEST(csharp_class);
1349713549
RUN_TEST(csharp_interface);
1349813550
RUN_TEST(csharp_extension_block_members);

0 commit comments

Comments
 (0)