File tree Expand file tree Collapse file tree 3 files changed +62
-8
lines changed Expand file tree Collapse file tree 3 files changed +62
-8
lines changed Original file line number Diff line number Diff line change 107
107
108
108
### Formatter
109
109
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
+
110
131
### Bug fixes
111
132
112
133
- Fixed a bug where ` echo ` could crash on JavaScript if the module contains
Original file line number Diff line number Diff line change @@ -2521,16 +2521,17 @@ impl<'comments> Formatter<'comments> {
2521
2521
2522
2522
fn negate_bool < ' a > ( & mut self , expr : & ' a UntypedExpr ) -> Document < ' a > {
2523
2523
match expr {
2524
+ UntypedExpr :: NegateBool { value, .. } => self . expr ( value) ,
2524
2525
UntypedExpr :: BinOp { .. } => "!" . to_doc ( ) . append ( wrap_block ( self . expr ( expr) ) ) ,
2525
2526
_ => docvec ! [ "!" , self . expr( expr) ] ,
2526
2527
}
2527
2528
}
2528
2529
2529
2530
fn negate_int < ' a > ( & mut self , expr : & ' a UntypedExpr ) -> Document < ' a > {
2530
2531
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 ) ) ,
2534
2535
2535
2536
_ => docvec ! [ "-" , self . expr( expr) ] ,
2536
2537
}
Original file line number Diff line number Diff line change @@ -4902,7 +4902,7 @@ fn double_negate() {
4902
4902
"# ,
4903
4903
r#"pub fn main() {
4904
4904
let a = 3
4905
- let b = - - a
4905
+ let b = a
4906
4906
}
4907
4907
"#
4908
4908
) ;
@@ -4918,7 +4918,7 @@ fn triple_negate() {
4918
4918
"# ,
4919
4919
r#"pub fn main() {
4920
4920
let a = 3
4921
- let b = - - - a
4921
+ let b = -a
4922
4922
}
4923
4923
"#
4924
4924
) ;
@@ -4950,14 +4950,32 @@ fn binary_double_negate() {
4950
4950
"# ,
4951
4951
r#"pub fn main() {
4952
4952
let a = 3
4953
- let b = - - { a + 3 }
4953
+ let b = { a + 3 }
4954
4954
}
4955
4955
"#
4956
4956
) ;
4957
4957
}
4958
4958
4959
4959
#[ 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 ( ) {
4961
4979
assert_format_rewrite ! (
4962
4980
r#"pub fn main() {
4963
4981
let a = 3
@@ -4968,12 +4986,26 @@ fn repeated_negate_after_subtract() {
4968
4986
r#"pub fn main() {
4969
4987
let a = 3
4970
4988
let b = 4
4971
- let c = a - - - - - - - - b
4989
+ let c = a - -b
4972
4990
}
4973
4991
"#
4974
4992
) ;
4975
4993
}
4976
4994
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
+
4977
5009
#[ test]
4978
5010
fn wrap_long_line_with_int_negation ( ) {
4979
5011
assert_format_rewrite ! (
You can’t perform that action at this time.
0 commit comments