Skip to content

Commit 381a8eb

Browse files
committed
[FIX] missing ImportError on Import statement without from or asname
1 parent 2c0abe5 commit 381a8eb

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

server/src/core/import_resolver.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use super::odoo::SyncOdoo;
2121
use super::symbols::symbol::Symbol;
2222

2323
pub struct ImportResult {
24-
pub name: OYarn,
24+
pub name: OYarn, //the last imported element
25+
pub var_name: OYarn, // the effective symbol name (asname, or first part in a import A.B.C)
2526
pub found: bool,
2627
pub symbol: Rc<RefCell<Symbol>>,
2728
pub file_tree: Vec<OYarn>, //contains only the first part of a Tree
@@ -108,7 +109,8 @@ pub fn resolve_import_stmt(session: &mut SessionInfo, source_file_symbol: &Rc<Re
108109
let mut result = vec![];
109110
for alias in name_aliases {
110111
result.push(ImportResult{
111-
name: OYarn::from(alias.asname.as_ref().unwrap_or(&alias.name).to_string()),
112+
name: OYarn::from(alias.name.as_ref().to_string()),
113+
var_name: OYarn::from(alias.asname.as_ref().unwrap_or(&alias.name).to_string()),
112114
found: false,
113115
symbol: fallback_sym.as_ref().unwrap().clone(),
114116
file_tree: file_tree.clone(),
@@ -163,8 +165,8 @@ pub fn resolve_import_stmt(session: &mut SessionInfo, source_file_symbol: &Rc<Re
163165
// In all "from X import A" case, it simply means search for A
164166
// But in "import A.B.C", it means search for A only, and import B.C
165167
// If user typed import A.B.C as D, we will search for A.B.C to link it to symbol D,
166-
result[name_index as usize].name = name.split(".").map(|s| oyarn!("{}", s)).next().unwrap();
167-
result[name_index as usize].found = true;
168+
result[name_index as usize].var_name = name.split(".").map(|s| oyarn!("{}", s)).next().unwrap();
169+
//result[name_index as usize].found = true; //even if found at this stage, we want to check everything anyway for diagnostics. But if found, we'll keep this symbol as imported
168170
result[name_index as usize].symbol = next_symbol.as_ref().unwrap().clone();
169171
}
170172
if !name_middle_part.is_empty() {
@@ -197,15 +199,19 @@ pub fn resolve_import_stmt(session: &mut SessionInfo, source_file_symbol: &Rc<Re
197199
//TODO what if multiple values?
198200
let ns = next_symbol.as_ref().unwrap().borrow().get_symbol(&(vec![], name_last_name), u32::MAX).get(0).cloned();
199201
last_symbol = ns;
200-
if alias.asname.is_some() && last_symbol.is_none() {
201-
result[name_index as usize].symbol = fallback_sym.as_ref().unwrap_or(&source_root).clone();
202+
if last_symbol.is_none() {
203+
if alias.asname.is_some() {
204+
result[name_index as usize].symbol = fallback_sym.as_ref().unwrap_or(&source_root).clone();
205+
}
202206
continue;
203207
}
204208
}
205209
// we found it ! store the result if not already done
206-
if alias.asname.is_some() && result[name_index as usize].found == false {
210+
if result[name_index as usize].found == false {
207211
result[name_index as usize].found = true;
208-
result[name_index as usize].symbol = last_symbol.as_ref().unwrap().clone();
212+
if alias.asname.is_some() {
213+
result[name_index as usize].symbol = last_symbol.as_ref().unwrap().clone();
214+
}
209215
}
210216
} else {
211217
//everything is ok, let's store the result if not already done

server/src/core/python_arch_eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl PythonArchEval {
393393
&mut Some(&mut self.diagnostics));
394394

395395
for _import_result in import_results.iter() {
396-
let variable = self.sym_stack.last().unwrap().borrow().get_positioned_symbol(&_import_result.name, &_import_result.range);
396+
let variable = self.sym_stack.last().unwrap().borrow().get_positioned_symbol(&_import_result.var_name, &_import_result.range);
397397
let Some(variable) = variable.clone() else {
398398
continue;
399399
};

server/src/features/ast_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ impl AstUtils {
9090
if alias.range().contains(TextSize::new(offset)) {
9191
let mut is_last = false;
9292
let (to_analyze, range) = if alias.name.range().contains(TextSize::new(offset)) {
93-
let next_dot_offset = alias.name.id.as_str()[offset as usize - alias.range().start().to_usize()..].find(".");
93+
let next_dot_offset = alias.name.id.as_str()[offset as usize - alias.name.range().start().to_usize()..].find(".");
9494
if let Some(next_dot_offset) = next_dot_offset {
9595
let end = offset as usize + next_dot_offset;
96-
let text = &alias.name.id.as_str()[..end - alias.range().start().to_usize()];
96+
let text = &alias.name.id.as_str()[..end - alias.name.range().start().to_usize()];
9797
let start_range = text.rfind(".").map(|p| p+1).unwrap_or(0) + alias.name.range().start().to_usize();
9898
(text, TextRange::new(TextSize::new(start_range as u32), TextSize::new(end as u32)))
9999
} else {

0 commit comments

Comments
 (0)