@@ -1998,6 +1998,22 @@ static const CBMType *ts_new_bare_class_type(TSLSPContext *ctx, const char *cnam
19981998 return cbm_type_named (ctx -> arena , local_qn );
19991999}
20002000
2001+ /* Type of a `new_expression`: a bare constructor name follows TS scoping
2002+ * (ts_new_bare_class_type), a dotted one is taken as spelled. NULL when the
2003+ * node carries no constructor. Shared by expression evaluation and by the
2004+ * field-initializer inference in ast_sweep_shapes, so `const t = new C()`
2005+ * and `private t = new C()` type the instance identically. */
2006+ static const CBMType * ts_new_expression_type (TSLSPContext * ctx , TSNode node ) {
2007+ TSNode ctor = ts_node_child_by_field_name (node , "constructor" , TS_LSP_FIELD_LEN ("constructor" ));
2008+ if (ts_node_is_null (ctor ))
2009+ return NULL ;
2010+ char * cname = node_text (ctx , ctor );
2011+ if (!cname )
2012+ return NULL ;
2013+ return strchr (cname , '.' ) == NULL ? ts_new_bare_class_type (ctx , cname )
2014+ : cbm_type_named (ctx -> arena , cname );
2015+ }
2016+
20012017static const CBMRegisteredFunc * ts_lookup_namespace_call (TSLSPContext * ctx , const char * object_name ,
20022018 const char * method_name , TSNode args ,
20032019 bool * out_is_import ) {
@@ -2223,15 +2239,9 @@ const CBMType *ts_eval_expr_type(TSLSPContext *ctx, TSNode node) {
22232239 result = lookup_member_type (ctx , recv , pname );
22242240 }
22252241 } else if (strcmp (kind , "new_expression" ) == 0 ) {
2226- TSNode ctor =
2227- ts_node_child_by_field_name (node , "constructor" , TS_LSP_FIELD_LEN ("constructor" ));
2228- if (!ts_node_is_null (ctor )) {
2229- char * cname = node_text (ctx , ctor );
2230- if (cname ) {
2231- result = strchr (cname , '.' ) == NULL ? ts_new_bare_class_type (ctx , cname )
2232- : cbm_type_named (ctx -> arena , cname );
2233- }
2234- }
2242+ const CBMType * nt = ts_new_expression_type (ctx , node );
2243+ if (nt )
2244+ result = nt ;
22352245 } else if (strcmp (kind , "call_expression" ) == 0 ) {
22362246 TSNode fn = ts_node_child_by_field_name (node , "function" , TS_LSP_FIELD_LEN ("function" ));
22372247 if (!ts_node_is_null (fn )) {
@@ -5357,6 +5367,76 @@ static const CBMType *interface_method_signature_from_ast(TSLSPContext *ctx, TSN
53575367 return cbm_type_func (ctx -> arena , NULL , param_count ? param_types : NULL , return_types );
53585368}
53595369
5370+ /* Parsed type of a `type_annotation` field (`: T`), or unknown. */
5371+ static const CBMType * ts_annotation_field_type (TSLSPContext * ctx , TSNode owner ) {
5372+ TSNode tann = ts_node_child_by_field_name (owner , "type" , TS_LSP_FIELD_LEN ("type" ));
5373+ if (ts_node_is_null (tann ))
5374+ return cbm_type_unknown ();
5375+ TSNode tn =
5376+ strcmp (ts_node_type (tann ), "type_annotation" ) == 0 ? ts_node_named_child (tann , 0 ) : tann ;
5377+ return ts_node_is_null (tn ) ? cbm_type_unknown () : ts_parse_type_node (ctx , tn );
5378+ }
5379+
5380+ /* Declared type of a class field (`public_field_definition`) or interface
5381+ * property: the annotation when present, else, for an unannotated field
5382+ * initialised with `new C()`, the type of that instance (#514). No other
5383+ * initializer is inferred: `new` names its type exactly, arbitrary
5384+ * expressions do not. */
5385+ static const CBMType * ts_field_decl_type (TSLSPContext * ctx , TSNode m ) {
5386+ if (!ts_node_is_null (ts_node_child_by_field_name (m , "type" , TS_LSP_FIELD_LEN ("type" ))))
5387+ return ts_annotation_field_type (ctx , m );
5388+ TSNode val = ts_node_child_by_field_name (m , "value" , TS_LSP_FIELD_LEN ("value" ));
5389+ if (ts_node_is_null (val ) || strcmp (ts_node_type (val ), "new_expression" ) != 0 )
5390+ return cbm_type_unknown ();
5391+ const CBMType * nt = ts_new_expression_type (ctx , val );
5392+ return nt ? nt : cbm_type_unknown ();
5393+ }
5394+
5395+ /* A constructor parameter is a TS parameter property, i.e. also declares a
5396+ * class field, iff it carries an accessibility modifier, `readonly` or
5397+ * `override`. */
5398+ static bool ts_param_is_property (TSNode p ) {
5399+ uint32_t pc = ts_node_child_count (p );
5400+ for (uint32_t ci = 0 ; ci < pc ; ci ++ ) {
5401+ const char * ck = ts_node_type (ts_node_child (p , ci ));
5402+ if (strcmp (ck , "accessibility_modifier" ) == 0 || strcmp (ck , "readonly" ) == 0 ||
5403+ strcmp (ck , "override_modifier" ) == 0 )
5404+ return true;
5405+ }
5406+ return false;
5407+ }
5408+
5409+ /* Append the parameter properties of a `constructor` method_definition to
5410+ * the field arrays (#514): `constructor(private readonly svc: Svc)` declares
5411+ * field `svc: Svc`, the dominant NestJS injection shape. Any other member is
5412+ * ignored. Returns the new field count. */
5413+ static int ts_collect_param_properties (TSLSPContext * ctx , TSNode m , const char * * names ,
5414+ const CBMType * * types , int count , int cap ) {
5415+ TSNode mname = ts_node_child_by_field_name (m , "name" , TS_LSP_FIELD_LEN ("name" ));
5416+ char * mnm = ts_node_is_null (mname ) ? NULL : node_text (ctx , mname );
5417+ if (!mnm || strcmp (mnm , "constructor" ) != 0 )
5418+ return count ;
5419+ TSNode params = ts_node_child_by_field_name (m , "parameters" , TS_LSP_FIELD_LEN ("parameters" ));
5420+ uint32_t pnc = ts_node_is_null (params ) ? 0 : ts_node_named_child_count (params );
5421+ for (uint32_t pi = 0 ; pi < pnc && count < cap ; pi ++ ) {
5422+ TSNode p = ts_node_named_child (params , pi );
5423+ const char * pk = ts_node_type (p );
5424+ if ((strcmp (pk , "required_parameter" ) != 0 && strcmp (pk , "optional_parameter" ) != 0 ) ||
5425+ !ts_param_is_property (p ))
5426+ continue ;
5427+ TSNode pat = ts_node_child_by_field_name (p , "pattern" , TS_LSP_FIELD_LEN ("pattern" ));
5428+ if (ts_node_is_null (pat ) || strcmp (ts_node_type (pat ), "identifier" ) != 0 )
5429+ continue ;
5430+ char * pnm = node_text (ctx , pat );
5431+ if (!pnm )
5432+ continue ;
5433+ names [count ] = pnm ;
5434+ types [count ] = ts_annotation_field_type (ctx , p );
5435+ count ++ ;
5436+ }
5437+ return count ;
5438+ }
5439+
53605440// AST sweep: walk class/interface bodies to collect field names+types and refine
53615441// embedded_types / type_param_names.
53625442static void ast_sweep_shapes (TSLSPContext * ctx , TSNode root , CBMTypeRegistry * reg ) {
@@ -5469,24 +5549,24 @@ static void ast_sweep_shapes(TSLSPContext *ctx, TSNode root, CBMTypeRegistry *re
54695549 if (strcmp (mk , "public_field_definition" ) == 0 ||
54705550 strcmp (mk , "property_signature" ) == 0 ) {
54715551 TSNode fname = ts_node_child_by_field_name (m , "name" , TS_LSP_FIELD_LEN ("name" ));
5472- TSNode ftype = ts_node_child_by_field_name (m , "type" , TS_LSP_FIELD_LEN ("type" ));
54735552 if (ts_node_is_null (fname ))
54745553 continue ;
54755554 char * fnm = node_text (ctx , fname );
54765555 if (!fnm )
54775556 continue ;
5478- const CBMType * ft = cbm_type_unknown ();
5479- if (!ts_node_is_null (ftype )) {
5480- TSNode tch = ts_node_named_child (ftype , 0 );
5481- if (!ts_node_is_null (tch ))
5482- ft = ts_parse_type_node (ctx , tch );
5483- }
54845557 field_names [field_count ] = fnm ;
5485- field_types [field_count ] = ft ;
5558+ field_types [field_count ] = ts_field_decl_type ( ctx , m ) ;
54865559 field_count ++ ;
54875560 continue ;
54885561 }
54895562
5563+ // Constructor parameter properties are class fields too (#514).
5564+ if (!is_interface && strcmp (mk , "method_definition" ) == 0 ) {
5565+ field_count =
5566+ ts_collect_param_properties (ctx , m , field_names , field_types , field_count , 63 );
5567+ continue ;
5568+ }
5569+
54905570 // Interface method signature: `methodName(params): ReturnType;`
54915571 // Or class method declaration in `.d.ts` ambient mode.
54925572 if (is_interface && (strcmp (mk , "method_signature" ) == 0 )) {
0 commit comments