@@ -289,7 +289,9 @@ struct
289
289
| Try_statement _
290
290
| Function_declaration _
291
291
| Class_declaration _
292
- | Debugger_statement -> false
292
+ | Debugger_statement
293
+ | Import _
294
+ | Export _ -> false
293
295
294
296
let starts_with ~obj ~funct ~let_identifier ~async_identifier l e =
295
297
let rec traverse l e =
@@ -368,6 +370,13 @@ struct
368
370
Buffer. add_char b quote;
369
371
PP. string f (Buffer. contents b)
370
372
373
+ let pp_string_lit f (Stdlib.Utf8_string. Utf8 s ) =
374
+ let quote = best_string_quote s in
375
+ pp_string f ~quote s
376
+
377
+ let pp_ident_or_string_lit f (Stdlib.Utf8_string. Utf8 s_lit as s ) =
378
+ if is_ident s_lit then PP. string f s_lit else pp_string_lit f s
379
+
371
380
let rec comma_list f f_elt l =
372
381
match l with
373
382
| [] -> ()
@@ -523,9 +532,7 @@ struct
523
532
then (
524
533
PP. string f " )" ;
525
534
PP. end_group f)
526
- | EStr (Utf8 s ) ->
527
- let quote = best_string_quote s in
528
- pp_string f ~quote s
535
+ | EStr x -> pp_string_lit f x
529
536
| ETemplate l -> template f l
530
537
| EBool b -> PP. string f (if b then " true" else " false" )
531
538
| ENum num ->
@@ -833,9 +840,7 @@ struct
833
840
and property_name f n =
834
841
match n with
835
842
| PNI (Utf8 s ) -> PP. string f s
836
- | PNS (Utf8 s ) ->
837
- let quote = best_string_quote s in
838
- pp_string f ~quote s
843
+ | PNS s -> pp_string_lit f s
839
844
| PNN v -> expression Expression f (ENum v)
840
845
| PComputed e ->
841
846
PP. string f " [" ;
@@ -1409,6 +1414,140 @@ struct
1409
1414
PP. string f " finally" ;
1410
1415
block f b);
1411
1416
PP. end_group f
1417
+ | Import ({ kind; from } , _loc ) ->
1418
+ PP. start_group f 0 ;
1419
+ PP. string f " import" ;
1420
+ (match kind with
1421
+ | SideEffect -> ()
1422
+ | Default i ->
1423
+ PP. space f;
1424
+ ident f i
1425
+ | Namespace (def , i ) ->
1426
+ Option. iter def ~f: (fun def ->
1427
+ PP. space f;
1428
+ ident f def;
1429
+ PP. string f " ," );
1430
+ PP. space f;
1431
+ PP. string f " * as " ;
1432
+ ident f i
1433
+ | Named (def , l ) ->
1434
+ Option. iter def ~f: (fun def ->
1435
+ PP. space f;
1436
+ ident f def;
1437
+ PP. string f " ," );
1438
+ PP. space f;
1439
+ PP. string f " {" ;
1440
+ PP. space f;
1441
+ comma_list
1442
+ f
1443
+ (fun f (s , i ) ->
1444
+ if match i with
1445
+ | S { name; _ } when Stdlib.Utf8_string. equal name s -> true
1446
+ | _ -> false
1447
+ then ident f i
1448
+ else (
1449
+ pp_ident_or_string_lit f s;
1450
+ PP. string f " as " ;
1451
+ ident f i))
1452
+ l;
1453
+ PP. space f;
1454
+ PP. string f " }" );
1455
+ (match kind with
1456
+ | SideEffect -> ()
1457
+ | _ ->
1458
+ PP. space f;
1459
+ PP. string f " from" );
1460
+ PP. space f;
1461
+ pp_string_lit f from;
1462
+ PP. string f " ;" ;
1463
+ PP. end_group f
1464
+ | Export (e , _loc ) ->
1465
+ PP. start_group f 0 ;
1466
+ PP. string f " export" ;
1467
+ (match e with
1468
+ | ExportNames l ->
1469
+ PP. space f;
1470
+ PP. string f " {" ;
1471
+ PP. space f;
1472
+ comma_list
1473
+ f
1474
+ (fun f (i , s ) ->
1475
+ if match i with
1476
+ | S { name; _ } when Stdlib.Utf8_string. equal name s -> true
1477
+ | _ -> false
1478
+ then ident f i
1479
+ else (
1480
+ ident f i;
1481
+ PP. string f " as " ;
1482
+ pp_ident_or_string_lit f s))
1483
+ l;
1484
+ PP. space f;
1485
+ PP. string f " };"
1486
+ | ExportFrom { kind; from } ->
1487
+ PP. space f;
1488
+ (match kind with
1489
+ | Export_all None -> PP. string f " *"
1490
+ | Export_all (Some s ) ->
1491
+ PP. string f " * as " ;
1492
+ pp_ident_or_string_lit f s
1493
+ | Export_names l ->
1494
+ PP. string f " {" ;
1495
+ PP. space f;
1496
+ comma_list
1497
+ f
1498
+ (fun f (a , b ) ->
1499
+ if Stdlib.Utf8_string. equal a b
1500
+ then pp_ident_or_string_lit f a
1501
+ else (
1502
+ pp_ident_or_string_lit f a;
1503
+ PP. string f " as " ;
1504
+ pp_ident_or_string_lit f b))
1505
+ l;
1506
+ PP. space f;
1507
+ PP. string f " }" );
1508
+ PP. space f;
1509
+ PP. string f " from" ;
1510
+ PP. space f;
1511
+ pp_string_lit f from;
1512
+ PP. string f " ;"
1513
+ | ExportDefaultExpression ((EFun _ | EClass _ ) as e ) ->
1514
+ PP. space f;
1515
+ PP. string f " default" ;
1516
+ PP. space f;
1517
+ expression Expression f e
1518
+ | ExportDefaultExpression e ->
1519
+ PP. space f;
1520
+ PP. string f " default" ;
1521
+ PP. space f;
1522
+ parenthesized_expression
1523
+ ~last_semi
1524
+ ~obj: true
1525
+ ~funct: true
1526
+ ~let_identifier: true
1527
+ Expression
1528
+ f
1529
+ e
1530
+ | ExportDefaultFun (id , decl ) ->
1531
+ PP. space f;
1532
+ PP. string f " default" ;
1533
+ PP. space f;
1534
+ statement f (Function_declaration (id, decl), loc)
1535
+ | ExportDefaultClass (id , decl ) ->
1536
+ PP. space f;
1537
+ PP. string f " default" ;
1538
+ PP. space f;
1539
+ statement f (Class_declaration (id, decl), loc)
1540
+ | ExportFun (id , decl ) ->
1541
+ PP. space f;
1542
+ statement f (Function_declaration (id, decl), loc)
1543
+ | ExportClass (id , decl ) ->
1544
+ PP. space f;
1545
+ statement f (Class_declaration (id, decl), loc)
1546
+ | ExportVar (k , l ) ->
1547
+ PP. space f;
1548
+ variable_declaration_list k (not can_omit_semi) f l
1549
+ | CoverExportFrom e -> early_error e);
1550
+ PP. end_group f
1412
1551
1413
1552
and statement_list f ?skip_last_semi b =
1414
1553
match b with
0 commit comments