Fix formatter for unary operation

Was wrongly converting any unary operation into '!'
This commit is contained in:
KtorZ 2022-12-28 17:51:00 +01:00
parent 013fe886f5
commit bae8267f18
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 25 additions and 5 deletions

View File

@ -7,7 +7,7 @@ use crate::{
ast::{ ast::{
Annotation, Arg, ArgName, AssignmentKind, BinOp, CallArg, ClauseGuard, Constant, DataType, Annotation, Arg, ArgName, AssignmentKind, BinOp, CallArg, ClauseGuard, Constant, DataType,
Definition, Function, IfBranch, ModuleConstant, Pattern, RecordConstructor, Definition, Function, IfBranch, ModuleConstant, Pattern, RecordConstructor,
RecordConstructorArg, RecordUpdateSpread, Span, TypeAlias, TypedArg, TypedConstant, RecordConstructorArg, RecordUpdateSpread, Span, TypeAlias, TypedArg, TypedConstant, UnOp,
UnqualifiedImport, UntypedArg, UntypedClause, UntypedClauseGuard, UntypedDefinition, UnqualifiedImport, UntypedArg, UntypedClause, UntypedClauseGuard, UntypedDefinition,
UntypedModule, UntypedPattern, UntypedRecordUpdateArg, Use, CAPTURE_VARIABLE, UntypedModule, UntypedPattern, UntypedRecordUpdateArg, Use, CAPTURE_VARIABLE,
}, },
@ -695,7 +695,7 @@ impl<'comments> Formatter<'comments> {
UntypedExpr::Var { name, .. } => name.to_doc(), UntypedExpr::Var { name, .. } => name.to_doc(),
UntypedExpr::UnOp { value, .. } => self.negate(value), UntypedExpr::UnOp { value, op, .. } => self.un_op(value, op),
UntypedExpr::Fn { UntypedExpr::Fn {
is_capture: true, is_capture: true,
@ -1603,8 +1603,11 @@ impl<'comments> Formatter<'comments> {
} }
} }
fn negate<'a>(&mut self, value: &'a UntypedExpr) -> Document<'a> { fn un_op<'a>(&mut self, value: &'a UntypedExpr, op: &'a UnOp) -> Document<'a> {
docvec!["!", self.wrap_expr(value)] match op {
UnOp::Not => docvec!["!", self.wrap_expr(value)],
UnOp::Negate => docvec!["-", self.wrap_expr(value)],
}
} }
} }

View File

@ -14,7 +14,7 @@ fn assert_fmt(src: &str, expected: &str) {
let (module2, extra2) = parser::module(&out, ModuleKind::Lib).unwrap(); let (module2, extra2) = parser::module(&out, ModuleKind::Lib).unwrap();
let mut out2 = String::new(); let mut out2 = String::new();
format::pretty(&mut out2, module2, extra2, &out); format::pretty(&mut out2, module2, extra2, &out);
assert_eq!(out, out2); assert!(out == out2, "formatting isn't idempotent");
} }
#[test] #[test]
@ -197,3 +197,20 @@ fn test_format_imports() {
assert_fmt(src, expected) assert_fmt(src, expected)
} }
#[test]
fn test_negate() {
let src = indoc! {r#"
fn foo() {
- 42
}
"#};
let expected = indoc! {r#"
fn foo() {
-42
}
"#};
assert_fmt(src, expected)
}