summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2025-02-06 22:10:43 +0100
committerGitHub <noreply@github.com>2025-02-06 21:10:43 +0000
commite4f8e57c534db8a31d51e0342c46b913a7e22422 (patch)
treeb9fefb621c2c96d0dea52ed245b7965da9c20773
parenta1c73b41b862eb95f609f18ee99bdb6da039f478 (diff)
Fix unnecessary import rename warning (#5828)
-rw-r--r--crates/typst-eval/src/import.rs6
-rw-r--r--tests/suite/scripting/import.typ8
2 files changed, 7 insertions, 7 deletions
diff --git a/crates/typst-eval/src/import.rs b/crates/typst-eval/src/import.rs
index 27b06af4..1b164148 100644
--- a/crates/typst-eval/src/import.rs
+++ b/crates/typst-eval/src/import.rs
@@ -44,11 +44,10 @@ impl Eval for ast::ModuleImport<'_> {
}
// If there is a rename, import the source itself under that name.
- let bare_name = self.bare_name();
let new_name = self.new_name();
if let Some(new_name) = new_name {
- if let Ok(source_name) = &bare_name {
- if source_name == new_name.as_str() {
+ if let ast::Expr::Ident(ident) = self.source() {
+ if ident.as_str() == new_name.as_str() {
// Warn on `import x as x`
vm.engine.sink.warn(warning!(
new_name.span(),
@@ -57,6 +56,7 @@ impl Eval for ast::ModuleImport<'_> {
}
}
+ // Define renamed module on the scope.
vm.define(new_name, source.clone());
}
diff --git a/tests/suite/scripting/import.typ b/tests/suite/scripting/import.typ
index 03e2efc6..49b66ee5 100644
--- a/tests/suite/scripting/import.typ
+++ b/tests/suite/scripting/import.typ
@@ -255,6 +255,10 @@
// Warning: 17-21 unnecessary import rename to same name
#import enum as enum
+--- import-rename-necessary ---
+#import "module.typ" as module: a
+#test(module.a, a)
+
--- import-rename-unnecessary-mixed ---
// Warning: 17-21 unnecessary import rename to same name
#import enum as enum: item
@@ -263,10 +267,6 @@
// Warning: 31-35 unnecessary import rename to same name
#import enum as enum: item as item
---- import-item-rename-unnecessary-string ---
-// Warning: 25-31 unnecessary import rename to same name
-#import "module.typ" as module
-
--- import-item-rename-unnecessary-but-ok ---
#import "modul" + "e.typ" as module
#test(module.b, 1)