FEATURE: Improvements to func expression pretty printing.

This commit is contained in:
Jeremy Wall 2019-05-17 20:06:06 -05:00
parent 0d78438c2a
commit 9b99bc026c
2 changed files with 35 additions and 2 deletions

View File

@ -271,8 +271,14 @@ where
}
Expression::Func(_def) => {
self.w.write("func (".as_bytes())?;
for n in _def.argdefs.iter() {
write!(self.w, "{}, ", n.val)?;
if _def.argdefs.len() == 1 {
write!(self.w, "{}", _def.argdefs.first().unwrap())?;
} else {
let mut prefix = "";
for n in _def.argdefs.iter() {
write!(self.w, "{}{}", prefix, n.val)?;
prefix = ", ";
}
}
self.w.write(") => ".as_bytes())?;
self.render_expr(&_def.fields)?;

View File

@ -306,3 +306,30 @@ fn test_module_with_out_expr_printing() {
assert!(printer.err.is_none());
assert_eq!(String::from_utf8(buffer).unwrap(), format!("{}\n", input));
}
#[test]
fn test_func_expr_printing() {
let input = "let f = func (foo, bar) => {
foo = foo,
bar = bar,
};";
let stmts = assert_parse(input);
let mut buffer: Vec<u8> = Vec::new();
let mut printer = AstPrinter::new(2, &mut buffer);
printer.render(&stmts);
assert!(printer.err.is_none());
assert_eq!(String::from_utf8(buffer).unwrap(), format!("{}\n", input));
}
#[test]
fn test_func_expr_single_arg_printing() {
let input = "let f = func (foo) => {
foo = foo,
};";
let stmts = assert_parse(input);
let mut buffer: Vec<u8> = Vec::new();
let mut printer = AstPrinter::new(2, &mut buffer);
printer.render(&stmts);
assert!(printer.err.is_none());
assert_eq!(String::from_utf8(buffer).unwrap(), format!("{}\n", input));
}