Skip to content

Commit

Permalink
check len type params
Browse files Browse the repository at this point in the history
  • Loading branch information
millergarym committed Apr 20, 2023
1 parent cd27893 commit c8c980e
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions rust/compiler/src/cli/tsgen/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -317,13 +318,48 @@ impl TsGenVisitor<'_> {
/// returns (has_make_function,ts type)
fn rust_type(&mut self, te: &TypeExpr<TypeRef>) -> 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<TypeExpr<TypeRef>>,
) -> 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,
Expand Down

0 comments on commit c8c980e

Please sign in to comment.