Process an arrow_function or function_expression as a callback whose param types are contextually typed by the enclosing call (e.g. `arr.map(x => x.length)` — `x` should be bound to `arr`'s element type via Array .map's `(x: T) => U` signature). The caller passes `expected` = the FUNC type the callback must conform to.
| 2950 | // be bound to `arr`'s element type via Array<T>.map's `(x: T) => U` signature). The |
| 2951 | // caller passes `expected` = the FUNC type the callback must conform to. |
| 2952 | static void process_callback_arrow(TSLSPContext *ctx, TSNode arrow, const CBMType *expected) { |
| 2953 | if (ts_node_is_null(arrow) || !expected || expected->kind != CBM_TYPE_FUNC) |
| 2954 | return; |
| 2955 | |
| 2956 | CBMScope *saved = ctx->current_scope; |
| 2957 | ctx->current_scope = cbm_scope_push(ctx->arena, saved); |
| 2958 | |
| 2959 | // Tree-sitter typescript shapes for arrow functions: |
| 2960 | // `(x, y) => body` → parameters: formal_parameters |
| 2961 | // `x => body` → parameter: identifier (singular field) |
| 2962 | TSNode params = |
| 2963 | ts_node_child_by_field_name(arrow, "parameters", TS_LSP_FIELD_LEN("parameters")); |
| 2964 | TSNode bare_param = |
| 2965 | ts_node_child_by_field_name(arrow, "parameter", TS_LSP_FIELD_LEN("parameter")); |
| 2966 | int idx = 0; |
| 2967 | if (!ts_node_is_null(bare_param)) { |
| 2968 | // Single bare param: `x => ...`. |
| 2969 | const char *pk = ts_node_type(bare_param); |
| 2970 | if (strcmp(pk, "identifier") == 0) { |
| 2971 | char *nm = node_text(ctx, bare_param); |
| 2972 | const CBMType *pt = |
| 2973 | (expected->data.func.param_types && expected->data.func.param_types[0]) |
| 2974 | ? expected->data.func.param_types[0] |
| 2975 | : cbm_type_unknown(); |
| 2976 | if (nm) |
| 2977 | cbm_scope_bind(ctx->current_scope, nm, pt); |
| 2978 | } |
| 2979 | } else if (!ts_node_is_null(params)) { |
| 2980 | uint32_t pc = ts_node_named_child_count(params); |
| 2981 | for (uint32_t i = 0; i < pc; i++) { |
| 2982 | TSNode p = ts_node_named_child(params, i); |
| 2983 | if (ts_node_is_null(p)) |
| 2984 | continue; |
| 2985 | const char *pk = ts_node_type(p); |
| 2986 | const char *pname = NULL; |
| 2987 | const CBMType *expected_pt = |
| 2988 | (expected->data.func.param_types && expected->data.func.param_types[idx]) |
| 2989 | ? expected->data.func.param_types[idx] |
| 2990 | : NULL; |
| 2991 | |
| 2992 | if (strcmp(pk, "identifier") == 0) { |
| 2993 | pname = node_text(ctx, p); |
| 2994 | } else { |
| 2995 | // required_parameter / optional_parameter — extract pattern. |
| 2996 | TSNode pp = ts_node_child_by_field_name(p, "pattern", TS_LSP_FIELD_LEN("pattern")); |
| 2997 | if (ts_node_is_null(pp)) { |
| 2998 | pp = ts_node_child_by_field_name(p, "name", TS_LSP_FIELD_LEN("name")); |
| 2999 | } |
| 3000 | if (!ts_node_is_null(pp) && strcmp(ts_node_type(pp), "identifier") == 0) { |
| 3001 | pname = node_text(ctx, pp); |
| 3002 | } |
| 3003 | // If the param has its own annotation, use that. Otherwise use expected. |
| 3004 | TSNode tann = ts_node_child_by_field_name(p, "type", TS_LSP_FIELD_LEN("type")); |
| 3005 | if (!ts_node_is_null(tann)) { |
| 3006 | TSNode tch = (strcmp(ts_node_type(tann), "type_annotation") == 0) |
| 3007 | ? ts_node_named_child(tann, 0) |
| 3008 | : tann; |
| 3009 | if (!ts_node_is_null(tch)) { |
no test coverage detected