@willbooster/tree-sitter-javascript 16.0.0 → 17.0.0

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
@@ -7,6 +7,8 @@
7
7
 
8
8
  // @ts-check
9
9
 
10
+ /** @import {GrammarSymbols, RuleOrLiteral} from '@willbooster/web-tree-sitter/dsl' */
11
+
10
12
  // The CLI injects the DSL before loading this CommonJS grammar.
11
13
  const { grammar, alias, choice, field, optional, prec, repeat, repeat1, seq, sym, token, reserved } =
12
14
  /** @type {typeof globalThis & import('@willbooster/web-tree-sitter/dsl').DSL} */ (globalThis);
@@ -51,7 +53,6 @@ const RESERVED_WORDS = [
51
53
  ];
52
54
  const NAMED_RESERVED_WORDS = new Set(['this', 'super', 'true', 'false', 'null']);
53
55
 
54
- // Words that are keywords only in some positions and identifiers elsewhere.
55
56
  const MEMBER_MODIFIERS = ['get', 'set', 'static'];
56
57
  const CONTEXTUAL_KEYWORDS = ['async', 'await', 'let', 'of', 'using'];
57
58
  const CLASSIC_ASSIGNMENT_OPERATORS = ['+=', '-=', '*=', '/=', '%=', '^=', '&=', '|=', '>>=', '>>>=', '<<=', '**='];
@@ -128,6 +129,9 @@ module.exports = grammar({
128
129
  sym('_call_arguments_context'),
129
130
  sym('_async_keyword'),
130
131
  sym('_line_break_before_from'),
132
+ sym('_class_heritage_end'),
133
+ sym('_class_heritage_yield_identifier'),
134
+ sym('_class_heritage_await_identifier'),
131
135
  ],
132
136
 
133
137
  extras: ($) => [$.comment, sym('html_comment'), /[\s\p{Zs}\uFEFF\u2028\u2029\u2060\u200B]/u],
@@ -146,6 +150,8 @@ module.exports = grammar({
146
150
  $._binary_operand,
147
151
  $._unary_operand,
148
152
  $.primary_expression,
153
+ $._class_heritage_expression,
154
+ $._class_heritage_primary,
149
155
  $.pattern,
150
156
  $._non_rest_pattern,
151
157
  $._rest_pattern,
@@ -167,6 +173,7 @@ module.exports = grammar({
167
173
  $._semicolon,
168
174
  $._identifier,
169
175
  $._reserved_identifier,
176
+ $._expression_identifier,
170
177
  $._jsx_attribute,
171
178
  $._jsx_element_name,
172
179
  $._jsx_child,
@@ -220,6 +227,7 @@ module.exports = grammar({
220
227
  [$._simple_target_primary, $._non_rest_pattern],
221
228
  [$._primary_atom, $._simple_target_primary, $._non_rest_pattern],
222
229
  [$._call_callee, $.call_expression],
230
+ [$._class_heritage_expression, $._class_heritage_call],
223
231
  [$.for_in_clause, $._primary_atom],
224
232
  [$._primary_atom, $.rest_pattern],
225
233
  [$._primary_atom, $._object_rest_pattern],
@@ -249,10 +257,6 @@ module.exports = grammar({
249
257
 
250
258
  hash_bang_line: () => /#![^\n\r\u2028\u2029]*/,
251
259
 
252
- //
253
- // Export declarations
254
- //
255
-
256
260
  export_statement: ($) =>
257
261
  choice(
258
262
  field('declaration', alias($._decorated_export_declaration, $.declaration)),
@@ -327,10 +331,6 @@ module.exports = grammar({
327
331
  $.using_declaration
328
332
  ),
329
333
 
330
- //
331
- // Import declarations
332
- //
333
-
334
334
  import: () => token('import'),
335
335
 
336
336
  import_statement: ($) =>
@@ -363,10 +363,6 @@ module.exports = grammar({
363
363
 
364
364
  import_attribute: ($) => seq('with', $.object),
365
365
 
366
- //
367
- // Statements
368
- //
369
-
370
366
  statement: ($) =>
371
367
  choice(
372
368
  $.export_statement,
@@ -620,10 +616,6 @@ module.exports = grammar({
620
616
  field('body', $._single_statement)
621
617
  ),
622
618
 
623
- //
624
- // Statement components
625
- //
626
-
627
619
  switch_body: ($) => seq('{', repeat(choice($.switch_case, $.switch_default)), '}'),
628
620
 
629
621
  switch_case: ($) => seq('case', field('value', $._expressions), ':', field('body', repeat($._statement))),
@@ -641,9 +633,6 @@ module.exports = grammar({
641
633
 
642
634
  parenthesized_expression: ($) => seq('(', $._expressions, ')'),
643
635
 
644
- //
645
- // Expressions
646
- //
647
636
  _expressions: ($) => choice($.expression, $.sequence_expression),
648
637
 
649
638
  expression: ($) =>
@@ -664,16 +653,7 @@ module.exports = grammar({
664
653
 
665
654
  _unary_operand: ($) => choice($.primary_expression, $.await_expression, $.unary_expression, $.update_expression),
666
655
 
667
- primary_expression: ($) =>
668
- choice(
669
- $._primary_atom,
670
- $.subscript_expression,
671
- $.member_expression,
672
- $.call_expression,
673
- $.new_expression,
674
- alias($._argumentless_new_expression, $.new_expression),
675
- $.arrow_function
676
- ),
656
+ primary_expression: ($) => choice(leftHandSideExpression($), $.arrow_function),
677
657
 
678
658
  yield_expression: ($) =>
679
659
  prec.right(
@@ -759,9 +739,6 @@ module.exports = grammar({
759
739
  jsx_element: ($) =>
760
740
  seq(field('open_tag', $.jsx_opening_element), repeat($._jsx_child), field('close_tag', $.jsx_closing_element)),
761
741
 
762
- // An entity can be named, numeric (decimal), or numeric (hexadecimal). The
763
- // longest entity name is 29 characters long, and the HTML spec says that
764
- // no more will ever be added.
765
742
  html_character_reference: () => /&(#([xX][0-9a-fA-F]{1,6}|[0-9]{1,5})|[A-Za-z]{1,30});/,
766
743
 
767
744
  jsx_expression: ($) => seq('{', optional(choice($.expression, $.sequence_expression, $.spread_element)), '}'),
@@ -833,13 +810,8 @@ module.exports = grammar({
833
810
  )
834
811
  ),
835
812
 
836
- // Workaround to https://github.com/tree-sitter/tree-sitter/issues/1156
837
- // We give names to the token() constructs containing a regexp
838
- // so as to obtain a node in the CST.
839
- //
840
813
  unescaped_double_jsx_string_fragment: () => token.immediate(prec(1, /([^"&]|&[^#A-Za-z"&])+/)),
841
814
 
842
- // same here
843
815
  unescaped_single_jsx_string_fragment: () => token.immediate(prec(1, /([^'&]|&[^#A-Za-z'&])+/)),
844
816
 
845
817
  // An `&` that does not start a complete html_character_reference is a literal character, as in HTML. The lexer
@@ -878,7 +850,31 @@ module.exports = grammar({
878
850
  )
879
851
  ),
880
852
 
881
- class_heritage: ($) => seq('extends', $.expression),
853
+ class_heritage: ($) =>
854
+ seq('extends', alias($._class_heritage_expression, $.expression), sym('_class_heritage_end')),
855
+
856
+ _class_heritage_expression: ($) => alias($._class_heritage_primary, $.primary_expression),
857
+
858
+ _class_heritage_primary: ($) =>
859
+ leftHandSideExpression($, {
860
+ atom: $._class_heritage_atom,
861
+ call: alias($._class_heritage_call, $.call_expression),
862
+ member: alias($._class_heritage_member, $.member_expression),
863
+ subscript: alias($._class_heritage_subscript, $.subscript_expression),
864
+ }),
865
+
866
+ _class_heritage_atom: ($) => primaryAtom($, $._expression_identifier),
867
+
868
+ _class_heritage_call: ($) =>
869
+ callExpression(
870
+ $,
871
+ alias($._class_heritage_expression, $.expression),
872
+ alias($._class_heritage_primary, $.primary_expression)
873
+ ),
874
+
875
+ _class_heritage_member: ($) => memberExpression($, alias($._class_heritage_expression, $.expression)),
876
+
877
+ _class_heritage_subscript: ($) => subscriptExpression($, alias($._class_heritage_expression, $.expression)),
882
878
 
883
879
  function_expression: ($) =>
884
880
  prec(
@@ -991,46 +987,9 @@ module.exports = grammar({
991
987
 
992
988
  optional_chain: () => '?.',
993
989
 
994
- call_expression: ($) =>
995
- choice(
996
- prec(
997
- 'call',
998
- seq(field('function', choice(alias($._call_callee, $.expression), $.import)), field('arguments', $.arguments))
999
- ),
1000
- prec('template_call', seq(field('function', $.primary_expression), field('arguments', $.template_string))),
1001
- prec(
1002
- 'member',
1003
- seq(
1004
- field('function', $.primary_expression),
1005
- field('optional_chain', $.optional_chain),
1006
- field('arguments', $.arguments)
1007
- )
1008
- )
1009
- ),
990
+ call_expression: ($) => callExpression($, alias($._call_callee, $.expression), $.primary_expression),
1010
991
 
1011
- _primary_atom: ($) =>
1012
- choice(
1013
- $._jsx_element,
1014
- $.parenthesized_expression,
1015
- $._identifier,
1016
- alias(sym('_await_yield_identifier'), $.identifier),
1017
- aliasWithAsyncKeyword($._reserved_identifier, $.identifier),
1018
- $.this,
1019
- $.super,
1020
- $.number,
1021
- $.string,
1022
- $.template_string,
1023
- $.regex,
1024
- $.true,
1025
- $.false,
1026
- $.null,
1027
- $.object,
1028
- $.array,
1029
- $.function_expression,
1030
- $.generator_function,
1031
- $.class,
1032
- $.meta_property
1033
- ),
992
+ _primary_atom: ($) => primaryAtom($, $._reserved_identifier),
1034
993
 
1035
994
  _assignment_call: ($) =>
1036
995
  prec(
@@ -1220,33 +1179,9 @@ module.exports = grammar({
1220
1179
  )
1221
1180
  ),
1222
1181
 
1223
- member_expression: ($) =>
1224
- prec(
1225
- 'member',
1226
- seq(
1227
- field('object', choice(alias($._call_callee, $.expression), $.import)),
1228
- choice('.', field('optional_chain', $.optional_chain)),
1229
- field(
1230
- 'property',
1231
- choice(
1232
- $.private_property_identifier,
1233
- reserved('properties', alias($.identifier, sym('property_identifier')))
1234
- )
1235
- )
1236
- )
1237
- ),
1182
+ member_expression: ($) => memberExpression($, alias($._call_callee, $.expression)),
1238
1183
 
1239
- subscript_expression: ($) =>
1240
- prec.right(
1241
- 'member',
1242
- seq(
1243
- field('object', alias($._call_callee, $.expression)),
1244
- optional(field('optional_chain', $.optional_chain)),
1245
- '[',
1246
- field('index', $._expressions),
1247
- ']'
1248
- )
1249
- ),
1184
+ subscript_expression: ($) => subscriptExpression($, alias($._call_callee, $.expression)),
1250
1185
 
1251
1186
  _lhs_expression: ($) =>
1252
1187
  choice(
@@ -1411,10 +1346,6 @@ module.exports = grammar({
1411
1346
 
1412
1347
  sequence_expression: ($) => prec.right(commaSep1($.expression)),
1413
1348
 
1414
- //
1415
- // Primitives
1416
- //
1417
-
1418
1349
  string: ($) =>
1419
1350
  choice(
1420
1351
  seq(
@@ -1429,13 +1360,8 @@ module.exports = grammar({
1429
1360
  )
1430
1361
  ),
1431
1362
 
1432
- // Workaround to https://github.com/tree-sitter/tree-sitter/issues/1156
1433
- // We give names to the token() constructs containing a regexp
1434
- // so as to obtain a node in the CST.
1435
- //
1436
1363
  unescaped_double_string_fragment: () => token.immediate(prec(1, /[^"\\\r\n]+/)),
1437
1364
 
1438
- // same here
1439
1365
  unescaped_single_string_fragment: () => token.immediate(prec(1, /[^'\\\r\n]+/)),
1440
1366
 
1441
1367
  escape_sequence: () =>
@@ -1483,19 +1409,10 @@ module.exports = grammar({
1483
1409
  -1,
1484
1410
  repeat1(
1485
1411
  choice(
1486
- seq(
1487
- '[',
1488
- repeat(
1489
- choice(
1490
- seq('\\', /[^\n\r\u2028\u2029]/), // escaped character
1491
- /[^\]\\\n\r\u2028\u2029]/ // any character besides ']', '\', or a line terminator
1492
- )
1493
- ),
1494
- ']'
1495
- ), // square-bracket-delimited character class
1496
- seq('\\', /[^\n\r\u2028\u2029]/), // escaped character
1412
+ seq('[', repeat(choice(seq('\\', /[^\n\r\u2028\u2029]/), /[^\]\\\n\r\u2028\u2029]/)), ']'),
1413
+ seq('\\', /[^\n\r\u2028\u2029]/),
1497
1414
  // oxlint-disable-next-line no-useless-escape -- tree-sitter's regex parser rejects an unescaped `[` in a character class.
1498
- /[^/\\\[\n\r\u2028\u2029]/ // any character besides '[', '\', '/', or a line terminator
1415
+ /[^/\\\[\n\r\u2028\u2029]/
1499
1416
  )
1500
1417
  )
1501
1418
  )
@@ -1569,10 +1486,6 @@ module.exports = grammar({
1569
1486
  null: () => 'null',
1570
1487
  undefined: () => 'undefined',
1571
1488
 
1572
- //
1573
- // Expression components
1574
- //
1575
-
1576
1489
  arguments: ($) =>
1577
1490
  seq(choice('(', sym('_call_arguments_context')), commaSep(optional(choice($.expression, $.spread_element))), ')'),
1578
1491
 
@@ -1793,15 +1706,9 @@ module.exports = grammar({
1793
1706
 
1794
1707
  computed_property_name: ($) => seq('[', $.expression, ']'),
1795
1708
 
1796
- _reserved_identifier: ($) =>
1797
- choice(
1798
- sym('_yield_identifier'),
1799
- ...MEMBER_MODIFIERS,
1800
- $._escaped_let,
1801
- ...CONTEXTUAL_KEYWORDS.map((word) =>
1802
- word === 'await' ? choice(sym('_await_keyword'), sym('_await_identifier')) : word
1803
- )
1804
- ),
1709
+ _reserved_identifier: ($) => contextualIdentifier($, false),
1710
+
1711
+ _expression_identifier: ($) => contextualIdentifier($, true),
1805
1712
 
1806
1713
  _semicolon: () =>
1807
1714
  choice(
@@ -1813,6 +1720,118 @@ module.exports = grammar({
1813
1720
  },
1814
1721
  });
1815
1722
 
1723
+ /**
1724
+ * @param {GrammarSymbols<'_primary_atom' | 'subscript_expression' | 'member_expression' | 'call_expression' | 'new_expression' | '_argumentless_new_expression'>} $
1725
+ * @param {{atom?: RuleOrLiteral, subscript?: RuleOrLiteral, member?: RuleOrLiteral, call?: RuleOrLiteral}} [forms]
1726
+ */
1727
+ function leftHandSideExpression($, forms = {}) {
1728
+ return choice(
1729
+ forms.atom ?? $._primary_atom,
1730
+ forms.subscript ?? $.subscript_expression,
1731
+ forms.member ?? $.member_expression,
1732
+ forms.call ?? $.call_expression,
1733
+ $.new_expression,
1734
+ alias($._argumentless_new_expression, $.new_expression)
1735
+ );
1736
+ }
1737
+
1738
+ /**
1739
+ * @param {import('@willbooster/web-tree-sitter/dsl').GrammarSymbols<'_escaped_let'>} $
1740
+ * @param {boolean} heritage
1741
+ */
1742
+ function contextualIdentifier($, heritage) {
1743
+ const yieldWord = heritage ? sym('_class_heritage_yield_identifier') : sym('_yield_identifier');
1744
+ const awaitWord = heritage
1745
+ ? sym('_class_heritage_await_identifier')
1746
+ : choice(sym('_await_keyword'), sym('_await_identifier'));
1747
+ return choice(
1748
+ yieldWord,
1749
+ ...MEMBER_MODIFIERS,
1750
+ $._escaped_let,
1751
+ ...CONTEXTUAL_KEYWORDS.map((word) => (word === 'await' ? awaitWord : word))
1752
+ );
1753
+ }
1754
+
1755
+ /**
1756
+ * @param {GrammarSymbols<'import' | 'arguments' | 'template_string' | 'optional_chain'>} $
1757
+ * @param {RuleOrLiteral} callee
1758
+ * @param {RuleOrLiteral} primary
1759
+ */
1760
+ function callExpression($, callee, primary) {
1761
+ return choice(
1762
+ prec('call', seq(field('function', choice(callee, $.import)), field('arguments', $.arguments))),
1763
+ prec('template_call', seq(field('function', primary), field('arguments', $.template_string))),
1764
+ prec(
1765
+ 'member',
1766
+ seq(field('function', primary), field('optional_chain', $.optional_chain), field('arguments', $.arguments))
1767
+ )
1768
+ );
1769
+ }
1770
+
1771
+ /**
1772
+ * @param {GrammarSymbols<'_jsx_element' | 'parenthesized_expression' | '_identifier' | 'identifier' | 'this' | 'super' | 'number' | 'string' | 'template_string' | 'regex' | 'true' | 'false' | 'null' | 'object' | 'array' | 'function_expression' | 'generator_function' | 'class' | 'meta_property'>} $
1773
+ * @param {RuleOrLiteral} identifier
1774
+ */
1775
+ function primaryAtom($, identifier) {
1776
+ return choice(
1777
+ $._jsx_element,
1778
+ $.parenthesized_expression,
1779
+ $._identifier,
1780
+ alias(sym('_await_yield_identifier'), $.identifier),
1781
+ aliasWithAsyncKeyword(identifier, $.identifier),
1782
+ $.this,
1783
+ $.super,
1784
+ $.number,
1785
+ $.string,
1786
+ $.template_string,
1787
+ $.regex,
1788
+ $.true,
1789
+ $.false,
1790
+ $.null,
1791
+ $.object,
1792
+ $.array,
1793
+ $.function_expression,
1794
+ $.generator_function,
1795
+ $.class,
1796
+ $.meta_property
1797
+ );
1798
+ }
1799
+
1800
+ /**
1801
+ * @param {GrammarSymbols<'import' | 'optional_chain' | 'private_property_identifier' | 'identifier'>} $
1802
+ * @param {RuleOrLiteral} object
1803
+ */
1804
+ function memberExpression($, object) {
1805
+ return prec(
1806
+ 'member',
1807
+ seq(
1808
+ field('object', choice(object, $.import)),
1809
+ choice('.', field('optional_chain', $.optional_chain)),
1810
+ field(
1811
+ 'property',
1812
+ choice($.private_property_identifier, reserved('properties', alias($.identifier, sym('property_identifier'))))
1813
+ )
1814
+ )
1815
+ );
1816
+ }
1817
+
1818
+ /**
1819
+ * @param {GrammarSymbols<'optional_chain' | '_expressions'>} $
1820
+ * @param {RuleOrLiteral} object
1821
+ */
1822
+ function subscriptExpression($, object) {
1823
+ return prec.right(
1824
+ 'member',
1825
+ seq(
1826
+ field('object', object),
1827
+ optional(field('optional_chain', $.optional_chain)),
1828
+ '[',
1829
+ field('index', $._expressions),
1830
+ ']'
1831
+ )
1832
+ );
1833
+ }
1834
+
1816
1835
  /**
1817
1836
  * @param {import('@willbooster/web-tree-sitter/dsl').RuleOrLiteral} rule
1818
1837
  * @param {import('@willbooster/web-tree-sitter/dsl').SymbolRule<string>} symbol
@@ -1822,8 +1841,6 @@ function aliasWithAsyncKeyword(rule, symbol) {
1822
1841
  }
1823
1842
 
1824
1843
  /**
1825
- * Creates a rule to match one or more of the rules separated by a comma
1826
- *
1827
1844
  * @param {import('@willbooster/web-tree-sitter/dsl').RuleOrLiteral} rule
1828
1845
  *
1829
1846
  * @returns {import('@willbooster/web-tree-sitter/dsl').SeqRule}
@@ -1833,8 +1850,6 @@ function commaSep1(rule) {
1833
1850
  }
1834
1851
 
1835
1852
  /**
1836
- * Creates a rule to optionally match one or more of the rules separated by a comma
1837
- *
1838
1853
  * @param {import('@willbooster/web-tree-sitter/dsl').RuleOrLiteral} rule
1839
1854
  *
1840
1855
  * @returns {import('@willbooster/web-tree-sitter/dsl').ChoiceRule}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-javascript",
3
- "version": "16.0.0",
3
+ "version": "17.0.0",
4
4
  "description": "JavaScript grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "incremental",
Binary file
Binary file
package/tree-sitter.json CHANGED
@@ -48,7 +48,7 @@
48
48
  }
49
49
  ],
50
50
  "metadata": {
51
- "version": "16.0.0",
51
+ "version": "17.0.0",
52
52
  "license": "MIT",
53
53
  "description": "JavaScript grammar for tree-sitter",
54
54
  "authors": [