@willbooster/tree-sitter-javascript 29.0.0 → 30.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 +111 -28
- package/grammars/script/grammar.js +5 -1
- package/grammars/script/src/node-types.json +5 -4
- package/package.json +1 -1
- package/src/node-types.json +5 -4
- package/tree-sitter-javascript.wasm +0 -0
- package/tree-sitter-javascript_script.wasm +0 -0
- package/tree-sitter.json +1 -1
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: ($) => [...
|
|
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: ($) => [
|
|
@@ -328,10 +336,7 @@ module.exports = grammar({
|
|
|
328
336
|
$.identifier,
|
|
329
337
|
$.string,
|
|
330
338
|
// `this`, `super`, `true`, `false`, and `null` are named rules, and a bare string would make them non-terminals.
|
|
331
|
-
alias(
|
|
332
|
-
choice(...RESERVED_WORDS.map((word) => (NAMED_RESERVED_WORDS.has(word) ? sym(word) : word))),
|
|
333
|
-
$.identifier
|
|
334
|
-
)
|
|
339
|
+
alias(choice(...RESERVED_WORD_TOKENS), $.identifier)
|
|
335
340
|
),
|
|
336
341
|
|
|
337
342
|
declaration: ($) =>
|
|
@@ -437,10 +442,7 @@ module.exports = grammar({
|
|
|
437
442
|
),
|
|
438
443
|
|
|
439
444
|
_lexical_variable_declarator: ($) =>
|
|
440
|
-
seq(
|
|
441
|
-
field('name', choice(reserved('lexical', $.identifier), alias('of', $.identifier), $._destructuring_pattern)),
|
|
442
|
-
choice($._initializer, sym('_line_break_after_binding'))
|
|
443
|
-
),
|
|
445
|
+
seq(field('name', $._lexical_binding), choice($._initializer, sym('_line_break_after_binding'))),
|
|
444
446
|
|
|
445
447
|
_for_variable_declarator: ($) =>
|
|
446
448
|
seq(
|
|
@@ -448,11 +450,7 @@ module.exports = grammar({
|
|
|
448
450
|
optional($._initializer)
|
|
449
451
|
),
|
|
450
452
|
|
|
451
|
-
_for_lexical_variable_declarator: ($) =>
|
|
452
|
-
seq(
|
|
453
|
-
field('name', choice(reserved('lexical', $.identifier), alias('of', $.identifier), $._destructuring_pattern)),
|
|
454
|
-
optional($._initializer)
|
|
455
|
-
),
|
|
453
|
+
_for_lexical_variable_declarator: ($) => seq(field('name', $._lexical_binding), optional($._initializer)),
|
|
456
454
|
|
|
457
455
|
statement_block: ($) => prec.right(seq(token(prec(1, '{')), repeat($._statement), '}')),
|
|
458
456
|
|
|
@@ -540,16 +538,7 @@ module.exports = grammar({
|
|
|
540
538
|
field('left', choice($.identifier, alias('of', $.identifier), $._destructuring_pattern)),
|
|
541
539
|
optional($._initializer)
|
|
542
540
|
),
|
|
543
|
-
prec.dynamic(
|
|
544
|
-
1,
|
|
545
|
-
seq(
|
|
546
|
-
field('kind', choice('let', 'const')),
|
|
547
|
-
field(
|
|
548
|
-
'left',
|
|
549
|
-
choice(reserved('lexical', $.identifier), alias('of', $.identifier), $._destructuring_pattern)
|
|
550
|
-
)
|
|
551
|
-
)
|
|
552
|
-
)
|
|
541
|
+
prec.dynamic(1, seq(field('kind', choice('let', 'const')), field('left', $._lexical_binding)))
|
|
553
542
|
),
|
|
554
543
|
field('operator', choice('in', 'of'))
|
|
555
544
|
),
|
|
@@ -709,10 +698,7 @@ module.exports = grammar({
|
|
|
709
698
|
)
|
|
710
699
|
);
|
|
711
700
|
const rest = alias($._object_rest_pattern, $.rest_pattern);
|
|
712
|
-
return
|
|
713
|
-
'object',
|
|
714
|
-
seq('{', optional(choice(seq(commaSep1(property), optional(seq(',', optional(rest)))), rest)), '}')
|
|
715
|
-
);
|
|
701
|
+
return objectPattern(property, rest);
|
|
716
702
|
},
|
|
717
703
|
|
|
718
704
|
assignment_pattern: ($) =>
|
|
@@ -1334,6 +1320,92 @@ module.exports = grammar({
|
|
|
1334
1320
|
|
|
1335
1321
|
_initializer: ($) => seq('=', field('value', $.expression)),
|
|
1336
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
|
+
|
|
1337
1409
|
_destructuring_pattern: ($) => choice($.object_pattern, $.array_pattern),
|
|
1338
1410
|
|
|
1339
1411
|
spread_element: ($) => seq('...', $.expression),
|
|
@@ -1880,6 +1952,17 @@ function subscriptExpression($, object) {
|
|
|
1880
1952
|
);
|
|
1881
1953
|
}
|
|
1882
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
|
+
|
|
1883
1966
|
/**
|
|
1884
1967
|
* @param {import('@willbooster/web-tree-sitter/dsl').RuleOrLiteral} rule
|
|
1885
1968
|
* @param {import('@willbooster/web-tree-sitter/dsl').SymbolRule<string>} symbol
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
-
const { grammar, optional, repeat, seq, sym } =
|
|
3
|
+
const { choice, grammar, optional, repeat, seq, sym } =
|
|
4
4
|
/** @type {typeof globalThis & import('@willbooster/web-tree-sitter/dsl').DSL} */ (globalThis);
|
|
5
5
|
|
|
6
6
|
// oxlint-disable-next-line unicorn/prefer-module -- The CLI loads grammar extensions as CommonJS.
|
|
@@ -8,5 +8,9 @@ module.exports = grammar(require('../../grammar.js'), {
|
|
|
8
8
|
name: 'javascript_script',
|
|
9
9
|
rules: {
|
|
10
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
|
+
},
|
|
11
15
|
},
|
|
12
16
|
});
|
|
@@ -2235,6 +2235,11 @@
|
|
|
2235
2235
|
]
|
|
2236
2236
|
}
|
|
2237
2237
|
},
|
|
2238
|
+
{
|
|
2239
|
+
"type": "property_identifier",
|
|
2240
|
+
"named": true,
|
|
2241
|
+
"fields": {}
|
|
2242
|
+
},
|
|
2238
2243
|
{
|
|
2239
2244
|
"type": "regex",
|
|
2240
2245
|
"named": true,
|
|
@@ -3255,10 +3260,6 @@
|
|
|
3255
3260
|
"type": "private_property_identifier",
|
|
3256
3261
|
"named": true
|
|
3257
3262
|
},
|
|
3258
|
-
{
|
|
3259
|
-
"type": "property_identifier",
|
|
3260
|
-
"named": true
|
|
3261
|
-
},
|
|
3262
3263
|
{
|
|
3263
3264
|
"type": "regex_flags",
|
|
3264
3265
|
"named": true
|
package/package.json
CHANGED
package/src/node-types.json
CHANGED
|
@@ -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
|