Skip to content

Commit 2a2be4a

Browse files
committed
fix(ts): resolve inferred imported constructor receivers
Signed-off-by: mikemikimike <[email protected]>
1 parent 59a05eb commit 2a2be4a

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

‎internal/cbm/lsp/ts_lsp.c‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,10 +2176,19 @@ const CBMType *ts_eval_expr_type(TSLSPContext *ctx, TSNode node) {
21762176
if (!ts_node_is_null(ctor)) {
21772177
char *cname = node_text(ctx, ctor);
21782178
if (cname) {
2179-
// Bare class name → qualify against module.
2180-
if (strchr(cname, '.') == NULL && ctx->module_qn) {
2181-
const char *qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, cname);
2182-
result = cbm_type_named(ctx->arena, qn);
2179+
// Resolve lexical/import bindings before falling back to the
2180+
// current module. This preserves the imported class QN for
2181+
// inferred locals such as `const x = new ImportedClass()`.
2182+
if (strchr(cname, '.') == NULL) {
2183+
const CBMType *bound = type_of_identifier(ctx, cname);
2184+
if (bound && bound->kind == CBM_TYPE_NAMED &&
2185+
bound->data.named.qualified_name) {
2186+
result = bound;
2187+
} else if (ctx->module_qn) {
2188+
const char *qn =
2189+
cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, cname);
2190+
result = cbm_type_named(ctx->arena, qn);
2191+
}
21832192
} else {
21842193
result = cbm_type_named(ctx->arena, cname);
21852194
}

‎tests/repro/repro_lsp_ts.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,23 @@ static const RFile kTsImport[] = {
297297
"function caller(v: number): number { return helper(v); }\n"},
298298
};
299299

300+
/* An imported class constructed through an inferred local variable must retain
301+
* the imported class QN for subsequent member dispatch. The explicit type
302+
* annotation path already covers this shape; this fixture isolates inference.
303+
*/
304+
static const RFile kTsInferredImportedMethod[] = {
305+
{"store.ts",
306+
"export class WidgetStore {\n"
307+
" findById(id: string): string { return id; }\n"
308+
"}\n"},
309+
{"service.ts",
310+
"import { WidgetStore } from \"./store\";\n"
311+
"function load(id: string): string {\n"
312+
" const store = new WidgetStore();\n"
313+
" return store.findById(id);\n"
314+
"}\n"},
315+
};
316+
300317
/* lsp_ts_jsx — <Comp/> JSX element whose tag is a module-local component
301318
* function (ts_lsp.c:2643-2647). TSX only (jsx_mode); the tag's first letter is
302319
* uppercase so it is NOT treated as an intrinsic HTML element; it resolves via
@@ -370,6 +387,14 @@ TEST(repro_lsp_ts_import) {
370387
"lsp_ts_import");
371388
}
372389

390+
TEST(repro_lsp_ts_inferred_import_receiver) {
391+
return assert_lsp_strategy_files(
392+
kTsInferredImportedMethod,
393+
(int)(sizeof(kTsInferredImportedMethod) /
394+
sizeof(kTsInferredImportedMethod[0])),
395+
"lsp_ts_method");
396+
}
397+
373398
TEST(repro_lsp_ts_jsx) {
374399
return assert_lsp_strategy("app.tsx", kTsxJsx, "lsp_ts_jsx");
375400
}
@@ -403,6 +428,7 @@ SUITE(repro_lsp_ts) {
403428
RUN_TEST(repro_lsp_ts_method);
404429
RUN_TEST(repro_lsp_ts_namespace);
405430
RUN_TEST(repro_lsp_ts_import);
431+
RUN_TEST(repro_lsp_ts_inferred_import_receiver);
406432
RUN_TEST(repro_lsp_ts_jsx);
407433
RUN_TEST(repro_lsp_ts_jsx_import);
408434
RUN_TEST(repro_lsp_ts_default);

0 commit comments

Comments
 (0)