Skip to content

Commit bd640ca

Browse files
committed
fix(elixir): Phoenix channel extraction was unreachable in both branches
Neither half of extract_channels_elixir could ever fire, so an Elixir project produced zero Channel nodes regardless of how much Phoenix.PubSub or Phoenix.Channel it used. The emit side read ts_node_child_by_field_name(call, "arguments"). tree-sitter-elixir defines no such field — its whole field set is key, left, operand, operator, quoted_start, quoted_end, right, target and value — so args was always null and elixir_emit_second_arg() returned early every time. extract_defs.c has always used a positional second-child fallback for this; the same fallback is now used here. The listener side dispatched on strcmp(kind, "def"). There is no `def` node type in the grammar: an Elixir definition is a `call` whose target is the macro name. elixir_process_function_def() was therefore dead code, and it also read `name` and `parameters` fields that do not exist. It now takes the def call, unwraps a `when` guard, and reads the head's name and parameters positionally, so guarded clauses like `def handle_in(e, p, s) when is_map(s)` are recognized too. Verified against a Phoenix application; the new test covers Phoenix.PubSub.broadcast/subscribe, a bare push/broadcast emit, and two handle_in clauses, one of them guarded. Signed-off-by: Henry Hazan <henry@teramine.io>
1 parent 46ae198 commit bd640ca

2 files changed

Lines changed: 123 additions & 11 deletions

File tree

‎internal/cbm/extract_channels.c‎

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -901,8 +901,42 @@ static void elixir_emit_second_arg(CBMExtractCtx *ctx, TSNode call, TSNode args,
901901
}
902902
}
903903

904-
static void elixir_process_call(CBMExtractCtx *ctx, TSNode call) {
904+
/* tree-sitter-elixir gives a call's arguments node NO field name — its whole
905+
* field set is key, left, operand, operator, quoted_start, quoted_end, right,
906+
* target and value — so the generic field lookup is always null here and every
907+
* channel below was silently skipped. extract_defs.c has carried the same
908+
* positional fallback for the definition side since Elixir was added. */
909+
static TSNode elixir_channel_call_args(TSNode call) {
910+
TSNode args = ts_node_child_by_field_name(call, TS_FIELD("arguments"));
911+
if (ts_node_is_null(args) && ts_node_child_count(call) > 1) {
912+
args = ts_node_child(call, 1);
913+
}
914+
return args;
915+
}
916+
917+
/* The callee of an Elixir `call`. The grammar labels it `target`; child(0) is
918+
* the same node and is what extract_defs.c reads, so accept either. */
919+
static TSNode elixir_channel_call_target(TSNode call) {
905920
TSNode target = ts_node_child_by_field_name(call, TS_FIELD("target"));
921+
if (ts_node_is_null(target) && ts_node_child_count(call) > 0) {
922+
target = ts_node_child(call, 0);
923+
}
924+
return target;
925+
}
926+
927+
/* Elixir def* macros. `def`/`defp` cover Phoenix channel callbacks; the macro
928+
* forms are included because `handle_in` is generated by macros in some
929+
* libraries. */
930+
static bool elixir_channel_is_def_macro(const char *macro) {
931+
if (!macro) {
932+
return false;
933+
}
934+
return strcmp(macro, "def") == 0 || strcmp(macro, "defp") == 0 ||
935+
strcmp(macro, "defmacro") == 0 || strcmp(macro, "defmacrop") == 0;
936+
}
937+
938+
static void elixir_process_call(CBMExtractCtx *ctx, TSNode call) {
939+
TSNode target = elixir_channel_call_target(call);
906940
if (ts_node_is_null(target)) {
907941
return;
908942
}
@@ -911,7 +945,7 @@ static void elixir_process_call(CBMExtractCtx *ctx, TSNode call) {
911945
return;
912946
}
913947

914-
TSNode args = ts_node_child_by_field_name(call, TS_FIELD("arguments"));
948+
TSNode args = elixir_channel_call_args(call);
915949

916950
if (strstr(target_text, "PubSub.broadcast") || strstr(target_text, "PubSub.local_broadcast")) {
917951
elixir_emit_second_arg(ctx, call, args, "phoenix_pubsub", CBM_CHANNEL_EMIT);
@@ -927,18 +961,39 @@ static void elixir_process_call(CBMExtractCtx *ctx, TSNode call) {
927961
}
928962
}
929963

930-
/* Detect handle_in("event", payload, socket) — Phoenix Channel listener */
931-
static void elixir_process_function_def(CBMExtractCtx *ctx, TSNode func_def) {
932-
TSNode name_node = ts_node_child_by_field_name(func_def, TS_FIELD("name"));
933-
if (ts_node_is_null(name_node)) {
964+
/* Detect handle_in("event", payload, socket) — Phoenix Channel listener.
965+
* `def_call` is the `call` node of a def* macro. Elixir has no `def` node type
966+
* and no `name`/`parameters` fields: a definition is a call whose first
967+
* argument is the head, itself a call of name(params) — wrapped in a `when`
968+
* binary_operator when the clause carries a guard. */
969+
static void elixir_process_function_def(CBMExtractCtx *ctx, TSNode def_call) {
970+
TSNode args = elixir_channel_call_args(def_call);
971+
if (ts_node_is_null(args) || ts_node_named_child_count(args) == 0) {
972+
return;
973+
}
974+
TSNode head = ts_node_named_child(args, 0);
975+
if (strcmp(ts_node_type(head), "binary_operator") == 0) {
976+
TSNode op = ts_node_child_by_field_name(head, TS_FIELD("operator"));
977+
TSNode left = ts_node_child_by_field_name(head, TS_FIELD("left"));
978+
if (!ts_node_is_null(op) && !ts_node_is_null(left) &&
979+
ts_node_end_byte(op) - ts_node_start_byte(op) == 4 &&
980+
strncmp(ctx->source + ts_node_start_byte(op), "when", 4) == 0) {
981+
head = left;
982+
}
983+
}
984+
if (strcmp(ts_node_type(head), "call") != 0 || ts_node_child_count(head) == 0) {
985+
return;
986+
}
987+
TSNode name_node = ts_node_child(head, 0);
988+
if (ts_node_is_null(name_node) || strcmp(ts_node_type(name_node), "identifier") != 0) {
934989
return;
935990
}
936991
char *name = cbm_node_text(ctx->arena, name_node, ctx->source);
937992
if (!name || strcmp(name, "handle_in") != 0) {
938993
return;
939994
}
940995
/* First parameter is the event name pattern */
941-
TSNode params = ts_node_child_by_field_name(func_def, TS_FIELD("parameters"));
996+
TSNode params = elixir_channel_call_args(head);
942997
if (ts_node_is_null(params)) {
943998
return;
944999
}
@@ -952,7 +1007,7 @@ static void elixir_process_function_def(CBMExtractCtx *ctx, TSNode func_def) {
9521007
val = literal_from_first_child(ctx, first_param);
9531008
}
9541009
if (val) {
955-
push_channel(ctx, val, "phoenix_channel", CBM_CHANNEL_LISTEN, func_def);
1010+
push_channel(ctx, val, "phoenix_channel", CBM_CHANNEL_LISTEN, def_call);
9561011
}
9571012
}
9581013

@@ -964,10 +1019,18 @@ static void extract_channels_elixir(CBMExtractCtx *ctx) {
9641019
while (stack.count > 0) {
9651020
TSNode node = ts_nstack_pop(&stack);
9661021
const char *kind = ts_node_type(node);
1022+
/* Everything in Elixir is a `call`, including `def`. The old dispatch
1023+
* tested for a "def" node type the grammar does not have, so the
1024+
* handle_in listener branch was unreachable. */
9671025
if (strcmp(kind, "call") == 0) {
968-
elixir_process_call(ctx, node);
969-
} else if (strcmp(kind, "def") == 0) {
970-
elixir_process_function_def(ctx, node);
1026+
TSNode target = elixir_channel_call_target(node);
1027+
char *macro =
1028+
ts_node_is_null(target) ? NULL : cbm_node_text(ctx->arena, target, ctx->source);
1029+
if (elixir_channel_is_def_macro(macro)) {
1030+
elixir_process_function_def(ctx, node);
1031+
} else {
1032+
elixir_process_call(ctx, node);
1033+
}
9711034
}
9721035
uint32_t count = ts_node_child_count(node);
9731036
for (int i = (int)count - SKIP_ONE; i >= 0; i--) {

‎tests/test_extraction.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,54 @@ TEST(elixir_function) {
10051005
PASS();
10061006
}
10071007

1008+
/* True when a channel with this name/transport/direction was extracted. */
1009+
static int has_channel(CBMFileResult *r, const char *name, const char *transport,
1010+
CBMChannelDirection dir) {
1011+
for (int i = 0; i < r->channels.count; i++) {
1012+
const CBMChannel *c = &r->channels.items[i];
1013+
if (c->channel_name && strcmp(c->channel_name, name) == 0 && c->transport &&
1014+
strcmp(c->transport, transport) == 0 && c->direction == dir)
1015+
return 1;
1016+
}
1017+
return 0;
1018+
}
1019+
1020+
/* Phoenix pub/sub and channel listeners. Both branches were unreachable: the
1021+
* emit side looked up an "arguments" FIELD that tree-sitter-elixir does not
1022+
* define, and the listener side dispatched on a "def" NODE TYPE that does not
1023+
* exist (Elixir defs are `call` nodes), so a Phoenix app produced no channels
1024+
* at all. */
1025+
TEST(elixir_channels_pubsub_and_handle_in) {
1026+
CBMFileResult *r =
1027+
extract("defmodule Acme.Notifier do\n"
1028+
" def announce(p) do\n"
1029+
" Phoenix.PubSub.broadcast(Acme.PubSub, \"user:created\", p)\n"
1030+
" end\n"
1031+
" def watch, do: Phoenix.PubSub.subscribe(Acme.PubSub, \"user:updated\")\n"
1032+
"end\n"
1033+
"defmodule AcmeWeb.RoomChannel do\n"
1034+
" def handle_in(\"new_msg\", payload, socket) do\n"
1035+
" push(socket, \"pong\", %{})\n"
1036+
" {:noreply, socket}\n"
1037+
" end\n"
1038+
" def handle_in(\"ping\", _p, socket) when is_map(socket) do\n"
1039+
" {:noreply, socket}\n"
1040+
" end\n"
1041+
"end\n",
1042+
CBM_LANG_ELIXIR, "t", "chan.ex");
1043+
ASSERT_NOT_NULL(r);
1044+
ASSERT_FALSE(r->has_error);
1045+
ASSERT(has_channel(r, "user:created", "phoenix_pubsub", CBM_CHANNEL_EMIT));
1046+
ASSERT(has_channel(r, "user:updated", "phoenix_pubsub", CBM_CHANNEL_LISTEN));
1047+
/* handle_in clauses are listeners — including the guarded one. */
1048+
ASSERT(has_channel(r, "new_msg", "phoenix_channel", CBM_CHANNEL_LISTEN));
1049+
ASSERT(has_channel(r, "ping", "phoenix_channel", CBM_CHANNEL_LISTEN));
1050+
/* A bare push/broadcast inside the channel module is an emit. */
1051+
ASSERT(has_channel(r, "pong", "phoenix_channel", CBM_CHANNEL_EMIT));
1052+
cbm_free_result(r);
1053+
PASS();
1054+
}
1055+
10081056
/* --- Haskell --- */
10091057
TEST(haskell_function) {
10101058
CBMFileResult *r = extract("add :: Int -> Int -> Int\nadd x y = x + y\n\nmultiply :: Int -> "
@@ -5432,6 +5480,7 @@ SUITE(extraction) {
54325480

54335481
/* Functional */
54345482
RUN_TEST(elixir_function);
5483+
RUN_TEST(elixir_channels_pubsub_and_handle_in);
54355484
RUN_TEST(haskell_function);
54365485
RUN_TEST(ocaml_function);
54375486
RUN_TEST(erlang_function);

0 commit comments

Comments
 (0)