Skip to content

Commit f13b864

Browse files
giacomocavalierilpil
authored andcommitted
make the formatter remove double negations
1 parent 8f2abb9 commit f13b864

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@
107107

108108
### Formatter
109109

110+
- The formatter now removes needless multiple negations that are safe to remove.
111+
For example, this snippet of code:
112+
113+
```gleam
114+
pub fn useless_negations() {
115+
let lucky_number = --11
116+
let lucy_is_a_star = !!!False
117+
}
118+
```
119+
120+
Is rewritten as:
121+
122+
```gleam
123+
pub fn useless_negations() {
124+
let lucky_number = 11
125+
let lucy_is_a_star = !False
126+
}
127+
```
128+
129+
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
130+
110131
### Bug fixes
111132

112133
- Fixed a bug where `echo` could crash on JavaScript if the module contains

compiler-core/src/format.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2521,16 +2521,17 @@ impl<'comments> Formatter<'comments> {
25212521

25222522
fn negate_bool<'a>(&mut self, expr: &'a UntypedExpr) -> Document<'a> {
25232523
match expr {
2524+
UntypedExpr::NegateBool { value, .. } => self.expr(value),
25242525
UntypedExpr::BinOp { .. } => "!".to_doc().append(wrap_block(self.expr(expr))),
25252526
_ => docvec!["!", self.expr(expr)],
25262527
}
25272528
}
25282529

25292530
fn negate_int<'a>(&mut self, expr: &'a UntypedExpr) -> Document<'a> {
25302531
match expr {
2531-
UntypedExpr::BinOp { .. } | UntypedExpr::NegateInt { .. } => {
2532-
"- ".to_doc().append(self.expr(expr))
2533-
}
2532+
UntypedExpr::NegateInt { value, .. } => self.expr(value),
2533+
UntypedExpr::Int { value, .. } if value.starts_with('-') => self.int(value),
2534+
UntypedExpr::BinOp { .. } => "- ".to_doc().append(self.expr(expr)),
25342535

25352536
_ => docvec!["-", self.expr(expr)],
25362537
}

compiler-core/src/format/tests.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4902,7 +4902,7 @@ fn double_negate() {
49024902
"#,
49034903
r#"pub fn main() {
49044904
let a = 3
4905-
let b = - -a
4905+
let b = a
49064906
}
49074907
"#
49084908
);
@@ -4918,7 +4918,7 @@ fn triple_negate() {
49184918
"#,
49194919
r#"pub fn main() {
49204920
let a = 3
4921-
let b = - - -a
4921+
let b = -a
49224922
}
49234923
"#
49244924
);
@@ -4950,14 +4950,32 @@ fn binary_double_negate() {
49504950
"#,
49514951
r#"pub fn main() {
49524952
let a = 3
4953-
let b = - -{ a + 3 }
4953+
let b = { a + 3 }
49544954
}
49554955
"#
49564956
);
49574957
}
49584958

49594959
#[test]
4960-
fn repeated_negate_after_subtract() {
4960+
fn even_repeated_negate_after_subtract() {
4961+
assert_format_rewrite!(
4962+
r#"pub fn main() {
4963+
let a = 3
4964+
let b = 4
4965+
let c = a-------b
4966+
}
4967+
"#,
4968+
r#"pub fn main() {
4969+
let a = 3
4970+
let b = 4
4971+
let c = a - b
4972+
}
4973+
"#
4974+
);
4975+
}
4976+
4977+
#[test]
4978+
fn odd_repeated_negate_after_subtract() {
49614979
assert_format_rewrite!(
49624980
r#"pub fn main() {
49634981
let a = 3
@@ -4968,12 +4986,26 @@ fn repeated_negate_after_subtract() {
49684986
r#"pub fn main() {
49694987
let a = 3
49704988
let b = 4
4971-
let c = a - - - - - - - -b
4989+
let c = a - -b
49724990
}
49734991
"#
49744992
);
49754993
}
49764994

4995+
#[test]
4996+
fn double_negation_on_bools_is_removed() {
4997+
assert_format_rewrite!(
4998+
r#"pub fn main() {
4999+
!!True
5000+
}
5001+
"#,
5002+
"pub fn main() {
5003+
True
5004+
}
5005+
"
5006+
);
5007+
}
5008+
49775009
#[test]
49785010
fn wrap_long_line_with_int_negation() {
49795011
assert_format_rewrite!(

0 commit comments

Comments
 (0)