Skip to content

Commit 3d19ff8

Browse files
committed
Refactor import checks
1 parent cdfd2bf commit 3d19ff8

1 file changed

Lines changed: 13 additions & 30 deletions

File tree

src/check/imports.rs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,15 @@ use typst::{
88
syntax::{
99
ast::{self, AstNode, ModuleImport},
1010
package::{PackageSpec, PackageVersion, VersionlessPackageSpec},
11-
FileId, VirtualPath,
1211
},
1312
World,
1413
};
1514

16-
use crate::{
17-
check::{label, TryExt},
18-
world::SystemWorld,
19-
};
20-
21-
use super::{Diagnostics, Result};
15+
use crate::check::path::PackagePath;
16+
use crate::check::{label, Diagnostics, Result, TryExt};
17+
use crate::world::SystemWorld;
2218

2319
pub fn check(diags: &mut Diagnostics, package_dir: &Path, world: &SystemWorld) -> Result<()> {
24-
check_dir(diags, package_dir, world)
25-
}
26-
27-
pub fn check_dir(diags: &mut Diagnostics, dir: &Path, world: &SystemWorld) -> Result<()> {
2820
let root_path = world.root();
2921
let main_path = root_path
3022
.join(world.main().vpath().as_rootless_path())
@@ -35,36 +27,27 @@ pub fn check_dir(diags: &mut Diagnostics, dir: &Path, world: &SystemWorld) -> Re
3527
.and_then(|package_dir| package_dir.parent())
3628
.and_then(|namespace_dir| namespace_dir.parent());
3729

38-
for ch in std::fs::read_dir(dir).error("internal/io", "Can't read directory")? {
30+
for ch in ignore::Walk::new(package_dir) {
3931
let Ok(ch) = ch else {
4032
continue;
4133
};
4234
let Ok(meta) = ch.metadata() else {
4335
continue;
4436
};
45-
46-
let path = dir.join(ch.file_name());
47-
if meta.is_dir() {
48-
check_dir(diags, &path, world)?;
37+
if !meta.is_file() {
38+
continue;
4939
}
50-
if path.extension().and_then(|ext| ext.to_str()) == Some("typ") {
51-
let fid = FileId::new(
52-
None,
53-
VirtualPath::new(
54-
path.strip_prefix(root_path)
55-
// Not actually true
56-
.error(
57-
"internal",
58-
"Prefix striping failed even though `path` is built from `root_dir`",
59-
)?,
60-
),
61-
);
62-
let source = world.lookup(fid).error("io", "Can't read source file")?;
40+
41+
let path = PackagePath::from_full(package_dir, ch.path());
42+
if path.extension().is_some_and(|ext| ext == "typ") {
43+
let source = world
44+
.lookup(path.file_id())
45+
.error("io", "Can't read source file")?;
6346
check_ast(
6447
diags,
6548
world,
6649
source.root(),
67-
&path,
50+
path.full(),
6851
main_path.as_deref(),
6952
all_packages,
7053
);

0 commit comments

Comments
 (0)