Skip to content

Commit 1290824

Browse files
Skn0ttCopilot
andauthored
Highlight using / await using declarations in JS & TS (#4079)
Co-authored-by: Skn0tt <skn0tt@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9234b55 commit 1290824

3 files changed

Lines changed: 181 additions & 0 deletions

File tree

src/languages/javascript.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export default {
2929
},
3030
],
3131
'keyword': [
32+
{
33+
pattern:
34+
/(^|[^.]|\.\.\.\s*)\busing(?=\s+(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*\s*(?:=(?!=)|\bof\b))/,
35+
lookbehind: true,
36+
},
3237
{
3338
pattern:
3439
/(^|[^.]|\.\.\.\s*)\b(?:as|assert(?=\s*\{)|export|from(?=\s*(?:['"]|$))|import)\b/,

tests/languages/javascript/keyword_feature.test

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,23 @@ async (a,b,c) => {}
5353
import {} from "foo"
5454
import {} from 'foo'
5555
class { get foo(){} set baz(){} get [value](){} }
56+
using file = openFile();
57+
await using conn = getConn();
58+
using x = a, y = b;
59+
for (using x of arr) {}
60+
for (await using y of arr) {}
5661

5762
// import assertion
5863
import json from "./foo.json" assert { type: "json" };
5964

6065
// variables, not keywords
6166

6267
const { async, from, to } = bar;
68+
const using = 1;
69+
obj.using;
70+
using();
71+
using {x} = obj;
72+
import using from "foo";
6373
promise.catch(foo).finally(bar);
6474
assert.equal(foo, bar);
6575

@@ -199,6 +209,54 @@ assert.equal(foo, bar);
199209
["punctuation", "}"],
200210
["punctuation", "}"],
201211

212+
["keyword", "using"],
213+
" file ",
214+
["operator", "="],
215+
["function", "openFile"],
216+
["punctuation", "("],
217+
["punctuation", ")"],
218+
["punctuation", ";"],
219+
220+
["keyword", "await"],
221+
["keyword", "using"],
222+
" conn ",
223+
["operator", "="],
224+
["function", "getConn"],
225+
["punctuation", "("],
226+
["punctuation", ")"],
227+
["punctuation", ";"],
228+
229+
["keyword", "using"],
230+
" x ",
231+
["operator", "="],
232+
" a",
233+
["punctuation", ","],
234+
" y ",
235+
["operator", "="],
236+
" b",
237+
["punctuation", ";"],
238+
239+
["keyword", "for"],
240+
["punctuation", "("],
241+
["keyword", "using"],
242+
" x ",
243+
["keyword", "of"],
244+
" arr",
245+
["punctuation", ")"],
246+
["punctuation", "{"],
247+
["punctuation", "}"],
248+
249+
["keyword", "for"],
250+
["punctuation", "("],
251+
["keyword", "await"],
252+
["keyword", "using"],
253+
" y ",
254+
["keyword", "of"],
255+
" arr",
256+
["punctuation", ")"],
257+
["punctuation", "{"],
258+
["punctuation", "}"],
259+
202260
["comment", "// import assertion"],
203261

204262
["keyword", "import"],
@@ -227,6 +285,36 @@ assert.equal(foo, bar);
227285
" bar",
228286
["punctuation", ";"],
229287

288+
["keyword", "const"],
289+
" using ",
290+
["operator", "="],
291+
["number", "1"],
292+
["punctuation", ";"],
293+
294+
"\r\nobj",
295+
["punctuation", "."],
296+
"using",
297+
["punctuation", ";"],
298+
299+
["function", "using"],
300+
["punctuation", "("],
301+
["punctuation", ")"],
302+
["punctuation", ";"],
303+
304+
"\r\nusing ",
305+
["punctuation", "{"],
306+
"x",
307+
["punctuation", "}"],
308+
["operator", "="],
309+
" obj",
310+
["punctuation", ";"],
311+
312+
["keyword", "import"],
313+
" using ",
314+
["keyword", "from"],
315+
["string", "\"foo\""],
316+
["punctuation", ";"],
317+
230318
"\r\npromise",
231319
["punctuation", "."],
232320
["function", "catch"],

tests/languages/typescript/keyword_feature.test

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,20 @@ async (a,b,c) => {}
5555
import {} from "foo"
5656
import {} from 'foo'
5757
class { get foo(){} set baz(){} get [value](){} }
58+
using file = openFile();
59+
await using conn = getConn();
60+
using x = a, y = b;
61+
for (using x of arr) {}
62+
for (await using y of arr) {}
5863

5964
// variables, not keywords
6065

6166
const { async, from, to } = bar;
67+
const using = 1;
68+
obj.using;
69+
using();
70+
using {x} = obj;
71+
import using from "foo";
6272
promise.catch(foo).finally(bar);
6373

6474
// TypeScript keywords
@@ -220,6 +230,54 @@ import type *, {}
220230
["punctuation", "}"],
221231
["punctuation", "}"],
222232

233+
["keyword", "using"],
234+
" file ",
235+
["operator", "="],
236+
["function", "openFile"],
237+
["punctuation", "("],
238+
["punctuation", ")"],
239+
["punctuation", ";"],
240+
241+
["keyword", "await"],
242+
["keyword", "using"],
243+
" conn ",
244+
["operator", "="],
245+
["function", "getConn"],
246+
["punctuation", "("],
247+
["punctuation", ")"],
248+
["punctuation", ";"],
249+
250+
["keyword", "using"],
251+
" x ",
252+
["operator", "="],
253+
" a",
254+
["punctuation", ","],
255+
" y ",
256+
["operator", "="],
257+
" b",
258+
["punctuation", ";"],
259+
260+
["keyword", "for"],
261+
["punctuation", "("],
262+
["keyword", "using"],
263+
" x ",
264+
["keyword", "of"],
265+
" arr",
266+
["punctuation", ")"],
267+
["punctuation", "{"],
268+
["punctuation", "}"],
269+
270+
["keyword", "for"],
271+
["punctuation", "("],
272+
["keyword", "await"],
273+
["keyword", "using"],
274+
" y ",
275+
["keyword", "of"],
276+
" arr",
277+
["punctuation", ")"],
278+
["punctuation", "{"],
279+
["punctuation", "}"],
280+
223281
["comment", "// variables, not keywords"],
224282

225283
["keyword", "const"],
@@ -234,6 +292,36 @@ import type *, {}
234292
" bar",
235293
["punctuation", ";"],
236294

295+
["keyword", "const"],
296+
" using ",
297+
["operator", "="],
298+
["number", "1"],
299+
["punctuation", ";"],
300+
301+
"\r\nobj",
302+
["punctuation", "."],
303+
"using",
304+
["punctuation", ";"],
305+
306+
["function", "using"],
307+
["punctuation", "("],
308+
["punctuation", ")"],
309+
["punctuation", ";"],
310+
311+
"\r\nusing ",
312+
["punctuation", "{"],
313+
"x",
314+
["punctuation", "}"],
315+
["operator", "="],
316+
" obj",
317+
["punctuation", ";"],
318+
319+
["keyword", "import"],
320+
" using ",
321+
["keyword", "from"],
322+
["string", "\"foo\""],
323+
["punctuation", ";"],
324+
237325
"\r\npromise",
238326
["punctuation", "."],
239327
["function", "catch"],

0 commit comments

Comments
 (0)