biome_js_formatter 0.0.2

Biome's JavaScript formatter
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
use crate::js::auxiliary::initializer_clause::FormatJsInitializerClauseOptions;
use crate::js::expressions::arrow_function_expression::FormatJsArrowFunctionExpressionOptions;
use crate::prelude::*;
use crate::utils::member_chain::is_member_call_chain;
use crate::utils::object::write_member_name;
use crate::utils::AnyJsBinaryLikeExpression;
use biome_formatter::{format_args, write, CstFormatContext, FormatOptions, VecBuffer};
use biome_js_syntax::AnyJsLiteralExpression;
use biome_js_syntax::{
    AnyJsAssignmentPattern, AnyJsBindingPattern, AnyJsCallArgument, AnyJsClassMemberName,
    AnyJsExpression, AnyJsFunctionBody, AnyJsObjectAssignmentPatternMember,
    AnyJsObjectBindingPatternMember, AnyJsObjectMemberName, AnyJsTemplateElement, AnyTsType,
    AnyTsVariableAnnotation, JsAssignmentExpression, JsInitializerClause, JsLiteralMemberName,
    JsObjectAssignmentPattern, JsObjectAssignmentPatternProperty, JsObjectBindingPattern,
    JsPropertyClassMember, JsPropertyClassMemberFields, JsPropertyObjectMember, JsSyntaxKind,
    JsVariableDeclarator, TsIdentifierBinding, TsInitializedPropertySignatureClassMember,
    TsInitializedPropertySignatureClassMemberFields, TsPropertySignatureClassMember,
    TsPropertySignatureClassMemberFields, TsTypeAliasDeclaration, TsTypeArguments,
};
use biome_rowan::{declare_node_union, AstNode, SyntaxNodeOptionExt, SyntaxResult};
use std::iter;

declare_node_union! {
    pub(crate) AnyJsAssignmentLike =
        JsPropertyObjectMember |
        JsAssignmentExpression |
        JsObjectAssignmentPatternProperty |
        JsVariableDeclarator |
        TsTypeAliasDeclaration |
        JsPropertyClassMember |
        TsPropertySignatureClassMember |
        TsInitializedPropertySignatureClassMember
}

declare_node_union! {
    pub(crate) LeftAssignmentLike =
        AnyJsAssignmentPattern |
        AnyJsObjectMemberName |
        AnyJsBindingPattern |
        TsIdentifierBinding |
        JsLiteralMemberName |
        AnyJsClassMemberName
}

declare_node_union! {
    pub(crate) RightAssignmentLike = AnyJsExpression | AnyJsAssignmentPattern | JsInitializerClause | AnyTsType
}

declare_node_union! {
    /// This is a convenient enum to map object patterns.
    pub(crate) AnyObjectPattern = JsObjectAssignmentPattern | JsObjectBindingPattern
}

impl AnyObjectPattern {
    /// Determines if this is a complex pattern. A pattern is considered complex if it has more than 2 properties
    /// and any property:
    ///
    /// * is a shorthand property with an initializer
    /// * is a non-shorthand property
    ///
    /// ## Examples
    ///
    /// ```javascript
    /// let { a, b, c = "test"} = ...
    /// ```
    ///
    /// Is considered a complex binding because it has three properties and a shorthand property with an initializer.
    ///
    /// ```javascript
    /// let { a, b, c: d } = ...
    /// ```
    ///
    /// Is considered a complex binding because it has three properties and a non-shorthand property
    ///
    fn is_complex(&self) -> bool {
        match self {
            AnyObjectPattern::JsObjectAssignmentPattern(assignment_pattern) => {
                use AnyJsObjectAssignmentPatternMember::*;

                if assignment_pattern.properties().len() <= 2 {
                    return false;
                }

                assignment_pattern
                    .properties()
                    .iter()
                    .flatten()
                    .any(|property| match property {
                        JsObjectAssignmentPatternProperty(_) => true,
                        JsObjectAssignmentPatternShorthandProperty(short) => short.init().is_some(),
                        _ => false,
                    })
            }
            AnyObjectPattern::JsObjectBindingPattern(binding_pattern) => {
                use AnyJsObjectBindingPatternMember::*;

                if binding_pattern.properties().len() <= 2 {
                    return false;
                }

                binding_pattern
                    .properties()
                    .iter()
                    .flatten()
                    .any(|property| match property {
                        JsObjectBindingPatternProperty(_) => true,
                        JsObjectBindingPatternShorthandProperty(member) => member.init().is_some(),
                        _ => false,
                    })
            }
        }
    }
}

impl LeftAssignmentLike {
    fn into_object_pattern(self) -> Option<AnyObjectPattern> {
        use AnyJsAssignmentPattern::*;
        use AnyJsBindingPattern::*;

        match self {
            LeftAssignmentLike::AnyJsAssignmentPattern(JsObjectAssignmentPattern(node)) => {
                Some(AnyObjectPattern::from(node))
            }
            LeftAssignmentLike::AnyJsBindingPattern(JsObjectBindingPattern(node)) => {
                Some(AnyObjectPattern::from(node))
            }
            _ => None,
        }
    }
}

/// [Prettier applies]: https://github.com/prettier/prettier/blob/fde0b49d7866e203ca748c306808a87b7c15548f/src/language-js/print/assignment.js#L278
pub(crate) fn is_complex_type_annotation(
    annotation: AnyTsVariableAnnotation,
) -> SyntaxResult<bool> {
    let is_complex = annotation
        .type_annotation()?
        .and_then(|type_annotation| type_annotation.ty().ok())
        .and_then(|ty| match ty {
            AnyTsType::TsReferenceType(reference_type) => {
                let type_arguments = reference_type.type_arguments()?;
                let argument_list_len = type_arguments.ts_type_argument_list().len();

                if argument_list_len <= 1 {
                    return Some(false);
                }

                let has_at_least_a_complex_type = type_arguments
                    .ts_type_argument_list()
                    .iter()
                    .filter_map(|p| p.ok())
                    .any(|argument| {
                        if matches!(argument, AnyTsType::TsConditionalType(_)) {
                            return true;
                        }

                        let is_complex_type = argument
                            .as_ts_reference_type()
                            .and_then(|reference_type| reference_type.type_arguments())
                            .map_or(false, |type_arguments| {
                                type_arguments.ts_type_argument_list().len() > 0
                            });

                        is_complex_type
                    });
                Some(has_at_least_a_complex_type)
            }
            _ => Some(false),
        })
        .unwrap_or(false);

    Ok(is_complex)
}

impl RightAssignmentLike {
    fn as_expression(&self) -> Option<AnyJsExpression> {
        match self {
            RightAssignmentLike::AnyJsExpression(expression) => Some(expression.clone()),
            RightAssignmentLike::JsInitializerClause(initializer) => initializer.expression().ok(),
            RightAssignmentLike::AnyJsAssignmentPattern(_) => None,
            RightAssignmentLike::AnyTsType(_) => None,
        }
    }
}

impl Format<JsFormatContext> for RightAssignmentLike {
    fn fmt(&self, f: &mut Formatter<JsFormatContext>) -> FormatResult<()> {
        match self {
            RightAssignmentLike::AnyJsExpression(expression) => {
                write!(f, [expression.format()])
            }
            RightAssignmentLike::AnyJsAssignmentPattern(assignment) => {
                write!(f, [assignment.format()])
            }
            RightAssignmentLike::JsInitializerClause(initializer) => {
                write!(f, [space(), initializer.format()])
            }
            RightAssignmentLike::AnyTsType(ty) => {
                write!(f, [space(), ty.format()])
            }
        }
    }
}

/// Determines how a assignment like be formatted
///
/// Assignment like are:
/// - Assignment
/// - Object property member
/// - Variable declaration
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum AssignmentLikeLayout {
    /// This is a special layout usually used for variable declarations.
    /// This layout is hit, usually, when a [variable declarator](JsVariableDeclarator) doesn't have initializer:
    /// ```js
    ///     let variable;
    /// ```
    /// ```ts
    ///     let variable: Map<string, number>;
    /// ```
    OnlyLeft,

    /// First break right-hand side, then after operator.
    /// ```js
    /// {
    ///   "array-key": [
    ///     {
    ///       "nested-key-1": 1,
    ///       "nested-key-2": 2,
    ///     },
    ///   ]
    /// }
    /// ```
    Fluid,

    /// First break after operator, then the sides are broken independently on their own lines.
    /// There is a soft line break after operator token.
    /// ```js
    /// {
    ///     "enough-long-key-to-break-line":
    ///         1 + 2,
    ///     "not-long-enough-key":
    ///         "but long enough string to break line",
    /// }
    /// ```
    BreakAfterOperator,

    /// First break right-hand side, then left-hand side. There are not any soft line breaks
    /// between left and right parts
    /// ```js
    /// {
    ///     key1: "123",
    ///     key2: 123,
    ///     key3: class MyClass {
    ///        constructor() {},
    ///     },
    /// }
    /// ```
    NeverBreakAfterOperator,

    /// This is a special layout usually used for long variable declarations or assignment expressions
    /// This layout is hit, usually, when we are in the "middle" of the chain:
    ///
    /// ```js
    /// var a =
    ///     loreum =
    ///     ipsum =
    ///         "foo";
    /// ```
    ///
    /// Given the previous snippet, then `loreum` and `ipsum` will be formatted using the [Chain] layout.
    Chain,

    /// This is a special layout usually used for long variable declarations or assignment expressions
    /// This layout is hit, usually, when we are in the end of a chain:
    /// ```js
    /// var a = loreum = ipsum = "foo";
    /// ```
    ///
    /// Given the previous snippet, then `"foo"` formatted  using the [ChainTail] layout.
    ChainTail,

    /// This layout is used in cases where we want to "break" the left hand side
    /// of assignment like expression, but only when the group decides to do it.
    ///
    /// ```js
    /// const a {
    ///     loreum: { ipsum },
    ///     something_else,
    ///     happy_days: { fonzy }
    /// } = obj;
    /// ```
    ///
    /// The snippet triggers the layout because the left hand side contains a "complex destructuring"
    /// which requires having the properties broke on different lines.
    BreakLeftHandSide,

    /// This is a special case of the "chain" layout collection. This is triggered when there's
    /// a series of simple assignments (at least three) and in the middle we have an arrow function
    /// and this function followed by two more arrow functions.
    ///
    /// This layout will break the right hand side of the tail on a new line and add a new level
    /// of indentation
    ///
    /// ```js
    /// lorem =
    ///     fff =
    ///     ee =
    ///         () => (fff) => () => (fefef) => () => fff;
    /// ```
    ChainTailArrowFunction,

    /// Layout used when the operator and right hand side are part of a `JsInitializerClause<
    /// that has a suppression comment.
    SuppressedInitializer,
}

const MIN_OVERLAP_FOR_BREAK: u8 = 3;

impl AnyJsAssignmentLike {
    fn right(&self) -> SyntaxResult<RightAssignmentLike> {
        let right = match self {
            AnyJsAssignmentLike::JsPropertyObjectMember(property) => property.value()?.into(),
            AnyJsAssignmentLike::JsAssignmentExpression(assignment) => assignment.right()?.into(),
            AnyJsAssignmentLike::JsObjectAssignmentPatternProperty(assignment_pattern) => {
                assignment_pattern.pattern()?.into()
            }
            AnyJsAssignmentLike::JsVariableDeclarator(variable_declarator) => {
                // SAFETY: Calling `unwrap` here is safe because we check `has_only_left_hand_side` variant at the beginning of the `layout` function
                variable_declarator.initializer().unwrap().into()
            }
            AnyJsAssignmentLike::TsTypeAliasDeclaration(type_alias_declaration) => {
                type_alias_declaration.ty()?.into()
            }
            AnyJsAssignmentLike::JsPropertyClassMember(n) => {
                // SAFETY: Calling `unwrap` here is safe because we check `has_only_left_hand_side` variant at the beginning of the `layout` function
                n.value().unwrap().into()
            }
            AnyJsAssignmentLike::TsPropertySignatureClassMember(_) => {
                unreachable!("TsPropertySignatureClassMember doesn't have any right side. If you're here, `has_only_left_hand_side` hasn't been called")
            }
            AnyJsAssignmentLike::TsInitializedPropertySignatureClassMember(n) => {
                // SAFETY: Calling `unwrap` here is safe because we check `has_only_left_hand_side` variant at the beginning of the `layout` function
                n.value().unwrap().into()
            }
        };

        Ok(right)
    }

    fn left(&self) -> SyntaxResult<LeftAssignmentLike> {
        match self {
            AnyJsAssignmentLike::JsPropertyObjectMember(property) => Ok(property.name()?.into()),
            AnyJsAssignmentLike::JsAssignmentExpression(assignment) => {
                Ok(assignment.left()?.into())
            }
            AnyJsAssignmentLike::JsObjectAssignmentPatternProperty(property) => {
                Ok(property.pattern()?.into())
            }
            AnyJsAssignmentLike::JsVariableDeclarator(variable_declarator) => {
                Ok(variable_declarator.id()?.into())
            }
            AnyJsAssignmentLike::TsTypeAliasDeclaration(type_alias_declaration) => {
                Ok(type_alias_declaration.binding_identifier()?.into())
            }
            AnyJsAssignmentLike::JsPropertyClassMember(property_class_member) => {
                Ok(property_class_member.name()?.into())
            }
            AnyJsAssignmentLike::TsPropertySignatureClassMember(
                property_signature_class_member,
            ) => Ok(property_signature_class_member.name()?.into()),
            AnyJsAssignmentLike::TsInitializedPropertySignatureClassMember(
                property_signature_class_member,
            ) => Ok(property_signature_class_member.name()?.into()),
        }
    }

    fn annotation(&self) -> Option<AnyTsVariableAnnotation> {
        match self {
            AnyJsAssignmentLike::JsVariableDeclarator(variable_declarator) => {
                variable_declarator.variable_annotation()
            }
            _ => None,
        }
    }

    fn write_left(&self, f: &mut JsFormatter) -> FormatResult<bool> {
        match self {
            AnyJsAssignmentLike::JsPropertyObjectMember(property) => {
                let name = property.name()?;

                // It's safe to mark the name as checked here because it is at the beginning of the property
                // and any suppression comment that would apply to the name applies to the property too and is,
                // thus, handled on the property level.
                f.context()
                    .comments()
                    .mark_suppression_checked(name.syntax());

                let width = write_member_name(&name.into(), f)?;
                let text_width_for_break =
                    (u8::from(f.options().tab_width()) + MIN_OVERLAP_FOR_BREAK) as usize;
                Ok(width < text_width_for_break)
            }
            AnyJsAssignmentLike::JsAssignmentExpression(assignment) => {
                let left = assignment.left()?;
                write!(f, [&left.format()])?;
                Ok(false)
            }
            AnyJsAssignmentLike::JsObjectAssignmentPatternProperty(property) => {
                let member_name = property.member()?;

                // It's safe to mark the name as checked here because it is at the beginning of the property
                // and any suppression comment that would apply to the name applies to the property too and is,
                // thus, handled on the property level.
                f.context()
                    .comments()
                    .mark_suppression_checked(member_name.syntax());

                let width = write_member_name(&member_name.into(), f)?;
                let text_width_for_break =
                    (u8::from(f.options().tab_width()) + MIN_OVERLAP_FOR_BREAK) as usize;
                Ok(width < text_width_for_break)
            }
            AnyJsAssignmentLike::JsVariableDeclarator(variable_declarator) => {
                let id = variable_declarator.id()?;
                let variable_annotation = variable_declarator.variable_annotation();

                write!(f, [id.format(), variable_annotation.format()])?;
                Ok(false)
            }
            AnyJsAssignmentLike::TsTypeAliasDeclaration(type_alias_declaration) => {
                let binding_identifier = type_alias_declaration.binding_identifier()?;
                let type_parameters = type_alias_declaration.type_parameters();

                write!(f, [binding_identifier.format()])?;
                if let Some(type_parameters) = type_parameters {
                    write!(f, [type_parameters.format(),])?;
                }
                Ok(false)
            }
            AnyJsAssignmentLike::JsPropertyClassMember(property_class_member) => {
                let JsPropertyClassMemberFields {
                    modifiers,
                    name,
                    property_annotation,
                    value: _,
                    semicolon_token: _,
                } = property_class_member.as_fields();
                write!(f, [modifiers.format(), space()])?;

                let name = name?;

                if f.context().comments().is_suppressed(name.syntax()) {
                    write!(f, [format_suppressed_node(name.syntax())])?;
                } else {
                    write_member_name(&name.into(), f)?;
                };

                write!(f, [property_annotation.format()])?;

                Ok(false)
            }
            AnyJsAssignmentLike::TsPropertySignatureClassMember(
                property_signature_class_member,
            ) => {
                let TsPropertySignatureClassMemberFields {
                    modifiers,
                    name,
                    property_annotation,
                    semicolon_token: _,
                } = property_signature_class_member.as_fields();

                write!(f, [modifiers.format(), space(),])?;

                let width = write_member_name(&name?.into(), f)?;

                write!(f, [property_annotation.format()])?;
                let text_width_for_break =
                    (u8::from(f.options().tab_width()) + MIN_OVERLAP_FOR_BREAK) as usize;
                Ok(width < text_width_for_break)
            }
            AnyJsAssignmentLike::TsInitializedPropertySignatureClassMember(
                property_signature_class_member,
            ) => {
                let TsInitializedPropertySignatureClassMemberFields {
                    modifiers,
                    name,
                    question_mark_token,
                    value: _,
                    semicolon_token: _,
                } = property_signature_class_member.as_fields();

                write!(f, [modifiers.format(), space(),])?;

                let width = write_member_name(&name?.into(), f)?;

                write!(f, [question_mark_token.format()])?;
                let text_width_for_break =
                    (u8::from(f.options().tab_width()) + MIN_OVERLAP_FOR_BREAK) as usize;
                Ok(width < text_width_for_break)
            }
        }
    }

    fn write_operator(&self, f: &mut JsFormatter) -> FormatResult<()> {
        match self {
            AnyJsAssignmentLike::JsPropertyObjectMember(property) => {
                let colon_token = property.colon_token()?;
                write!(f, [colon_token.format()])
            }
            AnyJsAssignmentLike::JsAssignmentExpression(assignment) => {
                let operator_token = assignment.operator_token()?;
                write!(f, [space(), operator_token.format()])
            }
            AnyJsAssignmentLike::JsObjectAssignmentPatternProperty(property) => {
                let colon_token = property.colon_token()?;
                write!(f, [colon_token.format()])
            }
            AnyJsAssignmentLike::JsVariableDeclarator(variable_declarator) => {
                if let Some(initializer) = variable_declarator.initializer() {
                    let eq_token = initializer.eq_token()?;
                    write!(f, [space(), eq_token.format()])?
                }
                Ok(())
            }
            AnyJsAssignmentLike::TsTypeAliasDeclaration(type_alias_declaration) => {
                let eq_token = type_alias_declaration.eq_token()?;
                write!(f, [space(), eq_token.format()])
            }
            AnyJsAssignmentLike::JsPropertyClassMember(property_class_member) => {
                if let Some(initializer) = property_class_member.value() {
                    let eq_token = initializer.eq_token()?;
                    write!(f, [space(), eq_token.format()])?
                }
                Ok(())
            }
            // this variant doesn't have any operator
            AnyJsAssignmentLike::TsPropertySignatureClassMember(_) => Ok(()),
            AnyJsAssignmentLike::TsInitializedPropertySignatureClassMember(
                property_class_member,
            ) => {
                let initializer = property_class_member.value()?;
                let eq_token = initializer.eq_token()?;
                write!(f, [space(), eq_token.format()])
            }
        }
    }

    fn write_right(&self, f: &mut JsFormatter, layout: AssignmentLikeLayout) -> FormatResult<()> {
        match self {
            AnyJsAssignmentLike::JsPropertyObjectMember(property) => {
                let value = property.value()?;
                write!(f, [with_assignment_layout(&value, Some(layout))])
            }
            AnyJsAssignmentLike::JsAssignmentExpression(assignment) => {
                let right = assignment.right()?;
                write!(f, [space(), with_assignment_layout(&right, Some(layout))])
            }
            AnyJsAssignmentLike::JsObjectAssignmentPatternProperty(property) => {
                let pattern = property.pattern()?;
                let init = property.init();
                write!(f, [pattern.format()])?;
                if let Some(init) = init {
                    write!(
                        f,
                        [
                            space(),
                            init.format()
                                .with_options(FormatJsInitializerClauseOptions {
                                    assignment_layout: Some(layout)
                                })
                        ]
                    )?;
                }
                Ok(())
            }
            AnyJsAssignmentLike::JsVariableDeclarator(variable_declarator) => {
                if let Some(initializer) = variable_declarator.initializer() {
                    let expression = initializer.expression()?;
                    write!(
                        f,
                        [
                            space(),
                            format_leading_comments(initializer.syntax()),
                            with_assignment_layout(&expression, Some(layout)),
                            format_trailing_comments(initializer.syntax())
                        ]
                    )?;
                }
                Ok(())
            }
            AnyJsAssignmentLike::TsTypeAliasDeclaration(type_alias_declaration) => {
                let ty = type_alias_declaration.ty()?;
                write!(f, [space(), ty.format()])
            }
            AnyJsAssignmentLike::JsPropertyClassMember(property_class_member) => {
                if let Some(initializer) = property_class_member.value() {
                    let expression = initializer.expression()?;
                    write!(
                        f,
                        [
                            space(),
                            format_leading_comments(initializer.syntax()),
                            with_assignment_layout(&expression, Some(layout)),
                            format_trailing_comments(initializer.syntax())
                        ]
                    )?;
                }
                Ok(())
            }
            // this variant doesn't have any right part
            AnyJsAssignmentLike::TsPropertySignatureClassMember(_) => Ok(()),
            AnyJsAssignmentLike::TsInitializedPropertySignatureClassMember(
                property_class_member,
            ) => {
                let initializer = property_class_member.value()?;
                let expression = initializer.expression()?;
                write!(
                    f,
                    [
                        space(),
                        format_leading_comments(initializer.syntax()),
                        with_assignment_layout(&expression, Some(layout)),
                        format_trailing_comments(initializer.syntax())
                    ]
                )
            }
        }
    }

    fn write_suppressed_initializer(&self, f: &mut JsFormatter) -> FormatResult<()> {
        let initializer = match self {
            AnyJsAssignmentLike::JsPropertyClassMember(class_member) => class_member.value(),
            AnyJsAssignmentLike::TsInitializedPropertySignatureClassMember(class_member) => {
                Some(class_member.value()?)
            }
            AnyJsAssignmentLike::JsVariableDeclarator(variable_declarator) => {
                variable_declarator.initializer()
            }

            AnyJsAssignmentLike::JsPropertyObjectMember(_)
            | AnyJsAssignmentLike::JsAssignmentExpression(_)
            | AnyJsAssignmentLike::JsObjectAssignmentPatternProperty(_)
            | AnyJsAssignmentLike::TsTypeAliasDeclaration(_)
            | AnyJsAssignmentLike::TsPropertySignatureClassMember(_) => {
                unreachable!("These variants have no initializer")
            }
        };

        let initializer =
            initializer.expect("Expected an initializer because it has a suppression comment");

        write!(f, [soft_line_indent_or_space(&initializer.format())])
    }

    /// Returns the layout variant for an assignment like depending on right expression and left part length
    /// [Prettier applies]: https://github.com/prettier/prettier/blob/main/src/language-js/print/assignment.js
    fn layout(
        &self,
        is_left_short: bool,
        f: &mut Formatter<JsFormatContext>,
    ) -> FormatResult<AssignmentLikeLayout> {
        if self.has_only_left_hand_side() {
            return Ok(AssignmentLikeLayout::OnlyLeft);
        }

        let right = self.right()?;

        if let RightAssignmentLike::JsInitializerClause(initializer) = &right {
            if f.context().comments().is_suppressed(initializer.syntax()) {
                return Ok(AssignmentLikeLayout::SuppressedInitializer);
            }
        }
        let right_expression = right.as_expression();

        if let Some(layout) = self.chain_formatting_layout(right_expression.as_ref())? {
            return Ok(layout);
        }

        if let Some(AnyJsExpression::JsCallExpression(call_expression)) = &right_expression {
            if call_expression.callee()?.syntax().text() == "require" {
                return Ok(AssignmentLikeLayout::NeverBreakAfterOperator);
            }
        }

        if self.should_break_left_hand_side()? {
            return Ok(AssignmentLikeLayout::BreakLeftHandSide);
        }

        if self.should_break_after_operator(&right, f.context().comments())? {
            return Ok(AssignmentLikeLayout::BreakAfterOperator);
        }

        if is_left_short {
            return Ok(AssignmentLikeLayout::NeverBreakAfterOperator);
        }

        // Before checking `BreakAfterOperator` layout, we need to unwrap the right expression from `JsUnaryExpression` or `TsNonNullAssertionExpression`
        // [Prettier applies]: https://github.com/prettier/prettier/blob/a043ac0d733c4d53f980aa73807a63fc914f23bd/src/language-js/print/assignment.js#L199-L211
        // Example:
        //  !"123" -> "123"
        //  void "123" -> "123"
        //  !!"string"! -> "string"
        let right_expression = iter::successors(right_expression, |expression| match expression {
            AnyJsExpression::JsUnaryExpression(unary) => unary.argument().ok(),
            AnyJsExpression::TsNonNullAssertionExpression(assertion) => assertion.expression().ok(),
            _ => None,
        })
        .last();

        if matches!(
            right_expression,
            Some(AnyJsExpression::AnyJsLiteralExpression(
                AnyJsLiteralExpression::JsStringLiteralExpression(_)
            )),
        ) {
            return Ok(AssignmentLikeLayout::BreakAfterOperator);
        }

        let is_poorly_breakable = match &right_expression {
            Some(expression) => is_poorly_breakable_member_or_call_chain(expression, f)?,
            None => false,
        };

        if is_poorly_breakable {
            return Ok(AssignmentLikeLayout::BreakAfterOperator);
        }

        if matches!(
            right_expression,
            Some(
                AnyJsExpression::JsClassExpression(_)
                    | AnyJsExpression::JsTemplateExpression(_)
                    | AnyJsExpression::AnyJsLiteralExpression(
                        AnyJsLiteralExpression::JsBooleanLiteralExpression(_)
                            | AnyJsLiteralExpression::JsNumberLiteralExpression(_)
                    )
            )
        ) {
            return Ok(AssignmentLikeLayout::NeverBreakAfterOperator);
        }

        Ok(AssignmentLikeLayout::Fluid)
    }

    /// Checks that a [JsAnyAssignmentLike] consists only of the left part
    /// usually, when a [variable declarator](JsVariableDeclarator) doesn't have initializer
    fn has_only_left_hand_side(&self) -> bool {
        if let AnyJsAssignmentLike::JsVariableDeclarator(declarator) = self {
            declarator.initializer().is_none()
        } else if let AnyJsAssignmentLike::JsPropertyClassMember(class_member) = self {
            class_member.value().is_none()
        } else {
            matches!(self, AnyJsAssignmentLike::TsPropertySignatureClassMember(_))
        }
    }

    /// Checks if the right node is entitled of the chain formatting,
    /// and if so, it return the layout type
    fn chain_formatting_layout(
        &self,
        right_expression: Option<&AnyJsExpression>,
    ) -> SyntaxResult<Option<AssignmentLikeLayout>> {
        let right_is_tail = !matches!(
            right_expression,
            Some(AnyJsExpression::JsAssignmentExpression(_))
        );

        // The chain goes up two levels, by checking up to the great parent if all the conditions
        // are correctly met.
        let upper_chain_is_eligible =
            // First, we check if the current node is an assignment expression
            if let AnyJsAssignmentLike::JsAssignmentExpression(assignment) = self {
                assignment.syntax().parent().map_or(false, |parent| {
                    // Then we check if the parent is assignment expression or variable declarator
                    if matches!(
                        parent.kind(),
                        JsSyntaxKind::JS_ASSIGNMENT_EXPRESSION
                            | JsSyntaxKind::JS_INITIALIZER_CLAUSE
                    ) {
                        let great_parent_kind = parent.parent().kind();
                        // Finally, we check the great parent.
                        // The great parent triggers the eligibility when
                        // - the current node that we were inspecting is not a "tail"
                        // - or the great parent is not an expression statement or a variable declarator
                        !right_is_tail
                            || !matches!(
                                great_parent_kind,
                                Some(
                                    JsSyntaxKind::JS_EXPRESSION_STATEMENT
                                        | JsSyntaxKind::JS_VARIABLE_DECLARATOR
                                )
                            )
                    } else {
                        false
                    }
                })
            } else {
                false
            };

        let result = if upper_chain_is_eligible {
            if !right_is_tail {
                Some(AssignmentLikeLayout::Chain)
            } else {
                match right_expression {
                    Some(AnyJsExpression::JsArrowFunctionExpression(arrow)) => {
                        let this_body = arrow.body()?;
                        match this_body {
                            AnyJsFunctionBody::AnyJsExpression(expression) => {
                                if matches!(
                                    expression,
                                    AnyJsExpression::JsArrowFunctionExpression(_)
                                ) {
                                    Some(AssignmentLikeLayout::ChainTailArrowFunction)
                                } else {
                                    Some(AssignmentLikeLayout::ChainTail)
                                }
                            }
                            _ => Some(AssignmentLikeLayout::ChainTail),
                        }
                    }

                    _ => Some(AssignmentLikeLayout::ChainTail),
                }
            }
        } else {
            None
        };

        Ok(result)
    }

    fn is_complex_type_alias(&self) -> SyntaxResult<bool> {
        let result = if let AnyJsAssignmentLike::TsTypeAliasDeclaration(type_alias_declaration) =
            self
        {
            let type_parameters = type_alias_declaration.type_parameters();

            if let Some(type_parameters) = type_parameters {
                let items = type_parameters.items();
                if items.len() <= 1 {
                    return Ok(false);
                };
                for type_parameter in type_parameters.items() {
                    let type_parameter = type_parameter?;

                    if type_parameter.constraint().is_some() || type_parameter.default().is_some() {
                        return Ok(true);
                    }
                }
                return Ok(false);
            } else {
                false
            }
        } else {
            false
        };

        Ok(result)
    }

    /// Particular function that checks if the left hand side of a [JsAnyAssignmentLike] should
    /// be broken on multiple lines
    fn should_break_left_hand_side(&self) -> SyntaxResult<bool> {
        let is_complex_destructuring = self
            .left()?
            .into_object_pattern()
            .map_or(false, |pattern| pattern.is_complex());

        let has_complex_type_annotation = self
            .annotation()
            .and_then(|annotation| is_complex_type_annotation(annotation).ok())
            .unwrap_or(false);

        let is_complex_type_alias = self.is_complex_type_alias()?;

        Ok(is_complex_destructuring || has_complex_type_annotation || is_complex_type_alias)
    }

    /// Checks if the the current assignment is eligible for [AssignmentLikeLayout::BreakAfterOperator]
    ///
    /// This function is small wrapper around [should_break_after_operator] because it has to work
    /// for nodes that belong to TypeScript too.
    fn should_break_after_operator(
        &self,
        right: &RightAssignmentLike,
        comments: &JsComments,
    ) -> SyntaxResult<bool> {
        let result = match right {
            RightAssignmentLike::AnyJsExpression(expression) => {
                should_break_after_operator(expression, comments)?
            }
            RightAssignmentLike::JsInitializerClause(initializer) => {
                comments.has_leading_own_line_comment(initializer.syntax())
                    || should_break_after_operator(&initializer.expression()?, comments)?
            }
            RightAssignmentLike::AnyTsType(AnyTsType::TsUnionType(ty)) => {
                comments.has_leading_comments(ty.syntax())
            }
            right => comments.has_leading_own_line_comment(right.syntax()),
        };

        Ok(result)
    }
}

/// Checks if the function is entitled to be printed with layout [AssignmentLikeLayout::BreakAfterOperator]
pub(crate) fn should_break_after_operator(
    right: &AnyJsExpression,
    comments: &JsComments,
) -> SyntaxResult<bool> {
    if comments.has_leading_own_line_comment(right.syntax())
        && !matches!(right, AnyJsExpression::JsxTagExpression(_))
    {
        return Ok(true);
    }

    let result = match right {
        // head is a long chain, meaning that right -> right are both assignment expressions
        AnyJsExpression::JsAssignmentExpression(assignment) => {
            matches!(
                assignment.right()?,
                AnyJsExpression::JsAssignmentExpression(_)
            )
        }
        right if AnyJsBinaryLikeExpression::can_cast(right.syntax().kind()) => {
            let binary_like = AnyJsBinaryLikeExpression::unwrap_cast(right.syntax().clone());

            !binary_like.should_inline_logical_expression()
        }

        AnyJsExpression::JsSequenceExpression(_) => true,

        AnyJsExpression::JsConditionalExpression(conditional) => {
            AnyJsBinaryLikeExpression::cast(conditional.test()?.into_syntax())
                .map_or(false, |expression| {
                    !expression.should_inline_logical_expression()
                })
        }

        AnyJsExpression::JsClassExpression(class) => !class.decorators().is_empty(),

        _ => false,
    };

    Ok(result)
}

impl Format<JsFormatContext> for AnyJsAssignmentLike {
    fn fmt(&self, f: &mut JsFormatter) -> FormatResult<()> {
        let format_content = format_with(|f| {
            // We create a temporary buffer because the left hand side has to conditionally add
            // a group based on the layout, but the layout can only be computed by knowing the
            // width of the left hand side. The left hand side can be a member, and that has a width
            // can can be known only when it's formatted (it can incur in some transformation,
            // like removing some escapes, etc.).
            //
            // 1. we crate a temporary buffer
            // 2. we write the left hand side into the buffer and retrieve the `is_left_short` info
            // which is computed only when we format it
            // 3. we compute the layout
            // 4. we write the left node inside the main buffer based on the layout
            let mut buffer = VecBuffer::new(f.state_mut());
            let is_left_short = self.write_left(&mut Formatter::new(&mut buffer))?;
            let formatted_left = buffer.into_vec();

            // Compare name only if we are in a position of computing it.
            // If not (for example, left is not an identifier), then let's fallback to false,
            // so we can continue the chain of checks
            let layout = self.layout(is_left_short, f)?;

            let left = format_once(|f| f.write_elements(formatted_left));
            let right = format_with(|f| self.write_right(f, layout));

            let inner_content = format_with(|f| {
                if matches!(
                    &layout,
                    AssignmentLikeLayout::BreakLeftHandSide | AssignmentLikeLayout::OnlyLeft
                ) {
                    write!(f, [left])?;
                } else {
                    write!(f, [group(&left)])?;
                }

                if layout != AssignmentLikeLayout::SuppressedInitializer {
                    self.write_operator(f)?;
                }

                match layout {
                    AssignmentLikeLayout::OnlyLeft => Ok(()),
                    AssignmentLikeLayout::Fluid => {
                        let group_id = f.group_id("assignment_like");

                        write![
                            f,
                            [
                                group(&indent(&soft_line_break_or_space()),)
                                    .with_group_id(Some(group_id)),
                                line_suffix_boundary(),
                                indent_if_group_breaks(&right, group_id)
                            ]
                        ]
                    }
                    AssignmentLikeLayout::BreakAfterOperator => {
                        write![
                            f,
                            [group(&indent(&format_args![
                                soft_line_break_or_space(),
                                right,
                            ]))]
                        ]
                    }
                    AssignmentLikeLayout::NeverBreakAfterOperator => {
                        write![f, [space(), right]]
                    }

                    AssignmentLikeLayout::BreakLeftHandSide => {
                        write![f, [space(), group(&right)]]
                    }

                    AssignmentLikeLayout::Chain => {
                        write!(f, [soft_line_break_or_space(), right])
                    }

                    AssignmentLikeLayout::ChainTail => {
                        write!(
                            f,
                            [&indent(&format_args![soft_line_break_or_space(), right])]
                        )
                    }

                    AssignmentLikeLayout::ChainTailArrowFunction => {
                        write!(f, [space(), right])
                    }
                    AssignmentLikeLayout::SuppressedInitializer => {
                        self.write_suppressed_initializer(f)
                    }
                }
            });

            match layout {
                // Layouts that don't need enclosing group
                AssignmentLikeLayout::Chain
                | AssignmentLikeLayout::ChainTail
                | AssignmentLikeLayout::SuppressedInitializer
                | AssignmentLikeLayout::OnlyLeft => {
                    write!(f, [&inner_content])
                }
                _ => {
                    write!(f, [group(&inner_content)])
                }
            }
        });

        write!(f, [format_content])
    }
}

/// A chain that has no calls at all or all of whose calls have no arguments
/// or have only one which [is_short_argument], except for member call chains
/// [Prettier applies]: https://github.com/prettier/prettier/blob/a043ac0d733c4d53f980aa73807a63fc914f23bd/src/language-js/print/assignment.js#L329
fn is_poorly_breakable_member_or_call_chain(
    expression: &AnyJsExpression,
    f: &Formatter<JsFormatContext>,
) -> SyntaxResult<bool> {
    let threshold = f.options().line_width().value() / 4;

    // Only call and member chains are poorly breakable
    // - `obj.member.prop`
    // - `obj.member()()`
    let mut is_chain = false;

    // Only chains with simple head are poorly breakable
    // Simple head is `JsIdentifierExpression` or `JsThisExpression`
    let mut is_chain_head_simple = false;

    // Keeping track of all call expressions in the chain to check them later
    let mut call_expressions = vec![];

    let mut expression = Some(expression.clone());

    while let Some(node) = expression.take() {
        expression = match node {
            AnyJsExpression::TsNonNullAssertionExpression(assertion) => assertion.expression().ok(),
            AnyJsExpression::JsCallExpression(call_expression) => {
                is_chain = true;
                let callee = call_expression.callee()?;
                call_expressions.push(call_expression);
                Some(callee)
            }
            AnyJsExpression::JsStaticMemberExpression(node) => {
                is_chain = true;
                Some(node.object()?)
            }
            AnyJsExpression::JsComputedMemberExpression(node) => {
                is_chain = true;
                Some(node.object()?)
            }
            AnyJsExpression::JsIdentifierExpression(_) | AnyJsExpression::JsThisExpression(_) => {
                is_chain_head_simple = true;
                break;
            }
            _ => {
                break;
            }
        }
    }

    if !is_chain || !is_chain_head_simple {
        return Ok(false);
    }

    for call_expression in call_expressions {
        if is_member_call_chain(
            call_expression.clone(),
            f.comments(),
            f.options().tab_width(),
        )? {
            return Ok(false);
        }

        let args = call_expression.arguments()?.args();

        let is_breakable_call = match args.len() {
            0 => false,
            1 => match args.iter().next() {
                Some(first_argument) => {
                    !is_short_argument(first_argument?, threshold, f.context().comments())?
                }
                None => false,
            },
            _ => true,
        };

        if is_breakable_call {
            return Ok(false);
        }

        let is_breakable_type_arguments = match call_expression.type_arguments() {
            Some(type_arguments) => is_complex_type_arguments(type_arguments)?,
            None => false,
        };

        if is_breakable_type_arguments {
            return Ok(false);
        }
    }

    Ok(true)
}

/// This function checks if `JsAnyCallArgument` is short
/// We need it to decide if `JsCallExpression` with the argument is breakable or not
/// If the argument is short the function call isn't breakable
/// [Prettier applies]: https://github.com/prettier/prettier/blob/a043ac0d733c4d53f980aa73807a63fc914f23bd/src/language-js/print/assignment.js#L374
fn is_short_argument(
    argument: AnyJsCallArgument,
    threshold: u16,
    comments: &JsComments,
) -> SyntaxResult<bool> {
    if comments.has_comments(argument.syntax()) {
        return Ok(false);
    }

    if let AnyJsCallArgument::AnyJsExpression(expression) = argument {
        let is_short_argument = match expression {
            AnyJsExpression::JsThisExpression(_) => true,
            AnyJsExpression::JsIdentifierExpression(identifier) => {
                identifier.name()?.value_token()?.text_trimmed().len() <= threshold as usize
            }
            AnyJsExpression::JsUnaryExpression(unary_expression) => {
                let has_comments = comments.has_comments(unary_expression.argument()?.syntax());

                unary_expression.is_signed_numeric_literal()? && !has_comments
            }
            AnyJsExpression::AnyJsLiteralExpression(literal) => match literal {
                AnyJsLiteralExpression::JsRegexLiteralExpression(regex) => {
                    regex.pattern()?.chars().count() <= threshold as usize
                }
                AnyJsLiteralExpression::JsStringLiteralExpression(string) => {
                    string.value_token()?.text_trimmed().len() <= threshold as usize
                }
                _ => true,
            },
            AnyJsExpression::JsTemplateExpression(template) => {
                let elements = template.elements();

                // Besides checking length exceed we also need to check that the template doesn't have any expressions.
                // It means that the elements of the template are empty or have only one `JsTemplateChunkElement` element
                // Prettier: https://github.com/prettier/prettier/blob/a043ac0d733c4d53f980aa73807a63fc914f23bd/src/language-js/print/assignment.js#L402-L405
                match elements.len() {
                    0 => true,
                    1 => match elements.iter().next() {
                        Some(AnyJsTemplateElement::JsTemplateChunkElement(element)) => {
                            let token = element.template_chunk_token()?;
                            let text_trimmed = token.text_trimmed();
                            !text_trimmed.contains('\n') && text_trimmed.len() <= threshold as usize
                        }
                        _ => false,
                    },
                    _ => false,
                }
            }
            _ => false,
        };
        Ok(is_short_argument)
    } else {
        Ok(false)
    }
}

/// This function checks if `TsTypeArguments` is complex
/// We need it to decide if `JsCallExpression` with the type arguments is breakable or not
/// If the type arguments is complex the function call is breakable
/// [Prettier applies]: https://github.com/prettier/prettier/blob/a043ac0d733c4d53f980aa73807a63fc914f23bd/src/language-js/print/assignment.js#L432
fn is_complex_type_arguments(type_arguments: TsTypeArguments) -> SyntaxResult<bool> {
    let ts_type_argument_list = type_arguments.ts_type_argument_list();

    if ts_type_argument_list.len() > 1 {
        return Ok(true);
    }

    let is_first_argument_complex = ts_type_argument_list
        .iter()
        .next()
        .transpose()?
        .map(|first_argument| {
            matches!(
                first_argument,
                AnyTsType::TsUnionType(_)
                    | AnyTsType::TsIntersectionType(_)
                    | AnyTsType::TsObjectType(_)
            )
        })
        .unwrap_or(false);

    if is_first_argument_complex {
        return Ok(true);
    }

    // TODO: add here will_break logic
    // https://github.com/prettier/prettier/blob/a043ac0d733c4d53f980aa73807a63fc914f23bd/src/language-js/print/assignment.js#L454

    Ok(false)
}

/// Formats an expression and passes the assignment layout to its formatting function if the expressions
/// formatting rule takes the layout as an option.
pub(crate) struct WithAssignmentLayout<'a> {
    expression: &'a AnyJsExpression,
    layout: Option<AssignmentLikeLayout>,
}

pub(crate) fn with_assignment_layout(
    expression: &AnyJsExpression,
    layout: Option<AssignmentLikeLayout>,
) -> WithAssignmentLayout {
    WithAssignmentLayout { expression, layout }
}

impl Format<JsFormatContext> for WithAssignmentLayout<'_> {
    fn fmt(&self, f: &mut Formatter<JsFormatContext>) -> FormatResult<()> {
        match self.expression {
            AnyJsExpression::JsArrowFunctionExpression(arrow) => arrow
                .format()
                .with_options(FormatJsArrowFunctionExpressionOptions {
                    assignment_layout: self.layout,
                    ..FormatJsArrowFunctionExpressionOptions::default()
                })
                .fmt(f),
            expression => expression.format().fmt(f),
        }
    }
}