From 99041899109bb9f9f5973824251d0bb9106abcbd Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Thu, 20 Feb 2025 00:50:10 +0800 Subject: [PATCH] fix: the path to join is shadowed by a local variable (#1322) (#1335) * fix: the path to join is shadowed by a local variable * fix: redact target uri --- crates/tinymist-query/src/document_link.rs | 16 ++++++++++++++++ .../src/fixtures/document_link/json_path.typ | 4 ++++ .../src/fixtures/document_link/read_path.typ | 3 +++ .../document_link/snaps/test@json_path.typ.snap | 11 +++++++++++ .../document_link/snaps/test@read_path.typ.snap | 11 +++++++++++ crates/tinymist-query/src/tests.rs | 3 ++- crates/tinymist-vfs/src/path_mapper.rs | 3 ++- 7 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 crates/tinymist-query/src/fixtures/document_link/json_path.typ create mode 100644 crates/tinymist-query/src/fixtures/document_link/read_path.typ create mode 100644 crates/tinymist-query/src/fixtures/document_link/snaps/test@json_path.typ.snap create mode 100644 crates/tinymist-query/src/fixtures/document_link/snaps/test@read_path.typ.snap diff --git a/crates/tinymist-query/src/document_link.rs b/crates/tinymist-query/src/document_link.rs index 971f2c9c..cf50f45d 100644 --- a/crates/tinymist-query/src/document_link.rs +++ b/crates/tinymist-query/src/document_link.rs @@ -37,3 +37,19 @@ impl SemanticRequest for DocumentLinkRequest { Some(links.collect()) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::tests::*; + + #[test] + fn test() { + snapshot_testing("document_link", &|ctx, path| { + let request = DocumentLinkRequest { path: path.clone() }; + + let result = request.request(ctx); + assert_snapshot!(JsonRepr::new_redacted(result, &REDACT_LOC)); + }); + } +} diff --git a/crates/tinymist-query/src/fixtures/document_link/json_path.typ b/crates/tinymist-query/src/fixtures/document_link/json_path.typ new file mode 100644 index 00000000..26a6b552 --- /dev/null +++ b/crates/tinymist-query/src/fixtures/document_link/json_path.typ @@ -0,0 +1,4 @@ +/// path: base.json +{} +----- +#json(/* position after */ "base.json"); diff --git a/crates/tinymist-query/src/fixtures/document_link/read_path.typ b/crates/tinymist-query/src/fixtures/document_link/read_path.typ new file mode 100644 index 00000000..f1190ed5 --- /dev/null +++ b/crates/tinymist-query/src/fixtures/document_link/read_path.typ @@ -0,0 +1,3 @@ +/// path: base.typ +----- +#read(/* position after */ "base.typ"); diff --git a/crates/tinymist-query/src/fixtures/document_link/snaps/test@json_path.typ.snap b/crates/tinymist-query/src/fixtures/document_link/snaps/test@json_path.typ.snap new file mode 100644 index 00000000..1d02839e --- /dev/null +++ b/crates/tinymist-query/src/fixtures/document_link/snaps/test@json_path.typ.snap @@ -0,0 +1,11 @@ +--- +source: crates/tinymist-query/src/document_link.rs +expression: "JsonRepr::new_redacted(result, &REDACT_LOC)" +input_file: crates/tinymist-query/src/fixtures/document_link/json_path.typ +--- +[ + { + "range": "0:28:0:37", + "target": "base.json" + } +] diff --git a/crates/tinymist-query/src/fixtures/document_link/snaps/test@read_path.typ.snap b/crates/tinymist-query/src/fixtures/document_link/snaps/test@read_path.typ.snap new file mode 100644 index 00000000..13a1ead0 --- /dev/null +++ b/crates/tinymist-query/src/fixtures/document_link/snaps/test@read_path.typ.snap @@ -0,0 +1,11 @@ +--- +source: crates/tinymist-query/src/document_link.rs +expression: "JsonRepr::new_redacted(result, &REDACT_LOC)" +input_file: crates/tinymist-query/src/fixtures/document_link/read_path.typ +--- +[ + { + "range": "0:28:0:36", + "target": "base.typ" + } +] diff --git a/crates/tinymist-query/src/tests.rs b/crates/tinymist-query/src/tests.rs index cbdc03e3..a187268f 100644 --- a/crates/tinymist-query/src/tests.rs +++ b/crates/tinymist-query/src/tests.rs @@ -344,6 +344,7 @@ pub static REDACT_LOC: Lazy = Lazy::new(|| { "targetRange", "targetSelectionRange", "originSelectionRange", + "target", "targetUri", ]) }); @@ -416,7 +417,7 @@ impl Redact for RedactFields { ), ); } - "uri" | "oldUri" | "newUri" | "targetUri" => { + "uri" | "target" | "oldUri" | "newUri" | "targetUri" => { map.insert(key.to_owned(), file_name(t.as_str().unwrap()).into()); } "range" diff --git a/crates/tinymist-vfs/src/path_mapper.rs b/crates/tinymist-vfs/src/path_mapper.rs index f805f831..fb6069f7 100644 --- a/crates/tinymist-vfs/src/path_mapper.rs +++ b/crates/tinymist-vfs/src/path_mapper.rs @@ -17,6 +17,7 @@ use typst::syntax::VirtualPath; use super::TypstFileId; +#[derive(Debug)] pub enum PathResolution { Resolved(PathBuf), Rootless(Cow<'static, VirtualPath>), @@ -39,7 +40,7 @@ impl PathResolution { pub fn join(&self, path: &str) -> FileResult { match self { - PathResolution::Resolved(path) => Ok(PathResolution::Resolved(path.join(path))), + PathResolution::Resolved(root) => Ok(PathResolution::Resolved(root.join(path))), PathResolution::Rootless(root) => { Ok(PathResolution::Rootless(Cow::Owned(root.join(path)))) }