@@ -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--) {
0 commit comments