Warn on last show/set rule in block

This commit is contained in:
T0mstone 2025-03-22 22:31:51 +01:00
parent 1b2714e1a7
commit a517a08d2f
2 changed files with 25 additions and 0 deletions

View File

@ -285,6 +285,19 @@ impl Eval for ast::CodeBlock<'_> {
fn eval(self, vm: &mut Vm) -> SourceResult<Self::Output> { fn eval(self, vm: &mut Vm) -> SourceResult<Self::Output> {
vm.scopes.enter(); 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)?; let output = self.body().eval(vm)?;
vm.scopes.exit(); vm.scopes.exit();
Ok(output) Ok(output)

View File

@ -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)
}