Skip to content

Commit 91e3f3f

Browse files
committed
feat: add using declaration
1 parent cb16a1d commit 91e3f3f

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

grammar.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ module.exports = grammar({
181181
$.class_declaration,
182182
$.lexical_declaration,
183183
$.variable_declaration,
184+
$.using_declaration,
184185
),
185186

186187
//
@@ -286,6 +287,15 @@ module.exports = grammar({
286287
$._semicolon,
287288
),
288289

290+
using_declaration: $ => seq(
291+
field('kind', choice(
292+
'using',
293+
seq('await', 'using'),
294+
)),
295+
commaSep1($.variable_declarator),
296+
$._semicolon,
297+
),
298+
289299
variable_declarator: $ => seq(
290300
field('name', choice($.identifier, $._destructuring_pattern)),
291301
optional($._initializer),
@@ -360,6 +370,17 @@ module.exports = grammar({
360370
)),
361371
optional($._automatic_semicolon),
362372
),
373+
seq(
374+
field('kind', choice(
375+
'using',
376+
seq('await', 'using'),
377+
)),
378+
field('left', choice(
379+
$.identifier,
380+
$._destructuring_pattern,
381+
)),
382+
optional($._automatic_semicolon),
383+
),
363384
),
364385
field('operator', choice('in', 'of')),
365386
field('right', $._expressions),

test/corpus/statements.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,71 @@ var x, y = {}, z;
870870
(variable_declarator
871871
(identifier))))
872872

873+
============================================
874+
Using declarations
875+
============================================
876+
877+
using file = getFile();
878+
await using resource = getResource();
879+
using x = a, y = b;
880+
881+
---
882+
883+
(program
884+
(using_declaration
885+
(variable_declarator
886+
(identifier)
887+
(call_expression
888+
(identifier)
889+
(arguments))))
890+
(using_declaration
891+
(variable_declarator
892+
(identifier)
893+
(call_expression
894+
(identifier)
895+
(arguments))))
896+
(using_declaration
897+
(variable_declarator
898+
(identifier)
899+
(identifier))
900+
(variable_declarator
901+
(identifier)
902+
(identifier))))
903+
904+
============================================
905+
Using declarations in for loops
906+
============================================
907+
908+
for (using item of items) {
909+
process(item);
910+
}
911+
912+
for await (await using resource of resources) {
913+
consume(resource);
914+
}
915+
916+
---
917+
918+
(program
919+
(for_in_statement
920+
(identifier)
921+
(identifier)
922+
(statement_block
923+
(expression_statement
924+
(call_expression
925+
(identifier)
926+
(arguments
927+
(identifier))))))
928+
(for_in_statement
929+
(identifier)
930+
(identifier)
931+
(statement_block
932+
(expression_statement
933+
(call_expression
934+
(identifier)
935+
(arguments
936+
(identifier)))))))
937+
873938
============================================
874939
Comments
875940
============================================

0 commit comments

Comments
 (0)