@willbooster/tree-sitter-javascript 1.0.10 → 1.0.12

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
@@ -48,6 +48,10 @@ const RESERVED_WORDS = [
48
48
  ];
49
49
  const NAMED_RESERVED_WORDS = new Set(['this', 'super', 'true', 'false', 'null']);
50
50
 
51
+ // Words that are keywords only in some positions and identifiers elsewhere.
52
+ const MEMBER_MODIFIERS = ['get', 'set', 'static'];
53
+ const CONTEXTUAL_KEYWORDS = ['async', 'await', 'export', 'let', 'using'];
54
+
51
55
  // oxlint-disable-next-line unicorn/prefer-module -- This package is CommonJS, so tree-sitter loads grammar.js as CommonJS.
52
56
  module.exports = grammar({
53
57
  name: 'javascript',
@@ -69,6 +73,14 @@ module.exports = grammar({
69
73
  // _arrow_function_block_continuation, since the arrow function may be, e.g., an argument.
70
74
  $._arrow_function_block_end,
71
75
  $._arrow_function_block_continuation,
76
+ // Sentinels that the scanner never emits: the grammar allows each only where a line break has a fixed meaning, so
77
+ // the scanner can tell from the valid symbols how to treat one there (see LineBreakRule in src/scanner.c). Each is an
78
+ // alternative to what follows its position rather than an optional token before it, since an optional token would
79
+ // duplicate the parse states after it.
80
+ $._line_break_ends_statement,
81
+ $._line_break_after_binding,
82
+ $._line_break_after_field,
83
+ $._line_break_after_modifier,
72
84
  ],
73
85
 
74
86
  extras: ($) => [$.comment, $.html_comment, /[\s\p{Zs}\uFEFF\u2028\u2029\u2060\u200B]/u],
@@ -149,7 +161,6 @@ module.exports = grammar({
149
161
  [$.labeled_statement, $._property_name],
150
162
  [$.computed_property_name, $.array],
151
163
  [$.binary_expression, $._initializer],
152
- [$.class_static_block, $._property_name],
153
164
  ],
154
165
 
155
166
  word: ($) => $.identifier,
@@ -304,7 +315,7 @@ module.exports = grammar({
304
315
  variable_declarator: ($) =>
305
316
  seq(
306
317
  field('name', choice($.identifier, alias('of', $.identifier), $._destructuring_pattern)),
307
- optional($._initializer)
318
+ optional(choice($._initializer, $._line_break_after_binding))
308
319
  ),
309
320
 
310
321
  statement_block: ($) => prec.right(seq('{', repeat($.statement), '}', optional($._automatic_semicolon))),
@@ -417,14 +428,23 @@ module.exports = grammar({
417
428
  with_statement: ($) => seq('with', field('object', $.parenthesized_expression), field('body', $.statement)),
418
429
 
419
430
  break_statement: ($) =>
420
- seq('break', field('label', optional(alias($.identifier, $.statement_identifier))), $._semicolon),
431
+ seq(
432
+ 'break',
433
+ field('label', optional(alias($.identifier, $.statement_identifier))),
434
+ choice($._semicolon, $._line_break_ends_statement)
435
+ ),
421
436
 
422
437
  continue_statement: ($) =>
423
- seq('continue', field('label', optional(alias($.identifier, $.statement_identifier))), $._semicolon),
438
+ seq(
439
+ 'continue',
440
+ field('label', optional(alias($.identifier, $.statement_identifier))),
441
+ choice($._semicolon, $._line_break_ends_statement)
442
+ ),
424
443
 
425
- debugger_statement: ($) => seq('debugger', $._semicolon),
444
+ debugger_statement: ($) => seq('debugger', choice($._semicolon, $._line_break_ends_statement)),
426
445
 
427
- return_statement: ($) => seq('return', optional($._expressions), $._semicolon),
446
+ return_statement: ($) =>
447
+ seq('return', choice(seq(optional($._expressions), $._semicolon), $._line_break_ends_statement)),
428
448
 
429
449
  throw_statement: ($) => seq('throw', $._expressions, $._semicolon),
430
450
 
@@ -507,7 +527,8 @@ module.exports = grammar({
507
527
  $.call_expression
508
528
  ),
509
529
 
510
- yield_expression: ($) => prec.right(seq('yield', choice(seq('*', $.expression), optional($.expression)))),
530
+ yield_expression: ($) =>
531
+ prec.right(seq('yield', choice(seq('*', $.expression), optional($.expression), $._line_break_ends_statement))),
511
532
 
512
533
  object: ($) =>
513
534
  prec(
@@ -1128,14 +1149,35 @@ module.exports = grammar({
1128
1149
  field_definition: ($) =>
1129
1150
  seq(
1130
1151
  repeat(field('decorator', $.decorator)),
1131
- optional('static'),
1132
- field('property', $._property_name),
1133
- optional($._initializer)
1152
+ choice(
1153
+ seq(
1154
+ optional('static'),
1155
+ choice(
1156
+ seq(field('property', $._field_name), optional(choice($._initializer, $._line_break_after_field))),
1157
+ // A line break after `get` or `set` continues a getter or setter. Allowing both sentinels here tells
1158
+ // the scanner that a `*` on the next line cannot continue the member, unlike after `static`.
1159
+ seq(
1160
+ field('property', alias(choice('get', 'set'), $.property_identifier)),
1161
+ optional(choice($._initializer, $._line_break_after_modifier, $._line_break_after_field))
1162
+ )
1163
+ )
1164
+ ),
1165
+ // A line break after a leading `static` continues a static member; after `static static`, it ends the field.
1166
+ seq(
1167
+ field('property', alias('static', $.property_identifier)),
1168
+ optional(choice($._initializer, $._line_break_after_modifier))
1169
+ ),
1170
+ seq(
1171
+ 'static',
1172
+ field('property', alias('static', $.property_identifier)),
1173
+ optional(choice($._initializer, $._line_break_after_field))
1174
+ )
1175
+ )
1134
1176
  ),
1135
1177
 
1136
1178
  formal_parameters: ($) => seq('(', optional(seq(commaSep1($._formal_parameter), optional(','))), ')'),
1137
1179
 
1138
- class_static_block: ($) => seq('static', optional($._automatic_semicolon), field('body', $.statement_block)),
1180
+ class_static_block: ($) => seq('static', field('body', alias($._class_member_body, $.statement_block))),
1139
1181
 
1140
1182
  // This negative dynamic precedence ensures that during error recovery,
1141
1183
  // unfinished constructs are generally treated as literal expressions,
@@ -1147,19 +1189,36 @@ module.exports = grammar({
1147
1189
  method_definition: ($) =>
1148
1190
  seq(
1149
1191
  repeat(field('decorator', $.decorator)),
1150
- optional(choice('static', alias(token(seq('static', /\s+/, 'get', /\s*[\n\r\u2028\u2029]/)), 'static get'))),
1192
+ optional('static'),
1151
1193
  optional('async'),
1152
1194
  optional(choice('get', 'set', '*')),
1153
1195
  field('name', $._property_name),
1154
1196
  field('parameters', $.formal_parameters),
1155
- field('body', $.statement_block)
1197
+ field('body', alias($._class_member_body, $.statement_block))
1156
1198
  ),
1157
1199
 
1200
+ // The body of a method or a static block, a rule of its own rather than statement_block: sharing it let tree-sitter
1201
+ // merge the state after the body's `}` with the one after a function expression's `}`, where `in`, `instanceof`,
1202
+ // and `extends` are keywords, so a class member with one of those names could not follow.
1203
+ _class_member_body: ($) => seq('{', repeat($.statement), '}'),
1204
+
1158
1205
  pair: ($) => seq(field('key', $._property_name), ':', field('value', $.expression)),
1159
1206
 
1160
1207
  pair_pattern: ($) =>
1161
1208
  seq(field('key', $._property_name), ':', field('value', choice($.pattern, $.assignment_pattern))),
1162
1209
 
1210
+ _field_name: ($) =>
1211
+ reserved(
1212
+ 'properties',
1213
+ choice(
1214
+ alias(choice($.identifier, ...CONTEXTUAL_KEYWORDS), $.property_identifier),
1215
+ $.private_property_identifier,
1216
+ $.string,
1217
+ $.number,
1218
+ $.computed_property_name
1219
+ )
1220
+ ),
1221
+
1163
1222
  _property_name: ($) =>
1164
1223
  reserved(
1165
1224
  'properties',
@@ -1174,7 +1233,7 @@ module.exports = grammar({
1174
1233
 
1175
1234
  computed_property_name: ($) => seq('[', $.expression, ']'),
1176
1235
 
1177
- _reserved_identifier: () => choice('get', 'set', 'async', 'await', 'static', 'export', 'let', 'using'),
1236
+ _reserved_identifier: () => choice(...MEMBER_MODIFIERS, ...CONTEXTUAL_KEYWORDS),
1178
1237
 
1179
1238
  _semicolon: ($) => choice($._automatic_semicolon, ';'),
1180
1239
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-javascript",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "JavaScript grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "incremental",
@@ -3515,10 +3515,6 @@
3515
3515
  "type": "static",
3516
3516
  "named": false
3517
3517
  },
3518
- {
3519
- "type": "static get",
3520
- "named": false
3521
- },
3522
3518
  {
3523
3519
  "type": "string_fragment",
3524
3520
  "named": true
Binary file
package/tree-sitter.json CHANGED
@@ -16,7 +16,7 @@
16
16
  }
17
17
  ],
18
18
  "metadata": {
19
- "version": "1.0.10",
19
+ "version": "1.0.12",
20
20
  "license": "MIT",
21
21
  "description": "JavaScript grammar for tree-sitter",
22
22
  "authors": [