@@ -41,17 +41,15 @@ module.exports = grammar({
41
41
$ . _statement ,
42
42
$ . _declaration ,
43
43
$ . _expression ,
44
- $ . _destructuring_pattern ,
44
+ $ . primary_expression ,
45
+ $ . pattern ,
45
46
] ,
46
47
47
48
inline : $ => [
48
49
$ . _call_signature ,
49
- $ . _primary_expression ,
50
50
$ . _statement ,
51
51
$ . _expressions ,
52
52
$ . _semicolon ,
53
- $ . _formal_parameter ,
54
- $ . _destructuring_pattern ,
55
53
$ . _reserved_identifier ,
56
54
$ . _jsx_attribute ,
57
55
$ . _jsx_element_name ,
@@ -64,16 +62,22 @@ module.exports = grammar({
64
62
] ,
65
63
66
64
conflicts : $ => [
67
- [ $ . _expression , $ . _property_name ] ,
68
- [ $ . _expression , $ . _property_name , $ . arrow_function ] ,
69
- [ $ . _expression , $ . arrow_function ] ,
70
- [ $ . _expression , $ . method_definition ] ,
71
- [ $ . _expression , $ . formal_parameters ] ,
72
- [ $ . _expression , $ . rest_parameter ] ,
65
+ [ $ . primary_expression , $ . _property_name ] ,
66
+ [ $ . primary_expression , $ . _property_name , $ . arrow_function ] ,
67
+ [ $ . primary_expression , $ . arrow_function ] ,
68
+ [ $ . primary_expression , $ . method_definition ] ,
69
+ [ $ . primary_expression , $ . rest_parameter ] ,
70
+ [ $ . primary_expression , $ . pattern ] ,
71
+ [ $ . primary_expression , $ . assignment_expression , ] ,
72
+ [ $ . primary_expression , $ . _for_header ] ,
73
+ [ $ . object , $ . object_pattern ] ,
74
+ [ $ . array , $ . array_pattern ] ,
75
+ [ $ . assignment_expression , $ . pattern ] ,
76
+ [ $ . assignment_expression , $ . rest_parameter ] ,
77
+ [ $ . assignment_expression , $ . object_assignment_pattern ] ,
78
+ [ $ . assignment_expression , $ . assignment_pattern ] ,
73
79
[ $ . labeled_statement , $ . _property_name ] ,
74
- [ $ . assignment_pattern , $ . assignment_expression ] ,
75
80
[ $ . computed_property_name , $ . array ] ,
76
- [ $ . _for_header , $ . _expression ] ,
77
81
] ,
78
82
79
83
word : $ => $ . identifier ,
@@ -401,7 +405,7 @@ module.exports = grammar({
401
405
) ,
402
406
403
407
_expression : $ => choice (
404
- $ . _primary_expression ,
408
+ $ . primary_expression ,
405
409
$ . _jsx_element ,
406
410
$ . jsx_fragment ,
407
411
$ . assignment_expression ,
@@ -415,7 +419,7 @@ module.exports = grammar({
415
419
$ . yield_expression ,
416
420
) ,
417
421
418
- _primary_expression : $ => choice (
422
+ primary_expression : $ => choice (
419
423
$ . this ,
420
424
$ . super ,
421
425
$ . identifier ,
@@ -455,7 +459,6 @@ module.exports = grammar({
455
459
$ . pair ,
456
460
$ . spread_element ,
457
461
$ . method_definition ,
458
- $ . assignment_pattern ,
459
462
alias (
460
463
choice ( $ . identifier , $ . _reserved_identifier ) ,
461
464
$ . shorthand_property_identifier
@@ -464,9 +467,29 @@ module.exports = grammar({
464
467
'}'
465
468
) ) ,
466
469
470
+ object_pattern : $ => prec ( PREC . OBJECT , seq (
471
+ '{' ,
472
+ commaSep ( optional ( choice (
473
+ $ . pair_pattern ,
474
+ $ . rest_parameter ,
475
+ $ . object_assignment_pattern ,
476
+ alias (
477
+ choice ( $ . identifier , $ . _reserved_identifier ) ,
478
+ $ . shorthand_property_identifier_pattern
479
+ )
480
+ ) ) ) ,
481
+ '}'
482
+ ) ) ,
483
+
467
484
assignment_pattern : $ => seq (
485
+ field ( 'left' , $ . pattern ) ,
486
+ '=' ,
487
+ field ( 'right' , $ . _expression )
488
+ ) ,
489
+
490
+ object_assignment_pattern : $ => seq (
468
491
field ( 'left' , choice (
469
- alias ( choice ( $ . _reserved_identifier , $ . identifier ) , $ . shorthand_property_identifier ) ,
492
+ alias ( choice ( $ . _reserved_identifier , $ . identifier ) , $ . shorthand_property_identifier_pattern ) ,
470
493
$ . _destructuring_pattern
471
494
) ) ,
472
495
'=' ,
@@ -482,6 +505,14 @@ module.exports = grammar({
482
505
']'
483
506
) ,
484
507
508
+ array_pattern : $ => seq (
509
+ '[' ,
510
+ commaSep ( optional ( choice (
511
+ $ . pattern
512
+ ) ) ) ,
513
+ ']'
514
+ ) ,
515
+
485
516
_jsx_element : $ => choice ( $ . jsx_element , $ . jsx_self_closing_element ) ,
486
517
487
518
jsx_element : $ => seq (
@@ -655,15 +686,15 @@ module.exports = grammar({
655
686
field ( 'arguments' , choice ( $ . arguments , $ . template_string ) )
656
687
) ) ,
657
688
prec ( PREC . MEMBER , seq (
658
- field ( 'function' , $ . _primary_expression ) ,
689
+ field ( 'function' , $ . primary_expression ) ,
659
690
'?.' ,
660
691
field ( 'arguments' , $ . arguments )
661
692
) )
662
693
) ,
663
694
664
695
new_expression : $ => prec . right ( PREC . NEW , seq (
665
696
'new' ,
666
- field ( 'constructor' , $ . _primary_expression ) ,
697
+ field ( 'constructor' , $ . primary_expression ) ,
667
698
field ( 'arguments' , optional ( prec . dynamic ( 1 , $ . arguments ) ) )
668
699
) ) ,
669
700
@@ -673,13 +704,13 @@ module.exports = grammar({
673
704
) ,
674
705
675
706
member_expression : $ => prec ( PREC . MEMBER , seq (
676
- field ( 'object' , choice ( $ . _expression , $ . _primary_expression ) ) ,
707
+ field ( 'object' , choice ( $ . _expression , $ . primary_expression ) ) ,
677
708
choice ( '.' , '?.' ) ,
678
709
field ( 'property' , alias ( $ . identifier , $ . property_identifier ) )
679
710
) ) ,
680
711
681
712
subscript_expression : $ => prec . right ( PREC . MEMBER , seq (
682
- field ( 'object' , choice ( $ . _expression , $ . _primary_expression ) ) ,
713
+ field ( 'object' , choice ( $ . _expression , $ . primary_expression ) ) ,
683
714
optional ( '?.' ) ,
684
715
'[' , field ( 'index' , $ . _expressions ) , ']'
685
716
) ) ,
@@ -719,8 +750,8 @@ module.exports = grammar({
719
750
) ,
720
751
721
752
_destructuring_pattern : $ => choice (
722
- alias ( $ . object , $ . object_pattern ) ,
723
- alias ( $ . array , $ . array_pattern )
753
+ $ . object_pattern ,
754
+ $ . array_pattern
724
755
) ,
725
756
726
757
spread_element : $ => seq ( '...' , $ . _expression ) ,
@@ -997,13 +1028,13 @@ module.exports = grammar({
997
1028
formal_parameters : $ => seq (
998
1029
'(' ,
999
1030
optional ( seq (
1000
- commaSep1 ( $ . _formal_parameter ) ,
1031
+ commaSep1 ( $ . pattern ) ,
1001
1032
optional ( ',' )
1002
1033
) ) ,
1003
1034
')'
1004
1035
) ,
1005
1036
1006
- _formal_parameter : $ => choice (
1037
+ pattern : $ => choice (
1007
1038
$ . identifier ,
1008
1039
alias ( $ . _reserved_identifier , $ . identifier ) ,
1009
1040
$ . _destructuring_pattern ,
@@ -1035,6 +1066,12 @@ module.exports = grammar({
1035
1066
field ( 'value' , $ . _expression )
1036
1067
) ,
1037
1068
1069
+ pair_pattern : $ => seq (
1070
+ field ( 'key' , $ . _property_name ) ,
1071
+ ':' ,
1072
+ field ( 'value' , $ . pattern )
1073
+ ) ,
1074
+
1038
1075
_property_name : $ => choice (
1039
1076
alias ( choice (
1040
1077
$ . identifier ,
0 commit comments