From c8c980e7426a6d64aa4b4162c1dca8962fd90d5e Mon Sep 17 00:00:00 2001 From: Gary Miller Date: Thu, 20 Apr 2023 20:22:30 +1000 Subject: [PATCH] check len type params --- rust/compiler/src/cli/tsgen/generate.rs | 40 +++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/rust/compiler/src/cli/tsgen/generate.rs b/rust/compiler/src/cli/tsgen/generate.rs index fcb5f0df..7364b751 100644 --- a/rust/compiler/src/cli/tsgen/generate.rs +++ b/rust/compiler/src/cli/tsgen/generate.rs @@ -14,6 +14,7 @@ use crate::adlgen::sys::adlast2::{ }; use crate::parser::docstring_scoped_name; use crate::processing::resolver::Resolver; +use crate::utils::ast::get_type_params; use super::utils::{get_npm_pkg, npm_pkg_import, rel_import}; @@ -317,13 +318,48 @@ impl TsGenVisitor<'_> { /// returns (has_make_function,ts type) fn rust_type(&mut self, te: &TypeExpr) -> Result<(bool, String), String> { match &te.type_ref { - TypeRef::ScopedName(n) => self.tstype_from_scoped_name(n, &te.parameters), - TypeRef::LocalName(n) => self.tstype_from_local_name(n, &te.parameters), + TypeRef::ScopedName(n) => { + self.check_type_params_len(n, &te.parameters)?; + self.tstype_from_scoped_name(n, &te.parameters) + } + TypeRef::LocalName(n) => { + let sn = &ScopedName { + module_name: self.module.name.to_string(), + name: n.to_string(), + }; + self.check_type_params_len(sn, &te.parameters)?; + self.tstype_from_local_name(n, &te.parameters) + } TypeRef::Primitive(n) => self.tstype_from_prim(n, &te.parameters), TypeRef::TypeParam(n) => Ok((true, n.clone())), } } + fn check_type_params_len( + &mut self, + scoped_name: &ScopedName, + params: &Vec>, + ) -> Result<(), String> { + if let Some(decl) = self.resolver.get_decl(scoped_name) { + let type_params = get_type_params(&decl); + if type_params.len() != params.len() { + return Err(format!( + "Mismatch number of type params. Need {} received {}. For {}.{}", + type_params.len(), + params.len(), + scoped_name.module_name, + scoped_name.name + )); + } + } else { + return Err(format!( + "Can't resolve decl {}.{}", + scoped_name.module_name, scoped_name.name + )); + } + Ok(()) + } + fn tstype_from_scoped_name( &mut self, scoped_name: &ScopedName,