summaryrefslogtreecommitdiff
path: root/venv/lib/python3.11/site-packages/jsbeautifier/javascript/beautifier.py
blob: fff41a05f400184a5934cd370b37cce8eb146c99 (plain)
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
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
# The MIT License (MIT)
#
# Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import re
import string
import copy
from ..core.token import Token
from .tokenizer import Tokenizer
from .tokenizer import TOKEN
from .options import BeautifierOptions
from ..core.output import Output


def default_options():
    return BeautifierOptions()


class BeautifierFlags:
    def __init__(self, mode):
        self.mode = mode
        self.parent = None
        self.last_token = Token(TOKEN.START_BLOCK, "")
        self.last_word = ""
        self.declaration_statement = False
        self.declaration_assignment = False
        self.multiline_frame = False
        self.inline_frame = False
        self.if_block = False
        self.else_block = False
        self.class_start_block = False
        self.do_block = False
        self.do_while = False
        self.import_block = False
        self.in_case = False
        self.in_case_statement = False
        self.case_body = False
        self.case_block = False
        self.indentation_level = 0
        self.alignment = 0
        self.line_indent_level = 0
        self.start_line_index = 0
        self.ternary_depth = 0

    def apply_base(self, flags_base, added_newline):
        next_indent_level = flags_base.indentation_level
        if not added_newline and flags_base.line_indent_level > next_indent_level:
            next_indent_level = flags_base.line_indent_level

        self.parent = flags_base
        self.last_token = flags_base.last_token
        self.last_word = flags_base.last_word
        self.indentation_level = next_indent_level


OPERATOR_POSITION = {
    "before_newline": "before-newline",
    "after_newline": "after-newline",
    "preserve_newline": "preserve-newline",
}
OPERATOR_POSITION_BEFORE_OR_PRESERVE = [
    OPERATOR_POSITION["before_newline"],
    OPERATOR_POSITION["preserve_newline"],
]


class MODE:
    (
        BlockStatement,
        Statement,
        ObjectLiteral,
        ArrayLiteral,
        ForInitializer,
        Conditional,
        Expression,
    ) = range(7)


def remove_redundant_indentation(output, frame):
    # This implementation is effective but has some issues:
    #     - can cause line wrap to happen too soon due to indent removal
    #           after wrap points are calculated
    # These issues are minor compared to ugly indentation.

    if (
        frame.multiline_frame
        or frame.mode == MODE.ForInitializer
        or frame.mode == MODE.Conditional
    ):
        return

    # remove one indent from each line inside this section
    output.remove_indent(frame.start_line_index)


def reserved_word(token, word):
    return token and token.type == TOKEN.RESERVED and token.text == word


def reserved_array(token, words):
    return token and token.type == TOKEN.RESERVED and token.text in words


_special_word_set = frozenset(
    [
        "case",
        "return",
        "do",
        "if",
        "throw",
        "else",
        "await",
        "break",
        "continue",
        "async",
    ]
)


class Beautifier:
    def __init__(self, opts=None):
        import jsbeautifier.javascript.acorn as acorn

        self.acorn = acorn
        self._options = BeautifierOptions(opts)

        self._blank_state()

    def _blank_state(self, js_source_text=None):
        if js_source_text is None:
            js_source_text = ""

        # internal flags
        self._flags = None
        self._previous_flags = None
        self._flag_store = []
        self._tokens = None

        if self._options.eol == "auto":
            self._options.eol = "\n"
            if self.acorn.lineBreak.search(js_source_text or ""):
                self._options.eol = self.acorn.lineBreak.search(js_source_text).group()

        baseIndentString = re.search("^[\t ]*", js_source_text).group(0)
        self._last_last_text = ""  # pre-last token text

        self._output = Output(self._options, baseIndentString)
        # If testing the ignore directive, start with output disable set to
        # true
        self._output.raw = self._options.test_output_raw

        self.set_mode(MODE.BlockStatement)
        return js_source_text

    def beautify(self, source_text="", opts=None):
        if opts is not None:
            self._options = BeautifierOptions(opts)

        source_text = source_text or ""
        if self._options.disabled:
            return source_text

        source_text = self._blank_state(source_text)

        source_text = self.unpack(source_text, self._options.eval_code)

        self._tokens = Tokenizer(source_text, self._options).tokenize()

        for current_token in self._tokens:
            self.handle_token(current_token)

            self._last_last_text = self._flags.last_token.text
            self._flags.last_token = current_token

        sweet_code = self._output.get_code(self._options.eol)

        return sweet_code

    def handle_token(self, current_token, preserve_statement_flags=False):
        if current_token.type == TOKEN.START_EXPR:
            self.handle_start_expr(current_token)
        elif current_token.type == TOKEN.END_EXPR:
            self.handle_end_expr(current_token)
        elif current_token.type == TOKEN.START_BLOCK:
            self.handle_start_block(current_token)
        elif current_token.type == TOKEN.END_BLOCK:
            self.handle_end_block(current_token)
        elif current_token.type == TOKEN.WORD:
            self.handle_word(current_token)
        elif current_token.type == TOKEN.RESERVED:
            self.handle_word(current_token)
        elif current_token.type == TOKEN.SEMICOLON:
            self.handle_semicolon(current_token)
        elif current_token.type == TOKEN.STRING:
            self.handle_string(current_token)
        elif current_token.type == TOKEN.EQUALS:
            self.handle_equals(current_token)
        elif current_token.type == TOKEN.OPERATOR:
            self.handle_operator(current_token)
        elif current_token.type == TOKEN.COMMA:
            self.handle_comma(current_token)
        elif current_token.type == TOKEN.BLOCK_COMMENT:
            self.handle_block_comment(current_token, preserve_statement_flags)
        elif current_token.type == TOKEN.COMMENT:
            self.handle_comment(current_token, preserve_statement_flags)
        elif current_token.type == TOKEN.DOT:
            self.handle_dot(current_token)
        elif current_token.type == TOKEN.EOF:
            self.handle_eof(current_token)
        elif current_token.type == TOKEN.UNKNOWN:
            self.handle_unknown(current_token, preserve_statement_flags)
        else:
            self.handle_unknown(current_token, preserve_statement_flags)

    def handle_whitespace_and_comments(
        self, current_token, preserve_statement_flags=False
    ):
        newlines = current_token.newlines
        keep_whitespace = self._options.keep_array_indentation and self.is_array(
            self._flags.mode
        )

        if current_token.comments_before is not None:
            for comment_token in current_token.comments_before:
                # The cleanest handling of inline comments is to treat them
                # as though they aren't there.
                # Just continue formatting and the behavior should be logical.
                # Also ignore unknown tokens.  Again, this should result in better
                # behavior.
                self.handle_whitespace_and_comments(
                    comment_token, preserve_statement_flags
                )
                self.handle_token(comment_token, preserve_statement_flags)

        if keep_whitespace:
            for i in range(newlines):
                self.print_newline(i > 0, preserve_statement_flags)
        else:  # not keep_whitespace
            if (
                self._options.max_preserve_newlines != 0
                and newlines > self._options.max_preserve_newlines
            ):
                newlines = self._options.max_preserve_newlines

            if self._options.preserve_newlines and newlines > 1:
                self.print_newline(False, preserve_statement_flags)
                for i in range(1, newlines):
                    self.print_newline(True, preserve_statement_flags)

    def unpack(self, source, evalcode=False):
        import jsbeautifier.unpackers as unpackers

        try:
            return unpackers.run(source, evalcode)
        except unpackers.UnpackingError:
            return source

    def is_array(self, mode):
        return mode == MODE.ArrayLiteral

    def is_expression(self, mode):
        return (
            mode == MODE.Expression
            or mode == MODE.ForInitializer
            or mode == MODE.Conditional
        )

    _newline_restricted_tokens = frozenset(
        ["async", "break", "continue", "return", "throw", "yield"]
    )

    def allow_wrap_or_preserved_newline(self, current_token, force_linewrap=False):
        # never wrap the first token of a line.
        if self._output.just_added_newline():
            return

        shouldPreserveOrForce = (
            self._options.preserve_newlines and current_token.newlines
        ) or force_linewrap
        operatorLogicApplies = (
            self._flags.last_token.text in Tokenizer.positionable_operators
            or current_token.text in Tokenizer.positionable_operators
        )

        if operatorLogicApplies:
            shouldPrintOperatorNewline = (
                self._flags.last_token.text in Tokenizer.positionable_operators
                and self._options.operator_position
                in OPERATOR_POSITION_BEFORE_OR_PRESERVE
            ) or current_token.text in Tokenizer.positionable_operators
            shouldPreserveOrForce = shouldPreserveOrForce and shouldPrintOperatorNewline

        if shouldPreserveOrForce:
            self.print_newline(preserve_statement_flags=True)
        elif self._options.wrap_line_length > 0:
            if reserved_array(self._flags.last_token, self._newline_restricted_tokens):
                # These tokens should never have a newline inserted between
                # them and the following expression.
                return
            self._output.set_wrap_point()

    def print_newline(self, force_newline=False, preserve_statement_flags=False):
        if not preserve_statement_flags:
            if (
                self._flags.last_token.text != ";"
                and self._flags.last_token.text != ","
                and self._flags.last_token.text != "="
                and (
                    self._flags.last_token.type != TOKEN.OPERATOR
                    or self._flags.last_token.text == "--"
                    or self._flags.last_token.text == "++"
                )
            ):
                next_token = self._tokens.peek()
                while (
                    self._flags.mode == MODE.Statement
                    and not (self._flags.if_block and reserved_word(next_token, "else"))
                    and not self._flags.do_block
                ):
                    self.restore_mode()

        if self._output.add_new_line(force_newline):
            self._flags.multiline_frame = True

    def print_token_line_indentation(self, current_token):
        if self._output.just_added_newline():
            line = self._output.current_line
            if (
                self._options.keep_array_indentation
                and current_token.newlines
                and (self.is_array(self._flags.mode) or current_token.text == "[")
            ):
                line.set_indent(-1)
                line.push(current_token.whitespace_before)
                self._output.space_before_token = False
            elif self._output.set_indent(
                self._flags.indentation_level, self._flags.alignment
            ):
                self._flags.line_indent_level = self._flags.indentation_level

    def print_token(self, current_token, s=None):
        if self._output.raw:
            self._output.add_raw_token(current_token)
            return

        if (
            self._options.comma_first
            and current_token.previous
            and current_token.previous.type == TOKEN.COMMA
            and self._output.just_added_newline()
        ):
            if self._output.previous_line.last() == ",":
                # if the comma was already at the start of the line,
                # pull back onto that line and reprint the indentation
                popped = self._output.previous_line.pop()
                if self._output.previous_line.is_empty():
                    self._output.previous_line.push(popped)
                    self._output.trim(True)
                    self._output.current_line.pop()
                    self._output.trim()

                # add the comma in front of the next token
                self.print_token_line_indentation(current_token)
                self._output.add_token(",")
                self._output.space_before_token = True

        if s is None:
            s = current_token.text

        self.print_token_line_indentation(current_token)
        self._output.non_breaking_space = True
        self._output.add_token(s)
        if self._output.previous_token_wrapped:
            self._flags.multiline_frame = True

    def indent(self):
        self._flags.indentation_level += 1
        self._output.set_indent(self._flags.indentation_level, self._flags.alignment)

    def deindent(self):
        allow_deindent = self._flags.indentation_level > 0 and (
            (self._flags.parent is None)
            or self._flags.indentation_level > self._flags.parent.indentation_level
        )

        if allow_deindent:
            self._flags.indentation_level -= 1

        self._output.set_indent(self._flags.indentation_level, self._flags.alignment)

    def set_mode(self, mode):
        if self._flags:
            self._flag_store.append(self._flags)
            self._previous_flags = self._flags
        else:
            self._previous_flags = BeautifierFlags(mode)

        self._flags = BeautifierFlags(mode)
        self._flags.apply_base(self._previous_flags, self._output.just_added_newline())
        self._flags.start_line_index = self._output.get_line_number()

        self._output.set_indent(self._flags.indentation_level, self._flags.alignment)

    def restore_mode(self):
        if len(self._flag_store) > 0:
            self._previous_flags = self._flags
            self._flags = self._flag_store.pop()
            if self._previous_flags.mode == MODE.Statement:
                remove_redundant_indentation(self._output, self._previous_flags)

        self._output.set_indent(self._flags.indentation_level, self._flags.alignment)

    def start_of_object_property(self):
        return (
            self._flags.parent.mode == MODE.ObjectLiteral
            and self._flags.mode == MODE.Statement
            and (
                (self._flags.last_token.text == ":" and self._flags.ternary_depth == 0)
                or (reserved_array(self._flags.last_token, ["get", "set"]))
            )
        )

    def start_of_statement(self, current_token):
        start = False
        start = start or (
            reserved_array(self._flags.last_token, ["var", "let", "const"])
            and current_token.type == TOKEN.WORD
        )
        start = start or reserved_word(self._flags.last_token, "do")
        start = start or (
            not (
                self._flags.parent.mode == MODE.ObjectLiteral
                and self._flags.mode == MODE.Statement
            )
            and reserved_array(self._flags.last_token, self._newline_restricted_tokens)
            and not current_token.newlines
        )
        start = start or (
            reserved_word(self._flags.last_token, "else")
            and not (
                reserved_word(current_token, "if")
                and current_token.comments_before is None
            )
        )
        start = start or (
            self._flags.last_token.type == TOKEN.END_EXPR
            and (
                self._previous_flags.mode == MODE.ForInitializer
                or self._previous_flags.mode == MODE.Conditional
            )
        )
        start = start or (
            self._flags.last_token.type == TOKEN.WORD
            and self._flags.mode == MODE.BlockStatement
            and not self._flags.in_case
            and not (current_token.text == "--" or current_token.text == "++")
            and self._last_last_text != "function"
            and current_token.type != TOKEN.WORD
            and current_token.type != TOKEN.RESERVED
        )
        start = start or (
            self._flags.mode == MODE.ObjectLiteral
            and (
                (self._flags.last_token.text == ":" and self._flags.ternary_depth == 0)
                or (reserved_array(self._flags.last_token, ["get", "set"]))
            )
        )

        if start:
            self.set_mode(MODE.Statement)
            self.indent()

            self.handle_whitespace_and_comments(current_token, True)

            # Issue #276:
            # If starting a new statement with [if, for, while, do], push to a new line.
            # if (a) if (b) if(c) d(); else e(); else f();
            if not self.start_of_object_property():
                self.allow_wrap_or_preserved_newline(
                    current_token,
                    reserved_array(current_token, ["do", "for", "if", "while"]),
                )
            return True
        else:
            return False

    def handle_start_expr(self, current_token):
        if self.start_of_statement(current_token):
            # The conditional starts the statement if appropriate.
            pass
        else:
            self.handle_whitespace_and_comments(current_token)

        next_mode = MODE.Expression

        if current_token.text == "[":
            if (
                self._flags.last_token.type == TOKEN.WORD
                or self._flags.last_token.text == ")"
            ):
                if reserved_array(self._flags.last_token, Tokenizer.line_starters):
                    self._output.space_before_token = True
                self.print_token(current_token)
                self.set_mode(next_mode)
                self.indent()
                if self._options.space_in_paren:
                    self._output.space_before_token = True
                return

            next_mode = MODE.ArrayLiteral

            if self.is_array(self._flags.mode):
                if self._flags.last_token.text == "[" or (
                    self._flags.last_token.text == ","
                    and (self._last_last_text == "]" or self._last_last_text == "}")
                ):
                    # ], [ goes to a new line
                    # }, [ goes to a new line
                    if not self._options.keep_array_indentation:
                        self.print_newline()

            if self._flags.last_token.type not in [
                TOKEN.START_EXPR,
                TOKEN.END_EXPR,
                TOKEN.WORD,
                TOKEN.OPERATOR,
                TOKEN.DOT,
            ]:
                self._output.space_before_token = True

        else:
            if self._flags.last_token.type == TOKEN.RESERVED:
                if self._flags.last_token.text == "for":
                    self._output.space_before_token = (
                        self._options.space_before_conditional
                    )
                    next_mode = MODE.ForInitializer
                elif self._flags.last_token.text in ["if", "while", "switch"]:
                    self._output.space_before_token = (
                        self._options.space_before_conditional
                    )
                    next_mode = MODE.Conditional
                elif self._flags.last_word in ["await", "async"]:
                    # Should be a space between await and an IIFE, or async and
                    # an arrow function
                    self._output.space_before_token = True
                elif (
                    self._flags.last_token.text == "import"
                    and current_token.whitespace_before == ""
                ):
                    self._output.space_before_token = False
                elif (
                    self._flags.last_token.text in Tokenizer.line_starters
                    or self._flags.last_token.text == "catch"
                ):
                    self._output.space_before_token = True

            elif self._flags.last_token.type in [TOKEN.EQUALS, TOKEN.OPERATOR]:
                # Support of this kind of newline preservation:
                # a = (b &&
                #     (c || d));
                if not self.start_of_object_property():
                    self.allow_wrap_or_preserved_newline(current_token)
            elif self._flags.last_token.type == TOKEN.WORD:
                self._output.space_before_token = False
                # function name() vs function name ()
                # function* name() vs function* name ()
                # async name() vs async name ()
                # In ES6, you can also define the method properties of an object
                # var obj = {a: function() {}}
                # It can be abbreviated
                # var obj = {a() {}}
                # var obj = { a() {}} vs var obj = { a () {}}
                # var obj = { * a() {}} vs var obj = { * a () {}}
                peek_back_two = self._tokens.peek(-3)
                if self._options.space_after_named_function and peek_back_two:
                    # peek starts at next character so -1 is current token
                    peek_back_three = self._tokens.peek(-4)
                    if reserved_array(peek_back_two, ["async", "function"]) or (
                        peek_back_two.text == "*"
                        and reserved_array(peek_back_three, ["async", "function"])
                    ):
                        self._output.space_before_token = True
                    elif self._flags.mode == MODE.ObjectLiteral:
                        if (peek_back_two.text == "{" or peek_back_two.text == ",") or (
                            peek_back_two.text == "*"
                            and (
                                peek_back_three.text == "{"
                                or peek_back_three.text == ","
                            )
                        ):
                            self._output.space_before_token = True
                    elif self._flags.parent and self._flags.parent.class_start_block:
                        self._output.space_before_token = True
            else:
                # Support preserving wrapped arrow function expressions
                # a.b('c',
                #     () => d.e
                # )
                self.allow_wrap_or_preserved_newline(current_token)

            # function() vs function (), typeof() vs typeof ()
            # function*() vs function* (), yield*() vs yield* ()
            if (
                self._flags.last_token.type == TOKEN.RESERVED
                and (
                    self._flags.last_word == "function"
                    or self._flags.last_word == "typeof"
                )
            ) or (
                self._flags.last_token.text == "*"
                and (
                    self._last_last_text in ["function", "yield"]
                    or (
                        self._flags.mode == MODE.ObjectLiteral
                        and self._last_last_text in ["{", ","]
                    )
                )
            ):
                self._output.space_before_token = (
                    self._options.space_after_anon_function
                )

        if (
            self._flags.last_token.text == ";"
            or self._flags.last_token.type == TOKEN.START_BLOCK
        ):
            self.print_newline()
        elif (
            self._flags.last_token.type
            in [TOKEN.END_EXPR, TOKEN.START_EXPR, TOKEN.END_BLOCK, TOKEN.COMMA]
            or self._flags.last_token.text == "."
        ):
            # do nothing on (( and )( and ][ and ]( and .(
            # TODO: Consider whether forcing this is required.  Review failing
            # tests when removed.
            self.allow_wrap_or_preserved_newline(current_token, current_token.newlines)

        self.print_token(current_token)
        self.set_mode(next_mode)

        if self._options.space_in_paren:
            self._output.space_before_token = True

        # In all cases, if we newline while inside an expression it should be
        # indented.
        self.indent()

    def handle_end_expr(self, current_token):
        # statements inside expressions are not valid syntax, but...
        # statements must all be closed when their container closes
        while self._flags.mode == MODE.Statement:
            self.restore_mode()

        self.handle_whitespace_and_comments(current_token)

        if self._flags.multiline_frame:
            self.allow_wrap_or_preserved_newline(
                current_token,
                current_token.text == "]"
                and self.is_array(self._flags.mode)
                and not self._options.keep_array_indentation,
            )

        if self._options.space_in_paren:
            if (
                self._flags.last_token.type == TOKEN.START_EXPR
                and not self._options.space_in_empty_paren
            ):
                # empty parens are always "()" and "[]", not "( )" or "[ ]"
                self._output.space_before_token = False
                self._output.trim()
            else:
                self._output.space_before_token = True

        self.deindent()
        self.print_token(current_token)
        self.restore_mode()

        remove_redundant_indentation(self._output, self._previous_flags)

        # do {} while () // no statement required after
        if self._flags.do_while and self._previous_flags.mode == MODE.Conditional:
            self._previous_flags.mode = MODE.Expression
            self._flags.do_block = False
            self._flags.do_while = False

    def handle_start_block(self, current_token):
        self.handle_whitespace_and_comments(current_token)

        # Check if this is a BlockStatement that should be treated as a
        # ObjectLiteral
        next_token = self._tokens.peek()
        second_token = self._tokens.peek(1)
        if (
            self._flags.last_word == "switch"
            and self._flags.last_token.type == TOKEN.END_EXPR
        ):
            self.set_mode(MODE.BlockStatement)
            self._flags.in_case_statement = True
        elif self._flags.case_body:
            self.set_mode(MODE.BlockStatement)
        elif second_token is not None and (
            (
                second_token.text in [":", ","]
                and next_token.type in [TOKEN.STRING, TOKEN.WORD, TOKEN.RESERVED]
            )
            or (
                next_token.text in ["get", "set", "..."]
                and second_token.type in [TOKEN.WORD, TOKEN.RESERVED]
            )
        ):
            # We don't support TypeScript,but we didn't break it for a very long time.
            # We'll try to keep not breaking it.
            if self._last_last_text in [
                "class",
                "interface",
            ] and second_token.text not in [":", ","]:
                self.set_mode(MODE.BlockStatement)
            else:
                self.set_mode(MODE.ObjectLiteral)
        elif (
            self._flags.last_token.type == TOKEN.OPERATOR
            and self._flags.last_token.text == "=>"
        ):
            # arrow function: (param1, paramN) => { statements }
            self.set_mode(MODE.BlockStatement)
        elif self._flags.last_token.type in [
            TOKEN.EQUALS,
            TOKEN.START_EXPR,
            TOKEN.COMMA,
            TOKEN.OPERATOR,
        ] or reserved_array(
            self._flags.last_token, ["return", "throw", "import", "default"]
        ):
            # Detecting shorthand function syntax is difficult by scanning forward,
            #     so check the surrounding context.
            # If the block is being returned, imported, export default, passed as arg,
            # assigned with = or assigned in a nested object, treat as an
            # ObjectLiteral.
            self.set_mode(MODE.ObjectLiteral)
        else:
            self.set_mode(MODE.BlockStatement)

        if self._flags.last_token:
            if reserved_array(self._flags.last_token.previous, ["class", "extends"]):
                self._flags.class_start_block = True

        empty_braces = (
            (next_token is not None)
            and next_token.comments_before is None
            and next_token.text == "}"
        )
        empty_anonymous_function = (
            empty_braces
            and self._flags.last_word == "function"
            and self._flags.last_token.type == TOKEN.END_EXPR
        )

        if (
            self._options.brace_preserve_inline
        ):  # check for inline, set inline_frame if so
            # search forward for newline wanted inside this block
            index = 0
            check_token = None
            self._flags.inline_frame = True
            do_loop = True
            while do_loop:
                index += 1
                check_token = self._tokens.peek(index - 1)
                if check_token.newlines:
                    self._flags.inline_frame = False

                do_loop = check_token.type != TOKEN.EOF and not (
                    check_token.type == TOKEN.END_BLOCK
                    and check_token.opened == current_token
                )

        if (
            self._options.brace_style == "expand"
            or (self._options.brace_style == "none" and current_token.newlines)
        ) and not self._flags.inline_frame:
            if self._flags.last_token.type != TOKEN.OPERATOR and (
                empty_anonymous_function
                or self._flags.last_token.type == TOKEN.EQUALS
                or (
                    reserved_array(self._flags.last_token, _special_word_set)
                    and self._flags.last_token.text != "else"
                )
            ):
                self._output.space_before_token = True
            else:
                self.print_newline(preserve_statement_flags=True)
        else:  # collapse || inline_frame
            if self.is_array(self._previous_flags.mode) and (
                self._flags.last_token.type == TOKEN.START_EXPR
                or self._flags.last_token.type == TOKEN.COMMA
            ):
                # if we're preserving inline,
                # allow newline between comma and next brace.
                if self._flags.inline_frame:
                    self.allow_wrap_or_preserved_newline(current_token)
                    self._flags.inline_frame = True
                    self._previous_flags.multiline_frame = (
                        self._previous_flags.multiline_frame
                        or self._flags.multiline_frame
                    )
                    self._flags.multiline_frame = False
                elif self._flags.last_token.type == TOKEN.COMMA:
                    self._output.space_before_token = True

            elif self._flags.last_token.type not in [TOKEN.OPERATOR, TOKEN.START_EXPR]:
                if (
                    self._flags.last_token.type in [TOKEN.START_BLOCK, TOKEN.SEMICOLON]
                    and not self._flags.inline_frame
                ):
                    self.print_newline()
                else:
                    self._output.space_before_token = True

        self.print_token(current_token)
        self.indent()

        # Except for specific cases, open braces are followed by a new line.
        if not empty_braces and not (
            self._options.brace_preserve_inline and self._flags.inline_frame
        ):
            self.print_newline()

    def handle_end_block(self, current_token):
        # statements must all be closed when their container closes
        self.handle_whitespace_and_comments(current_token)

        while self._flags.mode == MODE.Statement:
            self.restore_mode()

        empty_braces = self._flags.last_token.type == TOKEN.START_BLOCK

        # try inline_frame (only set if opt.braces-preserve-inline) first
        if self._flags.inline_frame and not empty_braces:
            self._output.space_before_token = True
        elif self._options.brace_style == "expand":
            if not empty_braces:
                self.print_newline()
        else:
            # skip {}
            if not empty_braces:
                if (
                    self.is_array(self._flags.mode)
                    and self._options.keep_array_indentation
                ):
                    self._options.keep_array_indentation = False
                    self.print_newline()
                    self._options.keep_array_indentation = True
                else:
                    self.print_newline()

        self.restore_mode()
        self.print_token(current_token)

    def handle_word(self, current_token):
        if current_token.type == TOKEN.RESERVED:
            if (
                current_token.text in ["set", "get"]
                and self._flags.mode != MODE.ObjectLiteral
            ):
                current_token.type = TOKEN.WORD
            elif current_token.text == "import" and self._tokens.peek().text in [
                "(",
                ".",
            ]:
                current_token.type = TOKEN.WORD
            elif current_token.text in ["as", "from"] and not self._flags.import_block:
                current_token.type = TOKEN.WORD
            elif self._flags.mode == MODE.ObjectLiteral:
                next_token = self._tokens.peek()
                if next_token.text == ":":
                    current_token.type = TOKEN.WORD

        if self.start_of_statement(current_token):
            # The conditional starts the statement if appropriate.
            if (
                reserved_array(self._flags.last_token, ["var", "let", "const"])
                and current_token.type == TOKEN.WORD
            ):
                self._flags.declaration_statement = True

        elif (
            current_token.newlines
            and not self.is_expression(self._flags.mode)
            and (
                self._flags.last_token.type != TOKEN.OPERATOR
                or (
                    self._flags.last_token.text == "--"
                    or self._flags.last_token.text == "++"
                )
            )
            and self._flags.last_token.type != TOKEN.EQUALS
            and (
                self._options.preserve_newlines
                or not reserved_array(
                    self._flags.last_token, ["var", "let", "const", "set", "get"]
                )
            )
        ):
            self.handle_whitespace_and_comments(current_token)
            self.print_newline()
        else:
            self.handle_whitespace_and_comments(current_token)

        if self._flags.do_block and not self._flags.do_while:
            if reserved_word(current_token, "while"):
                # do {} ## while ()
                self._output.space_before_token = True
                self.print_token(current_token)
                self._output.space_before_token = True
                self._flags.do_while = True
                return
            else:
                # do {} should always have while as the next word.
                # if we don't see the expected while, recover
                self.print_newline()
                self._flags.do_block = False

        # if may be followed by else, or not
        # Bare/inline ifs are tricky
        # Need to unwind the modes correctly: if (a) if (b) c(); else d(); else
        # e();
        if self._flags.if_block:
            if (not self._flags.else_block) and reserved_word(current_token, "else"):
                self._flags.else_block = True
            else:
                while self._flags.mode == MODE.Statement:
                    self.restore_mode()

                self._flags.if_block = False

        if self._flags.in_case_statement and reserved_array(
            current_token, ["case", "default"]
        ):
            self.print_newline()
            if (not self._flags.case_block) and (
                self._flags.case_body or self._options.jslint_happy
            ):
                self.deindent()
            self._flags.case_body = False
            self.print_token(current_token)
            self._flags.in_case = True
            return

        if self._flags.last_token.type in [
            TOKEN.COMMA,
            TOKEN.START_EXPR,
            TOKEN.EQUALS,
            TOKEN.OPERATOR,
        ]:
            if not self.start_of_object_property() and not (
                # start of object property is different for numeric values with +/- prefix operators
                self._flags.last_token.text in ["+", "-"]
                and self._last_last_text == ":"
                and self._flags.parent.mode == MODE.ObjectLiteral
            ):
                self.allow_wrap_or_preserved_newline(current_token)

        if reserved_word(current_token, "function"):
            if self._flags.last_token.text in ["}", ";"] or (
                self._output.just_added_newline()
                and not (
                    self._flags.last_token.text in ["(", "[", "{", ":", "=", ","]
                    or self._flags.last_token.type == TOKEN.OPERATOR
                )
            ):
                # make sure there is a nice clean space of at least one blank line
                # before a new function definition, except in arrays
                if (
                    not self._output.just_added_blankline()
                    and current_token.comments_before is None
                ):
                    self.print_newline()
                    self.print_newline(True)

            if (
                self._flags.last_token.type == TOKEN.RESERVED
                or self._flags.last_token.type == TOKEN.WORD
            ):
                if reserved_array(
                    self._flags.last_token, ["get", "set", "new", "export"]
                ) or reserved_array(
                    self._flags.last_token, self._newline_restricted_tokens
                ):
                    self._output.space_before_token = True
                elif (
                    reserved_word(self._flags.last_token, "default")
                    and self._last_last_text == "export"
                ):
                    self._output.space_before_token = True
                elif self._flags.last_token.text == "declare":
                    # accomodates Typescript declare function formatting
                    self._output.space_before_token = True
                else:
                    self.print_newline()
            elif (
                self._flags.last_token.type == TOKEN.OPERATOR
                or self._flags.last_token.text == "="
            ):
                # foo = function
                self._output.space_before_token = True
            elif not self._flags.multiline_frame and (
                self.is_expression(self._flags.mode) or self.is_array(self._flags.mode)
            ):
                # (function
                pass
            else:
                self.print_newline()

            self.print_token(current_token)
            self._flags.last_word = current_token.text
            return

        prefix = "NONE"

        if self._flags.last_token.type == TOKEN.END_BLOCK:
            if self._previous_flags.inline_frame:
                prefix = "SPACE"
            elif not reserved_array(
                current_token, ["else", "catch", "finally", "from"]
            ):
                prefix = "NEWLINE"
            else:
                if self._options.brace_style in ["expand", "end-expand"] or (
                    self._options.brace_style == "none" and current_token.newlines
                ):
                    prefix = "NEWLINE"
                else:
                    prefix = "SPACE"
                    self._output.space_before_token = True
        elif (
            self._flags.last_token.type == TOKEN.SEMICOLON
            and self._flags.mode == MODE.BlockStatement
        ):
            # TODO: Should this be for STATEMENT as well?
            prefix = "NEWLINE"
        elif self._flags.last_token.type == TOKEN.SEMICOLON and self.is_expression(
            self._flags.mode
        ):
            prefix = "SPACE"
        elif self._flags.last_token.type == TOKEN.STRING:
            prefix = "NEWLINE"
        elif (
            self._flags.last_token.type == TOKEN.RESERVED
            or self._flags.last_token.type == TOKEN.WORD
            or (
                self._flags.last_token.text == "*"
                and (
                    self._last_last_text in ["function", "yield"]
                    or (
                        self._flags.mode == MODE.ObjectLiteral
                        and self._last_last_text in ["{", ","]
                    )
                )
            )
        ):
            prefix = "SPACE"
        elif self._flags.last_token.type == TOKEN.START_BLOCK:
            if self._flags.inline_frame:
                prefix = "SPACE"
            else:
                prefix = "NEWLINE"
        elif self._flags.last_token.type == TOKEN.END_EXPR:
            self._output.space_before_token = True
            prefix = "NEWLINE"

        if (
            reserved_array(current_token, Tokenizer.line_starters)
            and self._flags.last_token.text != ")"
        ):
            if (
                self._flags.inline_frame
                or self._flags.last_token.text == "else "
                or self._flags.last_token.text == "export"
            ):
                prefix = "SPACE"
            else:
                prefix = "NEWLINE"

        if reserved_array(current_token, ["else", "catch", "finally"]):
            if (
                (
                    not (
                        self._flags.last_token.type == TOKEN.END_BLOCK
                        and self._previous_flags.mode == MODE.BlockStatement
                    )
                )
                or self._options.brace_style == "expand"
                or self._options.brace_style == "end-expand"
                or (self._options.brace_style == "none" and current_token.newlines)
            ) and not self._flags.inline_frame:
                self.print_newline()
            else:
                self._output.trim(True)
                # If we trimmed and there's something other than a close block before us
                # put a newline back in.  Handles '} // comment' scenario.
                if self._output.current_line.last() != "}":
                    self.print_newline()

                self._output.space_before_token = True

        elif prefix == "NEWLINE":
            if reserved_array(self._flags.last_token, _special_word_set):
                # no newline between return nnn
                self._output.space_before_token = True
            elif self._flags.last_token.text == "declare" and reserved_array(
                current_token, ["var", "let", "const"]
            ):
                # accomodates Typescript declare formatting
                self._output.space_before_token = True
            elif self._flags.last_token.type != TOKEN.END_EXPR:
                if (
                    self._flags.last_token.type != TOKEN.START_EXPR
                    or not (reserved_array(current_token, ["var", "let", "const"]))
                ) and self._flags.last_token.text != ":":
                    # no need to force newline on VAR -
                    # for (var x = 0...
                    if (
                        reserved_word(current_token, "if")
                        and self._flags.last_token.text == "else"
                    ):
                        self._output.space_before_token = True
                    else:
                        self.print_newline()
            elif (
                reserved_array(current_token, Tokenizer.line_starters)
                and self._flags.last_token.text != ")"
            ):
                self.print_newline()
        elif (
            self._flags.multiline_frame
            and self.is_array(self._flags.mode)
            and self._flags.last_token.text == ","
            and self._last_last_text == "}"
        ):
            self.print_newline()  # }, in lists get a newline
        elif prefix == "SPACE":
            self._output.space_before_token = True

        if current_token.previous and (
            current_token.previous.type == TOKEN.WORD
            or current_token.previous.type == TOKEN.RESERVED
        ):
            self._output.space_before_token = True

        self.print_token(current_token)
        self._flags.last_word = current_token.text

        if current_token.type == TOKEN.RESERVED:
            if current_token.text == "do":
                self._flags.do_block = True
            elif current_token.text == "if":
                self._flags.if_block = True
            elif current_token.text == "import":
                self._flags.import_block = True
            elif current_token.text == "from" and self._flags.import_block:
                self._flags.import_block = False

    def handle_semicolon(self, current_token):
        if self.start_of_statement(current_token):
            # The conditional starts the statement if appropriate.
            # Semicolon can be the start (and end) of a statement
            self._output.space_before_token = False
        else:
            self.handle_whitespace_and_comments(current_token)

        next_token = self._tokens.peek()
        while (
            self._flags.mode == MODE.Statement
            and not (self._flags.if_block and reserved_word(next_token, "else"))
            and not self._flags.do_block
        ):
            self.restore_mode()

        if self._flags.import_block:
            self._flags.import_block = False

        self.print_token(current_token)

    def handle_string(self, current_token):
        if (
            current_token.text[0] == "`"
            and current_token.newlines == 0
            and current_token.whitespace_before == ""
            and (
                self._flags.last_token.type == TOKEN.WORD
                or current_token.previous.text == ")"
            )
        ):
            # This conditional checks backtick strings and makes no changes
            pass
        elif self.start_of_statement(current_token):
            # The conditional starts the statement if appropriate.
            # One difference - strings want at least a space before
            self._output.space_before_token = True
        else:
            self.handle_whitespace_and_comments(current_token)
            if (
                self._flags.last_token.type in [TOKEN.RESERVED, TOKEN.WORD]
                or self._flags.inline_frame
            ):
                self._output.space_before_token = True
            elif self._flags.last_token.type in [
                TOKEN.COMMA,
                TOKEN.START_EXPR,
                TOKEN.EQUALS,
                TOKEN.OPERATOR,
            ]:
                if not self.start_of_object_property():
                    self.allow_wrap_or_preserved_newline(current_token)
            elif (
                current_token.text[0] == "`"
                and self._flags.last_token.type == TOKEN.END_EXPR
                and current_token.previous.text in ["]", ")"]
                and current_token.newlines == 0
            ):
                self._output.space_before_token = True
            else:
                self.print_newline()

        self.print_token(current_token)

    def handle_equals(self, current_token):
        if self.start_of_statement(current_token):
            # The conditional starts the statement if appropriate.
            pass
        else:
            self.handle_whitespace_and_comments(current_token)

        if self._flags.declaration_statement:
            # just got an '=' in a var-line, different line breaking rules will
            # apply
            self._flags.declaration_assignment = True

        self._output.space_before_token = True
        self.print_token(current_token)
        self._output.space_before_token = True

    def handle_comma(self, current_token):
        self.handle_whitespace_and_comments(current_token, True)

        self.print_token(current_token)
        self._output.space_before_token = True

        if self._flags.declaration_statement:
            if self.is_expression(self._flags.parent.mode):
                # do not break on comma, for ( var a = 1, b = 2
                self._flags.declaration_assignment = False

            if self._flags.declaration_assignment:
                self._flags.declaration_assignment = False
                self.print_newline(preserve_statement_flags=True)
            elif self._options.comma_first:
                # for comma-first, we want to allow a newline before the comma
                # to turn into a newline after the comma, which we will fixup
                # later
                self.allow_wrap_or_preserved_newline(current_token)

        elif self._flags.mode == MODE.ObjectLiteral or (
            self._flags.mode == MODE.Statement
            and self._flags.parent.mode == MODE.ObjectLiteral
        ):
            if self._flags.mode == MODE.Statement:
                self.restore_mode()

            if not self._flags.inline_frame:
                self.print_newline()
        elif self._options.comma_first:
            # EXPR or DO_BLOCK
            # for comma-first, we want to allow a newline before the comma
            # to turn into a newline after the comma, which we will fixup later
            self.allow_wrap_or_preserved_newline(current_token)

    def handle_operator(self, current_token):
        isGeneratorAsterisk = current_token.text == "*" and (
            reserved_array(self._flags.last_token, ["function", "yield"])
            or (
                self._flags.last_token.type
                in [TOKEN.START_BLOCK, TOKEN.COMMA, TOKEN.END_BLOCK, TOKEN.SEMICOLON]
            )
        )
        isUnary = current_token.text in ["+", "-"] and (
            self._flags.last_token.type
            in [TOKEN.START_BLOCK, TOKEN.START_EXPR, TOKEN.EQUALS, TOKEN.OPERATOR]
            or self._flags.last_token.text in Tokenizer.line_starters
            or self._flags.last_token.text == ","
        )

        if self.start_of_statement(current_token):
            # The conditional starts the statement if appropriate.
            pass
        else:
            preserve_statement_flags = not isGeneratorAsterisk
            self.handle_whitespace_and_comments(current_token, preserve_statement_flags)

        # hack for actionscript's import .*;
        if current_token.text == "*" and self._flags.last_token.type == TOKEN.DOT:
            self.print_token(current_token)
            return

        if current_token.text == "::":
            # no spaces around the exotic namespacing syntax operator
            self.print_token(current_token)
            return

        if current_token.text in ["-", "+"] and self.start_of_object_property():
            # numeric value with +/- symbol in front as a property
            self.print_token(current_token)
            return

        # Allow line wrapping between operators when operator_position is
        #   set to before or preserve
        if (
            self._flags.last_token.type == TOKEN.OPERATOR
            and self._options.operator_position in OPERATOR_POSITION_BEFORE_OR_PRESERVE
        ):
            self.allow_wrap_or_preserved_newline(current_token)

        if current_token.text == ":" and self._flags.in_case:
            self.print_token(current_token)
            self._flags.in_case = False
            self._flags.case_body = True
            if self._tokens.peek().type != TOKEN.START_BLOCK:
                self.indent()
                self.print_newline()
                self._flags.case_block = False
            else:
                self._output.space_before_token = True
                self._flags.case_block = True

            return

        space_before = True
        space_after = True
        in_ternary = False

        if current_token.text == ":":
            if self._flags.ternary_depth == 0:
                # Colon is invalid javascript outside of ternary and object,
                # but do our best to guess what was meant.
                space_before = False
            else:
                self._flags.ternary_depth -= 1
                in_ternary = True
        elif current_token.text == "?":
            self._flags.ternary_depth += 1

        # let's handle the operator_position option prior to any conflicting
        # logic
        if (
            (not isUnary)
            and (not isGeneratorAsterisk)
            and self._options.preserve_newlines
            and current_token.text in Tokenizer.positionable_operators
        ):
            isColon = current_token.text == ":"
            isTernaryColon = isColon and in_ternary
            isOtherColon = isColon and not in_ternary

            if self._options.operator_position == OPERATOR_POSITION["before_newline"]:
                # if the current token is : and it's not a ternary statement
                # then we set space_before to false
                self._output.space_before_token = not isOtherColon

                self.print_token(current_token)

                if (not isColon) or isTernaryColon:
                    self.allow_wrap_or_preserved_newline(current_token)

                self._output.space_before_token = True

                return

            elif self._options.operator_position == OPERATOR_POSITION["after_newline"]:
                # if the current token is anything but colon, or (via deduction) it's a colon and in a ternary statement,
                #   then print a newline.
                self._output.space_before_token = True

                if (not isColon) or isTernaryColon:
                    if self._tokens.peek().newlines:
                        self.print_newline(preserve_statement_flags=True)
                    else:
                        self.allow_wrap_or_preserved_newline(current_token)
                else:
                    self._output.space_before_token = False

                self.print_token(current_token)

                self._output.space_before_token = True
                return

            elif (
                self._options.operator_position == OPERATOR_POSITION["preserve_newline"]
            ):
                if not isOtherColon:
                    self.allow_wrap_or_preserved_newline(current_token)

                # if we just added a newline, or the current token is : and it's not a ternary statement,
                #   then we set space_before to false
                self._output.space_before_token = not (
                    self._output.just_added_newline() or isOtherColon
                )

                self.print_token(current_token)

                self._output.space_before_token = True
                return

        if isGeneratorAsterisk:
            self.allow_wrap_or_preserved_newline(current_token)
            space_before = False
            next_token = self._tokens.peek()
            space_after = next_token and next_token.type in [TOKEN.WORD, TOKEN.RESERVED]
        elif current_token.text == "...":
            self.allow_wrap_or_preserved_newline(current_token)
            space_before = self._flags.last_token.type == TOKEN.START_BLOCK
            space_after = False
        elif current_token.text in ["--", "++", "!", "~"] or isUnary:
            if (
                self._flags.last_token.type == TOKEN.COMMA
                or self._flags.last_token.type == TOKEN.START_EXPR
            ):
                self.allow_wrap_or_preserved_newline(current_token)

            space_before = False
            space_after = False

            # http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
            # if there is a newline between -- or ++ and anything else we
            # should preserve it.
            if current_token.newlines and (
                current_token.text == "--"
                or current_token.text == "++"
                or current_token.text == "~"
            ):
                new_line_needed = (
                    reserved_array(self._flags.last_token, _special_word_set)
                    and current_token.newlines
                )
                if new_line_needed and (
                    self._previous_flags.if_block or self._previous_flags.else_block
                ):
                    self.restore_mode()
                self.print_newline(new_line_needed, True)

            if self._flags.last_token.text == ";" and self.is_expression(
                self._flags.mode
            ):
                # for (;; ++i)
                #         ^^
                space_before = True

            if self._flags.last_token.type == TOKEN.RESERVED:
                space_before = True
            elif self._flags.last_token.type == TOKEN.END_EXPR:
                space_before = not (
                    self._flags.last_token.text == "]"
                    and current_token.text in ["--", "++"]
                )
            elif self._flags.last_token.type == TOKEN.OPERATOR:
                # a++ + ++b
                # a - -b
                space_before = current_token.text in [
                    "--",
                    "-",
                    "++",
                    "+",
                ] and self._flags.last_token.text in ["--", "-", "++", "+"]
                # + and - are not unary when preceeded by -- or ++ operator
                # a-- + b
                # a * +b
                # a - -b
                if current_token.text in ["-", "+"] and self._flags.last_token.text in [
                    "--",
                    "++",
                ]:
                    space_after = True

            if (
                (
                    self._flags.mode == MODE.BlockStatement
                    and not self._flags.inline_frame
                )
                or self._flags.mode == MODE.Statement
            ) and self._flags.last_token.text in ["{", ";"]:
                # { foo: --i }
                # foo(): --bar
                self.print_newline()

        if space_before:
            self._output.space_before_token = True

        self.print_token(current_token)

        if space_after:
            self._output.space_before_token = True

    def handle_block_comment(self, current_token, preserve_statement_flags):
        if self._output.raw:
            self._output.add_raw_token(current_token)
            if (
                current_token.directives
                and current_token.directives.get("preserve") == "end"
            ):
                # If we're testing the raw output behavior, do not allow a
                # directive to turn it off.
                self._output.raw = self._options.test_output_raw
            return

        if current_token.directives:
            self.print_newline(preserve_statement_flags=preserve_statement_flags)
            self.print_token(current_token)
            if current_token.directives.get("preserve") == "start":
                self._output.raw = True

            self.print_newline(preserve_statement_flags=True)
            return

        # inline block
        if (
            not self.acorn.newline.search(current_token.text)
            and not current_token.newlines
        ):
            self._output.space_before_token = True
            self.print_token(current_token)
            self._output.space_before_token = True
            return

        lines = self.acorn.allLineBreaks.split(current_token.text)
        javadoc = False
        starless = False
        last_indent = current_token.whitespace_before
        last_indent_length = len(last_indent)

        # block comment starts with a new line
        self.print_newline(preserve_statement_flags=preserve_statement_flags)

        # first line always indented
        self.print_token(current_token, lines[0])
        self.print_newline(preserve_statement_flags=preserve_statement_flags)

        if len(lines) > 1:
            lines = lines[1:]
            javadoc = not any(
                l for l in lines if (l.strip() == "" or (l.lstrip())[0] != "*")
            )
            starless = all(l.startswith(last_indent) or l.strip() == "" for l in lines)

            if javadoc:
                self._flags.alignment = 1

            for line in lines:
                if javadoc:
                    # javadoc: reformat and re-indent
                    self.print_token(current_token, line.lstrip())
                elif starless and len(line) > last_indent_length:
                    # starless: re-indent non-empty content, avoiding trim
                    self.print_token(current_token, line[last_indent_length:])
                else:
                    # normal comments output raw
                    self._output.current_line.set_indent(-1)
                    self._output.add_token(line)

                # for comments on their own line or  more than one line,
                # make sure there's a new line after
                self.print_newline(preserve_statement_flags=preserve_statement_flags)

            self._flags.alignment = 0

    def handle_comment(self, current_token, preserve_statement_flags):
        if current_token.newlines:
            self.print_newline(preserve_statement_flags=preserve_statement_flags)

        if not current_token.newlines:
            self._output.trim(True)

        self._output.space_before_token = True
        self.print_token(current_token)
        self.print_newline(preserve_statement_flags=preserve_statement_flags)

    def handle_dot(self, current_token):
        if self.start_of_statement(current_token):
            # The conditional starts the statement if appropriate.
            pass
        else:
            self.handle_whitespace_and_comments(current_token, True)

        if re.search("^([0-9])+$", self._flags.last_token.text):
            self._output.space_before_token = True

        if reserved_array(self._flags.last_token, _special_word_set):
            self._output.space_before_token = False
        else:
            # allow preserved newlines before dots in general
            # force newlines on dots after close paren when break_chained - for
            # bar().baz()
            self.allow_wrap_or_preserved_newline(
                current_token,
                self._flags.last_token.text == ")"
                and self._options.break_chained_methods,
            )

        # Only unindent chained method dot if this dot starts a new line.
        # Otherwise the automatic extra indentation removal
        # will handle any over indent
        if self._options.unindent_chained_methods and self._output.just_added_newline():
            self.deindent()

        self.print_token(current_token)

    def handle_unknown(self, current_token, preserve_statement_flags):
        self.print_token(current_token)
        if current_token.text[-1] == "\n":
            self.print_newline(preserve_statement_flags=preserve_statement_flags)

    def handle_eof(self, current_token):
        # Unwind any open statements
        while self._flags.mode == MODE.Statement:
            self.restore_mode()

        self.handle_whitespace_and_comments(current_token)