@willbooster/tree-sitter-javascript 21.1.0 → 22.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/grammar.js CHANGED
@@ -135,6 +135,10 @@ module.exports = grammar({
135
135
  sym('_class_heritage_yield_identifier'),
136
136
  sym('_class_heritage_await_identifier'),
137
137
  sym('_template_escape_sequence'),
138
+ // The `function` and `class` keywords where a declaration may start, so that an expression statement cannot begin
139
+ // with them there, as ECMAScript's ExpressionStatement lookahead restriction requires.
140
+ sym('_declaration_function_keyword'),
141
+ sym('_declaration_class_keyword'),
138
142
  ],
139
143
 
140
144
  extras: ($) => [$.comment, sym('html_comment'), /[\s\p{Zs}\uFEFF\u2028\u2029\u2060\u200B]/u],
@@ -229,7 +233,6 @@ module.exports = grammar({
229
233
  [$._primary_atom, $._simple_target_primary],
230
234
  [$._simple_target_primary, $._non_rest_pattern],
231
235
  [$._primary_atom, $._simple_target_primary, $._non_rest_pattern],
232
- [$._call_callee, $.call_expression],
233
236
  [$._class_heritage_expression, $._class_heritage_call],
234
237
  [$.for_in_clause, $._primary_atom],
235
238
  [$._primary_atom, $.rest_pattern],
@@ -237,13 +240,17 @@ module.exports = grammar({
237
240
  [$._primary_atom, $._non_rest_pattern],
238
241
  [$.arrow_function, $._primary_atom],
239
242
  [$._primary_atom, $.await_expression],
240
- [$.call_expression, $._constructor_import],
241
- [$.primary_expression, $._assignment_call_primary],
243
+ [$._assignment_call_primary, $._classic_call_target],
244
+ [$._assignment_call_primary, $._simple_target_primary, $._non_rest_pattern],
245
+ [$._non_assignment_call_primary, $._non_rest_pattern],
246
+ [$._non_assignment_call_primary, $._object_rest_pattern],
247
+ [$.for_in_clause, $._non_assignment_call_primary],
248
+ [$._non_assignment_call_primary, $.rest_pattern],
242
249
  [$._local_export_specifier, $._module_export_name],
243
- [$.primary_expression, $.rest_pattern],
244
- [$.primary_expression, $._object_rest_pattern],
245
- [$.primary_expression, $._non_rest_pattern],
246
- [$.primary_expression, $.for_in_clause],
250
+ [$._assignment_call_primary, $.rest_pattern],
251
+ [$._assignment_call_primary, $._object_rest_pattern],
252
+ [$._assignment_call_primary, $._non_rest_pattern],
253
+ [$._assignment_call_primary, $.for_in_clause],
247
254
  [$.array, $.array_pattern],
248
255
  [$.object, $.object_pattern],
249
256
  [$.assignment_expression, $._non_rest_pattern],
@@ -579,8 +586,10 @@ module.exports = grammar({
579
586
  seq(
580
587
  'try',
581
588
  field('body', $.statement_block),
582
- optional(field('handler', $.catch_clause)),
583
- optional(field('finalizer', $.finally_clause))
589
+ choice(
590
+ seq(field('handler', $.catch_clause), optional(field('finalizer', $.finally_clause))),
591
+ field('finalizer', $.finally_clause)
592
+ )
584
593
  ),
585
594
 
586
595
  with_statement: ($) => seq('with', field('object', $.parenthesized_expression), field('body', $._single_statement)),
@@ -655,7 +664,14 @@ module.exports = grammar({
655
664
 
656
665
  _unary_operand: ($) => choice($.primary_expression, $.await_expression, $.unary_expression, $.update_expression),
657
666
 
658
- primary_expression: ($) => choice(leftHandSideExpression($), $.arrow_function),
667
+ // Calls in `_assignment_call_primary` chains can be classic call assignment targets; the other chains contain an
668
+ // optional chain or start with an arrow function. Keeping the two disjoint lets the tokens read so far decide every
669
+ // chain, where overlapping chains would split the parse at every call until its arguments end.
670
+ primary_expression: ($) =>
671
+ choice(alias($._assignment_call_primary, $.primary_expression), $._non_assignment_call_primary),
672
+
673
+ _non_assignment_call_primary: ($) =>
674
+ choice($.member_expression, $.subscript_expression, $.call_expression, $.arrow_function),
659
675
 
660
676
  yield_expression: ($) =>
661
677
  prec.right(
@@ -670,17 +686,12 @@ module.exports = grammar({
670
686
  'object',
671
687
  seq(
672
688
  '{',
673
- commaSep(
674
- optional(
675
- choice(
676
- $.pair,
677
- $.spread_element,
678
- $.method_definition,
679
- aliasWithAsyncKeyword(
680
- choice($.identifier, $._reserved_identifier),
681
- sym('shorthand_property_identifier')
682
- )
683
- )
689
+ trailingCommaSep(
690
+ choice(
691
+ $.pair,
692
+ $.spread_element,
693
+ $.method_definition,
694
+ aliasWithAsyncKeyword(choice($.identifier, $._reserved_identifier), sym('shorthand_property_identifier'))
684
695
  )
685
696
  ),
686
697
  '}'
@@ -696,14 +707,10 @@ module.exports = grammar({
696
707
  sym('shorthand_property_identifier_pattern')
697
708
  )
698
709
  );
710
+ const rest = alias($._object_rest_pattern, $.rest_pattern);
699
711
  return prec(
700
712
  'object',
701
- seq(
702
- '{',
703
- repeat(seq(optional(property), ',')),
704
- optional(choice(property, alias($._object_rest_pattern, $.rest_pattern))),
705
- '}'
706
- )
713
+ seq('{', optional(choice(seq(commaSep1(property), optional(seq(',', optional(rest)))), rest)), '}')
707
714
  );
708
715
  },
709
716
 
@@ -845,7 +852,11 @@ module.exports = grammar({
845
852
  'class',
846
853
  field('name', optional($.identifier))
847
854
  ),
848
- seq(repeat(field('decorator', $.decorator)), 'class', field('name', $.identifier))
855
+ seq(
856
+ repeat(field('decorator', $.decorator)),
857
+ choice('class', alias(sym('_declaration_class_keyword'), 'class')),
858
+ field('name', $.identifier)
859
+ )
849
860
  ),
850
861
  optional($.class_heritage),
851
862
  field('body', $.class_body)
@@ -858,12 +869,14 @@ module.exports = grammar({
858
869
  _class_heritage_expression: ($) => alias($._class_heritage_primary, $.primary_expression),
859
870
 
860
871
  _class_heritage_primary: ($) =>
861
- leftHandSideExpression($, {
862
- atom: $._class_heritage_atom,
863
- call: alias($._class_heritage_call, $.call_expression),
864
- member: alias($._class_heritage_member, $.member_expression),
865
- subscript: alias($._class_heritage_subscript, $.subscript_expression),
866
- }),
872
+ choice(
873
+ $._class_heritage_atom,
874
+ alias($._class_heritage_subscript, $.subscript_expression),
875
+ alias($._class_heritage_member, $.member_expression),
876
+ alias($._class_heritage_call, $.call_expression),
877
+ $.new_expression,
878
+ alias($._argumentless_new_expression, $.new_expression)
879
+ ),
867
880
 
868
881
  _class_heritage_atom: ($) => primaryAtom($, $._expression_identifier),
869
882
 
@@ -903,7 +916,11 @@ module.exports = grammar({
903
916
  'function',
904
917
  field('name', optional($.identifier))
905
918
  ),
906
- seq(...(async ? [alias(sym('_async_keyword'), 'async')] : []), 'function', field('name', $.identifier))
919
+ seq(
920
+ ...(async ? [alias(sym('_async_keyword'), 'async')] : []),
921
+ choice('function', alias(sym('_declaration_function_keyword'), 'function')),
922
+ field('name', $.identifier)
923
+ )
907
924
  ),
908
925
  functionScope(async, seq($._call_signature, field('body', $.statement_block)))
909
926
  )
@@ -939,7 +956,7 @@ module.exports = grammar({
939
956
  ),
940
957
  seq(
941
958
  ...(async ? [alias(sym('_async_keyword'), 'async')] : []),
942
- 'function',
959
+ choice('function', alias(sym('_declaration_function_keyword'), 'function')),
943
960
  '*',
944
961
  field('name', $.identifier)
945
962
  )
@@ -989,7 +1006,25 @@ module.exports = grammar({
989
1006
 
990
1007
  optional_chain: () => '?.',
991
1008
 
992
- call_expression: ($) => callExpression($, alias($._call_callee, $.expression), $.primary_expression),
1009
+ call_expression: ($) =>
1010
+ choice(
1011
+ prec('call', seq(field('function', $._non_assignment_call_primary), field('arguments', $.arguments))),
1012
+ prec(
1013
+ 'template_call',
1014
+ seq(
1015
+ field('function', $._non_assignment_call_primary),
1016
+ field('arguments', alias($._tagged_template_string, $.template_string))
1017
+ )
1018
+ ),
1019
+ prec(
1020
+ 'member',
1021
+ seq(
1022
+ field('function', alias($._call_callee, $.expression)),
1023
+ field('optional_chain', $.optional_chain),
1024
+ field('arguments', $.arguments)
1025
+ )
1026
+ )
1027
+ ),
993
1028
 
994
1029
  _primary_atom: ($) => primaryAtom($, $._reserved_identifier),
995
1030
 
@@ -1181,14 +1216,47 @@ module.exports = grammar({
1181
1216
  )
1182
1217
  ),
1183
1218
 
1184
- member_expression: ($) => memberExpression($, alias($._call_callee, $.expression)),
1219
+ member_expression: ($) =>
1220
+ prec(
1221
+ 'member',
1222
+ seq(
1223
+ choice(
1224
+ seq(
1225
+ field('object', choice(alias($._call_callee, $.expression), $.import)),
1226
+ field('optional_chain', $.optional_chain)
1227
+ ),
1228
+ seq(field('object', choice($._non_assignment_call_primary, $.import)), '.')
1229
+ ),
1230
+ field(
1231
+ 'property',
1232
+ choice(
1233
+ $.private_property_identifier,
1234
+ reserved('properties', alias($.identifier, sym('property_identifier')))
1235
+ )
1236
+ )
1237
+ )
1238
+ ),
1185
1239
 
1186
- subscript_expression: ($) => subscriptExpression($, alias($._call_callee, $.expression)),
1240
+ subscript_expression: ($) =>
1241
+ prec.right(
1242
+ 'member',
1243
+ seq(
1244
+ choice(
1245
+ seq(field('object', alias($._call_callee, $.expression)), field('optional_chain', $.optional_chain)),
1246
+ field('object', $._non_assignment_call_primary)
1247
+ ),
1248
+ '[',
1249
+ field('index', $._expressions),
1250
+ ']'
1251
+ )
1252
+ ),
1187
1253
 
1188
1254
  _lhs_expression: ($) =>
1189
1255
  choice(
1190
1256
  $.member_expression,
1191
1257
  $.subscript_expression,
1258
+ alias($._assignment_member, $.member_expression),
1259
+ alias($._assignment_subscript, $.subscript_expression),
1192
1260
  $._identifier,
1193
1261
  aliasWithAsyncKeyword($._reserved_identifier, $.identifier),
1194
1262
  $._destructuring_pattern
@@ -1241,6 +1309,8 @@ module.exports = grammar({
1241
1309
  choice(
1242
1310
  $.member_expression,
1243
1311
  $.subscript_expression,
1312
+ alias($._assignment_member, $.member_expression),
1313
+ alias($._assignment_subscript, $.subscript_expression),
1244
1314
  aliasWithAsyncKeyword($._reserved_identifier, $.identifier),
1245
1315
  $._identifier,
1246
1316
  alias($._parenthesized_simple_target, $.parenthesized_expression)
@@ -1476,7 +1546,7 @@ module.exports = grammar({
1476
1546
  undefined: () => 'undefined',
1477
1547
 
1478
1548
  arguments: ($) =>
1479
- seq(choice('(', sym('_call_arguments_context')), commaSep(optional(choice($.expression, $.spread_element))), ')'),
1549
+ seq(choice('(', sym('_call_arguments_context')), trailingCommaSep(choice($.expression, $.spread_element)), ')'),
1480
1550
 
1481
1551
  decorator: ($) =>
1482
1552
  seq(
@@ -1607,6 +1677,8 @@ module.exports = grammar({
1607
1677
  choice(
1608
1678
  $.member_expression,
1609
1679
  $.subscript_expression,
1680
+ alias($._assignment_member, $.member_expression),
1681
+ alias($._assignment_subscript, $.subscript_expression),
1610
1682
  $._identifier,
1611
1683
  aliasWithAsyncKeyword($._reserved_identifier, $.identifier),
1612
1684
  alias($._parenthesized_simple_target, $.parenthesized_expression)
@@ -1709,21 +1781,6 @@ module.exports = grammar({
1709
1781
  },
1710
1782
  });
1711
1783
 
1712
- /**
1713
- * @param {GrammarSymbols<'_primary_atom' | 'subscript_expression' | 'member_expression' | 'call_expression' | 'new_expression' | '_argumentless_new_expression'>} $
1714
- * @param {{atom?: RuleOrLiteral, subscript?: RuleOrLiteral, member?: RuleOrLiteral, call?: RuleOrLiteral}} [forms]
1715
- */
1716
- function leftHandSideExpression($, forms = {}) {
1717
- return choice(
1718
- forms.atom ?? $._primary_atom,
1719
- forms.subscript ?? $.subscript_expression,
1720
- forms.member ?? $.member_expression,
1721
- forms.call ?? $.call_expression,
1722
- $.new_expression,
1723
- alias($._argumentless_new_expression, $.new_expression)
1724
- );
1725
- }
1726
-
1727
1784
  /**
1728
1785
  * @param {import('@willbooster/web-tree-sitter/dsl').GrammarSymbols<'_escaped_let'>} $
1729
1786
  * @param {boolean} heritage
@@ -1850,6 +1907,15 @@ function commaSep(rule) {
1850
1907
  return optional(commaSep1(rule));
1851
1908
  }
1852
1909
 
1910
+ /**
1911
+ * @param {import('@willbooster/web-tree-sitter/dsl').RuleOrLiteral} rule
1912
+ *
1913
+ * @returns {import('@willbooster/web-tree-sitter/dsl').ChoiceRule}
1914
+ */
1915
+ function trailingCommaSep(rule) {
1916
+ return optional(seq(commaSep1(rule), optional(',')));
1917
+ }
1918
+
1853
1919
  /** @param {(isAsync: boolean) => import('@willbooster/web-tree-sitter/dsl').RuleOrLiteral} makeRule */
1854
1920
  function functionContext(makeRule) {
1855
1921
  return choice(makeRule(false), makeRule(true));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-javascript",
3
- "version": "21.1.0",
3
+ "version": "22.0.1",
4
4
  "description": "JavaScript grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "incremental",
Binary file
Binary file
package/tree-sitter.json CHANGED
@@ -34,7 +34,7 @@
34
34
  }
35
35
  ],
36
36
  "metadata": {
37
- "version": "21.1.0",
37
+ "version": "22.0.1",
38
38
  "license": "MIT",
39
39
  "description": "JavaScript grammar for tree-sitter",
40
40
  "authors": [