@willbooster/tree-sitter-javascript 28.0.0 → 30.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/README.md CHANGED
@@ -31,8 +31,7 @@ requires that runtime ABI; the upstream runtime rejects it. This package is test
31
31
  the package. Choose `tree-sitter-javascript.wasm` for the Module goal, or
32
32
  `tree-sitter-javascript_script.wasm` for the Script goal. The source goal comes from the host or file type;
33
33
  `.mjs` uses Module and `.cjs` uses Script, while `.js` depends on the host. For example, `await\nvalue` is one
34
- await expression in a Module and two identifier statements in a Script. Both languages preserve the same node
35
- schema. As with other tree-sitter grammars, parsing does not enforce every ECMAScript early error.
34
+ await expression in a Module and two identifier statements in a Script. Only Module accepts static import and export declarations, and they must appear at the top level. As with other tree-sitter grammars, parsing does not enforce every ECMAScript early error.
36
35
  For the native CLI, select Script with `tree-sitter parse --grammar-path grammars/script`.
37
36
 
38
37
  ```js
package/grammar.js CHANGED
@@ -52,6 +52,8 @@ const RESERVED_WORDS = [
52
52
  'with',
53
53
  ];
54
54
  const NAMED_RESERVED_WORDS = new Set(['this', 'super', 'true', 'false', 'null']);
55
+ const RESERVED_WORD_TOKENS = RESERVED_WORDS.map((word) => (NAMED_RESERVED_WORDS.has(word) ? sym(word) : word));
56
+ const LEXICAL_WORDS = [...RESERVED_WORDS, 'let'];
55
57
 
56
58
  const MEMBER_MODIFIERS = ['get', 'set', 'static'];
57
59
  const CONTEXTUAL_KEYWORDS = ['async', 'await', 'let', 'of', 'using'];
@@ -145,7 +147,8 @@ module.exports = grammar({
145
147
 
146
148
  reserved: {
147
149
  global: () => RESERVED_WORDS,
148
- lexical: ($) => [...RESERVED_WORDS, 'let', $._escaped_let],
150
+ lexical: ($) => [...LEXICAL_WORDS, $._escaped_let],
151
+ lexical_pattern: ($) => [...LEXICAL_WORDS, $._escaped_let, $._binding_await, $._binding_yield],
149
152
  properties: () => [],
150
153
  },
151
154
 
@@ -190,6 +193,11 @@ module.exports = grammar({
190
193
  $._jsx_identifier,
191
194
  $._lhs_expression,
192
195
  $._object_rest_target,
196
+ $._lexical_binding,
197
+ $._lexical_shorthand,
198
+ $._lexical_pattern_contextual,
199
+ $._lexical_pattern_identifier,
200
+ $._lexical_pattern_binding,
193
201
  ],
194
202
 
195
203
  precedences: ($) => [
@@ -259,7 +267,10 @@ module.exports = grammar({
259
267
  word: ($) => $.identifier,
260
268
 
261
269
  rules: {
262
- program: ($) => seq(optional($.hash_bang_line), repeat($._statement)),
270
+ program: ($) => seq(optional($.hash_bang_line), repeat(choice($._statement, $._module_declaration))),
271
+
272
+ _module_declaration: ($) =>
273
+ prec.right(seq(choice($.import_statement, $.export_statement), optional(sym('_statement_boundary')))),
263
274
 
264
275
  _statement: ($) => prec.right(seq($.statement, optional(sym('_statement_boundary')))),
265
276
 
@@ -325,10 +336,7 @@ module.exports = grammar({
325
336
  $.identifier,
326
337
  $.string,
327
338
  // `this`, `super`, `true`, `false`, and `null` are named rules, and a bare string would make them non-terminals.
328
- alias(
329
- choice(...RESERVED_WORDS.map((word) => (NAMED_RESERVED_WORDS.has(word) ? sym(word) : word))),
330
- $.identifier
331
- )
339
+ alias(choice(...RESERVED_WORD_TOKENS), $.identifier)
332
340
  ),
333
341
 
334
342
  declaration: ($) =>
@@ -375,8 +383,6 @@ module.exports = grammar({
375
383
 
376
384
  statement: ($) =>
377
385
  choice(
378
- $.export_statement,
379
- $.import_statement,
380
386
  $.debugger_statement,
381
387
  $.expression_statement,
382
388
  $.declaration,
@@ -436,10 +442,7 @@ module.exports = grammar({
436
442
  ),
437
443
 
438
444
  _lexical_variable_declarator: ($) =>
439
- seq(
440
- field('name', choice(reserved('lexical', $.identifier), alias('of', $.identifier), $._destructuring_pattern)),
441
- choice($._initializer, sym('_line_break_after_binding'))
442
- ),
445
+ seq(field('name', $._lexical_binding), choice($._initializer, sym('_line_break_after_binding'))),
443
446
 
444
447
  _for_variable_declarator: ($) =>
445
448
  seq(
@@ -447,11 +450,7 @@ module.exports = grammar({
447
450
  optional($._initializer)
448
451
  ),
449
452
 
450
- _for_lexical_variable_declarator: ($) =>
451
- seq(
452
- field('name', choice(reserved('lexical', $.identifier), alias('of', $.identifier), $._destructuring_pattern)),
453
- optional($._initializer)
454
- ),
453
+ _for_lexical_variable_declarator: ($) => seq(field('name', $._lexical_binding), optional($._initializer)),
455
454
 
456
455
  statement_block: ($) => prec.right(seq(token(prec(1, '{')), repeat($._statement), '}')),
457
456
 
@@ -539,16 +538,7 @@ module.exports = grammar({
539
538
  field('left', choice($.identifier, alias('of', $.identifier), $._destructuring_pattern)),
540
539
  optional($._initializer)
541
540
  ),
542
- prec.dynamic(
543
- 1,
544
- seq(
545
- field('kind', choice('let', 'const')),
546
- field(
547
- 'left',
548
- choice(reserved('lexical', $.identifier), alias('of', $.identifier), $._destructuring_pattern)
549
- )
550
- )
551
- )
541
+ prec.dynamic(1, seq(field('kind', choice('let', 'const')), field('left', $._lexical_binding)))
552
542
  ),
553
543
  field('operator', choice('in', 'of'))
554
544
  ),
@@ -708,10 +698,7 @@ module.exports = grammar({
708
698
  )
709
699
  );
710
700
  const rest = alias($._object_rest_pattern, $.rest_pattern);
711
- return prec(
712
- 'object',
713
- seq('{', optional(choice(seq(commaSep1(property), optional(seq(',', optional(rest)))), rest)), '}')
714
- );
701
+ return objectPattern(property, rest);
715
702
  },
716
703
 
717
704
  assignment_pattern: ($) =>
@@ -1333,6 +1320,92 @@ module.exports = grammar({
1333
1320
 
1334
1321
  _initializer: ($) => seq('=', field('value', $.expression)),
1335
1322
 
1323
+ _lexical_binding: ($) =>
1324
+ choice(reserved('lexical', $.identifier), alias('of', $.identifier), $._lexical_destructuring_pattern),
1325
+
1326
+ _lexical_destructuring_pattern: ($) =>
1327
+ choice(alias($._lexical_object_pattern, $.object_pattern), alias($._lexical_array_pattern, $.array_pattern)),
1328
+
1329
+ _binding_await: () => 'await',
1330
+
1331
+ _binding_yield: () => 'yield',
1332
+
1333
+ _lexical_pattern_contextual: () =>
1334
+ choice(
1335
+ ...MEMBER_MODIFIERS,
1336
+ ...CONTEXTUAL_KEYWORDS.filter((word) => word !== 'let' && word !== 'await'),
1337
+ sym('_async_keyword')
1338
+ ),
1339
+
1340
+ _lexical_pattern_identifier: ($) =>
1341
+ choice(reserved('lexical_pattern', $.identifier), alias($._lexical_pattern_contextual, $.identifier)),
1342
+
1343
+ _lexical_pattern_binding: ($) => choice($._lexical_pattern_identifier, $._lexical_destructuring_pattern),
1344
+
1345
+ _lexical_object_pattern: ($) =>
1346
+ objectPattern(
1347
+ choice(
1348
+ $._lexical_shorthand,
1349
+ alias($._lexical_pair_pattern, $.pair_pattern),
1350
+ alias($._lexical_object_default, $.object_assignment_pattern)
1351
+ ),
1352
+ alias($._lexical_object_rest, $.rest_pattern)
1353
+ ),
1354
+
1355
+ _lexical_shorthand: ($) =>
1356
+ choice(
1357
+ reserved('lexical_pattern', alias($.identifier, sym('shorthand_property_identifier_pattern'))),
1358
+ alias($._lexical_pattern_contextual, sym('shorthand_property_identifier_pattern'))
1359
+ ),
1360
+
1361
+ // These keys share a lexer state with binding shorthands, so accepting reserved words through the property
1362
+ // word set would also permit them as bound names. The explicit keyword route requires a following colon.
1363
+ _lexical_pattern_property: ($) =>
1364
+ choice(
1365
+ alias(
1366
+ choice(
1367
+ reserved('lexical_pattern', $.identifier),
1368
+ ...RESERVED_WORD_TOKENS,
1369
+ 'export',
1370
+ 'let',
1371
+ $._escaped_let,
1372
+ $._binding_await,
1373
+ $._binding_yield,
1374
+ $._lexical_pattern_contextual
1375
+ ),
1376
+ sym('property_identifier')
1377
+ ),
1378
+ $.private_property_identifier,
1379
+ $.string,
1380
+ $.number,
1381
+ $.computed_property_name
1382
+ ),
1383
+
1384
+ _lexical_pair_pattern: ($) =>
1385
+ seq(
1386
+ field('key', $._lexical_pattern_property),
1387
+ ':',
1388
+ field('value', choice($._lexical_pattern_binding, alias($._lexical_binding_default, $.assignment_pattern)))
1389
+ ),
1390
+
1391
+ _lexical_object_default: ($) => seq(field('left', $._lexical_shorthand), '=', field('right', $.expression)),
1392
+
1393
+ _lexical_array_pattern: ($) => {
1394
+ const element = choice($._lexical_pattern_binding, alias($._lexical_binding_default, $.assignment_pattern));
1395
+ return seq(
1396
+ '[',
1397
+ repeat(seq(optional(element), ',')),
1398
+ optional(choice(element, alias($._lexical_array_rest, $.rest_pattern))),
1399
+ ']'
1400
+ );
1401
+ },
1402
+
1403
+ _lexical_binding_default: ($) => seq(field('left', $._lexical_pattern_binding), '=', field('right', $.expression)),
1404
+
1405
+ _lexical_object_rest: ($) => seq('...', $._lexical_pattern_identifier),
1406
+
1407
+ _lexical_array_rest: ($) => seq('...', $._lexical_pattern_binding),
1408
+
1336
1409
  _destructuring_pattern: ($) => choice($.object_pattern, $.array_pattern),
1337
1410
 
1338
1411
  spread_element: ($) => seq('...', $.expression),
@@ -1735,6 +1808,7 @@ module.exports = grammar({
1735
1808
  aliasWithAsyncKeyword(
1736
1809
  choice(
1737
1810
  $.identifier,
1811
+ 'export',
1738
1812
  sym('_yield_identifier'),
1739
1813
  $._escaped_let,
1740
1814
  ...CONTEXTUAL_KEYWORDS.map((word) =>
@@ -1754,7 +1828,7 @@ module.exports = grammar({
1754
1828
  reserved(
1755
1829
  'properties',
1756
1830
  choice(
1757
- aliasWithAsyncKeyword(choice($.identifier, $._reserved_identifier), sym('property_identifier')),
1831
+ aliasWithAsyncKeyword(choice($.identifier, $._reserved_identifier, 'export'), sym('property_identifier')),
1758
1832
  $.private_property_identifier,
1759
1833
  $.string,
1760
1834
  $.number,
@@ -1878,6 +1952,17 @@ function subscriptExpression($, object) {
1878
1952
  );
1879
1953
  }
1880
1954
 
1955
+ /**
1956
+ * @param {RuleOrLiteral} property
1957
+ * @param {RuleOrLiteral} rest
1958
+ */
1959
+ function objectPattern(property, rest) {
1960
+ return prec(
1961
+ 'object',
1962
+ seq('{', optional(choice(seq(commaSep1(property), optional(seq(',', optional(rest)))), rest)), '}')
1963
+ );
1964
+ }
1965
+
1881
1966
  /**
1882
1967
  * @param {import('@willbooster/web-tree-sitter/dsl').RuleOrLiteral} rule
1883
1968
  * @param {import('@willbooster/web-tree-sitter/dsl').SymbolRule<string>} symbol
@@ -1,6 +1,16 @@
1
1
  // @ts-check
2
2
 
3
- const { grammar } = /** @type {typeof globalThis & import('@willbooster/web-tree-sitter/dsl').DSL} */ (globalThis);
3
+ const { choice, grammar, optional, repeat, seq, sym } =
4
+ /** @type {typeof globalThis & import('@willbooster/web-tree-sitter/dsl').DSL} */ (globalThis);
4
5
 
5
6
  // oxlint-disable-next-line unicorn/prefer-module -- The CLI loads grammar extensions as CommonJS.
6
- module.exports = grammar(require('../../grammar.js'), { name: 'javascript_script', rules: {} });
7
+ module.exports = grammar(require('../../grammar.js'), {
8
+ name: 'javascript_script',
9
+ rules: {
10
+ program: () => seq(optional(sym('hash_bang_line')), repeat(sym('_statement'))),
11
+ _lexical_pattern_contextual: (_, original) => {
12
+ if (!original) throw new Error('Expected the base grammar lexical pattern rule');
13
+ return choice(original, sym('_await_identifier'), sym('_yield_identifier'));
14
+ },
15
+ },
16
+ });
@@ -243,10 +243,6 @@
243
243
  "type": "empty_statement",
244
244
  "named": true
245
245
  },
246
- {
247
- "type": "export_statement",
248
- "named": true
249
- },
250
246
  {
251
247
  "type": "expression_statement",
252
248
  "named": true
@@ -263,10 +259,6 @@
263
259
  "type": "if_statement",
264
260
  "named": true
265
261
  },
266
- {
267
- "type": "import_statement",
268
- "named": true
269
- },
270
262
  {
271
263
  "type": "labeled_statement",
272
264
  "named": true
@@ -1091,109 +1083,6 @@
1091
1083
  "named": true,
1092
1084
  "fields": {}
1093
1085
  },
1094
- {
1095
- "type": "export_clause",
1096
- "named": true,
1097
- "fields": {},
1098
- "children": {
1099
- "multiple": true,
1100
- "required": false,
1101
- "types": [
1102
- {
1103
- "type": "export_specifier",
1104
- "named": true
1105
- }
1106
- ]
1107
- }
1108
- },
1109
- {
1110
- "type": "export_specifier",
1111
- "named": true,
1112
- "fields": {
1113
- "alias": {
1114
- "multiple": false,
1115
- "required": false,
1116
- "types": [
1117
- {
1118
- "type": "identifier",
1119
- "named": true
1120
- },
1121
- {
1122
- "type": "string",
1123
- "named": true
1124
- }
1125
- ]
1126
- },
1127
- "name": {
1128
- "multiple": false,
1129
- "required": true,
1130
- "types": [
1131
- {
1132
- "type": "identifier",
1133
- "named": true
1134
- },
1135
- {
1136
- "type": "string",
1137
- "named": true
1138
- }
1139
- ]
1140
- }
1141
- }
1142
- },
1143
- {
1144
- "type": "export_statement",
1145
- "named": true,
1146
- "fields": {
1147
- "declaration": {
1148
- "multiple": false,
1149
- "required": false,
1150
- "types": [
1151
- {
1152
- "type": "declaration",
1153
- "named": true
1154
- }
1155
- ]
1156
- },
1157
- "source": {
1158
- "multiple": false,
1159
- "required": false,
1160
- "types": [
1161
- {
1162
- "type": "string",
1163
- "named": true
1164
- }
1165
- ]
1166
- },
1167
- "value": {
1168
- "multiple": false,
1169
- "required": false,
1170
- "types": [
1171
- {
1172
- "type": "expression",
1173
- "named": true
1174
- }
1175
- ]
1176
- }
1177
- },
1178
- "children": {
1179
- "multiple": true,
1180
- "required": false,
1181
- "types": [
1182
- {
1183
- "type": "export_clause",
1184
- "named": true
1185
- },
1186
- {
1187
- "type": "import_attribute",
1188
- "named": true
1189
- },
1190
- {
1191
- "type": "namespace_export",
1192
- "named": true
1193
- }
1194
- ]
1195
- }
1196
- },
1197
1086
  {
1198
1087
  "type": "expression_statement",
1199
1088
  "named": true,
@@ -1713,104 +1602,6 @@
1713
1602
  "named": true,
1714
1603
  "fields": {}
1715
1604
  },
1716
- {
1717
- "type": "import_attribute",
1718
- "named": true,
1719
- "fields": {},
1720
- "children": {
1721
- "multiple": false,
1722
- "required": true,
1723
- "types": [
1724
- {
1725
- "type": "object",
1726
- "named": true
1727
- }
1728
- ]
1729
- }
1730
- },
1731
- {
1732
- "type": "import_clause",
1733
- "named": true,
1734
- "fields": {},
1735
- "children": {
1736
- "multiple": true,
1737
- "required": true,
1738
- "types": [
1739
- {
1740
- "type": "identifier",
1741
- "named": true
1742
- },
1743
- {
1744
- "type": "named_imports",
1745
- "named": true
1746
- },
1747
- {
1748
- "type": "namespace_import",
1749
- "named": true
1750
- }
1751
- ]
1752
- }
1753
- },
1754
- {
1755
- "type": "import_specifier",
1756
- "named": true,
1757
- "fields": {
1758
- "alias": {
1759
- "multiple": false,
1760
- "required": false,
1761
- "types": [
1762
- {
1763
- "type": "identifier",
1764
- "named": true
1765
- }
1766
- ]
1767
- },
1768
- "name": {
1769
- "multiple": false,
1770
- "required": true,
1771
- "types": [
1772
- {
1773
- "type": "identifier",
1774
- "named": true
1775
- },
1776
- {
1777
- "type": "string",
1778
- "named": true
1779
- }
1780
- ]
1781
- }
1782
- }
1783
- },
1784
- {
1785
- "type": "import_statement",
1786
- "named": true,
1787
- "fields": {
1788
- "source": {
1789
- "multiple": false,
1790
- "required": true,
1791
- "types": [
1792
- {
1793
- "type": "string",
1794
- "named": true
1795
- }
1796
- ]
1797
- }
1798
- },
1799
- "children": {
1800
- "multiple": true,
1801
- "required": false,
1802
- "types": [
1803
- {
1804
- "type": "import_attribute",
1805
- "named": true
1806
- },
1807
- {
1808
- "type": "import_clause",
1809
- "named": true
1810
- }
1811
- ]
1812
- }
1813
- },
1814
1605
  {
1815
1606
  "type": "jsx_attribute",
1816
1607
  "named": true,
@@ -2203,55 +1994,6 @@
2203
1994
  }
2204
1995
  }
2205
1996
  },
2206
- {
2207
- "type": "named_imports",
2208
- "named": true,
2209
- "fields": {},
2210
- "children": {
2211
- "multiple": true,
2212
- "required": false,
2213
- "types": [
2214
- {
2215
- "type": "import_specifier",
2216
- "named": true
2217
- }
2218
- ]
2219
- }
2220
- },
2221
- {
2222
- "type": "namespace_export",
2223
- "named": true,
2224
- "fields": {},
2225
- "children": {
2226
- "multiple": false,
2227
- "required": true,
2228
- "types": [
2229
- {
2230
- "type": "identifier",
2231
- "named": true
2232
- },
2233
- {
2234
- "type": "string",
2235
- "named": true
2236
- }
2237
- ]
2238
- }
2239
- },
2240
- {
2241
- "type": "namespace_import",
2242
- "named": true,
2243
- "fields": {},
2244
- "children": {
2245
- "multiple": false,
2246
- "required": true,
2247
- "types": [
2248
- {
2249
- "type": "identifier",
2250
- "named": true
2251
- }
2252
- ]
2253
- }
2254
- },
2255
1997
  {
2256
1998
  "type": "new_expression",
2257
1999
  "named": true,
@@ -2493,6 +2235,11 @@
2493
2235
  ]
2494
2236
  }
2495
2237
  },
2238
+ {
2239
+ "type": "property_identifier",
2240
+ "named": true,
2241
+ "fields": {}
2242
+ },
2496
2243
  {
2497
2244
  "type": "regex",
2498
2245
  "named": true,
@@ -3359,10 +3106,6 @@
3359
3106
  "type": "`",
3360
3107
  "named": false
3361
3108
  },
3362
- {
3363
- "type": "as",
3364
- "named": false
3365
- },
3366
3109
  {
3367
3110
  "type": "async",
3368
3111
  "named": false
@@ -3424,10 +3167,6 @@
3424
3167
  "type": "escape_sequence",
3425
3168
  "named": true
3426
3169
  },
3427
- {
3428
- "type": "export",
3429
- "named": false
3430
- },
3431
3170
  {
3432
3171
  "type": "extends",
3433
3172
  "named": false
@@ -3444,10 +3183,6 @@
3444
3183
  "type": "for",
3445
3184
  "named": false
3446
3185
  },
3447
- {
3448
- "type": "from",
3449
- "named": false
3450
- },
3451
3186
  {
3452
3187
  "type": "function",
3453
3188
  "named": false
@@ -3525,10 +3260,6 @@
3525
3260
  "type": "private_property_identifier",
3526
3261
  "named": true
3527
3262
  },
3528
- {
3529
- "type": "property_identifier",
3530
- "named": true
3531
- },
3532
3263
  {
3533
3264
  "type": "regex_flags",
3534
3265
  "named": true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-javascript",
3
- "version": "28.0.0",
3
+ "version": "30.0.0",
4
4
  "description": "JavaScript grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "javascript",
@@ -243,10 +243,6 @@
243
243
  "type": "empty_statement",
244
244
  "named": true
245
245
  },
246
- {
247
- "type": "export_statement",
248
- "named": true
249
- },
250
246
  {
251
247
  "type": "expression_statement",
252
248
  "named": true
@@ -263,10 +259,6 @@
263
259
  "type": "if_statement",
264
260
  "named": true
265
261
  },
266
- {
267
- "type": "import_statement",
268
- "named": true
269
- },
270
262
  {
271
263
  "type": "labeled_statement",
272
264
  "named": true
@@ -2482,10 +2474,18 @@
2482
2474
  "multiple": true,
2483
2475
  "required": false,
2484
2476
  "types": [
2477
+ {
2478
+ "type": "export_statement",
2479
+ "named": true
2480
+ },
2485
2481
  {
2486
2482
  "type": "hash_bang_line",
2487
2483
  "named": true
2488
2484
  },
2485
+ {
2486
+ "type": "import_statement",
2487
+ "named": true
2488
+ },
2489
2489
  {
2490
2490
  "type": "statement",
2491
2491
  "named": true
@@ -2493,6 +2493,11 @@
2493
2493
  ]
2494
2494
  }
2495
2495
  },
2496
+ {
2497
+ "type": "property_identifier",
2498
+ "named": true,
2499
+ "fields": {}
2500
+ },
2496
2501
  {
2497
2502
  "type": "regex",
2498
2503
  "named": true,
@@ -3525,10 +3530,6 @@
3525
3530
  "type": "private_property_identifier",
3526
3531
  "named": true
3527
3532
  },
3528
- {
3529
- "type": "property_identifier",
3530
- "named": true
3531
- },
3532
3533
  {
3533
3534
  "type": "regex_flags",
3534
3535
  "named": true
Binary file
Binary file
package/tree-sitter.json CHANGED
@@ -34,7 +34,7 @@
34
34
  }
35
35
  ],
36
36
  "metadata": {
37
- "version": "28.0.0",
37
+ "version": "30.0.0",
38
38
  "license": "MIT",
39
39
  "description": "JavaScript grammar for tree-sitter",
40
40
  "authors": [