@willbooster/tree-sitter-typescript 1.2.6 → 1.2.7

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.
@@ -242,7 +242,7 @@ module.exports = function defineGrammar(dialect) {
242
242
  prec(
243
243
  'generic_call',
244
244
  seq(
245
- field('function', choice($.primary_expression, $.new_expression)),
245
+ field('function', $.primary_expression),
246
246
  field('type_arguments', $.type_arguments),
247
247
  field('arguments', choice($.arguments, $.template_string))
248
248
  )
@@ -251,7 +251,7 @@ module.exports = function defineGrammar(dialect) {
251
251
  prec(
252
252
  'template_call',
253
253
  seq(
254
- field('function', choice($.primary_expression, $.new_expression)),
254
+ field('function', choice($.primary_expression, alias($._argumentless_new_expression, $.new_expression))),
255
255
  field('arguments', $.template_string)
256
256
  )
257
257
  ),
@@ -266,17 +266,29 @@ module.exports = function defineGrammar(dialect) {
266
266
  )
267
267
  ),
268
268
 
269
+ // As in ECMAScript, a `new` with arguments is a member-level (primary) expression and one without them is not, so a
270
+ // member access, a call, or a further argument list binds to the nearest `new` with arguments: `new new A().b`
271
+ // constructs `new A().b`, and `new new A()(2)` constructs `new A()`.
269
272
  new_expression: ($) =>
270
- prec.right(
273
+ prec(
271
274
  'new',
272
275
  seq(
273
276
  'new',
274
- field('constructor', $.primary_expression),
277
+ field('constructor', $._new_constructor),
275
278
  field('type_arguments', optional($.type_arguments)),
276
- field('arguments', optional($.arguments))
279
+ field('arguments', $.arguments)
277
280
  )
278
281
  ),
279
282
 
283
+ _argumentless_new_expression: ($) =>
284
+ prec.right(
285
+ 'new',
286
+ seq('new', field('constructor', $._new_constructor), field('type_arguments', optional($.type_arguments)))
287
+ ),
288
+
289
+ _new_constructor: ($) =>
290
+ prec('new', choice($.primary_expression, alias($._argumentless_new_expression, $.new_expression))),
291
+
280
292
  assignment_expression: ($) =>
281
293
  prec.right(
282
294
  'assign',
@@ -287,18 +299,26 @@ module.exports = function defineGrammar(dialect) {
287
299
 
288
300
  _lhs_expression: ($, previous) => choice(previous, $.non_null_expression),
289
301
 
290
- primary_expression: ($, previous) => choice(previous, $.non_null_expression),
302
+ primary_expression: ($, previous) => choice(previous, $.non_null_expression, $.new_expression),
291
303
 
292
304
  // If the dialect is regular typescript, we exclude JSX expressions and
293
305
  // include type assertions. If the dialect is TSX, we do the opposite.
294
306
  expression: ($, previous) => {
295
- const choices = [$.as_expression, $.satisfies_expression, $.instantiation_expression, $.internal_module];
307
+ const choices = [
308
+ $.as_expression,
309
+ $.satisfies_expression,
310
+ $.instantiation_expression,
311
+ $.internal_module,
312
+ alias($._argumentless_new_expression, $.new_expression),
313
+ ];
314
+ // A `new` with arguments is a primary expression (see new_expression).
315
+ const members = previous.members.filter((member) => member.name !== 'new_expression');
296
316
 
297
317
  if (dialect === 'typescript') {
298
318
  choices.push($.type_assertion);
299
- choices.push(...previous.members.filter((member) => member.name !== '_jsx_element'));
319
+ choices.push(...members.filter((member) => member.name !== '_jsx_element'));
300
320
  } else if (dialect === 'tsx') {
301
- choices.push(...previous.members);
321
+ choices.push(...members);
302
322
  } else {
303
323
  throw new Error(`Unknown dialect ${dialect}`);
304
324
  }
@@ -419,7 +439,8 @@ module.exports = function defineGrammar(dialect) {
419
439
  ')'
420
440
  ),
421
441
 
422
- non_null_expression: ($) => prec.left('unary', seq($.expression, '!')),
442
+ // TypeScript parses a postfix `!` as part of a member access chain, so `new A!()` constructs `A!`.
443
+ non_null_expression: ($) => prec('member', seq($.primary_expression, '!')),
423
444
 
424
445
  variable_declarator: ($) =>
425
446
  choice(
@@ -565,13 +586,7 @@ module.exports = function defineGrammar(dialect) {
565
586
  satisfies_expression: ($) => prec.left('binary', seq($.expression, 'satisfies', $.type)),
566
587
 
567
588
  instantiation_expression: ($) =>
568
- prec.dynamic(
569
- -1,
570
- prec(
571
- 'instantiation',
572
- seq(choice($.primary_expression, $.new_expression), field('type_arguments', $.type_arguments))
573
- )
574
- ),
589
+ prec.dynamic(-1, prec('instantiation', seq($.primary_expression, field('type_arguments', $.type_arguments)))),
575
590
 
576
591
  class_heritage: ($) => choice(seq($.extends_clause, optional($.implements_clause)), $.implements_clause),
577
592
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-typescript",
3
- "version": "1.2.6",
3
+ "version": "1.2.7",
4
4
  "description": "TypeScript and TSX grammars for tree-sitter",
5
5
  "keywords": [
6
6
  "incremental",
Binary file
Binary file
package/tree-sitter.json CHANGED
@@ -83,7 +83,7 @@
83
83
  }
84
84
  ],
85
85
  "metadata": {
86
- "version": "1.2.6",
86
+ "version": "1.2.7",
87
87
  "license": "MIT",
88
88
  "description": "TypeScript and TSX grammars for tree-sitter",
89
89
  "authors": [
@@ -221,6 +221,10 @@
221
221
  "type": "meta_property",
222
222
  "named": true
223
223
  },
224
+ {
225
+ "type": "new_expression",
226
+ "named": true
227
+ },
224
228
  {
225
229
  "type": "non_null_expression",
226
230
  "named": true
@@ -3110,10 +3114,6 @@
3110
3114
  "multiple": false,
3111
3115
  "required": false,
3112
3116
  "types": [
3113
- {
3114
- "type": "new_expression",
3115
- "named": true
3116
- },
3117
3117
  {
3118
3118
  "type": "primary_expression",
3119
3119
  "named": true
@@ -4057,7 +4057,7 @@
4057
4057
  "required": true,
4058
4058
  "types": [
4059
4059
  {
4060
- "type": "expression",
4060
+ "type": "primary_expression",
4061
4061
  "named": true
4062
4062
  }
4063
4063
  ]
@@ -217,6 +217,10 @@
217
217
  "type": "meta_property",
218
218
  "named": true
219
219
  },
220
+ {
221
+ "type": "new_expression",
222
+ "named": true
223
+ },
220
224
  {
221
225
  "type": "non_null_expression",
222
226
  "named": true
@@ -3106,10 +3110,6 @@
3106
3110
  "multiple": false,
3107
3111
  "required": false,
3108
3112
  "types": [
3109
- {
3110
- "type": "new_expression",
3111
- "named": true
3112
- },
3113
3113
  {
3114
3114
  "type": "primary_expression",
3115
3115
  "named": true
@@ -3808,7 +3808,7 @@
3808
3808
  "required": true,
3809
3809
  "types": [
3810
3810
  {
3811
- "type": "expression",
3811
+ "type": "primary_expression",
3812
3812
  "named": true
3813
3813
  }
3814
3814
  ]