@willbooster/tree-sitter-javascript 7.0.0 → 7.1.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
@@ -17,11 +17,16 @@ This fork fixes parsing bugs and raises conformance with [ECMAScript (ECMA-262)]
17
17
 
18
18
  ## Usage
19
19
 
20
- The npm package ships `tree-sitter-javascript.wasm` for
20
+ The npm package ships two languages for
21
21
  [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter), which runs in Node.js, Bun,
22
22
  browsers, and Cloudflare Workers. The parser carries the ABI number of WillBooster/tree-sitter (`0x57420006`), so it
23
23
  loads only with that runtime, version 7.0.5 or newer within major 7; the upstream runtime rejects it. In Node.js and Bun, load it from
24
- the package:
24
+ the package. Choose `tree-sitter-javascript.wasm` for the Module goal, or
25
+ `tree-sitter-javascript_script.wasm` for the Script goal. The source goal comes from the host or file type;
26
+ `.mjs` uses Module and `.cjs` uses Script, while `.js` depends on the host. For example, `await\nvalue` is one
27
+ await expression in a Module and two identifier statements in a Script. Both languages preserve the same node
28
+ schema and query names. As with other tree-sitter grammars, parsing does not enforce every ECMAScript early error.
29
+ For the native CLI, select Script with `tree-sitter parse --grammar-path grammars/script`.
25
30
 
26
31
  ```js
27
32
  import { fileURLToPath } from 'node:url';
@@ -34,7 +39,9 @@ parser.setLanguage(await Language.load(wasmPath));
34
39
  const tree = parser.parse('const App = () => <p>Hello</p>;\n');
35
40
  ```
36
41
 
37
- In browsers, let your bundler serve both `.wasm` files and pass their URLs:
42
+ In browsers, let your bundler serve the runtime and the selected language `.wasm` files, then pass their URLs.
43
+ For the Script goal, replace the language import below with
44
+ `@willbooster/tree-sitter-javascript/tree-sitter-javascript_script.wasm?url`:
38
45
 
39
46
  ```js
40
47
  import { Language, Parser } from '@willbooster/web-tree-sitter';
@@ -46,8 +53,9 @@ const parser = new Parser();
46
53
  parser.setLanguage(await Language.load(javascriptUrl));
47
54
  ```
48
55
 
49
- In Cloudflare Workers, which do not allow compiling Wasm at run time, import both `.wasm` files as modules, with or
50
- without Node.js compatibility:
56
+ In Cloudflare Workers, which do not allow compiling Wasm at run time, import the runtime and the selected language
57
+ `.wasm` files as modules, with or without Node.js compatibility. For the Script goal, replace the language import
58
+ below with `@willbooster/tree-sitter-javascript/tree-sitter-javascript_script.wasm`:
51
59
 
52
60
  ```js
53
61
  import { Language, Parser } from '@willbooster/web-tree-sitter';
@@ -75,6 +83,7 @@ tree-sitter-javascript = { package = "willbooster-tree-sitter-javascript", versi
75
83
  ```rust
76
84
  let mut parser = tree_sitter::Parser::new();
77
85
  parser.set_language(&tree_sitter_javascript::LANGUAGE.into())?;
86
+ // Use LANGUAGE_SCRIPT for the Script goal.
78
87
  ```
79
88
 
80
89
  ## Development
@@ -96,7 +105,7 @@ the download fails or the release has no binary that runs here.
96
105
  `bun run generate` records a fresh generation profile from the applicable `test/corpus` cases and Git-tracked
97
106
  files in `examples/`, then generates compact parser tables. The parser also embeds metadata from `tree-sitter.json`. After changing a grammar,
98
107
  `tree-sitter.json`, a corpus case, or a tracked example,
99
- regenerate and commit `src/`. Stage added or removed examples with `git add -A examples` before generation so the profile uses the intended file list.
108
+ regenerate and commit the affected grammar's `src/` directory. Stage added or removed examples with `git add -A examples` before generation so the profile uses the intended file list.
100
109
  Profiles in `.tmp/generation-profiles/` are temporary and must not be committed. `bun run build-wasm`, `bun run build/ci`,
101
110
  and the release build regenerate the parsers before compiling them.
102
111
 
package/grammar.js CHANGED
@@ -118,6 +118,12 @@ module.exports = grammar({
118
118
  sym('_function_context_end'),
119
119
  sym('_arrow_context_end'),
120
120
  sym('_await_identifier'),
121
+ sym('_generator_function_start'),
122
+ sym('_async_generator_function_start'),
123
+ sym('_yield_keyword'),
124
+ sym('_yield_identifier'),
125
+ sym('_function_signature_start'),
126
+ sym('_async_function_signature_start'),
121
127
  ],
122
128
 
123
129
  extras: ($) => [$.comment, sym('html_comment'), /[\s\p{Zs}\uFEFF\u2028\u2029\u2060\u200B]/u],
@@ -646,7 +652,10 @@ module.exports = grammar({
646
652
 
647
653
  yield_expression: ($) =>
648
654
  prec.right(
649
- seq('yield', choice(seq('*', $.expression), optional($.expression), sym('_line_break_ends_statement')))
655
+ seq(
656
+ alias(sym('_yield_keyword'), 'yield'),
657
+ choice(seq('*', $.expression), optional($.expression), sym('_line_break_ends_statement'))
658
+ )
650
659
  ),
651
660
 
652
661
  object: ($) =>
@@ -840,8 +849,7 @@ module.exports = grammar({
840
849
  ...(async ? ['async'] : []),
841
850
  'function',
842
851
  field('name', optional($.identifier)),
843
- $._call_signature,
844
- contextBody(async, field('body', $.statement_block))
852
+ functionScope(async, seq($._call_signature, field('body', $.statement_block)))
845
853
  )
846
854
  )
847
855
  ),
@@ -860,8 +868,7 @@ module.exports = grammar({
860
868
  ),
861
869
  seq(...(async ? ['async'] : []), 'function', field('name', $.identifier))
862
870
  ),
863
- $._call_signature,
864
- contextBody(async, field('body', $.statement_block))
871
+ functionScope(async, seq($._call_signature, field('body', $.statement_block)))
865
872
  )
866
873
  )
867
874
  ),
@@ -875,8 +882,7 @@ module.exports = grammar({
875
882
  'function',
876
883
  '*',
877
884
  field('name', optional($.identifier)),
878
- $._call_signature,
879
- contextBody(async, field('body', $.statement_block))
885
+ functionScope(async, seq($._call_signature, field('body', $.statement_block)), 'generator_function')
880
886
  )
881
887
  )
882
888
  ),
@@ -896,8 +902,7 @@ module.exports = grammar({
896
902
  ),
897
903
  seq(...(async ? ['async'] : []), 'function', '*', field('name', $.identifier))
898
904
  ),
899
- $._call_signature,
900
- contextBody(async, field('body', $.statement_block))
905
+ functionScope(async, seq($._call_signature, field('body', $.statement_block)), 'generator_function')
901
906
  )
902
907
  )
903
908
  ),
@@ -964,8 +969,7 @@ module.exports = grammar({
964
969
  ...(async ? ['async'] : []),
965
970
  'function',
966
971
  field('name', optional($.identifier)),
967
- $._call_signature,
968
- contextBody(async, field('body', $.statement_block))
972
+ functionScope(async, seq($._call_signature, field('body', $.statement_block)))
969
973
  )
970
974
  )
971
975
  ),
@@ -980,8 +984,7 @@ module.exports = grammar({
980
984
  'function',
981
985
  '*',
982
986
  field('name', optional($.identifier)),
983
- $._call_signature,
984
- contextBody(async, field('body', $.statement_block))
987
+ functionScope(async, seq($._call_signature, field('body', $.statement_block)), 'generator_function')
985
988
  )
986
989
  )
987
990
  ),
@@ -1452,28 +1455,36 @@ module.exports = grammar({
1452
1455
  seq(
1453
1456
  optional('static'),
1454
1457
  choice(
1455
- seq(field('property', $._field_name), optional(choice($._initializer, sym('_line_break_after_field')))),
1458
+ seq(
1459
+ field('property', $._field_name),
1460
+ optional(choice($._field_initializer, sym('_line_break_after_field')))
1461
+ ),
1456
1462
  // A line break after `get` or `set` continues a getter or setter. Allowing both sentinels here tells
1457
1463
  // the scanner that a `*` on the next line cannot continue the member, unlike after `static`.
1458
1464
  seq(
1459
1465
  field('property', alias(choice('get', 'set'), sym('property_identifier'))),
1460
- optional(choice($._initializer, sym('_line_break_after_modifier'), sym('_line_break_after_field')))
1466
+ optional(
1467
+ choice($._field_initializer, sym('_line_break_after_modifier'), sym('_line_break_after_field'))
1468
+ )
1461
1469
  )
1462
1470
  )
1463
1471
  ),
1464
1472
  // A line break after a leading `static` continues a static member; after `static static`, it ends the field.
1465
1473
  seq(
1466
1474
  field('property', alias('static', sym('property_identifier'))),
1467
- optional(choice($._initializer, sym('_line_break_after_modifier')))
1475
+ optional(choice($._field_initializer, sym('_line_break_after_modifier')))
1468
1476
  ),
1469
1477
  seq(
1470
1478
  'static',
1471
1479
  field('property', alias('static', sym('property_identifier'))),
1472
- optional(choice($._initializer, sym('_line_break_after_field')))
1480
+ optional(choice($._field_initializer, sym('_line_break_after_field')))
1473
1481
  )
1474
1482
  )
1475
1483
  ),
1476
1484
 
1485
+ _field_initializer: ($) =>
1486
+ seq('=', sym('_sync_function_start'), field('value', $.expression), sym('_arrow_context_end')),
1487
+
1477
1488
  formal_parameters: ($) => seq('(', optional(seq(commaSep1($._formal_parameter), optional(','))), ')'),
1478
1489
 
1479
1490
  class_static_block: ($) => seq('static', field('body', alias($._class_member_body, $.statement_block))),
@@ -1487,14 +1498,22 @@ module.exports = grammar({
1487
1498
 
1488
1499
  method_definition: ($) =>
1489
1500
  functionContext((async) =>
1490
- seq(
1491
- repeat(field('decorator', $.decorator)),
1492
- optional('static'),
1493
- ...(async ? ['async'] : []),
1494
- optional(choice('get', 'set', '*')),
1495
- field('name', $._property_name),
1496
- field('parameters', $.formal_parameters),
1497
- contextBody(async, field('body', alias($._class_member_body, $.statement_block)))
1501
+ choice(
1502
+ ...[false, true].map((generator) =>
1503
+ seq(
1504
+ repeat(field('decorator', $.decorator)),
1505
+ optional('static'),
1506
+ ...(async ? ['async'] : []),
1507
+ ...(generator ? ['*'] : [optional(choice('get', 'set'))]),
1508
+ field('name', $._property_name),
1509
+ field('parameters', $.formal_parameters),
1510
+ functionScope(
1511
+ async,
1512
+ field('body', alias($._class_member_body, $.statement_block)),
1513
+ generator ? 'generator_function' : 'function'
1514
+ )
1515
+ )
1516
+ )
1498
1517
  )
1499
1518
  ),
1500
1519
 
@@ -1515,6 +1534,7 @@ module.exports = grammar({
1515
1534
  alias(
1516
1535
  choice(
1517
1536
  $.identifier,
1537
+ sym('_yield_identifier'),
1518
1538
  $._escaped_let,
1519
1539
  ...CONTEXTUAL_KEYWORDS.map((word) =>
1520
1540
  word === 'await' ? choice(sym('_await_keyword'), sym('_await_identifier')) : word
@@ -1545,6 +1565,7 @@ module.exports = grammar({
1545
1565
 
1546
1566
  _reserved_identifier: ($) =>
1547
1567
  choice(
1568
+ sym('_yield_identifier'),
1548
1569
  ...MEMBER_MODIFIERS,
1549
1570
  $._escaped_let,
1550
1571
  ...CONTEXTUAL_KEYWORDS.map((word) =>
@@ -1590,8 +1611,10 @@ function functionContext(makeRule) {
1590
1611
  }
1591
1612
  /**
1592
1613
  * @param {boolean} async
1593
- * @param {import('@willbooster/web-tree-sitter/dsl').RuleOrLiteral} body
1614
+ * @param {import('@willbooster/web-tree-sitter/dsl').RuleOrLiteral} content
1615
+ * @param {'function_signature' | 'generator_function' | 'function'} [kind]
1594
1616
  */
1595
- function contextBody(async, body) {
1596
- return seq(sym(async ? '_async_function_start' : '_sync_function_start'), body, sym('_function_context_end'));
1617
+ function functionScope(async, content, kind = 'function_signature') {
1618
+ const name = kind === 'function' && !async ? 'sync_function' : `${async ? 'async_' : ''}${kind}`;
1619
+ return seq(sym(`_${name}_start`), content, sym('_function_context_end'));
1597
1620
  }
@@ -0,0 +1,6 @@
1
+ // @ts-check
2
+
3
+ const { grammar } = /** @type {typeof globalThis & import('@willbooster/web-tree-sitter/dsl').DSL} */ (globalThis);
4
+
5
+ // 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: {} });