mirror of
https://github.com/mtshiba/pylyzer.git
synced 2025-08-04 14:28:24 +00:00
fix: module resolution bug
This commit is contained in:
parent
83d59d4c53
commit
7419411243
4 changed files with 32 additions and 6 deletions
|
@ -78,6 +78,7 @@ fn escape_name(name: String) -> String {
|
|||
"str" => "Str".into(),
|
||||
"bool" => "Bool".into(),
|
||||
"list" => "GenericArray".into(),
|
||||
"bytes" => "Bytes".into(),
|
||||
// "range" => "GenericRange".into(),
|
||||
"dict" => "GenericDict".into(),
|
||||
"set" => "GenericSet".into(),
|
||||
|
|
|
@ -56,17 +56,34 @@ pub fn gen_decl_er(input: &Input, hir: HIR, status: CheckStatus) -> DeclFile {
|
|||
fn gen_chunk_decl(namespace: &str, chunk: Expr, code: &mut String) {
|
||||
match chunk {
|
||||
Expr::Def(def) => {
|
||||
let name = def.sig.ident().inspect().replace('\0', "");
|
||||
if name.starts_with('%') {
|
||||
return;
|
||||
}
|
||||
let mut name = def
|
||||
.sig
|
||||
.ident()
|
||||
.inspect()
|
||||
.replace('\0', "")
|
||||
.replace('%', "___");
|
||||
let typ = def.sig.ident().ref_t().to_string();
|
||||
let typ = escape_type(typ);
|
||||
let decl = format!("{namespace}.{name}: {typ}");
|
||||
// Erg can automatically import nested modules
|
||||
// `import http.client` => `http = pyimport "http"`
|
||||
let decl = if def.sig.ident().ref_t().is_py_module() {
|
||||
name = name.split('.').next().unwrap().to_string();
|
||||
format!(
|
||||
"{namespace}.{name} = pyimport {}",
|
||||
def.sig.ident().ref_t().typarams()[0]
|
||||
)
|
||||
} else {
|
||||
format!("{namespace}.{name}: {typ}")
|
||||
};
|
||||
*code += &decl;
|
||||
}
|
||||
Expr::ClassDef(def) => {
|
||||
let class_name = def.sig.ident().inspect().replace('\0', "");
|
||||
let class_name = def
|
||||
.sig
|
||||
.ident()
|
||||
.inspect()
|
||||
.replace('\0', "")
|
||||
.replace('%', "___");
|
||||
let namespace = format!("{namespace}.{class_name}");
|
||||
let decl = format!(".{class_name}: ClassType");
|
||||
*code += &decl;
|
||||
|
@ -103,6 +120,11 @@ fn gen_chunk_decl(namespace: &str, chunk: Expr, code: &mut String) {
|
|||
gen_chunk_decl(&namespace, attr, code);
|
||||
}
|
||||
}
|
||||
Expr::Dummy(dummy) => {
|
||||
for chunk in dummy.into_iter() {
|
||||
gen_chunk_decl(namespace, chunk, code);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
code.push('\n');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue