Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90b050e

Browse files
feat: support explicit resource management in one-var (#19941)
* feat: support explicit resource management in `one-var` * chore: reverted unwanted formatting * docs: apply suggestions from code review Co-authored-by: Milos Djermanovic <[email protected]> * fix: correct autofix * chore: apply suggestions from code review Co-authored-by: Milos Djermanovic <[email protected]> * fix: update one-var rule type --------- Co-authored-by: Milos Djermanovic <[email protected]>
1 parent 27fa865 commit 90b050e

File tree

4 files changed

+396
-48
lines changed

4 files changed

+396
-48
lines changed

docs/src/rules/one-var.md

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ title: one-var
33
rule_type: suggestion
44
---
55

6-
7-
8-
Variables can be declared at any point in JavaScript code using `var`, `let`, or `const`. There are many styles and preferences related to the declaration of variables, and one of those is deciding on how many variable declarations should be allowed in a single function.
6+
Variables can be declared at any point in JavaScript code using `var`, `let`, `const`, `using`, or `await using`. There are many styles and preferences related to the declaration of variables, and one of those is deciding on how many variable declarations should be allowed in a single function.
97

108
There are two schools of thought in this regard:
119

@@ -31,7 +29,7 @@ The single-declaration school of thought is based in pre-ECMAScript 6 behaviors,
3129

3230
## Rule Details
3331

34-
This rule enforces variables to be declared either together or separately per function ( for `var`) or block (for `let` and `const`) scope.
32+
This rule enforces variables to be declared either together or separately per function ( for `var`) or block (for `let`, `const`, `using`, and `await using`) scope.
3533

3634
## Options
3735

@@ -54,6 +52,12 @@ Object option:
5452
* `"const": "always"` requires one `const` declaration per block
5553
* `"const": "never"` requires multiple `const` declarations per block
5654
* `"const": "consecutive"` requires consecutive `const` declarations to be a single declaration
55+
* `"using": "always"` requires one `using` declaration per block
56+
* `"using": "never"` requires multiple `using` declarations per block
57+
* `"using": "consecutive"` requires consecutive `using` declarations to be a single declaration
58+
* `"awaitUsing": "always"` requires one `await using` declaration per block
59+
* `"awaitUsing": "never"` requires multiple `await using` declarations per block
60+
* `"awaitUsing": "consecutive"` requires consecutive `await using` declarations to be a single declaration
5761
* `"separateRequires": true` enforces `requires` to be separate from declarations
5862

5963
Alternate object option:
@@ -339,14 +343,14 @@ class C {
339343

340344
:::
341345

342-
### var, let, and const
346+
### var, let, const, using, and awaitUsing
343347

344-
Examples of **incorrect** code for this rule with the `{ var: "always", let: "never", const: "never" }` option:
348+
Examples of **incorrect** code for this rule with the `{ var: "always", let: "never", const: "never", using: "never", awaitUsing: "never" }` option:
345349

346350
::: incorrect
347351

348352
```js
349-
/*eslint one-var: ["error", { var: "always", let: "never", const: "never" }]*/
353+
/*eslint one-var: ["error", { var: "always", let: "never", const: "never", using: "never", awaitUsing: "never" }]*/
350354

351355
function foo1() {
352356
var bar;
@@ -361,16 +365,23 @@ function foo2() {
361365
let qux,
362366
norf;
363367
}
368+
369+
async function foo3() {
370+
using bar = 1,
371+
baz = 2;
372+
await using qux = 3,
373+
norf = 4;
374+
}
364375
```
365376

366377
:::
367378

368-
Examples of **correct** code for this rule with the `{ var: "always", let: "never", const: "never" }` option:
379+
Examples of **correct** code for this rule with the `{ var: "always", let: "never", const: "never", using: "never", awaitUsing: "never" }` option:
369380

370381
::: correct
371382

372383
```js
373-
/*eslint one-var: ["error", { var: "always", let: "never", const: "never" }]*/
384+
/*eslint one-var: ["error", { var: "always", let: "never", const: "never", using: "never", awaitUsing: "never" }]*/
374385

375386
function foo1() {
376387
var bar,
@@ -385,6 +396,13 @@ function foo2() {
385396
let qux;
386397
let norf;
387398
}
399+
400+
async function foo3() {
401+
using bar = 1;
402+
using baz = 2;
403+
await using qux = 3;
404+
await using norf = 4;
405+
}
388406
```
389407

390408
:::
@@ -411,17 +429,23 @@ Examples of **correct** code for this rule with the `{ var: "never" }` option:
411429
```js
412430
/*eslint one-var: ["error", { var: "never" }]*/
413431

414-
function foo() {
432+
async function foo() {
415433
var bar;
416434
var baz;
417435

418-
// `const` and `let` declarations are ignored if they are not specified
436+
// `const`, `let`, `using` and `await using` declarations are ignored if they are not specified
419437
const foobar = 1;
420438
const foobaz = 2;
421439
const barfoo = 1, bazfoo = 2;
422440
let qux;
423441
let norf;
424442
let fooqux, foonorf;
443+
using foobarfoo = 1;
444+
using foobazfoo = 2;
445+
using bazbarfoo = 1, bazfoobar = 2;
446+
await using foobarbaz = 1;
447+
await using foobazqux = 2;
448+
await using bazbarqux = 1, bazfooqux = 2;
425449
}
426450
```
427451

@@ -464,12 +488,12 @@ var foo = require("foo"),
464488

465489
:::
466490

467-
Examples of **incorrect** code for this rule with the `{ var: "never", let: "consecutive", const: "consecutive" }` option:
491+
Examples of **incorrect** code for this rule with the `{ var: "never", let: "consecutive", const: "consecutive", using: "consecutive", awaitUsing: "consecutive" }` option:
468492

469493
::: incorrect
470494

471495
```js
472-
/*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive" }]*/
496+
/*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive", using: "consecutive", awaitUsing: "consecutive" }]*/
473497

474498
function foo1() {
475499
let a,
@@ -488,16 +512,34 @@ function foo2() {
488512
var d,
489513
e;
490514
}
515+
516+
function foo3() {
517+
using a = 1,
518+
b = 2;
519+
using c = 3;
520+
521+
var d,
522+
e;
523+
}
524+
525+
async function foo4() {
526+
await using a = 1,
527+
b = 2;
528+
await using c = 3;
529+
530+
var d,
531+
e;
532+
}
491533
```
492534

493535
:::
494536

495-
Examples of **correct** code for this rule with the `{ var: "never", let: "consecutive", const: "consecutive" }` option:
537+
Examples of **correct** code for this rule with the `{ var: "never", let: "consecutive", const: "consecutive", using: "consecutive", awaitUsing: "consecutive" }` option:
496538

497539
::: correct
498540

499541
```js
500-
/*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive" }]*/
542+
/*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive", using: "consecutive", awaitUsing: "consecutive" }]*/
501543

502544
function foo1() {
503545
let a,
@@ -518,6 +560,26 @@ function foo2() {
518560

519561
const e = 3;
520562
}
563+
564+
function foo3() {
565+
using a = 1,
566+
b = 2;
567+
568+
var c;
569+
var d;
570+
571+
using e = 3;
572+
}
573+
574+
async function foo4() {
575+
await using a = 1,
576+
b = 2;
577+
578+
var c;
579+
var d;
580+
581+
await using e = 3;
582+
}
521583
```
522584

523585
:::
@@ -544,13 +606,19 @@ Examples of **correct** code for this rule with the `{ var: "consecutive" }` opt
544606
```js
545607
/*eslint one-var: ["error", { var: "consecutive" }]*/
546608

547-
function foo() {
609+
async function foo() {
548610
var a,
549611
b;
550-
const c = 1; // `const` and `let` declarations are ignored if they are not specified
612+
613+
// `const`, `let`, `using`, and `await using` declarations are ignored if they are not specified
614+
const c = 1;
551615
const d = 2;
552616
let e;
553617
let f;
618+
using g = 3;
619+
using h = 4;
620+
await using i = 5;
621+
await using j = 6;
554622
}
555623
```
556624

0 commit comments

Comments
 (0)