From a517a08d2fc50706654bb649db496b21b47f421b Mon Sep 17 00:00:00 2001 From: T0mstone Date: Sat, 22 Mar 2025 22:31:51 +0100 Subject: [PATCH] Warn on last show/set rule in block --- crates/typst-eval/src/code.rs | 13 +++++++++++++ tests/suite/styling/in-block.typ | 12 ++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/suite/styling/in-block.typ diff --git a/crates/typst-eval/src/code.rs b/crates/typst-eval/src/code.rs index 9078418e4..56c96a071 100644 --- a/crates/typst-eval/src/code.rs +++ b/crates/typst-eval/src/code.rs @@ -285,6 +285,19 @@ impl Eval for ast::CodeBlock<'_> { fn eval(self, vm: &mut Vm) -> SourceResult { vm.scopes.enter(); + if let Some((span, kind)) = + self.body().exprs().next_back().and_then(|x| match x { + ast::Expr::ShowRule(_) => Some((x.span(), "show")), + ast::Expr::SetRule(_) => Some((x.span(), "set")), + _ => None, + }) + { + vm.engine.sink.warn(typst_library::diag::warning!( + span, + "{kind} rule has no effect"; + hint: "See https://typst.app/docs/tutorial/making-a-template/#set-and-show-rules" + )); + } let output = self.body().eval(vm)?; vm.scopes.exit(); Ok(output) diff --git a/tests/suite/styling/in-block.typ b/tests/suite/styling/in-block.typ new file mode 100644 index 000000000..e13a2d3a8 --- /dev/null +++ b/tests/suite/styling/in-block.typ @@ -0,0 +1,12 @@ +--- warn-show-set-last-in-block --- +#{ +// Warning: 1-14 show rule has no effect +// Hint: 1-14 See https://typst.app/docs/tutorial/making-a-template/#set-and-show-rules +show "a": "b" +} + +#{ +// Warning: 1-15 set rule has no effect +// Hint: 1-15 See https://typst.app/docs/tutorial/making-a-template/#set-and-show-rules +set text(blue) +}