mirror of
https://github.com/typst/typst
synced 2025-05-15 17:45:27 +08:00
Better error message for named instead of positional argument
This commit is contained in:
parent
b5ef789315
commit
2007f30b11
@ -2,7 +2,7 @@ use std::fmt::{self, Debug, Formatter};
|
|||||||
|
|
||||||
use ecow::{eco_format, eco_vec, EcoString, EcoVec};
|
use ecow::{eco_format, eco_vec, EcoString, EcoVec};
|
||||||
|
|
||||||
use crate::diag::{bail, At, SourceDiagnostic, SourceResult};
|
use crate::diag::{bail, error, At, SourceDiagnostic, SourceResult};
|
||||||
use crate::foundations::{
|
use crate::foundations::{
|
||||||
func, repr, scope, ty, Array, Dict, FromValue, IntoValue, Repr, Str, Value,
|
func, repr, scope, ty, Array, Dict, FromValue, IntoValue, Repr, Str, Value,
|
||||||
};
|
};
|
||||||
@ -121,10 +121,26 @@ impl Args {
|
|||||||
{
|
{
|
||||||
match self.eat()? {
|
match self.eat()? {
|
||||||
Some(v) => Ok(v),
|
Some(v) => Ok(v),
|
||||||
None => bail!(self.span, "missing argument: {what}"),
|
None => bail!(self.missing_argument(what)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The error message for missing arguments.
|
||||||
|
fn missing_argument(&self, what: &str) -> SourceDiagnostic {
|
||||||
|
for item in &self.items {
|
||||||
|
let Some(name) = item.name.as_deref() else { continue };
|
||||||
|
if name == what {
|
||||||
|
return error!(
|
||||||
|
item.span,
|
||||||
|
"the argument `{what}` is positional";
|
||||||
|
hint: "try removing `{}:`", name,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
error!(self.span, "missing argument: {what}")
|
||||||
|
}
|
||||||
|
|
||||||
/// Find and consume the first castable positional argument.
|
/// Find and consume the first castable positional argument.
|
||||||
pub fn find<T>(&mut self) -> SourceResult<Option<T>>
|
pub fn find<T>(&mut self) -> SourceResult<Option<T>>
|
||||||
where
|
where
|
||||||
|
@ -47,6 +47,11 @@
|
|||||||
// Error: 26-30 duplicate argument: font
|
// Error: 26-30 duplicate argument: font
|
||||||
#set text(font: "Arial", font: "Helvetica")
|
#set text(font: "Arial", font: "Helvetica")
|
||||||
|
|
||||||
|
---
|
||||||
|
// Error: 4-15 the argument `amount` is positional
|
||||||
|
// Hint: 4-15 try removing `amount:`
|
||||||
|
#h(amount: 0.5)
|
||||||
|
|
||||||
---
|
---
|
||||||
// Error: 2-6 expected function, found boolean
|
// Error: 2-6 expected function, found boolean
|
||||||
#true()
|
#true()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user