Skip to content

Commit 4c7f32d

Browse files
committed
Assemble ImportedObject using Visitor
The two-step approach is not necessary - we can get the full line contents while visiting the tree.
1 parent e39d9a4 commit 4c7f32d

2 files changed

Lines changed: 14 additions & 50 deletions

File tree

rust/src/import_parsing.rs

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,49 +30,12 @@ impl ImportedObject {
3030
}
3131
}
3232

33-
#[derive(Debug, PartialEq, Eq, Clone)]
34-
struct ImportedObjectWithoutLineContents {
35-
pub name: String,
36-
pub line_number: usize,
37-
pub typechecking_only: bool,
38-
}
39-
40-
impl ImportedObjectWithoutLineContents {
41-
fn new(name: String, line_number: usize, typechecking_only: bool) -> Self {
42-
Self {
43-
name,
44-
line_number,
45-
typechecking_only,
46-
}
47-
}
48-
}
49-
5033
pub fn parse_imports(path: &Path) -> GrimpResult<Vec<ImportedObject>> {
5134
let code = fs::read_to_string(path).expect("failed to read file");
5235
parse_imports_from_code(&code)
5336
}
5437

5538
pub fn parse_imports_from_code(code: &str) -> GrimpResult<Vec<ImportedObject>> {
56-
let imports_without_line_contents = parse_imports_from_code_without_line_contents(code)?;
57-
58-
let lines: Vec<&str> = code.lines().collect();
59-
60-
Ok(imports_without_line_contents
61-
.into_iter()
62-
.map(|i| {
63-
ImportedObject::new(
64-
i.name,
65-
i.line_number,
66-
lines[i.line_number - 1].trim_start().to_string(),
67-
i.typechecking_only,
68-
)
69-
})
70-
.collect())
71-
}
72-
73-
fn parse_imports_from_code_without_line_contents(
74-
code: &str,
75-
) -> GrimpResult<Vec<ImportedObjectWithoutLineContents>> {
7639
let line_index = LineIndex::from_source_text(code);
7740
let source_code = SourceCode::new(code, &line_index);
7841

@@ -99,7 +62,7 @@ fn parse_imports_from_code_without_line_contents(
9962
#[derive(Debug)]
10063
struct Visitor<'a> {
10164
source_code: SourceCode<'a, 'a>,
102-
pub imported_objects: Vec<ImportedObjectWithoutLineContents>,
65+
pub imported_objects: Vec<ImportedObject>,
10366
pub typechecking_only: bool,
10467
}
10568

@@ -119,12 +82,12 @@ impl<'a> StatementVisitor<'a> for Visitor<'a> {
11982
Stmt::Import(import_stmt) => {
12083
let line_number = self.source_code.line_index(import_stmt.range.start());
12184
for name in import_stmt.names.iter() {
122-
self.imported_objects
123-
.push(ImportedObjectWithoutLineContents::new(
124-
name.name.id.clone(),
125-
line_number.get(),
126-
self.typechecking_only,
127-
))
85+
self.imported_objects.push(ImportedObject::new(
86+
name.name.id.clone(),
87+
line_number.get(),
88+
self.source_code.line_text(line_number).trim().to_string(),
89+
self.typechecking_only,
90+
))
12891
}
12992
walk_stmt(self, stmt);
13093
}
@@ -148,12 +111,12 @@ impl<'a> StatementVisitor<'a> for Visitor<'a> {
148111
)
149112
}
150113
};
151-
self.imported_objects
152-
.push(ImportedObjectWithoutLineContents::new(
153-
imported_object_name,
154-
line_number.get(),
155-
self.typechecking_only,
156-
))
114+
self.imported_objects.push(ImportedObject::new(
115+
imported_object_name,
116+
line_number.get(),
117+
self.source_code.line_text(line_number).trim().to_string(),
118+
self.typechecking_only,
119+
))
157120
}
158121
walk_stmt(self, stmt);
159122
}

tests/functional/test_error_handling.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ def test_missing_root_init_file():
2929
),
3030
):
3131
build_graph("missingrootinitpackage", cache_dir=None)
32+

0 commit comments

Comments
 (0)