Skip to content

Commit 4682cdc

Browse files
authored
feat: support explicit resource management in no-undef-init (#19894)
1 parent 5848216 commit 4682cdc

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

docs/src/rules/no-undef-init.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ let bar;
5454

5555
:::
5656

57-
Please note that this rule does not check `const` declarations, destructuring patterns, function parameters, and class fields.
57+
Please note that this rule does not check `const` declarations, `using` declarations, `await using` declarations, destructuring patterns, function parameters, and class fields.
5858

5959
Examples of additional **correct** code for this rule:
6060

@@ -65,6 +65,10 @@ Examples of additional **correct** code for this rule:
6565

6666
const foo = undefined;
6767

68+
using foo1 = undefined;
69+
70+
await using foo2 = undefined;
71+
6872
let { bar = undefined } = baz;
6973

7074
[quux = undefined] = quuux;
@@ -159,7 +163,7 @@ Another such case is when a variable is redeclared using `var`. For example:
159163
function foo() {
160164
var x = 1;
161165
console.log(x); // output: 1
162-
166+
163167
var x;
164168
console.log(x); // output: 1
165169

lib/rules/no-undef-init.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@
55

66
"use strict";
77

8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
812
const astUtils = require("./utils/ast-utils");
913

14+
//------------------------------------------------------------------------------
15+
// Helpers
16+
//------------------------------------------------------------------------------
17+
18+
const CONSTANT_BINDINGS = new Set(["const", "using", "await using"]);
19+
1020
//------------------------------------------------------------------------------
1121
// Rule Definition
1222
//------------------------------------------------------------------------------
@@ -49,7 +59,7 @@ module.exports = {
4959

5060
if (
5161
init === "undefined" &&
52-
node.parent.kind !== "const" &&
62+
!CONSTANT_BINDINGS.has(node.parent.kind) &&
5363
!shadowed
5464
) {
5565
context.report({

tests/lib/rules/no-undef-init.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ ruleTester.run("no-undef-init", rule, {
2222
valid: [
2323
"var a;",
2424
{ code: "const foo = undefined", languageOptions: { ecmaVersion: 6 } },
25+
{
26+
code: "using foo = undefined",
27+
languageOptions: { ecmaVersion: 2026 },
28+
},
29+
{
30+
code: "await using foo = undefined",
31+
languageOptions: { ecmaVersion: 2026 },
32+
},
2533
"var undefined = 5; var foo = undefined;",
2634

2735
// doesn't apply to class fields

0 commit comments

Comments
 (0)