reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    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
 1648
 1649
 1650
 1651
 1652
 1653
 1654
 1655
 1656
 1657
 1658
 1659
 1660
 1661
 1662
 1663
 1664
 1665
 1666
 1667
 1668
 1669
 1670
 1671
 1672
 1673
 1674
 1675
 1676
 1677
 1678
 1679
 1680
 1681
 1682
 1683
 1684
 1685
 1686
 1687
 1688
 1689
 1690
 1691
 1692
 1693
 1694
 1695
 1696
 1697
 1698
 1699
 1700
 1701
 1702
 1703
 1704
 1705
 1706
 1707
 1708
 1709
 1710
 1711
 1712
 1713
 1714
 1715
 1716
 1717
 1718
 1719
 1720
 1721
 1722
 1723
 1724
 1725
 1726
 1727
 1728
 1729
 1730
 1731
 1732
 1733
 1734
 1735
 1736
 1737
 1738
 1739
 1740
 1741
 1742
 1743
 1744
 1745
 1746
 1747
 1748
 1749
 1750
 1751
 1752
 1753
 1754
 1755
 1756
 1757
 1758
 1759
 1760
 1761
 1762
 1763
 1764
 1765
 1766
 1767
 1768
 1769
 1770
 1771
 1772
 1773
 1774
 1775
 1776
 1777
 1778
 1779
 1780
 1781
 1782
 1783
 1784
 1785
 1786
 1787
 1788
 1789
 1790
 1791
 1792
 1793
 1794
 1795
 1796
 1797
 1798
 1799
 1800
 1801
 1802
 1803
 1804
 1805
 1806
 1807
 1808
 1809
 1810
 1811
 1812
 1813
 1814
 1815
 1816
 1817
 1818
 1819
 1820
 1821
 1822
 1823
 1824
 1825
 1826
 1827
 1828
 1829
 1830
 1831
 1832
 1833
 1834
 1835
 1836
 1837
 1838
 1839
 1840
 1841
 1842
 1843
 1844
 1845
 1846
 1847
 1848
 1849
 1850
 1851
 1852
 1853
 1854
 1855
 1856
 1857
 1858
 1859
 1860
 1861
 1862
 1863
 1864
 1865
 1866
 1867
 1868
 1869
 1870
 1871
 1872
 1873
 1874
 1875
 1876
 1877
 1878
 1879
 1880
 1881
 1882
 1883
 1884
 1885
 1886
 1887
 1888
 1889
 1890
 1891
 1892
 1893
 1894
 1895
 1896
 1897
 1898
 1899
 1900
 1901
 1902
 1903
 1904
 1905
 1906
 1907
 1908
 1909
 1910
 1911
 1912
 1913
 1914
 1915
 1916
 1917
 1918
 1919
 1920
 1921
 1922
 1923
 1924
 1925
 1926
 1927
 1928
 1929
 1930
 1931
 1932
 1933
 1934
 1935
 1936
 1937
 1938
 1939
 1940
 1941
 1942
 1943
 1944
 1945
 1946
 1947
 1948
 1949
 1950
 1951
 1952
 1953
 1954
 1955
 1956
 1957
 1958
 1959
 1960
 1961
 1962
 1963
 1964
 1965
 1966
 1967
 1968
 1969
 1970
 1971
 1972
 1973
 1974
 1975
 1976
 1977
 1978
 1979
 1980
 1981
 1982
 1983
 1984
 1985
 1986
 1987
 1988
 1989
 1990
 1991
 1992
 1993
 1994
 1995
 1996
 1997
 1998
 1999
 2000
 2001
 2002
 2003
 2004
 2005
 2006
 2007
 2008
 2009
 2010
 2011
 2012
 2013
 2014
 2015
 2016
 2017
 2018
 2019
 2020
 2021
 2022
 2023
 2024
 2025
 2026
 2027
 2028
 2029
 2030
 2031
 2032
 2033
 2034
 2035
 2036
 2037
 2038
 2039
 2040
 2041
 2042
 2043
 2044
 2045
 2046
 2047
 2048
 2049
 2050
 2051
 2052
 2053
 2054
 2055
 2056
 2057
 2058
 2059
 2060
 2061
 2062
 2063
 2064
 2065
 2066
 2067
 2068
 2069
 2070
 2071
 2072
 2073
 2074
 2075
 2076
 2077
 2078
 2079
 2080
 2081
 2082
 2083
 2084
 2085
 2086
 2087
 2088
 2089
 2090
 2091
 2092
 2093
 2094
 2095
 2096
 2097
 2098
 2099
 2100
 2101
 2102
 2103
 2104
 2105
 2106
 2107
 2108
 2109
 2110
 2111
 2112
 2113
 2114
 2115
 2116
 2117
 2118
 2119
 2120
 2121
 2122
 2123
 2124
 2125
 2126
 2127
 2128
 2129
 2130
 2131
 2132
 2133
 2134
 2135
 2136
 2137
 2138
 2139
 2140
 2141
 2142
 2143
 2144
 2145
 2146
 2147
 2148
 2149
 2150
 2151
 2152
 2153
 2154
 2155
 2156
 2157
 2158
 2159
 2160
 2161
 2162
 2163
 2164
 2165
 2166
 2167
 2168
 2169
 2170
 2171
 2172
 2173
 2174
 2175
 2176
 2177
 2178
 2179
 2180
 2181
 2182
 2183
 2184
 2185
 2186
 2187
 2188
 2189
 2190
 2191
 2192
 2193
 2194
 2195
 2196
 2197
 2198
 2199
 2200
 2201
 2202
 2203
 2204
 2205
 2206
 2207
 2208
 2209
 2210
 2211
 2212
 2213
 2214
 2215
 2216
 2217
 2218
 2219
 2220
 2221
 2222
 2223
 2224
 2225
 2226
 2227
 2228
 2229
 2230
 2231
 2232
 2233
 2234
 2235
 2236
 2237
 2238
 2239
 2240
 2241
 2242
 2243
 2244
 2245
 2246
 2247
 2248
 2249
 2250
 2251
 2252
 2253
 2254
 2255
 2256
 2257
 2258
 2259
 2260
 2261
 2262
 2263
 2264
 2265
 2266
 2267
 2268
 2269
 2270
 2271
 2272
 2273
 2274
 2275
 2276
 2277
 2278
 2279
 2280
 2281
 2282
 2283
 2284
 2285
 2286
 2287
 2288
 2289
 2290
 2291
 2292
 2293
 2294
 2295
 2296
 2297
 2298
 2299
 2300
 2301
 2302
 2303
 2304
 2305
 2306
 2307
 2308
 2309
 2310
 2311
 2312
 2313
 2314
 2315
 2316
 2317
 2318
 2319
 2320
 2321
 2322
 2323
 2324
 2325
 2326
 2327
 2328
 2329
 2330
 2331
 2332
 2333
 2334
 2335
 2336
 2337
 2338
 2339
 2340
 2341
 2342
 2343
 2344
 2345
 2346
 2347
 2348
 2349
 2350
 2351
 2352
 2353
 2354
 2355
 2356
 2357
 2358
 2359
 2360
 2361
 2362
 2363
//===- unittest/Format/FormatTestJS.cpp - Formatting unit tests for JS ----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "FormatTestUtils.h"
#include "clang/Format/Format.h"
#include "llvm/Support/Debug.h"
#include "gtest/gtest.h"

#define DEBUG_TYPE "format-test"

namespace clang {
namespace format {

class FormatTestJS : public ::testing::Test {
protected:
  static std::string format(llvm::StringRef Code, unsigned Offset,
                            unsigned Length, const FormatStyle &Style) {
    LLVM_DEBUG(llvm::errs() << "---\n");
    LLVM_DEBUG(llvm::errs() << Code << "\n\n");
    std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
    FormattingAttemptStatus Status;
    tooling::Replacements Replaces =
        reformat(Style, Code, Ranges, "<stdin>", &Status);
    EXPECT_TRUE(Status.FormatComplete);
    auto Result = applyAllReplacements(Code, Replaces);
    EXPECT_TRUE(static_cast<bool>(Result));
    LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
    return *Result;
  }

  static std::string format(
      llvm::StringRef Code,
      const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
    return format(Code, 0, Code.size(), Style);
  }

  static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
    FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
    Style.ColumnLimit = ColumnLimit;
    return Style;
  }

  static void verifyFormat(
      llvm::StringRef Code,
      const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
    EXPECT_EQ(Code.str(), format(Code, Style))
        << "Expected code is not stable";
    std::string Result = format(test::messUp(Code), Style);
    EXPECT_EQ(Code.str(), Result) << "Formatted:\n" << Result;
  }

  static void verifyFormat(
      llvm::StringRef Expected,
      llvm::StringRef Code,
      const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
    EXPECT_EQ(Expected.str(), format(Expected, Style))
        << "Expected code is not stable";
    std::string Result = format(Code, Style);
    EXPECT_EQ(Expected.str(), Result) << "Formatted:\n" << Result;
  }
};

TEST_F(FormatTestJS, BlockComments) {
  verifyFormat("/* aaaaaaaaaaaaa */ aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  // Breaks after a single line block comment.
  EXPECT_EQ("aaaaa = bbbb.ccccccccccccccc(\n"
            "    /** @type_{!cccc.rrrrrrr.MMMMMMMMMMMM.LLLLLLLLLLL.lala} */\n"
            "    mediaMessage);",
            format("aaaaa = bbbb.ccccccccccccccc(\n"
                   "    /** "
                   "@type_{!cccc.rrrrrrr.MMMMMMMMMMMM.LLLLLLLLLLL.lala} */ "
                   "mediaMessage);",
                   getGoogleJSStyleWithColumns(70)));
  // Breaks after a multiline block comment.
  EXPECT_EQ(
      "aaaaa = bbbb.ccccccccccccccc(\n"
      "    /**\n"
      "     * @type_{!cccc.rrrrrrr.MMMMMMMMMMMM.LLLLLLLLLLL.lala}\n"
      "     */\n"
      "    mediaMessage);",
      format("aaaaa = bbbb.ccccccccccccccc(\n"
             "    /**\n"
             "     * @type_{!cccc.rrrrrrr.MMMMMMMMMMMM.LLLLLLLLLLL.lala}\n"
             "     */ mediaMessage);",
             getGoogleJSStyleWithColumns(70)));
}

TEST_F(FormatTestJS, JSDocComments) {
  // Break the first line of a multiline jsdoc comment.
  EXPECT_EQ("/**\n"
            " * jsdoc line 1\n"
            " * jsdoc line 2\n"
            " */",
            format("/** jsdoc line 1\n"
                   " * jsdoc line 2\n"
                   " */",
                   getGoogleJSStyleWithColumns(20)));
  // Both break after '/**' and break the line itself.
  EXPECT_EQ("/**\n"
            " * jsdoc line long\n"
            " * long jsdoc line 2\n"
            " */",
            format("/** jsdoc line long long\n"
                   " * jsdoc line 2\n"
                   " */",
                   getGoogleJSStyleWithColumns(20)));
  // Break a short first line if the ending '*/' is on a newline.
  EXPECT_EQ("/**\n"
            " * jsdoc line 1\n"
            " */",
            format("/** jsdoc line 1\n"
                   " */", getGoogleJSStyleWithColumns(20)));
  // Don't break the first line of a short single line jsdoc comment.
  EXPECT_EQ("/** jsdoc line 1 */",
            format("/** jsdoc line 1 */", getGoogleJSStyleWithColumns(20)));
  // Don't break the first line of a single line jsdoc comment if it just fits
  // the column limit.
  EXPECT_EQ("/** jsdoc line 12 */",
            format("/** jsdoc line 12 */", getGoogleJSStyleWithColumns(20)));
  // Don't break after '/**' and before '*/' if there is no space between
  // '/**' and the content.
  EXPECT_EQ(
      "/*** nonjsdoc long\n"
      " * line */",
      format("/*** nonjsdoc long line */", getGoogleJSStyleWithColumns(20)));
  EXPECT_EQ(
      "/**strange long long\n"
      " * line */",
      format("/**strange long long line */", getGoogleJSStyleWithColumns(20)));
  // Break the first line of a single line jsdoc comment if it just exceeds the
  // column limit.
  EXPECT_EQ("/**\n"
            " * jsdoc line 123\n"
            " */",
            format("/** jsdoc line 123 */", getGoogleJSStyleWithColumns(20)));
  // Break also if the leading indent of the first line is more than 1 column.
  EXPECT_EQ("/**\n"
            " * jsdoc line 123\n"
            " */",
            format("/**  jsdoc line 123 */", getGoogleJSStyleWithColumns(20)));
  // Break also if the leading indent of the first line is more than 1 column.
  EXPECT_EQ("/**\n"
            " * jsdoc line 123\n"
            " */",
            format("/**   jsdoc line 123 */", getGoogleJSStyleWithColumns(20)));
  // Break after the content of the last line.
  EXPECT_EQ("/**\n"
            " * line 1\n"
            " * line 2\n"
            " */",
            format("/**\n"
                   " * line 1\n"
                   " * line 2 */",
                   getGoogleJSStyleWithColumns(20)));
  // Break both the content and after the content of the last line.
  EXPECT_EQ("/**\n"
            " * line 1\n"
            " * line long long\n"
            " * long\n"
            " */",
            format("/**\n"
                   " * line 1\n"
                   " * line long long long */",
                   getGoogleJSStyleWithColumns(20)));

  // The comment block gets indented.
  EXPECT_EQ("function f() {\n"
            "  /**\n"
            "   * comment about\n"
            "   * x\n"
            "   */\n"
            "  var x = 1;\n"
            "}",
            format("function f() {\n"
                   "/** comment about x */\n"
                   "var x = 1;\n"
                   "}",
                   getGoogleJSStyleWithColumns(20)));

  // Don't break the first line of a single line short jsdoc comment pragma.
  EXPECT_EQ("/** @returns j */",
            format("/** @returns j */",
                   getGoogleJSStyleWithColumns(20)));

  // Break a single line long jsdoc comment pragma.
  EXPECT_EQ("/**\n"
            " * @returns {string} jsdoc line 12\n"
            " */",
            format("/** @returns {string} jsdoc line 12 */",
                   getGoogleJSStyleWithColumns(20)));
  EXPECT_EQ("/**\n"
            " * @returns {string}\n"
            " *     jsdoc line 12\n"
            " */",
            format("/** @returns {string} jsdoc line 12 */",
                   getGoogleJSStyleWithColumns(25)));

  EXPECT_EQ("/**\n"
            " * @returns {string} jsdoc line 12\n"
            " */",
            format("/** @returns {string} jsdoc line 12  */",
                   getGoogleJSStyleWithColumns(20)));

  // FIXME: this overcounts the */ as a continuation of the 12 when breaking.
  // Related to the FIXME in BreakableBlockComment::getRangeLength.
  EXPECT_EQ("/**\n"
            " * @returns {string}\n"
            " *     jsdoc line line\n"
            " *     12\n"
            " */",
            format("/** @returns {string} jsdoc line line 12*/",
                   getGoogleJSStyleWithColumns(25)));

  // Fix a multiline jsdoc comment ending in a comment pragma.
  EXPECT_EQ("/**\n"
            " * line 1\n"
            " * line 2\n"
            " * @returns {string}\n"
            " *     jsdoc line 12\n"
            " */",
            format("/** line 1\n"
                   " * line 2\n"
                   " * @returns {string} jsdoc line 12 */",
                   getGoogleJSStyleWithColumns(20)));

  EXPECT_EQ("/**\n"
            " * line 1\n"
            " * line 2\n"
            " *\n"
            " * @returns j\n"
            " */",
            format("/** line 1\n"
                   " * line 2\n"
                   " *\n"
                   " * @returns j */",
                   getGoogleJSStyleWithColumns(20)));
}

TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
  verifyFormat("a == = b;");
  verifyFormat("a != = b;");

  verifyFormat("a === b;");
  verifyFormat("aaaaaaa ===\n    b;", getGoogleJSStyleWithColumns(10));
  verifyFormat("a !== b;");
  verifyFormat("aaaaaaa !==\n    b;", getGoogleJSStyleWithColumns(10));
  verifyFormat("if (a + b + c +\n"
               "        d !==\n"
               "    e + f + g)\n"
               "  q();",
               getGoogleJSStyleWithColumns(20));

  verifyFormat("a >> >= b;");

  verifyFormat("a >>> b;");
  verifyFormat("aaaaaaa >>>\n    b;", getGoogleJSStyleWithColumns(10));
  verifyFormat("a >>>= b;");
  verifyFormat("aaaaaaa >>>=\n    b;", getGoogleJSStyleWithColumns(10));
  verifyFormat("if (a + b + c +\n"
               "        d >>>\n"
               "    e + f + g)\n"
               "  q();",
               getGoogleJSStyleWithColumns(20));
  verifyFormat("var x = aaaaaaaaaa ?\n"
               "    bbbbbb :\n"
               "    ccc;",
               getGoogleJSStyleWithColumns(20));

  verifyFormat("var b = a.map((x) => x + 1);");
  verifyFormat("return ('aaa') in bbbb;");
  verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n"
               "    aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  FormatStyle Style = getGoogleJSStyleWithColumns(80);
  Style.AlignOperands = true;
  verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n"
               "        aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
               Style);
  Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa()\n"
               "            in aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
               Style);

  // ES6 spread operator.
  verifyFormat("someFunction(...a);");
  verifyFormat("var x = [1, ...a, 2];");
}

TEST_F(FormatTestJS, UnderstandsAmpAmp) {
  verifyFormat("e && e.SomeFunction();");
}

TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
  verifyFormat("not.and.or.not_eq = 1;");
}

TEST_F(FormatTestJS, ReservedWords) {
  // JavaScript reserved words (aka keywords) are only illegal when used as
  // Identifiers, but are legal as IdentifierNames.
  verifyFormat("x.class.struct = 1;");
  verifyFormat("x.case = 1;");
  verifyFormat("x.interface = 1;");
  verifyFormat("x.for = 1;");
  verifyFormat("x.of();");
  verifyFormat("of(null);");
  verifyFormat("return of(null);");
  verifyFormat("import {of} from 'x';");
  verifyFormat("x.in();");
  verifyFormat("x.let();");
  verifyFormat("x.var();");
  verifyFormat("x.for();");
  verifyFormat("x.as();");
  verifyFormat("x.instanceof();");
  verifyFormat("x.switch();");
  verifyFormat("x.case();");
  verifyFormat("x.delete();");
  verifyFormat("x.throw();");
  verifyFormat("x.throws();");
  verifyFormat("x.if();");
  verifyFormat("x = {\n"
               "  a: 12,\n"
               "  interface: 1,\n"
               "  switch: 1,\n"
               "};");
  verifyFormat("var struct = 2;");
  verifyFormat("var union = 2;");
  verifyFormat("var interface = 2;");
  verifyFormat("interface = 2;");
  verifyFormat("x = interface instanceof y;");
  verifyFormat("interface Test {\n"
               "  x: string;\n"
               "  switch: string;\n"
               "  case: string;\n"
               "  default: string;\n"
               "}\n");
  verifyFormat("const Axis = {\n"
               "  for: 'for',\n"
               "  x: 'x'\n"
               "};",
               "const Axis = {for: 'for', x:   'x'};");
}

TEST_F(FormatTestJS, ReservedWordsMethods) {
  verifyFormat(
      "class X {\n"
      "  delete() {\n"
      "    x();\n"
      "  }\n"
      "  interface() {\n"
      "    x();\n"
      "  }\n"
      "  let() {\n"
      "    x();\n"
      "  }\n"
      "}\n");
}

TEST_F(FormatTestJS, ReservedWordsParenthesized) {
  // All of these are statements using the keyword, not function calls.
  verifyFormat("throw (x + y);\n"
               "await (await x).y;\n"
               "typeof (x) === 'string';\n"
               "void (0);\n"
               "delete (x.y);\n"
               "return (x);\n");
}

TEST_F(FormatTestJS, CppKeywords) {
  // Make sure we don't mess stuff up because of C++ keywords.
  verifyFormat("return operator && (aa);");
  // .. or QT ones.
  verifyFormat("slots: Slot[];");
}

TEST_F(FormatTestJS, ES6DestructuringAssignment) {
  verifyFormat("var [a, b, c] = [1, 2, 3];");
  verifyFormat("const [a, b, c] = [1, 2, 3];");
  verifyFormat("let [a, b, c] = [1, 2, 3];");
  verifyFormat("var {a, b} = {a: 1, b: 2};");
  verifyFormat("let {a, b} = {a: 1, b: 2};");
}

TEST_F(FormatTestJS, ContainerLiterals) {
  verifyFormat("var x = {\n"
               "  y: function(a) {\n"
               "    return a;\n"
               "  }\n"
               "};");
  verifyFormat("return {\n"
               "  link: function() {\n"
               "    f();  //\n"
               "  }\n"
               "};");
  verifyFormat("return {\n"
               "  a: a,\n"
               "  link: function() {\n"
               "    f();  //\n"
               "  }\n"
               "};");
  verifyFormat("return {\n"
               "  a: a,\n"
               "  link: function() {\n"
               "    f();  //\n"
               "  },\n"
               "  link: function() {\n"
               "    f();  //\n"
               "  }\n"
               "};");
  verifyFormat("var stuff = {\n"
               "  // comment for update\n"
               "  update: false,\n"
               "  // comment for modules\n"
               "  modules: false,\n"
               "  // comment for tasks\n"
               "  tasks: false\n"
               "};");
  verifyFormat("return {\n"
               "  'finish':\n"
               "      //\n"
               "      a\n"
               "};");
  verifyFormat("var obj = {\n"
               "  fooooooooo: function(x) {\n"
               "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
               "  }\n"
               "};");
  // Simple object literal, as opposed to enum style below.
  verifyFormat("var obj = {a: 123};");
  // Enum style top level assignment.
  verifyFormat("X = {\n  a: 123\n};");
  verifyFormat("X.Y = {\n  a: 123\n};");
  // But only on the top level, otherwise its a plain object literal assignment.
  verifyFormat("function x() {\n"
               "  y = {z: 1};\n"
               "}");
  verifyFormat("x = foo && {a: 123};");

  // Arrow functions in object literals.
  verifyFormat("var x = {\n"
               "  y: (a) => {\n"
               "    return a;\n"
               "  }\n"
               "};");
  verifyFormat("var x = {y: (a) => a};");

  // Methods in object literals.
  verifyFormat("var x = {\n"
               "  y(a: string): number {\n"
               "    return a;\n"
               "  }\n"
               "};");
  verifyFormat("var x = {\n"
               "  y(a: string) {\n"
               "    return a;\n"
               "  }\n"
               "};");

  // Computed keys.
  verifyFormat("var x = {[a]: 1, b: 2, [c]: 3};");
  verifyFormat("var x = {\n"
               "  [a]: 1,\n"
               "  b: 2,\n"
               "  [c]: 3,\n"
               "};");

  // Object literals can leave out labels.
  verifyFormat("f({a}, () => {\n"
               "  g();  //\n"
               "});");

  // Keys can be quoted.
  verifyFormat("var x = {\n"
               "  a: a,\n"
               "  b: b,\n"
               "  'c': c,\n"
               "};");

  // Dict literals can skip the label names.
  verifyFormat("var x = {\n"
               "  aaa,\n"
               "  aaa,\n"
               "  aaa,\n"
               "};");
  verifyFormat("return {\n"
               "  a,\n"
               "  b: 'b',\n"
               "  c,\n"
               "};");
}

TEST_F(FormatTestJS, MethodsInObjectLiterals) {
  verifyFormat("var o = {\n"
               "  value: 'test',\n"
               "  get value() {  // getter\n"
               "    return this.value;\n"
               "  }\n"
               "};");
  verifyFormat("var o = {\n"
               "  value: 'test',\n"
               "  set value(val) {  // setter\n"
               "    this.value = val;\n"
               "  }\n"
               "};");
  verifyFormat("var o = {\n"
               "  value: 'test',\n"
               "  someMethod(val) {  // method\n"
               "    doSomething(this.value + val);\n"
               "  }\n"
               "};");
  verifyFormat("var o = {\n"
               "  someMethod(val) {  // method\n"
               "    doSomething(this.value + val);\n"
               "  },\n"
               "  someOtherMethod(val) {  // method\n"
               "    doSomething(this.value + val);\n"
               "  }\n"
               "};");
}

TEST_F(FormatTestJS, GettersSettersVisibilityKeywords) {
  // Don't break after "protected"
  verifyFormat("class X {\n"
               "  protected get getter():\n"
               "      number {\n"
               "    return 1;\n"
               "  }\n"
               "}",
               getGoogleJSStyleWithColumns(12));
  // Don't break after "get"
  verifyFormat("class X {\n"
               "  protected get someReallyLongGetterName():\n"
               "      number {\n"
               "    return 1;\n"
               "  }\n"
               "}",
               getGoogleJSStyleWithColumns(40));
}

TEST_F(FormatTestJS, SpacesInContainerLiterals) {
  verifyFormat("var arr = [1, 2, 3];");
  verifyFormat("f({a: 1, b: 2, c: 3});");

  verifyFormat("var object_literal_with_long_name = {\n"
               "  a: 'aaaaaaaaaaaaaaaaaa',\n"
               "  b: 'bbbbbbbbbbbbbbbbbb'\n"
               "};");

  verifyFormat("f({a: 1, b: 2, c: 3});",
               getChromiumStyle(FormatStyle::LK_JavaScript));
  verifyFormat("f({'a': [{}]});");
}

TEST_F(FormatTestJS, SingleQuotedStrings) {
  verifyFormat("this.function('', true);");
}

TEST_F(FormatTestJS, GoogScopes) {
  verifyFormat("goog.scope(function() {\n"
               "var x = a.b;\n"
               "var y = c.d;\n"
               "});  // goog.scope");
  verifyFormat("goog.scope(function() {\n"
               "// test\n"
               "var x = 0;\n"
               "// test\n"
               "});");
}

TEST_F(FormatTestJS, IIFEs) {
  // Internal calling parens; no semi.
  verifyFormat("(function() {\n"
               "var a = 1;\n"
               "}())");
  // External calling parens; no semi.
  verifyFormat("(function() {\n"
               "var b = 2;\n"
               "})()");
  // Internal calling parens; with semi.
  verifyFormat("(function() {\n"
               "var c = 3;\n"
               "}());");
  // External calling parens; with semi.
  verifyFormat("(function() {\n"
               "var d = 4;\n"
               "})();");
}

TEST_F(FormatTestJS, GoogModules) {
  verifyFormat("goog.module('this.is.really.absurdly.long');",
               getGoogleJSStyleWithColumns(40));
  verifyFormat("goog.require('this.is.really.absurdly.long');",
               getGoogleJSStyleWithColumns(40));
  verifyFormat("goog.provide('this.is.really.absurdly.long');",
               getGoogleJSStyleWithColumns(40));
  verifyFormat("var long = goog.require('this.is.really.absurdly.long');",
               getGoogleJSStyleWithColumns(40));
  verifyFormat("const X = goog.requireType('this.is.really.absurdly.long');",
               getGoogleJSStyleWithColumns(40));
  verifyFormat("goog.forwardDeclare('this.is.really.absurdly.long');",
               getGoogleJSStyleWithColumns(40));

  // These should be wrapped normally.
  verifyFormat(
      "var MyLongClassName =\n"
      "    goog.module.get('my.long.module.name.followedBy.MyLongClassName');");
  verifyFormat("function a() {\n"
               "  goog.setTestOnly();\n"
               "}\n",
               "function a() {\n"
               "goog.setTestOnly();\n"
               "}\n");
}

TEST_F(FormatTestJS, FormatsNamespaces) {
  verifyFormat("namespace Foo {\n"
               "  export let x = 1;\n"
               "}\n");
  verifyFormat("declare namespace Foo {\n"
               "  export let x: number;\n"
               "}\n");
}

TEST_F(FormatTestJS, NamespacesMayNotWrap) {
  verifyFormat("declare namespace foobarbaz {\n"
               "}\n", getGoogleJSStyleWithColumns(18));
  verifyFormat("declare module foobarbaz {\n"
               "}\n", getGoogleJSStyleWithColumns(15));
  verifyFormat("namespace foobarbaz {\n"
               "}\n", getGoogleJSStyleWithColumns(10));
  verifyFormat("module foobarbaz {\n"
               "}\n", getGoogleJSStyleWithColumns(7));
}

TEST_F(FormatTestJS, AmbientDeclarations) {
  FormatStyle NineCols = getGoogleJSStyleWithColumns(9);
  verifyFormat(
      "declare class\n"
      "    X {}",
      NineCols);
  verifyFormat(
      "declare function\n"
      "x();",  // TODO(martinprobst): should ideally be indented.
      NineCols);
  verifyFormat("declare function foo();\n"
               "let x = 1;\n");
  verifyFormat("declare function foo(): string;\n"
               "let x = 1;\n");
  verifyFormat("declare function foo(): {x: number};\n"
               "let x = 1;\n");
  verifyFormat("declare class X {}\n"
               "let x = 1;\n");
  verifyFormat("declare interface Y {}\n"
               "let x = 1;\n");
  verifyFormat(
      "declare enum X {\n"
      "}",
      NineCols);
  verifyFormat(
      "declare let\n"
      "    x: number;",
      NineCols);
}

TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
  verifyFormat("function outer1(a, b) {\n"
               "  function inner1(a, b) {\n"
               "    return a;\n"
               "  }\n"
               "  inner1(a, b);\n"
               "}\n"
               "function outer2(a, b) {\n"
               "  function inner2(a, b) {\n"
               "    return a;\n"
               "  }\n"
               "  inner2(a, b);\n"
               "}");
  verifyFormat("function f() {}");
  verifyFormat("function aFunction() {}\n"
               "(function f() {\n"
               "  var x = 1;\n"
               "}());\n");
  verifyFormat("function aFunction() {}\n"
               "{\n"
               "  let x = 1;\n"
               "  console.log(x);\n"
               "}\n");
}

TEST_F(FormatTestJS, GeneratorFunctions) {
  verifyFormat("function* f() {\n"
               "  let x = 1;\n"
               "  yield x;\n"
               "  yield* something();\n"
               "  yield [1, 2];\n"
               "  yield {a: 1};\n"
               "}");
  verifyFormat("function*\n"
               "    f() {\n"
               "}",
               getGoogleJSStyleWithColumns(8));
  verifyFormat("export function* f() {\n"
               "  yield 1;\n"
               "}\n");
  verifyFormat("class X {\n"
               "  * generatorMethod() {\n"
               "    yield x;\n"
               "  }\n"
               "}");
  verifyFormat("var x = {\n"
               "  a: function*() {\n"
               "    //\n"
               "  }\n"
               "}\n");
}

TEST_F(FormatTestJS, AsyncFunctions) {
  verifyFormat("async function f() {\n"
               "  let x = 1;\n"
               "  return fetch(x);\n"
               "}");
  verifyFormat("async function f() {\n"
               "  return 1;\n"
               "}\n"
               "\n"
               "function a() {\n"
               "  return 1;\n"
               "}\n",
               "  async   function f() {\n"
               "   return 1;\n"
               "}\n"
               "\n"
               "   function a() {\n"
               "  return   1;\n"
               "}  \n");
  verifyFormat("async function* f() {\n"
               "  yield fetch(x);\n"
               "}");
  verifyFormat("export async function f() {\n"
               "  return fetch(x);\n"
               "}");
  verifyFormat("let x = async () => f();");
  verifyFormat("let x = async function() {\n"
               "  f();\n"
               "};");
  verifyFormat("let x = async();");
  verifyFormat("class X {\n"
               "  async asyncMethod() {\n"
               "    return fetch(1);\n"
               "  }\n"
               "}");
  verifyFormat("function initialize() {\n"
               "  // Comment.\n"
               "  return async.then();\n"
               "}\n");
  verifyFormat("for await (const x of y) {\n"
               "  console.log(x);\n"
               "}\n");
  verifyFormat("function asyncLoop() {\n"
               "  for await (const x of y) {\n"
               "    console.log(x);\n"
               "  }\n"
               "}\n");
}

TEST_F(FormatTestJS, FunctionParametersTrailingComma) {
  verifyFormat("function trailingComma(\n"
               "    p1,\n"
               "    p2,\n"
               "    p3,\n"
               ") {\n"
               "  a;  //\n"
               "}\n",
               "function trailingComma(p1, p2, p3,) {\n"
               "  a;  //\n"
               "}\n");
  verifyFormat("trailingComma(\n"
               "    p1,\n"
               "    p2,\n"
               "    p3,\n"
               ");\n",
               "trailingComma(p1, p2, p3,);\n");
  verifyFormat("trailingComma(\n"
               "    p1  // hello\n"
               ");\n",
               "trailingComma(p1 // hello\n"
               ");\n");
}

TEST_F(FormatTestJS, ArrayLiterals) {
  verifyFormat("var aaaaa: List<SomeThing> =\n"
               "    [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];");
  verifyFormat("return [\n"
               "  aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
               "  ccccccccccccccccccccccccccc\n"
               "];");
  verifyFormat("return [\n"
               "  aaaa().bbbbbbbb('A'),\n"
               "  aaaa().bbbbbbbb('B'),\n"
               "  aaaa().bbbbbbbb('C'),\n"
               "];");
  verifyFormat("var someVariable = SomeFunction([\n"
               "  aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
               "  ccccccccccccccccccccccccccc\n"
               "]);");
  verifyFormat("var someVariable = SomeFunction([\n"
               "  [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n"
               "]);",
               getGoogleJSStyleWithColumns(51));
  verifyFormat("var someVariable = SomeFunction(aaaa, [\n"
               "  aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
               "  ccccccccccccccccccccccccccc\n"
               "]);");
  verifyFormat("var someVariable = SomeFunction(\n"
               "    aaaa,\n"
               "    [\n"
               "      aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
               "      cccccccccccccccccccccccccc\n"
               "    ],\n"
               "    aaaa);");
  verifyFormat("var aaaa = aaaaa ||  // wrap\n"
               "    [];");

  verifyFormat("someFunction([], {a: a});");

  verifyFormat("var string = [\n"
               "  'aaaaaa',\n"
               "  'bbbbbb',\n"
               "].join('+');");
}

TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) {
  verifyFormat("var array = [\n"
               "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
               "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
               "];");
  verifyFormat("var array = someFunction([\n"
               "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
               "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
               "]);");
}

TEST_F(FormatTestJS, FunctionLiterals) {
  FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
  Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  verifyFormat("doFoo(function() {});");
  verifyFormat("doFoo(function() { return 1; });", Style);
  verifyFormat("var func = function() {\n"
               "  return 1;\n"
               "};");
  verifyFormat("var func =  //\n"
               "    function() {\n"
               "  return 1;\n"
               "};");
  verifyFormat("return {\n"
               "  body: {\n"
               "    setAttribute: function(key, val) { this[key] = val; },\n"
               "    getAttribute: function(key) { return this[key]; },\n"
               "    style: {direction: ''}\n"
               "  }\n"
               "};",
               Style);
  verifyFormat("abc = xyz ? function() {\n"
               "  return 1;\n"
               "} : function() {\n"
               "  return -1;\n"
               "};");

  verifyFormat("var closure = goog.bind(\n"
               "    function() {  // comment\n"
               "      foo();\n"
               "      bar();\n"
               "    },\n"
               "    this, arg1IsReallyLongAndNeedsLineBreaks,\n"
               "    arg3IsReallyLongAndNeedsLineBreaks);");
  verifyFormat("var closure = goog.bind(function() {  // comment\n"
               "  foo();\n"
               "  bar();\n"
               "}, this);");
  verifyFormat("return {\n"
               "  a: 'E',\n"
               "  b: function() {\n"
               "    return function() {\n"
               "      f();  //\n"
               "    };\n"
               "  }\n"
               "};");
  verifyFormat("{\n"
               "  var someVariable = function(x) {\n"
               "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
               "  };\n"
               "}");
  verifyFormat("someLooooooooongFunction(\n"
               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
               "    function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
               "      // code\n"
               "    });");

  verifyFormat("return {\n"
               "  a: function SomeFunction() {\n"
               "    // ...\n"
               "    return 1;\n"
               "  }\n"
               "};");
  verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
               "    .then(goog.bind(function(aaaaaaaaaaa) {\n"
               "      someFunction();\n"
               "      someFunction();\n"
               "    }, this), aaaaaaaaaaaaaaaaa);");

  verifyFormat("someFunction(goog.bind(function() {\n"
               "  doSomething();\n"
               "  doSomething();\n"
               "}, this), goog.bind(function() {\n"
               "  doSomething();\n"
               "  doSomething();\n"
               "}, this));");

  verifyFormat("SomeFunction(function() {\n"
               "  foo();\n"
               "  bar();\n"
               "}.bind(this));");

  verifyFormat("SomeFunction((function() {\n"
               "               foo();\n"
               "               bar();\n"
               "             }).bind(this));");

  // FIXME: This is bad, we should be wrapping before "function() {".
  verifyFormat("someFunction(function() {\n"
               "  doSomething();  // break\n"
               "})\n"
               "    .doSomethingElse(\n"
               "        // break\n"
               "    );");

  Style.ColumnLimit = 33;
  verifyFormat("f({a: function() { return 1; }});", Style);
  Style.ColumnLimit = 32;
  verifyFormat("f({\n"
               "  a: function() { return 1; }\n"
               "});",
               Style);

}

TEST_F(FormatTestJS, DontWrapEmptyLiterals) {
  verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n"
               "    .and.returnValue(Observable.of([]));");
  verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n"
               "    .and.returnValue(Observable.of({}));");
  verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n"
               "    .and.returnValue(Observable.of(()));");
}

TEST_F(FormatTestJS, InliningFunctionLiterals) {
  FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
  Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  verifyFormat("var func = function() {\n"
               "  return 1;\n"
               "};",
               Style);
  verifyFormat("var func = doSomething(function() { return 1; });", Style);
  verifyFormat("var outer = function() {\n"
               "  var inner = function() { return 1; }\n"
               "};",
               Style);
  verifyFormat("function outer1(a, b) {\n"
               "  function inner1(a, b) { return a; }\n"
               "}",
               Style);

  Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  verifyFormat("var func = function() { return 1; };", Style);
  verifyFormat("var func = doSomething(function() { return 1; });", Style);
  verifyFormat(
      "var outer = function() { var inner = function() { return 1; } };",
      Style);
  verifyFormat("function outer1(a, b) {\n"
               "  function inner1(a, b) { return a; }\n"
               "}",
               Style);

  Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  verifyFormat("var func = function() {\n"
               "  return 1;\n"
               "};",
               Style);
  verifyFormat("var func = doSomething(function() {\n"
               "  return 1;\n"
               "});",
               Style);
  verifyFormat("var outer = function() {\n"
               "  var inner = function() {\n"
               "    return 1;\n"
               "  }\n"
               "};",
               Style);
  verifyFormat("function outer1(a, b) {\n"
               "  function inner1(a, b) {\n"
               "    return a;\n"
               "  }\n"
               "}",
               Style);

  Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
  verifyFormat("var func = function() {\n"
               "  return 1;\n"
               "};",
               Style);
}

TEST_F(FormatTestJS, MultipleFunctionLiterals) {
  FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
  Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  verifyFormat("promise.then(\n"
               "    function success() {\n"
               "      doFoo();\n"
               "      doBar();\n"
               "    },\n"
               "    function error() {\n"
               "      doFoo();\n"
               "      doBaz();\n"
               "    },\n"
               "    []);\n");
  verifyFormat("promise.then(\n"
               "    function success() {\n"
               "      doFoo();\n"
               "      doBar();\n"
               "    },\n"
               "    [],\n"
               "    function error() {\n"
               "      doFoo();\n"
               "      doBaz();\n"
               "    });\n");
  verifyFormat("promise.then(\n"
               "    [],\n"
               "    function success() {\n"
               "      doFoo();\n"
               "      doBar();\n"
               "    },\n"
               "    function error() {\n"
               "      doFoo();\n"
               "      doBaz();\n"
               "    });\n");

  verifyFormat("getSomeLongPromise()\n"
               "    .then(function(value) { body(); })\n"
               "    .thenCatch(function(error) {\n"
               "      body();\n"
               "      body();\n"
               "    });",
               Style);
  verifyFormat("getSomeLongPromise()\n"
               "    .then(function(value) {\n"
               "      body();\n"
               "      body();\n"
               "    })\n"
               "    .thenCatch(function(error) {\n"
               "      body();\n"
               "      body();\n"
               "    });");

  verifyFormat("getSomeLongPromise()\n"
               "    .then(function(value) { body(); })\n"
               "    .thenCatch(function(error) { body(); });",
               Style);

  verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n"
               "    .aaaaaaa(function() {\n"
               "      //\n"
               "    })\n"
               "    .bbbbbb();");
}

TEST_F(FormatTestJS, ArrowFunctions) {
  verifyFormat("var x = (a) => {\n"
               "  return a;\n"
               "};");
  verifyFormat("var x = (a) => {\n"
               "  function y() {\n"
               "    return 42;\n"
               "  }\n"
               "  return a;\n"
               "};");
  verifyFormat("var x = (a: type): {some: type} => {\n"
               "  return a;\n"
               "};");
  verifyFormat("var x = (a) => a;");
  verifyFormat("return () => [];");
  verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n"
               "  aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
               "      (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
               "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n"
               "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
               "};");
  verifyFormat("var a = a.aaaaaaa(\n"
               "    (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n"
               "        aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
  verifyFormat("var a = a.aaaaaaa(\n"
               "    (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n"
               "        aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n"
               "        aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");

  // FIXME: This is bad, we should be wrapping before "() => {".
  verifyFormat("someFunction(() => {\n"
               "  doSomething();  // break\n"
               "})\n"
               "    .doSomethingElse(\n"
               "        // break\n"
               "    );");
  verifyFormat("const f = (x: string|null): string|null => {\n"
               "  return x;\n"
               "}\n");
}

TEST_F(FormatTestJS, ReturnStatements) {
  verifyFormat("function() {\n"
               "  return [hello, world];\n"
               "}");
}

TEST_F(FormatTestJS, ForLoops) {
  verifyFormat("for (var i in [2, 3]) {\n"
               "}");
  verifyFormat("for (var i of [2, 3]) {\n"
               "}");
  verifyFormat("for (let {a, b} of x) {\n"
               "}");
  verifyFormat("for (let {a, b} of [x]) {\n"
               "}");
  verifyFormat("for (let [a, b] of [x]) {\n"
               "}");
  verifyFormat("for (let {a, b} in x) {\n"
               "}");
}

TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) {
  // The following statements must not wrap, as otherwise the program meaning
  // would change due to automatic semicolon insertion.
  // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1.
  verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10));
  verifyFormat("yield aaaaa;", getGoogleJSStyleWithColumns(10));
  verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
  verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10));
  verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
  verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10));
  verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10));
  verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10));
  verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10));
  verifyFormat("return [\n"
               "  aaa\n"
               "];",
               getGoogleJSStyleWithColumns(12));
  verifyFormat("class X {\n"
               "  readonly ratherLongField =\n"
               "      1;\n"
               "}",
               "class X {\n"
               "  readonly ratherLongField = 1;\n"
               "}",
               getGoogleJSStyleWithColumns(20));
  verifyFormat("const x = (5 + 9)\n"
               "const y = 3\n",
               "const x = (   5 +    9)\n"
               "const y = 3\n");
  // Ideally the foo() bit should be indented relative to the async function().
  verifyFormat("async function\n"
               "foo() {}",
               getGoogleJSStyleWithColumns(10));
  verifyFormat("await theReckoning;", getGoogleJSStyleWithColumns(10));
  verifyFormat("some['a']['b']", getGoogleJSStyleWithColumns(10));
  verifyFormat("x = (a['a']\n"
               "      ['b']);",
               getGoogleJSStyleWithColumns(10));
  verifyFormat("function f() {\n"
               "  return foo.bar(\n"
               "      (param): param is {\n"
               "        a: SomeType\n"
               "      }&ABC => 1)\n"
               "}",
               getGoogleJSStyleWithColumns(25));
}

TEST_F(FormatTestJS, AddsIsTheDictKeyOnNewline) {
  // Do not confuse is, the dict key with is, the type matcher. Put is, the dict
  // key, on a newline.
  verifyFormat("Polymer({\n"
               "  is: '',  //\n"
               "  rest: 1\n"
               "});",
               getGoogleJSStyleWithColumns(20));
}

TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) {
  verifyFormat("a\n"
               "b;",
               " a \n"
               " b ;");
  verifyFormat("a()\n"
               "b;",
               " a ()\n"
               " b ;");
  verifyFormat("a[b]\n"
               "c;",
               "a [b]\n"
               "c ;");
  verifyFormat("1\n"
               "a;",
               "1 \n"
               "a ;");
  verifyFormat("a\n"
               "1;",
               "a \n"
               "1 ;");
  verifyFormat("a\n"
               "'x';",
               "a \n"
               " 'x';");
  verifyFormat("a++\n"
               "b;",
               "a ++\n"
               "b ;");
  verifyFormat("a\n"
               "!b && c;",
               "a \n"
               " ! b && c;");
  verifyFormat("a\n"
               "if (1) f();",
               " a\n"
               " if (1) f();");
  verifyFormat("a\n"
               "class X {}",
               " a\n"
               " class X {}");
  verifyFormat("var a", "var\n"
                        "a");
  verifyFormat("x instanceof String", "x\n"
                                      "instanceof\n"
                                      "String");
  verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n"
                                          "  bar) {}");
  verifyFormat("function f(@Foo(Param) bar) {}", "function f(@Foo(Param)\n"
                                                 "  bar) {}");
  verifyFormat("a = true\n"
               "return 1",
               "a = true\n"
               "  return   1");
  verifyFormat("a = 's'\n"
               "return 1",
               "a = 's'\n"
               "  return   1");
  verifyFormat("a = null\n"
               "return 1",
               "a = null\n"
               "  return   1");
  // Below "class Y {}" should ideally be on its own line.
  verifyFormat(
      "x = {\n"
      "  a: 1\n"
      "} class Y {}",
      "  x  =  {a  : 1}\n"
      "   class  Y {  }");
  verifyFormat(
      "if (x) {\n"
      "}\n"
      "return 1",
      "if (x) {}\n"
      " return   1");
  verifyFormat(
      "if (x) {\n"
      "}\n"
      "class X {}",
      "if (x) {}\n"
      " class X {}");
}

TEST_F(FormatTestJS, ImportExportASI) {
  verifyFormat(
      "import {x} from 'y'\n"
      "export function z() {}",
      "import   {x} from 'y'\n"
      "  export function z() {}");
  // Below "class Y {}" should ideally be on its own line.
  verifyFormat(
      "export {x} class Y {}",
      "  export {x}\n"
      "  class  Y {\n}");
  verifyFormat(
      "if (x) {\n"
      "}\n"
      "export class Y {}",
      "if ( x ) { }\n"
      " export class Y {}");
}

TEST_F(FormatTestJS, ClosureStyleCasts) {
  verifyFormat("var x = /** @type {foo} */ (bar);");
}

TEST_F(FormatTestJS, TryCatch) {
  verifyFormat("try {\n"
               "  f();\n"
               "} catch (e) {\n"
               "  g();\n"
               "} finally {\n"
               "  h();\n"
               "}");

  // But, of course, "catch" is a perfectly fine function name in JavaScript.
  verifyFormat("someObject.catch();");
  verifyFormat("someObject.new();");
}

TEST_F(FormatTestJS, StringLiteralConcatenation) {
  verifyFormat("var literal = 'hello ' +\n"
               "    'world';");
}

TEST_F(FormatTestJS, RegexLiteralClassification) {
  // Regex literals.
  verifyFormat("var regex = /abc/;");
  verifyFormat("f(/abc/);");
  verifyFormat("f(abc, /abc/);");
  verifyFormat("some_map[/abc/];");
  verifyFormat("var x = a ? /abc/ : /abc/;");
  verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
  verifyFormat("var x = !/abc/.test(y);");
  verifyFormat("var x = foo()! / 10;");
  verifyFormat("var x = a && /abc/.test(y);");
  verifyFormat("var x = a || /abc/.test(y);");
  verifyFormat("var x = a + /abc/.search(y);");
  verifyFormat("/abc/.search(y);");
  verifyFormat("var regexs = {/abc/, /abc/};");
  verifyFormat("return /abc/;");

  // Not regex literals.
  verifyFormat("var a = a / 2 + b / 3;");
  verifyFormat("var a = a++ / 2;");
  // Prefix unary can operate on regex literals, not that it makes sense.
  verifyFormat("var a = ++/a/;");

  // This is a known issue, regular expressions are incorrectly detected if
  // directly following a closing parenthesis.
  verifyFormat("if (foo) / bar /.exec(baz);");
}

TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
  verifyFormat("var regex = /=/;");
  verifyFormat("var regex = /a*/;");
  verifyFormat("var regex = /a+/;");
  verifyFormat("var regex = /a?/;");
  verifyFormat("var regex = /.a./;");
  verifyFormat("var regex = /a\\*/;");
  verifyFormat("var regex = /^a$/;");
  verifyFormat("var regex = /\\/a/;");
  verifyFormat("var regex = /(?:x)/;");
  verifyFormat("var regex = /x(?=y)/;");
  verifyFormat("var regex = /x(?!y)/;");
  verifyFormat("var regex = /x|y/;");
  verifyFormat("var regex = /a{2}/;");
  verifyFormat("var regex = /a{1,3}/;");

  verifyFormat("var regex = /[abc]/;");
  verifyFormat("var regex = /[^abc]/;");
  verifyFormat("var regex = /[\\b]/;");
  verifyFormat("var regex = /[/]/;");
  verifyFormat("var regex = /[\\/]/;");
  verifyFormat("var regex = /\\[/;");
  verifyFormat("var regex = /\\\\[/]/;");
  verifyFormat("var regex = /}[\"]/;");
  verifyFormat("var regex = /}[/\"]/;");
  verifyFormat("var regex = /}[\"/]/;");

  verifyFormat("var regex = /\\b/;");
  verifyFormat("var regex = /\\B/;");
  verifyFormat("var regex = /\\d/;");
  verifyFormat("var regex = /\\D/;");
  verifyFormat("var regex = /\\f/;");
  verifyFormat("var regex = /\\n/;");
  verifyFormat("var regex = /\\r/;");
  verifyFormat("var regex = /\\s/;");
  verifyFormat("var regex = /\\S/;");
  verifyFormat("var regex = /\\t/;");
  verifyFormat("var regex = /\\v/;");
  verifyFormat("var regex = /\\w/;");
  verifyFormat("var regex = /\\W/;");
  verifyFormat("var regex = /a(a)\\1/;");
  verifyFormat("var regex = /\\0/;");
  verifyFormat("var regex = /\\\\/g;");
  verifyFormat("var regex = /\\a\\\\/g;");
  verifyFormat("var regex = /\a\\//g;");
  verifyFormat("var regex = /a\\//;\n"
               "var x = 0;");
  verifyFormat("var regex = /'/g;", "var regex = /'/g ;");
  verifyFormat("var regex = /'/g;  //'", "var regex = /'/g ; //'");
  verifyFormat("var regex = /\\/*/;\n"
               "var x = 0;",
               "var regex = /\\/*/;\n"
               "var x=0;");
  verifyFormat("var x = /a\\//;", "var x = /a\\//  \n;");
  verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16));
  verifyFormat("var regex =\n"
               "    /\"/;",
               getGoogleJSStyleWithColumns(15));
  verifyFormat("var regex =  //\n"
               "    /a/;");
  verifyFormat("var regexs = [\n"
               "  /d/,   //\n"
               "  /aa/,  //\n"
               "];");
}

TEST_F(FormatTestJS, RegexLiteralModifiers) {
  verifyFormat("var regex = /abc/g;");
  verifyFormat("var regex = /abc/i;");
  verifyFormat("var regex = /abc/m;");
  verifyFormat("var regex = /abc/y;");
}

TEST_F(FormatTestJS, RegexLiteralLength) {
  verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
               getGoogleJSStyleWithColumns(60));
  verifyFormat("var regex =\n"
               "    /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
               getGoogleJSStyleWithColumns(60));
  verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
               getGoogleJSStyleWithColumns(50));
}

TEST_F(FormatTestJS, RegexLiteralExamples) {
  verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
}

TEST_F(FormatTestJS, IgnoresMpegTS) {
  std::string MpegTS(200, ' ');
  MpegTS.replace(0, strlen("nearlyLooks  +   like +   ts + code;  "),
                 "nearlyLooks  +   like +   ts + code;  ");
  MpegTS[0] = 0x47;
  MpegTS[188] = 0x47;
  verifyFormat(MpegTS, MpegTS);
}

TEST_F(FormatTestJS, TypeAnnotations) {
  verifyFormat("var x: string;");
  verifyFormat("var x: {a: string; b: number;} = {};");
  verifyFormat("function x(): string {\n  return 'x';\n}");
  verifyFormat("function x(): {x: string} {\n  return {x: 'x'};\n}");
  verifyFormat("function x(y: string): string {\n  return 'x';\n}");
  verifyFormat("for (var y: string in x) {\n  x();\n}");
  verifyFormat("for (var y: string of x) {\n  x();\n}");
  verifyFormat("function x(y: {a?: number;} = {}): number {\n"
               "  return 12;\n"
               "}");
  verifyFormat("const x: Array<{a: number; b: string;}> = [];");
  verifyFormat("((a: string, b: number): string => a + b);");
  verifyFormat("var x: (y: number) => string;");
  verifyFormat("var x: P<string, (a: number) => string>;");
  verifyFormat("var x = {\n"
               "  y: function(): z {\n"
               "    return 1;\n"
               "  }\n"
               "};");
  verifyFormat("var x = {\n"
               "  y: function(): {a: number} {\n"
               "    return 1;\n"
               "  }\n"
               "};");
  verifyFormat("function someFunc(args: string[]):\n"
               "    {longReturnValue: string[]} {}",
               getGoogleJSStyleWithColumns(60));
  verifyFormat(
      "var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[])\n"
      "                    .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  verifyFormat("const xIsALongIdent:\n""    YJustBarelyFitsLinex[];",
      getGoogleJSStyleWithColumns(20));
  verifyFormat("const x = {\n"
               "  y: 1\n"
               "} as const;");
}

TEST_F(FormatTestJS, UnionIntersectionTypes) {
  verifyFormat("let x: A|B = A | B;");
  verifyFormat("let x: A&B|C = A & B;");
  verifyFormat("let x: Foo<A|B> = new Foo<A|B>();");
  verifyFormat("function(x: A|B): C&D {}");
  verifyFormat("function(x: A|B = A | B): C&D {}");
  verifyFormat("function x(path: number|string) {}");
  verifyFormat("function x(): string|number {}");
  verifyFormat("type Foo = Bar|Baz;");
  verifyFormat("type Foo = Bar<X>|Baz;");
  verifyFormat("type Foo = (Bar<X>|Baz);");
  verifyFormat("let x: Bar|Baz;");
  verifyFormat("let x: Bar<X>|Baz;");
  verifyFormat("let x: (Foo|Bar)[];");
  verifyFormat("type X = {\n"
               "  a: Foo|Bar;\n"
               "};");
  verifyFormat("export type X = {\n"
               "  a: Foo|Bar;\n"
               "};");
}

TEST_F(FormatTestJS, UnionIntersectionTypesInObjectType) {
  verifyFormat("let x: {x: number|null} = {x: number | null};");
  verifyFormat("let nested: {x: {y: number|null}};");
  verifyFormat("let mixed: {x: [number|null, {w: number}]};");
  verifyFormat("class X {\n"
               "  contructor(x: {\n"
               "    a: a|null,\n"
               "    b: b|null,\n"
               "  }) {}\n"
               "}");
}

TEST_F(FormatTestJS, ClassDeclarations) {
  verifyFormat("class C {\n  x: string = 12;\n}");
  verifyFormat("class C {\n  x(): string => 12;\n}");
  verifyFormat("class C {\n  ['x' + 2]: string = 12;\n}");
  verifyFormat("class C {\n"
               "  foo() {}\n"
               "  [bar]() {}\n"
               "}\n");
  verifyFormat("class C {\n  private x: string = 12;\n}");
  verifyFormat("class C {\n  private static x: string = 12;\n}");
  verifyFormat("class C {\n  static x(): string {\n    return 'asd';\n  }\n}");
  verifyFormat("class C extends P implements I {}");
  verifyFormat("class C extends p.P implements i.I {}");
  verifyFormat(
      "x(class {\n"
      "  a(): A {}\n"
      "});");
  verifyFormat("class Test {\n"
               "  aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n"
               "      aaaaaaaaaaaaaaaaaaaaaa {}\n"
               "}");
  verifyFormat("foo = class Name {\n"
               "  constructor() {}\n"
               "};");
  verifyFormat("foo = class {\n"
               "  constructor() {}\n"
               "};");
  verifyFormat("class C {\n"
               "  x: {y: Z;} = {};\n"
               "  private y: {y: Z;} = {};\n"
               "}");

  // ':' is not a type declaration here.
  verifyFormat("class X {\n"
               "  subs = {\n"
               "    'b': {\n"
               "      'c': 1,\n"
               "    },\n"
               "  };\n"
               "}");
  verifyFormat("@Component({\n"
               "  moduleId: module.id,\n"
               "})\n"
               "class SessionListComponent implements OnDestroy, OnInit {\n"
               "}");
}

TEST_F(FormatTestJS, StrictPropInitWrap) {
  const FormatStyle &Style = getGoogleJSStyleWithColumns(22);
  verifyFormat("class X {\n"
               "  strictPropInitField!:\n"
               "      string;\n"
               "}",
               Style);
}

TEST_F(FormatTestJS, InterfaceDeclarations) {
  verifyFormat("interface I {\n"
               "  x: string;\n"
               "  enum: string[];\n"
               "  enum?: string[];\n"
               "}\n"
               "var y;");
  // Ensure that state is reset after parsing the interface.
  verifyFormat("interface a {}\n"
               "export function b() {}\n"
               "var x;");

  // Arrays of object type literals.
  verifyFormat("interface I {\n"
               "  o: {}[];\n"
               "}");
}

TEST_F(FormatTestJS, ObjectTypesInExtendsImplements) {
  verifyFormat("class C extends {} {}");
  verifyFormat("class C implements {bar: number} {}");
  // Somewhat odd, but probably closest to reasonable formatting?
  verifyFormat("class C implements {\n"
               "  bar: number,\n"
               "  baz: string,\n"
               "} {}");
  verifyFormat("class C<P extends {}> {}");
}

TEST_F(FormatTestJS, EnumDeclarations) {
  verifyFormat("enum Foo {\n"
               "  A = 1,\n"
               "  B\n"
               "}");
  verifyFormat("export /* somecomment*/ enum Foo {\n"
               "  A = 1,\n"
               "  B\n"
               "}");
  verifyFormat("enum Foo {\n"
               "  A = 1,  // comment\n"
               "  B\n"
               "}\n"
               "var x = 1;");
  verifyFormat("const enum Foo {\n"
               "  A = 1,\n"
               "  B\n"
               "}");
  verifyFormat("export const enum Foo {\n"
               "  A = 1,\n"
               "  B\n"
               "}");
}

TEST_F(FormatTestJS, Decorators) {
  verifyFormat("@A\nclass C {\n}");
  verifyFormat("@A({arg: 'value'})\nclass C {\n}");
  verifyFormat("@A\n@B\nclass C {\n}");
  verifyFormat("class C {\n  @A x: string;\n}");
  verifyFormat("class C {\n"
               "  @A\n"
               "  private x(): string {\n"
               "    return 'y';\n"
               "  }\n"
               "}");
  verifyFormat("class C {\n"
               "  private x(@A x: string) {}\n"
               "}");
  verifyFormat("class X {}\n"
               "class Y {}");
  verifyFormat("class X {\n"
               "  @property() private isReply = false;\n"
               "}\n");
}

TEST_F(FormatTestJS, TypeAliases) {
  verifyFormat("type X = number;\n"
               "class C {}");
  verifyFormat("type X<Y> = Z<Y>;");
  verifyFormat("type X = {\n"
               "  y: number\n"
               "};\n"
               "class C {}");
  verifyFormat("export type X = {\n"
               "  a: string,\n"
               "  b?: string,\n"
               "};\n");
}

TEST_F(FormatTestJS, TypeInterfaceLineWrapping) {
  const FormatStyle &Style = getGoogleJSStyleWithColumns(20);
  verifyFormat("type LongTypeIsReallyUnreasonablyLong =\n"
               "    string;\n",
               "type LongTypeIsReallyUnreasonablyLong = string;\n",
               Style);
  verifyFormat(
      "interface AbstractStrategyFactoryProvider {\n"
      "  a: number\n"
      "}\n",
      "interface AbstractStrategyFactoryProvider { a: number }\n",
      Style);
}

TEST_F(FormatTestJS, RemoveEmptyLinesInArrowFunctions) {
  verifyFormat("x = () => {\n"
               "  foo();\n"
               "};\n",
               "x = () => {\n"
               "\n"
               "  foo();\n"
               "\n"
               "};\n");
}

TEST_F(FormatTestJS, Modules) {
  verifyFormat("import SomeThing from 'some/module.js';");
  verifyFormat("import {X, Y} from 'some/module.js';");
  verifyFormat("import a, {X, Y} from 'some/module.js';");
  verifyFormat("import {X, Y,} from 'some/module.js';");
  verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
  // Ensure Automatic Semicolon Insertion does not break on "as\n".
  verifyFormat("import {X as myX} from 'm';", "import {X as\n"
                                              " myX} from 'm';");
  verifyFormat("import * as lib from 'some/module.js';");
  verifyFormat("var x = {import: 1};\nx.import = 2;");

  verifyFormat("export function fn() {\n"
               "  return 'fn';\n"
               "}");
  verifyFormat("export function A() {}\n"
               "export default function B() {}\n"
               "export function C() {}");
  verifyFormat("export default () => {\n"
               "  let x = 1;\n"
               "  return x;\n"
               "}");
  verifyFormat("export const x = 12;");
  verifyFormat("export default class X {}");
  verifyFormat("export {X, Y} from 'some/module.js';");
  verifyFormat("export {X, Y,} from 'some/module.js';");
  verifyFormat("export {SomeVeryLongExport as X, "
               "SomeOtherVeryLongExport as Y} from 'some/module.js';");
  // export without 'from' is wrapped.
  verifyFormat("export let someRatherLongVariableName =\n"
               "    someSurprisinglyLongVariable + someOtherRatherLongVar;");
  // ... but not if from is just an identifier.
  verifyFormat("export {\n"
               "  from as from,\n"
               "  someSurprisinglyLongVariable as\n"
               "      from\n"
               "};",
               getGoogleJSStyleWithColumns(20));
  verifyFormat("export class C {\n"
               "  x: number;\n"
               "  y: string;\n"
               "}");
  verifyFormat("export class X {\n"
               "  y: number;\n"
               "}");
  verifyFormat("export abstract class X {\n"
               "  y: number;\n"
               "}");
  verifyFormat("export default class X {\n"
               "  y: number\n"
               "}");
  verifyFormat("export default function() {\n  return 1;\n}");
  verifyFormat("export var x = 12;");
  verifyFormat("class C {}\n"
               "export function f() {}\n"
               "var v;");
  verifyFormat("export var x: number = 12;");
  verifyFormat("export const y = {\n"
               "  a: 1,\n"
               "  b: 2\n"
               "};");
  verifyFormat("export enum Foo {\n"
               "  BAR,\n"
               "  // adsdasd\n"
               "  BAZ\n"
               "}");
  verifyFormat("export default [\n"
               "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
               "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
               "];");
  verifyFormat("export default [];");
  verifyFormat("export default () => {};");
  verifyFormat("export interface Foo {\n"
               "  foo: number;\n"
               "}\n"
               "export class Bar {\n"
               "  blah(): string {\n"
               "    return this.blah;\n"
               "  };\n"
               "}");
}

TEST_F(FormatTestJS, ImportWrapping) {
  verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,"
               " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying"
               "} from 'some/module.js';");
  FormatStyle Style = getGoogleJSStyleWithColumns(80);
  Style.JavaScriptWrapImports = true;
  verifyFormat("import {\n"
               "  VeryLongImportsAreAnnoying,\n"
               "  VeryLongImportsAreAnnoying,\n"
               "  VeryLongImportsAreAnnoying,\n"
               "} from 'some/module.js';",
               Style);
  verifyFormat("import {\n"
               "  A,\n"
               "  A,\n"
               "} from 'some/module.js';",
               Style);
  verifyFormat("export {\n"
               "  A,\n"
               "  A,\n"
               "} from 'some/module.js';",
               Style);
  Style.ColumnLimit = 40;
  // Using this version of verifyFormat because test::messUp hides the issue.
  verifyFormat("import {\n"
               "  A,\n"
               "} from\n"
               "    'some/path/longer/than/column/limit/module.js';",
               " import  {  \n"
               "    A,  \n"
               "  }    from\n"
               "      'some/path/longer/than/column/limit/module.js'  ; ",
               Style);
}

TEST_F(FormatTestJS, TemplateStrings) {
  // Keeps any whitespace/indentation within the template string.
  verifyFormat("var x = `hello\n"
            "     ${name}\n"
            "  !`;",
            "var x    =    `hello\n"
                   "     ${  name    }\n"
                   "  !`;");

  verifyFormat("var x =\n"
               "    `hello ${world}` >= some();",
               getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
  verifyFormat("var x = `hello ${world}` >= some();",
               getGoogleJSStyleWithColumns(35)); // Barely fits.
  verifyFormat("var x = `hellö ${wörld}` >= söme();",
               getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8.
  verifyFormat("var x = `hello\n"
            "  ${world}` >=\n"
            "    some();",
            "var x =\n"
                   "    `hello\n"
                   "  ${world}` >= some();",
                   getGoogleJSStyleWithColumns(21)); // Barely doesn't fit.
  verifyFormat("var x = `hello\n"
            "  ${world}` >= some();",
            "var x =\n"
                   "    `hello\n"
                   "  ${world}` >= some();",
                   getGoogleJSStyleWithColumns(22)); // Barely fits.

  verifyFormat("var x =\n"
               "    `h`;",
               getGoogleJSStyleWithColumns(11));
  verifyFormat("var x =\n    `multi\n  line`;", "var x = `multi\n  line`;",
               getGoogleJSStyleWithColumns(13));
  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
               "    `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);");
  // Repro for an obscure width-miscounting issue with template strings.
  verifyFormat(
      "someLongVariable =\n"
      "    "
      "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;",
      "someLongVariable = "
      "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;");

  // Make sure template strings get a proper ColumnWidth assigned, even if they
  // are first token in line.
  verifyFormat(
      "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
      "    `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;");

  // Two template strings.
  verifyFormat("var x = `hello` == `hello`;");

  // Comments in template strings.
  verifyFormat("var x = `//a`;\n"
            "var y;",
            "var x =\n `//a`;\n"
                   "var y  ;");
  verifyFormat("var x = `/*a`;\n"
               "var y;",
               "var x =\n `/*a`;\n"
               "var y;");
  // Unterminated string literals in a template string.
  verifyFormat("var x = `'`;  // comment with matching quote '\n"
               "var y;");
  verifyFormat("var x = `\"`;  // comment with matching quote \"\n"
               "var y;");
  verifyFormat("it(`'aaaaaaaaaaaaaaa   `, aaaaaaaaa);",
               "it(`'aaaaaaaaaaaaaaa   `,   aaaaaaaaa) ;",
               getGoogleJSStyleWithColumns(40));
  // Backticks in a comment - not a template string.
  verifyFormat("var x = 1  // `/*a`;\n"
               "    ;",
               "var x =\n 1  // `/*a`;\n"
               "    ;");
  verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */");
  // Comment spans multiple template strings.
  verifyFormat("var x = `/*a`;\n"
               "var y = ` */ `;",
               "var x =\n `/*a`;\n"
               "var y =\n ` */ `;");
  // Escaped backtick.
  verifyFormat("var x = ` \\` a`;\n"
               "var y;",
               "var x = ` \\` a`;\n"
               "var y;");
  // Escaped dollar.
  verifyFormat("var x = ` \\${foo}`;\n");

  // The token stream can contain two string_literals in sequence, but that
  // doesn't mean that they are implicitly concatenated in JavaScript.
  verifyFormat("var f = `aaaa ${a ? 'a' : 'b'}`;");

  // Ensure that scopes are appropriately set around evaluated expressions in
  // template strings.
  verifyFormat("var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa\n"
               "         aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;",
               "var f = `aaaaaaaaaaaaa:${aaaaaaa.  aaaaa} aaaaaaaa\n"
               "         aaaaaaaaaaaaa:${  aaaaaaa. aaaaa} aaaaaaaa`;");
  verifyFormat("var x = someFunction(`${})`)  //\n"
               "            .oooooooooooooooooon();");
  verifyFormat("var x = someFunction(`${aaaa}${\n"
               "    aaaaa(  //\n"
               "        aaaaa)})`);");
}

TEST_F(FormatTestJS, TemplateStringMultiLineExpression) {
  verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${\n"
               "    aaaaa +  //\n"
               "    bbbb}`;",
               "var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa +  //\n"
               "                               bbbb}`;");
  verifyFormat("var f = `\n"
               "  aaaaaaaaaaaaaaaaaa: ${\n"
               "    aaaaa +  //\n"
               "    bbbb}`;",
               "var f  =  `\n"
               "  aaaaaaaaaaaaaaaaaa: ${   aaaaa  +  //\n"
               "                        bbbb }`;");
  verifyFormat("var f = `\n"
               "  aaaaaaaaaaaaaaaaaa: ${\n"
               "    someFunction(\n"
               "        aaaaa +  //\n"
               "        bbbb)}`;",
               "var f  =  `\n"
               "  aaaaaaaaaaaaaaaaaa: ${someFunction (\n"
               "                            aaaaa  +   //\n"
               "                            bbbb)}`;");

  // It might be preferable to wrap before "someFunction".
  verifyFormat("var f = `\n"
               "  aaaaaaaaaaaaaaaaaa: ${someFunction({\n"
               "  aaaa: aaaaa,\n"
               "  bbbb: bbbbb,\n"
               "})}`;",
               "var f  =  `\n"
               "  aaaaaaaaaaaaaaaaaa: ${someFunction ({\n"
               "                          aaaa:  aaaaa,\n"
               "                          bbbb:  bbbbb,\n"
               "                        })}`;");
}

TEST_F(FormatTestJS, TemplateStringASI) {
  verifyFormat("var x = `hello${world}`;", "var x = `hello${\n"
                                           "    world\n"
                                           "}`;");
}

TEST_F(FormatTestJS, NestedTemplateStrings) {
  verifyFormat(
      "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;");
  verifyFormat("var x = `he${({text: 'll'}.text)}o`;");

  // Crashed at some point.
  verifyFormat("}");
}

TEST_F(FormatTestJS, TaggedTemplateStrings) {
  verifyFormat("var x = html`<ul>`;");
  verifyFormat("yield `hello`;");
  verifyFormat("var f = {\n"
               "  param: longTagName`This is a ${\n"
               "                    'really'} long line`\n"
               "};",
               "var f = {param: longTagName`This is a ${'really'} long line`};",
               getGoogleJSStyleWithColumns(40));
}

TEST_F(FormatTestJS, CastSyntax) {
  verifyFormat("var x = <type>foo;");
  verifyFormat("var x = foo as type;");
  verifyFormat("let x = (a + b) as\n"
               "    LongTypeIsLong;",
               getGoogleJSStyleWithColumns(20));
  verifyFormat("foo = <Bar[]>[\n"
               "  1,  //\n"
               "  2\n"
               "];");
  verifyFormat("var x = [{x: 1} as type];");
  verifyFormat("x = x as [a, b];");
  verifyFormat("x = x as {a: string};");
  verifyFormat("x = x as (string);");
  verifyFormat("x = x! as (string);");
  verifyFormat("x = y! in z;");
  verifyFormat("var x = something.someFunction() as\n"
               "    something;",
               getGoogleJSStyleWithColumns(40));
}

TEST_F(FormatTestJS, TypeArguments) {
  verifyFormat("class X<Y> {}");
  verifyFormat("new X<Y>();");
  verifyFormat("foo<Y>(a);");
  verifyFormat("var x: X<Y>[];");
  verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
  verifyFormat("function f(a: List<any> = null) {}");
  verifyFormat("function f(): List<any> {}");
  verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n"
               "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}");
  verifyFormat("function aaaaaaaaaa(\n"
               "    aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n"
               "    aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n"
               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}");
}

TEST_F(FormatTestJS, UserDefinedTypeGuards) {
  verifyFormat(
      "function foo(check: Object):\n"
      "    check is {foo: string, bar: string, baz: string, foobar: string} {\n"
      "  return 'bar' in check;\n"
      "}\n");
}

TEST_F(FormatTestJS, OptionalTypes) {
  verifyFormat("function x(a?: b, c?, d?) {}");
  verifyFormat("class X {\n"
               "  y?: z;\n"
               "  z?;\n"
               "}");
  verifyFormat("interface X {\n"
               "  y?(): z;\n"
               "}");
  verifyFormat("constructor({aa}: {\n"
               "  aa?: string,\n"
               "  aaaaaaaa?: string,\n"
               "  aaaaaaaaaaaaaaa?: boolean,\n"
               "  aaaaaa?: List<string>\n"
               "}) {}");
}

TEST_F(FormatTestJS, IndexSignature) {
  verifyFormat("var x: {[k: string]: v};");
}

TEST_F(FormatTestJS, WrapAfterParen) {
  verifyFormat("xxxxxxxxxxx(\n"
               "    aaa, aaa);",
               getGoogleJSStyleWithColumns(20));
  verifyFormat("xxxxxxxxxxx(\n"
               "    aaa, aaa, aaa,\n"
               "    aaa, aaa, aaa);",
               getGoogleJSStyleWithColumns(20));
  verifyFormat("xxxxxxxxxxx(\n"
               "    aaaaaaaaaaaaaaaaaaaaaaaa,\n"
               "    function(x) {\n"
               "      y();  //\n"
               "    });",
               getGoogleJSStyleWithColumns(40));
  verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
               "       bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
}

TEST_F(FormatTestJS, JSDocAnnotations) {
  verifyFormat("/**\n"
               " * @exports {this.is.a.long.path.to.a.Type}\n"
               " */",
               "/**\n"
               " * @exports {this.is.a.long.path.to.a.Type}\n"
               " */",
               getGoogleJSStyleWithColumns(20));
  verifyFormat("/**\n"
               " * @mods {this.is.a.long.path.to.a.Type}\n"
               " */",
               "/**\n"
               " * @mods {this.is.a.long.path.to.a.Type}\n"
               " */",
               getGoogleJSStyleWithColumns(20));
  verifyFormat("/**\n"
               " * @mods {this.is.a.long.path.to.a.Type}\n"
               " */",
               "/**\n"
               " * @mods {this.is.a.long.path.to.a.Type}\n"
               " */",
               getGoogleJSStyleWithColumns(20));
  verifyFormat("/**\n"
               " * @param {canWrap\n"
               " *     onSpace}\n"
               " */",
               "/**\n"
               " * @param {canWrap onSpace}\n"
               " */",
               getGoogleJSStyleWithColumns(20));
  // make sure clang-format doesn't break before *any* '{'
  verifyFormat("/**\n"
               " * @lala {lala {lalala\n"
               " */\n",
               "/**\n"
               " * @lala {lala {lalala\n"
               " */\n",
               getGoogleJSStyleWithColumns(20));
  verifyFormat("/**\n"
               " * @see http://very/very/long/url/is/long\n"
               " */",
               "/**\n"
               " * @see http://very/very/long/url/is/long\n"
               " */",
               getGoogleJSStyleWithColumns(20));
  verifyFormat(
      "/**\n"
      " * @param This is a\n"
      " *     long comment\n"
      " *     but no type\n"
      " */",
      "/**\n"
      " * @param This is a long comment but no type\n"
      " */",
      getGoogleJSStyleWithColumns(20));
  // Break and reindent @param line and reflow unrelated lines.
  EXPECT_EQ("{\n"
            "  /**\n"
            "   * long long long\n"
            "   * long\n"
            "   * @param {this.is.a.long.path.to.a.Type}\n"
            "   *     a\n"
            "   * long long long\n"
            "   * long long\n"
            "   */\n"
            "  function f(a) {}\n"
            "}",
            format("{\n"
                   "/**\n"
                   " * long long long long\n"
                   " * @param {this.is.a.long.path.to.a.Type} a\n"
                   " * long long long long\n"
                   " * long\n"
                   " */\n"
                   "  function f(a) {}\n"
                   "}",
                   getGoogleJSStyleWithColumns(20)));
}

TEST_F(FormatTestJS, RequoteStringsSingle) {
  verifyFormat("var x = 'foo';", "var x = \"foo\";");
  verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";");
  verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";");
  verifyFormat(
      "var x =\n"
      "    'foo\\'';",
      // Code below is 15 chars wide, doesn't fit into the line with the
      // \ escape added.
      "var x = \"foo'\";", getGoogleJSStyleWithColumns(15));
  // Removes no-longer needed \ escape from ".
  verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";");
  // Code below fits into 15 chars *after* removing the \ escape.
  verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";",
               getGoogleJSStyleWithColumns(15));
  verifyFormat("// clang-format off\n"
               "let x = \"double\";\n"
               "// clang-format on\n"
               "let x = 'single';\n",
               "// clang-format off\n"
               "let x = \"double\";\n"
               "// clang-format on\n"
               "let x = \"single\";\n");
}

TEST_F(FormatTestJS, RequoteAndIndent) {
  verifyFormat("let x = someVeryLongFunctionThatGoesOnAndOn(\n"
               "    'double quoted string that needs wrapping');",
               "let x = someVeryLongFunctionThatGoesOnAndOn("
               "\"double quoted string that needs wrapping\");");

  verifyFormat("let x =\n"
               "    'foo\\'oo';\n"
               "let x =\n"
               "    'foo\\'oo';",
               "let x=\"foo'oo\";\n"
               "let x=\"foo'oo\";",
               getGoogleJSStyleWithColumns(15));
}

TEST_F(FormatTestJS, RequoteStringsDouble) {
  FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
  DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double;
  verifyFormat("var x = \"foo\";", DoubleQuotes);
  verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes);
  verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes);
}

TEST_F(FormatTestJS, RequoteStringsLeave) {
  FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
  LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave;
  verifyFormat("var x = \"foo\";", LeaveQuotes);
  verifyFormat("var x = 'foo';", LeaveQuotes);
}

TEST_F(FormatTestJS, SupportShebangLines) {
  verifyFormat("#!/usr/bin/env node\n"
               "var x = hello();",
               "#!/usr/bin/env node\n"
               "var x   =  hello();");
}

TEST_F(FormatTestJS, NonNullAssertionOperator) {
  verifyFormat("let x = foo!.bar();\n");
  verifyFormat("let x = foo ? bar! : baz;\n");
  verifyFormat("let x = !foo;\n");
  verifyFormat("if (!+a) {\n}");
  verifyFormat("let x = foo[0]!;\n");
  verifyFormat("let x = (foo)!;\n");
  verifyFormat("let x = x(foo!);\n");
  verifyFormat(
      "a.aaaaaa(a.a!).then(\n"
      "    x => x(x));\n",
      getGoogleJSStyleWithColumns(20));
  verifyFormat("let x = foo! - 1;\n");
  verifyFormat("let x = {foo: 1}!;\n");
  verifyFormat(
      "let x = hello.foo()!\n"
      "            .foo()!\n"
      "            .foo()!\n"
      "            .foo()!;\n",
      getGoogleJSStyleWithColumns(20));
  verifyFormat("let x = namespace!;\n");
  verifyFormat("return !!x;\n");
}

TEST_F(FormatTestJS, Conditional) {
  verifyFormat("y = x ? 1 : 2;");
  verifyFormat("x ? 1 : 2;");
  verifyFormat("class Foo {\n"
               "  field = true ? 1 : 2;\n"
               "  method(a = true ? 1 : 2) {}\n"
               "}");
}

TEST_F(FormatTestJS, ImportComments) {
  verifyFormat("import {x} from 'x';  // from some location",
               getGoogleJSStyleWithColumns(25));
  verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10));
  verifyFormat("/// <reference path=\"some/location\" />", getGoogleJSStyleWithColumns(10));
}

TEST_F(FormatTestJS, Exponentiation) {
  verifyFormat("squared = x ** 2;");
  verifyFormat("squared **= 2;");
}

TEST_F(FormatTestJS, NestedLiterals) {
  FormatStyle FourSpaces = getGoogleJSStyleWithColumns(15);
  FourSpaces.IndentWidth = 4;
  verifyFormat("var l = [\n"
               "    [\n"
               "        1,\n"
               "    ],\n"
               "];", FourSpaces);
  verifyFormat("var l = [\n"
               "    {\n"
               "        1: 1,\n"
               "    },\n"
               "];", FourSpaces);
  verifyFormat("someFunction(\n"
               "    p1,\n"
               "    [\n"
               "        1,\n"
               "    ],\n"
               ");", FourSpaces);
  verifyFormat("someFunction(\n"
               "    p1,\n"
               "    {\n"
               "        1: 1,\n"
               "    },\n"
               ");", FourSpaces);
  verifyFormat("var o = {\n"
               "    1: 1,\n"
               "    2: {\n"
               "        3: 3,\n"
               "    },\n"
               "};", FourSpaces);
  verifyFormat("var o = {\n"
               "    1: 1,\n"
               "    2: [\n"
               "        3,\n"
               "    ],\n"
               "};", FourSpaces);
}

TEST_F(FormatTestJS, BackslashesInComments) {
  verifyFormat("// hello \\\n"
               "if (x) foo();\n",
               "// hello \\\n"
               "     if ( x) \n"
               "   foo();\n");
  verifyFormat("/* ignore \\\n"
               " */\n"
               "if (x) foo();\n",
               "/* ignore \\\n"
               " */\n"
               " if (  x) foo();\n");
  verifyFormat("// st \\ art\\\n"
               "// comment"
               "// continue \\\n"
               "formatMe();\n",
               "// st \\ art\\\n"
               "// comment"
               "// continue \\\n"
               "formatMe( );\n");
}

TEST_F(FormatTestJS, AddsLastLinePenaltyIfEndingIsBroken) {
  EXPECT_EQ(
      "a = function() {\n"
      "  b = function() {\n"
      "    this.aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa] = aaaa.aaaaaa ?\n"
      "        aaaa.aaaaaa : /** @type "
      "{aaaa.aaaa.aaaaaaaaa.aaaaaaaaaaaaaaaaaaa} */\n"
      "        (aaaa.aaaa.aaaaaaaaa.aaaaaaaaaaaaa.aaaaaaaaaaaaaaaaa);\n"
      "  };\n"
      "};",
      format("a = function() {\n"
             "  b = function() {\n"
             "    this.aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa] = aaaa.aaaaaa ? "
             "aaaa.aaaaaa : /** @type "
             "{aaaa.aaaa.aaaaaaaaa.aaaaaaaaaaaaaaaaaaa} */\n"
             "        (aaaa.aaaa.aaaaaaaaa.aaaaaaaaaaaaa.aaaaaaaaaaaaaaaaa);\n"
             "  };\n"
             "};"));
}

TEST_F(FormatTestJS, ParameterNamingComment) {
  verifyFormat("callFoo(/*spaceAfterParameterNamingComment=*/ 1);");
}

TEST_F(FormatTestJS, ConditionalTypes) {
  // Formatting below is not necessarily intentional, this just ensures that
  // clang-format does not break the code.
  verifyFormat( // wrap
      "type UnionToIntersection<U> =\n"
      "    (U extends any ? (k: U) => void :\n"
      "                     never) extends((k: infer I) => void) ? I : never;");
}

TEST_F(FormatTestJS, SupportPrivateFieldsAndMethods) {
  verifyFormat("class Example {\n"
               "  pub = 1;\n"
               "  #priv = 2;\n"
               "  static pub2 = 'foo';\n"
               "  static #priv2 = 'bar';\n"
               "  method() {\n"
               "    this.#priv = 5;\n"
               "  }\n"
               "  static staticMethod() {\n"
               "    switch (this.#priv) {\n"
               "      case '1':\n"
               "        #priv = 3;\n"
               "        break;\n"
               "    }\n"
               "  }\n"
               "  #privateMethod() {\n"
               "    this.#privateMethod();  // infinite loop\n"
               "  }\n"
               "  static #staticPrivateMethod() {}\n");
}

} // namespace format
} // end namespace clang